ThinLTOCodeGenerator.cpp 46 KB

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