LTO.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. //===-LTO.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 functions and classes used to support LTO.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/LTO/LTO.h"
  13. #include "llvm/ADT/ScopeExit.h"
  14. #include "llvm/ADT/SmallSet.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  18. #include "llvm/Analysis/StackSafetyAnalysis.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/CodeGen/Analysis.h"
  24. #include "llvm/Config/llvm-config.h"
  25. #include "llvm/IR/AutoUpgrade.h"
  26. #include "llvm/IR/DiagnosticPrinter.h"
  27. #include "llvm/IR/Intrinsics.h"
  28. #include "llvm/IR/LLVMRemarkStreamer.h"
  29. #include "llvm/IR/LegacyPassManager.h"
  30. #include "llvm/IR/Mangler.h"
  31. #include "llvm/IR/Metadata.h"
  32. #include "llvm/LTO/LTOBackend.h"
  33. #include "llvm/LTO/SummaryBasedOptimizations.h"
  34. #include "llvm/Linker/IRMover.h"
  35. #include "llvm/MC/TargetRegistry.h"
  36. #include "llvm/Object/IRObjectFile.h"
  37. #include "llvm/Support/CommandLine.h"
  38. #include "llvm/Support/Error.h"
  39. #include "llvm/Support/FileSystem.h"
  40. #include "llvm/Support/ManagedStatic.h"
  41. #include "llvm/Support/MemoryBuffer.h"
  42. #include "llvm/Support/Path.h"
  43. #include "llvm/Support/SHA1.h"
  44. #include "llvm/Support/SourceMgr.h"
  45. #include "llvm/Support/ThreadPool.h"
  46. #include "llvm/Support/Threading.h"
  47. #include "llvm/Support/TimeProfiler.h"
  48. #include "llvm/Support/ToolOutputFile.h"
  49. #include "llvm/Support/VCSRevision.h"
  50. #include "llvm/Support/raw_ostream.h"
  51. #include "llvm/Target/TargetOptions.h"
  52. #include "llvm/Transforms/IPO.h"
  53. #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
  54. #include "llvm/Transforms/Utils/FunctionImportUtils.h"
  55. #include "llvm/Transforms/Utils/SplitModule.h"
  56. #include <optional>
  57. #include <set>
  58. using namespace llvm;
  59. using namespace lto;
  60. using namespace object;
  61. #define DEBUG_TYPE "lto"
  62. static cl::opt<bool>
  63. DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
  64. cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
  65. namespace llvm {
  66. /// Enable global value internalization in LTO.
  67. cl::opt<bool> EnableLTOInternalization(
  68. "enable-lto-internalization", cl::init(true), cl::Hidden,
  69. cl::desc("Enable global value internalization in LTO"));
  70. }
  71. // Computes a unique hash for the Module considering the current list of
  72. // export/import and other global analysis results.
  73. // The hash is produced in \p Key.
  74. void llvm::computeLTOCacheKey(
  75. SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
  76. StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
  77. const FunctionImporter::ExportSetTy &ExportList,
  78. const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
  79. const GVSummaryMapTy &DefinedGlobals,
  80. const std::set<GlobalValue::GUID> &CfiFunctionDefs,
  81. const std::set<GlobalValue::GUID> &CfiFunctionDecls) {
  82. // Compute the unique hash for this entry.
  83. // This is based on the current compiler version, the module itself, the
  84. // export list, the hash for every single module in the import list, the
  85. // list of ResolvedODR for the module, and the list of preserved symbols.
  86. SHA1 Hasher;
  87. // Start with the compiler revision
  88. Hasher.update(LLVM_VERSION_STRING);
  89. #ifdef LLVM_REVISION
  90. Hasher.update(LLVM_REVISION);
  91. #endif
  92. // Include the parts of the LTO configuration that affect code generation.
  93. auto AddString = [&](StringRef Str) {
  94. Hasher.update(Str);
  95. Hasher.update(ArrayRef<uint8_t>{0});
  96. };
  97. auto AddUnsigned = [&](unsigned I) {
  98. uint8_t Data[4];
  99. support::endian::write32le(Data, I);
  100. Hasher.update(ArrayRef<uint8_t>{Data, 4});
  101. };
  102. auto AddUint64 = [&](uint64_t I) {
  103. uint8_t Data[8];
  104. support::endian::write64le(Data, I);
  105. Hasher.update(ArrayRef<uint8_t>{Data, 8});
  106. };
  107. AddString(Conf.CPU);
  108. // FIXME: Hash more of Options. For now all clients initialize Options from
  109. // command-line flags (which is unsupported in production), but may set
  110. // RelaxELFRelocations. The clang driver can also pass FunctionSections,
  111. // DataSections and DebuggerTuning via command line flags.
  112. AddUnsigned(Conf.Options.RelaxELFRelocations);
  113. AddUnsigned(Conf.Options.FunctionSections);
  114. AddUnsigned(Conf.Options.DataSections);
  115. AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
  116. for (auto &A : Conf.MAttrs)
  117. AddString(A);
  118. if (Conf.RelocModel)
  119. AddUnsigned(*Conf.RelocModel);
  120. else
  121. AddUnsigned(-1);
  122. if (Conf.CodeModel)
  123. AddUnsigned(*Conf.CodeModel);
  124. else
  125. AddUnsigned(-1);
  126. for (const auto &S : Conf.MllvmArgs)
  127. AddString(S);
  128. AddUnsigned(Conf.CGOptLevel);
  129. AddUnsigned(Conf.CGFileType);
  130. AddUnsigned(Conf.OptLevel);
  131. AddUnsigned(Conf.Freestanding);
  132. AddString(Conf.OptPipeline);
  133. AddString(Conf.AAPipeline);
  134. AddString(Conf.OverrideTriple);
  135. AddString(Conf.DefaultTriple);
  136. AddString(Conf.DwoDir);
  137. // Include the hash for the current module
  138. auto ModHash = Index.getModuleHash(ModuleID);
  139. Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
  140. std::vector<uint64_t> ExportsGUID;
  141. ExportsGUID.reserve(ExportList.size());
  142. for (const auto &VI : ExportList) {
  143. auto GUID = VI.getGUID();
  144. ExportsGUID.push_back(GUID);
  145. }
  146. // Sort the export list elements GUIDs.
  147. llvm::sort(ExportsGUID);
  148. for (uint64_t GUID : ExportsGUID) {
  149. // The export list can impact the internalization, be conservative here
  150. Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
  151. }
  152. // Include the hash for every module we import functions from. The set of
  153. // imported symbols for each module may affect code generation and is
  154. // sensitive to link order, so include that as well.
  155. using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
  156. std::vector<ImportMapIteratorTy> ImportModulesVector;
  157. ImportModulesVector.reserve(ImportList.size());
  158. for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
  159. ++It) {
  160. ImportModulesVector.push_back(It);
  161. }
  162. llvm::sort(ImportModulesVector,
  163. [](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
  164. -> bool { return Lhs->getKey() < Rhs->getKey(); });
  165. for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
  166. auto ModHash = Index.getModuleHash(EntryIt->first());
  167. Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
  168. AddUint64(EntryIt->second.size());
  169. for (auto &Fn : EntryIt->second)
  170. AddUint64(Fn);
  171. }
  172. // Include the hash for the resolved ODR.
  173. for (auto &Entry : ResolvedODR) {
  174. Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
  175. sizeof(GlobalValue::GUID)));
  176. Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
  177. sizeof(GlobalValue::LinkageTypes)));
  178. }
  179. // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
  180. // defined in this module.
  181. std::set<GlobalValue::GUID> UsedCfiDefs;
  182. std::set<GlobalValue::GUID> UsedCfiDecls;
  183. // Typeids used in this module.
  184. std::set<GlobalValue::GUID> UsedTypeIds;
  185. auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
  186. if (CfiFunctionDefs.count(ValueGUID))
  187. UsedCfiDefs.insert(ValueGUID);
  188. if (CfiFunctionDecls.count(ValueGUID))
  189. UsedCfiDecls.insert(ValueGUID);
  190. };
  191. auto AddUsedThings = [&](GlobalValueSummary *GS) {
  192. if (!GS) return;
  193. AddUnsigned(GS->getVisibility());
  194. AddUnsigned(GS->isLive());
  195. AddUnsigned(GS->canAutoHide());
  196. for (const ValueInfo &VI : GS->refs()) {
  197. AddUnsigned(VI.isDSOLocal(Index.withDSOLocalPropagation()));
  198. AddUsedCfiGlobal(VI.getGUID());
  199. }
  200. if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
  201. AddUnsigned(GVS->maybeReadOnly());
  202. AddUnsigned(GVS->maybeWriteOnly());
  203. }
  204. if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
  205. for (auto &TT : FS->type_tests())
  206. UsedTypeIds.insert(TT);
  207. for (auto &TT : FS->type_test_assume_vcalls())
  208. UsedTypeIds.insert(TT.GUID);
  209. for (auto &TT : FS->type_checked_load_vcalls())
  210. UsedTypeIds.insert(TT.GUID);
  211. for (auto &TT : FS->type_test_assume_const_vcalls())
  212. UsedTypeIds.insert(TT.VFunc.GUID);
  213. for (auto &TT : FS->type_checked_load_const_vcalls())
  214. UsedTypeIds.insert(TT.VFunc.GUID);
  215. for (auto &ET : FS->calls()) {
  216. AddUnsigned(ET.first.isDSOLocal(Index.withDSOLocalPropagation()));
  217. AddUsedCfiGlobal(ET.first.getGUID());
  218. }
  219. }
  220. };
  221. // Include the hash for the linkage type to reflect internalization and weak
  222. // resolution, and collect any used type identifier resolutions.
  223. for (auto &GS : DefinedGlobals) {
  224. GlobalValue::LinkageTypes Linkage = GS.second->linkage();
  225. Hasher.update(
  226. ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
  227. AddUsedCfiGlobal(GS.first);
  228. AddUsedThings(GS.second);
  229. }
  230. // Imported functions may introduce new uses of type identifier resolutions,
  231. // so we need to collect their used resolutions as well.
  232. for (auto &ImpM : ImportList)
  233. for (auto &ImpF : ImpM.second) {
  234. GlobalValueSummary *S = Index.findSummaryInModule(ImpF, ImpM.first());
  235. AddUsedThings(S);
  236. // If this is an alias, we also care about any types/etc. that the aliasee
  237. // may reference.
  238. if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
  239. AddUsedThings(AS->getBaseObject());
  240. }
  241. auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
  242. AddString(TId);
  243. AddUnsigned(S.TTRes.TheKind);
  244. AddUnsigned(S.TTRes.SizeM1BitWidth);
  245. AddUint64(S.TTRes.AlignLog2);
  246. AddUint64(S.TTRes.SizeM1);
  247. AddUint64(S.TTRes.BitMask);
  248. AddUint64(S.TTRes.InlineBits);
  249. AddUint64(S.WPDRes.size());
  250. for (auto &WPD : S.WPDRes) {
  251. AddUnsigned(WPD.first);
  252. AddUnsigned(WPD.second.TheKind);
  253. AddString(WPD.second.SingleImplName);
  254. AddUint64(WPD.second.ResByArg.size());
  255. for (auto &ByArg : WPD.second.ResByArg) {
  256. AddUint64(ByArg.first.size());
  257. for (uint64_t Arg : ByArg.first)
  258. AddUint64(Arg);
  259. AddUnsigned(ByArg.second.TheKind);
  260. AddUint64(ByArg.second.Info);
  261. AddUnsigned(ByArg.second.Byte);
  262. AddUnsigned(ByArg.second.Bit);
  263. }
  264. }
  265. };
  266. // Include the hash for all type identifiers used by this module.
  267. for (GlobalValue::GUID TId : UsedTypeIds) {
  268. auto TidIter = Index.typeIds().equal_range(TId);
  269. for (auto It = TidIter.first; It != TidIter.second; ++It)
  270. AddTypeIdSummary(It->second.first, It->second.second);
  271. }
  272. AddUnsigned(UsedCfiDefs.size());
  273. for (auto &V : UsedCfiDefs)
  274. AddUint64(V);
  275. AddUnsigned(UsedCfiDecls.size());
  276. for (auto &V : UsedCfiDecls)
  277. AddUint64(V);
  278. if (!Conf.SampleProfile.empty()) {
  279. auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
  280. if (FileOrErr) {
  281. Hasher.update(FileOrErr.get()->getBuffer());
  282. if (!Conf.ProfileRemapping.empty()) {
  283. FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
  284. if (FileOrErr)
  285. Hasher.update(FileOrErr.get()->getBuffer());
  286. }
  287. }
  288. }
  289. Key = toHex(Hasher.result());
  290. }
  291. static void thinLTOResolvePrevailingGUID(
  292. const Config &C, ValueInfo VI,
  293. DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
  294. function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
  295. isPrevailing,
  296. function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
  297. recordNewLinkage,
  298. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
  299. GlobalValue::VisibilityTypes Visibility =
  300. C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
  301. : GlobalValue::DefaultVisibility;
  302. for (auto &S : VI.getSummaryList()) {
  303. GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
  304. // Ignore local and appending linkage values since the linker
  305. // doesn't resolve them.
  306. if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
  307. GlobalValue::isAppendingLinkage(S->linkage()))
  308. continue;
  309. // We need to emit only one of these. The prevailing module will keep it,
  310. // but turned into a weak, while the others will drop it when possible.
  311. // This is both a compile-time optimization and a correctness
  312. // transformation. This is necessary for correctness when we have exported
  313. // a reference - we need to convert the linkonce to weak to
  314. // ensure a copy is kept to satisfy the exported reference.
  315. // FIXME: We may want to split the compile time and correctness
  316. // aspects into separate routines.
  317. if (isPrevailing(VI.getGUID(), S.get())) {
  318. if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
  319. S->setLinkage(GlobalValue::getWeakLinkage(
  320. GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
  321. // The kept copy is eligible for auto-hiding (hidden visibility) if all
  322. // copies were (i.e. they were all linkonce_odr global unnamed addr).
  323. // If any copy is not (e.g. it was originally weak_odr), then the symbol
  324. // must remain externally available (e.g. a weak_odr from an explicitly
  325. // instantiated template). Additionally, if it is in the
  326. // GUIDPreservedSymbols set, that means that it is visibile outside
  327. // the summary (e.g. in a native object or a bitcode file without
  328. // summary), and in that case we cannot hide it as it isn't possible to
  329. // check all copies.
  330. S->setCanAutoHide(VI.canAutoHide() &&
  331. !GUIDPreservedSymbols.count(VI.getGUID()));
  332. }
  333. if (C.VisibilityScheme == Config::FromPrevailing)
  334. Visibility = S->getVisibility();
  335. }
  336. // Alias and aliasee can't be turned into available_externally.
  337. else if (!isa<AliasSummary>(S.get()) &&
  338. !GlobalInvolvedWithAlias.count(S.get()))
  339. S->setLinkage(GlobalValue::AvailableExternallyLinkage);
  340. // For ELF, set visibility to the computed visibility from summaries. We
  341. // don't track visibility from declarations so this may be more relaxed than
  342. // the most constraining one.
  343. if (C.VisibilityScheme == Config::ELF)
  344. S->setVisibility(Visibility);
  345. if (S->linkage() != OriginalLinkage)
  346. recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
  347. }
  348. if (C.VisibilityScheme == Config::FromPrevailing) {
  349. for (auto &S : VI.getSummaryList()) {
  350. GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
  351. if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
  352. GlobalValue::isAppendingLinkage(S->linkage()))
  353. continue;
  354. S->setVisibility(Visibility);
  355. }
  356. }
  357. }
  358. /// Resolve linkage for prevailing symbols in the \p Index.
  359. //
  360. // We'd like to drop these functions if they are no longer referenced in the
  361. // current module. However there is a chance that another module is still
  362. // referencing them because of the import. We make sure we always emit at least
  363. // one copy.
  364. void llvm::thinLTOResolvePrevailingInIndex(
  365. const Config &C, ModuleSummaryIndex &Index,
  366. function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
  367. isPrevailing,
  368. function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
  369. recordNewLinkage,
  370. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
  371. // We won't optimize the globals that are referenced by an alias for now
  372. // Ideally we should turn the alias into a global and duplicate the definition
  373. // when needed.
  374. DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
  375. for (auto &I : Index)
  376. for (auto &S : I.second.SummaryList)
  377. if (auto AS = dyn_cast<AliasSummary>(S.get()))
  378. GlobalInvolvedWithAlias.insert(&AS->getAliasee());
  379. for (auto &I : Index)
  380. thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
  381. GlobalInvolvedWithAlias, isPrevailing,
  382. recordNewLinkage, GUIDPreservedSymbols);
  383. }
  384. static bool isWeakObjectWithRWAccess(GlobalValueSummary *GVS) {
  385. if (auto *VarSummary = dyn_cast<GlobalVarSummary>(GVS->getBaseObject()))
  386. return !VarSummary->maybeReadOnly() && !VarSummary->maybeWriteOnly() &&
  387. (VarSummary->linkage() == GlobalValue::WeakODRLinkage ||
  388. VarSummary->linkage() == GlobalValue::LinkOnceODRLinkage);
  389. return false;
  390. }
  391. static void thinLTOInternalizeAndPromoteGUID(
  392. ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
  393. function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
  394. isPrevailing) {
  395. for (auto &S : VI.getSummaryList()) {
  396. if (isExported(S->modulePath(), VI)) {
  397. if (GlobalValue::isLocalLinkage(S->linkage()))
  398. S->setLinkage(GlobalValue::ExternalLinkage);
  399. } else if (EnableLTOInternalization &&
  400. // Ignore local and appending linkage values since the linker
  401. // doesn't resolve them.
  402. !GlobalValue::isLocalLinkage(S->linkage()) &&
  403. (!GlobalValue::isInterposableLinkage(S->linkage()) ||
  404. isPrevailing(VI.getGUID(), S.get())) &&
  405. S->linkage() != GlobalValue::AppendingLinkage &&
  406. // We can't internalize available_externally globals because this
  407. // can break function pointer equality.
  408. S->linkage() != GlobalValue::AvailableExternallyLinkage &&
  409. // Functions and read-only variables with linkonce_odr and
  410. // weak_odr linkage can be internalized. We can't internalize
  411. // linkonce_odr and weak_odr variables which are both modified
  412. // and read somewhere in the program because reads and writes
  413. // will become inconsistent.
  414. !isWeakObjectWithRWAccess(S.get()))
  415. S->setLinkage(GlobalValue::InternalLinkage);
  416. }
  417. }
  418. // Update the linkages in the given \p Index to mark exported values
  419. // as external and non-exported values as internal.
  420. void llvm::thinLTOInternalizeAndPromoteInIndex(
  421. ModuleSummaryIndex &Index,
  422. function_ref<bool(StringRef, ValueInfo)> isExported,
  423. function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
  424. isPrevailing) {
  425. for (auto &I : Index)
  426. thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
  427. isPrevailing);
  428. }
  429. // Requires a destructor for std::vector<InputModule>.
  430. InputFile::~InputFile() = default;
  431. Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
  432. std::unique_ptr<InputFile> File(new InputFile);
  433. Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
  434. if (!FOrErr)
  435. return FOrErr.takeError();
  436. File->TargetTriple = FOrErr->TheReader.getTargetTriple();
  437. File->SourceFileName = FOrErr->TheReader.getSourceFileName();
  438. File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
  439. File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
  440. File->ComdatTable = FOrErr->TheReader.getComdatTable();
  441. for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
  442. size_t Begin = File->Symbols.size();
  443. for (const irsymtab::Reader::SymbolRef &Sym :
  444. FOrErr->TheReader.module_symbols(I))
  445. // Skip symbols that are irrelevant to LTO. Note that this condition needs
  446. // to match the one in Skip() in LTO::addRegularLTO().
  447. if (Sym.isGlobal() && !Sym.isFormatSpecific())
  448. File->Symbols.push_back(Sym);
  449. File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
  450. }
  451. File->Mods = FOrErr->Mods;
  452. File->Strtab = std::move(FOrErr->Strtab);
  453. return std::move(File);
  454. }
  455. StringRef InputFile::getName() const {
  456. return Mods[0].getModuleIdentifier();
  457. }
  458. BitcodeModule &InputFile::getSingleBitcodeModule() {
  459. assert(Mods.size() == 1 && "Expect only one bitcode module");
  460. return Mods[0];
  461. }
  462. LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
  463. const Config &Conf)
  464. : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
  465. Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
  466. Mover(std::make_unique<IRMover>(*CombinedModule)) {}
  467. LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
  468. : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
  469. if (!Backend)
  470. this->Backend =
  471. createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
  472. }
  473. LTO::LTO(Config Conf, ThinBackend Backend,
  474. unsigned ParallelCodeGenParallelismLevel)
  475. : Conf(std::move(Conf)),
  476. RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
  477. ThinLTO(std::move(Backend)) {}
  478. // Requires a destructor for MapVector<BitcodeModule>.
  479. LTO::~LTO() = default;
  480. // Add the symbols in the given module to the GlobalResolutions map, and resolve
  481. // their partitions.
  482. void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
  483. ArrayRef<SymbolResolution> Res,
  484. unsigned Partition, bool InSummary) {
  485. auto *ResI = Res.begin();
  486. auto *ResE = Res.end();
  487. (void)ResE;
  488. const Triple TT(RegularLTO.CombinedModule->getTargetTriple());
  489. for (const InputFile::Symbol &Sym : Syms) {
  490. assert(ResI != ResE);
  491. SymbolResolution Res = *ResI++;
  492. StringRef Name = Sym.getName();
  493. // Strip the __imp_ prefix from COFF dllimport symbols (similar to the
  494. // way they are handled by lld), otherwise we can end up with two
  495. // global resolutions (one with and one for a copy of the symbol without).
  496. if (TT.isOSBinFormatCOFF() && Name.startswith("__imp_"))
  497. Name = Name.substr(strlen("__imp_"));
  498. auto &GlobalRes = GlobalResolutions[Name];
  499. GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
  500. if (Res.Prevailing) {
  501. assert(!GlobalRes.Prevailing &&
  502. "Multiple prevailing defs are not allowed");
  503. GlobalRes.Prevailing = true;
  504. GlobalRes.IRName = std::string(Sym.getIRName());
  505. } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
  506. // Sometimes it can be two copies of symbol in a module and prevailing
  507. // symbol can have no IR name. That might happen if symbol is defined in
  508. // module level inline asm block. In case we have multiple modules with
  509. // the same symbol we want to use IR name of the prevailing symbol.
  510. // Otherwise, if we haven't seen a prevailing symbol, set the name so that
  511. // we can later use it to check if there is any prevailing copy in IR.
  512. GlobalRes.IRName = std::string(Sym.getIRName());
  513. }
  514. // In rare occasion, the symbol used to initialize GlobalRes has a different
  515. // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
  516. // symbol is referenced through its mangled name, say @"\01_symbol" while
  517. // the IRName is @symbol (the prefix underscore comes from MachO mangling).
  518. // In that case, we have the same actual Symbol that can get two different
  519. // GUID, leading to some invalid internalization. Workaround this by marking
  520. // the GlobalRes external.
  521. // FIXME: instead of this check, it would be desirable to compute GUIDs
  522. // based on mangled name, but this requires an access to the Target Triple
  523. // and would be relatively invasive on the codebase.
  524. if (GlobalRes.IRName != Sym.getIRName()) {
  525. GlobalRes.Partition = GlobalResolution::External;
  526. GlobalRes.VisibleOutsideSummary = true;
  527. }
  528. // Set the partition to external if we know it is re-defined by the linker
  529. // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
  530. // regular object, is referenced from llvm.compiler.used/llvm.used, or was
  531. // already recorded as being referenced from a different partition.
  532. if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
  533. (GlobalRes.Partition != GlobalResolution::Unknown &&
  534. GlobalRes.Partition != Partition)) {
  535. GlobalRes.Partition = GlobalResolution::External;
  536. } else
  537. // First recorded reference, save the current partition.
  538. GlobalRes.Partition = Partition;
  539. // Flag as visible outside of summary if visible from a regular object or
  540. // from a module that does not have a summary.
  541. GlobalRes.VisibleOutsideSummary |=
  542. (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
  543. GlobalRes.ExportDynamic |= Res.ExportDynamic;
  544. }
  545. }
  546. static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
  547. ArrayRef<SymbolResolution> Res) {
  548. StringRef Path = Input->getName();
  549. OS << Path << '\n';
  550. auto ResI = Res.begin();
  551. for (const InputFile::Symbol &Sym : Input->symbols()) {
  552. assert(ResI != Res.end());
  553. SymbolResolution Res = *ResI++;
  554. OS << "-r=" << Path << ',' << Sym.getName() << ',';
  555. if (Res.Prevailing)
  556. OS << 'p';
  557. if (Res.FinalDefinitionInLinkageUnit)
  558. OS << 'l';
  559. if (Res.VisibleToRegularObj)
  560. OS << 'x';
  561. if (Res.LinkerRedefined)
  562. OS << 'r';
  563. OS << '\n';
  564. }
  565. OS.flush();
  566. assert(ResI == Res.end());
  567. }
  568. Error LTO::add(std::unique_ptr<InputFile> Input,
  569. ArrayRef<SymbolResolution> Res) {
  570. assert(!CalledGetMaxTasks);
  571. if (Conf.ResolutionFile)
  572. writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
  573. if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
  574. RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
  575. if (Triple(Input->getTargetTriple()).isOSBinFormatELF())
  576. Conf.VisibilityScheme = Config::ELF;
  577. }
  578. const SymbolResolution *ResI = Res.begin();
  579. for (unsigned I = 0; I != Input->Mods.size(); ++I)
  580. if (Error Err = addModule(*Input, I, ResI, Res.end()))
  581. return Err;
  582. assert(ResI == Res.end());
  583. return Error::success();
  584. }
  585. Error LTO::addModule(InputFile &Input, unsigned ModI,
  586. const SymbolResolution *&ResI,
  587. const SymbolResolution *ResE) {
  588. Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
  589. if (!LTOInfo)
  590. return LTOInfo.takeError();
  591. if (EnableSplitLTOUnit) {
  592. // If only some modules were split, flag this in the index so that
  593. // we can skip or error on optimizations that need consistently split
  594. // modules (whole program devirt and lower type tests).
  595. if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
  596. ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
  597. } else
  598. EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
  599. BitcodeModule BM = Input.Mods[ModI];
  600. auto ModSyms = Input.module_symbols(ModI);
  601. addModuleToGlobalRes(ModSyms, {ResI, ResE},
  602. LTOInfo->IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
  603. LTOInfo->HasSummary);
  604. if (LTOInfo->IsThinLTO)
  605. return addThinLTO(BM, ModSyms, ResI, ResE);
  606. RegularLTO.EmptyCombinedModule = false;
  607. Expected<RegularLTOState::AddedModule> ModOrErr =
  608. addRegularLTO(BM, ModSyms, ResI, ResE);
  609. if (!ModOrErr)
  610. return ModOrErr.takeError();
  611. if (!LTOInfo->HasSummary)
  612. return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
  613. // Regular LTO module summaries are added to a dummy module that represents
  614. // the combined regular LTO module.
  615. if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, "", -1ull))
  616. return Err;
  617. RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
  618. return Error::success();
  619. }
  620. // Checks whether the given global value is in a non-prevailing comdat
  621. // (comdat containing values the linker indicated were not prevailing,
  622. // which we then dropped to available_externally), and if so, removes
  623. // it from the comdat. This is called for all global values to ensure the
  624. // comdat is empty rather than leaving an incomplete comdat. It is needed for
  625. // regular LTO modules, in case we are in a mixed-LTO mode (both regular
  626. // and thin LTO modules) compilation. Since the regular LTO module will be
  627. // linked first in the final native link, we want to make sure the linker
  628. // doesn't select any of these incomplete comdats that would be left
  629. // in the regular LTO module without this cleanup.
  630. static void
  631. handleNonPrevailingComdat(GlobalValue &GV,
  632. std::set<const Comdat *> &NonPrevailingComdats) {
  633. Comdat *C = GV.getComdat();
  634. if (!C)
  635. return;
  636. if (!NonPrevailingComdats.count(C))
  637. return;
  638. // Additionally need to drop all global values from the comdat to
  639. // available_externally, to satisfy the COMDAT requirement that all members
  640. // are discarded as a unit. The non-local linkage global values avoid
  641. // duplicate definition linker errors.
  642. GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
  643. if (auto GO = dyn_cast<GlobalObject>(&GV))
  644. GO->setComdat(nullptr);
  645. }
  646. // Add a regular LTO object to the link.
  647. // The resulting module needs to be linked into the combined LTO module with
  648. // linkRegularLTO.
  649. Expected<LTO::RegularLTOState::AddedModule>
  650. LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
  651. const SymbolResolution *&ResI,
  652. const SymbolResolution *ResE) {
  653. RegularLTOState::AddedModule Mod;
  654. Expected<std::unique_ptr<Module>> MOrErr =
  655. BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
  656. /*IsImporting*/ false);
  657. if (!MOrErr)
  658. return MOrErr.takeError();
  659. Module &M = **MOrErr;
  660. Mod.M = std::move(*MOrErr);
  661. if (Error Err = M.materializeMetadata())
  662. return std::move(Err);
  663. UpgradeDebugInfo(M);
  664. ModuleSymbolTable SymTab;
  665. SymTab.addModule(&M);
  666. for (GlobalVariable &GV : M.globals())
  667. if (GV.hasAppendingLinkage())
  668. Mod.Keep.push_back(&GV);
  669. DenseSet<GlobalObject *> AliasedGlobals;
  670. for (auto &GA : M.aliases())
  671. if (GlobalObject *GO = GA.getAliaseeObject())
  672. AliasedGlobals.insert(GO);
  673. // In this function we need IR GlobalValues matching the symbols in Syms
  674. // (which is not backed by a module), so we need to enumerate them in the same
  675. // order. The symbol enumeration order of a ModuleSymbolTable intentionally
  676. // matches the order of an irsymtab, but when we read the irsymtab in
  677. // InputFile::create we omit some symbols that are irrelevant to LTO. The
  678. // Skip() function skips the same symbols from the module as InputFile does
  679. // from the symbol table.
  680. auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
  681. auto Skip = [&]() {
  682. while (MsymI != MsymE) {
  683. auto Flags = SymTab.getSymbolFlags(*MsymI);
  684. if ((Flags & object::BasicSymbolRef::SF_Global) &&
  685. !(Flags & object::BasicSymbolRef::SF_FormatSpecific))
  686. return;
  687. ++MsymI;
  688. }
  689. };
  690. Skip();
  691. std::set<const Comdat *> NonPrevailingComdats;
  692. SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
  693. for (const InputFile::Symbol &Sym : Syms) {
  694. assert(ResI != ResE);
  695. SymbolResolution Res = *ResI++;
  696. assert(MsymI != MsymE);
  697. ModuleSymbolTable::Symbol Msym = *MsymI++;
  698. Skip();
  699. if (GlobalValue *GV = Msym.dyn_cast<GlobalValue *>()) {
  700. if (Res.Prevailing) {
  701. if (Sym.isUndefined())
  702. continue;
  703. Mod.Keep.push_back(GV);
  704. // For symbols re-defined with linker -wrap and -defsym options,
  705. // set the linkage to weak to inhibit IPO. The linkage will be
  706. // restored by the linker.
  707. if (Res.LinkerRedefined)
  708. GV->setLinkage(GlobalValue::WeakAnyLinkage);
  709. GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
  710. if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
  711. GV->setLinkage(GlobalValue::getWeakLinkage(
  712. GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
  713. } else if (isa<GlobalObject>(GV) &&
  714. (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
  715. GV->hasAvailableExternallyLinkage()) &&
  716. !AliasedGlobals.count(cast<GlobalObject>(GV))) {
  717. // Any of the above three types of linkage indicates that the
  718. // chosen prevailing symbol will have the same semantics as this copy of
  719. // the symbol, so we may be able to link it with available_externally
  720. // linkage. We will decide later whether to do that when we link this
  721. // module (in linkRegularLTO), based on whether it is undefined.
  722. Mod.Keep.push_back(GV);
  723. GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
  724. if (GV->hasComdat())
  725. NonPrevailingComdats.insert(GV->getComdat());
  726. cast<GlobalObject>(GV)->setComdat(nullptr);
  727. }
  728. // Set the 'local' flag based on the linker resolution for this symbol.
  729. if (Res.FinalDefinitionInLinkageUnit) {
  730. GV->setDSOLocal(true);
  731. if (GV->hasDLLImportStorageClass())
  732. GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
  733. DefaultStorageClass);
  734. }
  735. } else if (auto *AS = Msym.dyn_cast<ModuleSymbolTable::AsmSymbol *>()) {
  736. // Collect non-prevailing symbols.
  737. if (!Res.Prevailing)
  738. NonPrevailingAsmSymbols.insert(AS->first);
  739. } else {
  740. llvm_unreachable("unknown symbol type");
  741. }
  742. // Common resolution: collect the maximum size/alignment over all commons.
  743. // We also record if we see an instance of a common as prevailing, so that
  744. // if none is prevailing we can ignore it later.
  745. if (Sym.isCommon()) {
  746. // FIXME: We should figure out what to do about commons defined by asm.
  747. // For now they aren't reported correctly by ModuleSymbolTable.
  748. auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
  749. CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
  750. if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
  751. const Align SymAlign(SymAlignValue);
  752. CommonRes.Align = std::max(SymAlign, CommonRes.Align.valueOrOne());
  753. }
  754. CommonRes.Prevailing |= Res.Prevailing;
  755. }
  756. }
  757. if (!M.getComdatSymbolTable().empty())
  758. for (GlobalValue &GV : M.global_values())
  759. handleNonPrevailingComdat(GV, NonPrevailingComdats);
  760. // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
  761. // block.
  762. if (!M.getModuleInlineAsm().empty()) {
  763. std::string NewIA = ".lto_discard";
  764. if (!NonPrevailingAsmSymbols.empty()) {
  765. // Don't dicard a symbol if there is a live .symver for it.
  766. ModuleSymbolTable::CollectAsmSymvers(
  767. M, [&](StringRef Name, StringRef Alias) {
  768. if (!NonPrevailingAsmSymbols.count(Alias))
  769. NonPrevailingAsmSymbols.erase(Name);
  770. });
  771. NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
  772. }
  773. NewIA += "\n";
  774. M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
  775. }
  776. assert(MsymI == MsymE);
  777. return std::move(Mod);
  778. }
  779. Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
  780. bool LivenessFromIndex) {
  781. std::vector<GlobalValue *> Keep;
  782. for (GlobalValue *GV : Mod.Keep) {
  783. if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
  784. if (Function *F = dyn_cast<Function>(GV)) {
  785. if (DiagnosticOutputFile) {
  786. if (Error Err = F->materialize())
  787. return Err;
  788. OptimizationRemarkEmitter ORE(F, nullptr);
  789. ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
  790. << ore::NV("Function", F)
  791. << " not added to the combined module ");
  792. }
  793. }
  794. continue;
  795. }
  796. if (!GV->hasAvailableExternallyLinkage()) {
  797. Keep.push_back(GV);
  798. continue;
  799. }
  800. // Only link available_externally definitions if we don't already have a
  801. // definition.
  802. GlobalValue *CombinedGV =
  803. RegularLTO.CombinedModule->getNamedValue(GV->getName());
  804. if (CombinedGV && !CombinedGV->isDeclaration())
  805. continue;
  806. Keep.push_back(GV);
  807. }
  808. return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
  809. /* IsPerformingImport */ false);
  810. }
  811. // Add a ThinLTO module to the link.
  812. Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
  813. const SymbolResolution *&ResI,
  814. const SymbolResolution *ResE) {
  815. const SymbolResolution *ResITmp = ResI;
  816. for (const InputFile::Symbol &Sym : Syms) {
  817. assert(ResITmp != ResE);
  818. SymbolResolution Res = *ResITmp++;
  819. if (!Sym.getIRName().empty()) {
  820. auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
  821. Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
  822. if (Res.Prevailing)
  823. ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
  824. }
  825. }
  826. if (Error Err =
  827. BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
  828. ThinLTO.ModuleMap.size(), [&](GlobalValue::GUID GUID) {
  829. return ThinLTO.PrevailingModuleForGUID[GUID] ==
  830. BM.getModuleIdentifier();
  831. }))
  832. return Err;
  833. for (const InputFile::Symbol &Sym : Syms) {
  834. assert(ResI != ResE);
  835. SymbolResolution Res = *ResI++;
  836. if (!Sym.getIRName().empty()) {
  837. auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
  838. Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
  839. if (Res.Prevailing) {
  840. assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
  841. BM.getModuleIdentifier());
  842. // For linker redefined symbols (via --wrap or --defsym) we want to
  843. // switch the linkage to `weak` to prevent IPOs from happening.
  844. // Find the summary in the module for this very GV and record the new
  845. // linkage so that we can switch it when we import the GV.
  846. if (Res.LinkerRedefined)
  847. if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
  848. GUID, BM.getModuleIdentifier()))
  849. S->setLinkage(GlobalValue::WeakAnyLinkage);
  850. }
  851. // If the linker resolved the symbol to a local definition then mark it
  852. // as local in the summary for the module we are adding.
  853. if (Res.FinalDefinitionInLinkageUnit) {
  854. if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
  855. GUID, BM.getModuleIdentifier())) {
  856. S->setDSOLocal(true);
  857. }
  858. }
  859. }
  860. }
  861. if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
  862. return make_error<StringError>(
  863. "Expected at most one ThinLTO module per bitcode file",
  864. inconvertibleErrorCode());
  865. if (!Conf.ThinLTOModulesToCompile.empty()) {
  866. if (!ThinLTO.ModulesToCompile)
  867. ThinLTO.ModulesToCompile = ModuleMapType();
  868. // This is a fuzzy name matching where only modules with name containing the
  869. // specified switch values are going to be compiled.
  870. for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
  871. if (BM.getModuleIdentifier().contains(Name)) {
  872. ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
  873. llvm::errs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
  874. << " to compile\n";
  875. }
  876. }
  877. }
  878. return Error::success();
  879. }
  880. unsigned LTO::getMaxTasks() const {
  881. CalledGetMaxTasks = true;
  882. auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
  883. : ThinLTO.ModuleMap.size();
  884. return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
  885. }
  886. // If only some of the modules were split, we cannot correctly handle
  887. // code that contains type tests or type checked loads.
  888. Error LTO::checkPartiallySplit() {
  889. if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
  890. return Error::success();
  891. Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
  892. Intrinsic::getName(Intrinsic::type_test));
  893. Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
  894. Intrinsic::getName(Intrinsic::type_checked_load));
  895. // First check if there are type tests / type checked loads in the
  896. // merged regular LTO module IR.
  897. if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
  898. (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()))
  899. return make_error<StringError>(
  900. "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
  901. inconvertibleErrorCode());
  902. // Otherwise check if there are any recorded in the combined summary from the
  903. // ThinLTO modules.
  904. for (auto &P : ThinLTO.CombinedIndex) {
  905. for (auto &S : P.second.SummaryList) {
  906. auto *FS = dyn_cast<FunctionSummary>(S.get());
  907. if (!FS)
  908. continue;
  909. if (!FS->type_test_assume_vcalls().empty() ||
  910. !FS->type_checked_load_vcalls().empty() ||
  911. !FS->type_test_assume_const_vcalls().empty() ||
  912. !FS->type_checked_load_const_vcalls().empty() ||
  913. !FS->type_tests().empty())
  914. return make_error<StringError>(
  915. "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
  916. inconvertibleErrorCode());
  917. }
  918. }
  919. return Error::success();
  920. }
  921. Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
  922. // Compute "dead" symbols, we don't want to import/export these!
  923. DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
  924. DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
  925. for (auto &Res : GlobalResolutions) {
  926. // Normally resolution have IR name of symbol. We can do nothing here
  927. // otherwise. See comments in GlobalResolution struct for more details.
  928. if (Res.second.IRName.empty())
  929. continue;
  930. GlobalValue::GUID GUID = GlobalValue::getGUID(
  931. GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
  932. if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
  933. GUIDPreservedSymbols.insert(GUID);
  934. if (Res.second.ExportDynamic)
  935. DynamicExportSymbols.insert(GUID);
  936. GUIDPrevailingResolutions[GUID] =
  937. Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
  938. }
  939. auto isPrevailing = [&](GlobalValue::GUID G) {
  940. auto It = GUIDPrevailingResolutions.find(G);
  941. if (It == GUIDPrevailingResolutions.end())
  942. return PrevailingType::Unknown;
  943. return It->second;
  944. };
  945. computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
  946. isPrevailing, Conf.OptLevel > 0);
  947. // Setup output file to emit statistics.
  948. auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
  949. if (!StatsFileOrErr)
  950. return StatsFileOrErr.takeError();
  951. std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
  952. Error Result = runRegularLTO(AddStream);
  953. if (!Result)
  954. Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
  955. if (StatsFile)
  956. PrintStatisticsJSON(StatsFile->os());
  957. return Result;
  958. }
  959. Error LTO::runRegularLTO(AddStreamFn AddStream) {
  960. // Setup optimization remarks.
  961. auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
  962. RegularLTO.CombinedModule->getContext(), Conf.RemarksFilename,
  963. Conf.RemarksPasses, Conf.RemarksFormat, Conf.RemarksWithHotness,
  964. Conf.RemarksHotnessThreshold);
  965. if (!DiagFileOrErr)
  966. return DiagFileOrErr.takeError();
  967. DiagnosticOutputFile = std::move(*DiagFileOrErr);
  968. // Finalize linking of regular LTO modules containing summaries now that
  969. // we have computed liveness information.
  970. for (auto &M : RegularLTO.ModsWithSummaries)
  971. if (Error Err = linkRegularLTO(std::move(M),
  972. /*LivenessFromIndex=*/true))
  973. return Err;
  974. // Ensure we don't have inconsistently split LTO units with type tests.
  975. // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
  976. // this path both cases but eventually this should be split into two and
  977. // do the ThinLTO checks in `runThinLTO`.
  978. if (Error Err = checkPartiallySplit())
  979. return Err;
  980. // Make sure commons have the right size/alignment: we kept the largest from
  981. // all the prevailing when adding the inputs, and we apply it here.
  982. const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
  983. for (auto &I : RegularLTO.Commons) {
  984. if (!I.second.Prevailing)
  985. // Don't do anything if no instance of this common was prevailing.
  986. continue;
  987. GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
  988. if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
  989. // Don't create a new global if the type is already correct, just make
  990. // sure the alignment is correct.
  991. OldGV->setAlignment(I.second.Align);
  992. continue;
  993. }
  994. ArrayType *Ty =
  995. ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
  996. auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
  997. GlobalValue::CommonLinkage,
  998. ConstantAggregateZero::get(Ty), "");
  999. GV->setAlignment(I.second.Align);
  1000. if (OldGV) {
  1001. OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType()));
  1002. GV->takeName(OldGV);
  1003. OldGV->eraseFromParent();
  1004. } else {
  1005. GV->setName(I.first);
  1006. }
  1007. }
  1008. // If allowed, upgrade public vcall visibility metadata to linkage unit
  1009. // visibility before whole program devirtualization in the optimizer.
  1010. updateVCallVisibilityInModule(*RegularLTO.CombinedModule,
  1011. Conf.HasWholeProgramVisibility,
  1012. DynamicExportSymbols);
  1013. updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
  1014. Conf.HasWholeProgramVisibility);
  1015. if (Conf.PreOptModuleHook &&
  1016. !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
  1017. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  1018. if (!Conf.CodeGenOnly) {
  1019. for (const auto &R : GlobalResolutions) {
  1020. if (!R.second.isPrevailingIRSymbol())
  1021. continue;
  1022. if (R.second.Partition != 0 &&
  1023. R.second.Partition != GlobalResolution::External)
  1024. continue;
  1025. GlobalValue *GV =
  1026. RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
  1027. // Ignore symbols defined in other partitions.
  1028. // Also skip declarations, which are not allowed to have internal linkage.
  1029. if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
  1030. continue;
  1031. GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
  1032. : GlobalValue::UnnamedAddr::None);
  1033. if (EnableLTOInternalization && R.second.Partition == 0)
  1034. GV->setLinkage(GlobalValue::InternalLinkage);
  1035. }
  1036. RegularLTO.CombinedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
  1037. if (Conf.PostInternalizeModuleHook &&
  1038. !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
  1039. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  1040. }
  1041. if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
  1042. if (Error Err =
  1043. backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
  1044. *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
  1045. return Err;
  1046. }
  1047. return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
  1048. }
  1049. static const char *libcallRoutineNames[] = {
  1050. #define HANDLE_LIBCALL(code, name) name,
  1051. #include "llvm/IR/RuntimeLibcalls.def"
  1052. #undef HANDLE_LIBCALL
  1053. };
  1054. ArrayRef<const char*> LTO::getRuntimeLibcallSymbols() {
  1055. return ArrayRef(libcallRoutineNames);
  1056. }
  1057. /// This class defines the interface to the ThinLTO backend.
  1058. class lto::ThinBackendProc {
  1059. protected:
  1060. const Config &Conf;
  1061. ModuleSummaryIndex &CombinedIndex;
  1062. const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries;
  1063. lto::IndexWriteCallback OnWrite;
  1064. bool ShouldEmitImportsFiles;
  1065. public:
  1066. ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex,
  1067. const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
  1068. lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
  1069. : Conf(Conf), CombinedIndex(CombinedIndex),
  1070. ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
  1071. OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
  1072. virtual ~ThinBackendProc() = default;
  1073. virtual Error start(
  1074. unsigned Task, BitcodeModule BM,
  1075. const FunctionImporter::ImportMapTy &ImportList,
  1076. const FunctionImporter::ExportSetTy &ExportList,
  1077. const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
  1078. MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
  1079. virtual Error wait() = 0;
  1080. virtual unsigned getThreadCount() = 0;
  1081. // Write sharded indices and (optionally) imports to disk
  1082. Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
  1083. llvm::StringRef ModulePath,
  1084. const std::string &NewModulePath) {
  1085. std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
  1086. std::error_code EC;
  1087. gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
  1088. ImportList, ModuleToSummariesForIndex);
  1089. raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
  1090. sys::fs::OpenFlags::OF_None);
  1091. if (EC)
  1092. return errorCodeToError(EC);
  1093. writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
  1094. if (ShouldEmitImportsFiles) {
  1095. EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
  1096. ModuleToSummariesForIndex);
  1097. if (EC)
  1098. return errorCodeToError(EC);
  1099. }
  1100. return Error::success();
  1101. }
  1102. };
  1103. namespace {
  1104. class InProcessThinBackend : public ThinBackendProc {
  1105. ThreadPool BackendThreadPool;
  1106. AddStreamFn AddStream;
  1107. FileCache Cache;
  1108. std::set<GlobalValue::GUID> CfiFunctionDefs;
  1109. std::set<GlobalValue::GUID> CfiFunctionDecls;
  1110. std::optional<Error> Err;
  1111. std::mutex ErrMu;
  1112. bool ShouldEmitIndexFiles;
  1113. public:
  1114. InProcessThinBackend(
  1115. const Config &Conf, ModuleSummaryIndex &CombinedIndex,
  1116. ThreadPoolStrategy ThinLTOParallelism,
  1117. const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
  1118. AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
  1119. bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
  1120. : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
  1121. OnWrite, ShouldEmitImportsFiles),
  1122. BackendThreadPool(ThinLTOParallelism), AddStream(std::move(AddStream)),
  1123. Cache(std::move(Cache)), ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
  1124. for (auto &Name : CombinedIndex.cfiFunctionDefs())
  1125. CfiFunctionDefs.insert(
  1126. GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
  1127. for (auto &Name : CombinedIndex.cfiFunctionDecls())
  1128. CfiFunctionDecls.insert(
  1129. GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
  1130. }
  1131. Error runThinLTOBackendThread(
  1132. AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
  1133. ModuleSummaryIndex &CombinedIndex,
  1134. const FunctionImporter::ImportMapTy &ImportList,
  1135. const FunctionImporter::ExportSetTy &ExportList,
  1136. const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
  1137. const GVSummaryMapTy &DefinedGlobals,
  1138. MapVector<StringRef, BitcodeModule> &ModuleMap) {
  1139. auto RunThinBackend = [&](AddStreamFn AddStream) {
  1140. LTOLLVMContext BackendContext(Conf);
  1141. Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
  1142. if (!MOrErr)
  1143. return MOrErr.takeError();
  1144. return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
  1145. ImportList, DefinedGlobals, &ModuleMap);
  1146. };
  1147. auto ModuleID = BM.getModuleIdentifier();
  1148. if (ShouldEmitIndexFiles) {
  1149. if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
  1150. return E;
  1151. }
  1152. if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
  1153. all_of(CombinedIndex.getModuleHash(ModuleID),
  1154. [](uint32_t V) { return V == 0; }))
  1155. // Cache disabled or no entry for this module in the combined index or
  1156. // no module hash.
  1157. return RunThinBackend(AddStream);
  1158. SmallString<40> Key;
  1159. // The module may be cached, this helps handling it.
  1160. computeLTOCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList,
  1161. ExportList, ResolvedODR, DefinedGlobals, CfiFunctionDefs,
  1162. CfiFunctionDecls);
  1163. Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
  1164. if (Error Err = CacheAddStreamOrErr.takeError())
  1165. return Err;
  1166. AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
  1167. if (CacheAddStream)
  1168. return RunThinBackend(CacheAddStream);
  1169. return Error::success();
  1170. }
  1171. Error start(
  1172. unsigned Task, BitcodeModule BM,
  1173. const FunctionImporter::ImportMapTy &ImportList,
  1174. const FunctionImporter::ExportSetTy &ExportList,
  1175. const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
  1176. MapVector<StringRef, BitcodeModule> &ModuleMap) override {
  1177. StringRef ModulePath = BM.getModuleIdentifier();
  1178. assert(ModuleToDefinedGVSummaries.count(ModulePath));
  1179. const GVSummaryMapTy &DefinedGlobals =
  1180. ModuleToDefinedGVSummaries.find(ModulePath)->second;
  1181. BackendThreadPool.async(
  1182. [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
  1183. const FunctionImporter::ImportMapTy &ImportList,
  1184. const FunctionImporter::ExportSetTy &ExportList,
  1185. const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
  1186. &ResolvedODR,
  1187. const GVSummaryMapTy &DefinedGlobals,
  1188. MapVector<StringRef, BitcodeModule> &ModuleMap) {
  1189. if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
  1190. timeTraceProfilerInitialize(Conf.TimeTraceGranularity,
  1191. "thin backend");
  1192. Error E = runThinLTOBackendThread(
  1193. AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
  1194. ResolvedODR, DefinedGlobals, ModuleMap);
  1195. if (E) {
  1196. std::unique_lock<std::mutex> L(ErrMu);
  1197. if (Err)
  1198. Err = joinErrors(std::move(*Err), std::move(E));
  1199. else
  1200. Err = std::move(E);
  1201. }
  1202. if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
  1203. timeTraceProfilerFinishThread();
  1204. },
  1205. BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
  1206. std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
  1207. if (OnWrite)
  1208. OnWrite(std::string(ModulePath));
  1209. return Error::success();
  1210. }
  1211. Error wait() override {
  1212. BackendThreadPool.wait();
  1213. if (Err)
  1214. return std::move(*Err);
  1215. else
  1216. return Error::success();
  1217. }
  1218. unsigned getThreadCount() override {
  1219. return BackendThreadPool.getThreadCount();
  1220. }
  1221. };
  1222. } // end anonymous namespace
  1223. ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
  1224. lto::IndexWriteCallback OnWrite,
  1225. bool ShouldEmitIndexFiles,
  1226. bool ShouldEmitImportsFiles) {
  1227. return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
  1228. const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
  1229. AddStreamFn AddStream, FileCache Cache) {
  1230. return std::make_unique<InProcessThinBackend>(
  1231. Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream,
  1232. Cache, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles);
  1233. };
  1234. }
  1235. // Given the original \p Path to an output file, replace any path
  1236. // prefix matching \p OldPrefix with \p NewPrefix. Also, create the
  1237. // resulting directory if it does not yet exist.
  1238. std::string lto::getThinLTOOutputFile(const std::string &Path,
  1239. const std::string &OldPrefix,
  1240. const std::string &NewPrefix) {
  1241. if (OldPrefix.empty() && NewPrefix.empty())
  1242. return Path;
  1243. SmallString<128> NewPath(Path);
  1244. llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
  1245. StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
  1246. if (!ParentPath.empty()) {
  1247. // Make sure the new directory exists, creating it if necessary.
  1248. if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
  1249. llvm::errs() << "warning: could not create directory '" << ParentPath
  1250. << "': " << EC.message() << '\n';
  1251. }
  1252. return std::string(NewPath.str());
  1253. }
  1254. namespace {
  1255. class WriteIndexesThinBackend : public ThinBackendProc {
  1256. std::string OldPrefix, NewPrefix;
  1257. raw_fd_ostream *LinkedObjectsFile;
  1258. public:
  1259. WriteIndexesThinBackend(
  1260. const Config &Conf, ModuleSummaryIndex &CombinedIndex,
  1261. const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
  1262. std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
  1263. raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
  1264. : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
  1265. OnWrite, ShouldEmitImportsFiles),
  1266. OldPrefix(OldPrefix), NewPrefix(NewPrefix),
  1267. LinkedObjectsFile(LinkedObjectsFile) {}
  1268. Error start(
  1269. unsigned Task, BitcodeModule BM,
  1270. const FunctionImporter::ImportMapTy &ImportList,
  1271. const FunctionImporter::ExportSetTy &ExportList,
  1272. const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
  1273. MapVector<StringRef, BitcodeModule> &ModuleMap) override {
  1274. StringRef ModulePath = BM.getModuleIdentifier();
  1275. std::string NewModulePath =
  1276. getThinLTOOutputFile(std::string(ModulePath), OldPrefix, NewPrefix);
  1277. if (LinkedObjectsFile)
  1278. *LinkedObjectsFile << NewModulePath << '\n';
  1279. if (auto E = emitFiles(ImportList, ModulePath, NewModulePath))
  1280. return E;
  1281. if (OnWrite)
  1282. OnWrite(std::string(ModulePath));
  1283. return Error::success();
  1284. }
  1285. Error wait() override { return Error::success(); }
  1286. // WriteIndexesThinBackend should always return 1 to prevent module
  1287. // re-ordering and avoid non-determinism in the final link.
  1288. unsigned getThreadCount() override { return 1; }
  1289. };
  1290. } // end anonymous namespace
  1291. ThinBackend lto::createWriteIndexesThinBackend(
  1292. std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
  1293. raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
  1294. return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
  1295. const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
  1296. AddStreamFn AddStream, FileCache Cache) {
  1297. return std::make_unique<WriteIndexesThinBackend>(
  1298. Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
  1299. ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite);
  1300. };
  1301. }
  1302. Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
  1303. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
  1304. ThinLTO.CombinedIndex.releaseTemporaryMemory();
  1305. timeTraceProfilerBegin("ThinLink", StringRef(""));
  1306. auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
  1307. if (llvm::timeTraceProfilerEnabled())
  1308. llvm::timeTraceProfilerEnd();
  1309. });
  1310. if (ThinLTO.ModuleMap.empty())
  1311. return Error::success();
  1312. if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
  1313. llvm::errs() << "warning: [ThinLTO] No module compiled\n";
  1314. return Error::success();
  1315. }
  1316. if (Conf.CombinedIndexHook &&
  1317. !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
  1318. return Error::success();
  1319. // Collect for each module the list of function it defines (GUID ->
  1320. // Summary).
  1321. StringMap<GVSummaryMapTy>
  1322. ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
  1323. ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
  1324. ModuleToDefinedGVSummaries);
  1325. // Create entries for any modules that didn't have any GV summaries
  1326. // (either they didn't have any GVs to start with, or we suppressed
  1327. // generation of the summaries because they e.g. had inline assembly
  1328. // uses that couldn't be promoted/renamed on export). This is so
  1329. // InProcessThinBackend::start can still launch a backend thread, which
  1330. // is passed the map of summaries for the module, without any special
  1331. // handling for this case.
  1332. for (auto &Mod : ThinLTO.ModuleMap)
  1333. if (!ModuleToDefinedGVSummaries.count(Mod.first))
  1334. ModuleToDefinedGVSummaries.try_emplace(Mod.first);
  1335. // Synthesize entry counts for functions in the CombinedIndex.
  1336. computeSyntheticCounts(ThinLTO.CombinedIndex);
  1337. StringMap<FunctionImporter::ImportMapTy> ImportLists(
  1338. ThinLTO.ModuleMap.size());
  1339. StringMap<FunctionImporter::ExportSetTy> ExportLists(
  1340. ThinLTO.ModuleMap.size());
  1341. StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
  1342. if (DumpThinCGSCCs)
  1343. ThinLTO.CombinedIndex.dumpSCCs(outs());
  1344. std::set<GlobalValue::GUID> ExportedGUIDs;
  1345. if (hasWholeProgramVisibility(Conf.HasWholeProgramVisibility))
  1346. ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
  1347. // If allowed, upgrade public vcall visibility to linkage unit visibility in
  1348. // the summaries before whole program devirtualization below.
  1349. updateVCallVisibilityInIndex(ThinLTO.CombinedIndex,
  1350. Conf.HasWholeProgramVisibility,
  1351. DynamicExportSymbols);
  1352. // Perform index-based WPD. This will return immediately if there are
  1353. // no index entries in the typeIdMetadata map (e.g. if we are instead
  1354. // performing IR-based WPD in hybrid regular/thin LTO mode).
  1355. std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
  1356. runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
  1357. LocalWPDTargetsMap);
  1358. if (Conf.OptLevel > 0)
  1359. ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
  1360. ImportLists, ExportLists);
  1361. // Figure out which symbols need to be internalized. This also needs to happen
  1362. // at -O0 because summary-based DCE is implemented using internalization, and
  1363. // we must apply DCE consistently with the full LTO module in order to avoid
  1364. // undefined references during the final link.
  1365. for (auto &Res : GlobalResolutions) {
  1366. // If the symbol does not have external references or it is not prevailing,
  1367. // then not need to mark it as exported from a ThinLTO partition.
  1368. if (Res.second.Partition != GlobalResolution::External ||
  1369. !Res.second.isPrevailingIRSymbol())
  1370. continue;
  1371. auto GUID = GlobalValue::getGUID(
  1372. GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
  1373. // Mark exported unless index-based analysis determined it to be dead.
  1374. if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
  1375. ExportedGUIDs.insert(GUID);
  1376. }
  1377. // Any functions referenced by the jump table in the regular LTO object must
  1378. // be exported.
  1379. for (auto &Def : ThinLTO.CombinedIndex.cfiFunctionDefs())
  1380. ExportedGUIDs.insert(
  1381. GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Def)));
  1382. for (auto &Decl : ThinLTO.CombinedIndex.cfiFunctionDecls())
  1383. ExportedGUIDs.insert(
  1384. GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Decl)));
  1385. auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
  1386. const auto &ExportList = ExportLists.find(ModuleIdentifier);
  1387. return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
  1388. ExportedGUIDs.count(VI.getGUID());
  1389. };
  1390. // Update local devirtualized targets that were exported by cross-module
  1391. // importing or by other devirtualizations marked in the ExportedGUIDs set.
  1392. updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
  1393. LocalWPDTargetsMap);
  1394. auto isPrevailing = [&](GlobalValue::GUID GUID,
  1395. const GlobalValueSummary *S) {
  1396. return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
  1397. };
  1398. thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
  1399. isPrevailing);
  1400. auto recordNewLinkage = [&](StringRef ModuleIdentifier,
  1401. GlobalValue::GUID GUID,
  1402. GlobalValue::LinkageTypes NewLinkage) {
  1403. ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
  1404. };
  1405. thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
  1406. recordNewLinkage, GUIDPreservedSymbols);
  1407. thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
  1408. generateParamAccessSummary(ThinLTO.CombinedIndex);
  1409. if (llvm::timeTraceProfilerEnabled())
  1410. llvm::timeTraceProfilerEnd();
  1411. TimeTraceScopeExit.release();
  1412. std::unique_ptr<ThinBackendProc> BackendProc =
  1413. ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
  1414. AddStream, Cache);
  1415. auto &ModuleMap =
  1416. ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
  1417. auto ProcessOneModule = [&](int I) -> Error {
  1418. auto &Mod = *(ModuleMap.begin() + I);
  1419. // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
  1420. // combined module and parallel code generation partitions.
  1421. return BackendProc->start(RegularLTO.ParallelCodeGenParallelismLevel + I,
  1422. Mod.second, ImportLists[Mod.first],
  1423. ExportLists[Mod.first], ResolvedODR[Mod.first],
  1424. ThinLTO.ModuleMap);
  1425. };
  1426. if (BackendProc->getThreadCount() == 1) {
  1427. // Process the modules in the order they were provided on the command-line.
  1428. // It is important for this codepath to be used for WriteIndexesThinBackend,
  1429. // to ensure the emitted LinkedObjectsFile lists ThinLTO objects in the same
  1430. // order as the inputs, which otherwise would affect the final link order.
  1431. for (int I = 0, E = ModuleMap.size(); I != E; ++I)
  1432. if (Error E = ProcessOneModule(I))
  1433. return E;
  1434. } else {
  1435. // When executing in parallel, process largest bitsize modules first to
  1436. // improve parallelism, and avoid starving the thread pool near the end.
  1437. // This saves about 15 sec on a 36-core machine while link `clang.exe` (out
  1438. // of 100 sec).
  1439. std::vector<BitcodeModule *> ModulesVec;
  1440. ModulesVec.reserve(ModuleMap.size());
  1441. for (auto &Mod : ModuleMap)
  1442. ModulesVec.push_back(&Mod.second);
  1443. for (int I : generateModulesOrdering(ModulesVec))
  1444. if (Error E = ProcessOneModule(I))
  1445. return E;
  1446. }
  1447. return BackendProc->wait();
  1448. }
  1449. Expected<std::unique_ptr<ToolOutputFile>> lto::setupLLVMOptimizationRemarks(
  1450. LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
  1451. StringRef RemarksFormat, bool RemarksWithHotness,
  1452. std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
  1453. std::string Filename = std::string(RemarksFilename);
  1454. // For ThinLTO, file.opt.<format> becomes
  1455. // file.opt.<format>.thin.<num>.<format>.
  1456. if (!Filename.empty() && Count != -1)
  1457. Filename =
  1458. (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
  1459. .str();
  1460. auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
  1461. Context, Filename, RemarksPasses, RemarksFormat, RemarksWithHotness,
  1462. RemarksHotnessThreshold);
  1463. if (Error E = ResultOrErr.takeError())
  1464. return std::move(E);
  1465. if (*ResultOrErr)
  1466. (*ResultOrErr)->keep();
  1467. return ResultOrErr;
  1468. }
  1469. Expected<std::unique_ptr<ToolOutputFile>>
  1470. lto::setupStatsFile(StringRef StatsFilename) {
  1471. // Setup output file to emit statistics.
  1472. if (StatsFilename.empty())
  1473. return nullptr;
  1474. llvm::EnableStatistics(false);
  1475. std::error_code EC;
  1476. auto StatsFile =
  1477. std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
  1478. if (EC)
  1479. return errorCodeToError(EC);
  1480. StatsFile->keep();
  1481. return std::move(StatsFile);
  1482. }
  1483. // Compute the ordering we will process the inputs: the rough heuristic here
  1484. // is to sort them per size so that the largest module get schedule as soon as
  1485. // possible. This is purely a compile-time optimization.
  1486. std::vector<int> lto::generateModulesOrdering(ArrayRef<BitcodeModule *> R) {
  1487. auto Seq = llvm::seq<int>(0, R.size());
  1488. std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
  1489. llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
  1490. auto LSize = R[LeftIndex]->getBuffer().size();
  1491. auto RSize = R[RightIndex]->getBuffer().size();
  1492. return LSize > RSize;
  1493. });
  1494. return ModulesOrdering;
  1495. }