Skip to content

[WPD]Provide branch weight for checking mode. #124084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

mingmingl-llvm
Copy link
Contributor

Checking mode aims to help diagnose and confirm undefined behavior. In most cases, source code don't cast pointers between unrelated types for virtual calls, so we expect direct calls in the frequent branch and debug trap in the unlikely branch.

This way, the overhead of checking mode is not higher than an indirect call promotion as long as the callsite doesn't run the debug trap branch.

@llvmbot llvmbot added LTO Link time optimization (regular/full LTO or ThinLTO) llvm:transforms labels Jan 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Mingming Liu (mingmingl-llvm)

Changes

Checking mode aims to help diagnose and confirm undefined behavior. In most cases, source code don't cast pointers between unrelated types for virtual calls, so we expect direct calls in the frequent branch and debug trap in the unlikely branch.

This way, the overhead of checking mode is not higher than an indirect call promotion as long as the callsite doesn't run the debug trap branch.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp (+3-2)
  • (modified) llvm/test/ThinLTO/X86/devirt_check.ll (+3-1)
diff --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
index 2f171c3c981d40..aaea67aa8fcb64 100644
--- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
+++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
@@ -1225,8 +1225,9 @@ void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
       // perform a debug trap.
       if (DevirtCheckMode == WPDCheckMode::Trap) {
         auto *Cond = Builder.CreateICmpNE(CB.getCalledOperand(), Callee);
-        Instruction *ThenTerm =
-            SplitBlockAndInsertIfThen(Cond, &CB, /*Unreachable=*/false);
+        Instruction *ThenTerm = SplitBlockAndInsertIfThen(
+            Cond, &CB, /*Unreachable=*/false,
+            MDBuilder(M.getContext()).createUnlikelyBranchWeights());
         Builder.SetInsertPoint(ThenTerm);
         Function *TrapFn =
             Intrinsic::getOrInsertDeclaration(&M, Intrinsic::debugtrap);
diff --git a/llvm/test/ThinLTO/X86/devirt_check.ll b/llvm/test/ThinLTO/X86/devirt_check.ll
index 74f1dfd6ac012a..4a1a971177ae68 100644
--- a/llvm/test/ThinLTO/X86/devirt_check.ll
+++ b/llvm/test/ThinLTO/X86/devirt_check.ll
@@ -58,7 +58,7 @@ entry:
   ; Ensure !prof and !callees metadata for indirect call promotion removed.
   ; TRAP-NOT: prof
   ; TRAP-NOT: callees
-  ; TRAP:   br i1 %.not, label %1, label %0
+  ; TRAP:   br i1 %.not, label %1, label %0, !prof ![[UNLIKELY:[0-9]+]]
   ; TRAP: 0:
   ; TRAP:   tail call void @llvm.debugtrap()
   ; TRAP:   br label %1
@@ -89,6 +89,8 @@ entry:
 ; CHECK-LABEL:   ret i32
 ; CHECK-LABEL: }
 
+; TRAP: ![[UNLIKELY]] = !{!"branch_weights", i32 1, i32 1048575}
+
 declare i1 @llvm.type.test(i8*, metadata)
 declare void @llvm.assume(i1)
 

@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2025

@llvm/pr-subscribers-lto

Author: Mingming Liu (mingmingl-llvm)

Changes

Checking mode aims to help diagnose and confirm undefined behavior. In most cases, source code don't cast pointers between unrelated types for virtual calls, so we expect direct calls in the frequent branch and debug trap in the unlikely branch.

This way, the overhead of checking mode is not higher than an indirect call promotion as long as the callsite doesn't run the debug trap branch.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp (+3-2)
  • (modified) llvm/test/ThinLTO/X86/devirt_check.ll (+3-1)
diff --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
index 2f171c3c981d40..aaea67aa8fcb64 100644
--- a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
+++ b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
@@ -1225,8 +1225,9 @@ void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
       // perform a debug trap.
       if (DevirtCheckMode == WPDCheckMode::Trap) {
         auto *Cond = Builder.CreateICmpNE(CB.getCalledOperand(), Callee);
-        Instruction *ThenTerm =
-            SplitBlockAndInsertIfThen(Cond, &CB, /*Unreachable=*/false);
+        Instruction *ThenTerm = SplitBlockAndInsertIfThen(
+            Cond, &CB, /*Unreachable=*/false,
+            MDBuilder(M.getContext()).createUnlikelyBranchWeights());
         Builder.SetInsertPoint(ThenTerm);
         Function *TrapFn =
             Intrinsic::getOrInsertDeclaration(&M, Intrinsic::debugtrap);
diff --git a/llvm/test/ThinLTO/X86/devirt_check.ll b/llvm/test/ThinLTO/X86/devirt_check.ll
index 74f1dfd6ac012a..4a1a971177ae68 100644
--- a/llvm/test/ThinLTO/X86/devirt_check.ll
+++ b/llvm/test/ThinLTO/X86/devirt_check.ll
@@ -58,7 +58,7 @@ entry:
   ; Ensure !prof and !callees metadata for indirect call promotion removed.
   ; TRAP-NOT: prof
   ; TRAP-NOT: callees
-  ; TRAP:   br i1 %.not, label %1, label %0
+  ; TRAP:   br i1 %.not, label %1, label %0, !prof ![[UNLIKELY:[0-9]+]]
   ; TRAP: 0:
   ; TRAP:   tail call void @llvm.debugtrap()
   ; TRAP:   br label %1
@@ -89,6 +89,8 @@ entry:
 ; CHECK-LABEL:   ret i32
 ; CHECK-LABEL: }
 
+; TRAP: ![[UNLIKELY]] = !{!"branch_weights", i32 1, i32 1048575}
+
 declare i1 @llvm.type.test(i8*, metadata)
 declare void @llvm.assume(i1)
 

@mingmingl-llvm mingmingl-llvm merged commit e1aa1e4 into main Jan 23, 2025
8 checks passed
@mingmingl-llvm mingmingl-llvm deleted the users/mingmingl-llvm/spr/wpd branch January 23, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:transforms LTO Link time optimization (regular/full LTO or ThinLTO)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants