Skip to content

Commit e1aa1e4

Browse files
[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.
1 parent 6fe0fc6 commit e1aa1e4

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,9 @@ void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
12251225
// perform a debug trap.
12261226
if (DevirtCheckMode == WPDCheckMode::Trap) {
12271227
auto *Cond = Builder.CreateICmpNE(CB.getCalledOperand(), Callee);
1228-
Instruction *ThenTerm =
1229-
SplitBlockAndInsertIfThen(Cond, &CB, /*Unreachable=*/false);
1228+
Instruction *ThenTerm = SplitBlockAndInsertIfThen(
1229+
Cond, &CB, /*Unreachable=*/false,
1230+
MDBuilder(M.getContext()).createUnlikelyBranchWeights());
12301231
Builder.SetInsertPoint(ThenTerm);
12311232
Function *TrapFn =
12321233
Intrinsic::getOrInsertDeclaration(&M, Intrinsic::debugtrap);

llvm/test/ThinLTO/X86/devirt_check.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ entry:
5858
; Ensure !prof and !callees metadata for indirect call promotion removed.
5959
; TRAP-NOT: prof
6060
; TRAP-NOT: callees
61-
; TRAP: br i1 %.not, label %1, label %0
61+
; TRAP: br i1 %.not, label %1, label %0, !prof ![[PROF:[0-9]+]]
6262
; TRAP: 0:
6363
; TRAP: tail call void @llvm.debugtrap()
6464
; TRAP: br label %1
@@ -89,6 +89,8 @@ entry:
8989
; CHECK-LABEL: ret i32
9090
; CHECK-LABEL: }
9191

92+
; TRAP: ![[PROF]] = !{!"branch_weights", i32 1048575, i32 1}
93+
9294
declare i1 @llvm.type.test(i8*, metadata)
9395
declare void @llvm.assume(i1)
9496

0 commit comments

Comments
 (0)