Skip to content

Commit bf34b2e

Browse files
authored
[profcheck] Add heuristical profile metadata for lowering table-based cttz. (#161898)
When lowering a `table-based cttz` calculation to the `llvm.cttz` intrinsic, `AggressiveInstCombine` was not attaching profile metadata to the newly generated `select` instruction. This PR adds heuristic branch weights to the `select`. It uses a strong 100-to-1 probability favoring the `cttz` path over the zero-input case. This allows later passes to optimize code layout and branch prediction.
1 parent 72fe2d2 commit bf34b2e

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
#include "llvm/IR/Dominators.h"
2929
#include "llvm/IR/Function.h"
3030
#include "llvm/IR/IRBuilder.h"
31+
#include "llvm/IR/Instruction.h"
32+
#include "llvm/IR/MDBuilder.h"
3133
#include "llvm/IR/PatternMatch.h"
3234
#include "llvm/IR/ProfDataUtils.h"
35+
#include "llvm/Support/Casting.h"
36+
#include "llvm/Support/CommandLine.h"
3337
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
3438
#include "llvm/Transforms/Utils/BuildLibCalls.h"
3539
#include "llvm/Transforms/Utils/Local.h"
@@ -39,6 +43,10 @@ using namespace PatternMatch;
3943

4044
#define DEBUG_TYPE "aggressive-instcombine"
4145

46+
namespace llvm {
47+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
48+
}
49+
4250
STATISTIC(NumAnyOrAllBitsSet, "Number of any/all-bits-set patterns folded");
4351
STATISTIC(NumGuardedRotates,
4452
"Number of guarded rotates transformed into funnel shifts");
@@ -599,6 +607,14 @@ static bool tryToRecognizeTableBasedCttz(Instruction &I, const DataLayout &DL) {
599607
auto Cmp = B.CreateICmpEQ(X1, ConstantInt::get(XType, 0));
600608
auto Select = B.CreateSelect(Cmp, B.CreateZExt(ZeroTableElem, XType), Cttz);
601609

610+
// The true branch of select handles the cttz(0) case, which is rare.
611+
if (!ProfcheckDisableMetadataFixes) {
612+
if (Instruction *SelectI = dyn_cast<Instruction>(Select))
613+
SelectI->setMetadata(
614+
LLVMContext::MD_prof,
615+
MDBuilder(SelectI->getContext()).createUnlikelyBranchWeights());
616+
}
617+
602618
// NOTE: If the table[0] is 0, but the cttz(0) is defined by the Target
603619
// it should be handled as: `cttz(x) & (typeSize - 1)`.
604620

llvm/test/Transforms/AggressiveInstCombine/lower-table-based-cttz-basics.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@
9191

9292
@ctz7.table = internal unnamed_addr constant [32 x i8] c"\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09", align 1
9393

94-
define i32 @ctz1(i32 %x) {
94+
define i32 @ctz1(i32 %x) !prof !0 {
9595
; CHECK-LABEL: @ctz1(
96+
; CHECK: !prof [[PROF_0:![0-9]+]] {
9697
; CHECK-NEXT: entry:
9798
; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
9899
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X]], 0
99-
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
100+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]], !prof [[PROF_1:![0-9]+]]
100101
; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
101102
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[TMP3]] to i32
102103
; CHECK-NEXT: ret i32 [[CONV]]
@@ -498,3 +499,7 @@ entry:
498499
%conv = zext i8 %0 to i32
499500
ret i32 %conv
500501
}
502+
503+
!0 = !{!"function_entry_count", i64 1000}
504+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
505+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}

llvm/test/Transforms/AggressiveInstCombine/lower-table-based-cttz-dereferencing-pointer.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020

