Skip to content

Commit 7c937df

Browse files
authored
[CodeGen] Forbid passing a PointerType to MVT::getVT and EVT::getEVT (#92671)
There is the expectation throughout CodeGen that, for types representing "real" values, the MVT or EVT is self-contained. However, MVT::iPTR is challenging, because it has no address space, and even if it did, there often is no DataLayout immediately accessible to determine what actually is the underlying type. Historically it was documented as being TableGen-only, but that was lost in 631bfdb's conversion to using the generated defines. Let's preserve that intent by not allowing it to originate through accidental calls to get(E)VT with a PointerType. If you need to support that, be sure to use something like TargetLowering's getValueType, which takes a DataLayout and can map pointers to their concrete MVTs. Whilst here, reintroduce documentation about these value types being TableGen-only.
1 parent cfa9769 commit 7c937df

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

llvm/include/llvm/CodeGen/ValueTypes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,10 @@ namespace llvm {
488488
Type *getTypeForEVT(LLVMContext &Context) const;
489489

490490
/// Return the value type corresponding to the specified type.
491-
/// This returns all pointers as iPTR. If HandleUnknown is true, unknown
492-
/// types are returned as Other, otherwise they are invalid.
491+
/// If HandleUnknown is true, unknown types are returned as Other,
492+
/// otherwise they are invalid.
493+
/// NB: This includes pointer types, which require a DataLayout to convert
494+
/// to a concrete value type.
493495
static EVT getEVT(Type *Ty, bool HandleUnknown = false);
494496

495497
intptr_t getRawBits() const {

llvm/include/llvm/CodeGen/ValueTypes.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,23 @@ def MetadataVT : ValueType<0, 249> { // Metadata
296296
def iPTRAny : VTAny<250>;
297297

298298
// Pseudo valuetype to represent "vector of any size"
299+
// Should only be used in TableGen.
299300
def vAny : VTAny<251>;
300301

301302
// Pseudo valuetype to represent "float of any format"
303+
// Should only be used in TableGen.
302304
def fAny : VTAny<252>;
303305

304306
// Pseudo valuetype to represent "integer of any bit width"
307+
// Should only be used in TableGen.
305308
def iAny : VTAny<253>;
306309

307310
// Pseudo valuetype mapped to the current pointer size.
311+
// Should only be used in TableGen.
308312
def iPTR : ValueType<0, 254>;
309313

310314
// Pseudo valuetype to represent "any type of any size".
315+
// Should only be used in TableGen.
311316
def Any : VTAny<255>;
312317

313318
} // end defset ValueTypes

llvm/include/llvm/CodeGenTypes/MachineValueType.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,11 @@ namespace llvm {
476476
return getVectorVT(VT, EC.getKnownMinValue());
477477
}
478478

479-
/// Return the value type corresponding to the specified type. This returns
480-
/// all pointers as iPTR. If HandleUnknown is true, unknown types are
481-
/// returned as Other, otherwise they are invalid.
479+
/// Return the value type corresponding to the specified type.
480+
/// If HandleUnknown is true, unknown types are returned as Other,
481+
/// otherwise they are invalid.
482+
/// NB: This includes pointer types, which require a DataLayout to convert
483+
/// to a concrete value type.
482484
static MVT getVT(Type *Ty, bool HandleUnknown = false);
483485

484486
public:

llvm/lib/CodeGen/ValueTypes.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,11 @@ Type *EVT::getTypeForEVT(LLVMContext &Context) const {
579579
// clang-format on
580580
}
581581

582-
/// Return the value type corresponding to the specified type. This returns all
583-
/// pointers as MVT::iPTR. If HandleUnknown is true, unknown types are returned
584-
/// as Other, otherwise they are invalid.
582+
/// Return the value type corresponding to the specified type.
583+
/// If HandleUnknown is true, unknown types are returned as Other, otherwise
584+
/// they are invalid.
585+
/// NB: This includes pointer types, which require a DataLayout to convert
586+
/// to a concrete value type.
585587
MVT MVT::getVT(Type *Ty, bool HandleUnknown){
586588
assert(Ty != nullptr && "Invalid type");
587589
switch (Ty->getTypeID()) {
@@ -611,7 +613,6 @@ MVT MVT::getVT(Type *Ty, bool HandleUnknown){
611613
case Type::X86_AMXTyID: return MVT(MVT::x86amx);
612614
case Type::FP128TyID: return MVT(MVT::f128);
613615
case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
614-
case Type::PointerTyID: return MVT(MVT::iPTR);
615616
case Type::FixedVectorTyID:
616617
case Type::ScalableVectorTyID: {
617618
VectorType *VTy = cast<VectorType>(Ty);
@@ -622,9 +623,11 @@ MVT MVT::getVT(Type *Ty, bool HandleUnknown){
622623
}
623624
}
624625

625-
/// getEVT - Return the value type corresponding to the specified type. This
626-
/// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types
627-
/// are returned as Other, otherwise they are invalid.
626+
/// getEVT - Return the value type corresponding to the specified type.
627+
/// If HandleUnknown is true, unknown types are returned as Other, otherwise
628+
/// they are invalid.
629+
/// NB: This includes pointer types, which require a DataLayout to convert
630+
/// to a concrete value type.
628631
EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
629632
switch (Ty->getTypeID()) {
630633
default:

0 commit comments

Comments
 (0)