Skip to content

Commit e7d4aa9

Browse files
committed
[SLU][profcheck] create likely branch weights for guard->branch
1 parent 770453d commit e7d4aa9

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/IR/Instruction.h"
4141
#include "llvm/IR/Instructions.h"
4242
#include "llvm/IR/IntrinsicInst.h"
43+
#include "llvm/IR/MDBuilder.h"
4344
#include "llvm/IR/Module.h"
4445
#include "llvm/IR/PatternMatch.h"
4546
#include "llvm/IR/ProfDataUtils.h"
@@ -2831,9 +2832,14 @@ static BranchInst *turnGuardIntoBranch(IntrinsicInst *GI, Loop &L,
28312832
MSSAU->getMemorySSA()->verifyMemorySSA();
28322833

28332834
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
2834-
Instruction *DeoptBlockTerm =
2835-
SplitBlockAndInsertIfThen(GI->getArgOperand(0), GI, true,
2836-
GI->getMetadata(LLVMContext::MD_prof), &DTU, &LI);
2835+
// llvm.experimental.guard doesn't have branch weights. We can assume,
2836+
// however, that the deopt path is unlikely.
2837+
Instruction *DeoptBlockTerm = SplitBlockAndInsertIfThen(
2838+
GI->getArgOperand(0), GI, true,
2839+
!ProfcheckDisableMetadataFixes && EstimateProfile
2840+
? MDBuilder(GI->getContext()).createUnlikelyBranchWeights()
2841+
: nullptr,
2842+
&DTU, &LI);
28372843
BranchInst *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
28382844
// SplitBlockAndInsertIfThen inserts control flow that branches to
28392845
// DeoptBlockTerm if the condition is true. We want the opposite.

llvm/test/Transforms/SimpleLoopUnswitch/guards.ll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: -p --version 5
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: -p --check-globals all --version 5
22
; RUN: opt -passes='loop(simple-loop-unswitch<nontrivial>),verify<loops>' -simple-loop-unswitch-guards -S < %s | FileCheck %s
33
; RUN: opt -passes='simple-loop-unswitch<nontrivial>' -simple-loop-unswitch-guards -S < %s | FileCheck %s
44
; RUN: opt -passes='loop-mssa(simple-loop-unswitch<nontrivial>),verify<loops>' -simple-loop-unswitch-guards -verify-memoryssa -verify-loop-info -S < %s | FileCheck %s
@@ -167,7 +167,7 @@ exit:
167167
define void @test_nested_loop(i1 %cond, i32 %N, i1 %arg) {
168168
; CHECK-LABEL: define void @test_nested_loop(i1 %cond, i32 %N, i1 %arg) {
169169
; CHECK-NEXT: entry:
170-
; CHECK-NEXT: br i1 %cond, label %entry.split, label %outer_loop.split
170+
; CHECK-NEXT: br i1 %cond, label %entry.split, label %outer_loop.split, !prof !1
171171
; CHECK: entry.split:
172172
; CHECK-NEXT: br i1 %arg, label %entry.split.split.us, label %entry.split.split
173173
; CHECK: entry.split.split.us:
@@ -337,3 +337,9 @@ exit:
337337

338338
declare void @may_throw(i32 %i)
339339
declare i32 @__CxxFrameHandler3(...)
340+
341+
!0 = !{!"function_entry_count", i32 10}
342+
;.
343+
; CHECK: !0 = !{!"function_entry_count", i32 10}
344+
; CHECK: !1 = !{!"branch_weights", i32 1048575, i32 1}
345+
;.

0 commit comments

Comments
 (0)