Skip to content

MachineOutliner: Use PM to query MachineModuleInfo #99688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions llvm/include/llvm/CodeGen/TargetInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,7 @@ class TargetInstrInfo : public MCInstrInfo {
/// information for a set of outlining candidates. Returns std::nullopt if the
/// candidates are not suitable for outlining.
virtual std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
llvm_unreachable(
"Target didn't implement TargetInstrInfo::getOutliningCandidateInfo!");
Expand All @@ -2103,16 +2104,18 @@ class TargetInstrInfo : public MCInstrInfo {
protected:
/// Target-dependent implementation for getOutliningTypeImpl.
virtual outliner::InstrType
getOutliningTypeImpl(MachineBasicBlock::iterator &MIT, unsigned Flags) const {
getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT, unsigned Flags) const {
llvm_unreachable(
"Target didn't implement TargetInstrInfo::getOutliningTypeImpl!");
}

public:
/// Returns how or if \p MIT should be outlined. \p Flags is the
/// target-specific information returned by isMBBSafeToOutlineFrom.
outliner::InstrType
getOutliningType(MachineBasicBlock::iterator &MIT, unsigned Flags) const;
outliner::InstrType getOutliningType(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const;

/// Optional target hook that returns true if \p MBB is safe to outline from,
/// and returns any target-specific information in \p Flags.
Expand Down
43 changes: 21 additions & 22 deletions llvm/lib/CodeGen/MachineOutliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace {

/// Maps \p MachineInstrs to unsigned integers and stores the mappings.
struct InstructionMapper {
const MachineModuleInfo &MMI;

/// The next available integer to assign to a \p MachineInstr that
/// cannot be outlined.
Expand Down Expand Up @@ -333,7 +334,7 @@ struct InstructionMapper {
// which may be outlinable. Check if each instruction is known to be safe.
for (; It != OutlinableRangeEnd; ++It) {
// Keep track of where this instruction is in the module.
switch (TII.getOutliningType(It, Flags)) {
switch (TII.getOutliningType(MMI, It, Flags)) {
case InstrType::Illegal:
mapToIllegalUnsigned(It, CanOutlineWithPrevInstr, UnsignedVecForMBB,
InstrListForMBB);
Expand Down Expand Up @@ -382,7 +383,7 @@ struct InstructionMapper {
}
}

InstructionMapper() {
InstructionMapper(const MachineModuleInfo &MMI_) : MMI(MMI_) {
// Make sure that the implementation of DenseMapInfo<unsigned> hasn't
// changed.
assert(DenseMapInfo<unsigned>::getEmptyKey() == (unsigned)-1 &&
Expand All @@ -405,6 +406,8 @@ struct MachineOutliner : public ModulePass {

static char ID;

MachineModuleInfo *MMI = nullptr;

/// Set to true if the outliner should consider functions with
/// linkonceodr linkage.
bool OutlineFromLinkOnceODRs = false;
Expand Down Expand Up @@ -489,20 +492,19 @@ struct MachineOutliner : public ModulePass {

/// Populate and \p InstructionMapper with instruction-to-integer mappings.
/// These are used to construct a suffix tree.
void populateMapper(InstructionMapper &Mapper, Module &M,
MachineModuleInfo &MMI);
void populateMapper(InstructionMapper &Mapper, Module &M);

/// Initialize information necessary to output a size remark.
/// FIXME: This should be handled by the pass manager, not the outliner.
/// FIXME: This is nearly identical to the initSizeRemarkInfo in the legacy
/// pass manager.
void initSizeRemarkInfo(const Module &M, const MachineModuleInfo &MMI,
void initSizeRemarkInfo(const Module &M,
StringMap<unsigned> &FunctionToInstrCount);

/// Emit the remark.
// FIXME: This should be handled by the pass manager, not the outliner.
void
emitInstrCountChangedRemark(const Module &M, const MachineModuleInfo &MMI,
emitInstrCountChangedRemark(const Module &M,
const StringMap<unsigned> &FunctionToInstrCount);
};
} // Anonymous namespace.
Expand Down Expand Up @@ -669,7 +671,7 @@ void MachineOutliner::findCandidates(
CandidatesForRepeatedSeq[0].getMF()->getSubtarget().getInstrInfo();

std::optional<OutlinedFunction> OF =
TII->getOutliningCandidateInfo(CandidatesForRepeatedSeq);
TII->getOutliningCandidateInfo(*MMI, CandidatesForRepeatedSeq);

// If we deleted too many candidates, then there's nothing worth outlining.
// FIXME: This should take target-specified instruction sizes into account.
Expand Down Expand Up @@ -988,8 +990,7 @@ bool MachineOutliner::outline(Module &M,
return OutlinedSomething;
}

void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,
MachineModuleInfo &MMI) {
void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M) {
// Build instruction mappings for each function in the module. Start by
// iterating over each Function in M.
LLVM_DEBUG(dbgs() << "*** Populating mapper ***\n");
Expand All @@ -1003,7 +1004,7 @@ void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,

// There's something in F. Check if it has a MachineFunction associated with
// it.
MachineFunction *MF = MMI.getMachineFunction(F);
MachineFunction *MF = MMI->getMachineFunction(F);

// If it doesn't, then there's nothing to outline from. Move to the next
// Function.
Expand Down Expand Up @@ -1062,12 +1063,11 @@ void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M,
}

void MachineOutliner::initSizeRemarkInfo(
const Module &M, const MachineModuleInfo &MMI,
StringMap<unsigned> &FunctionToInstrCount) {
const Module &M, StringMap<unsigned> &FunctionToInstrCount) {
// Collect instruction counts for every function. We'll use this to emit
// per-function size remarks later.
for (const Function &F : M) {
MachineFunction *MF = MMI.getMachineFunction(F);
MachineFunction *MF = MMI->getMachineFunction(F);

// We only care about MI counts here. If there's no MachineFunction at this
// point, then there won't be after the outliner runs, so let's move on.
Expand All @@ -1078,13 +1078,12 @@ void MachineOutliner::initSizeRemarkInfo(
}

void MachineOutliner::emitInstrCountChangedRemark(
const Module &M, const MachineModuleInfo &MMI,
const StringMap<unsigned> &FunctionToInstrCount) {
const Module &M, const StringMap<unsigned> &FunctionToInstrCount) {
// Iterate over each function in the module and emit remarks.
// Note that we won't miss anything by doing this, because the outliner never
// deletes functions.
for (const Function &F : M) {
MachineFunction *MF = MMI.getMachineFunction(F);
MachineFunction *MF = MMI->getMachineFunction(F);

// The outliner never deletes functions. If we don't have a MF here, then we
// didn't have one prior to outlining either.
Expand Down Expand Up @@ -1135,6 +1134,8 @@ bool MachineOutliner::runOnModule(Module &M) {
if (M.empty())
return false;

MMI = &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();

// Number to append to the current outlined function.
unsigned OutlinedFunctionNum = 0;

Expand All @@ -1158,8 +1159,6 @@ bool MachineOutliner::runOnModule(Module &M) {
}

bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();

// If the user passed -enable-machine-outliner=always or
// -enable-machine-outliner, the pass will run on all functions in the module.
// Otherwise, if the target supports default outlining, it will run on all
Expand All @@ -1177,10 +1176,10 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
// If the user specifies that they want to outline from linkonceodrs, set
// it here.
OutlineFromLinkOnceODRs = EnableLinkOnceODROutlining;
InstructionMapper Mapper;
InstructionMapper Mapper(*MMI);

// Prepare instruction mappings for the suffix tree.
populateMapper(Mapper, M, MMI);
populateMapper(Mapper, M);
std::vector<OutlinedFunction> FunctionList;

// Find all of the outlining candidates.
Expand All @@ -1198,7 +1197,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
bool ShouldEmitSizeRemarks = M.shouldEmitInstrCountChangedRemark();
StringMap<unsigned> FunctionToInstrCount;
if (ShouldEmitSizeRemarks)
initSizeRemarkInfo(M, MMI, FunctionToInstrCount);
initSizeRemarkInfo(M, FunctionToInstrCount);

// Outline each of the candidates and return true if something was outlined.
bool OutlinedSomething =
Expand All @@ -1208,7 +1207,7 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
// module. If we've asked for size remarks, then output them.
// FIXME: This should be in the pass manager.
if (ShouldEmitSizeRemarks && OutlinedSomething)
emitInstrCountChangedRemark(M, MMI, FunctionToInstrCount);
emitInstrCountChangedRemark(M, FunctionToInstrCount);

LLVM_DEBUG({
if (!OutlinedSomething)
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/CodeGen/TargetInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1809,15 +1809,17 @@ void TargetInstrInfo::mergeOutliningCandidateAttributes(
F.addFnAttr(Attribute::NoUnwind);
}

outliner::InstrType TargetInstrInfo::getOutliningType(
MachineBasicBlock::iterator &MIT, unsigned Flags) const {
outliner::InstrType
TargetInstrInfo::getOutliningType(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const {
MachineInstr &MI = *MIT;

// NOTE: MI.isMetaInstruction() will match CFI_INSTRUCTION, but some targets
// have support for outlining those. Special-case that here.
if (MI.isCFIInstruction())
// Just go right to the target implementation.
return getOutliningTypeImpl(MIT, Flags);
return getOutliningTypeImpl(MMI, MIT, Flags);

// Be conservative about inline assembly.
if (MI.isInlineAsm())
Expand Down Expand Up @@ -1883,7 +1885,7 @@ outliner::InstrType TargetInstrInfo::getOutliningType(
}

// If we don't know, delegate to the target-specific hook.
return getOutliningTypeImpl(MIT, Flags);
return getOutliningTypeImpl(MMI, MIT, Flags);
}

bool TargetInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8286,6 +8286,7 @@ static bool outliningCandidatesV8_3OpsConsensus(const outliner::Candidate &a,

std::optional<outliner::OutlinedFunction>
AArch64InstrInfo::getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
unsigned SequenceSize = 0;
for (auto &MI : RepeatedSequenceLocs[0])
Expand Down Expand Up @@ -8861,8 +8862,9 @@ AArch64InstrInfo::getOutlinableRanges(MachineBasicBlock &MBB,
}

outliner::InstrType
AArch64InstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
unsigned Flags) const {
AArch64InstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const {
MachineInstr &MI = *MIT;
MachineBasicBlock *MBB = MI.getParent();
MachineFunction *MF = MBB->getParent();
Expand Down Expand Up @@ -8974,7 +8976,7 @@ AArch64InstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,

// We have a function we have information about. Check it if it's something
// can safely outline.
MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
MachineFunction *CalleeMF = MMI.getMachineFunction(*Callee);

// We don't know what's going on with the callee at all. Don't touch it.
if (!CalleeMF)
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,13 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
bool OutlineFromLinkOnceODRs) const override;
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
void mergeOutliningCandidateAttributes(
Function &F, std::vector<outliner::Candidate> &Candidates) const override;
outliner::InstrType
getOutliningTypeImpl(MachineBasicBlock::iterator &MIT, unsigned Flags) const override;
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const override;
SmallVector<
std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator>>
getOutlinableRanges(MachineBasicBlock &MBB, unsigned &Flags) const override;
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5873,6 +5873,7 @@ static bool isLRAvailable(const TargetRegisterInfo &TRI,

std::optional<outliner::OutlinedFunction>
ARMBaseInstrInfo::getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
unsigned SequenceSize = 0;
for (auto &MI : RepeatedSequenceLocs[0])
Expand Down Expand Up @@ -6278,8 +6279,9 @@ bool ARMBaseInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
}

outliner::InstrType
ARMBaseInstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
unsigned Flags) const {
ARMBaseInstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const {
MachineInstr &MI = *MIT;
const TargetRegisterInfo *TRI = &getRegisterInfo();

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/ARM/ARMBaseInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
bool OutlineFromLinkOnceODRs) const override;
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
void mergeOutliningCandidateAttributes(
Function &F, std::vector<outliner::Candidate> &Candidates) const override;
outliner::InstrType getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
unsigned Flags) const override;
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const override;
bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
unsigned &Flags) const override;
void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2830,6 +2830,7 @@ bool RISCVInstrInfo::shouldOutlineFromFunctionByDefault(

std::optional<outliner::OutlinedFunction>
RISCVInstrInfo::getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {

// First we need to filter out candidates where the X5 register (IE t0) can't
Expand Down Expand Up @@ -2868,8 +2869,9 @@ RISCVInstrInfo::getOutliningCandidateInfo(
}

outliner::InstrType
RISCVInstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MBBI,
unsigned Flags) const {
RISCVInstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MBBI,
unsigned Flags) const {
MachineInstr &MI = *MBBI;
MachineBasicBlock *MBB = MI.getParent();
const TargetRegisterInfo *TRI =
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/Target/RISCV/RISCVInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {

// Calculate target-specific information for a set of outlining candidates.
std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;

// Return if/how a given MachineInstr should be outlined.
virtual outliner::InstrType
getOutliningTypeImpl(MachineBasicBlock::iterator &MBBI,
getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MBBI,
unsigned Flags) const override;

// Insert a custom frame for outlined functions.
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/Target/X86/X86InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10468,6 +10468,7 @@ enum MachineOutlinerClass { MachineOutlinerDefault, MachineOutlinerTailCall };

std::optional<outliner::OutlinedFunction>
X86InstrInfo::getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
unsigned SequenceSize = 0;
for (auto &MI : RepeatedSequenceLocs[0]) {
Expand Down Expand Up @@ -10544,7 +10545,8 @@ bool X86InstrInfo::isFunctionSafeToOutlineFrom(
}

outliner::InstrType
X86InstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
X86InstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const {
MachineInstr &MI = *MIT;

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/X86/X86InstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,15 @@ class X86InstrInfo final : public X86GenInstrInfo {
getSerializableDirectMachineOperandTargetFlags() const override;

std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
const MachineModuleInfo &MMI,
std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;

bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
bool OutlineFromLinkOnceODRs) const override;

outliner::InstrType
getOutliningTypeImpl(MachineBasicBlock::iterator &MIT, unsigned Flags) const override;
outliner::InstrType getOutliningTypeImpl(const MachineModuleInfo &MMI,
MachineBasicBlock::iterator &MIT,
unsigned Flags) const override;

void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
const outliner::OutlinedFunction &OF) const override;
Expand Down
Loading