llvm-lto2.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. //===-- llvm-lto2: test harness for the resolution-based LTO interface ----===//
  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 program takes in a list of bitcode files, links them and performs
  10. // link-time optimization according to the provided symbol resolutions using the
  11. // resolution-based LTO interface, and outputs one or more object files.
  12. //
  13. // This program is intended to eventually replace llvm-lto which uses the legacy
  14. // LTO interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/Bitcode/BitcodeReader.h"
  18. #include "llvm/CodeGen/CommandFlags.h"
  19. #include "llvm/IR/DiagnosticPrinter.h"
  20. #include "llvm/LTO/LTO.h"
  21. #include "llvm/Passes/PassPlugin.h"
  22. #include "llvm/Remarks/HotnessThresholdParser.h"
  23. #include "llvm/Support/Caching.h"
  24. #include "llvm/Support/CommandLine.h"
  25. #include "llvm/Support/FileSystem.h"
  26. #include "llvm/Support/InitLLVM.h"
  27. #include "llvm/Support/PluginLoader.h"
  28. #include "llvm/Support/TargetSelect.h"
  29. #include "llvm/Support/Threading.h"
  30. #include <atomic>
  31. using namespace llvm;
  32. using namespace lto;
  33. static codegen::RegisterCodeGenFlags CGF;
  34. static cl::opt<char>
  35. OptLevel("O",
  36. cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
  37. "(default = '-O2')"),
  38. cl::Prefix, cl::init('2'));
  39. static cl::opt<char> CGOptLevel(
  40. "cg-opt-level",
  41. cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"),
  42. cl::init('2'));
  43. static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
  44. cl::desc("<input bitcode files>"));
  45. static cl::opt<std::string> OutputFilename("o", cl::Required,
  46. cl::desc("Output filename"),
  47. cl::value_desc("filename"));
  48. static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
  49. cl::value_desc("directory"));
  50. static cl::opt<std::string> OptPipeline("opt-pipeline",
  51. cl::desc("Optimizer Pipeline"),
  52. cl::value_desc("pipeline"));
  53. static cl::opt<std::string> AAPipeline("aa-pipeline",
  54. cl::desc("Alias Analysis Pipeline"),
  55. cl::value_desc("aapipeline"));
  56. static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
  57. static cl::list<std::string> SelectSaveTemps(
  58. "select-save-temps",
  59. cl::value_desc("One, or multiple of: "
  60. "resolution,preopt,promote,internalize,import,opt,precodegen"
  61. ",combinedindex"),
  62. cl::desc("Save selected temporary files. Cannot be specified together with "
  63. "-save-temps"),
  64. cl::CommaSeparated);
  65. constexpr const char *SaveTempsValues[] = {
  66. "resolution", "preopt", "promote", "internalize",
  67. "import", "opt", "precodegen", "combinedindex"};
  68. static cl::opt<bool>
  69. ThinLTODistributedIndexes("thinlto-distributed-indexes",
  70. cl::desc("Write out individual index and "
  71. "import files for the "
  72. "distributed backend case"));
  73. static cl::opt<bool>
  74. ThinLTOEmitIndexes("thinlto-emit-indexes",
  75. cl::desc("Write out individual index files via "
  76. "InProcessThinLTO"));
  77. static cl::opt<bool>
  78. ThinLTOEmitImports("thinlto-emit-imports",
  79. cl::desc("Write out individual imports files via "
  80. "InProcessThinLTO. Has no effect unless "
  81. "specified with -thinlto-emit-indexes or "
  82. "-thinlto-distributed-indexes"));
  83. // Default to using all available threads in the system, but using only one
  84. // thread per core (no SMT).
  85. // Use -thinlto-threads=all to use hardware_concurrency() instead, which means
  86. // to use all hardware threads or cores in the system.
  87. static cl::opt<std::string> Threads("thinlto-threads");
  88. static cl::list<std::string> SymbolResolutions(
  89. "r",
  90. cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
  91. "where \"resolution\" is a sequence (which may be empty) of the\n"
  92. "following characters:\n"
  93. " p - prevailing: the linker has chosen this definition of the\n"
  94. " symbol\n"
  95. " l - local: the definition of this symbol is unpreemptable at\n"
  96. " runtime and is known to be in this linkage unit\n"
  97. " x - externally visible: the definition of this symbol is\n"
  98. " visible outside of the LTO unit\n"
  99. "A resolution for each symbol must be specified"));
  100. static cl::opt<std::string> OverrideTriple(
  101. "override-triple",
  102. cl::desc("Replace target triples in input files with this triple"));
  103. static cl::opt<std::string> DefaultTriple(
  104. "default-triple",
  105. cl::desc(
  106. "Replace unspecified target triples in input files with this triple"));
  107. static cl::opt<bool> RemarksWithHotness(
  108. "pass-remarks-with-hotness",
  109. cl::desc("With PGO, include profile count in optimization remarks"),
  110. cl::Hidden);
  111. cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
  112. RemarksHotnessThreshold(
  113. "pass-remarks-hotness-threshold",
  114. cl::desc("Minimum profile count required for an "
  115. "optimization remark to be output."
  116. " Use 'auto' to apply the threshold from profile summary."),
  117. cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden);
  118. static cl::opt<std::string>
  119. RemarksFilename("pass-remarks-output",
  120. cl::desc("Output filename for pass remarks"),
  121. cl::value_desc("filename"));
  122. static cl::opt<std::string>
  123. RemarksPasses("pass-remarks-filter",
  124. cl::desc("Only record optimization remarks from passes whose "
  125. "names match the given regular expression"),
  126. cl::value_desc("regex"));
  127. static cl::opt<std::string> RemarksFormat(
  128. "pass-remarks-format",
  129. cl::desc("The format used for serializing remarks (default: YAML)"),
  130. cl::value_desc("format"), cl::init("yaml"));
  131. static cl::opt<std::string>
  132. SamplePGOFile("lto-sample-profile-file",
  133. cl::desc("Specify a SamplePGO profile file"));
  134. static cl::opt<std::string>
  135. CSPGOFile("lto-cspgo-profile-file",
  136. cl::desc("Specify a context sensitive PGO profile file"));
  137. static cl::opt<bool>
  138. RunCSIRInstr("lto-cspgo-gen",
  139. cl::desc("Run PGO context sensitive IR instrumentation"),
  140. cl::Hidden);
  141. static cl::opt<bool> LtoOpaquePointers("lto-opaque-pointers",
  142. cl::desc("Enable opaque pointer types"),
  143. cl::init(true), cl::Hidden);
  144. static cl::opt<bool>
  145. DebugPassManager("debug-pass-manager", cl::Hidden,
  146. cl::desc("Print pass management debugging information"));
  147. static cl::opt<std::string>
  148. StatsFile("stats-file", cl::desc("Filename to write statistics to"));
  149. static cl::list<std::string>
  150. PassPlugins("load-pass-plugin",
  151. cl::desc("Load passes from plugin library"));
  152. static cl::opt<bool> EnableFreestanding(
  153. "lto-freestanding",
  154. cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"),
  155. cl::Hidden);
  156. static void check(Error E, std::string Msg) {
  157. if (!E)
  158. return;
  159. handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
  160. errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';
  161. });
  162. exit(1);
  163. }
  164. template <typename T> static T check(Expected<T> E, std::string Msg) {
  165. if (E)
  166. return std::move(*E);
  167. check(E.takeError(), Msg);
  168. return T();
  169. }
  170. static void check(std::error_code EC, std::string Msg) {
  171. check(errorCodeToError(EC), Msg);
  172. }
  173. template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
  174. if (E)
  175. return std::move(*E);
  176. check(E.getError(), Msg);
  177. return T();
  178. }
  179. static int usage() {
  180. errs() << "Available subcommands: dump-symtab run\n";
  181. return 1;
  182. }
  183. static int run(int argc, char **argv) {
  184. cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
  185. // FIXME: Workaround PR30396 which means that a symbol can appear
  186. // more than once if it is defined in module-level assembly and
  187. // has a GV declaration. We allow (file, symbol) pairs to have multiple
  188. // resolutions and apply them in the order observed.
  189. std::map<std::pair<std::string, std::string>, std::list<SymbolResolution>>
  190. CommandLineResolutions;
  191. for (std::string R : SymbolResolutions) {
  192. StringRef Rest = R;
  193. StringRef FileName, SymbolName;
  194. std::tie(FileName, Rest) = Rest.split(',');
  195. if (Rest.empty()) {
  196. llvm::errs() << "invalid resolution: " << R << '\n';
  197. return 1;
  198. }
  199. std::tie(SymbolName, Rest) = Rest.split(',');
  200. SymbolResolution Res;
  201. for (char C : Rest) {
  202. if (C == 'p')
  203. Res.Prevailing = true;
  204. else if (C == 'l')
  205. Res.FinalDefinitionInLinkageUnit = true;
  206. else if (C == 'x')
  207. Res.VisibleToRegularObj = true;
  208. else if (C == 'r')
  209. Res.LinkerRedefined = true;
  210. else {
  211. llvm::errs() << "invalid character " << C << " in resolution: " << R
  212. << '\n';
  213. return 1;
  214. }
  215. }
  216. CommandLineResolutions[{std::string(FileName), std::string(SymbolName)}]
  217. .push_back(Res);
  218. }
  219. std::vector<std::unique_ptr<MemoryBuffer>> MBs;
  220. Config Conf;
  221. Conf.CPU = codegen::getMCPU();
  222. Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple());
  223. Conf.MAttrs = codegen::getMAttrs();
  224. if (auto RM = codegen::getExplicitRelocModel())
  225. Conf.RelocModel = *RM;
  226. Conf.CodeModel = codegen::getExplicitCodeModel();
  227. Conf.DebugPassManager = DebugPassManager;
  228. if (SaveTemps && !SelectSaveTemps.empty()) {
  229. llvm::errs() << "-save-temps cannot be specified with -select-save-temps\n";
  230. return 1;
  231. }
  232. if (SaveTemps || !SelectSaveTemps.empty()) {
  233. DenseSet<StringRef> SaveTempsArgs;
  234. for (auto &S : SelectSaveTemps)
  235. if (is_contained(SaveTempsValues, S))
  236. SaveTempsArgs.insert(S);
  237. else {
  238. llvm::errs() << ("invalid -select-save-temps argument: " + S) << '\n';
  239. return 1;
  240. }
  241. check(Conf.addSaveTemps(OutputFilename + ".", false, SaveTempsArgs),
  242. "Config::addSaveTemps failed");
  243. }
  244. // Optimization remarks.
  245. Conf.RemarksFilename = RemarksFilename;
  246. Conf.RemarksPasses = RemarksPasses;
  247. Conf.RemarksWithHotness = RemarksWithHotness;
  248. Conf.RemarksHotnessThreshold = RemarksHotnessThreshold;
  249. Conf.RemarksFormat = RemarksFormat;
  250. Conf.SampleProfile = SamplePGOFile;
  251. Conf.CSIRProfile = CSPGOFile;
  252. Conf.RunCSIRInstr = RunCSIRInstr;
  253. // Run a custom pipeline, if asked for.
  254. Conf.OptPipeline = OptPipeline;
  255. Conf.AAPipeline = AAPipeline;
  256. Conf.OptLevel = OptLevel - '0';
  257. Conf.Freestanding = EnableFreestanding;
  258. for (auto &PluginFN : PassPlugins)
  259. Conf.PassPlugins.push_back(PluginFN);
  260. if (auto Level = CodeGenOpt::parseLevel(CGOptLevel)) {
  261. Conf.CGOptLevel = *Level;
  262. } else {
  263. llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
  264. return 1;
  265. }
  266. if (auto FT = codegen::getExplicitFileType())
  267. Conf.CGFileType = *FT;
  268. Conf.OverrideTriple = OverrideTriple;
  269. Conf.DefaultTriple = DefaultTriple;
  270. Conf.StatsFile = StatsFile;
  271. Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
  272. Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
  273. Conf.OpaquePointers = LtoOpaquePointers;
  274. ThinBackend Backend;
  275. if (ThinLTODistributedIndexes)
  276. Backend =
  277. createWriteIndexesThinBackend(/* OldPrefix */ "",
  278. /* NewPrefix */ "", ThinLTOEmitImports,
  279. /* LinkedObjectsFile */ nullptr,
  280. /* OnWrite */ {});
  281. else
  282. Backend = createInProcessThinBackend(
  283. llvm::heavyweight_hardware_concurrency(Threads),
  284. /* OnWrite */ {}, ThinLTOEmitIndexes, ThinLTOEmitImports);
  285. // Track whether we hit an error; in particular, in the multi-threaded case,
  286. // we can't exit() early because the rest of the threads wouldn't have had a
  287. // change to be join-ed, and that would result in a "terminate called without
  288. // an active exception". Altogether, this results in nondeterministic
  289. // behavior. Instead, we don't exit in the multi-threaded case, but we make
  290. // sure to report the error and then at the end (after joining cleanly)
  291. // exit(1).
  292. std::atomic<bool> HasErrors;
  293. std::atomic_init(&HasErrors, false);
  294. Conf.DiagHandler = [&](const DiagnosticInfo &DI) {
  295. DiagnosticPrinterRawOStream DP(errs());
  296. DI.print(DP);
  297. errs() << '\n';
  298. if (DI.getSeverity() == DS_Error)
  299. HasErrors = true;
  300. };
  301. LTO Lto(std::move(Conf), std::move(Backend));
  302. for (std::string F : InputFilenames) {
  303. std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
  304. std::unique_ptr<InputFile> Input =
  305. check(InputFile::create(MB->getMemBufferRef()), F);
  306. std::vector<SymbolResolution> Res;
  307. for (const InputFile::Symbol &Sym : Input->symbols()) {
  308. auto I = CommandLineResolutions.find({F, std::string(Sym.getName())});
  309. // If it isn't found, look for ".", which would have been added
  310. // (followed by a hash) when the symbol was promoted during module
  311. // splitting if it was defined in one part and used in the other.
  312. // Try looking up the symbol name before the suffix.
  313. if (I == CommandLineResolutions.end()) {
  314. auto SplitName = Sym.getName().rsplit(".");
  315. I = CommandLineResolutions.find({F, std::string(SplitName.first)});
  316. }
  317. if (I == CommandLineResolutions.end()) {
  318. llvm::errs() << argv[0] << ": missing symbol resolution for " << F
  319. << ',' << Sym.getName() << '\n';
  320. HasErrors = true;
  321. } else {
  322. Res.push_back(I->second.front());
  323. I->second.pop_front();
  324. if (I->second.empty())
  325. CommandLineResolutions.erase(I);
  326. }
  327. }
  328. if (HasErrors)
  329. continue;
  330. MBs.push_back(std::move(MB));
  331. check(Lto.add(std::move(Input), Res), F);
  332. }
  333. if (!CommandLineResolutions.empty()) {
  334. HasErrors = true;
  335. for (auto UnusedRes : CommandLineResolutions)
  336. llvm::errs() << argv[0] << ": unused symbol resolution for "
  337. << UnusedRes.first.first << ',' << UnusedRes.first.second
  338. << '\n';
  339. }
  340. if (HasErrors)
  341. return 1;
  342. auto AddStream =
  343. [&](size_t Task,
  344. const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
  345. std::string Path = OutputFilename + "." + utostr(Task);
  346. std::error_code EC;
  347. auto S = std::make_unique<raw_fd_ostream>(Path, EC, sys::fs::OF_None);
  348. check(EC, Path);
  349. return std::make_unique<CachedFileStream>(std::move(S), Path);
  350. };
  351. auto AddBuffer = [&](size_t Task, const Twine &ModuleName,
  352. std::unique_ptr<MemoryBuffer> MB) {
  353. *AddStream(Task, ModuleName)->OS << MB->getBuffer();
  354. };
  355. FileCache Cache;
  356. if (!CacheDir.empty())
  357. Cache = check(localCache("ThinLTO", "Thin", CacheDir, AddBuffer),
  358. "failed to create cache");
  359. check(Lto.run(AddStream, Cache), "LTO::run failed");
  360. return static_cast<int>(HasErrors);
  361. }
  362. static int dumpSymtab(int argc, char **argv) {
  363. for (StringRef F : make_range(argv + 1, argv + argc)) {
  364. std::unique_ptr<MemoryBuffer> MB =
  365. check(MemoryBuffer::getFile(F), std::string(F));
  366. BitcodeFileContents BFC =
  367. check(getBitcodeFileContents(*MB), std::string(F));
  368. if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
  369. auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(
  370. BFC.Symtab.data());
  371. outs() << "version: " << Hdr->Version << '\n';
  372. if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
  373. outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)
  374. << '\n';
  375. }
  376. std::unique_ptr<InputFile> Input =
  377. check(InputFile::create(MB->getMemBufferRef()), std::string(F));
  378. outs() << "target triple: " << Input->getTargetTriple() << '\n';
  379. Triple TT(Input->getTargetTriple());
  380. outs() << "source filename: " << Input->getSourceFileName() << '\n';
  381. if (TT.isOSBinFormatCOFF())
  382. outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';
  383. if (TT.isOSBinFormatELF()) {
  384. outs() << "dependent libraries:";
  385. for (auto L : Input->getDependentLibraries())
  386. outs() << " \"" << L << "\"";
  387. outs() << '\n';
  388. }
  389. ArrayRef<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable =
  390. Input->getComdatTable();
  391. for (const InputFile::Symbol &Sym : Input->symbols()) {
  392. switch (Sym.getVisibility()) {
  393. case GlobalValue::HiddenVisibility:
  394. outs() << 'H';
  395. break;
  396. case GlobalValue::ProtectedVisibility:
  397. outs() << 'P';
  398. break;
  399. case GlobalValue::DefaultVisibility:
  400. outs() << 'D';
  401. break;
  402. }
  403. auto PrintBool = [&](char C, bool B) { outs() << (B ? C : '-'); };
  404. PrintBool('U', Sym.isUndefined());
  405. PrintBool('C', Sym.isCommon());
  406. PrintBool('W', Sym.isWeak());
  407. PrintBool('I', Sym.isIndirect());
  408. PrintBool('O', Sym.canBeOmittedFromSymbolTable());
  409. PrintBool('T', Sym.isTLS());
  410. PrintBool('X', Sym.isExecutable());
  411. outs() << ' ' << Sym.getName() << '\n';
  412. if (Sym.isCommon())
  413. outs() << " size " << Sym.getCommonSize() << " align "
  414. << Sym.getCommonAlignment() << '\n';
  415. int Comdat = Sym.getComdatIndex();
  416. if (Comdat != -1) {
  417. outs() << " comdat ";
  418. switch (ComdatTable[Comdat].second) {
  419. case Comdat::Any:
  420. outs() << "any";
  421. break;
  422. case Comdat::ExactMatch:
  423. outs() << "exactmatch";
  424. break;
  425. case Comdat::Largest:
  426. outs() << "largest";
  427. break;
  428. case Comdat::NoDeduplicate:
  429. outs() << "nodeduplicate";
  430. break;
  431. case Comdat::SameSize:
  432. outs() << "samesize";
  433. break;
  434. }
  435. outs() << ' ' << ComdatTable[Comdat].first << '\n';
  436. }
  437. if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())
  438. outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';
  439. if (!Sym.getSectionName().empty())
  440. outs() << " section " << Sym.getSectionName() << "\n";
  441. }
  442. outs() << '\n';
  443. }
  444. return 0;
  445. }
  446. int main(int argc, char **argv) {
  447. InitLLVM X(argc, argv);
  448. InitializeAllTargets();
  449. InitializeAllTargetMCs();
  450. InitializeAllAsmPrinters();
  451. InitializeAllAsmParsers();
  452. // FIXME: This should use llvm::cl subcommands, but it isn't currently
  453. // possible to pass an argument not associated with a subcommand to a
  454. // subcommand (e.g. -use-new-pm).
  455. if (argc < 2)
  456. return usage();
  457. StringRef Subcommand = argv[1];
  458. // Ensure that argv[0] is correct after adjusting argv/argc.
  459. argv[1] = argv[0];
  460. if (Subcommand == "dump-symtab")
  461. return dumpSymtab(argc - 1, argv + 1);
  462. if (Subcommand == "run")
  463. return run(argc - 1, argv + 1);
  464. return usage();
  465. }