2121
@table = internal unnamed_addr constant [64 x i32] [i32 0, i32 1, i32 12, i32 2, i32 13, i32 22, i32 17, i32 3, i32 14, i32 33, i32 23, i32 36, i32 18, i32 58, i32 28, i32 4, i32 62, i32 15, i32 34, i32 26, i32 24, i32 48, i32 50, i32 37, i32 19, i32 55, i32 59, i32 52, i32 29, i32 44, i32 39, i32 5, i32 63, i32 11, i32 21, i32 16, i32 32, i32 35, i32 57, i32 27, i32 61, i32 25, i32 47, i32 49, i32 54, i32 51, i32 43, i32 38, i32 10, i32 20, i32 31, i32 56, i32 60, i32 46, i32 53, i32 42, i32 9, i32 30, i32 45, i32 41, i32 8, i32 40, i32 7, i32 6], align 4
2222

23-
define i32 @ctz6(ptr nocapture readonly %b) {
23+
define i32 @ctz6(ptr nocapture readonly %b) !prof !0 {
2424
; CHECK-LABEL: @ctz6(
25+
; CHECK: !prof [[PROF_0:![0-9]+]] {
2526
; CHECK-NEXT: entry:
2627
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[B:%.*]], align 8
2728
; CHECK-NEXT: [[TMP1:%.*]] = call i64 @llvm.cttz.i64(i64 [[TMP0]], i1 true)
2829
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[TMP0]], 0
29-
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i64 0, i64 [[TMP1]]
30+
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i64 0, i64 [[TMP1]], !prof [[PROF_1:![0-9]+]]
3031
; CHECK-NEXT: [[TMP4:%.*]] = trunc i64 [[TMP3]] to i32
3132
; CHECK-NEXT: ret i32 [[TMP4]]
3233
;
@@ -40,3 +41,7 @@ entry:
4041
%1 = load i32, ptr %arrayidx, align 4
4142
ret i32 %1
4243
}
44+
45+
!0 = !{!"function_entry_count", i64 1000}
46+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
47+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}

llvm/test/Transforms/AggressiveInstCombine/lower-table-based-cttz-non-argument-value.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
@.str = private constant [3 x i8] c"%u\00", align 1
2121
@test.table = internal constant [32 x i8] c"\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09", align 1
2222

23-
define i32 @test() {
23+
define i32 @test() !prof !0 {
2424
; CHECK-LABEL: @test(
25+
; CHECK: !prof [[PROF_0:![0-9]+]] {
2526
; CHECK-NEXT: entry:
2627
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr @x, align 4
2728
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0]], i1 true)
2829
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP0]], 0
29-
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 0, i32 [[TMP1]]
30+
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 0, i32 [[TMP1]], !prof [[PROF_1:![0-9]+]]
3031
; CHECK-NEXT: [[TMP4:%.*]] = trunc i32 [[TMP3]] to i8
3132
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[TMP4]] to i32
3233
; CHECK-NEXT: ret i32 [[CONV]]
@@ -43,3 +44,7 @@ entry:
4344
%conv = zext i8 %1 to i32
4445
ret i32 %conv
4546
}
47+
48+
!0 = !{!"function_entry_count", i64 1000}
49+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
50+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}

llvm/test/Transforms/AggressiveInstCombine/lower-table-based-cttz-zero-element.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
@ctz1.table = internal constant [32 x i8] c"\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09", align 1
55

6-
define i32 @ctz1(i32 %x) {
6+
define i32 @ctz1(i32 %x) !prof !0 {
77
; CHECK-LABEL: @ctz1(
8+
; CHECK: !prof [[PROF_0:![0-9]+]] {
89
; CHECK-NEXT: entry:
910
; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
1011
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X]], 0
11-
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
12+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]], !prof [[PROF_1:![0-9]+]]
1213
; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
1314
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[TMP3]] to i32
1415
; CHECK-NEXT: ret i32 [[CONV]]
@@ -24,3 +25,7 @@ entry:
2425
%conv = zext i8 %0 to i32
2526
ret i32 %conv
2627
}
28+
29+
!0 = !{!"function_entry_count", i64 1000}
30+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
31+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 1, i32 1048575}

0 commit comments

Comments
 (0)