Skip to content

Commit 35716fb

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

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
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: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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
55

66
declare void @llvm.experimental.guard(i1, ...)
77

8-
define void @test_simple_case(i1 %cond, i32 %N) {
9-
; CHECK-LABEL: define void @test_simple_case(i1 %cond, i32 %N) {
8+
define void @test_simple_case(i1 %cond, i32 %N) !prof !0 {
9+
; CHECK-LABEL: define void @test_simple_case(i1 %cond, i32 %N) !prof !0 {
1010
; CHECK-NEXT: entry:
11-
; CHECK-NEXT: br i1 %cond, label %entry.split.us, label %entry.split
11+
; CHECK-NEXT: br i1 %cond, label %entry.split.us, label %entry.split, !prof !1
1212
; CHECK: entry.split.us:
1313
; CHECK-NEXT: br label %loop.us
1414
; CHECK: loop.us:
@@ -48,9 +48,9 @@ exit:
4848
define void @test_two_guards(i1 %cond1, i1 %cond2, i32 %N) {
4949
; CHECK-LABEL: define void @test_two_guards(i1 %cond1, i1 %cond2, i32 %N) {
5050
; CHECK-NEXT: entry:
51-
; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
51+
; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split, !prof !1
5252
; CHECK: entry.split.us:
53-
; CHECK-NEXT: br i1 %cond2, label %entry.split.us.split.us, label %entry.split.us.split
53+
; CHECK-NEXT: br i1 %cond2, label %entry.split.us.split.us, label %entry.split.us.split, !prof !1
5454
; CHECK: entry.split.us.split.us:
5555
; CHECK-NEXT: br label %loop.us.us
5656
; CHECK: loop.us.us:
@@ -105,7 +105,7 @@ define void @test_conditional_guards(i1 %cond, i32 %N) {
105105
; CHECK-LABEL: define void @test_conditional_guards(i1 %cond, i32 %N) {
106106
; CHECK-NEXT: entry:
107107
; CHECK-NEXT: %cond.fr = freeze i1 %cond
108-
; CHECK-NEXT: br i1 %cond.fr, label %entry.split.us, label %entry.split
108+
; CHECK-NEXT: br i1 %cond.fr, label %entry.split.us, label %entry.split, !prof !1
109109
; CHECK: entry.split.us:
110110
; CHECK-NEXT: br label %loop.us
111111
; CHECK: loop.us:
@@ -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:
@@ -238,7 +238,7 @@ exit:
238238
define void @test_sibling_loops(i1 %cond1, i1 %cond2, i32 %N) {
239239
; CHECK-LABEL: define void @test_sibling_loops(i1 %cond1, i1 %cond2, i32 %N) {
240240
; CHECK-NEXT: entry:
241-
; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split
241+
; CHECK-NEXT: br i1 %cond1, label %entry.split.us, label %entry.split, !prof !1
242242
; CHECK: entry.split.us:
243243
; CHECK-NEXT: br label %loop1.us
244244
; CHECK: loop1.us:
@@ -258,7 +258,7 @@ define void @test_sibling_loops(i1 %cond1, i1 %cond2, i32 %N) {
258258
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
259259
; CHECK-NEXT: unreachable
260260
; CHECK: between:
261-
; CHECK-NEXT: br i1 %cond2, label %between.split.us2, label %between.split
261+
; CHECK-NEXT: br i1 %cond2, label %between.split.us2, label %between.split, !prof !1
262262
; CHECK: between.split.us2:
263263
; CHECK-NEXT: br label %loop2.us
264264
; CHECK: loop2.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)