-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Usha Gupta (usha1830) ChangesThis 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:
Full diff: https://github.com/llvm/llvm-project/pull/150383.diff 1 Files Affected:
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);
|
There was a problem hiding this 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.
This patch helps in fixing the regression seen in this issue #143219. |
7a473e3
to
4ada34b
Compare
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