[llvm-branch-commits] [llvm] [CalledValuePropagation] Fix handling of unnamed functions (PR #244846)
Ömer Sinan Ağacan via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Nov 21 00:37:43 PST 2026
https://github.com/osa1 created https://github.com/llvm/llvm-project/pull/244846
None
>From 73273da8e619d9568066adf19df38942cbd0a72d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= <omer at osa1.net>
Date: Sat, 21 Nov 2026 08:31:13 +0000
Subject: [PATCH 1/2] Add test
---
.../unnamed-functions.ll | 52 +++++++++++++++++++
1 file changed, 52 insertions(+)
create mode 100644 llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll
diff --git a/llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll b/llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll
new file mode 100644
index 00000000000000..c0a5c3454c5acb
--- /dev/null
+++ b/llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll
@@ -0,0 +1,52 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=called-value-propagation -S < %s | FileCheck %s
+
+; Check handling of unnamed functions.
+
+define internal void @0() {
+; CHECK-LABEL: define internal void @0() {
+; CHECK-NEXT: ret void
+;
+ ret void
+}
+
+define internal void @1() {
+; CHECK-LABEL: define internal void @1() {
+; CHECK-NEXT: ret void
+;
+ ret void
+}
+
+define void @named_a() {
+; CHECK-LABEL: define void @named_a() {
+; CHECK-NEXT: ret void
+;
+ ret void
+}
+
+define void @named_b() {
+; CHECK-LABEL: define void @named_b() {
+; CHECK-NEXT: ret void
+;
+ ret void
+}
+
+define void @test(i1 %c) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: i1 [[C:%.*]]) {
+; CHECK-NEXT: [[P:%.*]] = select i1 [[C]], ptr @[[GLOB0:[0-9]+]], ptr @[[GLOB1:[0-9]+]]
+; CHECK-NEXT: call void [[P]](), !callees [[META0:![0-9]+]]
+; CHECK-NEXT: [[Q:%.*]] = select i1 [[C]], ptr @named_a, ptr @named_b
+; CHECK-NEXT: call void [[Q]](), !callees [[META1:![0-9]+]]
+; CHECK-NEXT: ret void
+;
+ %p = select i1 %c, ptr @0, ptr @1
+ call void %p()
+ %q = select i1 %c, ptr @named_a, ptr @named_b
+ call void %q()
+ ret void
+}
+;.
+; CHECK: [[META0]] = !{ptr @[[GLOB0]]}
+; CHECK: [[META1]] = !{ptr @named_a, ptr @named_b}
+;.
>From 60e18840f4dfe77735bc66baa9ea01dd11c08ba8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= <omer at osa1.net>
Date: Sat, 21 Nov 2026 08:31:46 +0000
Subject: [PATCH 2/2] Fix it
---
.../Transforms/IPO/CalledValuePropagation.cpp | 34 +++++++++++++------
.../unnamed-functions.ll | 2 +-
2 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp b/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp
index 6d68fa46a6d609..fd5a1507b22846 100644
--- a/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp
+++ b/llvm/lib/Transforms/IPO/CalledValuePropagation.cpp
@@ -51,19 +51,21 @@ class CVPLatticeVal {
/// refer.
enum CVPLatticeStateTy { Undefined, FunctionSet, Overdefined, Untracked };
- /// Comparator for sorting the functions set. We want to keep the order
- /// deterministic for testing, etc.
+ /// Comparator for sorting the functions set. Compare using the unique and
+ /// deterministic function indices for deterministic output.
struct Compare {
+ const DenseMap<const Function *, uint32_t> &FunctionIndices;
bool operator()(const Function *LHS, const Function *RHS) const {
- return LHS->getName() < RHS->getName();
+ return FunctionIndices.at(LHS) < FunctionIndices.at(RHS);
}
};
CVPLatticeVal() = default;
CVPLatticeVal(CVPLatticeStateTy LatticeState) : LatticeState(LatticeState) {}
- CVPLatticeVal(std::vector<Function *> &&Functions)
+ CVPLatticeVal(std::vector<Function *> &&Functions,
+ const DenseMap<const Function *, uint32_t> &FunctionIndices)
: LatticeState(FunctionSet), Functions(std::move(Functions)) {
- assert(llvm::is_sorted(this->Functions, Compare()));
+ assert(llvm::is_sorted(this->Functions, Compare{FunctionIndices}));
}
/// Get a reference to the functions held by this lattice value. The number
@@ -105,13 +107,16 @@ class CVPLatticeVal {
class CVPLatticeFunc
: public AbstractLatticeFunction<CVPLatticeKey, CVPLatticeVal> {
const IPOOptions &Opts;
+ const DenseMap<const Function *, uint32_t> &FunctionIndices;
public:
- explicit CVPLatticeFunc(const IPOOptions &Opts)
+ explicit CVPLatticeFunc(
+ const IPOOptions &Opts,
+ const DenseMap<const Function *, uint32_t> &FunctionIndices)
: AbstractLatticeFunction(CVPLatticeVal(CVPLatticeVal::Undefined),
CVPLatticeVal(CVPLatticeVal::Overdefined),
CVPLatticeVal(CVPLatticeVal::Untracked)),
- Opts(Opts) {}
+ Opts(Opts), FunctionIndices(FunctionIndices) {}
/// Compute and return a CVPLatticeVal for the given CVPLatticeKey.
CVPLatticeVal ComputeLatticeVal(CVPLatticeKey Key) override {
@@ -151,10 +156,11 @@ class CVPLatticeFunc
std::vector<Function *> Union;
std::set_union(X.getFunctions().begin(), X.getFunctions().end(),
Y.getFunctions().begin(), Y.getFunctions().end(),
- std::back_inserter(Union), CVPLatticeVal::Compare{});
+ std::back_inserter(Union),
+ CVPLatticeVal::Compare{FunctionIndices});
if (Union.size() > Opts.cvp_max_functions_per_value)
return getOverdefinedVal();
- return CVPLatticeVal(std::move(Union));
+ return CVPLatticeVal(std::move(Union), FunctionIndices);
}
/// Compute the lattice values that change as a result of executing the given
@@ -226,7 +232,7 @@ class CVPLatticeFunc
if (isa<ConstantPointerNull>(C))
return CVPLatticeVal(CVPLatticeVal::FunctionSet);
if (auto *F = dyn_cast<Function>(C->stripPointerCasts()))
- return CVPLatticeVal({F});
+ return CVPLatticeVal({F}, FunctionIndices);
return getOverdefinedVal();
}
@@ -366,8 +372,14 @@ template <> struct LatticeKeyInfo<CVPLatticeKey> {
} // namespace llvm
static bool runCVP(Module &M) {
+ // Assign unique numbers to functions, to be used when ordering the functions
+ // in the lattice.
+ DenseMap<const Function *, uint32_t> FunctionIndices;
+ for (const auto &[Idx, F] : enumerate(M))
+ FunctionIndices.try_emplace(&F, Idx);
+
// Our custom lattice function and generic sparse propagation solver.
- CVPLatticeFunc Lattice(getIPOOptions(M));
+ CVPLatticeFunc Lattice(getIPOOptions(M), FunctionIndices);
SparseSolver<CVPLatticeKey, CVPLatticeVal> Solver(&Lattice);
// For each function in the module, if we can't track its arguments, let the
diff --git a/llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll b/llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll
index c0a5c3454c5acb..75f66910788782 100644
--- a/llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll
+++ b/llvm/test/Transforms/CalledValuePropagation/unnamed-functions.ll
@@ -47,6 +47,6 @@ define void @test(i1 %c) {
ret void
}
;.
-; CHECK: [[META0]] = !{ptr @[[GLOB0]]}
+; CHECK: [[META0]] = !{ptr @[[GLOB0]], ptr @[[GLOB1]]}
; CHECK: [[META1]] = !{ptr @named_a, ptr @named_b}
;.
More information about the llvm-branch-commits
mailing list