Skip to content

Commit

Permalink
[Instruction] Remove setProfWeight()
Browse files Browse the repository at this point in the history
Remove the function Instruction::setProfWeight() and make
use of Instruction::copyMetadata(.., {LLVMContext::MD_prof}).
This is correct for all use cases of setProfWeight() as it
is applied to CallBase instructions only.
This change results in prof metadata copied intact even if
the source has "VP". The old pair of calls
extractProfTotalWeight() + setProfWeight() resulted in
setting branch_weights if the source had "VP" data.

Reviewers: yamauchi, davidxl
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D80987
  • Loading branch information
Yevgeny Rouban committed Jun 4, 2020
1 parent 2f671c4 commit 417bcb8
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 34 deletions.
3 changes: 0 additions & 3 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,6 @@ class Instruction : public User,
/// Returns false if no metadata was found.
bool extractProfTotalWeight(uint64_t &TotalVal) const;

/// Sets the branch_weights metadata to \p W for CallInst.
void setProfWeight(uint64_t W);

/// Set the debug location information for this instruction.
void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }

Expand Down
9 changes: 0 additions & 9 deletions llvm/lib/IR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,3 @@ Instruction *Instruction::clone() const {
New->copyMetadata(*this);
return New;
}

void Instruction::setProfWeight(uint64_t W) {
assert(isa<CallBase>(this) &&
"Can only set weights for call like instructions");
SmallVector<uint32_t, 1> Weights;
Weights.push_back(W);
MDBuilder MDB(getContext());
setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
}
5 changes: 1 addition & 4 deletions llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,7 @@ doPromotion(Function *F, SmallPtrSetImpl<Argument *> &ArgsToPromote,
NewCS->setAttributes(
AttributeList::get(F->getContext(), CallPAL.getFnAttributes(),
CallPAL.getRetAttributes(), ArgAttrVec));
NewCS->setDebugLoc(CB.getDebugLoc());
uint64_t W;
if (CB.extractProfTotalWeight(W))
NewCS->setProfWeight(W);
NewCS->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
Args.clear();
ArgAttrVec.clear();

Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/Transforms/IPO/Attributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1568,11 +1568,8 @@ ChangeStatus Attributor::rewriteFunctionSignatures(
}

// Copy over various properties and the new attributes.
uint64_t W;
if (OldCB->extractProfTotalWeight(W))
NewCB->setProfWeight(W);
NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
NewCB->setCallingConv(OldCB->getCallingConv());
NewCB->setDebugLoc(OldCB->getDebugLoc());
NewCB->takeName(OldCB);
NewCB->setAttributes(AttributeList::get(
Ctx, OldCallAttributeList.getFnAttributes(),
Expand Down
10 changes: 2 additions & 8 deletions llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ bool DeadArgumentEliminationPass::DeleteDeadVarargs(Function &Fn) {
}
NewCB->setCallingConv(CB->getCallingConv());
NewCB->setAttributes(PAL);
NewCB->setDebugLoc(CB->getDebugLoc());
uint64_t W;
if (CB->extractProfTotalWeight(W))
NewCB->setProfWeight(W);
NewCB->copyMetadata(*CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});

Args.clear();

Expand Down Expand Up @@ -936,10 +933,7 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {
}
NewCB->setCallingConv(CB.getCallingConv());
NewCB->setAttributes(NewCallPAL);
NewCB->setDebugLoc(CB.getDebugLoc());
uint64_t W;
if (CB.extractProfTotalWeight(W))
NewCB->setProfWeight(W);
NewCB->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
Args.clear();
ArgAttrVec.clear();

Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4955,11 +4955,8 @@ bool InstCombiner::transformConstExprCastCall(CallBase &Call) {
NewCall->setCallingConv(Call.getCallingConv());
NewCall->setAttributes(NewCallerPAL);

// Preserve the weight metadata for the new call instruction. The metadata
// is used by SamplePGO to check callsite's hotness.
uint64_t W;
if (Caller->extractProfTotalWeight(W))
NewCall->setProfWeight(W);
// Preserve prof metadata if any.
NewCall->copyMetadata(*Caller, {LLVMContext::MD_prof});

// Insert a cast of the return type as necessary.
Instruction *NC = NewCall;
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/cast-call-combine-prof.ll
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ lpad:
unreachable
}

; CHECK: ![[$PROF]] = !{!"branch_weights", i32 2000}
; CHECK: ![[$PROF]] = !{!"VP", i32 0, i64 2000, i64 -3913987384944532146, i64 2000}
!0 = !{!"VP", i32 0, i64 2000, i64 -3913987384944532146, i64 2000}

!llvm.module.flags = !{!1}
Expand Down

0 comments on commit 417bcb8

Please sign in to comment.