2424#include " llvm/Bitcode/BitcodeReader.h"
2525#include " llvm/Bitcode/BitcodeWriter.h"
2626#include " llvm/Bitcode/BitcodeWriterPass.h"
27+ #include " llvm/CodeGen/MachineModuleInfo.h"
2728#include " llvm/CodeGen/TargetSubtargetInfo.h"
29+ #include " llvm/CodeGen/TargetPassConfig.h"
2830#include " llvm/Config/llvm-config.h"
2931#include " llvm/Frontend/Driver/CodeGenOptions.h"
3032#include " llvm/IR/DataLayout.h"
@@ -189,7 +191,12 @@ class EmitAssemblyHelper {
189191 std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC);
190192 void RunCodegenPipeline (BackendAction Action,
191193 std::unique_ptr<raw_pwrite_stream> &OS,
192- std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
194+ std::unique_ptr<llvm::ToolOutputFile> &DwoOS,
195+ TargetMachine &TM);
196+ void RunCodegenPipelineWithNewPM (BackendAction Action,
197+ std::unique_ptr<raw_pwrite_stream> &OS,
198+ std::unique_ptr<llvm::ToolOutputFile> &DwoOS,
199+ TargetMachine &TM);
193200
194201 // / Check whether we should emit a module summary for regular LTO.
195202 // / The module summary should be emitted by default for regular LTO
@@ -1211,10 +1218,16 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
12111218
12121219void EmitAssemblyHelper::RunCodegenPipeline (
12131220 BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1214- std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
1221+ std::unique_ptr<llvm::ToolOutputFile> &DwoOS, TargetMachine &TM ) {
12151222 // We still use the legacy PM to run the codegen pipeline since the new PM
12161223 // does not work with the codegen pipeline.
12171224 // FIXME: make the new PM work with the codegen pipeline.
1225+
1226+ if (TM.EnableNewPMForBackend ()) {
1227+ RunCodegenPipelineWithNewPM (Action, OS, DwoOS, TM);
1228+ return ;
1229+ }
1230+
12181231 legacy::PassManager CodeGenPasses;
12191232
12201233 // Append any output we need to the pass manager.
@@ -1259,6 +1272,74 @@ void EmitAssemblyHelper::RunCodegenPipeline(
12591272 }
12601273}
12611274
1275+ void EmitAssemblyHelper::RunCodegenPipelineWithNewPM (BackendAction Action,
1276+ std::unique_ptr<raw_pwrite_stream> &OS,
1277+ std::unique_ptr<llvm::ToolOutputFile> &DwoOS, TargetMachine &TM) {
1278+ MachineModuleInfo MMI (&TM);
1279+ CGPassBuilderOption Opt = getCGPassBuilderOption ();
1280+
1281+ Opt.DisableVerify = !CodeGenOpts.VerifyModule ;
1282+ Opt.DebugPM = CodeGenOpts.DebugPassManager ;
1283+
1284+ PassInstrumentationCallbacks PIC;
1285+ StandardInstrumentations SI (TheModule->getContext (), CodeGenOpts.DebugPassManager ,
1286+ CodeGenOpts.VerifyEach );
1287+ registerCodeGenCallback (PIC, TM);
1288+
1289+ MachineFunctionAnalysisManager MFAM;
1290+ LoopAnalysisManager LAM;
1291+ FunctionAnalysisManager FAM;
1292+ CGSCCAnalysisManager CGAM;
1293+ ModuleAnalysisManager MAM;
1294+ PassBuilder PB (&TM, PipelineTuningOptions (), std::nullopt , &PIC);
1295+
1296+ PB.registerModuleAnalyses (MAM);
1297+ PB.registerCGSCCAnalyses (CGAM);
1298+ PB.registerFunctionAnalyses (FAM);
1299+ PB.registerLoopAnalyses (LAM);
1300+ PB.registerMachineFunctionAnalyses (MFAM);
1301+ PB.crossRegisterProxies (LAM, FAM, CGAM, MAM, &MFAM);
1302+ SI.registerCallbacks (PIC, &MAM);
1303+
1304+ TargetLibraryInfoImpl TLII (TheModule->getTargetTriple ());
1305+
1306+ FAM.registerPass ([&] { return TargetLibraryAnalysis (TLII); });
1307+ MAM.registerPass ([&] { return MachineModuleAnalysis (MMI); });
1308+
1309+ ModulePassManager MPM;
1310+ FunctionPassManager FPM;
1311+
1312+ if (!TM.buildCodeGenPipeline (MPM, *OS, DwoOS ? &DwoOS->os () : nullptr ,
1313+ getCodeGenFileType (Action), Opt, MMI.getContext (), &PIC)) {
1314+ Diags.Report (diag::err_fe_unable_to_interface_with_target);
1315+ return ;
1316+ }
1317+
1318+ if (PrintPipelinePasses) {
1319+ std::string PipelineStr;
1320+ raw_string_ostream OutS (PipelineStr);
1321+ MPM.printPipeline (OutS, [&PIC](StringRef ClassName) {
1322+ auto PassName = PIC.getPassNameForClassName (ClassName);
1323+ return PassName.empty () ? ClassName : PassName;
1324+ });
1325+ outs () << PipelineStr << ' \n ' ;
1326+ return ;
1327+ }
1328+
1329+ {
1330+ PrettyStackTraceString CrashInfo (" Code generation" );
1331+ llvm::TimeTraceScope TimeScope (" CodeGenPasses" );
1332+ Timer timer;
1333+ if (CI.getCodeGenOpts ().TimePasses ) {
1334+ timer.init (" codegen" , " Machine code generation" , CI.getTimerGroup ());
1335+ CI.getFrontendTimer ().yieldTo (timer);
1336+ }
1337+ MPM.run (*TheModule, MAM);
1338+ if (CI.getCodeGenOpts ().TimePasses )
1339+ timer.yieldTo (CI.getFrontendTimer ());
1340+ }
1341+ }
1342+
12621343void EmitAssemblyHelper::emitAssembly (BackendAction Action,
12631344 std::unique_ptr<raw_pwrite_stream> OS,
12641345 BackendConsumer *BC) {
@@ -1277,7 +1358,7 @@ void EmitAssemblyHelper::emitAssembly(BackendAction Action,
12771358
12781359 std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
12791360 RunOptimizationPipeline (Action, OS, ThinLinkOS, BC);
1280- RunCodegenPipeline (Action, OS, DwoOS);
1361+ RunCodegenPipeline (Action, OS, DwoOS, *TM );
12811362
12821363 if (ThinLinkOS)
12831364 ThinLinkOS->keep ();
0 commit comments