Skip to content

Commit ee836b9

Browse files
committed
[Profile] refactor meta data copying/swapping code
Differential Revision: http://reviews.llvm.org/D23619 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279523 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 20885de commit ee836b9

File tree

4 files changed

+62
-57
lines changed

4 files changed

+62
-57
lines changed

include/llvm/IR/Instruction.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ class Instruction : public User,
198198
void setMetadata(unsigned KindID, MDNode *Node);
199199
void setMetadata(StringRef Kind, MDNode *Node);
200200

201+
/// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty,
202+
/// specifies the list of meta data that needs to be copied. If \p WL is
203+
/// empty, all meta data will be copied.
204+
void copyMetadata(const Instruction &SrcInst, ArrayRef<unsigned> WL = {});
205+
206+
/// If the instruction has "branch_weights" MD_prof metadata and the MDNode
207+
/// has three operands (including name string), swap the order of the
208+
/// metadata.
209+
void swapProfMetadata();
210+
201211
/// Drop all unknown metadata except for debug locations.
202212
/// @{
203213
/// Passes are required to drop metadata they don't understand. This is a

lib/IR/Instruction.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/ADT/DenseSet.h"
1415
#include "llvm/IR/Instruction.h"
1516
#include "llvm/IR/CallSite.h"
1617
#include "llvm/IR/Constants.h"
@@ -632,6 +633,47 @@ Instruction *Instruction::cloneImpl() const {
632633
llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
633634
}
634635

636+
void Instruction::swapProfMetadata() {
637+
MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
638+
if (!ProfileData || ProfileData->getNumOperands() != 3 ||
639+
!isa<MDString>(ProfileData->getOperand(0)))
640+
return;
641+
642+
MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
643+
if (MDName->getString() != "branch_weights")
644+
return;
645+
646+
// The first operand is the name. Fetch them backwards and build a new one.
647+
Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
648+
ProfileData->getOperand(1)};
649+
setMetadata(LLVMContext::MD_prof,
650+
MDNode::get(ProfileData->getContext(), Ops));
651+
}
652+
653+
/// Copy meta data from \p SrcInst to this instruction. If WL is empty, all
654+
/// data will be copied, otherwise only ones specified in WL will be copied.
655+
void Instruction::copyMetadata(const Instruction &SrcInst,
656+
ArrayRef<unsigned> WL) {
657+
if (!SrcInst.hasMetadata())
658+
return;
659+
660+
DenseSet<unsigned> WLS;
661+
for (unsigned M : WL)
662+
WLS.insert(M);
663+
664+
// Otherwise, enumerate and copy over metadata from the old instruction to the
665+
// new one.
666+
SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
667+
SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
668+
for (const auto &MD : TheMDs) {
669+
if (WL.empty() || WLS.count(MD.first))
670+
setMetadata(MD.first, MD.second);
671+
}
672+
if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
673+
setDebugLoc(SrcInst.getDebugLoc());
674+
return;
675+
}
676+
635677
Instruction *Instruction::clone() const {
636678
Instruction *New = nullptr;
637679
switch (getOpcode()) {
@@ -646,16 +688,6 @@ Instruction *Instruction::clone() const {
646688
}
647689

648690
New->SubclassOptionalData = SubclassOptionalData;
649-
if (!hasMetadata())
650-
return New;
651-
652-
// Otherwise, enumerate and copy over metadata from the old instruction to the
653-
// new one.
654-
SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
655-
getAllMetadataOtherThanDebugLoc(TheMDs);
656-
for (const auto &MD : TheMDs)
657-
New->setMetadata(MD.first, MD.second);
658-
659-
New->setDebugLoc(getDebugLoc());
691+
New->copyMetadata(*this);
660692
return New;
661693
}

lib/IR/Instructions.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,15 +1209,7 @@ void BranchInst::swapSuccessors() {
12091209

12101210
// Update profile metadata if present and it matches our structural
12111211
// expectations.
1212-
MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
1213-
if (!ProfileData || ProfileData->getNumOperands() != 3)
1214-
return;
1215-
1216-
// The first operand is the name. Fetch them backwards and build a new one.
1217-
Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1218-
ProfileData->getOperand(1)};
1219-
setMetadata(LLVMContext::MD_prof,
1220-
MDNode::get(ProfileData->getContext(), Ops));
1212+
swapProfMetadata();
12211213
}
12221214

12231215
BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {

lib/Transforms/Scalar/LoopUnswitch.cpp

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -742,42 +742,6 @@ static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
742742
return &New;
743743
}
744744

745-
static void copyMetadata(Instruction *DstInst, const Instruction *SrcInst,
746-
bool Swapped) {
747-
if (!SrcInst || !SrcInst->hasMetadata())
748-
return;
749-
750-
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
751-
SrcInst->getAllMetadata(MDs);
752-
for (auto &MD : MDs) {
753-
switch (MD.first) {
754-
default:
755-
break;
756-
case LLVMContext::MD_prof:
757-
if (Swapped && MD.second->getNumOperands() == 3 &&
758-
isa<MDString>(MD.second->getOperand(0))) {
759-
MDString *MDName = cast<MDString>(MD.second->getOperand(0));
760-
if (MDName->getString() == "branch_weights") {
761-
auto *ValT = cast_or_null<ConstantAsMetadata>(
762-
MD.second->getOperand(1))->getValue();
763-
auto *ValF = cast_or_null<ConstantAsMetadata>(
764-
MD.second->getOperand(2))->getValue();
765-
assert(ValT && ValF && "Invalid Operands of branch_weights");
766-
auto NewMD =
767-
MDBuilder(DstInst->getParent()->getContext())
768-
.createBranchWeights(cast<ConstantInt>(ValF)->getZExtValue(),
769-
cast<ConstantInt>(ValT)->getZExtValue());
770-
MD.second = NewMD;
771-
}
772-
}
773-
LLVM_FALLTHROUGH;
774-
case LLVMContext::MD_make_implicit:
775-
case LLVMContext::MD_dbg:
776-
DstInst->setMetadata(MD.first, MD.second);
777-
}
778-
}
779-
}
780-
781745
/// Emit a conditional branch on two values if LIC == Val, branch to TrueDst,
782746
/// otherwise branch to FalseDest. Insert the code immediately before InsertPt.
783747
void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
@@ -800,7 +764,14 @@ void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
800764

801765
// Insert the new branch.
802766
BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt);
803-
copyMetadata(BI, TI, Swapped);
767+
if (TI) {
768+
// FIXME: check why white list is needed here:
769+
ArrayRef<unsigned> WL = {LLVMContext::MD_dbg, LLVMContext::MD_prof,
770+
LLVMContext::MD_make_implicit};
771+
BI->copyMetadata(*TI, WL);
772+
if (Swapped)
773+
BI->swapProfMetadata();
774+
}
804775

805776
// If either edge is critical, split it. This helps preserve LoopSimplify
806777
// form for enclosing loops.

0 commit comments

Comments
 (0)