|
59 | 59 | #include "llvm/IRPrinter/IRPrintingPasses.h" |
60 | 60 | #include "llvm/MC/MCAsmInfo.h" |
61 | 61 | #include "llvm/MC/MCTargetOptions.h" |
| 62 | +#include "llvm/Passes/PassBuilder.h" |
62 | 63 | #include "llvm/Support/CodeGen.h" |
63 | 64 | #include "llvm/Support/Debug.h" |
64 | 65 | #include "llvm/Support/Error.h" |
65 | 66 | #include "llvm/Support/ErrorHandling.h" |
| 67 | +#include "llvm/Support/FormatVariadic.h" |
66 | 68 | #include "llvm/Target/CGPassBuilderOption.h" |
67 | 69 | #include "llvm/Target/TargetMachine.h" |
68 | 70 | #include "llvm/Transforms/CFGuard.h" |
@@ -116,9 +118,8 @@ namespace llvm { |
116 | 118 | template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder { |
117 | 119 | public: |
118 | 120 | explicit CodeGenPassBuilder(TargetMachineT &TM, |
119 | | - const CGPassBuilderOption &Opts, |
120 | | - PassInstrumentationCallbacks *PIC) |
121 | | - : TM(TM), Opt(Opts), PIC(PIC) { |
| 121 | + const CGPassBuilderOption &Opts, PassBuilder &PB) |
| 122 | + : TM(TM), Opt(Opts), PB(PB), PIC(PB.getPassInstrumentationCallbacks()) { |
122 | 123 | // Target could set CGPassBuilderOption::MISchedPostRA to true to achieve |
123 | 124 | // substitutePass(&PostRASchedulerID, &PostMachineSchedulerID) |
124 | 125 |
|
@@ -253,6 +254,7 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder { |
253 | 254 |
|
254 | 255 | TargetMachineT &TM; |
255 | 256 | CGPassBuilderOption Opt; |
| 257 | + PassBuilder &PB; |
256 | 258 | PassInstrumentationCallbacks *PIC; |
257 | 259 |
|
258 | 260 | template <typename TMC> TMC &getTM() const { return static_cast<TMC &>(TM); } |
@@ -453,13 +455,9 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder { |
453 | 455 | /// Utilities for targets to add passes to the pass manager. |
454 | 456 | /// |
455 | 457 |
|
456 | | - /// createTargetRegisterAllocator - Create the register allocator pass for |
457 | | - /// this target at the current optimization level. |
458 | | - void addTargetRegisterAllocator(AddMachinePass &, bool Optimized) const; |
459 | | - |
460 | 458 | /// addMachinePasses helper to create the target-selected or overriden |
461 | 459 | /// regalloc pass. |
462 | | - void addRegAllocPass(AddMachinePass &, bool Optimized) const; |
| 460 | + Error addRegAllocPass(AddMachinePass &, StringRef FilterName = "all") const; |
463 | 461 |
|
464 | 462 | /// Add core register alloator passes which do the actual register assignment |
465 | 463 | /// and rewriting. \returns true if any passes were added. |
@@ -521,6 +519,9 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline( |
521 | 519 | return StartStopInfo.takeError(); |
522 | 520 | setStartStopPasses(*StartStopInfo); |
523 | 521 |
|
| 522 | + if (auto Err = PB.parseRegAllocOpt(Opt.RegAlloc)) |
| 523 | + return Err; |
| 524 | + |
524 | 525 | bool PrintAsm = TargetPassConfig::willCompleteCodeGenPipeline(); |
525 | 526 | bool PrintMIR = !PrintAsm && FileType != CodeGenFileType::Null; |
526 | 527 |
|
@@ -1025,45 +1026,40 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineSSAOptimization( |
1025 | 1026 | /// Register Allocation Pass Configuration |
1026 | 1027 | //===---------------------------------------------------------------------===// |
1027 | 1028 |
|
1028 | | -/// Instantiate the default register allocator pass for this target for either |
1029 | | -/// the optimized or unoptimized allocation path. This will be added to the pass |
1030 | | -/// manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc |
1031 | | -/// in the optimized case. |
1032 | | -/// |
1033 | | -/// A target that uses the standard regalloc pass order for fast or optimized |
1034 | | -/// allocation may still override this for per-target regalloc |
1035 | | -/// selection. But -regalloc=... always takes precedence. |
1036 | | -template <typename Derived, typename TargetMachineT> |
1037 | | -void CodeGenPassBuilder<Derived, TargetMachineT>::addTargetRegisterAllocator( |
1038 | | - AddMachinePass &addPass, bool Optimized) const { |
1039 | | - if (Optimized) |
1040 | | - addPass(RAGreedyPass()); |
1041 | | - else |
1042 | | - addPass(RegAllocFastPass()); |
1043 | | -} |
1044 | | - |
1045 | 1029 | /// Find and instantiate the register allocation pass requested by this target |
1046 | 1030 | /// at the current optimization level. Different register allocators are |
1047 | 1031 | /// defined as separate passes because they may require different analysis. |
1048 | 1032 | template <typename Derived, typename TargetMachineT> |
1049 | | -void CodeGenPassBuilder<Derived, TargetMachineT>::addRegAllocPass( |
1050 | | - AddMachinePass &addPass, bool Optimized) const { |
1051 | | - // TODO: Parse Opt.RegAlloc to add register allocator. |
| 1033 | +Error CodeGenPassBuilder<Derived, TargetMachineT>::addRegAllocPass( |
| 1034 | + AddMachinePass &addPass, StringRef FilterName) const { |
| 1035 | + auto &RegAllocMap = PB.getRegAllocMap(); |
| 1036 | + if (RegAllocMap.contains("none")) |
| 1037 | + return Error::success(); |
| 1038 | + |
| 1039 | + if (!RegAllocMap.contains(FilterName)) { |
| 1040 | + return make_error<StringError>( |
| 1041 | + formatv("No register allocator for register class filter '{0}'", |
| 1042 | + FilterName) |
| 1043 | + .str(), |
| 1044 | + inconvertibleErrorCode()); |
| 1045 | + } |
| 1046 | + |
| 1047 | + addPass(std::move(RegAllocMap[FilterName])); |
| 1048 | + return Error::success(); |
1052 | 1049 | } |
1053 | 1050 |
|
1054 | 1051 | template <typename Derived, typename TargetMachineT> |
1055 | 1052 | Error CodeGenPassBuilder<Derived, TargetMachineT>::addRegAssignmentFast( |
1056 | 1053 | AddMachinePass &addPass) const { |
1057 | | - // TODO: Ensure allocator is default or fast. |
1058 | | - addRegAllocPass(addPass, false); |
1059 | | - return Error::success(); |
| 1054 | + return addRegAllocPass(addPass); |
1060 | 1055 | } |
1061 | 1056 |
|
1062 | 1057 | template <typename Derived, typename TargetMachineT> |
1063 | 1058 | Error CodeGenPassBuilder<Derived, TargetMachineT>::addRegAssignmentOptimized( |
1064 | 1059 | AddMachinePass &addPass) const { |
1065 | 1060 | // Add the selected register allocation pass. |
1066 | | - addRegAllocPass(addPass, true); |
| 1061 | + if (auto Err = addRegAllocPass(addPass)) |
| 1062 | + return Err; |
1067 | 1063 |
|
1068 | 1064 | // Allow targets to change the register assignments before rewriting. |
1069 | 1065 | derived().addPreRewrite(addPass); |
|
0 commit comments