[llvm] e1aa1e4 - [WPD]Provide branch weight for checking mode. (#124084)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 23 07:52:34 PST 2025


Author: Mingming Liu
Date: 2025-01-23T07:52:30-08:00
New Revision: e1aa1e43decf9275175845bea970ef6d7c2b1af6

URL: https://github.com/llvm/llvm-project/commit/e1aa1e43decf9275175845bea970ef6d7c2b1af6
DIFF: https://github.com/llvm/llvm-project/commit/e1aa1e43decf9275175845bea970ef6d7c2b1af6.diff

LOG: [WPD]Provide branch weight for checking mode. (#124084)

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 for a hot callsite as long as the callsite doesn't run the debug trap
branch.

Added: 
    

Modified: 
    llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
    llvm/test/ThinLTO/X86/devirt_check.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp b/llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
index e889926930082f..30e935ea663f34 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..4a9a7a4547fd6c 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 ![[PROF:[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: ![[PROF]] = !{!"branch_weights", i32 1048575, i32 1}
+
 declare i1 @llvm.type.test(i8*, metadata)
 declare void @llvm.assume(i1)
 


        


More information about the llvm-commits mailing list