Skip to content

Commit 7faa264

Browse files
committed
[MachineOutliner][NFC] Refactor
1 parent ef71699 commit 7faa264

File tree

5 files changed

+47
-34
lines changed

5 files changed

+47
-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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,13 +2053,20 @@ class TargetInstrInfo : public MCInstrInfo {
20532053

20542054
/// Returns a \p outliner::OutlinedFunction struct containing target-specific
20552055
/// information for a set of outlining candidates. Returns std::nullopt if the
2056-
/// candidates are not suitable for outlining.
2056+
/// candidates are not suitable for outlining. \p MinRep is the minimum
2057+
/// number of times the instruction sequence must be repeated.
20572058
virtual std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
2058-
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
2059+
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
2060+
unsigned MipRep) const {
20592061
llvm_unreachable(
20602062
"Target didn't implement TargetInstrInfo::getOutliningCandidateInfo!");
20612063
}
20622064

2065+
virtual std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
2066+
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
2067+
return getOutliningCandidateInfo(RepeatedSequenceLocs, /*MipRep=*/2);
2068+
}
2069+
20632070
/// Optional target hook to create the LLVM IR attributes for the outlined
20642071
/// function. If overridden, the overriding function must call the default
20652072
/// implementation.

llvm/lib/CodeGen/MachineOutliner.cpp

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

458459
/// Replace the sequences of instructions represented by \p OutlinedFunctions
459460
/// with calls to functions.
460461
///
461462
/// \param M The module we are outlining from.
462463
/// \param FunctionList A list of functions to be inserted into the module.
463464
/// \param Mapper Contains the instruction mappings for the module.
464-
bool outline(Module &M, std::vector<OutlinedFunction> &FunctionList,
465+
/// \param[out] OutlinedFunctionNum The outlined function number.
466+
bool outline(Module &M,
467+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
465468
InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
466469

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

582585
void MachineOutliner::findCandidates(
583-
InstructionMapper &Mapper, std::vector<OutlinedFunction> &FunctionList) {
586+
InstructionMapper &Mapper,
587+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) {
584588
FunctionList.clear();
585589
SuffixTree ST(Mapper.UnsignedVec, OutlinerLeafDescendants);
586590

@@ -682,7 +686,7 @@ void MachineOutliner::findCandidates(
682686
continue;
683687
}
684688

685-
FunctionList.push_back(*OF);
689+
FunctionList.push_back(std::make_unique<OutlinedFunction>(*OF));
686690
}
687691
}
688692

@@ -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());
@@ -1183,7 +1186,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
11831186

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

11881191
// Find all of the outlining candidates.
11891192
findCandidates(Mapper, FunctionList);

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8240,7 +8240,8 @@ static bool outliningCandidatesV8_3OpsConsensus(const outliner::Candidate &a,
82408240

82418241
std::optional<outliner::OutlinedFunction>
82428242
AArch64InstrInfo::getOutliningCandidateInfo(
8243-
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
8243+
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
8244+
unsigned MinRep) const {
82448245
outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
82458246

82468247
unsigned SequenceSize = 0;
@@ -8354,7 +8355,7 @@ AArch64InstrInfo::getOutliningCandidateInfo(
83548355
llvm::erase_if(RepeatedSequenceLocs, hasIllegalSPModification);
83558356

83568357
// If the sequence doesn't have enough candidates left, then we're done.
8357-
if (RepeatedSequenceLocs.size() < 2)
8358+
if (RepeatedSequenceLocs.size() < MinRep)
83588359
return std::nullopt;
83598360
}
83608361

@@ -8598,7 +8599,7 @@ AArch64InstrInfo::getOutliningCandidateInfo(
85988599
}
85998600

86008601
// If we dropped all of the candidates, bail out here.
8601-
if (RepeatedSequenceLocs.size() < 2) {
8602+
if (RepeatedSequenceLocs.size() < MinRep) {
86028603
RepeatedSequenceLocs.clear();
86038604
return std::nullopt;
86048605
}

llvm/lib/Target/AArch64/AArch64InstrInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
463463
bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
464464
bool OutlineFromLinkOnceODRs) const override;
465465
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
466-
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
466+
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
467+
unsigned MinRep) const override;
467468
void mergeOutliningCandidateAttributes(
468469
Function &F, std::vector<outliner::Candidate> &Candidates) const override;
469470
outliner::InstrType

0 commit comments

Comments
 (0)