LTOBackend.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. //===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
  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 file implements the "backend" phase of LTO, i.e. it performs
  10. // optimization and code generation on a loaded module. It is generally used
  11. // internally by the LTO class but can also be used independently, for example
  12. // to implement a standalone ThinLTO backend.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/LTO/LTOBackend.h"
  16. #include "llvm/Analysis/AliasAnalysis.h"
  17. #include "llvm/Analysis/CGSCCPassManager.h"
  18. #include "llvm/Analysis/ModuleSummaryAnalysis.h"
  19. #include "llvm/Analysis/TargetLibraryInfo.h"
  20. #include "llvm/Bitcode/BitcodeReader.h"
  21. #include "llvm/Bitcode/BitcodeWriter.h"
  22. #include "llvm/IR/LLVMRemarkStreamer.h"
  23. #include "llvm/IR/LegacyPassManager.h"
  24. #include "llvm/IR/PassManager.h"
  25. #include "llvm/IR/Verifier.h"
  26. #include "llvm/LTO/LTO.h"
  27. #include "llvm/MC/SubtargetFeature.h"
  28. #include "llvm/MC/TargetRegistry.h"
  29. #include "llvm/Object/ModuleSymbolTable.h"
  30. #include "llvm/Passes/PassBuilder.h"
  31. #include "llvm/Passes/PassPlugin.h"
  32. #include "llvm/Passes/StandardInstrumentations.h"
  33. #include "llvm/Support/Error.h"
  34. #include "llvm/Support/FileSystem.h"
  35. #include "llvm/Support/MemoryBuffer.h"
  36. #include "llvm/Support/Path.h"
  37. #include "llvm/Support/Program.h"
  38. #include "llvm/Support/ThreadPool.h"
  39. #include "llvm/Support/ToolOutputFile.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include "llvm/Target/TargetMachine.h"
  42. #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
  43. #include "llvm/Transforms/Scalar/LoopPassManager.h"
  44. #include "llvm/Transforms/Utils/FunctionImportUtils.h"
  45. #include "llvm/Transforms/Utils/SplitModule.h"
  46. #include <optional>
  47. using namespace llvm;
  48. using namespace lto;
  49. #define DEBUG_TYPE "lto-backend"
  50. enum class LTOBitcodeEmbedding {
  51. DoNotEmbed = 0,
  52. EmbedOptimized = 1,
  53. EmbedPostMergePreOptimized = 2
  54. };
  55. static cl::opt<LTOBitcodeEmbedding> EmbedBitcode(
  56. "lto-embed-bitcode", cl::init(LTOBitcodeEmbedding::DoNotEmbed),
  57. cl::values(clEnumValN(LTOBitcodeEmbedding::DoNotEmbed, "none",
  58. "Do not embed"),
  59. clEnumValN(LTOBitcodeEmbedding::EmbedOptimized, "optimized",
  60. "Embed after all optimization passes"),
  61. clEnumValN(LTOBitcodeEmbedding::EmbedPostMergePreOptimized,
  62. "post-merge-pre-opt",
  63. "Embed post merge, but before optimizations")),
  64. cl::desc("Embed LLVM bitcode in object files produced by LTO"));
  65. static cl::opt<bool> ThinLTOAssumeMerged(
  66. "thinlto-assume-merged", cl::init(false),
  67. cl::desc("Assume the input has already undergone ThinLTO function "
  68. "importing and the other pre-optimization pipeline changes."));
  69. namespace llvm {
  70. extern cl::opt<bool> NoPGOWarnMismatch;
  71. }
  72. [[noreturn]] static void reportOpenError(StringRef Path, Twine Msg) {
  73. errs() << "failed to open " << Path << ": " << Msg << '\n';
  74. errs().flush();
  75. exit(1);
  76. }
  77. Error Config::addSaveTemps(std::string OutputFileName, bool UseInputModulePath,
  78. const DenseSet<StringRef> &SaveTempsArgs) {
  79. ShouldDiscardValueNames = false;
  80. std::error_code EC;
  81. if (SaveTempsArgs.empty() || SaveTempsArgs.contains("resolution")) {
  82. ResolutionFile =
  83. std::make_unique<raw_fd_ostream>(OutputFileName + "resolution.txt", EC,
  84. sys::fs::OpenFlags::OF_TextWithCRLF);
  85. if (EC) {
  86. ResolutionFile.reset();
  87. return errorCodeToError(EC);
  88. }
  89. }
  90. auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
  91. // Keep track of the hook provided by the linker, which also needs to run.
  92. ModuleHookFn LinkerHook = Hook;
  93. Hook = [=](unsigned Task, const Module &M) {
  94. // If the linker's hook returned false, we need to pass that result
  95. // through.
  96. if (LinkerHook && !LinkerHook(Task, M))
  97. return false;
  98. std::string PathPrefix;
  99. // If this is the combined module (not a ThinLTO backend compile) or the
  100. // user hasn't requested using the input module's path, emit to a file
  101. // named from the provided OutputFileName with the Task ID appended.
  102. if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
  103. PathPrefix = OutputFileName;
  104. if (Task != (unsigned)-1)
  105. PathPrefix += utostr(Task) + ".";
  106. } else
  107. PathPrefix = M.getModuleIdentifier() + ".";
  108. std::string Path = PathPrefix + PathSuffix + ".bc";
  109. std::error_code EC;
  110. raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
  111. // Because -save-temps is a debugging feature, we report the error
  112. // directly and exit.
  113. if (EC)
  114. reportOpenError(Path, EC.message());
  115. WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false);
  116. return true;
  117. };
  118. };
  119. auto SaveCombinedIndex =
  120. [=](const ModuleSummaryIndex &Index,
  121. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
  122. std::string Path = OutputFileName + "index.bc";
  123. std::error_code EC;
  124. raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
  125. // Because -save-temps is a debugging feature, we report the error
  126. // directly and exit.
  127. if (EC)
  128. reportOpenError(Path, EC.message());
  129. writeIndexToFile(Index, OS);
  130. Path = OutputFileName + "index.dot";
  131. raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::OF_None);
  132. if (EC)
  133. reportOpenError(Path, EC.message());
  134. Index.exportToDot(OSDot, GUIDPreservedSymbols);
  135. return true;
  136. };
  137. if (SaveTempsArgs.empty()) {
  138. setHook("0.preopt", PreOptModuleHook);
  139. setHook("1.promote", PostPromoteModuleHook);
  140. setHook("2.internalize", PostInternalizeModuleHook);
  141. setHook("3.import", PostImportModuleHook);
  142. setHook("4.opt", PostOptModuleHook);
  143. setHook("5.precodegen", PreCodeGenModuleHook);
  144. CombinedIndexHook = SaveCombinedIndex;
  145. } else {
  146. if (SaveTempsArgs.contains("preopt"))
  147. setHook("0.preopt", PreOptModuleHook);
  148. if (SaveTempsArgs.contains("promote"))
  149. setHook("1.promote", PostPromoteModuleHook);
  150. if (SaveTempsArgs.contains("internalize"))
  151. setHook("2.internalize", PostInternalizeModuleHook);
  152. if (SaveTempsArgs.contains("import"))
  153. setHook("3.import", PostImportModuleHook);
  154. if (SaveTempsArgs.contains("opt"))
  155. setHook("4.opt", PostOptModuleHook);
  156. if (SaveTempsArgs.contains("precodegen"))
  157. setHook("5.precodegen", PreCodeGenModuleHook);
  158. if (SaveTempsArgs.contains("combinedindex"))
  159. CombinedIndexHook = SaveCombinedIndex;
  160. }
  161. return Error::success();
  162. }
  163. #define HANDLE_EXTENSION(Ext) \
  164. llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
  165. #include "llvm/Support/Extension.def"
  166. static void RegisterPassPlugins(ArrayRef<std::string> PassPlugins,
  167. PassBuilder &PB) {
  168. #define HANDLE_EXTENSION(Ext) \
  169. get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
  170. #include "llvm/Support/Extension.def"
  171. // Load requested pass plugins and let them register pass builder callbacks
  172. for (auto &PluginFN : PassPlugins) {
  173. auto PassPlugin = PassPlugin::Load(PluginFN);
  174. if (!PassPlugin) {
  175. errs() << "Failed to load passes from '" << PluginFN
  176. << "'. Request ignored.\n";
  177. continue;
  178. }
  179. PassPlugin->registerPassBuilderCallbacks(PB);
  180. }
  181. }
  182. static std::unique_ptr<TargetMachine>
  183. createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
  184. StringRef TheTriple = M.getTargetTriple();
  185. SubtargetFeatures Features;
  186. Features.getDefaultSubtargetFeatures(Triple(TheTriple));
  187. for (const std::string &A : Conf.MAttrs)
  188. Features.AddFeature(A);
  189. std::optional<Reloc::Model> RelocModel;
  190. if (Conf.RelocModel)
  191. RelocModel = *Conf.RelocModel;
  192. else if (M.getModuleFlag("PIC Level"))
  193. RelocModel =
  194. M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
  195. std::optional<CodeModel::Model> CodeModel;
  196. if (Conf.CodeModel)
  197. CodeModel = *Conf.CodeModel;
  198. else
  199. CodeModel = M.getCodeModel();
  200. std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
  201. TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
  202. CodeModel, Conf.CGOptLevel));
  203. assert(TM && "Failed to create target machine");
  204. return TM;
  205. }
  206. static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
  207. unsigned OptLevel, bool IsThinLTO,
  208. ModuleSummaryIndex *ExportSummary,
  209. const ModuleSummaryIndex *ImportSummary) {
  210. std::optional<PGOOptions> PGOOpt;
  211. if (!Conf.SampleProfile.empty())
  212. PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,
  213. PGOOptions::SampleUse, PGOOptions::NoCSAction, true);
  214. else if (Conf.RunCSIRInstr) {
  215. PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping,
  216. PGOOptions::IRUse, PGOOptions::CSIRInstr,
  217. Conf.AddFSDiscriminator);
  218. } else if (!Conf.CSIRProfile.empty()) {
  219. PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping,
  220. PGOOptions::IRUse, PGOOptions::CSIRUse,
  221. Conf.AddFSDiscriminator);
  222. NoPGOWarnMismatch = !Conf.PGOWarnMismatch;
  223. } else if (Conf.AddFSDiscriminator) {
  224. PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
  225. PGOOptions::NoCSAction, true);
  226. }
  227. TM->setPGOOption(PGOOpt);
  228. LoopAnalysisManager LAM;
  229. FunctionAnalysisManager FAM;
  230. CGSCCAnalysisManager CGAM;
  231. ModuleAnalysisManager MAM;
  232. PassInstrumentationCallbacks PIC;
  233. StandardInstrumentations SI(Mod.getContext(), Conf.DebugPassManager);
  234. SI.registerCallbacks(PIC, &FAM);
  235. PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
  236. RegisterPassPlugins(Conf.PassPlugins, PB);
  237. std::unique_ptr<TargetLibraryInfoImpl> TLII(
  238. new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())));
  239. if (Conf.Freestanding)
  240. TLII->disableAllFunctions();
  241. FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
  242. // Parse a custom AA pipeline if asked to.
  243. if (!Conf.AAPipeline.empty()) {
  244. AAManager AA;
  245. if (auto Err = PB.parseAAPipeline(AA, Conf.AAPipeline)) {
  246. report_fatal_error(Twine("unable to parse AA pipeline description '") +
  247. Conf.AAPipeline + "': " + toString(std::move(Err)));
  248. }
  249. // Register the AA manager first so that our version is the one used.
  250. FAM.registerPass([&] { return std::move(AA); });
  251. }
  252. // Register all the basic analyses with the managers.
  253. PB.registerModuleAnalyses(MAM);
  254. PB.registerCGSCCAnalyses(CGAM);
  255. PB.registerFunctionAnalyses(FAM);
  256. PB.registerLoopAnalyses(LAM);
  257. PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
  258. ModulePassManager MPM;
  259. if (!Conf.DisableVerify)
  260. MPM.addPass(VerifierPass());
  261. OptimizationLevel OL;
  262. switch (OptLevel) {
  263. default:
  264. llvm_unreachable("Invalid optimization level");
  265. case 0:
  266. OL = OptimizationLevel::O0;
  267. break;
  268. case 1:
  269. OL = OptimizationLevel::O1;
  270. break;
  271. case 2:
  272. OL = OptimizationLevel::O2;
  273. break;
  274. case 3:
  275. OL = OptimizationLevel::O3;
  276. break;
  277. }
  278. // Parse a custom pipeline if asked to.
  279. if (!Conf.OptPipeline.empty()) {
  280. if (auto Err = PB.parsePassPipeline(MPM, Conf.OptPipeline)) {
  281. report_fatal_error(Twine("unable to parse pass pipeline description '") +
  282. Conf.OptPipeline + "': " + toString(std::move(Err)));
  283. }
  284. } else if (Conf.UseDefaultPipeline) {
  285. MPM.addPass(PB.buildPerModuleDefaultPipeline(OL));
  286. } else if (IsThinLTO) {
  287. MPM.addPass(PB.buildThinLTODefaultPipeline(OL, ImportSummary));
  288. } else {
  289. MPM.addPass(PB.buildLTODefaultPipeline(OL, ExportSummary));
  290. }
  291. if (!Conf.DisableVerify)
  292. MPM.addPass(VerifierPass());
  293. MPM.run(Mod, MAM);
  294. }
  295. bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
  296. bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
  297. const ModuleSummaryIndex *ImportSummary,
  298. const std::vector<uint8_t> &CmdArgs) {
  299. if (EmbedBitcode == LTOBitcodeEmbedding::EmbedPostMergePreOptimized) {
  300. // FIXME: the motivation for capturing post-merge bitcode and command line
  301. // is replicating the compilation environment from bitcode, without needing
  302. // to understand the dependencies (the functions to be imported). This
  303. // assumes a clang - based invocation, case in which we have the command
  304. // line.
  305. // It's not very clear how the above motivation would map in the
  306. // linker-based case, so we currently don't plumb the command line args in
  307. // that case.
  308. if (CmdArgs.empty())
  309. LLVM_DEBUG(
  310. dbgs() << "Post-(Thin)LTO merge bitcode embedding was requested, but "
  311. "command line arguments are not available");
  312. llvm::embedBitcodeInModule(Mod, llvm::MemoryBufferRef(),
  313. /*EmbedBitcode*/ true, /*EmbedCmdline*/ true,
  314. /*Cmdline*/ CmdArgs);
  315. }
  316. // FIXME: Plumb the combined index into the new pass manager.
  317. runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
  318. ImportSummary);
  319. return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
  320. }
  321. static void codegen(const Config &Conf, TargetMachine *TM,
  322. AddStreamFn AddStream, unsigned Task, Module &Mod,
  323. const ModuleSummaryIndex &CombinedIndex) {
  324. if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
  325. return;
  326. if (EmbedBitcode == LTOBitcodeEmbedding::EmbedOptimized)
  327. llvm::embedBitcodeInModule(Mod, llvm::MemoryBufferRef(),
  328. /*EmbedBitcode*/ true,
  329. /*EmbedCmdline*/ false,
  330. /*CmdArgs*/ std::vector<uint8_t>());
  331. std::unique_ptr<ToolOutputFile> DwoOut;
  332. SmallString<1024> DwoFile(Conf.SplitDwarfOutput);
  333. if (!Conf.DwoDir.empty()) {
  334. std::error_code EC;
  335. if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
  336. report_fatal_error(Twine("Failed to create directory ") + Conf.DwoDir +
  337. ": " + EC.message());
  338. DwoFile = Conf.DwoDir;
  339. sys::path::append(DwoFile, std::to_string(Task) + ".dwo");
  340. TM->Options.MCOptions.SplitDwarfFile = std::string(DwoFile);
  341. } else
  342. TM->Options.MCOptions.SplitDwarfFile = Conf.SplitDwarfFile;
  343. if (!DwoFile.empty()) {
  344. std::error_code EC;
  345. DwoOut = std::make_unique<ToolOutputFile>(DwoFile, EC, sys::fs::OF_None);
  346. if (EC)
  347. report_fatal_error(Twine("Failed to open ") + DwoFile + ": " +
  348. EC.message());
  349. }
  350. Expected<std::unique_ptr<CachedFileStream>> StreamOrErr =
  351. AddStream(Task, Mod.getModuleIdentifier());
  352. if (Error Err = StreamOrErr.takeError())
  353. report_fatal_error(std::move(Err));
  354. std::unique_ptr<CachedFileStream> &Stream = *StreamOrErr;
  355. TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName;
  356. legacy::PassManager CodeGenPasses;
  357. TargetLibraryInfoImpl TLII(Triple(Mod.getTargetTriple()));
  358. CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
  359. CodeGenPasses.add(
  360. createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
  361. if (Conf.PreCodeGenPassesHook)
  362. Conf.PreCodeGenPassesHook(CodeGenPasses);
  363. if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
  364. DwoOut ? &DwoOut->os() : nullptr,
  365. Conf.CGFileType))
  366. report_fatal_error("Failed to setup codegen");
  367. CodeGenPasses.run(Mod);
  368. if (DwoOut)
  369. DwoOut->keep();
  370. }
  371. static void splitCodeGen(const Config &C, TargetMachine *TM,
  372. AddStreamFn AddStream,
  373. unsigned ParallelCodeGenParallelismLevel, Module &Mod,
  374. const ModuleSummaryIndex &CombinedIndex) {
  375. ThreadPool CodegenThreadPool(
  376. heavyweight_hardware_concurrency(ParallelCodeGenParallelismLevel));
  377. unsigned ThreadCount = 0;
  378. const Target *T = &TM->getTarget();
  379. SplitModule(
  380. Mod, ParallelCodeGenParallelismLevel,
  381. [&](std::unique_ptr<Module> MPart) {
  382. // We want to clone the module in a new context to multi-thread the
  383. // codegen. We do it by serializing partition modules to bitcode
  384. // (while still on the main thread, in order to avoid data races) and
  385. // spinning up new threads which deserialize the partitions into
  386. // separate contexts.
  387. // FIXME: Provide a more direct way to do this in LLVM.
  388. SmallString<0> BC;
  389. raw_svector_ostream BCOS(BC);
  390. WriteBitcodeToFile(*MPart, BCOS);
  391. // Enqueue the task
  392. CodegenThreadPool.async(
  393. [&](const SmallString<0> &BC, unsigned ThreadId) {
  394. LTOLLVMContext Ctx(C);
  395. Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
  396. MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
  397. Ctx);
  398. if (!MOrErr)
  399. report_fatal_error("Failed to read bitcode");
  400. std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
  401. std::unique_ptr<TargetMachine> TM =
  402. createTargetMachine(C, T, *MPartInCtx);
  403. codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx,
  404. CombinedIndex);
  405. },
  406. // Pass BC using std::move to ensure that it get moved rather than
  407. // copied into the thread's context.
  408. std::move(BC), ThreadCount++);
  409. },
  410. false);
  411. // Because the inner lambda (which runs in a worker thread) captures our local
  412. // variables, we need to wait for the worker threads to terminate before we
  413. // can leave the function scope.
  414. CodegenThreadPool.wait();
  415. }
  416. static Expected<const Target *> initAndLookupTarget(const Config &C,
  417. Module &Mod) {
  418. if (!C.OverrideTriple.empty())
  419. Mod.setTargetTriple(C.OverrideTriple);
  420. else if (Mod.getTargetTriple().empty())
  421. Mod.setTargetTriple(C.DefaultTriple);
  422. std::string Msg;
  423. const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
  424. if (!T)
  425. return make_error<StringError>(Msg, inconvertibleErrorCode());
  426. return T;
  427. }
  428. Error lto::finalizeOptimizationRemarks(
  429. std::unique_ptr<ToolOutputFile> DiagOutputFile) {
  430. // Make sure we flush the diagnostic remarks file in case the linker doesn't
  431. // call the global destructors before exiting.
  432. if (!DiagOutputFile)
  433. return Error::success();
  434. DiagOutputFile->keep();
  435. DiagOutputFile->os().flush();
  436. return Error::success();
  437. }
  438. Error lto::backend(const Config &C, AddStreamFn AddStream,
  439. unsigned ParallelCodeGenParallelismLevel, Module &Mod,
  440. ModuleSummaryIndex &CombinedIndex) {
  441. Expected<const Target *> TOrErr = initAndLookupTarget(C, Mod);
  442. if (!TOrErr)
  443. return TOrErr.takeError();
  444. std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, Mod);
  445. if (!C.CodeGenOnly) {
  446. if (!opt(C, TM.get(), 0, Mod, /*IsThinLTO=*/false,
  447. /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
  448. /*CmdArgs*/ std::vector<uint8_t>()))
  449. return Error::success();
  450. }
  451. if (ParallelCodeGenParallelismLevel == 1) {
  452. codegen(C, TM.get(), AddStream, 0, Mod, CombinedIndex);
  453. } else {
  454. splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel, Mod,
  455. CombinedIndex);
  456. }
  457. return Error::success();
  458. }
  459. static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
  460. const ModuleSummaryIndex &Index) {
  461. std::vector<GlobalValue*> DeadGVs;
  462. for (auto &GV : Mod.global_values())
  463. if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
  464. if (!Index.isGlobalValueLive(GVS)) {
  465. DeadGVs.push_back(&GV);
  466. convertToDeclaration(GV);
  467. }
  468. // Now that all dead bodies have been dropped, delete the actual objects
  469. // themselves when possible.
  470. for (GlobalValue *GV : DeadGVs) {
  471. GV->removeDeadConstantUsers();
  472. // Might reference something defined in native object (i.e. dropped a
  473. // non-prevailing IR def, but we need to keep the declaration).
  474. if (GV->use_empty())
  475. GV->eraseFromParent();
  476. }
  477. }
  478. Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
  479. Module &Mod, const ModuleSummaryIndex &CombinedIndex,
  480. const FunctionImporter::ImportMapTy &ImportList,
  481. const GVSummaryMapTy &DefinedGlobals,
  482. MapVector<StringRef, BitcodeModule> *ModuleMap,
  483. const std::vector<uint8_t> &CmdArgs) {
  484. Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
  485. if (!TOrErr)
  486. return TOrErr.takeError();
  487. std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
  488. // Setup optimization remarks.
  489. auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
  490. Mod.getContext(), Conf.RemarksFilename, Conf.RemarksPasses,
  491. Conf.RemarksFormat, Conf.RemarksWithHotness, Conf.RemarksHotnessThreshold,
  492. Task);
  493. if (!DiagFileOrErr)
  494. return DiagFileOrErr.takeError();
  495. auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
  496. // Set the partial sample profile ratio in the profile summary module flag of
  497. // the module, if applicable.
  498. Mod.setPartialSampleProfileRatio(CombinedIndex);
  499. updatePublicTypeTestCalls(Mod, CombinedIndex.withWholeProgramVisibility());
  500. if (Conf.CodeGenOnly) {
  501. codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex);
  502. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  503. }
  504. if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
  505. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  506. auto OptimizeAndCodegen =
  507. [&](Module &Mod, TargetMachine *TM,
  508. std::unique_ptr<ToolOutputFile> DiagnosticOutputFile) {
  509. if (!opt(Conf, TM, Task, Mod, /*IsThinLTO=*/true,
  510. /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex,
  511. CmdArgs))
  512. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  513. codegen(Conf, TM, AddStream, Task, Mod, CombinedIndex);
  514. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  515. };
  516. if (ThinLTOAssumeMerged)
  517. return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
  518. // When linking an ELF shared object, dso_local should be dropped. We
  519. // conservatively do this for -fpic.
  520. bool ClearDSOLocalOnDeclarations =
  521. TM->getTargetTriple().isOSBinFormatELF() &&
  522. TM->getRelocationModel() != Reloc::Static &&
  523. Mod.getPIELevel() == PIELevel::Default;
  524. renameModuleForThinLTO(Mod, CombinedIndex, ClearDSOLocalOnDeclarations);
  525. dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex);
  526. thinLTOFinalizeInModule(Mod, DefinedGlobals, /*PropagateAttrs=*/true);
  527. if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
  528. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  529. if (!DefinedGlobals.empty())
  530. thinLTOInternalizeModule(Mod, DefinedGlobals);
  531. if (Conf.PostInternalizeModuleHook &&
  532. !Conf.PostInternalizeModuleHook(Task, Mod))
  533. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  534. auto ModuleLoader = [&](StringRef Identifier) {
  535. assert(Mod.getContext().isODRUniquingDebugTypes() &&
  536. "ODR Type uniquing should be enabled on the context");
  537. if (ModuleMap) {
  538. auto I = ModuleMap->find(Identifier);
  539. assert(I != ModuleMap->end());
  540. return I->second.getLazyModule(Mod.getContext(),
  541. /*ShouldLazyLoadMetadata=*/true,
  542. /*IsImporting*/ true);
  543. }
  544. ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
  545. llvm::MemoryBuffer::getFile(Identifier);
  546. if (!MBOrErr)
  547. return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
  548. Twine("Error loading imported file ") + Identifier + " : ",
  549. MBOrErr.getError()));
  550. Expected<BitcodeModule> BMOrErr = findThinLTOModule(**MBOrErr);
  551. if (!BMOrErr)
  552. return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
  553. Twine("Error loading imported file ") + Identifier + " : " +
  554. toString(BMOrErr.takeError()),
  555. inconvertibleErrorCode()));
  556. Expected<std::unique_ptr<Module>> MOrErr =
  557. BMOrErr->getLazyModule(Mod.getContext(),
  558. /*ShouldLazyLoadMetadata=*/true,
  559. /*IsImporting*/ true);
  560. if (MOrErr)
  561. (*MOrErr)->setOwnedMemoryBuffer(std::move(*MBOrErr));
  562. return MOrErr;
  563. };
  564. FunctionImporter Importer(CombinedIndex, ModuleLoader,
  565. ClearDSOLocalOnDeclarations);
  566. if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
  567. return Err;
  568. if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
  569. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  570. return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
  571. }
  572. BitcodeModule *lto::findThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
  573. if (ThinLTOAssumeMerged && BMs.size() == 1)
  574. return BMs.begin();
  575. for (BitcodeModule &BM : BMs) {
  576. Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
  577. if (LTOInfo && LTOInfo->IsThinLTO)
  578. return &BM;
  579. }
  580. return nullptr;
  581. }
  582. Expected<BitcodeModule> lto::findThinLTOModule(MemoryBufferRef MBRef) {
  583. Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
  584. if (!BMsOrErr)
  585. return BMsOrErr.takeError();
  586. // The bitcode file may contain multiple modules, we want the one that is
  587. // marked as being the ThinLTO module.
  588. if (const BitcodeModule *Bm = lto::findThinLTOModule(*BMsOrErr))
  589. return *Bm;
  590. return make_error<StringError>("Could not find module summary",
  591. inconvertibleErrorCode());
  592. }
  593. bool lto::initImportList(const Module &M,
  594. const ModuleSummaryIndex &CombinedIndex,
  595. FunctionImporter::ImportMapTy &ImportList) {
  596. if (ThinLTOAssumeMerged)
  597. return true;
  598. // We can simply import the values mentioned in the combined index, since
  599. // we should only invoke this using the individual indexes written out
  600. // via a WriteIndexesThinBackend.
  601. for (const auto &GlobalList : CombinedIndex) {
  602. // Ignore entries for undefined references.
  603. if (GlobalList.second.SummaryList.empty())
  604. continue;
  605. auto GUID = GlobalList.first;
  606. for (const auto &Summary : GlobalList.second.SummaryList) {
  607. // Skip the summaries for the importing module. These are included to
  608. // e.g. record required linkage changes.
  609. if (Summary->modulePath() == M.getModuleIdentifier())
  610. continue;
  611. // Add an entry to provoke importing by thinBackend.
  612. ImportList[Summary->modulePath()].insert(GUID);
  613. }
  614. }
  615. return true;
  616. }