Skip to content

Commit 52cb6e9

Browse files
mtrofinfhahn
andauthored
[ProfCheck][NFC] Make Function argument from branch weight setter optional (#166032)
This picks up from #166028, making the `Function` argument optional: most cases don't need to provide it, but in e.g. InstCombine's case, where the instruction (select, branch) is not attached to a function yet, the function needs to be passed explicitly. Co-authored-by: Florian Hahn <[email protected]>
1 parent 87fb7b0 commit 52cb6e9

File tree

7 files changed

+16
-16
lines changed

7 files changed

+16
-16
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,11 @@ LLVM_ABI void setExplicitlyUnknownBranchWeights(Instruction &I,
194194
/// Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch
195195
/// weights in the new instruction if the parent function of the original
196196
/// instruction has an entry count. This is to not confuse users by injecting
197-
/// profile data into non-profiled functions.
198-
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I,
199-
Function &F,
200-
StringRef PassName);
197+
/// profile data into non-profiled functions. If \p F is nullptr, we will fetch
198+
/// the function from \p I.
199+
LLVM_ABI void
200+
setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName,
201+
const Function *F = nullptr);
201202

202203
/// Analogous to setExplicitlyUnknownBranchWeights, but for functions and their
203204
/// entry counts.

llvm/lib/IR/IRBuilder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,8 +1019,7 @@ Value *IRBuilderBase::CreateSelectWithUnknownProfile(Value *C, Value *True,
10191019
const Twine &Name) {
10201020
Value *Ret = CreateSelectFMF(C, True, False, {}, Name);
10211021
if (auto *SI = dyn_cast<SelectInst>(Ret)) {
1022-
setExplicitlyUnknownBranchWeightsIfProfiled(
1023-
*SI, *SI->getParent()->getParent(), PassName);
1022+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, PassName);
10241023
}
10251024
return Ret;
10261025
}

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,12 @@ void llvm::setExplicitlyUnknownBranchWeights(Instruction &I,
274274
}
275275

276276
void llvm::setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I,
277-
Function &F,
278-
StringRef PassName) {
279-
if (std::optional<Function::ProfileCount> EC = F.getEntryCount();
277+
StringRef PassName,
278+
const Function *F) {
279+
F = F ? F : I.getFunction();
280+
assert(F && "Either pass a instruction attached to a Function, or explicitly "
281+
"pass the Function that it will be attached to");
282+
if (std::optional<Function::ProfileCount> EC = F->getEntryCount();
280283
EC && EC->getCount() > 0)
281284
setExplicitlyUnknownBranchWeights(I, PassName);
282285
}

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,8 +1378,7 @@ static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
13781378
IRB.CreateTrunc(Call->getArgOperand(1), ByteTy), BBNext, N);
13791379
// We can't know the precise weights here, as they would depend on the value
13801380
// distribution of Call->getArgOperand(1). So we just mark it as "unknown".
1381-
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, *Call->getFunction(),
1382-
DEBUG_TYPE);
1381+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
13831382
Type *IndexTy = DL.getIndexType(Call->getType());
13841383
SmallVector<DominatorTree::UpdateType, 8> Updates;
13851384

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
479479
const Twine &NameStr = "",
480480
InsertPosition InsertBefore = nullptr) {
481481
auto *Sel = SelectInst::Create(C, S1, S2, NameStr, InsertBefore, nullptr);
482-
setExplicitlyUnknownBranchWeightsIfProfiled(*Sel, F, DEBUG_TYPE);
482+
setExplicitlyUnknownBranchWeightsIfProfiled(*Sel, DEBUG_TYPE, &F);
483483
return Sel;
484484
}
485485

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ static void buildPartialUnswitchConditionalBranch(
330330
HasBranchWeights ? ComputeProfFrom.getMetadata(LLVMContext::MD_prof)
331331
: nullptr);
332332
if (!HasBranchWeights)
333-
setExplicitlyUnknownBranchWeightsIfProfiled(
334-
*BR, *BR->getParent()->getParent(), DEBUG_TYPE);
333+
setExplicitlyUnknownBranchWeightsIfProfiled(*BR, DEBUG_TYPE);
335334
}
336335

337336
/// Copy a set of loop invariant values, and conditionally branch on them.

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5214,8 +5214,7 @@ bool SimplifyCFGOpt::simplifyBranchOnICmpChain(BranchInst *BI,
52145214
// We don't have any info about this condition.
52155215
auto *Br = TrueWhenEqual ? Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB)
52165216
: Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
5217-
setExplicitlyUnknownBranchWeightsIfProfiled(*Br, *NewBB->getParent(),
5218-
DEBUG_TYPE);
5217+
setExplicitlyUnknownBranchWeightsIfProfiled(*Br, DEBUG_TYPE);
52195218

52205219
OldTI->eraseFromParent();
52215220

0 commit comments

Comments
 (0)