Skip to content

Commit 0e484f1

Browse files
committed
[MachineOutliner][NFC] Refactor
1 parent 031f7f0 commit 0e484f1

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
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: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -446,16 +446,19 @@ struct MachineOutliner : public ModulePass {
446446
/// \param Mapper Contains outlining mapping information.
447447
/// \param[out] FunctionList Filled with a list of \p OutlinedFunctions
448448
/// each type of candidate.
449-
void findCandidates(InstructionMapper &Mapper,
450-
std::vector<OutlinedFunction> &FunctionList);
449+
void
450+
findCandidates(InstructionMapper &Mapper,
451+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList);
451452

452453
/// Replace the sequences of instructions represented by \p OutlinedFunctions
453454
/// with calls to functions.
454455
///
455456
/// \param M The module we are outlining from.
456457
/// \param FunctionList A list of functions to be inserted into the module.
457458
/// \param Mapper Contains the instruction mappings for the module.
458-
bool outline(Module &M, std::vector<OutlinedFunction> &FunctionList,
459+
/// \param[out] OutlinedFunctionNum The outlined function number.
460+
bool outline(Module &M,
461+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
459462
InstructionMapper &Mapper, unsigned &OutlinedFunctionNum);
460463

461464
/// Creates a function for \p OF and inserts it into the module.
@@ -574,7 +577,8 @@ void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) {
574577
}
575578

576579
void MachineOutliner::findCandidates(
577-
InstructionMapper &Mapper, std::vector<OutlinedFunction> &FunctionList) {
580+
InstructionMapper &Mapper,
581+
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) {
578582
FunctionList.clear();
579583
SuffixTree ST(Mapper.UnsignedVec);
580584

@@ -674,7 +678,7 @@ void MachineOutliner::findCandidates(
674678
continue;
675679
}
676680

677-
FunctionList.push_back(*OF);
681+
FunctionList.push_back(std::make_unique<OutlinedFunction>(*OF));
678682
}
679683
}
680684

@@ -819,69 +823,68 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
819823
return &MF;
820824
}
821825

822-
bool MachineOutliner::outline(Module &M,
823-
std::vector<OutlinedFunction> &FunctionList,
824-
InstructionMapper &Mapper,
825-
unsigned &OutlinedFunctionNum) {
826+
bool MachineOutliner::outline(
827+
Module &M, std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
828+
InstructionMapper &Mapper, unsigned &OutlinedFunctionNum) {
826829
LLVM_DEBUG(dbgs() << "*** Outlining ***\n");
827830
LLVM_DEBUG(dbgs() << "NUMBER OF POTENTIAL FUNCTIONS: " << FunctionList.size()
828831
<< "\n");
829832
bool OutlinedSomething = false;
830833

831834
// Sort by benefit. The most beneficial functions should be outlined first.
832-
stable_sort(FunctionList,
833-
[](const OutlinedFunction &LHS, const OutlinedFunction &RHS) {
834-
return LHS.getBenefit() > RHS.getBenefit();
835-
});
835+
stable_sort(FunctionList, [](const std::unique_ptr<OutlinedFunction> &LHS,
836+
const std::unique_ptr<OutlinedFunction> &RHS) {
837+
return LHS->getBenefit() > RHS->getBenefit();
838+
});
836839

837840
// Walk over each function, outlining them as we go along. Functions are
838841
// outlined greedily, based off the sort above.
839842
auto *UnsignedVecBegin = Mapper.UnsignedVec.begin();
840843
LLVM_DEBUG(dbgs() << "WALKING FUNCTION LIST\n");
841-
for (OutlinedFunction &OF : FunctionList) {
844+
for (auto &OF : FunctionList) {
842845
#ifndef NDEBUG
843-
auto NumCandidatesBefore = OF.Candidates.size();
846+
auto NumCandidatesBefore = OF->Candidates.size();
844847
#endif
845848
// If we outlined something that overlapped with a candidate in a previous
846849
// step, then we can't outline from it.
847-
erase_if(OF.Candidates, [&UnsignedVecBegin](Candidate &C) {
850+
erase_if(OF->Candidates, [&UnsignedVecBegin](Candidate &C) {
848851
return std::any_of(UnsignedVecBegin + C.getStartIdx(),
849852
UnsignedVecBegin + C.getEndIdx() + 1, [](unsigned I) {
850853
return I == static_cast<unsigned>(-1);
851854
});
852855
});
853856

854857
#ifndef NDEBUG
855-
auto NumCandidatesAfter = OF.Candidates.size();
858+
auto NumCandidatesAfter = OF->Candidates.size();
856859
LLVM_DEBUG(dbgs() << "PRUNED: " << NumCandidatesBefore - NumCandidatesAfter
857860
<< "/" << NumCandidatesBefore << " candidates\n");
858861
#endif
859862

860863
// If we made it unbeneficial to outline this function, skip it.
861-
if (OF.getBenefit() < OutlinerBenefitThreshold) {
862-
LLVM_DEBUG(dbgs() << "SKIP: Expected benefit (" << OF.getBenefit()
864+
if (OF->getBenefit() < OutlinerBenefitThreshold) {
865+
LLVM_DEBUG(dbgs() << "SKIP: Expected benefit (" << OF->getBenefit()
863866
<< " B) < threshold (" << OutlinerBenefitThreshold
864867
<< " B)\n");
865868
continue;
866869
}
867870

868-
LLVM_DEBUG(dbgs() << "OUTLINE: Expected benefit (" << OF.getBenefit()
871+
LLVM_DEBUG(dbgs() << "OUTLINE: Expected benefit (" << OF->getBenefit()
869872
<< " B) > threshold (" << OutlinerBenefitThreshold
870873
<< " B)\n");
871874

872875
// It's beneficial. Create the function and outline its sequence's
873876
// occurrences.
874-
OF.MF = createOutlinedFunction(M, OF, Mapper, OutlinedFunctionNum);
875-
emitOutlinedFunctionRemark(OF);
877+
OF->MF = createOutlinedFunction(M, *OF, Mapper, OutlinedFunctionNum);
878+
emitOutlinedFunctionRemark(*OF);
876879
FunctionsCreated++;
877880
OutlinedFunctionNum++; // Created a function, move to the next name.
878-
MachineFunction *MF = OF.MF;
881+
MachineFunction *MF = OF->MF;
879882
const TargetSubtargetInfo &STI = MF->getSubtarget();
880883
const TargetInstrInfo &TII = *STI.getInstrInfo();
881884

882885
// Replace occurrences of the sequence with calls to the new function.
883886
LLVM_DEBUG(dbgs() << "CREATE OUTLINED CALLS\n");
884-
for (Candidate &C : OF.Candidates) {
887+
for (Candidate &C : OF->Candidates) {
885888
MachineBasicBlock &MBB = *C.getMBB();
886889
MachineBasicBlock::iterator StartIt = C.begin();
887890
MachineBasicBlock::iterator EndIt = std::prev(C.end());
@@ -1173,7 +1176,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
11731176

11741177
// Prepare instruction mappings for the suffix tree.
11751178
populateMapper(Mapper, M, MMI);
1176-
std::vector<OutlinedFunction> FunctionList;
1179+
std::vector<std::unique_ptr<OutlinedFunction>> FunctionList;
11771180

11781181
// Find all of the outlining candidates.
11791182
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)