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