[llvm] [LLVM][IR] When evaluting GEP offsets don't assume ConstantInt is a scalar. (PR #117162)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 06:15:46 PST 2024


================
@@ -163,28 +165,30 @@ bool GEPOperator::accumulateConstantOffset(
     Value *V = GTI.getOperand();
     StructType *STy = GTI.getStructTypeOrNull();
     // Handle ConstantInt if possible.
-    if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
-      if (ConstOffset->isZero())
-        continue;
-      // if the type is scalable and the constant is not zero (vscale * n * 0 =
-      // 0) bailout.
-      if (ScalableType)
-        return false;
-      // Handle a struct index, which adds its field offset to the pointer.
-      if (STy) {
-        unsigned ElementIdx = ConstOffset->getZExtValue();
-        const StructLayout *SL = DL.getStructLayout(STy);
-        // Element offset is in bytes.
-        if (!AccumulateOffset(
-                APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
-                1))
+    if (V->getType()->isIntegerTy()) {
+      if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
+        if (ConstOffset->isZero())
+          continue;
+        // if the type is scalable and the constant is not zero (vscale * n * 0
+        // = 0) bailout.
+        if (ScalableType)
+          return false;
+        // Handle a struct index, which adds its field offset to the pointer.
+        if (STy) {
+          unsigned ElementIdx = ConstOffset->getZExtValue();
+          const StructLayout *SL = DL.getStructLayout(STy);
+          // Element offset is in bytes.
+          if (!AccumulateOffset(
+                  APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
+                  1))
+            return false;
+          continue;
+        }
+        if (!AccumulateOffset(ConstOffset->getValue(),
+                              GTI.getSequentialElementStride(DL)))
----------------
nikic wrote:

Also update the very similar code in collectOffset() below?

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


More information about the llvm-commits mailing list