Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Use const Type* in DataLayout #70009

Closed
wants to merge 2 commits into from

Conversation

jasilvanus
Copy link
Contributor

DataLayout functions that are used to query infos about types used to work on Type*.
Change these to use const Type* instead.

This is an easy mechanical change that does not need any follup changes elsewhere.

Most of LLVM does not care a lot about constness of Types, but requiring non-const Type* in DL forces everyone to use non-const Type*, or add ugly const_casts.

DataLayout functions that are used to query infos
about types used to work on Type*.
Change these to use const Type* instead.

This is an easy mechanical change that does not need
any follup changes elsewhere.

Most of LLVM does not care a lot about constness of Types,
but requiring non-const Type* in DL forces everyone to use
non-const Type*, or add ugly const_casts.
@jasilvanus jasilvanus added enhancement Improving things as opposed to bug fixing, e.g. new or missing feature code-quality llvm:ir labels Oct 24, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 24, 2023

@llvm/pr-subscribers-llvm-ir

Author: Jannik Silvanus (jasilvanus)

Changes

DataLayout functions that are used to query infos about types used to work on Type*.
Change these to use const Type* instead.

This is an easy mechanical change that does not need any follup changes elsewhere.

Most of LLVM does not care a lot about constness of Types, but requiring non-const Type* in DL forces everyone to use non-const Type*, or add ugly const_casts.


Full diff: https://github.com/llvm/llvm-project/pull/70009.diff

2 Files Affected:

  • (modified) llvm/include/llvm/IR/DataLayout.h (+23-23)
  • (modified) llvm/lib/IR/DataLayout.cpp (+14-14)
diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index b3633b67b9debda..30311f3ff46a3c3 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -179,7 +179,7 @@ class DataLayout {
   Align getIntegerAlignment(uint32_t BitWidth, bool abi_or_pref) const;
 
   /// Internal helper method that returns requested alignment for type.
-  Align getAlignment(Type *Ty, bool abi_or_pref) const;
+  Align getAlignment(const Type *Ty, bool abi_or_pref) const;
 
   /// Attempts to parse a target data specification string and reports an error
   /// if the string is malformed.
@@ -395,11 +395,11 @@ class DataLayout {
     return is_contained(NonIntegralSpaces, AddrSpace);
   }
 
-  bool isNonIntegralPointerType(PointerType *PT) const {
+  bool isNonIntegralPointerType(const PointerType *PT) const {
     return isNonIntegralAddressSpace(PT->getAddressSpace());
   }
 
-  bool isNonIntegralPointerType(Type *Ty) const {
+  bool isNonIntegralPointerType(const Type *Ty) const {
     auto *PTy = dyn_cast<PointerType>(Ty);
     return PTy && isNonIntegralPointerType(PTy);
   }
@@ -426,13 +426,13 @@ class DataLayout {
   /// If this function is called with a vector of pointers, then the type size
   /// of the pointer is returned.  This should only be called with a pointer or
   /// vector of pointers.
-  unsigned getPointerTypeSizeInBits(Type *) const;
+  unsigned getPointerTypeSizeInBits(const Type *) const;
 
   /// Layout size of the index used in GEP calculation.
   /// The function should be called with pointer or vector of pointers type.
-  unsigned getIndexTypeSizeInBits(Type *Ty) const;
+  unsigned getIndexTypeSizeInBits(const Type *Ty) const;
 
-  unsigned getPointerTypeSize(Type *Ty) const {
+  unsigned getPointerTypeSize(const Type *Ty) const {
     return getPointerTypeSizeInBits(Ty) / 8;
   }
 
@@ -460,7 +460,7 @@ class DataLayout {
   ///
   /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
   /// have a size (Type::isSized() must return true).
-  TypeSize getTypeSizeInBits(Type *Ty) const;
+  TypeSize getTypeSizeInBits(const Type *Ty) const;
 
   /// Returns the maximum number of bytes that may be overwritten by
   /// storing the specified type.
@@ -469,7 +469,7 @@ class DataLayout {
   /// the runtime size will be a positive integer multiple of the base size.
   ///
   /// For example, returns 5 for i36 and 10 for x86_fp80.
-  TypeSize getTypeStoreSize(Type *Ty) const {
+  TypeSize getTypeStoreSize(const Type *Ty) const {
     TypeSize BaseSize = getTypeSizeInBits(Ty);
     return {divideCeil(BaseSize.getKnownMinValue(), 8), BaseSize.isScalable()};
   }
@@ -481,7 +481,7 @@ class DataLayout {
   /// the runtime size will be a positive integer multiple of the base size.
   ///
   /// For example, returns 40 for i36 and 80 for x86_fp80.
-  TypeSize getTypeStoreSizeInBits(Type *Ty) const {
+  TypeSize getTypeStoreSizeInBits(const Type *Ty) const {
     return 8 * getTypeStoreSize(Ty);
   }
 
@@ -489,7 +489,7 @@ class DataLayout {
   /// specified type.
   ///
   /// For example, returns false for i19 that has a 24-bit store size.
-  bool typeSizeEqualsStoreSize(Type *Ty) const {
+  bool typeSizeEqualsStoreSize(const Type *Ty) const {
     return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty);
   }
 
@@ -501,7 +501,7 @@ class DataLayout {
   ///
   /// This is the amount that alloca reserves for this type. For example,
   /// returns 12 or 16 for x86_fp80, depending on alignment.
-  TypeSize getTypeAllocSize(Type *Ty) const {
+  TypeSize getTypeAllocSize(const Type *Ty) const {
     // Round up to the next alignment boundary.
     return alignTo(getTypeStoreSize(Ty), getABITypeAlign(Ty).value());
   }
@@ -514,17 +514,17 @@ class DataLayout {
   ///
   /// This is the amount that alloca reserves for this type. For example,
   /// returns 96 or 128 for x86_fp80, depending on alignment.
-  TypeSize getTypeAllocSizeInBits(Type *Ty) const {
+  TypeSize getTypeAllocSizeInBits(const Type *Ty) const {
     return 8 * getTypeAllocSize(Ty);
   }
 
   /// Returns the minimum ABI-required alignment for the specified type.
-  Align getABITypeAlign(Type *Ty) const;
+  Align getABITypeAlign(const Type *Ty) const;
 
   /// Helper function to return `Alignment` if it's set or the result of
   /// `getABITypeAlign(Ty)`, in any case the result is a valid alignment.
   inline Align getValueOrABITypeAlignment(MaybeAlign Alignment,
-                                          Type *Ty) const {
+                                          const Type *Ty) const {
     return Alignment ? *Alignment : getABITypeAlign(Ty);
   }
 
@@ -540,13 +540,13 @@ class DataLayout {
   /// This is always at least as good as the ABI alignment.
   /// FIXME: Deprecate this function once migration to Align is over.
   LLVM_DEPRECATED("use getPrefTypeAlign instead", "getPrefTypeAlign")
-  uint64_t getPrefTypeAlignment(Type *Ty) const;
+  uint64_t getPrefTypeAlignment(const Type *Ty) const;
 
   /// Returns the preferred stack/global alignment for the specified
   /// type.
   ///
   /// This is always at least as good as the ABI alignment.
-  Align getPrefTypeAlign(Type *Ty) const;
+  Align getPrefTypeAlign(const Type *Ty) const;
 
   /// Returns an integer type with size at least as big as that of a
   /// pointer in the given address space.
@@ -554,7 +554,7 @@ class DataLayout {
 
   /// Returns an integer (vector of integer) type with size at least as
   /// big as that of a pointer of the given pointer (vector of pointer) type.
-  Type *getIntPtrType(Type *) const;
+  Type *getIntPtrType(const Type *) const;
 
   /// Returns the smallest integer type with size at least as big as
   /// Width bits.
@@ -578,7 +578,7 @@ class DataLayout {
   /// Returns the type of a GEP index.
   /// If it was not specified explicitly, it will be the integer type of the
   /// pointer width - IntPtrType.
-  Type *getIndexType(Type *PtrTy) const;
+  Type *getIndexType(const Type *PtrTy) const;
 
   /// Returns the offset from the beginning of the type for the specified
   /// indices.
@@ -601,7 +601,7 @@ class DataLayout {
   /// struct, its size, and the offsets of its fields.
   ///
   /// Note that this information is lazily cached.
-  const StructLayout *getStructLayout(StructType *Ty) const;
+  const StructLayout *getStructLayout(const StructType *Ty) const;
 
   /// Returns the preferred alignment of the specified global.
   ///
@@ -660,7 +660,7 @@ class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
 private:
   friend class DataLayout; // Only DataLayout can create this class
 
-  StructLayout(StructType *ST, const DataLayout &DL);
+  StructLayout(const StructType *ST, const DataLayout &DL);
 
   size_t numTrailingObjects(OverloadToken<TypeSize>) const {
     return NumElements;
@@ -669,7 +669,7 @@ class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
 
 // The implementation of this method is provided inline as it is particularly
 // well suited to constant folding when called on a specific Type subclass.
-inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
+inline TypeSize DataLayout::getTypeSizeInBits(const Type *Ty) const {
   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
   switch (Ty->getTypeID()) {
   case Type::LabelTyID:
@@ -677,7 +677,7 @@ inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
   case Type::PointerTyID:
     return TypeSize::Fixed(getPointerSizeInBits(Ty->getPointerAddressSpace()));
   case Type::ArrayTyID: {
-    ArrayType *ATy = cast<ArrayType>(Ty);
+    const ArrayType *ATy = cast<ArrayType>(Ty);
     return ATy->getNumElements() *
            getTypeAllocSizeInBits(ATy->getElementType());
   }
@@ -705,7 +705,7 @@ inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
     return TypeSize::Fixed(80);
   case Type::FixedVectorTyID:
   case Type::ScalableVectorTyID: {
-    VectorType *VTy = cast<VectorType>(Ty);
+    const VectorType *VTy = cast<VectorType>(Ty);
     auto EltCnt = VTy->getElementCount();
     uint64_t MinBits = EltCnt.getKnownMinValue() *
                        getTypeSizeInBits(VTy->getElementType()).getFixedValue();
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index d324c4b488f7294..fb26897e01b6614 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -45,7 +45,7 @@ using namespace llvm;
 // Support for StructLayout
 //===----------------------------------------------------------------------===//
 
-StructLayout::StructLayout(StructType *ST, const DataLayout &DL)
+StructLayout::StructLayout(const StructType *ST, const DataLayout &DL)
     : StructSize(TypeSize::Fixed(0)) {
   assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
   IsPadded = false;
@@ -681,7 +681,7 @@ Align DataLayout::getIntegerAlignment(uint32_t BitWidth,
 namespace {
 
 class StructLayoutMap {
-  using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
+  using LayoutInfoTy = DenseMap<const StructType*, StructLayout*>;
   LayoutInfoTy LayoutInfo;
 
 public:
@@ -694,7 +694,7 @@ class StructLayoutMap {
     }
   }
 
-  StructLayout *&operator[](StructType *STy) {
+  StructLayout *&operator[](const StructType *STy) {
     return LayoutInfo[STy];
   }
 };
@@ -715,7 +715,7 @@ DataLayout::~DataLayout() {
   clear();
 }
 
-const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
+const StructLayout *DataLayout::getStructLayout(const StructType *Ty) const {
   if (!LayoutMap)
     LayoutMap = new StructLayoutMap();
 
@@ -758,7 +758,7 @@ unsigned DataLayout::getMaxIndexSize() const {
   return MaxIndexSize;
 }
 
-unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
+unsigned DataLayout::getPointerTypeSizeInBits(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "This should only be called with a pointer or pointer vector type");
   Ty = Ty->getScalarType();
@@ -769,7 +769,7 @@ unsigned DataLayout::getIndexSize(unsigned AS) const {
   return divideCeil(getPointerAlignElem(AS).IndexBitWidth, 8);
 }
 
-unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
+unsigned DataLayout::getIndexTypeSizeInBits(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "This should only be called with a pointer or pointer vector type");
   Ty = Ty->getScalarType();
@@ -784,7 +784,7 @@ unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
   == false) for the requested type \a Ty.
  */
-Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
+Align DataLayout::getAlignment(const Type *Ty, bool abi_or_pref) const {
   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
   switch (Ty->getTypeID()) {
   // Early escape for the non-numeric types.
@@ -860,16 +860,16 @@ Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
   }
 }
 
-Align DataLayout::getABITypeAlign(Type *Ty) const {
+Align DataLayout::getABITypeAlign(const Type *Ty) const {
   return getAlignment(Ty, true);
 }
 
 /// TODO: Remove this function once the transition to Align is over.
-uint64_t DataLayout::getPrefTypeAlignment(Type *Ty) const {
+uint64_t DataLayout::getPrefTypeAlignment(const Type *Ty) const {
   return getPrefTypeAlign(Ty).value();
 }
 
-Align DataLayout::getPrefTypeAlign(Type *Ty) const {
+Align DataLayout::getPrefTypeAlign(const Type *Ty) const {
   return getAlignment(Ty, false);
 }
 
@@ -878,12 +878,12 @@ IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
   return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
 }
 
-Type *DataLayout::getIntPtrType(Type *Ty) const {
+Type *DataLayout::getIntPtrType(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "Expected a pointer or pointer vector type.");
   unsigned NumBits = getPointerTypeSizeInBits(Ty);
   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
-  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
+  if (const VectorType *VecTy = dyn_cast<VectorType>(Ty))
     return VectorType::get(IntTy, VecTy);
   return IntTy;
 }
@@ -905,12 +905,12 @@ IntegerType *DataLayout::getIndexType(LLVMContext &C,
   return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
 }
 
-Type *DataLayout::getIndexType(Type *Ty) const {
+Type *DataLayout::getIndexType(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "Expected a pointer or pointer vector type.");
   unsigned NumBits = getIndexTypeSizeInBits(Ty);
   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
-  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
+  if (const VectorType *VecTy = dyn_cast<VectorType>(Ty))
     return VectorType::get(IntTy, VecTy);
   return IntTy;
 }

@github-actions
Copy link

github-actions bot commented Oct 24, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

These changes are unrelated, which is why they were
not in the initial commit, but are apparently needed
to make the github actions bot happy.

Change-Id: Ib6e064604f50c4a2c4cccf4a6a0aaa20bb2aaab7
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure we have a general policy of not using const Type (by dint of being meaningless) and there was a big change at some point to drop all uses of it.

I have at least found the change that reverted someone doing the same thing you're trying to do here: 0ae7393

@jasilvanus
Copy link
Contributor Author

That unfortunate, abandoning then.

@jasilvanus jasilvanus closed this Oct 24, 2023
@jasilvanus jasilvanus deleted the jsilvanu/dl-const-type branch October 24, 2023 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-quality enhancement Improving things as opposed to bug fixing, e.g. new or missing feature llvm:ir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants