Skip to content
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

[Transforms] Preserve inbounds attribute of transformed GEPs when flattening loops #86961

Merged
merged 1 commit into from
May 15, 2024

Conversation

AtariDreams
Copy link
Contributor

@AtariDreams AtariDreams commented Mar 28, 2024

When flattening the loop, if the GEP was inbound, it should stay inbound, because the only thing that changed is how the pointers are calculated, not the elements being accessed.

Proof: https://alive2.llvm.org/ce/z/dApMpQ

@AtariDreams AtariDreams changed the title Preserve inbound of transformed GEPs when flattening loops [Transforms] Preserve inbound of transformed GEPs when flattening loops Mar 28, 2024
@AtariDreams AtariDreams force-pushed the inbounds branch 2 times, most recently from 1f461fd to df472c4 Compare March 28, 2024 14:54
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 28, 2024

@llvm/pr-subscribers-llvm-transforms

Author: AtariDreams (AtariDreams)

Changes

When flattening the loop, if the GEP was inbound, it should stay inbound, because the only thing that changed is how the pointers are calculated, not the elements being accessed.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/LoopFlatten.cpp (+3-2)
  • (modified) llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll (+3-3)
diff --git a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
index 0e9cf328f149be..9316333aa2dd6d 100644
--- a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
@@ -808,8 +808,9 @@ static bool DoFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
       // we need to insert the new GEP where the old GEP was.
       if (!DT->dominates(Base, &*Builder.GetInsertPoint()))
         Builder.SetInsertPoint(cast<Instruction>(V));
-      OuterValue = Builder.CreateGEP(GEP->getSourceElementType(), Base,
-                                     OuterValue, "flatten." + V->getName());
+      OuterValue =
+          Builder.CreateGEP(GEP->getSourceElementType(), Base, OuterValue,
+                            "flatten." + V->getName(), GEP->isInBounds());
     }
 
     LLVM_DEBUG(dbgs() << "Replacing: "; V->dump(); dbgs() << "with:      ";
diff --git a/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll b/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll
index f4b8ea97237fe6..e30001670b1e95 100644
--- a/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll
+++ b/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll
@@ -15,7 +15,7 @@ for.outer.preheader:
   br label %for.inner.preheader
 
 ; CHECK-LABEL: for.inner.preheader:
-; CHECK: %flatten.arrayidx = getelementptr i32, ptr %A, i32 %i
+; CHECK: %flatten.arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
 for.inner.preheader:
   %i = phi i32 [ 0, %for.outer.preheader ], [ %inc2, %for.outer ]
   br label %for.inner
@@ -61,13 +61,13 @@ for.outer.preheader:
   br label %for.inner.preheader
 
 ; CHECK-LABEL: for.inner.preheader:
-; CHECK-NOT: getelementptr i32, ptr %ptr, i32 %i
+; CHECK-NOT: getelementptr inbounds i32, ptr %ptr, i32 %i
 for.inner.preheader:
   %i = phi i32 [ 0, %for.outer.preheader ], [ %inc2, %for.outer ]
   br label %for.inner
 
 ; CHECK-LABEL: for.inner:
-; CHECK: %flatten.arrayidx = getelementptr i32, ptr %ptr, i32 %i
+; CHECK: %flatten.arrayidx = getelementptr inbounds i32, ptr %ptr, i32 %i
 ; CHECK: store i32 0, ptr %flatten.arrayidx, align 4
 ; CHECK: br label %for.outer
 for.inner:

@AtariDreams
Copy link
Contributor Author

@dtcxzyw dtcxzyw requested a review from fhahn March 28, 2024 15:15
@AtariDreams AtariDreams changed the title [Transforms] Preserve inbound of transformed GEPs when flattening loops [Transforms] Preserve inbounds attribute of transformed GEPs when flattening loops Apr 6, 2024
@AtariDreams AtariDreams force-pushed the inbounds branch 2 times, most recently from a9a48a7 to dc6a1b4 Compare April 6, 2024 19:56
@AtariDreams
Copy link
Contributor Author

@fhahn What do you think about this?

@AtariDreams
Copy link
Contributor Author

@john-brawn-arm What do you think about this?

@AtariDreams AtariDreams force-pushed the inbounds branch 2 times, most recently from 50ebb27 to 5720359 Compare May 14, 2024 00:07
…ttening loops

When flattening the loop, if the GEP was inbound, it should stay inbound, because the only thing that changed is how the pointers are calculated, not the elements being accessed.

Proof: https://alive2.llvm.org/ce/z/dApMpQ
@john-brawn-arm
Copy link
Collaborator

Looks good to me.

@AtariDreams
Copy link
Contributor Author

@dtcxzyw What do you think?

@AtariDreams
Copy link
Contributor Author

Looks good to me.

Thank you!

@dtcxzyw dtcxzyw requested a review from nikic May 14, 2024 13:48
@nikic nikic merged commit 4d1ecf1 into llvm:main May 15, 2024
4 checks passed
@AtariDreams AtariDreams deleted the inbounds branch May 15, 2024 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants