Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 6d53d18

Browse files
david-xlluqmana
authored andcommitted
[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 2e951c3 commit 6d53d18

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
@@ -197,6 +197,16 @@ class Instruction : public User,
197197
void setMetadata(unsigned KindID, MDNode *Node);
198198
void setMetadata(StringRef Kind, MDNode *Node);
199199

200+
/// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty,
201+
/// specifies the list of meta data that needs to be copied. If \p WL is
202+
/// empty, all meta data will be copied.
203+
void copyMetadata(const Instruction &SrcInst, ArrayRef<unsigned> WL = {});
204+
205+
/// If the instruction has "branch_weights" MD_prof metadata and the MDNode
206+
/// has three operands (including name string), swap the order of the
207+
/// metadata.
208+
void swapProfMetadata();
209+
200210
/// Drop all unknown metadata except for debug locations.
201211
/// @{
202212
/// 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"
@@ -627,6 +628,47 @@ Instruction *Instruction::cloneImpl() const {
627628
llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
628629
}
629630

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

643685
New->SubclassOptionalData = SubclassOptionalData;
644-
if (!hasMetadata())
645-
return New;
646-
647-
// Otherwise, enumerate and copy over metadata from the old instruction to the
648-
// new one.
649-
SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
650-
getAllMetadataOtherThanDebugLoc(TheMDs);
651-
for (const auto &MD : TheMDs)
652-
New->setMetadata(MD.first, MD.second);
653-
654-
New->setDebugLoc(getDebugLoc());
686+
New->copyMetadata(*this);
655687
return New;
656688
}

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-
// 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)