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

Moses Narrow via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 7 10:12:34 PST 2026


https://github.com/0pcom created https://github.com/llvm/llvm-project/pull/240011

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.

>From c80b63094f8db0cafb53283074f9783557bcf8b8 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Tue, 13 Oct 2026 17:45:31 +0200
Subject: [PATCH] FunctionAttrs: Check all uses when inferring captures for
 argument SCCs (#231536)

When merging the capture components of an SCC of arguments, only the
first use of each argument was considered, because of a misplaced break.
If an argument was passed to another argument in the SCC first and to a
capturing argument outside of it later, it was incorrectly inferred to
not be captured.

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
(cherry picked from commit 05b3eeffba9f93d14a9f79af253c4769d8c01827)
---
 llvm/lib/Transforms/IPO/FunctionAttrs.cpp     |  1 -
 .../Transforms/FunctionAttrs/arg_returned.ll  |  4 +-
 .../Transforms/FunctionAttrs/nocapture.ll     | 48 +++++++++++++++++++
 3 files changed, 50 insertions(+), 3 deletions(-)

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
+}



More information about the llvm-branch-commits mailing list