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

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 9 10:16:15 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)
----------------
alexey-bataev wrote:

`for (size_t I : seq<size_t>(GEPSubscripts.size()))`

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


More information about the llvm-commits mailing list