Path: blob/main/contrib/llvm-project/llvm/tools/llvm-lto2/llvm-lto2.cpp
35260 views
//===-- llvm-lto2: test harness for the resolution-based LTO interface ----===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This program takes in a list of bitcode files, links them and performs9// link-time optimization according to the provided symbol resolutions using the10// resolution-based LTO interface, and outputs one or more object files.11//12// This program is intended to eventually replace llvm-lto which uses the legacy13// LTO interface.14//15//===----------------------------------------------------------------------===//1617#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>3132using namespace llvm;33using namespace lto;3435static codegen::RegisterCodeGenFlags CGF;3637static cl::opt<char>38OptLevel("O",39cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "40"(default = '-O2')"),41cl::Prefix, cl::init('2'));4243static cl::opt<char> CGOptLevel(44"cg-opt-level",45cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"),46cl::init('2'));4748static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,49cl::desc("<input bitcode files>"));5051static cl::opt<std::string> OutputFilename("o", cl::Required,52cl::desc("Output filename"),53cl::value_desc("filename"));5455static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),56cl::value_desc("directory"));5758static cl::opt<std::string> OptPipeline("opt-pipeline",59cl::desc("Optimizer Pipeline"),60cl::value_desc("pipeline"));6162static cl::opt<std::string> AAPipeline("aa-pipeline",63cl::desc("Alias Analysis Pipeline"),64cl::value_desc("aapipeline"));6566static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));6768static cl::list<std::string> SelectSaveTemps(69"select-save-temps",70cl::value_desc("One, or multiple of: "71"resolution,preopt,promote,internalize,import,opt,precodegen"72",combinedindex"),73cl::desc("Save selected temporary files. Cannot be specified together with "74"-save-temps"),75cl::CommaSeparated);7677constexpr const char *SaveTempsValues[] = {78"resolution", "preopt", "promote", "internalize",79"import", "opt", "precodegen", "combinedindex"};8081static cl::opt<bool>82ThinLTODistributedIndexes("thinlto-distributed-indexes",83cl::desc("Write out individual index and "84"import files for the "85"distributed backend case"));8687static cl::opt<bool>88ThinLTOEmitIndexes("thinlto-emit-indexes",89cl::desc("Write out individual index files via "90"InProcessThinLTO"));9192static cl::opt<bool>93ThinLTOEmitImports("thinlto-emit-imports",94cl::desc("Write out individual imports files via "95"InProcessThinLTO. Has no effect unless "96"specified with -thinlto-emit-indexes or "97"-thinlto-distributed-indexes"));9899// Default to using all available threads in the system, but using only one100// thread per core (no SMT).101// Use -thinlto-threads=all to use hardware_concurrency() instead, which means102// to use all hardware threads or cores in the system.103static cl::opt<std::string> Threads("thinlto-threads");104105static cl::list<std::string> SymbolResolutions(106"r",107cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"108"where \"resolution\" is a sequence (which may be empty) of the\n"109"following characters:\n"110" p - prevailing: the linker has chosen this definition of the\n"111" symbol\n"112" l - local: the definition of this symbol is unpreemptable at\n"113" runtime and is known to be in this linkage unit\n"114" x - externally visible: the definition of this symbol is\n"115" visible outside of the LTO unit\n"116"A resolution for each symbol must be specified"));117118static cl::opt<std::string> OverrideTriple(119"override-triple",120cl::desc("Replace target triples in input files with this triple"));121122static cl::opt<std::string> DefaultTriple(123"default-triple",124cl::desc(125"Replace unspecified target triples in input files with this triple"));126127static cl::opt<bool> RemarksWithHotness(128"pass-remarks-with-hotness",129cl::desc("With PGO, include profile count in optimization remarks"),130cl::Hidden);131132cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>133RemarksHotnessThreshold(134"pass-remarks-hotness-threshold",135cl::desc("Minimum profile count required for an "136"optimization remark to be output."137" Use 'auto' to apply the threshold from profile summary."),138cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden);139140static cl::opt<std::string>141RemarksFilename("pass-remarks-output",142cl::desc("Output filename for pass remarks"),143cl::value_desc("filename"));144145static cl::opt<std::string>146RemarksPasses("pass-remarks-filter",147cl::desc("Only record optimization remarks from passes whose "148"names match the given regular expression"),149cl::value_desc("regex"));150151static cl::opt<std::string> RemarksFormat(152"pass-remarks-format",153cl::desc("The format used for serializing remarks (default: YAML)"),154cl::value_desc("format"), cl::init("yaml"));155156static cl::opt<std::string>157SamplePGOFile("lto-sample-profile-file",158cl::desc("Specify a SamplePGO profile file"));159160static cl::opt<std::string>161CSPGOFile("lto-cspgo-profile-file",162cl::desc("Specify a context sensitive PGO profile file"));163164static cl::opt<bool>165RunCSIRInstr("lto-cspgo-gen",166cl::desc("Run PGO context sensitive IR instrumentation"),167cl::Hidden);168169static cl::opt<bool>170DebugPassManager("debug-pass-manager", cl::Hidden,171cl::desc("Print pass management debugging information"));172173static cl::opt<std::string>174StatsFile("stats-file", cl::desc("Filename to write statistics to"));175176static cl::list<std::string>177PassPlugins("load-pass-plugin",178cl::desc("Load passes from plugin library"));179180static cl::opt<std::string> UnifiedLTOMode("unified-lto", cl::Optional,181cl::desc("Set LTO mode"),182cl::value_desc("mode"));183184static cl::opt<bool> EnableFreestanding(185"lto-freestanding",186cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"),187cl::Hidden);188189static cl::opt<bool> TryUseNewDbgInfoFormat(190"try-experimental-debuginfo-iterators",191cl::desc("Enable debuginfo iterator positions, if they're built in"),192cl::init(false), cl::Hidden);193194extern cl::opt<bool> UseNewDbgInfoFormat;195extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;196extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;197198static void check(Error E, std::string Msg) {199if (!E)200return;201handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {202errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';203});204exit(1);205}206207template <typename T> static T check(Expected<T> E, std::string Msg) {208if (E)209return std::move(*E);210check(E.takeError(), Msg);211return T();212}213214static void check(std::error_code EC, std::string Msg) {215check(errorCodeToError(EC), Msg);216}217218template <typename T> static T check(ErrorOr<T> E, std::string Msg) {219if (E)220return std::move(*E);221check(E.getError(), Msg);222return T();223}224225static int usage() {226errs() << "Available subcommands: dump-symtab run\n";227return 1;228}229230static int run(int argc, char **argv) {231cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");232// Load bitcode into the new debug info format by default.233if (LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_UNSET)234LoadBitcodeIntoNewDbgInfoFormat = cl::boolOrDefault::BOU_TRUE;235236// RemoveDIs debug-info transition: tests may request that we /try/ to use the237// new debug-info format.238if (TryUseNewDbgInfoFormat) {239// Turn the new debug-info format on.240UseNewDbgInfoFormat = true;241}242// Since llvm-lto2 collects multiple IR modules together, for simplicity's243// sake we disable the "PreserveInputDbgFormat" flag to enforce a single debug244// info format.245PreserveInputDbgFormat = cl::boolOrDefault::BOU_FALSE;246247// FIXME: Workaround PR30396 which means that a symbol can appear248// more than once if it is defined in module-level assembly and249// has a GV declaration. We allow (file, symbol) pairs to have multiple250// resolutions and apply them in the order observed.251std::map<std::pair<std::string, std::string>, std::list<SymbolResolution>>252CommandLineResolutions;253for (StringRef R : SymbolResolutions) {254StringRef Rest, FileName, SymbolName;255std::tie(FileName, Rest) = R.split(',');256if (Rest.empty()) {257llvm::errs() << "invalid resolution: " << R << '\n';258return 1;259}260std::tie(SymbolName, Rest) = Rest.split(',');261SymbolResolution Res;262for (char C : Rest) {263if (C == 'p')264Res.Prevailing = true;265else if (C == 'l')266Res.FinalDefinitionInLinkageUnit = true;267else if (C == 'x')268Res.VisibleToRegularObj = true;269else if (C == 'r')270Res.LinkerRedefined = true;271else {272llvm::errs() << "invalid character " << C << " in resolution: " << R273<< '\n';274return 1;275}276}277CommandLineResolutions[{std::string(FileName), std::string(SymbolName)}]278.push_back(Res);279}280281std::vector<std::unique_ptr<MemoryBuffer>> MBs;282283Config Conf;284285Conf.CPU = codegen::getMCPU();286Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple());287Conf.MAttrs = codegen::getMAttrs();288if (auto RM = codegen::getExplicitRelocModel())289Conf.RelocModel = *RM;290Conf.CodeModel = codegen::getExplicitCodeModel();291292Conf.DebugPassManager = DebugPassManager;293294if (SaveTemps && !SelectSaveTemps.empty()) {295llvm::errs() << "-save-temps cannot be specified with -select-save-temps\n";296return 1;297}298if (SaveTemps || !SelectSaveTemps.empty()) {299DenseSet<StringRef> SaveTempsArgs;300for (auto &S : SelectSaveTemps)301if (is_contained(SaveTempsValues, S))302SaveTempsArgs.insert(S);303else {304llvm::errs() << ("invalid -select-save-temps argument: " + S) << '\n';305return 1;306}307check(Conf.addSaveTemps(OutputFilename + ".", false, SaveTempsArgs),308"Config::addSaveTemps failed");309}310311// Optimization remarks.312Conf.RemarksFilename = RemarksFilename;313Conf.RemarksPasses = RemarksPasses;314Conf.RemarksWithHotness = RemarksWithHotness;315Conf.RemarksHotnessThreshold = RemarksHotnessThreshold;316Conf.RemarksFormat = RemarksFormat;317318Conf.SampleProfile = SamplePGOFile;319Conf.CSIRProfile = CSPGOFile;320Conf.RunCSIRInstr = RunCSIRInstr;321322// Run a custom pipeline, if asked for.323Conf.OptPipeline = OptPipeline;324Conf.AAPipeline = AAPipeline;325326Conf.OptLevel = OptLevel - '0';327Conf.Freestanding = EnableFreestanding;328for (auto &PluginFN : PassPlugins)329Conf.PassPlugins.push_back(PluginFN);330if (auto Level = CodeGenOpt::parseLevel(CGOptLevel)) {331Conf.CGOptLevel = *Level;332} else {333llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';334return 1;335}336337if (auto FT = codegen::getExplicitFileType())338Conf.CGFileType = *FT;339340Conf.OverrideTriple = OverrideTriple;341Conf.DefaultTriple = DefaultTriple;342Conf.StatsFile = StatsFile;343Conf.PTO.LoopVectorization = Conf.OptLevel > 1;344Conf.PTO.SLPVectorization = Conf.OptLevel > 1;345346ThinBackend Backend;347if (ThinLTODistributedIndexes)348Backend = createWriteIndexesThinBackend(/*OldPrefix=*/"",349/*NewPrefix=*/"",350/*NativeObjectPrefix=*/"",351ThinLTOEmitImports,352/*LinkedObjectsFile=*/nullptr,353/*OnWrite=*/{});354else355Backend = createInProcessThinBackend(356llvm::heavyweight_hardware_concurrency(Threads),357/* OnWrite */ {}, ThinLTOEmitIndexes, ThinLTOEmitImports);358359// Track whether we hit an error; in particular, in the multi-threaded case,360// we can't exit() early because the rest of the threads wouldn't have had a361// change to be join-ed, and that would result in a "terminate called without362// an active exception". Altogether, this results in nondeterministic363// behavior. Instead, we don't exit in the multi-threaded case, but we make364// sure to report the error and then at the end (after joining cleanly)365// exit(1).366std::atomic<bool> HasErrors;367std::atomic_init(&HasErrors, false);368Conf.DiagHandler = [&](const DiagnosticInfo &DI) {369DiagnosticPrinterRawOStream DP(errs());370DI.print(DP);371errs() << '\n';372if (DI.getSeverity() == DS_Error)373HasErrors = true;374};375376LTO::LTOKind LTOMode = LTO::LTOK_Default;377378if (UnifiedLTOMode == "full") {379LTOMode = LTO::LTOK_UnifiedRegular;380} else if (UnifiedLTOMode == "thin") {381LTOMode = LTO::LTOK_UnifiedThin;382} else if (UnifiedLTOMode == "default") {383LTOMode = LTO::LTOK_Default;384} else if (!UnifiedLTOMode.empty()) {385llvm::errs() << "invalid LTO mode\n";386return 1;387}388389LTO Lto(std::move(Conf), std::move(Backend), 1, LTOMode);390391for (std::string F : InputFilenames) {392std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);393std::unique_ptr<InputFile> Input =394check(InputFile::create(MB->getMemBufferRef()), F);395396std::vector<SymbolResolution> Res;397for (const InputFile::Symbol &Sym : Input->symbols()) {398auto I = CommandLineResolutions.find({F, std::string(Sym.getName())});399// If it isn't found, look for ".", which would have been added400// (followed by a hash) when the symbol was promoted during module401// splitting if it was defined in one part and used in the other.402// Try looking up the symbol name before the suffix.403if (I == CommandLineResolutions.end()) {404auto SplitName = Sym.getName().rsplit(".");405I = CommandLineResolutions.find({F, std::string(SplitName.first)});406}407if (I == CommandLineResolutions.end()) {408llvm::errs() << argv[0] << ": missing symbol resolution for " << F409<< ',' << Sym.getName() << '\n';410HasErrors = true;411} else {412Res.push_back(I->second.front());413I->second.pop_front();414if (I->second.empty())415CommandLineResolutions.erase(I);416}417}418419if (HasErrors)420continue;421422MBs.push_back(std::move(MB));423check(Lto.add(std::move(Input), Res), F);424}425426if (!CommandLineResolutions.empty()) {427HasErrors = true;428for (auto UnusedRes : CommandLineResolutions)429llvm::errs() << argv[0] << ": unused symbol resolution for "430<< UnusedRes.first.first << ',' << UnusedRes.first.second431<< '\n';432}433if (HasErrors)434return 1;435436auto AddStream =437[&](size_t Task,438const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {439std::string Path = OutputFilename + "." + utostr(Task);440441std::error_code EC;442auto S = std::make_unique<raw_fd_ostream>(Path, EC, sys::fs::OF_None);443check(EC, Path);444return std::make_unique<CachedFileStream>(std::move(S), Path);445};446447auto AddBuffer = [&](size_t Task, const Twine &ModuleName,448std::unique_ptr<MemoryBuffer> MB) {449*AddStream(Task, ModuleName)->OS << MB->getBuffer();450};451452FileCache Cache;453if (!CacheDir.empty())454Cache = check(localCache("ThinLTO", "Thin", CacheDir, AddBuffer),455"failed to create cache");456457check(Lto.run(AddStream, Cache), "LTO::run failed");458return static_cast<int>(HasErrors);459}460461static int dumpSymtab(int argc, char **argv) {462for (StringRef F : make_range(argv + 1, argv + argc)) {463std::unique_ptr<MemoryBuffer> MB =464check(MemoryBuffer::getFile(F), std::string(F));465BitcodeFileContents BFC =466check(getBitcodeFileContents(*MB), std::string(F));467468if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {469auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(470BFC.Symtab.data());471outs() << "version: " << Hdr->Version << '\n';472if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)473outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)474<< '\n';475}476477std::unique_ptr<InputFile> Input =478check(InputFile::create(MB->getMemBufferRef()), std::string(F));479480outs() << "target triple: " << Input->getTargetTriple() << '\n';481Triple TT(Input->getTargetTriple());482483outs() << "source filename: " << Input->getSourceFileName() << '\n';484485if (TT.isOSBinFormatCOFF())486outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';487488if (TT.isOSBinFormatELF()) {489outs() << "dependent libraries:";490for (auto L : Input->getDependentLibraries())491outs() << " \"" << L << "\"";492outs() << '\n';493}494495ArrayRef<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable =496Input->getComdatTable();497for (const InputFile::Symbol &Sym : Input->symbols()) {498switch (Sym.getVisibility()) {499case GlobalValue::HiddenVisibility:500outs() << 'H';501break;502case GlobalValue::ProtectedVisibility:503outs() << 'P';504break;505case GlobalValue::DefaultVisibility:506outs() << 'D';507break;508}509510auto PrintBool = [&](char C, bool B) { outs() << (B ? C : '-'); };511PrintBool('U', Sym.isUndefined());512PrintBool('C', Sym.isCommon());513PrintBool('W', Sym.isWeak());514PrintBool('I', Sym.isIndirect());515PrintBool('O', Sym.canBeOmittedFromSymbolTable());516PrintBool('T', Sym.isTLS());517PrintBool('X', Sym.isExecutable());518outs() << ' ' << Sym.getName() << '\n';519520if (Sym.isCommon())521outs() << " size " << Sym.getCommonSize() << " align "522<< Sym.getCommonAlignment() << '\n';523524int Comdat = Sym.getComdatIndex();525if (Comdat != -1) {526outs() << " comdat ";527switch (ComdatTable[Comdat].second) {528case Comdat::Any:529outs() << "any";530break;531case Comdat::ExactMatch:532outs() << "exactmatch";533break;534case Comdat::Largest:535outs() << "largest";536break;537case Comdat::NoDeduplicate:538outs() << "nodeduplicate";539break;540case Comdat::SameSize:541outs() << "samesize";542break;543}544outs() << ' ' << ComdatTable[Comdat].first << '\n';545}546547if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())548outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';549550if (!Sym.getSectionName().empty())551outs() << " section " << Sym.getSectionName() << "\n";552}553554outs() << '\n';555}556557return 0;558}559560int main(int argc, char **argv) {561InitLLVM X(argc, argv);562InitializeAllTargets();563InitializeAllTargetMCs();564InitializeAllAsmPrinters();565InitializeAllAsmParsers();566567// FIXME: This should use llvm::cl subcommands, but it isn't currently568// possible to pass an argument not associated with a subcommand to a569// subcommand (e.g. -use-new-pm).570if (argc < 2)571return usage();572573StringRef Subcommand = argv[1];574// Ensure that argv[0] is correct after adjusting argv/argc.575argv[1] = argv[0];576if (Subcommand == "dump-symtab")577return dumpSymtab(argc - 1, argv + 1);578if (Subcommand == "run")579return run(argc - 1, argv + 1);580return usage();581}582583584