Skip to content

Commit

Permalink
[NewPM][CodeGen] Port selection dag isel to new pass manager (#83567)
Browse files Browse the repository at this point in the history
Port selection dag isel to new pass manager.
Only `AMDGPU` and `X86` support new pass version. `-verify-machineinstrs` in new pass manager belongs to verify instrumentation, it is enabled by default.
  • Loading branch information
paperchalice authored Jun 2, 2024
1 parent 0310f7f commit d2cdc8a
Show file tree
Hide file tree
Showing 122 changed files with 783 additions and 280 deletions.
12 changes: 12 additions & 0 deletions llvm/include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/CodeGen/ISDOpcodes.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/SelectionDAGNodes.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/CodeGenTypes/MachineValueType.h"
Expand Down Expand Up @@ -230,6 +231,7 @@ class SelectionDAG {
const TargetLibraryInfo *LibInfo = nullptr;
const FunctionVarLocs *FnVarLocs = nullptr;
MachineFunction *MF;
MachineFunctionAnalysisManager *MFAM = nullptr;
Pass *SDAGISelPass = nullptr;
LLVMContext *Context;
CodeGenOptLevel OptLevel;
Expand Down Expand Up @@ -459,6 +461,15 @@ class SelectionDAG {
UniformityInfo *UA, ProfileSummaryInfo *PSIin,
BlockFrequencyInfo *BFIin, FunctionVarLocs const *FnVarLocs);

void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE,
MachineFunctionAnalysisManager &AM,
const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA,
ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin,
FunctionVarLocs const *FnVarLocs) {
init(NewMF, NewORE, nullptr, LibraryInfo, UA, PSIin, BFIin, FnVarLocs);
MFAM = &AM;
}

void setFunctionLoweringInfo(FunctionLoweringInfo * FuncInfo) {
FLI = FuncInfo;
}
Expand All @@ -469,6 +480,7 @@ class SelectionDAG {

MachineFunction &getMachineFunction() const { return *MF; }
const Pass *getPass() const { return SDAGISelPass; }
MachineFunctionAnalysisManager *getMFAM() { return MFAM; }

CodeGenOptLevel getOptLevel() const { return OptLevel; }
const DataLayout &getDataLayout() const { return MF->getDataLayout(); }
Expand Down
43 changes: 38 additions & 5 deletions llvm/include/llvm/CodeGen/SelectionDAGISel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define LLVM_CODEGEN_SELECTIONDAGISEL_H

#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/IR/BasicBlock.h"
#include <memory>
Expand All @@ -24,21 +25,23 @@ class AAResults;
class AssumptionCache;
class TargetInstrInfo;
class TargetMachine;
class SSPLayoutInfo;
class SelectionDAGBuilder;
class SDValue;
class MachineRegisterInfo;
class MachineFunction;
class OptimizationRemarkEmitter;
class TargetLowering;
class TargetLibraryInfo;
class TargetTransformInfo;
class FunctionLoweringInfo;
class SwiftErrorValueTracking;
class GCFunctionInfo;
class ScheduleDAGSDNodes;

/// SelectionDAGISel - This is the common base class used for SelectionDAG-based
/// pattern-matching instruction selectors.
class SelectionDAGISel : public MachineFunctionPass {
class SelectionDAGISel {
public:
TargetMachine &TM;
const TargetLibraryInfo *LibInfo;
Expand All @@ -51,6 +54,10 @@ class SelectionDAGISel : public MachineFunctionPass {
AAResults *AA = nullptr;
AssumptionCache *AC = nullptr;
GCFunctionInfo *GFI = nullptr;
SSPLayoutInfo *SP = nullptr;
#ifndef NDEBUG
TargetTransformInfo *TTI = nullptr;
#endif
CodeGenOptLevel OptLevel;
const TargetInstrInfo *TII;
const TargetLowering *TLI;
Expand All @@ -67,16 +74,18 @@ class SelectionDAGISel : public MachineFunctionPass {
/// functions. Storing the filter result here so that we only need to do the
/// filtering once.
bool MatchFilterFuncName = false;
StringRef FuncName;

explicit SelectionDAGISel(char &ID, TargetMachine &tm,
explicit SelectionDAGISel(TargetMachine &tm,
CodeGenOptLevel OL = CodeGenOptLevel::Default);
~SelectionDAGISel() override;
~SelectionDAGISel();

const TargetLowering *getTargetLowering() const { return TLI; }

void getAnalysisUsage(AnalysisUsage &AU) const override;
void initializeAnalysisResults(MachineFunctionAnalysisManager &MFAM);
void initializeAnalysisResults(MachineFunctionPass &MFP);

bool runOnMachineFunction(MachineFunction &MF) override;
virtual bool runOnMachineFunction(MachineFunction &mf);

virtual void emitFunctionEntryCode() {}

Expand Down Expand Up @@ -517,6 +526,30 @@ class SelectionDAGISel : public MachineFunctionPass {
bool isMorphNodeTo);
};

class SelectionDAGISelLegacy : public MachineFunctionPass {
std::unique_ptr<SelectionDAGISel> Selector;

public:
SelectionDAGISelLegacy(char &ID, std::unique_ptr<SelectionDAGISel> S);

~SelectionDAGISelLegacy() override = default;

void getAnalysisUsage(AnalysisUsage &AU) const override;

bool runOnMachineFunction(MachineFunction &MF) override;
};

class SelectionDAGISelPass : public PassInfoMixin<SelectionDAGISelPass> {
std::unique_ptr<SelectionDAGISel> Selector;

protected:
SelectionDAGISelPass(std::unique_ptr<SelectionDAGISel> Selector)
: Selector(std::move(Selector)) {}

public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
};
}

#endif /* LLVM_CODEGEN_SELECTIONDAGISEL_H */
2 changes: 2 additions & 0 deletions llvm/include/llvm/CodeGen/StackProtector.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class StackProtector : public FunctionPass {

StackProtector();

SSPLayoutInfo &getLayoutInfo() { return LayoutInfo; }

void getAnalysisUsage(AnalysisUsage &AU) const override;

// Return true if StackProtector is supposed to be handled by SelectionDAG.
Expand Down
9 changes: 7 additions & 2 deletions llvm/include/llvm/Passes/CodeGenPassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
}

protected:
template <typename PassT>
using has_required_t = decltype(std::declval<PassT &>().isRequired());

template <typename PassT>
using is_module_pass_t = decltype(std::declval<PassT &>().run(
std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
Expand Down Expand Up @@ -170,8 +173,10 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
static_assert((is_detected<is_function_pass_t, PassT>::value ||
is_detected<is_module_pass_t, PassT>::value) &&
"Only module pass and function pass are supported.");

if (!PB.runBeforeAdding(Name))
bool Required = false;
if constexpr (is_detected<has_required_t, PassT>::value)
Required = PassT::isRequired();
if (!PB.runBeforeAdding(Name) && !Required)
return;

// Add Function Pass
Expand Down
Loading

0 comments on commit d2cdc8a

Please sign in to comment.