Skip to content

Commit 3ed5913

Browse files
committed
[MachineOutliner][NFC] Refactor
1 parent dfc3494 commit 3ed5913

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

llvm/include/llvm/CodeGen/MachineOutliner.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ struct OutlinedFunction {
234234
unsigned FrameConstructionID = 0;
235235

236236
/// Return the number of candidates for this \p OutlinedFunction.
237-
unsigned getOccurrenceCount() const { return Candidates.size(); }
237+
virtual unsigned getOccurrenceCount() const { return Candidates.size(); }
238238

239239
/// Return the number of bytes it would take to outline this
240240
/// function.
241-
unsigned getOutliningCost() const {
241+
virtual unsigned getOutliningCost() const {
242242
unsigned CallOverhead = 0;
243243
for (const Candidate &C : Candidates)
244244
CallOverhead += C.getCallOverhead();
@@ -272,6 +272,7 @@ struct OutlinedFunction {
272272
}
273273

274274
OutlinedFunction() = delete;
275+
virtual ~OutlinedFunction() = default;
275276
};
276277
} // namespace outliner
277278
} // namespace llvm

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,14 +2088,22 @@ class TargetInstrInfo : public MCInstrInfo {
20882088

20892089
/// Returns a \p outliner::OutlinedFunction struct containing target-specific
20902090
/// information for a set of outlining candidates. Returns std::nullopt if the
2091-
/// candidates are not suitable for outlining.
2091+
/// candidates are not suitable for outlining. \p MinRep is the minimum
2092+
/// number of times the instruction sequence must be repeated.
20922093
virtual std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
20932094
const MachineModuleInfo &MMI,
2094-
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
2095+
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
2096+
unsigned MipRep) const {
20952097
llvm_unreachable(
20962098
"Target didn't implement TargetInstrInfo::getOutliningCandidateInfo!");
20972099
}
20982100

2101+
virtual std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
2102+
const MachineModuleInfo &MMI,
2103+
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
2104+
return getOutliningCandidateInfo(MMI, RepeatedSequenceLocs, /*MipRep=*/2);
2105+
}
2106+
20992107
/// Optional target hook to create the LLVM IR attributes for the outlined
21002108
/// function. If overridden, the overriding function must call the default
21012109
/// implementation.

llvm/lib/CodeGen/MachineOutliner.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,19 @@ struct MachineOutliner : public ModulePass {
456456
/// \param Mapper Contains outlining mapping information.
457457
/// \param[out] FunctionList Filled with a list of \p OutlinedFunctions
458458
/// each type of candidate.
459-
void findCandidates(InstructionMapper &Mapper,
460-
std::vector<OutlinedFunction> &FunctionList);
459+
void
460+
findCandidates(InstructionMapper &Mapper,
461+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList);
461462

462463
/// Replace the sequences of instructions represented by \p OutlinedFunctions
463464
/// with calls to functions.
464465
///
465466
/// \param M The module we are outlining from.
466467
/// \param FunctionList A list of functions to be inserted into the module.
467468
/// \param Mapper Contains the instruction mappings for the module.
468-
bool outline(Module &M, std::vector<OutlinedFunction> &FunctionList,
469+
/// \param[out] OutlinedFunctionNum The outlined function number.
470+
bool outline(Module &M,
471+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
469472
InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
470473

471474
/// Creates a function for \p OF and inserts it into the module.
@@ -583,7 +586,8 @@ void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) {
583586
}
584587

585588
void MachineOutliner::findCandidates(
586-
InstructionMapper &Mapper, std::vector<OutlinedFunction> &FunctionList) {
589+
InstructionMapper &Mapper,
590+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) {
587591
FunctionList.clear();
588592
SuffixTree ST(Mapper.UnsignedVec, OutlinerLeafDescendants);
589593

@@ -684,7 +688,7 @@ void MachineOutliner::findCandidates(
684688
continue;
685689
}
686690

687-
FunctionList.push_back(*OF);
691+
FunctionList.push_back(std::make_unique<OutlinedFunction>(*OF));
688692
}
689693
}
690694

@@ -827,71 +831,70 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
827831
return &MF;
828832
}
829833

