[PATCH] D106573: Enabling the copy-constant-to-alloca optimization in more instances

Mohammad Fawaz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 22 10:10:18 PDT 2021


mfawaz created this revision.
mfawaz added a reviewer: arsenm.
Herald added a subscriber: hiraditya.
mfawaz requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

This patch allows lifetime calls to be ignored (and later erased) if we
know that the copy-constant-to-alloca optimization is going to happen. 
The case that is missed is when the global variable is in a different address 
space than the alloca (as shown in the example added to the lit test.)

This used to work before https://github.com/llvm/llvm-project/commit/6da31fa4a61d68af21dfa1e144e726ed6d77903e


https://reviews.llvm.org/D106573

Files:
  llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
  llvm/test/Transforms/InstCombine/memcpy-from-global.ll


Index: llvm/test/Transforms/InstCombine/memcpy-from-global.ll
===================================================================
--- llvm/test/Transforms/InstCombine/memcpy-from-global.ll
+++ llvm/test/Transforms/InstCombine/memcpy-from-global.ll
@@ -73,6 +73,7 @@
 
 @G = constant %T {i8 1, [123 x i8] zeroinitializer }
 @H = constant [2 x %U] zeroinitializer, align 16
+ at I = internal addrspace(1) constant [4 x float] zeroinitializer , align 4
 
 define void @test2() {
 ; CHECK-LABEL: @test2(
@@ -323,4 +324,22 @@
   ret void
 }
 
+; Should replace alloca with global even when the global is in a different address space
+define float @test11(i64 %i) {
+; CHECK-LABEL: @test11(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr [4 x float], [4 x float] addrspace(1)* @I, i64 0, i64 %i
+; CHECK-NEXT: [[LD:%.*]] = load float, float addrspace(1)* [[GEP]]
+; CHECK-NEXT: ret float [[LD]]
+
+entry:
+  %a = alloca [4 x float], align 4
+  %b = bitcast [4 x float]* %a to i8*
+  call void @llvm.lifetime.start.p0i8(i64 16, i8* %b)
+  call void @llvm.memcpy.p0i8.p1i8.i64(i8* align 4 %b, i8 addrspace(1)* align 4 bitcast ([4 x float] addrspace(1)* @I to i8 addrspace(1)*), i64 16, i1 false)
+  %g = getelementptr inbounds [4 x float], [4 x float]* %a, i64 0, i64 %i
+  %r = load float, float* %g, align 4
+  ret float %r
+}
+
 attributes #0 = { null_pointer_is_valid }
Index: llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -272,6 +272,8 @@
         return false;
     } else if (isa<MemTransferInst>(Inst)) {
       Worklist.insert(Inst);
+    } else if (Inst->isLifetimeStartOrEnd()) {
+      continue;
     } else {
       LLVM_DEBUG(dbgs() << "Cannot handle pointer user: " << *U << '\n');
       return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106573.360866.patch
Type: text/x-patch
Size: 1947 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210722/1140bc43/attachment.bin>


More information about the llvm-commits mailing list