LTO.cpp 64 KB

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