[IR] Change representation of getelementptr inrange (#84341)
As part of the migration to ptradd (https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699), we need to change the representation of the `inrange` attribute, which is used for vtable splitting. Currently, inrange is specified as follows: ``` getelementptr inbounds ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, inrange i32 1, i64 2) ``` The `inrange` is placed on a GEP index, and all accesses must be "in range" of that index. The new representation is as follows: ``` getelementptr inbounds inrange(-16, 16) ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, i32 1, i64 2) ``` This specifies which offsets are "in range" of the GEP result. The new representation will continue working when canonicalizing to ptradd representation: ``` getelementptr inbounds inrange(-16, 16) (i8, ptr @vt, i64 48) ``` The inrange offsets are relative to the return value of the GEP. An alternative design could make them relative to the source pointer instead. The result-relative format was chosen on the off-chance that we want to extend support to non-constant GEPs in the future, in which case this variant is more expressive. This implementation "upgrades" the old inrange representation in bitcode by simply dropping it. This is a very niche feature, and I don't think trying to upgrade it is worthwhile. Let me know if you disagree.
This commit is contained in:
1 parent
1f63a56ced
commit
0f46e31cfb
70 files changed
+511
-341
No files matched your search
@@ -520,25 +520,31 @@ public:
|
||||
struct ExtraInfo {
|
||||
uint8_t Opcode;
|
||||
uint8_t Flags;
|
||||
unsigned Extra;
|
||||
Type *SrcElemTy;
|
||||
unsigned BlockAddressBB = 0;
|
||||
Type *SrcElemTy = nullptr;
|
||||
std::optional<ConstantRange> InRange;
|
||||
|
||||
ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, unsigned Extra = 0,
|
||||
Type *SrcElemTy = nullptr)
|
||||
: Opcode(Opcode), Flags(Flags), Extra(Extra), SrcElemTy(SrcElemTy) {}
|
||||
ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, Type *SrcElemTy = nullptr,
|
||||
std::optional<ConstantRange> InRange = std::nullopt)
|
||||
: Opcode(Opcode), Flags(Flags), SrcElemTy(SrcElemTy),
|
||||
InRange(std::move(InRange)) {}
|
||||
|
||||
ExtraInfo(uint8_t Opcode, uint8_t Flags, unsigned BlockAddressBB)
|
||||
: Opcode(Opcode), Flags(Flags), BlockAddressBB(BlockAddressBB) {}
|
||||
};
|
||||
|
||||
uint8_t Opcode;
|
||||
uint8_t Flags;
|
||||
unsigned NumOperands;
|
||||
unsigned Extra; // GEP inrange index or blockaddress BB id.
|
||||
unsigned BlockAddressBB;
|
||||
Type *SrcElemTy; // GEP source element type.
|
||||
std::optional<ConstantRange> InRange; // GEP inrange attribute.
|
||||
|
||||
private:
|
||||
BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs)
|
||||
: Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags),
|
||||
NumOperands(OpIDs.size()), Extra(Info.Extra),
|
||||
SrcElemTy(Info.SrcElemTy) {
|
||||
NumOperands(OpIDs.size()), BlockAddressBB(Info.BlockAddressBB),
|
||||
SrcElemTy(Info.SrcElemTy), InRange(Info.InRange) {
|
||||
std::uninitialized_copy(OpIDs.begin(), OpIDs.end(),
|
||||
getTrailingObjects<unsigned>());
|
||||
}
|
||||
@@ -560,11 +566,9 @@ public:
|
||||
return ArrayRef(getTrailingObjects<unsigned>(), NumOperands);
|
||||
}
|
||||
|
||||
std::optional<unsigned> getInRangeIndex() const {
|
||||
std::optional<ConstantRange> getInRange() const {
|
||||
assert(Opcode == Instruction::GetElementPtr);
|
||||
if (Extra == (unsigned)-1)
|
||||
return std::nullopt;
|
||||
return Extra;
|
||||
return InRange;
|
||||
}
|
||||
|
||||
const char *getOpcodeName() const {
|
||||
@@ -1559,7 +1563,7 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
|
||||
// If the function is already parsed we can insert the block address
|
||||
// right away.
|
||||
BasicBlock *BB;
|
||||
unsigned BBID = BC->Extra;
|
||||
unsigned BBID = BC->BlockAddressBB;
|
||||
if (!BBID)
|
||||
// Invalid reference to entry block.
|
||||
return error("Invalid ID");
|
||||
@@ -1602,7 +1606,7 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
|
||||
case Instruction::GetElementPtr:
|
||||
C = ConstantExpr::getGetElementPtr(BC->SrcElemTy, ConstOps[0],
|
||||
ArrayRef(ConstOps).drop_front(),
|
||||
BC->Flags, BC->getInRangeIndex());
|
||||
BC->Flags, BC->getInRange());
|
||||
break;
|
||||
case Instruction::ExtractElement:
|
||||
C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]);
|
||||
@@ -3308,22 +3312,34 @@ Error BitcodeReader::parseConstants() {
|
||||
}
|
||||
case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
|
||||
case bitc::CST_CODE_CE_GEP: // [ty, n x operands]
|
||||
case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x
|
||||
// operands]
|
||||
case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD: // [ty, flags, n x
|
||||
// operands]
|
||||
case bitc::CST_CODE_CE_GEP_WITH_INRANGE: { // [ty, flags, start, end, n x
|
||||
// operands]
|
||||
if (Record.size() < 2)
|
||||
return error("Constant GEP record must have at least two elements");
|
||||
unsigned OpNum = 0;
|
||||
Type *PointeeType = nullptr;
|
||||
if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX ||
|
||||
Record.size() % 2)
|
||||
if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD ||
|
||||
BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE || Record.size() % 2)
|
||||
PointeeType = getTypeByID(Record[OpNum++]);
|
||||
|
||||
bool InBounds = false;
|
||||
std::optional<unsigned> InRangeIndex;
|
||||
if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) {
|
||||
std::optional<ConstantRange> InRange;
|
||||
if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD) {
|
||||
uint64_t Op = Record[OpNum++];
|
||||
InBounds = Op & 1;
|
||||
InRangeIndex = Op >> 1;
|
||||
unsigned InRangeIndex = Op >> 1;
|
||||
// "Upgrade" inrange by dropping it. The feature is too niche to
|
||||
// bother.
|
||||
(void)InRangeIndex;
|
||||
} else if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE) {
|
||||
uint64_t Op = Record[OpNum++];
|
||||
InBounds = Op & 1;
|
||||
Expected<ConstantRange> MaybeInRange = readConstantRange(Record, OpNum);
|
||||
if (!MaybeInRange)
|
||||
return MaybeInRange.takeError();
|
||||
InRange = MaybeInRange.get();
|
||||
} else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
|
||||
InBounds = true;
|
||||
|
||||
@@ -3356,10 +3372,9 @@ Error BitcodeReader::parseConstants() {
|
||||
return error("Missing element type for old-style constant GEP");
|
||||
}
|
||||
|
||||
V = BitcodeConstant::create(Alloc, CurTy,
|
||||
{Instruction::GetElementPtr, InBounds,
|
||||
InRangeIndex.value_or(-1), PointeeType},
|
||||
Elts);
|
||||
V = BitcodeConstant::create(
|
||||
Alloc, CurTy,
|
||||
{Instruction::GetElementPtr, InBounds, PointeeType, InRange}, Elts);
|
||||
break;
|
||||
}
|
||||
case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
|
||||
|
||||
Reference in new issue
Block a user