56
56
#include " llvm/IRPrinter/IRPrintingPasses.h"
57
57
#include " llvm/MC/MCAsmInfo.h"
58
58
#include " llvm/MC/MCTargetOptions.h"
59
+ #include " llvm/Passes/PassBuilder.h"
59
60
#include " llvm/Support/CodeGen.h"
60
61
#include " llvm/Support/Debug.h"
61
62
#include " llvm/Support/Error.h"
@@ -121,7 +122,7 @@ namespace llvm {
121
122
} \
122
123
static AnalysisKey Key; \
123
124
};
124
- #include " llvm/CodeGen /MachinePassRegistry.def"
125
+ #include " llvm/Passes /MachinePassRegistry.def"
125
126
126
127
// / This class provides access to building LLVM's passes.
127
128
// /
@@ -142,9 +143,10 @@ namespace llvm {
142
143
143
144
template <typename DerivedT> class CodeGenPassBuilder {
144
145
public:
145
- explicit CodeGenPassBuilder (LLVMTargetMachine &TM, CGPassBuilderOption Opts,
146
+ explicit CodeGenPassBuilder (LLVMTargetMachine &TM, PassBuilder &PB,
147
+ CGPassBuilderOption Opts,
146
148
PassInstrumentationCallbacks *PIC)
147
- : TM(TM), Opt(Opts), PIC(PIC) {
149
+ : TM(TM), PB(PB), Opt(Opts), PIC(PIC) {
148
150
// Target could set CGPassBuilderOption::MISchedPostRA to true to achieve
149
151
// substitutePass(&PostRASchedulerID, &PostMachineSchedulerID)
150
152
@@ -164,28 +166,6 @@ template <typename DerivedT> class CodeGenPassBuilder {
164
166
raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
165
167
CodeGenFileType FileType) const ;
166
168
167
- // / Parse single non-target-specific MIR pass
168
- // / @param Name the pass name
169
- // / @return true if failed
170
- bool parseMIRPass (MachineFunctionPassManager &MFPM, StringRef Name) const ;
171
-
172
- // / Parse MIR pass pipeline. Unlike IR pass pipeline,
173
- // / there is only one pass manager for machine function
174
- // / so there is no need to specify the pass nesting.
175
- // / @param Text a comma separated pass name list
176
- Error parseMIRPipeline (MachineFunctionPassManager &MFPM,
177
- StringRef Text) const {
178
- for (auto [LHS, RHS] = Text.split (' ,' ); LHS != " " ;
179
- std::tie (LHS, RHS) = RHS.split (' ,' )) {
180
- if (parseMIRPass (MFPM, LHS) && derived ().parseTargetMIRPass (MFPM, LHS)) {
181
- return createStringError (
182
- std::make_error_code (std::errc::invalid_argument),
183
- Twine (' \" ' ) + Twine (LHS) + Twine (" \" pass could not be found." ));
184
- }
185
- }
186
- return Error::success ();
187
- }
188
-
189
169
void registerModuleAnalyses (ModuleAnalysisManager &) const ;
190
170
void registerFunctionAnalyses (FunctionAnalysisManager &) const ;
191
171
void registerMachineFunctionAnalyses (MachineFunctionAnalysisManager &) const ;
@@ -279,6 +259,8 @@ template <typename DerivedT> class CodeGenPassBuilder {
279
259
});
280
260
}
281
261
262
+ MachineFunctionPassManager &getPM () { return PM; }
263
+
282
264
MachineFunctionPassManager releasePM () { return std::move (PM); }
283
265
284
266
private:
@@ -311,14 +293,10 @@ template <typename DerivedT> class CodeGenPassBuilder {
311
293
}
312
294
313
295
LLVMTargetMachine &TM;
296
+ PassBuilder &PB;
314
297
CGPassBuilderOption Opt;
315
298
PassInstrumentationCallbacks *PIC;
316
299
317
- // / Target override these hooks to parse target-specific analyses.
318
- void registerTargetAnalysis (ModuleAnalysisManager &) const {}
319
- void registerTargetAnalysis (FunctionAnalysisManager &) const {}
320
- void registerTargetAnalysis (MachineFunctionAnalysisManager &) const {}
321
-
322
300
template <typename TMC> TMC &getTM () const { return static_cast <TMC &>(TM); }
323
301
CodeGenOptLevel getOptLevel () const { return TM.getOptLevel (); }
324
302
@@ -556,14 +534,6 @@ template <typename DerivedT> class CodeGenPassBuilder {
556
534
// / Utilities for targets to add passes to the pass manager.
557
535
// /
558
536
559
- // / createTargetRegisterAllocator - Create the register allocator pass for
560
- // / this target at the current optimization level.
561
- void addTargetRegisterAllocator (AddMachinePass &, bool Optimized) const ;
562
-
563
- // / addMachinePasses helper to create the target-selected or overriden
564
- // / regalloc pass.
565
- void addRegAllocPass (AddMachinePass &, bool Optimized) const ;
566
-
567
537
// / Add core register allocator passes which do the actual register assignment
568
538
// / and rewriting. \returns Error::success() if any passes were added.
569
539
Error addRegAssignAndRewriteFast (AddMachinePass &addPass) const ;
@@ -1257,54 +1227,16 @@ void CodeGenPassBuilder<Derived>::addMachineSSAOptimization(
1257
1227
// / Register Allocation Pass Configuration
1258
1228
// ===---------------------------------------------------------------------===//
1259
1229
1260
- // / Instantiate the default register allocator pass for this target for either
1261
- // / the optimized or unoptimized allocation path. This will be added to the pass
1262
- // / manager by addFastRegAlloc in the unoptimized case or addOptimizedRegAlloc
1263
- // / in the optimized case.
1264
- // /
1265
- // / A target that uses the standard regalloc pass order for fast or optimized
1266
- // / allocation may still override this for per-target regalloc
1267
- // / selection. But -regalloc=... always takes precedence.
1268
- template <typename Derived>
1269
- void CodeGenPassBuilder<Derived>::addTargetRegisterAllocator(
1270
- AddMachinePass &addPass, bool Optimized) const {
1271
- if (Optimized)
1272
- addPass (RAGreedyPass ());
1273
- else
1274
- addPass (RAFastPass ());
1275
- }
1276
-
1277
- // / Find and instantiate the register allocation pass requested by this target
1278
- // / at the current optimization level. Different register allocators are
1279
- // / defined as separate passes because they may require different analysis.
1280
- template <typename Derived>
1281
- void CodeGenPassBuilder<Derived>::addRegAllocPass(AddMachinePass &addPass,
1282
- bool Optimized) const {
1283
- if (Opt.RegAlloc == RegAllocType::Default)
1284
- // With no -regalloc= override, ask the target for a regalloc pass.
1285
- derived ().addTargetRegisterAllocator (addPass, Optimized);
1286
- else if (Opt.RegAlloc == RegAllocType::Basic)
1287
- addPass (RABasicPass ());
1288
- else if (Opt.RegAlloc == RegAllocType::Fast)
1289
- addPass (RAFastPass ());
1290
- else if (Opt.RegAlloc == RegAllocType::Greedy)
1291
- addPass (RAGreedyPass ());
1292
- else if (Opt.RegAlloc == RegAllocType::PBQP)
1293
- addPass (RAPBQPPass ());
1294
- else
1295
- llvm_unreachable (" unknonwn register allocator type" );
1296
- }
1297
-
1298
1230
template <typename Derived>
1299
1231
Error CodeGenPassBuilder<Derived>::addRegAssignAndRewriteFast(
1300
1232
AddMachinePass &addPass) const {
1301
- if (Opt.RegAlloc != RegAllocType::Default &&
1302
- Opt.RegAlloc != RegAllocType::Fast)
1233
+ if (Opt.RegAlloc != " default" && !Opt.RegAlloc .starts_with (" fast" ))
1303
1234
return make_error<StringError>(
1304
1235
" Must use fast (default) register allocator for unoptimized regalloc." ,
1305
1236
inconvertibleErrorCode ());
1306
1237
1307
- addPass (RegAllocPass (false ));
1238
+ if (Error Err = PB.parseRegAllocPass (addPass.getPM (), Opt.RegAlloc , false ))
1239
+ return Err;
1308
1240
1309
1241
// Allow targets to change the register assignments after
1310
1242
// fast register allocation.
@@ -1315,7 +1247,8 @@ template <typename DerivedT>
1315
1247
Error CodeGenPassBuilder<DerivedT>::addRegAssignAndRewriteOptimized(
1316
1248
AddMachinePass &addPass) const {
1317
1249
// Add the selected register allocation pass.
1318
- addRegAllocPass (addPass, true );
1250
+ if (Error Err = PB.parseRegAllocPass (addPass.getPM (), Opt.RegAlloc , true ))
1251
+ return Err;
1319
1252
// Allow targets to change the register assignments before rewriting.
1320
1253
addPreRewrite (addPass);
1321
1254
@@ -1367,22 +1300,22 @@ Error CodeGenPassBuilder<Derived>::addOptimizedRegAlloc(
1367
1300
// PreRA instruction scheduling.
1368
1301
addPass (MachineSchedulerPass ());
1369
1302
1370
- Error E = derived ().addRegAssignAndRewriteOptimized (addPass);
1371
- if (!E) {
1372
- // Allow targets to expand pseudo instructions depending on the choice of
1373
- // registers before MachineCopyPropagation.
1374
- derived ().addPostRewrite (addPass);
1303
+ if (Error Err = derived ().addRegAssignAndRewriteOptimized (addPass))
1304
+ return Err;
1375
1305
1376
- // Copy propagate to forward register uses and try to eliminate COPYs that
1377
- // were not coalesced .
1378
- addPass ( MachineCopyPropagationPass () );
1306
+ // Allow targets to expand pseudo instructions depending on the choice of
1307
+ // registers before MachineCopyPropagation .
1308
+ derived (). addPostRewrite (addPass );
1379
1309
1380
- // Run post-ra machine LICM to hoist reloads / remats.
1381
- //
1382
- // FIXME: can this move into MachineLateOptimization?
1383
- addPass (MachineLICMPass ());
1384
- }
1385
- return E;
1310
+ // Copy propagate to forward register uses and try to eliminate COPYs that
1311
+ // were not coalesced.
1312
+ addPass (MachineCopyPropagationPass ());
1313
+
1314
+ // Run post-ra machine LICM to hoist reloads / remats.
1315
+ //
1316
+ // FIXME: can this move into MachineLateOptimization?
1317
+ addPass (MachineLICMPass ());
1318
+ return Error::success ();
1386
1319
}
1387
1320
1388
1321
// ===---------------------------------------------------------------------===//
0 commit comments