[llvm] [CodeExtractor] Reset stale FuncRetVal in findInputsOutputs (PR #195484)
Steven Wu via llvm-commits
llvm-commits at lists.llvm.org
Sat May 2 14:12:52 PDT 2026
https://github.com/cachemeifyoucan created https://github.com/llvm/llvm-project/pull/195484
HotColdSplitting calls findInputsOutputs once for cost analysis before
extractCodeRegion runs normalizeCFGForExtraction. The first call may set
FuncRetVal to a single-output value which severSplitPHINodesOfEntry then
moves back into the parent function. The second findInputsOutputs call
correctly finds zero outputs but leaves FuncRetVal pointing at the
parent's instruction, so the cold function emits a `ret` referencing a
value in another function and fails verification.
Reset FuncRetVal at the start of the selection block so each call
recomputes it from the current CFG.
rdar://176075761
>From ecbd653b66358e1c320d2ed5b5338b1a653efc7f Mon Sep 17 00:00:00 2001
From: Steven Wu <stevenwu at apple.com>
Date: Sat, 2 May 2026 14:12:42 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
---
llvm/lib/Transforms/Utils/CodeExtractor.cpp | 3 ++
.../stale-funcretval-after-sever.ll | 32 +++++++++++++++++++
2 files changed, 35 insertions(+)
create mode 100644 llvm/test/Transforms/HotColdSplit/stale-funcretval-after-sever.ll
diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
index d2215542ff785..949aae3329589 100644
--- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -707,6 +707,9 @@ void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
}
}
+ // Reset stale state from any prior call in HotColdSplitting; the CFG may
+ // have changed since.
+ FuncRetVal = nullptr;
if (!VoidReturnWithSingleOutput && !AggregateArgs && Outputs.size() == 1 &&
getCommonExitBlock(Blocks)) {
FuncRetVal = Outputs[0];
diff --git a/llvm/test/Transforms/HotColdSplit/stale-funcretval-after-sever.ll b/llvm/test/Transforms/HotColdSplit/stale-funcretval-after-sever.ll
new file mode 100644
index 0000000000000..cac54ac7b9dc0
--- /dev/null
+++ b/llvm/test/Transforms/HotColdSplit/stale-funcretval-after-sever.ll
@@ -0,0 +1,32 @@
+; RUN: opt -S -passes=hotcoldsplit -hotcoldsplit-threshold=0 < %s | FileCheck %s
+
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "arm64-apple-macosx27.0.0"
+
+declare void @sink_call(ptr, i32, ptr)
+
+; CHECK-LABEL: define ptr @test_stale_funcretval(
+; CHECK: call void @test_stale_funcretval.cold.1()
+; CHECK-LABEL: end:
+; CHECK-NEXT: phi ptr [ %ph, %codeRepl ], [ null, %then ]
+
+; CHECK-LABEL: define internal void @test_stale_funcretval.cold.1()
+; CHECK: ret void
+define ptr @test_stale_funcretval() {
+entry:
+ br i1 false, label %sink, label %then
+
+then: ; preds = %entry
+ br i1 false, label %end, label %sink, !prof !0
+
+sink: ; preds = %then, %entry
+ %ph = phi ptr [ null, %then ], [ null, %entry ]
+ tail call void @sink_call(ptr null, i32 0, ptr null)
+ br label %end
+
+end: ; preds = %sink, %then
+ %val = phi ptr [ %ph, %sink ], [ null, %then ]
+ ret ptr %val
+}
+
+!0 = !{!"branch_weights", !"expected", i32 2000, i32 1}
More information about the llvm-commits
mailing list