ThinLTOCodeGenerator.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
  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 Thin Link Time Optimization library. This library is
  10. // intended to be used by linker to optimize code at link time.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include "llvm/ADT/ScopeExit.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/Analysis/AliasAnalysis.h"
  19. #include "llvm/Analysis/ModuleSummaryAnalysis.h"
  20. #include "llvm/Analysis/ProfileSummaryInfo.h"
  21. #include "llvm/Analysis/TargetLibraryInfo.h"
  22. #include "llvm/Analysis/TargetTransformInfo.h"
  23. #include "llvm/Bitcode/BitcodeReader.h"
  24. #include "llvm/Bitcode/BitcodeWriter.h"
  25. #include "llvm/Bitcode/BitcodeWriterPass.h"
  26. #include "llvm/Config/llvm-config.h"
  27. #include "llvm/IR/DebugInfo.h"
  28. #include "llvm/IR/DiagnosticPrinter.h"
  29. #include "llvm/IR/LLVMContext.h"
  30. #include "llvm/IR/LLVMRemarkStreamer.h"
  31. #include "llvm/IR/LegacyPassManager.h"
  32. #include "llvm/IR/Mangler.h"
  33. #include "llvm/IR/PassTimingInfo.h"
  34. #include "llvm/IR/Verifier.h"
  35. #include "llvm/IRReader/IRReader.h"
  36. #include "llvm/LTO/LTO.h"
  37. #include "llvm/LTO/SummaryBasedOptimizations.h"
  38. #include "llvm/MC/SubtargetFeature.h"
  39. #include "llvm/MC/TargetRegistry.h"
  40. #include "llvm/Object/IRObjectFile.h"
  41. #include "llvm/Passes/PassBuilder.h"
  42. #include "llvm/Passes/StandardInstrumentations.h"
  43. #include "llvm/Remarks/HotnessThresholdParser.h"
  44. #include "llvm/Support/CachePruning.h"
  45. #include "llvm/Support/Debug.h"
  46. #include "llvm/Support/Error.h"
  47. #include "llvm/Support/FileUtilities.h"
  48. #include "llvm/Support/Path.h"
  49. #include "llvm/Support/SHA1.h"
  50. #include "llvm/Support/SmallVectorMemoryBuffer.h"
  51. #include "llvm/Support/ThreadPool.h"
  52. #include "llvm/Support/Threading.h"
  53. #include "llvm/Support/ToolOutputFile.h"
  54. #include "llvm/Target/TargetMachine.h"
  55. #include "llvm/Transforms/IPO.h"
  56. #include "llvm/Transforms/IPO/FunctionAttrs.h"
  57. #include "llvm/Transforms/IPO/FunctionImport.h"
  58. #include "llvm/Transforms/IPO/Internalize.h"
  59. #include "llvm/Transforms/IPO/PassManagerBuilder.h"
  60. #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
  61. #include "llvm/Transforms/ObjCARC.h"
  62. #include "llvm/Transforms/Utils/FunctionImportUtils.h"
  63. #include <numeric>
  64. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  65. #include <unistd.h>
  66. #else
  67. #include <io.h>
  68. #endif
  69. using namespace llvm;
  70. #define DEBUG_TYPE "thinlto"
  71. namespace llvm {
  72. // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
  73. extern cl::opt<bool> LTODiscardValueNames;
  74. extern cl::opt<std::string> RemarksFilename;
  75. extern cl::opt<std::string> RemarksPasses;
  76. extern cl::opt<bool> RemarksWithHotness;
  77. extern cl::opt<Optional<uint64_t>, false, remarks::HotnessThresholdParser>
  78. RemarksHotnessThreshold;
  79. extern cl::opt<std::string> RemarksFormat;
  80. }
  81. namespace {
  82. // Default to using all available threads in the system, but using only one
  83. // thred per core, as indicated by the usage of
  84. // heavyweight_hardware_concurrency() below.
  85. static cl::opt<int> ThreadCount("threads", cl::init(0));
  86. // Simple helper to save temporary files for debug.
  87. static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
  88. unsigned count, StringRef Suffix) {
  89. if (TempDir.empty())
  90. return;
  91. // User asked to save temps, let dump the bitcode file after import.
  92. std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
  93. std::error_code EC;
  94. raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
  95. if (EC)
  96. report_fatal_error(Twine("Failed to open ") + SaveTempPath +
  97. " to save optimized bitcode\n");
  98. WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
  99. }
  100. static const GlobalValueSummary *
  101. getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
  102. // If there is any strong definition anywhere, get it.
  103. auto StrongDefForLinker = llvm::find_if(
  104. GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
  105. auto Linkage = Summary->linkage();
  106. return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
  107. !GlobalValue::isWeakForLinker(Linkage);
  108. });
  109. if (StrongDefForLinker != GVSummaryList.end())
  110. return StrongDefForLinker->get();
  111. // Get the first *linker visible* definition for this global in the summary
  112. // list.
  113. auto FirstDefForLinker = llvm::find_if(
  114. GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
  115. auto Linkage = Summary->linkage();
  116. return !GlobalValue::isAvailableExternallyLinkage(Linkage);
  117. });
  118. // Extern templates can be emitted as available_externally.
  119. if (FirstDefForLinker == GVSummaryList.end())
  120. return nullptr;
  121. return FirstDefForLinker->get();
  122. }
  123. // Populate map of GUID to the prevailing copy for any multiply defined
  124. // symbols. Currently assume first copy is prevailing, or any strong
  125. // definition. Can be refined with Linker information in the future.
  126. static void computePrevailingCopies(
  127. const ModuleSummaryIndex &Index,
  128. DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
  129. auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
  130. return GVSummaryList.size() > 1;
  131. };
  132. for (auto &I : Index) {
  133. if (HasMultipleCopies(I.second.SummaryList))
  134. PrevailingCopy[I.first] =
  135. getFirstDefinitionForLinker(I.second.SummaryList);
  136. }
  137. }
  138. static StringMap<lto::InputFile *>
  139. generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
  140. StringMap<lto::InputFile *> ModuleMap;
  141. for (auto &M : Modules) {
  142. assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
  143. "Expect unique Buffer Identifier");
  144. ModuleMap[M->getName()] = M.get();
  145. }
  146. return ModuleMap;
  147. }
  148. static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index,
  149. bool ClearDSOLocalOnDeclarations) {
  150. if (renameModuleForThinLTO(TheModule, Index, ClearDSOLocalOnDeclarations))
  151. report_fatal_error("renameModuleForThinLTO failed");
  152. }
  153. namespace {
  154. class ThinLTODiagnosticInfo : public DiagnosticInfo {
  155. const Twine &Msg;
  156. public:
  157. ThinLTODiagnosticInfo(const Twine &DiagMsg,
  158. DiagnosticSeverity Severity = DS_Error)
  159. : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
  160. void print(DiagnosticPrinter &DP) const override { DP << Msg; }
  161. };
  162. }
  163. /// Verify the module and strip broken debug info.
  164. static void verifyLoadedModule(Module &TheModule) {
  165. bool BrokenDebugInfo = false;
  166. if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
  167. report_fatal_error("Broken module found, compilation aborted!");
  168. if (BrokenDebugInfo) {
  169. TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
  170. "Invalid debug info found, debug info will be stripped", DS_Warning));
  171. StripDebugInfo(TheModule);
  172. }
  173. }
  174. static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
  175. LLVMContext &Context,
  176. bool Lazy,
  177. bool IsImporting) {
  178. auto &Mod = Input->getSingleBitcodeModule();
  179. SMDiagnostic Err;
  180. Expected<std::unique_ptr<Module>> ModuleOrErr =
  181. Lazy ? Mod.getLazyModule(Context,
  182. /* ShouldLazyLoadMetadata */ true, IsImporting)
  183. : Mod.parseModule(Context);
  184. if (!ModuleOrErr) {
  185. handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
  186. SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
  187. SourceMgr::DK_Error, EIB.message());
  188. Err.print("ThinLTO", errs());
  189. });
  190. report_fatal_error("Can't load module, abort.");
  191. }
  192. if (!Lazy)
  193. verifyLoadedModule(*ModuleOrErr.get());
  194. return std::move(*ModuleOrErr);
  195. }
  196. static void
  197. crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
  198. StringMap<lto::InputFile *> &ModuleMap,
  199. const FunctionImporter::ImportMapTy &ImportList,
  200. bool ClearDSOLocalOnDeclarations) {
  201. auto Loader = [&](StringRef Identifier) {
  202. auto &Input = ModuleMap[Identifier];
  203. return loadModuleFromInput(Input, TheModule.getContext(),
  204. /*Lazy=*/true, /*IsImporting*/ true);
  205. };
  206. FunctionImporter Importer(Index, Loader, ClearDSOLocalOnDeclarations);
  207. Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
  208. if (!Result) {
  209. handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
  210. SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
  211. SourceMgr::DK_Error, EIB.message());
  212. Err.print("ThinLTO", errs());
  213. });
  214. report_fatal_error("importFunctions failed");
  215. }
  216. // Verify again after cross-importing.
  217. verifyLoadedModule(TheModule);
  218. }
  219. static void optimizeModule(Module &TheModule, TargetMachine &TM,
  220. unsigned OptLevel, bool Freestanding,
  221. ModuleSummaryIndex *Index) {
  222. // Populate the PassManager
  223. PassManagerBuilder PMB;
  224. PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
  225. if (Freestanding)
  226. PMB.LibraryInfo->disableAllFunctions();
  227. PMB.Inliner = createFunctionInliningPass();
  228. // FIXME: should get it from the bitcode?
  229. PMB.OptLevel = OptLevel;
  230. PMB.LoopVectorize = true;
  231. PMB.SLPVectorize = true;
  232. // Already did this in verifyLoadedModule().
  233. PMB.VerifyInput = false;
  234. PMB.VerifyOutput = false;
  235. PMB.ImportSummary = Index;
  236. legacy::PassManager PM;
  237. // Add the TTI (required to inform the vectorizer about register size for
  238. // instance)
  239. PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
  240. // Add optimizations
  241. PMB.populateThinLTOPassManager(PM);
  242. PM.run(TheModule);
  243. }
  244. static void optimizeModuleNewPM(Module &TheModule, TargetMachine &TM,
  245. unsigned OptLevel, bool Freestanding,
  246. bool DebugPassManager,
  247. ModuleSummaryIndex *Index) {
  248. Optional<PGOOptions> PGOOpt;
  249. LoopAnalysisManager LAM;
  250. FunctionAnalysisManager FAM;
  251. CGSCCAnalysisManager CGAM;
  252. ModuleAnalysisManager MAM;
  253. PassInstrumentationCallbacks PIC;
  254. StandardInstrumentations SI(DebugPassManager);
  255. SI.registerCallbacks(PIC, &FAM);
  256. PipelineTuningOptions PTO;
  257. PTO.LoopVectorization = true;
  258. PTO.SLPVectorization = true;
  259. PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
  260. std::unique_ptr<TargetLibraryInfoImpl> TLII(
  261. new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())));
  262. if (Freestanding)
  263. TLII->disableAllFunctions();
  264. FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
  265. // Register all the basic analyses with the managers.
  266. PB.registerModuleAnalyses(MAM);
  267. PB.registerCGSCCAnalyses(CGAM);
  268. PB.registerFunctionAnalyses(FAM);
  269. PB.registerLoopAnalyses(LAM);
  270. PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
  271. ModulePassManager MPM;
  272. OptimizationLevel OL;
  273. switch (OptLevel) {
  274. default:
  275. llvm_unreachable("Invalid optimization level");
  276. case 0:
  277. OL = OptimizationLevel::O0;
  278. break;
  279. case 1:
  280. OL = OptimizationLevel::O1;
  281. break;
  282. case 2:
  283. OL = OptimizationLevel::O2;
  284. break;
  285. case 3:
  286. OL = OptimizationLevel::O3;
  287. break;
  288. }
  289. MPM.addPass(PB.buildThinLTODefaultPipeline(OL, Index));
  290. MPM.run(TheModule, MAM);
  291. }
  292. static void
  293. addUsedSymbolToPreservedGUID(const lto::InputFile &File,
  294. DenseSet<GlobalValue::GUID> &PreservedGUID) {
  295. for (const auto &Sym : File.symbols()) {
  296. if (Sym.isUsed())
  297. PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
  298. }
  299. }
  300. // Convert the PreservedSymbols map from "Name" based to "GUID" based.
  301. static void computeGUIDPreservedSymbols(const lto::InputFile &File,
  302. const StringSet<> &PreservedSymbols,
  303. const Triple &TheTriple,
  304. DenseSet<GlobalValue::GUID> &GUIDs) {
  305. // Iterate the symbols in the input file and if the input has preserved symbol
  306. // compute the GUID for the symbol.
  307. for (const auto &Sym : File.symbols()) {
  308. if (PreservedSymbols.count(Sym.getName()) && !Sym.getIRName().empty())
  309. GUIDs.insert(GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
  310. Sym.getIRName(), GlobalValue::ExternalLinkage, "")));
  311. }
  312. }
  313. static DenseSet<GlobalValue::GUID>
  314. computeGUIDPreservedSymbols(const lto::InputFile &File,
  315. const StringSet<> &PreservedSymbols,
  316. const Triple &TheTriple) {
  317. DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
  318. computeGUIDPreservedSymbols(File, PreservedSymbols, TheTriple,
  319. GUIDPreservedSymbols);
  320. return GUIDPreservedSymbols;
  321. }
  322. std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
  323. TargetMachine &TM) {
  324. SmallVector<char, 128> OutputBuffer;
  325. // CodeGen
  326. {
  327. raw_svector_ostream OS(OutputBuffer);
  328. legacy::PassManager PM;
  329. // If the bitcode files contain ARC code and were compiled with optimization,
  330. // the ObjCARCContractPass must be run, so do it unconditionally here.
  331. PM.add(createObjCARCContractPass());
  332. // Setup the codegen now.
  333. if (TM.addPassesToEmitFile(PM, OS, nullptr, CGFT_ObjectFile,
  334. /* DisableVerify */ true))
  335. report_fatal_error("Failed to setup codegen");
  336. // Run codegen now. resulting binary is in OutputBuffer.
  337. PM.run(TheModule);
  338. }
  339. return std::make_unique<SmallVectorMemoryBuffer>(
  340. std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
  341. }
  342. /// Manage caching for a single Module.
  343. class ModuleCacheEntry {
  344. SmallString<128> EntryPath;
  345. public:
  346. // Create a cache entry. This compute a unique hash for the Module considering
  347. // the current list of export/import, and offer an interface to query to
  348. // access the content in the cache.
  349. ModuleCacheEntry(
  350. StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
  351. const FunctionImporter::ImportMapTy &ImportList,
  352. const FunctionImporter::ExportSetTy &ExportList,
  353. const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
  354. const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
  355. bool Freestanding, const TargetMachineBuilder &TMBuilder) {
  356. if (CachePath.empty())
  357. return;
  358. if (!Index.modulePaths().count(ModuleID))
  359. // The module does not have an entry, it can't have a hash at all
  360. return;
  361. if (all_of(Index.getModuleHash(ModuleID),
  362. [](uint32_t V) { return V == 0; }))
  363. // No hash entry, no caching!
  364. return;
  365. llvm::lto::Config Conf;
  366. Conf.OptLevel = OptLevel;
  367. Conf.Options = TMBuilder.Options;
  368. Conf.CPU = TMBuilder.MCpu;
  369. Conf.MAttrs.push_back(TMBuilder.MAttr);
  370. Conf.RelocModel = TMBuilder.RelocModel;
  371. Conf.CGOptLevel = TMBuilder.CGOptLevel;
  372. Conf.Freestanding = Freestanding;
  373. SmallString<40> Key;
  374. computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
  375. ResolvedODR, DefinedGVSummaries);
  376. // This choice of file name allows the cache to be pruned (see pruneCache()
  377. // in include/llvm/Support/CachePruning.h).
  378. sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
  379. }
  380. // Access the path to this entry in the cache.
  381. StringRef getEntryPath() { return EntryPath; }
  382. // Try loading the buffer for this cache entry.
  383. ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
  384. if (EntryPath.empty())
  385. return std::error_code();
  386. SmallString<64> ResultPath;
  387. Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForRead(
  388. Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
  389. if (!FDOrErr)
  390. return errorToErrorCode(FDOrErr.takeError());
  391. ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getOpenFile(
  392. *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
  393. sys::fs::closeFile(*FDOrErr);
  394. return MBOrErr;
  395. }
  396. // Cache the Produced object file
  397. void write(const MemoryBuffer &OutputBuffer) {
  398. if (EntryPath.empty())
  399. return;
  400. // Write to a temporary to avoid race condition
  401. SmallString<128> TempFilename;
  402. SmallString<128> CachePath(EntryPath);
  403. llvm::sys::path::remove_filename(CachePath);
  404. sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
  405. if (auto Err = handleErrors(
  406. llvm::writeFileAtomically(TempFilename, EntryPath,
  407. OutputBuffer.getBuffer()),
  408. [](const llvm::AtomicFileWriteError &E) {
  409. std::string ErrorMsgBuffer;
  410. llvm::raw_string_ostream S(ErrorMsgBuffer);
  411. E.log(S);
  412. if (E.Error ==
  413. llvm::atomic_write_error::failed_to_create_uniq_file) {
  414. errs() << "Error: " << ErrorMsgBuffer << "\n";
  415. report_fatal_error("ThinLTO: Can't get a temporary file");
  416. }
  417. })) {
  418. // FIXME
  419. consumeError(std::move(Err));
  420. }
  421. }
  422. };
  423. static std::unique_ptr<MemoryBuffer>
  424. ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
  425. StringMap<lto::InputFile *> &ModuleMap, TargetMachine &TM,
  426. const FunctionImporter::ImportMapTy &ImportList,
  427. const FunctionImporter::ExportSetTy &ExportList,
  428. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
  429. const GVSummaryMapTy &DefinedGlobals,
  430. const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
  431. bool DisableCodeGen, StringRef SaveTempsDir,
  432. bool Freestanding, unsigned OptLevel, unsigned count,
  433. bool UseNewPM, bool DebugPassManager) {
  434. // "Benchmark"-like optimization: single-source case
  435. bool SingleModule = (ModuleMap.size() == 1);
  436. // When linking an ELF shared object, dso_local should be dropped. We
  437. // conservatively do this for -fpic.
  438. bool ClearDSOLocalOnDeclarations =
  439. TM.getTargetTriple().isOSBinFormatELF() &&
  440. TM.getRelocationModel() != Reloc::Static &&
  441. TheModule.getPIELevel() == PIELevel::Default;
  442. if (!SingleModule) {
  443. promoteModule(TheModule, Index, ClearDSOLocalOnDeclarations);
  444. // Apply summary-based prevailing-symbol resolution decisions.
  445. thinLTOFinalizeInModule(TheModule, DefinedGlobals, /*PropagateAttrs=*/true);
  446. // Save temps: after promotion.
  447. saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
  448. }
  449. // Be friendly and don't nuke totally the module when the client didn't
  450. // supply anything to preserve.
  451. if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
  452. // Apply summary-based internalization decisions.
  453. thinLTOInternalizeModule(TheModule, DefinedGlobals);
  454. }
  455. // Save internalized bitcode
  456. saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
  457. if (!SingleModule) {
  458. crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
  459. ClearDSOLocalOnDeclarations);
  460. // Save temps: after cross-module import.
  461. saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
  462. }
  463. if (UseNewPM)
  464. optimizeModuleNewPM(TheModule, TM, OptLevel, Freestanding, DebugPassManager,
  465. &Index);
  466. else
  467. optimizeModule(TheModule, TM, OptLevel, Freestanding, &Index);
  468. saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
  469. if (DisableCodeGen) {
  470. // Configured to stop before CodeGen, serialize the bitcode and return.
  471. SmallVector<char, 128> OutputBuffer;
  472. {
  473. raw_svector_ostream OS(OutputBuffer);
  474. ProfileSummaryInfo PSI(TheModule);
  475. auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
  476. WriteBitcodeToFile(TheModule, OS, true, &Index);
  477. }
  478. return std::make_unique<SmallVectorMemoryBuffer>(
  479. std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
  480. }
  481. return codegenModule(TheModule, TM);
  482. }
  483. /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
  484. /// for caching, and in the \p Index for application during the ThinLTO
  485. /// backends. This is needed for correctness for exported symbols (ensure
  486. /// at least one copy kept) and a compile-time optimization (to drop duplicate
  487. /// copies when possible).
  488. static void resolvePrevailingInIndex(
  489. ModuleSummaryIndex &Index,
  490. StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
  491. &ResolvedODR,
  492. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
  493. const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
  494. &PrevailingCopy) {
  495. auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
  496. const auto &Prevailing = PrevailingCopy.find(GUID);
  497. // Not in map means that there was only one copy, which must be prevailing.
  498. if (Prevailing == PrevailingCopy.end())
  499. return true;
  500. return Prevailing->second == S;
  501. };
  502. auto recordNewLinkage = [&](StringRef ModuleIdentifier,
  503. GlobalValue::GUID GUID,
  504. GlobalValue::LinkageTypes NewLinkage) {
  505. ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
  506. };
  507. // TODO Conf.VisibilityScheme can be lto::Config::ELF for ELF.
  508. lto::Config Conf;
  509. thinLTOResolvePrevailingInIndex(Conf, Index, isPrevailing, recordNewLinkage,
  510. GUIDPreservedSymbols);
  511. }
  512. // Initialize the TargetMachine builder for a given Triple
  513. static void initTMBuilder(TargetMachineBuilder &TMBuilder,
  514. const Triple &TheTriple) {
  515. // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
  516. // FIXME this looks pretty terrible...
  517. if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
  518. if (TheTriple.getArch() == llvm::Triple::x86_64)
  519. TMBuilder.MCpu = "core2";
  520. else if (TheTriple.getArch() == llvm::Triple::x86)
  521. TMBuilder.MCpu = "yonah";
  522. else if (TheTriple.getArch() == llvm::Triple::aarch64 ||
  523. TheTriple.getArch() == llvm::Triple::aarch64_32)
  524. TMBuilder.MCpu = "cyclone";
  525. }
  526. TMBuilder.TheTriple = std::move(TheTriple);
  527. }
  528. } // end anonymous namespace
  529. void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
  530. MemoryBufferRef Buffer(Data, Identifier);
  531. auto InputOrError = lto::InputFile::create(Buffer);
  532. if (!InputOrError)
  533. report_fatal_error(Twine("ThinLTO cannot create input file: ") +
  534. toString(InputOrError.takeError()));
  535. auto TripleStr = (*InputOrError)->getTargetTriple();
  536. Triple TheTriple(TripleStr);
  537. if (Modules.empty())
  538. initTMBuilder(TMBuilder, Triple(TheTriple));
  539. else if (TMBuilder.TheTriple != TheTriple) {
  540. if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
  541. report_fatal_error("ThinLTO modules with incompatible triples not "
  542. "supported");
  543. initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
  544. }
  545. Modules.emplace_back(std::move(*InputOrError));
  546. }
  547. void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
  548. PreservedSymbols.insert(Name);
  549. }
  550. void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
  551. // FIXME: At the moment, we don't take advantage of this extra information,
  552. // we're conservatively considering cross-references as preserved.
  553. // CrossReferencedSymbols.insert(Name);
  554. PreservedSymbols.insert(Name);
  555. }
  556. // TargetMachine factory
  557. std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
  558. std::string ErrMsg;
  559. const Target *TheTarget =
  560. TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
  561. if (!TheTarget) {
  562. report_fatal_error(Twine("Can't load target for this Triple: ") + ErrMsg);
  563. }
  564. // Use MAttr as the default set of features.
  565. SubtargetFeatures Features(MAttr);
  566. Features.getDefaultSubtargetFeatures(TheTriple);
  567. std::string FeatureStr = Features.getString();
  568. std::unique_ptr<TargetMachine> TM(
  569. TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
  570. RelocModel, None, CGOptLevel));
  571. assert(TM && "Cannot create target machine");
  572. return TM;
  573. }
  574. /**
  575. * Produce the combined summary index from all the bitcode files:
  576. * "thin-link".
  577. */
  578. std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
  579. std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
  580. std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
  581. uint64_t NextModuleId = 0;
  582. for (auto &Mod : Modules) {
  583. auto &M = Mod->getSingleBitcodeModule();
  584. if (Error Err =
  585. M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
  586. // FIXME diagnose
  587. logAllUnhandledErrors(
  588. std::move(Err), errs(),
  589. "error: can't create module summary index for buffer: ");
  590. return nullptr;
  591. }
  592. }
  593. return CombinedIndex;
  594. }
  595. namespace {
  596. struct IsExported {
  597. const StringMap<FunctionImporter::ExportSetTy> &ExportLists;
  598. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols;
  599. IsExported(const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
  600. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
  601. : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
  602. bool operator()(StringRef ModuleIdentifier, ValueInfo VI) const {
  603. const auto &ExportList = ExportLists.find(ModuleIdentifier);
  604. return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
  605. GUIDPreservedSymbols.count(VI.getGUID());
  606. }
  607. };
  608. struct IsPrevailing {
  609. const DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy;
  610. IsPrevailing(const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
  611. &PrevailingCopy)
  612. : PrevailingCopy(PrevailingCopy) {}
  613. bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
  614. const auto &Prevailing = PrevailingCopy.find(GUID);
  615. // Not in map means that there was only one copy, which must be prevailing.
  616. if (Prevailing == PrevailingCopy.end())
  617. return true;
  618. return Prevailing->second == S;
  619. };
  620. };
  621. } // namespace
  622. static void computeDeadSymbolsInIndex(
  623. ModuleSummaryIndex &Index,
  624. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
  625. // We have no symbols resolution available. And can't do any better now in the
  626. // case where the prevailing symbol is in a native object. It can be refined
  627. // with linker information in the future.
  628. auto isPrevailing = [&](GlobalValue::GUID G) {
  629. return PrevailingType::Unknown;
  630. };
  631. computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
  632. /* ImportEnabled = */ true);
  633. }
  634. /**
  635. * Perform promotion and renaming of exported internal functions.
  636. * Index is updated to reflect linkage changes from weak resolution.
  637. */
  638. void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index,
  639. const lto::InputFile &File) {
  640. auto ModuleCount = Index.modulePaths().size();
  641. auto ModuleIdentifier = TheModule.getModuleIdentifier();
  642. // Collect for each module the list of function it defines (GUID -> Summary).
  643. StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
  644. Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
  645. // Convert the preserved symbols set from string to GUID
  646. auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
  647. File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
  648. // Add used symbol to the preserved symbols.
  649. addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
  650. // Compute "dead" symbols, we don't want to import/export these!
  651. computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
  652. // Generate import/export list
  653. StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
  654. StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
  655. ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
  656. ExportLists);
  657. DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
  658. computePrevailingCopies(Index, PrevailingCopy);
  659. // Resolve prevailing symbols
  660. StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
  661. resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
  662. PrevailingCopy);
  663. thinLTOFinalizeInModule(TheModule,
  664. ModuleToDefinedGVSummaries[ModuleIdentifier],
  665. /*PropagateAttrs=*/false);
  666. // Promote the exported values in the index, so that they are promoted
  667. // in the module.
  668. thinLTOInternalizeAndPromoteInIndex(
  669. Index, IsExported(ExportLists, GUIDPreservedSymbols),
  670. IsPrevailing(PrevailingCopy));
  671. // FIXME Set ClearDSOLocalOnDeclarations.
  672. promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
  673. }
  674. /**
  675. * Perform cross-module importing for the module identified by ModuleIdentifier.
  676. */
  677. void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
  678. ModuleSummaryIndex &Index,
  679. const lto::InputFile &File) {
  680. auto ModuleMap = generateModuleMap(Modules);
  681. auto ModuleCount = Index.modulePaths().size();
  682. // Collect for each module the list of function it defines (GUID -> Summary).
  683. StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
  684. Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
  685. // Convert the preserved symbols set from string to GUID
  686. auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
  687. File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
  688. addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
  689. // Compute "dead" symbols, we don't want to import/export these!
  690. computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
  691. // Generate import/export list
  692. StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
  693. StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
  694. ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
  695. ExportLists);
  696. auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
  697. // FIXME Set ClearDSOLocalOnDeclarations.
  698. crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
  699. /*ClearDSOLocalOnDeclarations=*/false);
  700. }
  701. /**
  702. * Compute the list of summaries needed for importing into module.
  703. */
  704. void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
  705. Module &TheModule, ModuleSummaryIndex &Index,
  706. std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
  707. const lto::InputFile &File) {
  708. auto ModuleCount = Index.modulePaths().size();
  709. auto ModuleIdentifier = TheModule.getModuleIdentifier();
  710. // Collect for each module the list of function it defines (GUID -> Summary).
  711. StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
  712. Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
  713. // Convert the preserved symbols set from string to GUID
  714. auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
  715. File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
  716. addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
  717. // Compute "dead" symbols, we don't want to import/export these!
  718. computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
  719. // Generate import/export list
  720. StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
  721. StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
  722. ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
  723. ExportLists);
  724. llvm::gatherImportedSummariesForModule(
  725. ModuleIdentifier, ModuleToDefinedGVSummaries,
  726. ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
  727. }
  728. /**
  729. * Emit the list of files needed for importing into module.
  730. */
  731. void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
  732. ModuleSummaryIndex &Index,
  733. const lto::InputFile &File) {
  734. auto ModuleCount = Index.modulePaths().size();
  735. auto ModuleIdentifier = TheModule.getModuleIdentifier();
  736. // Collect for each module the list of function it defines (GUID -> Summary).
  737. StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
  738. Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
  739. // Convert the preserved symbols set from string to GUID
  740. auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
  741. File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
  742. addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
  743. // Compute "dead" symbols, we don't want to import/export these!
  744. computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
  745. // Generate import/export list
  746. StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
  747. StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
  748. ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
  749. ExportLists);
  750. std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
  751. llvm::gatherImportedSummariesForModule(
  752. ModuleIdentifier, ModuleToDefinedGVSummaries,
  753. ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
  754. std::error_code EC;
  755. if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
  756. ModuleToSummariesForIndex)))
  757. report_fatal_error(Twine("Failed to open ") + OutputName +
  758. " to save imports lists\n");
  759. }
  760. /**
  761. * Perform internalization. Runs promote and internalization together.
  762. * Index is updated to reflect linkage changes.
  763. */
  764. void ThinLTOCodeGenerator::internalize(Module &TheModule,
  765. ModuleSummaryIndex &Index,
  766. const lto::InputFile &File) {
  767. initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
  768. auto ModuleCount = Index.modulePaths().size();
  769. auto ModuleIdentifier = TheModule.getModuleIdentifier();
  770. // Convert the preserved symbols set from string to GUID
  771. auto GUIDPreservedSymbols =
  772. computeGUIDPreservedSymbols(File, PreservedSymbols, TMBuilder.TheTriple);
  773. addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
  774. // Collect for each module the list of function it defines (GUID -> Summary).
  775. StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
  776. Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
  777. // Compute "dead" symbols, we don't want to import/export these!
  778. computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
  779. // Generate import/export list
  780. StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
  781. StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
  782. ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
  783. ExportLists);
  784. auto &ExportList = ExportLists[ModuleIdentifier];
  785. // Be friendly and don't nuke totally the module when the client didn't
  786. // supply anything to preserve.
  787. if (ExportList.empty() && GUIDPreservedSymbols.empty())
  788. return;
  789. DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
  790. computePrevailingCopies(Index, PrevailingCopy);
  791. // Resolve prevailing symbols
  792. StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
  793. resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
  794. PrevailingCopy);
  795. // Promote the exported values in the index, so that they are promoted
  796. // in the module.
  797. thinLTOInternalizeAndPromoteInIndex(
  798. Index, IsExported(ExportLists, GUIDPreservedSymbols),
  799. IsPrevailing(PrevailingCopy));
  800. // FIXME Set ClearDSOLocalOnDeclarations.
  801. promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
  802. // Internalization
  803. thinLTOFinalizeInModule(TheModule,
  804. ModuleToDefinedGVSummaries[ModuleIdentifier],
  805. /*PropagateAttrs=*/false);
  806. thinLTOInternalizeModule(TheModule,
  807. ModuleToDefinedGVSummaries[ModuleIdentifier]);
  808. }
  809. /**
  810. * Perform post-importing ThinLTO optimizations.
  811. */
  812. void ThinLTOCodeGenerator::optimize(Module &TheModule) {
  813. initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
  814. // Optimize now
  815. optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
  816. nullptr);
  817. }
  818. /// Write out the generated object file, either from CacheEntryPath or from
  819. /// OutputBuffer, preferring hard-link when possible.
  820. /// Returns the path to the generated file in SavedObjectsDirectoryPath.
  821. std::string
  822. ThinLTOCodeGenerator::writeGeneratedObject(int count, StringRef CacheEntryPath,
  823. const MemoryBuffer &OutputBuffer) {
  824. auto ArchName = TMBuilder.TheTriple.getArchName();
  825. SmallString<128> OutputPath(SavedObjectsDirectoryPath);
  826. llvm::sys::path::append(OutputPath,
  827. Twine(count) + "." + ArchName + ".thinlto.o");
  828. OutputPath.c_str(); // Ensure the string is null terminated.
  829. if (sys::fs::exists(OutputPath))
  830. sys::fs::remove(OutputPath);
  831. // We don't return a memory buffer to the linker, just a list of files.
  832. if (!CacheEntryPath.empty()) {
  833. // Cache is enabled, hard-link the entry (or copy if hard-link fails).
  834. auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
  835. if (!Err)
  836. return std::string(OutputPath.str());
  837. // Hard linking failed, try to copy.
  838. Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
  839. if (!Err)
  840. return std::string(OutputPath.str());
  841. // Copy failed (could be because the CacheEntry was removed from the cache
  842. // in the meantime by another process), fall back and try to write down the
  843. // buffer to the output.
  844. errs() << "remark: can't link or copy from cached entry '" << CacheEntryPath
  845. << "' to '" << OutputPath << "'\n";
  846. }
  847. // No cache entry, just write out the buffer.
  848. std::error_code Err;
  849. raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
  850. if (Err)
  851. report_fatal_error(Twine("Can't open output '") + OutputPath + "'\n");
  852. OS << OutputBuffer.getBuffer();
  853. return std::string(OutputPath.str());
  854. }
  855. // Main entry point for the ThinLTO processing
  856. void ThinLTOCodeGenerator::run() {
  857. timeTraceProfilerBegin("ThinLink", StringRef(""));
  858. auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
  859. if (llvm::timeTraceProfilerEnabled())
  860. llvm::timeTraceProfilerEnd();
  861. });
  862. // Prepare the resulting object vector
  863. assert(ProducedBinaries.empty() && "The generator should not be reused");
  864. if (SavedObjectsDirectoryPath.empty())
  865. ProducedBinaries.resize(Modules.size());
  866. else {
  867. sys::fs::create_directories(SavedObjectsDirectoryPath);
  868. bool IsDir;
  869. sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
  870. if (!IsDir)
  871. report_fatal_error(Twine("Unexistent dir: '") + SavedObjectsDirectoryPath + "'");
  872. ProducedBinaryFiles.resize(Modules.size());
  873. }
  874. if (CodeGenOnly) {
  875. // Perform only parallel codegen and return.
  876. ThreadPool Pool;
  877. int count = 0;
  878. for (auto &Mod : Modules) {
  879. Pool.async([&](int count) {
  880. LLVMContext Context;
  881. Context.setDiscardValueNames(LTODiscardValueNames);
  882. // Parse module now
  883. auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
  884. /*IsImporting*/ false);
  885. // CodeGen
  886. auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
  887. if (SavedObjectsDirectoryPath.empty())
  888. ProducedBinaries[count] = std::move(OutputBuffer);
  889. else
  890. ProducedBinaryFiles[count] =
  891. writeGeneratedObject(count, "", *OutputBuffer);
  892. }, count++);
  893. }
  894. return;
  895. }
  896. // Sequential linking phase
  897. auto Index = linkCombinedIndex();
  898. // Save temps: index.
  899. if (!SaveTempsDir.empty()) {
  900. auto SaveTempPath = SaveTempsDir + "index.bc";
  901. std::error_code EC;
  902. raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
  903. if (EC)
  904. report_fatal_error(Twine("Failed to open ") + SaveTempPath +
  905. " to save optimized bitcode\n");
  906. writeIndexToFile(*Index, OS);
  907. }
  908. // Prepare the module map.
  909. auto ModuleMap = generateModuleMap(Modules);
  910. auto ModuleCount = Modules.size();
  911. // Collect for each module the list of function it defines (GUID -> Summary).
  912. StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
  913. Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
  914. // Convert the preserved symbols set from string to GUID, this is needed for
  915. // computing the caching hash and the internalization.
  916. DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
  917. for (const auto &M : Modules)
  918. computeGUIDPreservedSymbols(*M, PreservedSymbols, TMBuilder.TheTriple,
  919. GUIDPreservedSymbols);
  920. // Add used symbol from inputs to the preserved symbols.
  921. for (const auto &M : Modules)
  922. addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
  923. // Compute "dead" symbols, we don't want to import/export these!
  924. computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
  925. // Synthesize entry counts for functions in the combined index.
  926. computeSyntheticCounts(*Index);
  927. // Currently there is no support for enabling whole program visibility via a
  928. // linker option in the old LTO API, but this call allows it to be specified
  929. // via the internal option. Must be done before WPD below.
  930. updateVCallVisibilityInIndex(*Index,
  931. /* WholeProgramVisibilityEnabledInLTO */ false,
  932. // FIXME: This needs linker information via a
  933. // TBD new interface.
  934. /* DynamicExportSymbols */ {});
  935. // Perform index-based WPD. This will return immediately if there are
  936. // no index entries in the typeIdMetadata map (e.g. if we are instead
  937. // performing IR-based WPD in hybrid regular/thin LTO mode).
  938. std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
  939. std::set<GlobalValue::GUID> ExportedGUIDs;
  940. runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
  941. for (auto GUID : ExportedGUIDs)
  942. GUIDPreservedSymbols.insert(GUID);
  943. // Collect the import/export lists for all modules from the call-graph in the
  944. // combined index.
  945. StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
  946. StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
  947. ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
  948. ExportLists);
  949. // We use a std::map here to be able to have a defined ordering when
  950. // producing a hash for the cache entry.
  951. // FIXME: we should be able to compute the caching hash for the entry based
  952. // on the index, and nuke this map.
  953. StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
  954. DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
  955. computePrevailingCopies(*Index, PrevailingCopy);
  956. // Resolve prevailing symbols, this has to be computed early because it
  957. // impacts the caching.
  958. resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
  959. PrevailingCopy);
  960. // Use global summary-based analysis to identify symbols that can be
  961. // internalized (because they aren't exported or preserved as per callback).
  962. // Changes are made in the index, consumed in the ThinLTO backends.
  963. updateIndexWPDForExports(*Index,
  964. IsExported(ExportLists, GUIDPreservedSymbols),
  965. LocalWPDTargetsMap);
  966. thinLTOInternalizeAndPromoteInIndex(
  967. *Index, IsExported(ExportLists, GUIDPreservedSymbols),
  968. IsPrevailing(PrevailingCopy));
  969. thinLTOPropagateFunctionAttrs(*Index, IsPrevailing(PrevailingCopy));
  970. // Make sure that every module has an entry in the ExportLists, ImportList,
  971. // GVSummary and ResolvedODR maps to enable threaded access to these maps
  972. // below.
  973. for (auto &Module : Modules) {
  974. auto ModuleIdentifier = Module->getName();
  975. ExportLists[ModuleIdentifier];
  976. ImportLists[ModuleIdentifier];
  977. ResolvedODR[ModuleIdentifier];
  978. ModuleToDefinedGVSummaries[ModuleIdentifier];
  979. }
  980. std::vector<BitcodeModule *> ModulesVec;
  981. ModulesVec.reserve(Modules.size());
  982. for (auto &Mod : Modules)
  983. ModulesVec.push_back(&Mod->getSingleBitcodeModule());
  984. std::vector<int> ModulesOrdering = lto::generateModulesOrdering(ModulesVec);
  985. if (llvm::timeTraceProfilerEnabled())
  986. llvm::timeTraceProfilerEnd();
  987. TimeTraceScopeExit.release();
  988. // Parallel optimizer + codegen
  989. {
  990. ThreadPool Pool(heavyweight_hardware_concurrency(ThreadCount));
  991. for (auto IndexCount : ModulesOrdering) {
  992. auto &Mod = Modules[IndexCount];
  993. Pool.async([&](int count) {
  994. auto ModuleIdentifier = Mod->getName();
  995. auto &ExportList = ExportLists[ModuleIdentifier];
  996. auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
  997. // The module may be cached, this helps handling it.
  998. ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
  999. ImportLists[ModuleIdentifier], ExportList,
  1000. ResolvedODR[ModuleIdentifier],
  1001. DefinedGVSummaries, OptLevel, Freestanding,
  1002. TMBuilder);
  1003. auto CacheEntryPath = CacheEntry.getEntryPath();
  1004. {
  1005. auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
  1006. LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
  1007. << " '" << CacheEntryPath << "' for buffer "
  1008. << count << " " << ModuleIdentifier << "\n");
  1009. if (ErrOrBuffer) {
  1010. // Cache Hit!
  1011. if (SavedObjectsDirectoryPath.empty())
  1012. ProducedBinaries[count] = std::move(ErrOrBuffer.get());
  1013. else
  1014. ProducedBinaryFiles[count] = writeGeneratedObject(
  1015. count, CacheEntryPath, *ErrOrBuffer.get());
  1016. return;
  1017. }
  1018. }
  1019. LLVMContext Context;
  1020. Context.setDiscardValueNames(LTODiscardValueNames);
  1021. Context.enableDebugTypeODRUniquing();
  1022. auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
  1023. Context, RemarksFilename, RemarksPasses, RemarksFormat,
  1024. RemarksWithHotness, RemarksHotnessThreshold, count);
  1025. if (!DiagFileOrErr) {
  1026. errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
  1027. report_fatal_error("ThinLTO: Can't get an output file for the "
  1028. "remarks");
  1029. }
  1030. // Parse module now
  1031. auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
  1032. /*IsImporting*/ false);
  1033. // Save temps: original file.
  1034. saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
  1035. auto &ImportList = ImportLists[ModuleIdentifier];
  1036. // Run the main process now, and generates a binary
  1037. auto OutputBuffer = ProcessThinLTOModule(
  1038. *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
  1039. ExportList, GUIDPreservedSymbols,
  1040. ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
  1041. DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
  1042. UseNewPM, DebugPassManager);
  1043. // Commit to the cache (if enabled)
  1044. CacheEntry.write(*OutputBuffer);
  1045. if (SavedObjectsDirectoryPath.empty()) {
  1046. // We need to generated a memory buffer for the linker.
  1047. if (!CacheEntryPath.empty()) {
  1048. // When cache is enabled, reload from the cache if possible.
  1049. // Releasing the buffer from the heap and reloading it from the
  1050. // cache file with mmap helps us to lower memory pressure.
  1051. // The freed memory can be used for the next input file.
  1052. // The final binary link will read from the VFS cache (hopefully!)
  1053. // or from disk (if the memory pressure was too high).
  1054. auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
  1055. if (auto EC = ReloadedBufferOrErr.getError()) {
  1056. // On error, keep the preexisting buffer and print a diagnostic.
  1057. errs() << "remark: can't reload cached file '" << CacheEntryPath
  1058. << "': " << EC.message() << "\n";
  1059. } else {
  1060. OutputBuffer = std::move(*ReloadedBufferOrErr);
  1061. }
  1062. }
  1063. ProducedBinaries[count] = std::move(OutputBuffer);
  1064. return;
  1065. }
  1066. ProducedBinaryFiles[count] = writeGeneratedObject(
  1067. count, CacheEntryPath, *OutputBuffer);
  1068. }, IndexCount);
  1069. }
  1070. }
  1071. pruneCache(CacheOptions.Path, CacheOptions.Policy);
  1072. // If statistics were requested, print them out now.
  1073. if (llvm::AreStatisticsEnabled())
  1074. llvm::PrintStatistics();
  1075. reportAndResetTimings();
  1076. }