[llvm] [polly] [delinearize] Extract array dimensions from alloca and global declarations (PR #156342)

Sebastian Pop via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 15:32:32 PDT 2025


================
@@ -714,7 +895,140 @@ bool llvm::tryDelinearizeFixedSizeImpl(
   if (!SrcGEP)
     return false;
 
-  getIndexExpressionsFromGEP(*SE, SrcGEP, Subscripts, Sizes);
+  // When flag useGEPToDelinearize is false, delinearize only using array_info.
+  if (!useGEPToDelinearize) {
+    SmallVector<const SCEV *, 4> SCEVSizes;
+    const SCEV *ElementSize = SE->getElementSize(Inst);
+    if (!delinearizeUsingArrayInfo(*SE, AccessFn, Subscripts, SCEVSizes,
+                                   ElementSize))
+      return false;
+
+    // TODO: Remove the following code. Convert SCEV sizes to int sizes. This
+    // conversion is only needed as long as getIndexExpressionsFromGEP is still
+    // around. Remove this code and change the interface of
+    // tryDelinearizeFixedSizeImpl to take a SmallVectorImpl<const SCEV *>
+    // &Sizes.
+    convertSCEVSizesToIntSizes(SCEVSizes, Sizes);
+    return true;
+  }
+
+  // TODO: Remove all the following code once we are satisfied with array_info.
+  // Run both methods when useGEPToDelinearize is true: validation is enabled.
+
+  // Store results from both methods.
+  SmallVector<const SCEV *, 4> GEPSubscripts, ArrayInfoSubscripts;
+  SmallVector<int, 4> GEPSizes, ArrayInfoSizes;
+
+  // GEP-based delinearization.
+  bool GEPSuccess =
+      getIndexExpressionsFromGEP(*SE, SrcGEP, GEPSubscripts, GEPSizes);
+
+  // Array_info delinearization.
+  SmallVector<const SCEV *, 4> SCEVSizes;
+  const SCEV *ElementSize = SE->getElementSize(Inst);
+  bool ArrayInfoSuccess = delinearizeUsingArrayInfo(
+      *SE, AccessFn, ArrayInfoSubscripts, SCEVSizes, ElementSize);
+
+  // TODO: Remove the following code. Convert SCEV sizes to int sizes. This
+  // conversion is only needed as long as getIndexExpressionsFromGEP is still
+  // around. Remove this code and change the interface of
+  // tryDelinearizeFixedSizeImpl to take a SmallVectorImpl<const SCEV *> &Sizes.
+  if (ArrayInfoSuccess)
+    convertSCEVSizesToIntSizes(SCEVSizes, ArrayInfoSizes);
+
+  // Validate consistency between methods.
+  if (GEPSuccess && ArrayInfoSuccess) {
+    // If both methods succeeded, validate they produce the same results.
+    // Compare sizes arrays.
+    if (GEPSizes.size() + 2 != ArrayInfoSizes.size()) {
+      LLVM_DEBUG({
+        dbgs() << "WARN: Size arrays have different lengths!\n";
+        dbgs() << "GEP sizes count: " << GEPSizes.size() << "\n"
+               << "ArrayInfo sizes count: " << ArrayInfoSizes.size() << "\n";
+      });
+    }
+
+    for (size_t i = 0; i < GEPSizes.size(); ++i) {
+      if (GEPSizes[i] != ArrayInfoSizes[i + 1]) {
+        LLVM_DEBUG({
+          dbgs() << "WARN: Size arrays differ at index " << i << "!\n";
+          dbgs() << "GEP size[" << i << "]: " << GEPSizes[i] << "\n"
+                 << "ArrayInfo size[" << i + 1 << "]: " << ArrayInfoSizes[i + 1]
+                 << "\n";
+        });
+      }
+    }
+
+    // Compare subscripts arrays.
+    if (GEPSubscripts.size() != ArrayInfoSubscripts.size()) {
+      LLVM_DEBUG({
+        dbgs() << "WARN: Subscript arrays have different lengths!\n";
+        dbgs() << "  GEP subscripts count: " << GEPSubscripts.size() << "\n"
+               << "  ArrayInfo subscripts count: " << ArrayInfoSubscripts.size()
+               << "\n";
+
+        dbgs() << "  GEP subscripts:\n";
+        for (size_t i = 0; i < GEPSubscripts.size(); ++i)
+          dbgs() << "    subscript[" << i << "]: " << *GEPSubscripts[i] << "\n";
+
+        dbgs() << "  ArrayInfo subscripts:\n";
+        for (size_t i = 0; i < ArrayInfoSubscripts.size(); ++i)
+          dbgs() << "    subscript[" << i << "]: " << *ArrayInfoSubscripts[i]
+                 << "\n";
+      });
+    }
+
+    for (size_t i = 0; i < GEPSubscripts.size(); ++i) {
+      const SCEV *GEPS = GEPSubscripts[i];
+      const SCEV *AIS = ArrayInfoSubscripts[i];
+      // FIXME: there's no good way to compare two scevs: don't abort, warn.
+      if (GEPS != AIS || !SE->getMinusSCEV(GEPS, AIS)->isZero()) {
+        LLVM_DEBUG({
+          dbgs() << "WARN: Subscript arrays differ at index " << i << "!\n";
+          dbgs() << "  GEP subscript[" << i << "]: " << *GEPSubscripts[i]
+                 << "\n"
+                 << "  ArrayInfo subscript[" << i
+                 << "]: " << *ArrayInfoSubscripts[i] << "\n";
+        });
+      }
+    }
+
+    LLVM_DEBUG(dbgs() << "SUCCESS: Both delinearization methods produced "
+                         "identical results\n");
+  } else if (GEPSuccess && !ArrayInfoSuccess) {
+    LLVM_DEBUG({
+      dbgs() << "WARNING: array_info failed and GEP analysis succeeded.\n";
+      dbgs() << "  Instruction: " << *Inst << "\n";
+      dbgs() << "  Using GEP analysis results despite array_info failure\n";
+    });
+  } else if (!GEPSuccess && ArrayInfoSuccess) {
+    LLVM_DEBUG({
+      dbgs() << "WARNING: GEP failed and array_info analysis succeeded.\n";
+      dbgs() << "  Instruction: " << *Inst << "\n";
+      dbgs() << "  Using array_info analysis results despite GEP failure\n";
+    });
+  } else if (!GEPSuccess && !ArrayInfoSuccess) {
+    LLVM_DEBUG({
+      dbgs() << "WARNING: both GEP and array_info analysis failed.\n";
+      dbgs() << "  Instruction: " << *Inst << "\n";
+    });
+  }
----------------
sebpop wrote:

I moved all this code under a large section of LLVM_DEBUG.
This is not important as the next patch to remove GEP-delinearize also removes all this code.

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


More information about the llvm-commits mailing list