Skip to content

Commit cc4e8b4

Browse files
committed
add profile unknown only if the enclosing function has profile counts
1 parent b42e0c5 commit cc4e8b4

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
185185
LLVM_ABI void setExplicitlyUnknownBranchWeights(Instruction &I,
186186
StringRef PassName);
187187

188+
/// Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch
189+
/// weights in the new instruction if the parent function of the original
190+
/// instruction has function counts. This is to not confuse users by injecting
191+
/// profile data into non-profiled functions.
192+
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &New,
193+
Instruction &Original,
194+
StringRef PassName);
195+
188196
/// Analogous to setExplicitlyUnknownBranchWeights, but for functions and their
189197
/// entry counts.
190198
LLVM_ABI void setExplicitlyUnknownFunctionEntryCount(Function &F,

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ void setExplicitlyUnknownBranchWeights(Instruction &I, StringRef PassName) {
252252
MDB.createString(PassName)}));
253253
}
254254

255+
void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &New,
256+
Instruction &Original,
257+
StringRef PassName) {
258+
Function *F = Original.getFunction();
259+
assert(F && "instruction does not belong to a function!");
260+
std::optional<Function::ProfileCount> EC = F->getEntryCount();
261+
if (EC && EC->getCount() > 0)
262+
setExplicitlyUnknownBranchWeights(New, PassName);
263+
}
264+
255265
void setExplicitlyUnknownFunctionEntryCount(Function &F, StringRef PassName) {
256266
MDBuilder MDB(F.getContext());
257267
F.setMetadata(

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
888888
X->getType()->getScalarSizeInBits() == 1)
889889
SI = SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
890890
if (SI) {
891-
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
891+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, Add, DEBUG_TYPE);
892892
return SI;
893893
}
894894

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
12551255
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
12561256
auto *NewC = Builder.CreateShl(ConstantInt::get(Ty, 1), C1);
12571257
auto *SI = SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1258-
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
1258+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, I, DEBUG_TYPE);
12591259
return SI;
12601260
}
12611261
}

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
17371737
Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
17381738
Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
17391739
SelectInst *SI = SelectInst::Create(X, TVal, FVal);
1740-
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
1740+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, BO, DEBUG_TYPE);
17411741
return SI;
17421742
}
17431743

llvm/test/Transforms/InstCombine/preserve-profile.ll

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ define i32 @NegBin(i1 %C) !prof !0 {
4646
ret i32 %V
4747
}
4848

49-
define i32 @select_C_minus_1_or_C_from_bool(i1 %x) {
49+
define i32 @select_C_minus_1_or_C_from_bool(i1 %x) !prof !0 {
5050
; CHECK-LABEL: define i32 @select_C_minus_1_or_C_from_bool(
51-
; CHECK-SAME: i1 [[X:%.*]]) {
51+
; CHECK-SAME: i1 [[X:%.*]]) !prof [[PROF0]] {
5252
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[X]], i32 41, i32 42, !prof [[PROF2:![0-9]+]]
5353
; CHECK-NEXT: ret i32 [[ADD]]
5454
;
@@ -57,9 +57,9 @@ define i32 @select_C_minus_1_or_C_from_bool(i1 %x) {
5757
ret i32 %add
5858
}
5959

60-
define i5 @and_add(i1 %x, i1 %y) {
60+
define i5 @and_add(i1 %x, i1 %y) !prof !0 {
6161
; CHECK-LABEL: define i5 @and_add(
62-
; CHECK-SAME: i1 [[X:%.*]], i1 [[Y:%.*]]) {
62+
; CHECK-SAME: i1 [[X:%.*]], i1 [[Y:%.*]]) !prof [[PROF0]] {
6363
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[X]], true
6464
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[Y]], [[TMP1]]
6565
; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i5 -2, i5 0, !prof [[PROF2]]
@@ -72,9 +72,9 @@ define i5 @and_add(i1 %x, i1 %y) {
7272
ret i5 %r
7373
}
7474

75-
define i32 @add_zext_zext_i1(i1 %a) {
75+
define i32 @add_zext_zext_i1(i1 %a) !prof !0 {
7676
; CHECK-LABEL: define i32 @add_zext_zext_i1(
77-
; CHECK-SAME: i1 [[A:%.*]]) {
77+
; CHECK-SAME: i1 [[A:%.*]]) !prof [[PROF0]] {
7878
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[A]], i32 2, i32 0, !prof [[PROF2]]
7979
; CHECK-NEXT: ret i32 [[ADD]]
8080
;
@@ -83,6 +83,17 @@ define i32 @add_zext_zext_i1(i1 %a) {
8383
ret i32 %add
8484
}
8585

86+
define i32 @no_count_no_branch_weights(i1 %a) {
87+
; CHECK-LABEL: define i32 @no_count_no_branch_weights(
88+
; CHECK-SAME: i1 [[A:%.*]]) {
89+
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[A]], i32 2, i32 0
90+
; CHECK-NEXT: ret i32 [[ADD]]
91+
;
92+
%zext = zext i1 %a to i32
93+
%add = add i32 %zext, %zext
94+
ret i32 %add
95+
}
96+
8697

8798
!0 = !{!"function_entry_count", i64 1000}
8899
!1 = !{!"branch_weights", i32 2, i32 3}

0 commit comments

Comments
 (0)