[llvm] [JumpTableToSwitch] Fix wrong function used for GUID computation (PR #192877)
Alexander Shaposhnikov via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 19 15:14:00 PDT 2026
https://github.com/alexander-shaposhnikov created https://github.com/llvm/llvm-project/pull/192877
The FuncToGuid lambda's fallback path (when target functions lack !guid metadata) was using 'F' (the caller) instead of 'Fct' (the callee) in getIRPGOFuncName, causing all GUID lookups to resolve to the caller's GUID.
>From 7d11f9e21f73042bfb6972fedf7ad7d1a5e9cf5a Mon Sep 17 00:00:00 2001
From: Alexander Shaposhnikov <alexander.v.shaposhnikov at gmail.com>
Date: Sun, 19 Apr 2026 15:11:22 -0700
Subject: [PATCH] [JumpTableToSwitch] Fix wrong function used for GUID
computation in profile data lookup
---
.../Transforms/Scalar/JumpTableToSwitch.cpp | 2 +-
.../profile-no-guid-metadata.ll | 41 +++++++++++++++++++
2 files changed, 42 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/Transforms/JumpTableToSwitch/profile-no-guid-metadata.ll
diff --git a/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp b/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp
index a3e3b9a207ca1..4ff9756705320 100644
--- a/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp
@@ -215,7 +215,7 @@ PreservedAnalyses JumpTableToSwitchPass::run(Function &F,
if (Fct.getMetadata(AssignGUIDPass::GUIDMetadataName))
return AssignGUIDPass::getGUID(Fct);
- return Function::getGUIDAssumingExternalLinkage(getIRPGOFuncName(F, InLTO));
+ return Function::getGUIDAssumingExternalLinkage(getIRPGOFuncName(Fct, InLTO));
};
for (BasicBlock &BB : make_early_inc_range(F)) {
diff --git a/llvm/test/Transforms/JumpTableToSwitch/profile-no-guid-metadata.ll b/llvm/test/Transforms/JumpTableToSwitch/profile-no-guid-metadata.ll
new file mode 100644
index 0000000000000..32c416cd9355b
--- /dev/null
+++ b/llvm/test/Transforms/JumpTableToSwitch/profile-no-guid-metadata.ll
@@ -0,0 +1,41 @@
+; RUN: opt < %s -passes=jump-table-to-switch -S | FileCheck %s
+
+;; Test that when target functions lack !guid metadata, the pass correctly
+;; computes GUIDs from the target function names (not the caller's name)
+;; for matching against value profile data. This is a regression test for
+;; a bug where the caller function was used instead of the callee when
+;; computing PGO function names for GUID lookup.
+
+ at jt = constant [2 x ptr] [ptr @jt_target_0, ptr @jt_target_1]
+
+;; Note: these functions intentionally do NOT have !guid metadata,
+;; forcing the pass to compute GUIDs via getIRPGOFuncName.
+define i32 @jt_target_0() {
+ ret i32 10
+}
+
+define i32 @jt_target_1() {
+ ret i32 20
+}
+
+define i32 @caller(i32 %idx) {
+; CHECK-LABEL: define i32 @caller(
+; CHECK: switch i32 [[IDX:%.*]], label %{{.*}} [
+; CHECK-NEXT: i32 0, label %[[CALL0:.*]]
+; CHECK-NEXT: i32 1, label %[[CALL1:.*]]
+; CHECK-NEXT: ], !prof [[PROF:![0-9]+]]
+; CHECK: [[CALL0]]:
+; CHECK-NEXT: {{.*}} = call i32 @jt_target_0()
+; CHECK: [[CALL1]]:
+; CHECK-NEXT: {{.*}} = call i32 @jt_target_1()
+ %gep = getelementptr inbounds [2 x ptr], ptr @jt, i32 0, i32 %idx
+ %fptr = load ptr, ptr %gep
+ %r = call i32 %fptr(), !prof !0
+ ret i32 %r
+}
+
+;; VP metadata: GUID 11912887233601027218 = MD5("jt_target_0"), count 100
+;; GUID 18156790114353049777 = MD5("jt_target_1"), count 50
+!0 = !{!"VP", i32 0, i64 150, i64 11912887233601027218, i64 100, i64 18156790114353049777, i64 50}
+
+; CHECK: [[PROF]] = !{!"branch_weights", i32 0, i32 100, i32 50}
More information about the llvm-commits
mailing list