[llvm] [GVN] Allow non local MemoryDef tries to run (PR #228836)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 4 00:07:17 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: aokblast

<details>
<summary>Changes</summary>

The MemoryDef can be outside of the BB but the user still inside bb. An early exit while loop miss this case. We at least give scanMemoryAccessUsers a chance to run.

Assisted-by:	Claude # Test

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/GVN.cpp (+2-2) 
- (modified) llvm/test/Transforms/GVN/nonescaping.ll (+43) 


``````````diff
diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index bf7115106fa6a..3fd9aaf82d082 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -2639,7 +2639,7 @@ bool GVNPass::findReachingValuesForLoad(LoadInst *L,
   // Phase 1. First off, look for a local dependency to avoid having to
   // disambiguate between before the load and after the load of the starting
   // block (as the load may be visited from a backedge).
-  do {
+  for (;;) {
     // Scan users of the clobbering memory access.
     if (auto RMV = scanMemoryAccessesUsers(
             Loc, IsInvariantLoad, StartBlock,
@@ -2664,7 +2664,7 @@ bool GVNPass::findReachingValuesForLoad(LoadInst *L,
     // It may happen that the clobbering memory access does not actually
     // clobber our load location, transition to its defining memory access.
     ClobberMA = cast<MemoryUseOrDef>(ClobberMA)->getDefiningAccess();
-  } while (ClobberMA->getBlock() == StartBlock);
+  }
 
   // Non-local speculations are not allowed under ASan.
   if (L->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
diff --git a/llvm/test/Transforms/GVN/nonescaping.ll b/llvm/test/Transforms/GVN/nonescaping.ll
index 5db807fb95942..61f45e6b83bdd 100644
--- a/llvm/test/Transforms/GVN/nonescaping.ll
+++ b/llvm/test/Transforms/GVN/nonescaping.ll
@@ -50,6 +50,49 @@ define i8 @test_opnew(ptr %p) {
   call void @escape(ptr %obj)
   ret i8 %sub
 }
+declare void @opaque()
+declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
+
+; The call to @opaque cannot modify the alloca, which has not escaped yet, so the
+; clobbering access of %b is the (non-local) memcpy. %a is a user of it in the
+; same block and must still be found.
+define i32 @test_load_across_call_before_escape(ptr %src, i1 %c) {
+; CHECK-LABEL: @test_load_across_call_before_escape(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[AL:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr [[AL]], ptr [[SRC:%.*]], i64 4, i1 false)
+; CHECK-NEXT:    br i1 [[C:%.*]], label [[P1:%.*]], label [[P2:%.*]]
+; CHECK:       p1:
+; CHECK-NEXT:    br label [[BB:%.*]]
+; CHECK:       p2:
+; CHECK-NEXT:    br label [[BB]]
+; CHECK:       bb:
+; CHECK-NEXT:    [[A:%.*]] = load i32, ptr [[AL]], align 4
+; CHECK-NEXT:    call void @opaque()
+; CHECK-NEXT:    [[R:%.*]] = add i32 [[A]], [[A]]
+; CHECK-NEXT:    call void @escape(ptr [[AL]])
+; CHECK-NEXT:    ret i32 [[R]]
+;
+entry:
+  %al = alloca i32
+  call void @llvm.memcpy.p0.p0.i64(ptr %al, ptr %src, i64 4, i1 false)
+  br i1 %c, label %p1, label %p2
+
+p1:
+  br label %bb
+
+p2:
+  br label %bb
+
+bb:
+  %a = load i32, ptr %al
+  call void @opaque()
+  %b = load i32, ptr %al
+  %r = add i32 %a, %b
+  call void @escape(ptr %al)
+  ret i32 %r
+}
+
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
 ; MDEP: {{.*}}
 ; MSSA: {{.*}}

``````````

</details>


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


More information about the llvm-commits mailing list