[llvm] [clang] [IR] Fix GEP offset computations for vector GEPs (PR #75448)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 14 05:34:49 PST 2023


================
@@ -108,7 +143,23 @@ class generic_gep_type_iterator {
   // that.
 
   bool isStruct() const { return isa<StructType *>(CurTy); }
-  bool isSequential() const { return isa<Type *>(CurTy); }
+  bool isVector() const { return isa<VectorType *>(CurTy); }
+  bool isSequential() const { return !isStruct(); }
+
+  // For sequential GEP indices (all except those into structs), the index value
+  // can be translated into a byte offset by multiplying with an element stride.
+  // This function returns this stride, which both depends on the element type,
+  // and the containing aggregate type, as vectors always tightly bit-pack their
+  // elements.
+  TypeSize getSequentialElementStride(const DataLayout &DL) const {
+    assert(isSequential());
+    Type *ElemTy = getIndexedType();
+    TypeSize ElemSizeInBits = isVector() ? DL.getTypeSizeInBits(ElemTy)
+                                         : DL.getTypeAllocSizeInBits(ElemTy);
+    // Check for invalid GEPs that are not byte-addressable.
+    assert(ElemSizeInBits.isKnownMultipleOf(8));
+    return ElemSizeInBits.divideCoefficientBy(8);
----------------
nikic wrote:

```suggestion
    if (isVector()) {
      assert(DL.typeSizeEqualsStoreSize(ElemTy) && "Not byte-addressable");
      return DL.getTypeStoreSize(ElemTy);
    }
    return DL.getTypeAllocSize(ElemTy);
```
Avoid explicit `8` constants.

https://github.com/llvm/llvm-project/pull/75448


More information about the cfe-commits mailing list