[llvm-branch-commits] [llvm] [release/23.x] FunctionAttrs: Check all uses when inferring captures for argument SCCs (#231536) (PR #240011)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 7 10:13:28 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Moses Narrow (0pcom)

<details>
<summary>Changes</summary>

Backport of 05b3eeffba9f93d14a9f79af253c4769d8c01827 (#<!-- -->231536), as requested in #<!-- -->231536.

The code change applies cleanly. The nocapture.ll test conflicted only because release/23.x still declares llvm.strip.invariant.group.p0 where main appended the new test; the resolution keeps that declaration and adds the test unchanged. Both RUN lines of nocapture.ll and arg_returned.ll pass on 23.1.5 with the fix, and the new test fails without it.

The bug miscompiles TinyGo programs built with LLVM 23: a false captures(none) on an argument SCC lets TinyGo's escape analysis put an escaping allocation on the stack.

I used Claude Code to prepare the cherry-pick and run the tests, and reviewed the result.

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


3 Files Affected:

- (modified) llvm/lib/Transforms/IPO/FunctionAttrs.cpp (-1) 
- (modified) llvm/test/Transforms/FunctionAttrs/arg_returned.ll (+2-2) 
- (modified) llvm/test/Transforms/FunctionAttrs/nocapture.ll (+48) 


``````````diff
diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index d98d3a1c46309d..cce9b6b1da341a 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -1367,7 +1367,6 @@ static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
           CC |= Use->CC;
         else
           CC |= CaptureComponents(A->getAttributes().getCaptureInfo());
-        break;
       }
       if (capturesAll(CC))
         break;
diff --git a/llvm/test/Transforms/FunctionAttrs/arg_returned.ll b/llvm/test/Transforms/FunctionAttrs/arg_returned.ll
index 21e76620f0a990..7096feb15d5255 100644
--- a/llvm/test/Transforms/FunctionAttrs/arg_returned.ll
+++ b/llvm/test/Transforms/FunctionAttrs/arg_returned.ll
@@ -260,8 +260,8 @@ entry:
 
 ; TEST another SCC test
 ;
-; FNATTR:  define ptr @rt2_helper(ptr nofree readnone captures(address_is_null) %a)
-; FNATTR:  define ptr @rt2(ptr nofree readnone captures(address_is_null) %a, ptr nofree readnone captures(ret: address, provenance) %b)
+; FNATTR:  define ptr @rt2_helper(ptr nofree readnone %a)
+; FNATTR:  define ptr @rt2(ptr nofree readnone %a, ptr nofree readnone captures(ret: address, provenance) %b)
 define ptr @rt2_helper(ptr %a) #0 {
 entry:
   %call = call ptr @rt2(ptr %a, ptr %a)
diff --git a/llvm/test/Transforms/FunctionAttrs/nocapture.ll b/llvm/test/Transforms/FunctionAttrs/nocapture.ll
index 4ff806ca6aeb6a..dc8001eceb79d9 100644
--- a/llvm/test/Transforms/FunctionAttrs/nocapture.ll
+++ b/llvm/test/Transforms/FunctionAttrs/nocapture.ll
@@ -1468,3 +1468,51 @@ define void @captures_metadata_provenance(ptr %x, ptr %y) {
 
 declare ptr @llvm.launder.invariant.group.p0(ptr)
 declare ptr @llvm.strip.invariant.group.p0(ptr)
+
+; %p is captured through @scc_capture_b, which is not the first use of %p in
+; the argument SCC.
+declare void @capture_unknown(ptr)
+
+define void @scc_capture_a(ptr %p, i1 %c) {
+; COMMON-LABEL: define void @scc_capture_a
+; COMMON-SAME: (ptr [[P:%.*]], i1 [[C:%.*]]) {
+; COMMON-NEXT:    br i1 [[C]], label [[T:%.*]], label [[E:%.*]]
+; COMMON:       t:
+; COMMON-NEXT:    call void @scc_capture_c(ptr [[P]], i1 [[C]])
+; COMMON-NEXT:    ret void
+; COMMON:       e:
+; COMMON-NEXT:    call void @scc_capture_b(ptr [[P]])
+; COMMON-NEXT:    ret void
+;
+  br i1 %c, label %t, label %e
+
+t:
+  call void @scc_capture_c(ptr %p, i1 %c)
+  ret void
+
+e:
+  call void @scc_capture_b(ptr %p)
+  ret void
+}
+
+define void @scc_capture_b(ptr %p) {
+; COMMON-LABEL: define void @scc_capture_b
+; COMMON-SAME: (ptr [[P:%.*]]) {
+; COMMON-NEXT:    call void @scc_capture_c(ptr null, i1 false)
+; COMMON-NEXT:    call void @capture_unknown(ptr [[P]])
+; COMMON-NEXT:    ret void
+;
+  call void @scc_capture_c(ptr null, i1 false)
+  call void @capture_unknown(ptr %p)
+  ret void
+}
+
+define void @scc_capture_c(ptr %p, i1 %c) {
+; COMMON-LABEL: define void @scc_capture_c
+; COMMON-SAME: (ptr [[P:%.*]], i1 [[C:%.*]]) {
+; COMMON-NEXT:    call void @scc_capture_a(ptr [[P]], i1 [[C]])
+; COMMON-NEXT:    ret void
+;
+  call void @scc_capture_a(ptr %p, i1 %c)
+  ret void
+}

``````````

</details>


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


More information about the llvm-branch-commits mailing list