Skip to content

Commit

Permalink
[WPD]Provide branch weight for checking mode. (#124084)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mingmingl-llvm authored Jan 23, 2025
1 parent 6fe0fc6 commit e1aa1e4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion llvm/test/ThinLTO/X86/devirt_check.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit e1aa1e4

Please sign in to comment.