ThinLTOCodeGenerator.cpp 48 KB

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