|
| 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/TargetLibraryInfo.h" |
| 18 | +#include "llvm/CodeGen/CodeGenPassBuilder.h" |
| 19 | +#include "llvm/CodeGen/CommandFlags.h" |
| 20 | +#include "llvm/CodeGen/MIRParser/MIRParser.h" |
| 21 | +#include "llvm/CodeGen/MIRPrinter.h" |
| 22 | +#include "llvm/CodeGen/MachineModuleInfo.h" |
| 23 | +#include "llvm/CodeGen/MachinePassManager.h" |
| 24 | +#include "llvm/CodeGen/TargetPassConfig.h" |
| 25 | +#include "llvm/IR/DiagnosticInfo.h" |
| 26 | +#include "llvm/IR/DiagnosticPrinter.h" |
| 27 | +#include "llvm/IR/IRPrintingPasses.h" |
| 28 | +#include "llvm/IR/LLVMContext.h" |
| 29 | +#include "llvm/IR/Module.h" |
| 30 | +#include "llvm/IR/PassManager.h" |
| 31 | +#include "llvm/IR/Verifier.h" |
| 32 | +#include "llvm/IRReader/IRReader.h" |
| 33 | +#include "llvm/Passes/PassBuilder.h" |
| 34 | +#include "llvm/Passes/StandardInstrumentations.h" |
| 35 | +#include "llvm/Support/CommandLine.h" |
| 36 | +#include "llvm/Support/Debug.h" |
| 37 | +#include "llvm/Support/Error.h" |
| 38 | +#include "llvm/Support/ErrorHandling.h" |
| 39 | +#include "llvm/Support/FormattedStream.h" |
| 40 | +#include "llvm/Support/ToolOutputFile.h" |
| 41 | +#include "llvm/Support/WithColor.h" |
| 42 | +#include "llvm/Target/CGPassBuilderOption.h" |
| 43 | +#include "llvm/Target/TargetMachine.h" |
| 44 | +#include "llvm/Target/TargetOptions.h" |
| 45 | +#include "llvm/Transforms/Scalar/LoopPassManager.h" |
| 46 | +#include "llvm/Transforms/Utils/Cloning.h" |
| 47 | + |
| 48 | +using namespace llvm; |
| 49 | + |
| 50 | +static cl::opt<RegAllocType> RegAlloc( |
| 51 | + "regalloc-npm", cl::desc("Register allocator to use for new pass manager"), |
| 52 | + cl::Hidden, cl::ValueOptional, cl::init(RegAllocType::Default), |
| 53 | + cl::values( |
| 54 | + clEnumValN(RegAllocType::Default, "default", |
| 55 | + "pick register allocator based on -O option"), |
| 56 | + clEnumValN(RegAllocType::Basic, "basic", "basic register allocator"), |
| 57 | + clEnumValN(RegAllocType::Fast, "fast", "fast register allocator"), |
| 58 | + clEnumValN(RegAllocType::Greedy, "greedy", "greedy register allocator"), |
| 59 | + clEnumValN(RegAllocType::PBQP, "pbqp", "PBQP register allocator"))); |
| 60 | + |
| 61 | +static cl::opt<bool> |
| 62 | + DebugPM("debug-pass-manager", cl::Hidden, |
| 63 | + cl::desc("Print pass management debugging information")); |
| 64 | + |
| 65 | +bool LLCDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) { |
| 66 | + if (DI.getKind() == llvm::DK_SrcMgr) { |
| 67 | + const auto &DISM = cast<DiagnosticInfoSrcMgr>(DI); |
| 68 | + const SMDiagnostic &SMD = DISM.getSMDiag(); |
| 69 | + |
| 70 | + if (SMD.getKind() == SourceMgr::DK_Error) |
| 71 | + *HasError = true; |
| 72 | + |
| 73 | + SMD.print(nullptr, errs()); |
| 74 | + |
| 75 | + // For testing purposes, we print the LocCookie here. |
| 76 | + if (DISM.isInlineAsmDiag() && DISM.getLocCookie()) |
| 77 | + WithColor::note() << "!srcloc = " << DISM.getLocCookie() << "\n"; |
| 78 | + |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + if (DI.getSeverity() == DS_Error) |
| 83 | + *HasError = true; |
| 84 | + |
| 85 | + if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) |
| 86 | + if (!Remark->isEnabled()) |
| 87 | + return true; |
| 88 | + |
| 89 | + DiagnosticPrinterRawOStream DP(errs()); |
| 90 | + errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; |
| 91 | + DI.print(DP); |
| 92 | + errs() << "\n"; |
| 93 | + return true; |
| 94 | +} |
| 95 | + |
| 96 | +static llvm::ExitOnError ExitOnErr; |
| 97 | + |
| 98 | +static void RunPasses(bool BOS, ToolOutputFile *Out, Module *M, |
| 99 | + LLVMContext &Context, SmallString<0> &Buffer, |
| 100 | + ModulePassManager *MPM, ModuleAnalysisManager *MAM, |
| 101 | + MachineFunctionPassManager &MFPM, |
| 102 | + MachineFunctionAnalysisManager &MFAM) { |
| 103 | + assert(M && "invalid input module!"); |
| 104 | + |
| 105 | + // Before executing passes, print the final values of the LLVM options. |
| 106 | + cl::PrintOptionValues(); |
| 107 | + |
| 108 | + if (MPM) { |
| 109 | + assert(MAM && "expect a ModuleAnalysisManager!"); |
| 110 | + MPM->run(*M, *MAM); |
| 111 | + } |
| 112 | + |
| 113 | + ExitOnErr(MFPM.run(*M, MFAM)); |
| 114 | + |
| 115 | + auto HasError = |
| 116 | + ((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError; |
| 117 | + if (*HasError) |
| 118 | + exit(1); |
| 119 | + |
| 120 | + if (BOS) |
| 121 | + Out->os() << Buffer; |
| 122 | +} |
| 123 | + |
| 124 | +int llvm::compileModuleWithNewPM( |
| 125 | + StringRef Arg0, std::unique_ptr<Module> M, std::unique_ptr<MIRParser> MIR, |
| 126 | + std::unique_ptr<TargetMachine> Target, std::unique_ptr<ToolOutputFile> Out, |
| 127 | + std::unique_ptr<ToolOutputFile> DwoOut, LLVMContext &Context, |
| 128 | + const TargetLibraryInfoImpl &TLII, bool NoVerify, |
| 129 | + const std::vector<std::string> &RunPassNames, CodeGenFileType FileType) { |
| 130 | + |
| 131 | + if (!RunPassNames.empty() && TargetPassConfig::hasLimitedCodeGenPipeline()) { |
| 132 | + WithColor::warning(errs(), Arg0) |
| 133 | + << "run-pass cannot be used with " |
| 134 | + << TargetPassConfig::getLimitedCodeGenPipelineReason(" and ") << ".\n"; |
| 135 | + return 1; |
| 136 | + } |
| 137 | + |
| 138 | + LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine &>(*Target); |
| 139 | + |
| 140 | + { |
| 141 | + raw_pwrite_stream *OS = &Out->os(); |
| 142 | + |
| 143 | + // Manually do the buffering rather than using buffer_ostream, |
| 144 | + // so we can memcmp the contents in CompileTwice mode in future. |
| 145 | + SmallString<0> Buffer; |
| 146 | + std::unique_ptr<raw_svector_ostream> BOS; |
| 147 | + if ((codegen::getFileType() != CodeGenFileType::AssemblyFile && |
| 148 | + !Out->os().supportsSeeking())) { |
| 149 | + BOS = std::make_unique<raw_svector_ostream>(Buffer); |
| 150 | + OS = BOS.get(); |
| 151 | + } |
| 152 | + |
| 153 | + // Fetch options from TargetPassConfig |
| 154 | + CGPassBuilderOption Opt = getCGPassBuilderOption(); |
| 155 | + Opt.DisableVerify = NoVerify; |
| 156 | + Opt.DebugPM = DebugPM; |
| 157 | + Opt.RegAlloc = RegAlloc; |
| 158 | + |
| 159 | + PassInstrumentationCallbacks PIC; |
| 160 | + StandardInstrumentations SI(Context, Opt.DebugPM); |
| 161 | + SI.registerCallbacks(PIC); |
| 162 | + registerCodeGenCallback(PIC, LLVMTM); |
| 163 | + |
| 164 | + LoopAnalysisManager LAM; |
| 165 | + FunctionAnalysisManager FAM; |
| 166 | + CGSCCAnalysisManager CGAM; |
| 167 | + ModuleAnalysisManager MAM; |
| 168 | + PassBuilder PB(Target.get(), PipelineTuningOptions(), std::nullopt, &PIC); |
| 169 | + PB.registerModuleAnalyses(MAM); |
| 170 | + PB.registerCGSCCAnalyses(CGAM); |
| 171 | + PB.registerFunctionAnalyses(FAM); |
| 172 | + PB.registerLoopAnalyses(LAM); |
| 173 | + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 174 | + |
| 175 | + FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); }); |
| 176 | + MAM.registerPass([&] { return MachineModuleAnalysis(&LLVMTM); }); |
| 177 | + |
| 178 | + MachineFunctionAnalysisManager MFAM(FAM, MAM); |
| 179 | + |
| 180 | + if (!RunPassNames.empty()) { |
| 181 | + // Construct a custom pass pipeline that starts after instruction |
| 182 | + // selection. |
| 183 | + |
| 184 | + if (!MIR) { |
| 185 | + WithColor::warning(errs(), Arg0) << "run-pass is for .mir file only.\n"; |
| 186 | + return 1; |
| 187 | + } |
| 188 | + |
| 189 | + MachineFunctionPassManager MFPM; |
| 190 | + ExitOnErr(LLVMTM.parseMIRPipeline(MFPM, llvm::join(RunPassNames, ","), |
| 191 | + Opt, MFAM, &PIC)); |
| 192 | + MFPM.addPass(PrintMIRPass(*OS)); |
| 193 | + MFPM.addPass(FreeMachineFunctionPass()); |
| 194 | + |
| 195 | + auto &MMI = MFAM.getResult<MachineModuleAnalysis>(*M); |
| 196 | + if (MIR->parseMachineFunctions(*M, MMI)) |
| 197 | + return 1; |
| 198 | + |
| 199 | + RunPasses(BOS.get(), Out.get(), M.get(), Context, Buffer, nullptr, |
| 200 | + nullptr, MFPM, MFAM); |
| 201 | + } else { |
| 202 | + ModulePassManager MPM; |
| 203 | + MachineFunctionPassManager MFPM; |
| 204 | + |
| 205 | + ExitOnErr(LLVMTM.buildCodeGenPipeline(MPM, MFPM, MFAM, *OS, |
| 206 | + DwoOut ? &DwoOut->os() : nullptr, |
| 207 | + FileType, Opt, &PIC)); |
| 208 | + |
| 209 | + // Add IR or MIR printing pass according the pass type. |
| 210 | + if (PIC.isStartStopInfoRegistered()) { |
| 211 | + auto &Info = PIC.getStartStopInfo(); |
| 212 | + if (!Info.willCompleteCodeGenPipeline()) { |
| 213 | + if (Info.isStopMachineFunctionPass()) |
| 214 | + MFPM.addPass(PrintMIRPass(*OS)); |
| 215 | + else |
| 216 | + MPM.addPass(PrintModulePass(*OS)); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + RunPasses(BOS.get(), Out.get(), M.get(), Context, Buffer, &MPM, &MAM, |
| 221 | + MFPM, MFAM); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + // Declare success. |
| 226 | + Out->keep(); |
| 227 | + if (DwoOut) |
| 228 | + DwoOut->keep(); |
| 229 | + |
| 230 | + return 0; |
| 231 | +} |
0 commit comments