Skip to content

Conversation

@mtrofin
Copy link
Member

@mtrofin mtrofin commented Nov 10, 2025

We don't have enough information to infer the probability of a weak function pointer being nullptr or not (open question if we could propagate this from the linker)

Issue #147390

Copy link
Member Author

mtrofin commented Nov 10, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions
Copy link

github-actions bot commented Nov 10, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Constant::getNullValue(F->getType()));

if (auto *SI = dyn_cast<SelectInst>(Select))
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pcc a question - would it be technically possible to propagate information about the weak function from [thin]link over to this point, and thus know if we even need a select in the first place?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just add a TODO to consider that.

@mtrofin mtrofin marked this pull request as ready for review November 10, 2025 22:57
@mtrofin mtrofin requested a review from pcc November 10, 2025 22:57
@llvmbot
Copy link
Member

llvmbot commented Nov 10, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Mircea Trofin (mtrofin)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/LowerTypeTests.cpp (+3)
  • (modified) llvm/test/Transforms/LowerTypeTests/function-weak.ll (+3-2)
diff --git a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
index 961a2ba2f58e5..6c5045f7c84ff 100644
--- a/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/lib/Transforms/IPO/LowerTypeTests.cpp
@@ -1494,6 +1494,9 @@ void LowerTypeTestsModule::replaceWeakDeclarationWithJumpTablePtr(
                                      Constant::getNullValue(F->getType()));
     Value *Select = Builder.CreateSelect(ICmp, JT,
                                          Constant::getNullValue(F->getType()));
+    
+    if (auto *SI = dyn_cast<SelectInst>(Select))
+      setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
     // For phi nodes, we need to update the incoming value for all operands
     // with the same predecessor.
     if (PN)
diff --git a/llvm/test/Transforms/LowerTypeTests/function-weak.ll b/llvm/test/Transforms/LowerTypeTests/function-weak.ll
index 4ea03b6c2c1fa..dbbe8fa4a0a9a 100644
--- a/llvm/test/Transforms/LowerTypeTests/function-weak.ll
+++ b/llvm/test/Transforms/LowerTypeTests/function-weak.ll
@@ -32,10 +32,10 @@ target triple = "x86_64-unknown-linux-gnu"
 declare !type !0 extern_weak void @f()
 
 ; CHECK: define zeroext i1 @check_f()
-define zeroext i1 @check_f() {
+define zeroext i1 @check_f() !prof !{!"function_entry_count", i32 10} {
 entry:
 ; CHECK: [[CMP:%.*]] = icmp ne ptr @f, null
-; CHECK: [[SEL:%.*]] = select i1 [[CMP]], ptr @[[JT:.*]], ptr null
+; CHECK: [[SEL:%.*]] = select i1 [[CMP]], ptr @[[JT:.*]], ptr null, !prof ![[SELPROF:[0-9]+]]
 ; CHECK: [[PTI:%.*]] = ptrtoint ptr [[SEL]] to i1
 ; CHECK: ret i1 [[PTI]]
   ret i1 ptrtoint (ptr @f to i1)
@@ -165,3 +165,4 @@ define i1 @foo(ptr %p) {
 ; CHECK-NEXT: }
 
 !0 = !{i32 0, !"typeid1"}
+; CHECK: ![[SELPROF]] = !{!"unknown", !"lowertypetests"}
\ No newline at end of file

@mtrofin mtrofin force-pushed the users/mtrofin/11-10-_ltt_mark_as_unkown_weak_function_tests branch from 0c5994b to 660888e Compare November 10, 2025 22:58
@mtrofin mtrofin force-pushed the users/mtrofin/11-07-_ltt_profcheck_set_branch_weights_for_complex_llvm.type.test_lowering branch from a6bc19f to 2bc1823 Compare November 13, 2025 20:28
@mtrofin mtrofin force-pushed the users/mtrofin/11-10-_ltt_mark_as_unkown_weak_function_tests branch from 660888e to 473fd29 Compare November 13, 2025 20:28
@mtrofin mtrofin force-pushed the users/mtrofin/11-10-_ltt_mark_as_unkown_weak_function_tests branch from 473fd29 to e7a9788 Compare November 13, 2025 20:47
@mtrofin mtrofin force-pushed the users/mtrofin/11-07-_ltt_profcheck_set_branch_weights_for_complex_llvm.type.test_lowering branch from 2bc1823 to 9a54c8d Compare November 13, 2025 20:47
Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Constant::getNullValue(F->getType()));

if (auto *SI = dyn_cast<SelectInst>(Select))
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just add a TODO to consider that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants