[llvm] [JumpTableToSwitch] Fix wrong function used for GUID computation (PR #192877)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 19 15:14:29 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Alexander Shaposhnikov (alexander-shaposhnikov)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp (+1-1) 
- (added) llvm/test/Transforms/JumpTableToSwitch/profile-no-guid-metadata.ll (+41) 


``````````diff
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}

``````````

</details>


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


More information about the llvm-commits mailing list