830-
bool MachineOutliner::outline(Module &M,
831-
std::vector<OutlinedFunction> &FunctionList,
832-
InstructionMapper &Mapper,
833-
unsigned &OutlinedFunctionNum) {
834+
bool MachineOutliner::outline(
835+
Module &M, std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
836+
InstructionMapper &Mapper, unsigned &OutlinedFunctionNum) {
834837
LLVM_DEBUG(dbgs() << "*** Outlining ***\n");
835838
LLVM_DEBUG(dbgs() << "NUMBER OF POTENTIAL FUNCTIONS: " << FunctionList.size()
836839
<< "\n");
837840
bool OutlinedSomething = false;
838841

839842
// Sort by priority where priority := getNotOutlinedCost / getOutliningCost.
840843
// The function with highest priority should be outlined first.
841-
stable_sort(FunctionList,
842-
[](const OutlinedFunction &LHS, const OutlinedFunction &RHS) {
843-
return LHS.getNotOutlinedCost() * RHS.getOutliningCost() >
844-
RHS.getNotOutlinedCost() * LHS.getOutliningCost();
845-
});
844+
stable_sort(FunctionList, [](const std::unique_ptr<OutlinedFunction> &LHS,
845+
const std::unique_ptr<OutlinedFunction> &RHS) {
846+
return LHS->getNotOutlinedCost() * RHS->getOutliningCost() >
847+
RHS->getNotOutlinedCost() * LHS->getOutliningCost();
848+
});
846849

847850
// Walk over each function, outlining them as we go along. Functions are
848851
// outlined greedily, based off the sort above.
849852
auto *UnsignedVecBegin = Mapper.UnsignedVec.begin();
850853
LLVM_DEBUG(dbgs() << "WALKING FUNCTION LIST\n");
851-
for (OutlinedFunction &OF : FunctionList) {
854+
for (auto &OF : FunctionList) {
852855
#ifndef NDEBUG
853-
auto NumCandidatesBefore = OF.Candidates.size();
856+
auto NumCandidatesBefore = OF->Candidates.size();
854857
#endif
855858
// If we outlined something that overlapped with a candidate in a previous
856859
// step, then we can't outline from it.
857-
erase_if(OF.Candidates, [&UnsignedVecBegin](Candidate &C) {
860+
erase_if(OF->Candidates, [&UnsignedVecBegin](Candidate &C) {
858861
return std::any_of(UnsignedVecBegin + C.getStartIdx(),
859862
UnsignedVecBegin + C.getEndIdx() + 1, [](unsigned I) {
860863
return I == static_cast<unsigned>(-1);
861864
});
862865
});
863866

864867
#ifndef NDEBUG
865-
auto NumCandidatesAfter = OF.Candidates.size();
868+
auto NumCandidatesAfter = OF->Candidates.size();
866869
LLVM_DEBUG(dbgs() << "PRUNED: " << NumCandidatesBefore - NumCandidatesAfter
867870
<< "/" << NumCandidatesBefore << " candidates\n");
868871
#endif
869872

870873
// If we made it unbeneficial to outline this function, skip it.
871-
if (OF.getBenefit() < OutlinerBenefitThreshold) {
872-
LLVM_DEBUG(dbgs() << "SKIP: Expected benefit (" << OF.getBenefit()
874+
if (OF->getBenefit() < OutlinerBenefitThreshold) {
875+
LLVM_DEBUG(dbgs() << "SKIP: Expected benefit (" << OF->getBenefit()
873876
<< " B) < threshold (" << OutlinerBenefitThreshold
874877
<< " B)\n");
875878
continue;
876879
}
877880

878-
LLVM_DEBUG(dbgs() << "OUTLINE: Expected benefit (" << OF.getBenefit()
881+
LLVM_DEBUG(dbgs() << "OUTLINE: Expected benefit (" << OF->getBenefit()
879882
<< " B) > threshold (" << OutlinerBenefitThreshold
880883
<< " B)\n");
881884

882885
// It's beneficial. Create the function and outline its sequence's
883886
// occurrences.
884-
OF.MF = createOutlinedFunction(M, OF, Mapper, OutlinedFunctionNum);
885-
emitOutlinedFunctionRemark(OF);
887+
OF->MF = createOutlinedFunction(M, *OF, Mapper, OutlinedFunctionNum);
888+
emitOutlinedFunctionRemark(*OF);
886889
FunctionsCreated++;
887890
OutlinedFunctionNum++; // Created a function, move to the next name.
888-
MachineFunction *MF = OF.MF;
891+
MachineFunction *MF = OF->MF;
889892
const TargetSubtargetInfo &STI = MF->getSubtarget();
890893
const TargetInstrInfo &TII = *STI.getInstrInfo();
891894

892895
// Replace occurrences of the sequence with calls to the new function.
893896
LLVM_DEBUG(dbgs() << "CREATE OUTLINED CALLS\n");
894-
for (Candidate &C : OF.Candidates) {
897+
for (Candidate &C : OF->Candidates) {
895898
MachineBasicBlock &MBB = *C.getMBB();
896899
MachineBasicBlock::iterator StartIt = C.begin();
897900
MachineBasicBlock::iterator EndIt = std::prev(C.end());
@@ -1180,7 +1183,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
11801183

11811184
// Prepare instruction mappings for the suffix tree.
11821185
populateMapper(Mapper, M);
1183-
std::vector<OutlinedFunction> FunctionList;
1186+
std::vector<std::unique_ptr<OutlinedFunction>> FunctionList;
11841187

11851188
// Find all of the outlining candidates.
11861189
findCandidates(Mapper, FunctionList);

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8687,7 +8687,8 @@ static bool outliningCandidatesV8_3OpsConsensus(const outliner::Candidate &a,
86878687
std::optional<outliner::OutlinedFunction>
86888688
AArch64InstrInfo::getOutliningCandidateInfo(
86898689
const MachineModuleInfo &MMI,
8690-
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
8690+
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
8691+
unsigned MinRep) const {
86918692
unsigned SequenceSize = 0;
86928693
for (auto &MI : RepeatedSequenceLocs[0])
86938694
SequenceSize += getInstSizeInBytes(MI);
@@ -8801,7 +8802,7 @@ AArch64InstrInfo::getOutliningCandidateInfo(
88018802
llvm::erase_if(RepeatedSequenceLocs, hasIllegalSPModification);
88028803

88038804
// If the sequence doesn't have enough candidates left, then we're done.
8804-
if (RepeatedSequenceLocs.size() < 2)
8805+
if (RepeatedSequenceLocs.size() < MinRep)
88058806
return std::nullopt;
88068807
}
88078808

@@ -9048,7 +9049,7 @@ AArch64InstrInfo::getOutliningCandidateInfo(
90489049
}
90499050

90509051
// If we dropped all of the candidates, bail out here.
9051-
if (RepeatedSequenceLocs.size() < 2) {
9052+
if (RepeatedSequenceLocs.size() < MinRep) {
90529053
RepeatedSequenceLocs.clear();
90539054
return std::nullopt;
90549055
}

llvm/lib/Target/AArch64/AArch64InstrInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
473473
bool OutlineFromLinkOnceODRs) const override;
474474
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
475475
const MachineModuleInfo &MMI,
476-
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
476+
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
477+
unsigned MinRep) const override;
477478
void mergeOutliningCandidateAttributes(
478479
Function &F, std::vector<outliner::Candidate> &Candidates) const override;
479480
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI,

0 commit comments

Comments
 (0)