| 1 | //===- NewPMDriver.cpp - Driver for llc using new PM ----------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// |
| 10 | /// This file is just a split of the code that logically belongs in llc.cpp but |
| 11 | /// that includes the new pass manager headers. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "NewPMDriver.h" |
| 16 | #include "llvm/Analysis/CGSCCPassManager.h" |
| 17 | #include "llvm/Analysis/RuntimeLibcallInfo.h" |
| 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 19 | #include "llvm/CodeGen/CommandFlags.h" |
| 20 | #include "llvm/CodeGen/LibcallLoweringInfo.h" |
| 21 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
| 22 | #include "llvm/CodeGen/MIRPrinter.h" |
| 23 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 24 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 25 | #include "llvm/CodeGen/MachinePassManager.h" |
| 26 | #include "llvm/CodeGen/MachineVerifier.h" |
| 27 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 28 | #include "llvm/IR/DiagnosticInfo.h" |
| 29 | #include "llvm/IR/DiagnosticPrinter.h" |
| 30 | #include "llvm/IR/IRPrintingPasses.h" |
| 31 | #include "llvm/IR/LLVMContext.h" |
| 32 | #include "llvm/IR/Module.h" |
| 33 | #include "llvm/IR/PassManager.h" |
| 34 | #include "llvm/IR/Verifier.h" |
| 35 | #include "llvm/IRReader/IRReader.h" |
| 36 | #include "llvm/Passes/PassBuilder.h" |
| 37 | #include "llvm/Passes/StandardInstrumentations.h" |
| 38 | #include "llvm/Plugins/PassPlugin.h" |
| 39 | #include "llvm/Support/CommandLine.h" |
| 40 | #include "llvm/Support/Debug.h" |
| 41 | #include "llvm/Support/Error.h" |
| 42 | #include "llvm/Support/ErrorHandling.h" |
| 43 | #include "llvm/Support/FormattedStream.h" |
| 44 | #include "llvm/Support/ToolOutputFile.h" |
| 45 | #include "llvm/Support/WithColor.h" |
| 46 | #include "llvm/Target/CGPassBuilderOption.h" |
| 47 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 48 | #include "llvm/Target/TargetMachine.h" |
| 49 | #include "llvm/Target/TargetOptions.h" |
| 50 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
| 51 | #include "llvm/Transforms/Utils/Cloning.h" |
| 52 | |
| 53 | using namespace llvm; |
| 54 | |
| 55 | static cl::opt<RegAllocType, false, RegAllocTypeParser> |
| 56 | RegAlloc("regalloc-npm" , |
| 57 | cl::desc("Register allocator to use for new pass manager" ), |
| 58 | cl::Hidden, cl::init(Val: RegAllocType::Unset)); |
| 59 | |
| 60 | static cl::opt<bool> |
| 61 | DebugPM("debug-pass-manager" , cl::Hidden, |
| 62 | cl::desc("Print pass management debugging information" )); |
| 63 | |
| 64 | bool LLCDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) { |
| 65 | DiagnosticHandler::handleDiagnostics(DI); |
| 66 | if (DI.getKind() == llvm::DK_SrcMgr) { |
| 67 | const auto &DISM = cast<DiagnosticInfoSrcMgr>(Val: DI); |
| 68 | const SMDiagnostic &SMD = DISM.getSMDiag(); |
| 69 | |
| 70 | SMD.print(ProgName: nullptr, S&: errs()); |
| 71 | |
| 72 | // For testing purposes, we print the LocCookie here. |
| 73 | if (DISM.isInlineAsmDiag() && DISM.getLocCookie()) |
| 74 | WithColor::note() << "!srcloc = " << DISM.getLocCookie() << "\n" ; |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (auto * = dyn_cast<DiagnosticInfoOptimizationBase>(Val: &DI)) |
| 80 | if (!Remark->isEnabled()) |
| 81 | return true; |
| 82 | |
| 83 | DiagnosticPrinterRawOStream DP(errs()); |
| 84 | errs() << LLVMContext::getDiagnosticMessagePrefix(Severity: DI.getSeverity()) << ": " ; |
| 85 | DI.print(DP); |
| 86 | errs() << "\n" ; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static llvm::ExitOnError ExitOnErr; |
| 91 | |
| 92 | int llvm::compileModuleWithNewPM( |
| 93 | StringRef Arg0, std::unique_ptr<Module> M, std::unique_ptr<MIRParser> MIR, |
| 94 | std::unique_ptr<TargetMachine> Target, std::unique_ptr<ToolOutputFile> Out, |
| 95 | std::unique_ptr<ToolOutputFile> DwoOut, LLVMContext &Context, |
| 96 | const TargetLibraryInfoImpl &TLII, VerifierKind VK, StringRef PassPipeline, |
| 97 | ArrayRef<PassPlugin> PassPlugins, CodeGenFileType FileType) { |
| 98 | |
| 99 | if (!PassPipeline.empty() && TargetPassConfig::hasLimitedCodeGenPipeline()) { |
| 100 | WithColor::error(OS&: errs(), Prefix: Arg0) |
| 101 | << "--passes cannot be used with " |
| 102 | << TargetPassConfig::getLimitedCodeGenPipelineReason() << ".\n" ; |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | raw_pwrite_stream *OS = &Out->os(); |
| 107 | |
| 108 | std::unique_ptr<buffer_ostream> BOS; |
| 109 | if (codegen::getFileType() != CodeGenFileType::AssemblyFile && |
| 110 | !Out->os().supportsSeeking()) { |
| 111 | BOS = std::make_unique<buffer_ostream>(args&: Out->os()); |
| 112 | OS = BOS.get(); |
| 113 | } |
| 114 | |
| 115 | // Fetch options from TargetPassConfig |
| 116 | CGPassBuilderOption Opt = getCGPassBuilderOption(); |
| 117 | Opt.DisableVerify = VK != VerifierKind::InputOutput; |
| 118 | Opt.DebugPM = DebugPM; |
| 119 | Opt.RegAlloc = RegAlloc; |
| 120 | |
| 121 | MachineModuleInfo MMI(Target.get()); |
| 122 | |
| 123 | Target->getObjFileLowering()->Initialize(ctx&: MMI.getContext(), TM: *Target); |
| 124 | |
| 125 | PassInstrumentationCallbacks PIC; |
| 126 | StandardInstrumentations SI(Context, Opt.DebugPM, |
| 127 | VK == VerifierKind::EachPass); |
| 128 | registerCodeGenCallback(PIC, *Target); |
| 129 | |
| 130 | MachineFunctionAnalysisManager MFAM; |
| 131 | LoopAnalysisManager LAM; |
| 132 | FunctionAnalysisManager FAM; |
| 133 | CGSCCAnalysisManager CGAM; |
| 134 | ModuleAnalysisManager MAM; |
| 135 | |
| 136 | FAM.registerPass(PassBuilder: [&] { return TargetLibraryAnalysis(TLII); }); |
| 137 | |
| 138 | MAM.registerPass(PassBuilder: [&] { |
| 139 | const TargetOptions &Options = Target->Options; |
| 140 | return RuntimeLibraryAnalysis(Options.ExceptionModel, Options.EABIVersion, |
| 141 | Options.MCOptions.ABIName, Options.VecLib); |
| 142 | }); |
| 143 | |
| 144 | MAM.registerPass(PassBuilder: [&] { return MachineModuleAnalysis(MMI); }); |
| 145 | |
| 146 | PassBuilder PB(Target.get(), PipelineTuningOptions(), std::nullopt, &PIC); |
| 147 | for (auto &PassPlugin : PassPlugins) |
| 148 | PassPlugin.registerPassBuilderCallbacks(PB); |
| 149 | PB.registerModuleAnalyses(MAM); |
| 150 | PB.registerCGSCCAnalyses(CGAM); |
| 151 | PB.registerFunctionAnalyses(FAM); |
| 152 | PB.registerLoopAnalyses(LAM); |
| 153 | PB.registerMachineFunctionAnalyses(MFAM); |
| 154 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, MFAM: &MFAM); |
| 155 | SI.registerCallbacks(PIC, MAM: &MAM); |
| 156 | |
| 157 | ModulePassManager MPM; |
| 158 | FunctionPassManager FPM; |
| 159 | |
| 160 | if (!PassPipeline.empty()) { |
| 161 | // Construct a custom pass pipeline that starts after instruction |
| 162 | // selection. |
| 163 | |
| 164 | if (!MIR) { |
| 165 | WithColor::error(OS&: errs(), Prefix: Arg0) << "-passes is for .mir file only.\n" ; |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | // FIXME: verify that there are no IR passes. |
| 170 | ExitOnErr(PB.parsePassPipeline(MPM, PipelineText: PassPipeline)); |
| 171 | MPM.addPass(Pass: PrintMIRPreparePass(*OS)); |
| 172 | MachineFunctionPassManager MFPM; |
| 173 | if (VK == VerifierKind::InputOutput) |
| 174 | MFPM.addPass(Pass: MachineVerifierPass()); |
| 175 | MFPM.addPass(Pass: PrintMIRPass(*OS)); |
| 176 | FPM.addPass(Pass: createFunctionToMachineFunctionPassAdaptor(Pass: std::move(MFPM))); |
| 177 | MPM.addPass(Pass: createModuleToFunctionPassAdaptor(Pass: std::move(FPM))); |
| 178 | |
| 179 | } else { |
| 180 | ExitOnErr(Target->buildCodeGenPipeline( |
| 181 | MPM, MAM, Out&: *OS, DwoOut: DwoOut ? &DwoOut->os() : nullptr, FileType, Opt, |
| 182 | Ctx&: MMI.getContext(), PIC: &PIC)); |
| 183 | } |
| 184 | |
| 185 | // If user only wants to print the pipeline, print it before parsing the MIR. |
| 186 | if (PrintPipelinePasses) { |
| 187 | std::string PipelineStr; |
| 188 | raw_string_ostream OS(PipelineStr); |
| 189 | MPM.printPipeline(OS, MapClassName2PassName: [&PIC](StringRef ClassName) { |
| 190 | auto PassName = PIC.getPassNameForClassName(ClassName); |
| 191 | return PassName.empty() ? ClassName : PassName; |
| 192 | }); |
| 193 | printFormattedPipelinePasses(OS&: outs(), Pipeline: PipelineStr, Format: *PrintPipelinePasses); |
| 194 | outs() << '\n'; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | if (MIR && MIR->parseMachineFunctions(M&: *M, MAM)) |
| 199 | return 1; |
| 200 | |
| 201 | // Before executing passes, print the final values of the LLVM options. |
| 202 | cl::PrintOptionValues(); |
| 203 | |
| 204 | MPM.run(IR&: *M, AM&: MAM); |
| 205 | |
| 206 | if (Context.getDiagHandlerPtr()->HasErrors) |
| 207 | return 1; |
| 208 | |
| 209 | // Declare success. |
| 210 | Out->keep(); |
| 211 | if (DwoOut) |
| 212 | DwoOut->keep(); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |