| 1 | //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// |
| 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 | // |
| 9 | // This is the llc code generator driver. It provides a convenient |
| 10 | // command-line interface for generating an assembly file or a relocatable file, |
| 11 | // given LLVM bitcode. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "NewPMDriver.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/ScopeExit.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
| 19 | #include "llvm/Analysis/RuntimeLibcallInfo.h" |
| 20 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 21 | #include "llvm/CodeGen/CommandFlags.h" |
| 22 | #include "llvm/CodeGen/LinkAllAsmWriterComponents.h" |
| 23 | #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
| 24 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
| 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 27 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 28 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 29 | #include "llvm/IR/AutoUpgrade.h" |
| 30 | #include "llvm/IR/DataLayout.h" |
| 31 | #include "llvm/IR/DiagnosticInfo.h" |
| 32 | #include "llvm/IR/DiagnosticPrinter.h" |
| 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/IR/LLVMRemarkStreamer.h" |
| 35 | #include "llvm/IR/LegacyPassManager.h" |
| 36 | #include "llvm/IR/Module.h" |
| 37 | #include "llvm/IR/Verifier.h" |
| 38 | #include "llvm/IRReader/IRReader.h" |
| 39 | #include "llvm/InitializePasses.h" |
| 40 | #include "llvm/MC/MCTargetOptionsCommandFlags.h" |
| 41 | #include "llvm/MC/TargetRegistry.h" |
| 42 | #include "llvm/Pass.h" |
| 43 | #include "llvm/Plugins/PassPlugin.h" |
| 44 | #include "llvm/Remarks/HotnessThresholdParser.h" |
| 45 | #include "llvm/Support/CommandLine.h" |
| 46 | #include "llvm/Support/Debug.h" |
| 47 | #include "llvm/Support/FileSystem.h" |
| 48 | #include "llvm/Support/FormattedStream.h" |
| 49 | #include "llvm/Support/InitLLVM.h" |
| 50 | #include "llvm/Support/PGOOptions.h" |
| 51 | #include "llvm/Support/Path.h" |
| 52 | #include "llvm/Support/PluginLoader.h" |
| 53 | #include "llvm/Support/SourceMgr.h" |
| 54 | #include "llvm/Support/TargetSelect.h" |
| 55 | #include "llvm/Support/TimeProfiler.h" |
| 56 | #include "llvm/Support/ToolOutputFile.h" |
| 57 | #include "llvm/Support/WithColor.h" |
| 58 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 59 | #include "llvm/Target/TargetMachine.h" |
| 60 | #include "llvm/TargetParser/Host.h" |
| 61 | #include "llvm/TargetParser/SubtargetFeature.h" |
| 62 | #include "llvm/TargetParser/Triple.h" |
| 63 | #include "llvm/Transforms/Utils/Cloning.h" |
| 64 | #include <cassert> |
| 65 | #include <memory> |
| 66 | #include <optional> |
| 67 | using namespace llvm; |
| 68 | |
| 69 | static codegen::RegisterCodeGenFlags CGF; |
| 70 | static codegen::RegisterSaveStatsFlag SSF; |
| 71 | |
| 72 | // General options for llc. Other pass-specific options are specified |
| 73 | // within the corresponding llc passes, and target-specific options |
| 74 | // and back-end code generation options are specified with the target machine. |
| 75 | // |
| 76 | static cl::opt<std::string> |
| 77 | InputFilename(cl::Positional, cl::desc("<input bitcode>" ), cl::init(Val: "-" )); |
| 78 | |
| 79 | static cl::list<std::string> |
| 80 | InstPrinterOptions("M" , cl::desc("InstPrinter options" )); |
| 81 | |
| 82 | static cl::opt<std::string> |
| 83 | InputLanguage("x" , cl::desc("Input language ('ir' or 'mir')" )); |
| 84 | |
| 85 | static cl::opt<std::string> OutputFilename("o" , cl::desc("Output filename" ), |
| 86 | cl::value_desc("filename" )); |
| 87 | |
| 88 | static cl::opt<std::string> |
| 89 | SplitDwarfOutputFile("split-dwarf-output" , cl::desc(".dwo output filename" ), |
| 90 | cl::value_desc("filename" )); |
| 91 | |
| 92 | static cl::opt<unsigned> |
| 93 | TimeCompilations("time-compilations" , cl::Hidden, cl::init(Val: 1u), |
| 94 | cl::value_desc("N" ), |
| 95 | cl::desc("Repeat compilation N times for timing" )); |
| 96 | |
| 97 | static cl::opt<bool> TimeTrace("time-trace" , cl::desc("Record time trace" )); |
| 98 | |
| 99 | static cl::opt<unsigned> TimeTraceGranularity( |
| 100 | "time-trace-granularity" , |
| 101 | cl::desc( |
| 102 | "Minimum time granularity (in microseconds) traced by time profiler" ), |
| 103 | cl::init(Val: 500), cl::Hidden); |
| 104 | |
| 105 | static cl::opt<std::string> |
| 106 | TimeTraceFile("time-trace-file" , |
| 107 | cl::desc("Specify time trace file destination" ), |
| 108 | cl::value_desc("filename" )); |
| 109 | |
| 110 | static cl::opt<std::string> |
| 111 | BinutilsVersion("binutils-version" , cl::Hidden, |
| 112 | cl::desc("Produced object files can use all ELF features " |
| 113 | "supported by this binutils version and newer." |
| 114 | "If -no-integrated-as is specified, the generated " |
| 115 | "assembly will consider GNU as support." |
| 116 | "'none' means that all ELF features can be used, " |
| 117 | "regardless of binutils support" )); |
| 118 | |
| 119 | static cl::opt<bool> |
| 120 | ("preserve-as-comments" , cl::Hidden, |
| 121 | cl::desc("Preserve Comments in outputted assembly" ), |
| 122 | cl::init(Val: true)); |
| 123 | |
| 124 | // Determine optimization level. |
| 125 | static cl::opt<char> |
| 126 | OptLevel("O" , |
| 127 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 128 | "(default = '-O2')" ), |
| 129 | cl::Prefix, cl::init(Val: '2')); |
| 130 | |
| 131 | static cl::opt<std::string> |
| 132 | TargetTriple("mtriple" , cl::desc("Override target triple for module" )); |
| 133 | |
| 134 | static cl::opt<std::string> SplitDwarfFile( |
| 135 | "split-dwarf-file" , |
| 136 | cl::desc( |
| 137 | "Specify the name of the .dwo file to encode in the DWARF output" )); |
| 138 | |
| 139 | static cl::opt<bool> NoVerify("disable-verify" , cl::Hidden, |
| 140 | cl::desc("Do not verify input module" )); |
| 141 | |
| 142 | static cl::opt<bool> VerifyEach("verify-each" , |
| 143 | cl::desc("Verify after each transform" )); |
| 144 | |
| 145 | static cl::opt<bool> |
| 146 | DisableSimplifyLibCalls("disable-simplify-libcalls" , |
| 147 | cl::desc("Disable simplify-libcalls" )); |
| 148 | |
| 149 | static cl::opt<bool> ShowMCEncoding("show-mc-encoding" , cl::Hidden, |
| 150 | cl::desc("Show encoding in .s output" )); |
| 151 | |
| 152 | static cl::opt<unsigned> |
| 153 | OutputAsmVariant("output-asm-variant" , |
| 154 | cl::desc("Syntax variant to use for output printing" )); |
| 155 | |
| 156 | static cl::opt<bool> |
| 157 | DwarfDirectory("dwarf-directory" , cl::Hidden, |
| 158 | cl::desc("Use .file directives with an explicit directory" ), |
| 159 | cl::init(Val: true)); |
| 160 | |
| 161 | static cl::opt<bool> AsmVerbose("asm-verbose" , |
| 162 | cl::desc("Add comments to directives." ), |
| 163 | cl::init(Val: true)); |
| 164 | |
| 165 | static cl::opt<bool> |
| 166 | CompileTwice("compile-twice" , cl::Hidden, |
| 167 | cl::desc("Run everything twice, re-using the same pass " |
| 168 | "manager and verify the result is the same." ), |
| 169 | cl::init(Val: false)); |
| 170 | |
| 171 | static cl::opt<bool> DiscardValueNames( |
| 172 | "discard-value-names" , |
| 173 | cl::desc("Discard names from Value (other than GlobalValue)." ), |
| 174 | cl::init(Val: false), cl::Hidden); |
| 175 | |
| 176 | static cl::opt<bool> |
| 177 | PrintMIR2VecVocab("print-mir2vec-vocab" , cl::Hidden, |
| 178 | cl::desc("Print MIR2Vec vocabulary contents" ), |
| 179 | cl::init(Val: false)); |
| 180 | |
| 181 | static cl::opt<bool> |
| 182 | PrintMIR2Vec("print-mir2vec" , cl::Hidden, |
| 183 | cl::desc("Print MIR2Vec embeddings for functions" ), |
| 184 | cl::init(Val: false)); |
| 185 | |
| 186 | static cl::list<std::string> IncludeDirs("I" , cl::desc("include search path" )); |
| 187 | |
| 188 | static cl::opt<bool> ( |
| 189 | "pass-remarks-with-hotness" , |
| 190 | cl::desc("With PGO, include profile count in optimization remarks" ), |
| 191 | cl::Hidden); |
| 192 | |
| 193 | static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser> |
| 194 | ( |
| 195 | "pass-remarks-hotness-threshold" , |
| 196 | cl::desc("Minimum profile count required for " |
| 197 | "an optimization remark to be output. " |
| 198 | "Use 'auto' to apply the threshold from profile summary." ), |
| 199 | cl::value_desc("N or 'auto'" ), cl::init(Val: 0), cl::Hidden); |
| 200 | |
| 201 | static cl::opt<std::string> |
| 202 | ("pass-remarks-output" , |
| 203 | cl::desc("Output filename for pass remarks" ), |
| 204 | cl::value_desc("filename" )); |
| 205 | |
| 206 | static cl::opt<std::string> |
| 207 | ("pass-remarks-filter" , |
| 208 | cl::desc("Only record optimization remarks from passes whose " |
| 209 | "names match the given regular expression" ), |
| 210 | cl::value_desc("regex" )); |
| 211 | |
| 212 | static cl::opt<std::string> ( |
| 213 | "pass-remarks-format" , |
| 214 | cl::desc("The format used for serializing remarks (default: YAML)" ), |
| 215 | cl::value_desc("format" ), cl::init(Val: "yaml" )); |
| 216 | |
| 217 | static cl::list<std::string> PassPlugins("load-pass-plugin" , |
| 218 | cl::desc("Load plugin library" )); |
| 219 | |
| 220 | static cl::opt<bool> EnableNewPassManager( |
| 221 | "enable-new-pm" , cl::desc("Enable the new pass manager" ), cl::init(Val: false)); |
| 222 | |
| 223 | // This flag specifies a textual description of the optimization pass pipeline |
| 224 | // to run over the module. This flag switches opt to use the new pass manager |
| 225 | // infrastructure, completely disabling all of the flags specific to the old |
| 226 | // pass management. |
| 227 | static cl::opt<std::string> PassPipeline( |
| 228 | "passes" , |
| 229 | cl::desc( |
| 230 | "A textual description of the pass pipeline. To have analysis passes " |
| 231 | "available before a certain pass, add 'require<foo-analysis>'." )); |
| 232 | static cl::alias PassPipeline2("p" , cl::aliasopt(PassPipeline), |
| 233 | cl::desc("Alias for -passes" )); |
| 234 | |
| 235 | static std::vector<std::string> &getRunPassNames() { |
| 236 | static std::vector<std::string> RunPassNames; |
| 237 | return RunPassNames; |
| 238 | } |
| 239 | |
| 240 | namespace { |
| 241 | struct RunPassOption { |
| 242 | void operator=(const std::string &Val) const { |
| 243 | if (Val.empty()) |
| 244 | return; |
| 245 | SmallVector<StringRef, 8> PassNames; |
| 246 | StringRef(Val).split(A&: PassNames, Separator: ',', MaxSplit: -1, KeepEmpty: false); |
| 247 | for (auto PassName : PassNames) |
| 248 | getRunPassNames().push_back(x: std::string(PassName)); |
| 249 | } |
| 250 | }; |
| 251 | } // namespace |
| 252 | |
| 253 | static RunPassOption RunPassOpt; |
| 254 | |
| 255 | static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass( |
| 256 | "run-pass" , |
| 257 | cl::desc("Run compiler only for specified passes (comma separated list)" ), |
| 258 | cl::value_desc("pass-name" ), cl::location(L&: RunPassOpt)); |
| 259 | |
| 260 | // PGO command line options |
| 261 | enum PGOKind { |
| 262 | NoPGO, |
| 263 | SampleUse, |
| 264 | }; |
| 265 | |
| 266 | static cl::opt<PGOKind> |
| 267 | PGOKindFlag("pgo-kind" , cl::init(Val: NoPGO), cl::Hidden, |
| 268 | cl::desc("The kind of profile guided optimization" ), |
| 269 | cl::values(clEnumValN(NoPGO, "nopgo" , "Do not use PGO." ), |
| 270 | clEnumValN(SampleUse, "pgo-sample-use-pipeline" , |
| 271 | "Use sampled profile to guide PGO." ))); |
| 272 | |
| 273 | // Function to set PGO options on TargetMachine based on command line flags. |
| 274 | static void setPGOOptions(TargetMachine &TM) { |
| 275 | std::optional<PGOOptions> PGOOpt; |
| 276 | |
| 277 | switch (PGOKindFlag) { |
| 278 | case SampleUse: |
| 279 | // Use default values for other PGOOptions parameters. This parameter |
| 280 | // is used to test that PGO data is preserved at -O0. |
| 281 | PGOOpt = PGOOptions("" , "" , "" , "" , PGOOptions::SampleUse, |
| 282 | PGOOptions::NoCSAction); |
| 283 | break; |
| 284 | case NoPGO: |
| 285 | PGOOpt = std::nullopt; |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | if (PGOOpt) |
| 290 | TM.setPGOOption(PGOOpt); |
| 291 | } |
| 292 | |
| 293 | static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &, |
| 294 | LLVMContext &Context, std::string &OutputFilename); |
| 295 | |
| 296 | [[noreturn]] static void reportError(Twine Msg, StringRef Filename = "" ) { |
| 297 | SmallString<256> Prefix; |
| 298 | if (!Filename.empty()) { |
| 299 | if (Filename == "-" ) |
| 300 | Filename = "<stdin>" ; |
| 301 | ("'" + Twine(Filename) + "': " ).toStringRef(Out&: Prefix); |
| 302 | } |
| 303 | WithColor::error(OS&: errs(), Prefix: "llc" ) << Prefix << Msg << "\n" ; |
| 304 | exit(status: 1); |
| 305 | } |
| 306 | |
| 307 | [[noreturn]] static void reportError(Error Err, StringRef Filename) { |
| 308 | assert(Err); |
| 309 | handleAllErrors(E: createFileError(F: Filename, E: std::move(Err)), |
| 310 | Handlers: [&](const ErrorInfoBase &EI) { reportError(Msg: EI.message()); }); |
| 311 | llvm_unreachable("reportError() should not return" ); |
| 312 | } |
| 313 | |
| 314 | static std::unique_ptr<ToolOutputFile> GetOutputStream(Triple::OSType OS) { |
| 315 | // If we don't yet have an output filename, make one. |
| 316 | if (OutputFilename.empty()) { |
| 317 | if (InputFilename == "-" ) |
| 318 | OutputFilename = "-" ; |
| 319 | else { |
| 320 | // If InputFilename ends in .bc or .ll, remove it. |
| 321 | StringRef IFN = InputFilename; |
| 322 | if (IFN.ends_with(Suffix: ".bc" ) || IFN.ends_with(Suffix: ".ll" )) |
| 323 | OutputFilename = std::string(IFN.drop_back(N: 3)); |
| 324 | else if (IFN.ends_with(Suffix: ".mir" )) |
| 325 | OutputFilename = std::string(IFN.drop_back(N: 4)); |
| 326 | else |
| 327 | OutputFilename = std::string(IFN); |
| 328 | |
| 329 | switch (codegen::getFileType()) { |
| 330 | case CodeGenFileType::AssemblyFile: |
| 331 | OutputFilename += ".s" ; |
| 332 | break; |
| 333 | case CodeGenFileType::ObjectFile: |
| 334 | if (OS == Triple::Win32) |
| 335 | OutputFilename += ".obj" ; |
| 336 | else |
| 337 | OutputFilename += ".o" ; |
| 338 | break; |
| 339 | case CodeGenFileType::Null: |
| 340 | OutputFilename = "-" ; |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Decide if we need "binary" output. |
| 347 | bool Binary = false; |
| 348 | switch (codegen::getFileType()) { |
| 349 | case CodeGenFileType::AssemblyFile: |
| 350 | break; |
| 351 | case CodeGenFileType::ObjectFile: |
| 352 | case CodeGenFileType::Null: |
| 353 | Binary = true; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | // Open the file. |
| 358 | std::error_code EC; |
| 359 | sys::fs::OpenFlags OpenFlags = sys::fs::OF_None; |
| 360 | if (!Binary) |
| 361 | OpenFlags |= sys::fs::OF_TextWithCRLF; |
| 362 | auto FDOut = std::make_unique<ToolOutputFile>(args&: OutputFilename, args&: EC, args&: OpenFlags); |
| 363 | if (EC) |
| 364 | reportError(Msg: EC.message()); |
| 365 | return FDOut; |
| 366 | } |
| 367 | |
| 368 | // main - Entry point for the llc compiler. |
| 369 | // |
| 370 | int main(int argc, char **argv) { |
| 371 | InitLLVM X(argc, argv); |
| 372 | |
| 373 | // Enable debug stream buffering. |
| 374 | EnableDebugBuffering = true; |
| 375 | |
| 376 | // Initialize targets first, so that --version shows registered targets. |
| 377 | InitializeAllTargets(); |
| 378 | InitializeAllTargetMCs(); |
| 379 | InitializeAllAsmPrinters(); |
| 380 | InitializeAllAsmParsers(); |
| 381 | |
| 382 | // Initialize codegen and IR passes used by llc so that the -print-after, |
| 383 | // -print-before, and -stop-after options work. |
| 384 | PassRegistry *Registry = PassRegistry::getPassRegistry(); |
| 385 | initializeCore(*Registry); |
| 386 | initializeCodeGen(*Registry); |
| 387 | initializeLoopStrengthReducePass(*Registry); |
| 388 | initializePostInlineEntryExitInstrumenterPass(*Registry); |
| 389 | initializeUnreachableBlockElimLegacyPassPass(*Registry); |
| 390 | initializeConstantHoistingLegacyPassPass(*Registry); |
| 391 | initializeScalarOpts(*Registry); |
| 392 | initializeVectorization(*Registry); |
| 393 | initializeScalarizeMaskedMemIntrinLegacyPassPass(*Registry); |
| 394 | initializeTransformUtils(*Registry); |
| 395 | |
| 396 | // Initialize debugging passes. |
| 397 | initializeScavengerTestPass(*Registry); |
| 398 | |
| 399 | SmallVector<PassPlugin, 1> PluginList; |
| 400 | PassPlugins.setCallback([&](const std::string &PluginPath) { |
| 401 | auto Plugin = PassPlugin::Load(Filename: PluginPath); |
| 402 | if (!Plugin) |
| 403 | reportFatalUsageError(Err: Plugin.takeError()); |
| 404 | PluginList.emplace_back(Args&: Plugin.get()); |
| 405 | }); |
| 406 | |
| 407 | // Register the Target and CPU printer for --version. |
| 408 | cl::AddExtraVersionPrinter(func: sys::printDefaultTargetAndDetectedCPU); |
| 409 | // Register the target printer for --version. |
| 410 | cl::AddExtraVersionPrinter(func: TargetRegistry::printRegisteredTargetsForVersion); |
| 411 | |
| 412 | cl::ParseCommandLineOptions(argc, argv, Overview: "llvm system compiler\n" ); |
| 413 | |
| 414 | if (!PassPipeline.empty() && !getRunPassNames().empty()) { |
| 415 | errs() << "The `llc -run-pass=...` syntax for the new pass manager is " |
| 416 | "not supported, please use `llc -passes=<pipeline>` (or the `-p` " |
| 417 | "alias for a more concise version).\n" ; |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | if (TimeTrace) |
| 422 | timeTraceProfilerInitialize(TimeTraceGranularity, ProcName: argv[0]); |
| 423 | llvm::scope_exit TimeTraceScopeExit([]() { |
| 424 | if (TimeTrace) { |
| 425 | if (auto E = timeTraceProfilerWrite(PreferredFileName: TimeTraceFile, FallbackFileName: OutputFilename)) { |
| 426 | handleAllErrors(E: std::move(E), Handlers: [&](const StringError &SE) { |
| 427 | errs() << SE.getMessage() << "\n" ; |
| 428 | }); |
| 429 | return; |
| 430 | } |
| 431 | timeTraceProfilerCleanup(); |
| 432 | } |
| 433 | }); |
| 434 | |
| 435 | LLVMContext Context; |
| 436 | Context.setDiscardValueNames(DiscardValueNames); |
| 437 | |
| 438 | // Set a diagnostic handler that doesn't exit on the first error |
| 439 | Context.setDiagnosticHandler(DH: std::make_unique<LLCDiagnosticHandler>()); |
| 440 | |
| 441 | Expected<LLVMRemarkFileHandle> = |
| 442 | setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses, |
| 443 | RemarksFormat, RemarksWithHotness, |
| 444 | RemarksHotnessThreshold); |
| 445 | if (Error E = RemarksFileOrErr.takeError()) |
| 446 | reportError(Err: std::move(E), Filename: RemarksFilename); |
| 447 | LLVMRemarkFileHandle = std::move(*RemarksFileOrErr); |
| 448 | |
| 449 | codegen::MaybeEnableStatistics(); |
| 450 | std::string OutputFilename; |
| 451 | |
| 452 | if (InputLanguage != "" && InputLanguage != "ir" && InputLanguage != "mir" ) |
| 453 | reportError(Msg: "input language must be '', 'IR' or 'MIR'" ); |
| 454 | |
| 455 | // Compile the module TimeCompilations times to give better compile time |
| 456 | // metrics. |
| 457 | for (unsigned I = TimeCompilations; I; --I) |
| 458 | if (int RetVal = compileModule(argv, PluginList, Context, OutputFilename)) |
| 459 | return RetVal; |
| 460 | |
| 461 | if (RemarksFile) |
| 462 | RemarksFile->keep(); |
| 463 | |
| 464 | return codegen::MaybeSaveStatistics(OutputFilename, ToolName: "llc" ); |
| 465 | } |
| 466 | |
| 467 | static bool addPass(PassManagerBase &PM, const char *argv0, StringRef PassName, |
| 468 | TargetPassConfig &TPC) { |
| 469 | if (PassName == "none" ) |
| 470 | return false; |
| 471 | |
| 472 | const PassRegistry *PR = PassRegistry::getPassRegistry(); |
| 473 | const PassInfo *PI = PR->getPassInfo(Arg: PassName); |
| 474 | if (!PI) { |
| 475 | WithColor::error(OS&: errs(), Prefix: argv0) |
| 476 | << "run-pass " << PassName << " is not registered.\n" ; |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | Pass *P; |
| 481 | if (PI->getNormalCtor()) |
| 482 | P = PI->getNormalCtor()(); |
| 483 | else { |
| 484 | WithColor::error(OS&: errs(), Prefix: argv0) |
| 485 | << "cannot create pass: " << PI->getPassName() << "\n" ; |
| 486 | return true; |
| 487 | } |
| 488 | std::string Banner = std::string("After " ) + std::string(P->getPassName()); |
| 489 | TPC.addMachinePrePasses(); |
| 490 | PM.add(P); |
| 491 | TPC.addMachinePostPasses(Banner); |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList, |
| 497 | LLVMContext &Context, std::string &OutputFilename) { |
| 498 | // Load the module to be compiled... |
| 499 | SMDiagnostic Err; |
| 500 | std::unique_ptr<Module> M; |
| 501 | std::unique_ptr<MIRParser> MIR; |
| 502 | Triple TheTriple; |
| 503 | std::string CPUStr = codegen::getCPUStr(), |
| 504 | FeaturesStr = codegen::getFeaturesStr(); |
| 505 | |
| 506 | // Set attributes on functions as loaded from MIR from command line arguments. |
| 507 | auto setMIRFunctionAttributes = [&CPUStr, &FeaturesStr](Function &F) { |
| 508 | codegen::setFunctionAttributes(CPU: CPUStr, Features: FeaturesStr, F); |
| 509 | }; |
| 510 | |
| 511 | auto MAttrs = codegen::getMAttrs(); |
| 512 | bool SkipModule = |
| 513 | CPUStr == "help" || (!MAttrs.empty() && MAttrs.front() == "help" ); |
| 514 | |
| 515 | CodeGenOptLevel OLvl; |
| 516 | if (auto Level = CodeGenOpt::parseLevel(C: OptLevel)) { |
| 517 | OLvl = *Level; |
| 518 | } else { |
| 519 | WithColor::error(OS&: errs(), Prefix: argv[0]) << "invalid optimization level.\n" ; |
| 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | // Parse 'none' or '$major.$minor'. Disallow -binutils-version=0 because we |
| 524 | // use that to indicate the MC default. |
| 525 | if (!BinutilsVersion.empty() && BinutilsVersion != "none" ) { |
| 526 | StringRef V = BinutilsVersion.getValue(); |
| 527 | unsigned Num; |
| 528 | if (V.consumeInteger(Radix: 10, Result&: Num) || Num == 0 || |
| 529 | !(V.empty() || |
| 530 | (V.consume_front(Prefix: "." ) && !V.consumeInteger(Radix: 10, Result&: Num) && V.empty()))) { |
| 531 | WithColor::error(OS&: errs(), Prefix: argv[0]) |
| 532 | << "invalid -binutils-version, accepting 'none' or major.minor\n" ; |
| 533 | return 1; |
| 534 | } |
| 535 | } |
| 536 | TargetOptions Options; |
| 537 | auto InitializeOptions = [&](const Triple &TheTriple) { |
| 538 | Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple); |
| 539 | |
| 540 | if (Options.XCOFFReadOnlyPointers) { |
| 541 | if (!TheTriple.isOSAIX()) |
| 542 | reportError(Msg: "-mxcoff-roptr option is only supported on AIX" , |
| 543 | Filename: InputFilename); |
| 544 | |
| 545 | // Since the storage mapping class is specified per csect, |
| 546 | // without using data sections, it is less effective to use read-only |
| 547 | // pointers. Using read-only pointers may cause other RO variables in the |
| 548 | // same csect to become RW when the linker acts upon `-bforceimprw`; |
| 549 | // therefore, we require that separate data sections are used in the |
| 550 | // presence of ReadOnlyPointers. We respect the setting of data-sections |
| 551 | // since we have not found reasons to do otherwise that overcome the user |
| 552 | // surprise of not respecting the setting. |
| 553 | if (!Options.DataSections) |
| 554 | reportError(Msg: "-mxcoff-roptr option must be used with -data-sections" , |
| 555 | Filename: InputFilename); |
| 556 | } |
| 557 | |
| 558 | if (TheTriple.isX86() && |
| 559 | codegen::getFuseFPOps() != FPOpFusion::FPOpFusionMode::Standard) |
| 560 | WithColor::warning(OS&: errs(), Prefix: argv[0]) |
| 561 | << "X86 backend ignores --fp-contract setting; use IR fast-math " |
| 562 | "flags instead." ; |
| 563 | |
| 564 | Options.BinutilsVersion = |
| 565 | TargetMachine::parseBinutilsVersion(Version: BinutilsVersion); |
| 566 | Options.MCOptions.ShowMCEncoding = ShowMCEncoding; |
| 567 | Options.MCOptions.AsmVerbose = AsmVerbose; |
| 568 | Options.MCOptions.PreserveAsmComments = PreserveComments; |
| 569 | if (OutputAsmVariant.getNumOccurrences()) |
| 570 | Options.MCOptions.OutputAsmVariant = OutputAsmVariant; |
| 571 | Options.MCOptions.IASSearchPaths = IncludeDirs; |
| 572 | Options.MCOptions.InstPrinterOptions = InstPrinterOptions; |
| 573 | Options.MCOptions.SplitDwarfFile = SplitDwarfFile; |
| 574 | if (DwarfDirectory.getPosition()) { |
| 575 | Options.MCOptions.MCUseDwarfDirectory = |
| 576 | DwarfDirectory ? MCTargetOptions::EnableDwarfDirectory |
| 577 | : MCTargetOptions::DisableDwarfDirectory; |
| 578 | } else { |
| 579 | // -dwarf-directory is not set explicitly. Some assemblers |
| 580 | // (e.g. GNU as or ptxas) do not support `.file directory' |
| 581 | // syntax prior to DWARFv5. Let the target decide the default |
| 582 | // value. |
| 583 | Options.MCOptions.MCUseDwarfDirectory = |
| 584 | MCTargetOptions::DefaultDwarfDirectory; |
| 585 | } |
| 586 | }; |
| 587 | |
| 588 | std::optional<Reloc::Model> RM = codegen::getExplicitRelocModel(); |
| 589 | std::optional<CodeModel::Model> CM = codegen::getExplicitCodeModel(); |
| 590 | |
| 591 | const Target *TheTarget = nullptr; |
| 592 | std::unique_ptr<TargetMachine> Target; |
| 593 | |
| 594 | // If user just wants to list available options, skip module loading |
| 595 | if (!SkipModule) { |
| 596 | auto SetDataLayout = [&](StringRef DataLayoutTargetTriple, |
| 597 | StringRef OldDLStr) -> std::optional<std::string> { |
| 598 | // If we are supposed to override the target triple, do so now. |
| 599 | std::string IRTargetTriple = DataLayoutTargetTriple.str(); |
| 600 | if (!TargetTriple.empty()) |
| 601 | IRTargetTriple = Triple::normalize(Str: TargetTriple); |
| 602 | TheTriple = Triple(IRTargetTriple); |
| 603 | if (TheTriple.getTriple().empty()) |
| 604 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
| 605 | |
| 606 | std::string Error; |
| 607 | TheTarget = |
| 608 | TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error); |
| 609 | if (!TheTarget) { |
| 610 | WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n" ; |
| 611 | exit(status: 1); |
| 612 | } |
| 613 | |
| 614 | InitializeOptions(TheTriple); |
| 615 | Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
| 616 | TT: TheTriple, CPU: CPUStr, Features: FeaturesStr, Options, RM, CM, OL: OLvl)); |
| 617 | assert(Target && "Could not allocate target machine!" ); |
| 618 | |
| 619 | // Set PGO options based on command line flags |
| 620 | setPGOOptions(*Target); |
| 621 | |
| 622 | return Target->createDataLayout().getStringRepresentation(); |
| 623 | }; |
| 624 | if (InputLanguage == "mir" || |
| 625 | (InputLanguage == "" && StringRef(InputFilename).ends_with(Suffix: ".mir" ))) { |
| 626 | MIR = createMIRParserFromFile(Filename: InputFilename, Error&: Err, Context, |
| 627 | ProcessIRFunction: setMIRFunctionAttributes); |
| 628 | if (MIR) |
| 629 | M = MIR->parseIRModule(DataLayoutCallback: SetDataLayout); |
| 630 | } else { |
| 631 | M = parseIRFile(Filename: InputFilename, Err, Context, |
| 632 | Callbacks: ParserCallbacks(SetDataLayout)); |
| 633 | } |
| 634 | if (!M) { |
| 635 | Err.print(ProgName: argv[0], S&: WithColor::error(OS&: errs(), Prefix: argv[0])); |
| 636 | return 1; |
| 637 | } |
| 638 | if (!TargetTriple.empty()) |
| 639 | M->setTargetTriple(Triple(Triple::normalize(Str: TargetTriple))); |
| 640 | |
| 641 | std::optional<CodeModel::Model> CM_IR = M->getCodeModel(); |
| 642 | if (!CM && CM_IR) |
| 643 | Target->setCodeModel(*CM_IR); |
| 644 | if (std::optional<uint64_t> LDT = codegen::getExplicitLargeDataThreshold()) |
| 645 | Target->setLargeDataThreshold(*LDT); |
| 646 | } else { |
| 647 | TheTriple = Triple(Triple::normalize(Str: TargetTriple)); |
| 648 | if (TheTriple.getTriple().empty()) |
| 649 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
| 650 | |
| 651 | // Get the target specific parser. |
| 652 | std::string Error; |
| 653 | TheTarget = |
| 654 | TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error); |
| 655 | if (!TheTarget) { |
| 656 | WithColor::error(OS&: errs(), Prefix: argv[0]) << Error << "\n" ; |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | InitializeOptions(TheTriple); |
| 661 | Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
| 662 | TT: TheTriple, CPU: CPUStr, Features: FeaturesStr, Options, RM, CM, OL: OLvl)); |
| 663 | assert(Target && "Could not allocate target machine!" ); |
| 664 | |
| 665 | // Set PGO options based on command line flags |
| 666 | setPGOOptions(*Target); |
| 667 | |
| 668 | // If we don't have a module then just exit now. We do this down |
| 669 | // here since the CPU/Feature help is underneath the target machine |
| 670 | // creation. |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | assert(M && "Should have exited if we didn't have a module!" ); |
| 675 | if (codegen::getFloatABIForCalls() != FloatABI::Default) |
| 676 | Target->Options.FloatABIType = codegen::getFloatABIForCalls(); |
| 677 | |
| 678 | // Figure out where we are going to send the output. |
| 679 | std::unique_ptr<ToolOutputFile> Out = GetOutputStream(OS: TheTriple.getOS()); |
| 680 | if (!Out) |
| 681 | return 1; |
| 682 | |
| 683 | // Ensure the filename is passed down to CodeViewDebug. |
| 684 | Target->Options.ObjectFilenameForDebug = Out->outputFilename(); |
| 685 | |
| 686 | // Return a copy of the output filename via the output param |
| 687 | OutputFilename = Out->outputFilename(); |
| 688 | |
| 689 | // Tell target that this tool is not necessarily used with argument ABI |
| 690 | // compliance (i.e. narrow integer argument extensions). |
| 691 | Target->Options.VerifyArgABICompliance = 0; |
| 692 | |
| 693 | std::unique_ptr<ToolOutputFile> DwoOut; |
| 694 | if (!SplitDwarfOutputFile.empty()) { |
| 695 | std::error_code EC; |
| 696 | DwoOut = std::make_unique<ToolOutputFile>(args&: SplitDwarfOutputFile, args&: EC, |
| 697 | args: sys::fs::OF_None); |
| 698 | if (EC) |
| 699 | reportError(Msg: EC.message(), Filename: SplitDwarfOutputFile); |
| 700 | } |
| 701 | |
| 702 | // Add an appropriate TargetLibraryInfo pass for the module's triple. |
| 703 | TargetLibraryInfoImpl TLII(M->getTargetTriple(), Target->Options.VecLib); |
| 704 | |
| 705 | // The -disable-simplify-libcalls flag actually disables all builtin optzns. |
| 706 | if (DisableSimplifyLibCalls) |
| 707 | TLII.disableAllFunctions(); |
| 708 | |
| 709 | // Verify module immediately to catch problems before doInitialization() is |
| 710 | // called on any passes. |
| 711 | if (!NoVerify && verifyModule(M: *M, OS: &errs())) |
| 712 | reportError(Msg: "input module cannot be verified" , Filename: InputFilename); |
| 713 | |
| 714 | // Override function attributes based on CPUStr, FeaturesStr, and command line |
| 715 | // flags. |
| 716 | codegen::setFunctionAttributes(CPU: CPUStr, Features: FeaturesStr, M&: *M); |
| 717 | |
| 718 | for (auto &Plugin : PluginList) { |
| 719 | CodeGenFileType CGFT = codegen::getFileType(); |
| 720 | if (Plugin.invokePreCodeGenCallback(M&: *M, TM&: *Target, CGFT, OS&: Out->os())) { |
| 721 | // TODO: Deduplicate code with below and the NewPMDriver. |
| 722 | if (Context.getDiagHandlerPtr()->HasErrors) |
| 723 | exit(status: 1); |
| 724 | Out->keep(); |
| 725 | return 0; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if (mc::getExplicitRelaxAll() && |
| 730 | codegen::getFileType() != CodeGenFileType::ObjectFile) |
| 731 | WithColor::warning(OS&: errs(), Prefix: argv[0]) |
| 732 | << ": warning: ignoring -mc-relax-all because filetype != obj" ; |
| 733 | |
| 734 | VerifierKind VK = VerifierKind::InputOutput; |
| 735 | if (NoVerify) |
| 736 | VK = VerifierKind::None; |
| 737 | else if (VerifyEach) |
| 738 | VK = VerifierKind::EachPass; |
| 739 | |
| 740 | if (EnableNewPassManager || !PassPipeline.empty()) { |
| 741 | return compileModuleWithNewPM(Arg0: argv[0], M: std::move(M), MIR: std::move(MIR), |
| 742 | Target: std::move(Target), Out: std::move(Out), |
| 743 | DwoOut: std::move(DwoOut), Context, TLII, VK, |
| 744 | PassPipeline, FileType: codegen::getFileType()); |
| 745 | } |
| 746 | |
| 747 | // Build up all of the passes that we want to do to the module. |
| 748 | legacy::PassManager PM; |
| 749 | PM.add(P: new TargetLibraryInfoWrapperPass(TLII)); |
| 750 | PM.add(P: new RuntimeLibraryInfoWrapper( |
| 751 | TheTriple, Target->Options.ExceptionModel, Target->Options.FloatABIType, |
| 752 | Target->Options.EABIVersion, Options.MCOptions.ABIName, |
| 753 | Target->Options.VecLib)); |
| 754 | |
| 755 | { |
| 756 | raw_pwrite_stream *OS = &Out->os(); |
| 757 | |
| 758 | // Manually do the buffering rather than using buffer_ostream, |
| 759 | // so we can memcmp the contents in CompileTwice mode |
| 760 | SmallVector<char, 0> Buffer; |
| 761 | std::unique_ptr<raw_svector_ostream> BOS; |
| 762 | if ((codegen::getFileType() != CodeGenFileType::AssemblyFile && |
| 763 | !Out->os().supportsSeeking()) || |
| 764 | CompileTwice) { |
| 765 | BOS = std::make_unique<raw_svector_ostream>(args&: Buffer); |
| 766 | OS = BOS.get(); |
| 767 | } |
| 768 | |
| 769 | const char *argv0 = argv[0]; |
| 770 | MachineModuleInfoWrapperPass *MMIWP = |
| 771 | new MachineModuleInfoWrapperPass(Target.get()); |
| 772 | |
| 773 | // Set a temporary diagnostic handler. This is used before |
| 774 | // MachineModuleInfoWrapperPass::doInitialization for features like -M. |
| 775 | bool HasMCErrors = false; |
| 776 | MCContext &MCCtx = MMIWP->getMMI().getContext(); |
| 777 | MCCtx.setDiagnosticHandler([&](const SMDiagnostic &SMD, bool IsInlineAsm, |
| 778 | const SourceMgr &SrcMgr, |
| 779 | std::vector<const MDNode *> &LocInfos) { |
| 780 | WithColor::error(OS&: errs(), Prefix: argv0) << SMD.getMessage() << '\n'; |
| 781 | HasMCErrors = true; |
| 782 | }); |
| 783 | |
| 784 | // Construct a custom pass pipeline that starts after instruction |
| 785 | // selection. |
| 786 | if (!getRunPassNames().empty()) { |
| 787 | if (!MIR) { |
| 788 | WithColor::error(OS&: errs(), Prefix: argv[0]) |
| 789 | << "run-pass is for .mir file only.\n" ; |
| 790 | delete MMIWP; |
| 791 | return 1; |
| 792 | } |
| 793 | TargetPassConfig *PTPC = Target->createPassConfig(PM); |
| 794 | TargetPassConfig &TPC = *PTPC; |
| 795 | if (TPC.hasLimitedCodeGenPipeline()) { |
| 796 | WithColor::error(OS&: errs(), Prefix: argv[0]) |
| 797 | << "run-pass cannot be used with " |
| 798 | << TPC.getLimitedCodeGenPipelineReason() << ".\n" ; |
| 799 | delete PTPC; |
| 800 | delete MMIWP; |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | TPC.setDisableVerify(NoVerify); |
| 805 | PM.add(P: &TPC); |
| 806 | PM.add(P: MMIWP); |
| 807 | TPC.printAndVerify(Banner: "" ); |
| 808 | for (const std::string &RunPassName : getRunPassNames()) { |
| 809 | if (addPass(PM, argv0, PassName: RunPassName, TPC)) |
| 810 | return 1; |
| 811 | } |
| 812 | TPC.setInitialized(); |
| 813 | PM.add(P: createPrintMIRPass(OS&: *OS)); |
| 814 | |
| 815 | // Add MIR2Vec vocabulary printer if requested |
| 816 | if (PrintMIR2VecVocab) { |
| 817 | PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs())); |
| 818 | } |
| 819 | |
| 820 | // Add MIR2Vec printer if requested |
| 821 | if (PrintMIR2Vec) { |
| 822 | PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs())); |
| 823 | } |
| 824 | |
| 825 | PM.add(P: createFreeMachineFunctionPass()); |
| 826 | } else { |
| 827 | if (Target->addPassesToEmitFile(PM, *OS, DwoOut ? &DwoOut->os() : nullptr, |
| 828 | codegen::getFileType(), NoVerify, |
| 829 | MMIWP)) { |
| 830 | if (!HasMCErrors) |
| 831 | reportError(Msg: "target does not support generation of this file type" ); |
| 832 | } |
| 833 | |
| 834 | // Add MIR2Vec vocabulary printer if requested |
| 835 | if (PrintMIR2VecVocab) { |
| 836 | PM.add(P: createMIR2VecVocabPrinterLegacyPass(OS&: errs())); |
| 837 | } |
| 838 | |
| 839 | // Add MIR2Vec printer if requested |
| 840 | if (PrintMIR2Vec) { |
| 841 | PM.add(P: createMIR2VecPrinterLegacyPass(OS&: errs())); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | Target->getObjFileLowering()->Initialize(ctx&: MMIWP->getMMI().getContext(), |
| 846 | TM: *Target); |
| 847 | if (MIR) { |
| 848 | assert(MMIWP && "Forgot to create MMIWP?" ); |
| 849 | if (MIR->parseMachineFunctions(M&: *M, MMI&: MMIWP->getMMI())) |
| 850 | return 1; |
| 851 | } |
| 852 | |
| 853 | // Before executing passes, print the final values of the LLVM options. |
| 854 | cl::PrintOptionValues(); |
| 855 | |
| 856 | // If requested, run the pass manager over the same module again, |
| 857 | // to catch any bugs due to persistent state in the passes. Note that |
| 858 | // opt has the same functionality, so it may be worth abstracting this out |
| 859 | // in the future. |
| 860 | SmallVector<char, 0> CompileTwiceBuffer; |
| 861 | if (CompileTwice) { |
| 862 | std::unique_ptr<Module> M2(llvm::CloneModule(M: *M)); |
| 863 | PM.run(M&: *M2); |
| 864 | CompileTwiceBuffer = Buffer; |
| 865 | Buffer.clear(); |
| 866 | } |
| 867 | |
| 868 | PM.run(M&: *M); |
| 869 | |
| 870 | if (Context.getDiagHandlerPtr()->HasErrors || HasMCErrors) |
| 871 | return 1; |
| 872 | |
| 873 | // Compare the two outputs and make sure they're the same |
| 874 | if (CompileTwice) { |
| 875 | if (Buffer.size() != CompileTwiceBuffer.size() || |
| 876 | (memcmp(s1: Buffer.data(), s2: CompileTwiceBuffer.data(), n: Buffer.size()) != |
| 877 | 0)) { |
| 878 | errs() |
| 879 | << "Running the pass manager twice changed the output.\n" |
| 880 | "Writing the result of the second run to the specified output\n" |
| 881 | "To generate the one-run comparison binary, just run without\n" |
| 882 | "the compile-twice option\n" ; |
| 883 | Out->os() << Buffer; |
| 884 | Out->keep(); |
| 885 | return 1; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | if (BOS) { |
| 890 | Out->os() << Buffer; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | // Declare success. |
| 895 | Out->keep(); |
| 896 | if (DwoOut) |
| 897 | DwoOut->keep(); |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | |