Skip to content

[InstCombine] Lower multi-dimensional GEP to ptradd #150383

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

usha1830
Copy link
Contributor

@usha1830 usha1830 commented Jul 24, 2025

This change will help canonicalize multi-dimensional array geps with exactly one variable index and all other indices constant into a flat, single-index gep over the element type. This would cover cases such as:

%gep = getelementptr [9 x [9 x [9 x i32]]], ptr @arr, i64 0, i64 %i, i64 2, i64 3

https://alive2.llvm.org/ce/z/SWRKZd

@usha1830 usha1830 requested a review from nikic as a code owner July 24, 2025 07:49
@usha1830 usha1830 marked this pull request as draft July 24, 2025 07:49
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Jul 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Usha Gupta (usha1830)

Changes

This change will help canonicalize multi-dimensional array geps with exactly one variable index and all other indices constant into a flat, single-index gep over the element type. This would cover cases such as:

%gep = getelementptr [9 x [9 x [9 x i32]]], ptr @<!-- -->arr, i64 0, i64 %i, i64 2, i64 3


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+9)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index e2a9255ca9c6e..9b148e523b7a7 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2995,6 +2995,15 @@ static bool shouldCanonicalizeGEPToPtrAdd(GetElementPtrInst &GEP) {
                                  m_Shl(m_Value(), m_ConstantInt())))))
     return true;
 
+  // Flatten multidimensional GEPs with one variable index.
+  unsigned NumVarIndices = 0;
+  for (unsigned i = 1; i < GEP.getNumOperands(); ++i) {
+    if (!isa<ConstantInt>(GEP.getOperand(i)))
+      ++NumVarIndices;
+  }
+  if (NumVarIndices == 1)
+    return true;
+
   // gep (gep %p, C1), %x, C2 is expanded so the two constants can
   // possibly be merged together.
   auto PtrOpGep = dyn_cast<GEPOperator>(PtrOp);

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 think this change is going to have a very big impact on other transforms and will require lots of mitigation before it's viable. Especially after #137297 this is going to convert all GEPs into ptradd representation.

@usha1830
Copy link
Contributor Author

I think this change is going to have a very big impact on other transforms and will require lots of mitigation before it's viable. Especially after #137297 this is going to convert all GEPs into ptradd representation.

This patch helps in fixing the regression seen in this issue #143219.
And yes, I see a lot of test failures and am looking into addressing those.

@usha1830 usha1830 force-pushed the instcomb_lower_multidimgep branch from 7a473e3 to 4ada34b Compare July 28, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants