Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1 | //===-- llvm-lto2: test harness for the resolution-based LTO interface ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This program takes in a list of bitcode files, links them and performs |
| 11 | // link-time optimization according to the provided symbol resolutions using the |
| 12 | // resolution-based LTO interface, and outputs one or more object files. |
| 13 | // |
| 14 | // This program is intended to eventually replace llvm-lto which uses the legacy |
| 15 | // LTO interface. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "llvm/LTO/LTO.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/TargetSelect.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | using namespace lto; |
| 25 | using namespace object; |
| 26 | |
| 27 | static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore, |
| 28 | cl::desc("<input bitcode files>")); |
| 29 | |
| 30 | static cl::opt<std::string> OutputFilename("o", cl::Required, |
| 31 | cl::desc("Output filename"), |
| 32 | cl::value_desc("filename")); |
| 33 | |
| 34 | static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files")); |
| 35 | |
Mehdi Amini | 458f805 | 2016-08-19 23:54:40 +0000 | [diff] [blame^] | 36 | static cl::opt<bool> |
| 37 | ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false), |
| 38 | cl::desc("Write out individual index and " |
| 39 | "import files for the " |
| 40 | "distributed backend case")); |
| 41 | |
| 42 | static cl::opt<int> Threads("-thinlto-threads", |
| 43 | cl::init(thread::hardware_concurrency())); |
| 44 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 45 | static cl::list<std::string> SymbolResolutions( |
| 46 | "r", |
| 47 | cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n" |
| 48 | "where \"resolution\" is a sequence (which may be empty) of the\n" |
| 49 | "following characters:\n" |
| 50 | " p - prevailing: the linker has chosen this definition of the\n" |
| 51 | " symbol\n" |
| 52 | " l - local: the definition of this symbol is unpreemptable at\n" |
| 53 | " runtime and is known to be in this linkage unit\n" |
| 54 | " x - externally visible: the definition of this symbol is\n" |
| 55 | " visible outside of the LTO unit\n" |
| 56 | "A resolution for each symbol must be specified."), |
| 57 | cl::ZeroOrMore); |
| 58 | |
| 59 | static void check(Error E, std::string Msg) { |
| 60 | if (!E) |
| 61 | return; |
| 62 | handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { |
| 63 | errs() << "llvm-lto: " << Msg << ": " << EIB.message().c_str() << '\n'; |
| 64 | }); |
| 65 | exit(1); |
| 66 | } |
| 67 | |
| 68 | template <typename T> static T check(Expected<T> E, std::string Msg) { |
| 69 | if (E) |
| 70 | return std::move(*E); |
| 71 | check(E.takeError(), Msg); |
| 72 | return T(); |
| 73 | } |
| 74 | |
| 75 | static void check(std::error_code EC, std::string Msg) { |
| 76 | check(errorCodeToError(EC), Msg); |
| 77 | } |
| 78 | |
| 79 | template <typename T> static T check(ErrorOr<T> E, std::string Msg) { |
| 80 | if (E) |
| 81 | return std::move(*E); |
| 82 | check(E.getError(), Msg); |
| 83 | return T(); |
| 84 | } |
| 85 | |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 86 | namespace { |
| 87 | // Define the LTOOutput handling |
| 88 | class LTOOutput : public lto::NativeObjectOutput { |
Chandler Carruth | 5f6d73b | 2016-08-17 07:48:34 +0000 | [diff] [blame] | 89 | std::string Path; |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 90 | |
| 91 | public: |
Chandler Carruth | 5f6d73b | 2016-08-17 07:48:34 +0000 | [diff] [blame] | 92 | LTOOutput(std::string Path) : Path(std::move(Path)) {} |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 93 | std::unique_ptr<raw_pwrite_stream> getStream() override { |
| 94 | std::error_code EC; |
| 95 | auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None); |
| 96 | check(EC, Path); |
| 97 | return std::move(S); |
| 98 | } |
| 99 | }; |
| 100 | } |
| 101 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 102 | int main(int argc, char **argv) { |
| 103 | InitializeAllTargets(); |
| 104 | InitializeAllTargetMCs(); |
| 105 | InitializeAllAsmPrinters(); |
| 106 | InitializeAllAsmParsers(); |
| 107 | |
| 108 | cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness"); |
| 109 | |
| 110 | std::map<std::pair<std::string, std::string>, SymbolResolution> |
| 111 | CommandLineResolutions; |
| 112 | for (std::string R : SymbolResolutions) { |
| 113 | StringRef Rest = R; |
| 114 | StringRef FileName, SymbolName; |
| 115 | std::tie(FileName, Rest) = Rest.split(','); |
| 116 | if (Rest.empty()) { |
| 117 | llvm::errs() << "invalid resolution: " << R << '\n'; |
| 118 | return 1; |
| 119 | } |
| 120 | std::tie(SymbolName, Rest) = Rest.split(','); |
| 121 | SymbolResolution Res; |
| 122 | for (char C : Rest) { |
| 123 | if (C == 'p') |
| 124 | Res.Prevailing = true; |
| 125 | else if (C == 'l') |
| 126 | Res.FinalDefinitionInLinkageUnit = true; |
| 127 | else if (C == 'x') |
| 128 | Res.VisibleToRegularObj = true; |
| 129 | else |
| 130 | llvm::errs() << "invalid character " << C << " in resolution: " << R |
| 131 | << '\n'; |
| 132 | } |
| 133 | CommandLineResolutions[{FileName, SymbolName}] = Res; |
| 134 | } |
| 135 | |
| 136 | std::vector<std::unique_ptr<MemoryBuffer>> MBs; |
| 137 | |
| 138 | Config Conf; |
| 139 | Conf.DiagHandler = [](const DiagnosticInfo &) { |
| 140 | exit(1); |
| 141 | }; |
| 142 | |
| 143 | if (SaveTemps) |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 144 | check(Conf.addSaveTemps(OutputFilename + "."), |
| 145 | "Config::addSaveTemps failed"); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 146 | |
Mehdi Amini | 458f805 | 2016-08-19 23:54:40 +0000 | [diff] [blame^] | 147 | ThinBackend Backend; |
| 148 | if (ThinLTODistributedIndexes) |
| 149 | Backend = createWriteIndexesThinBackend("", "", true, ""); |
| 150 | else |
| 151 | Backend = createInProcessThinBackend(Threads); |
| 152 | LTO Lto(std::move(Conf), std::move(Backend)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 153 | |
| 154 | bool HasErrors = false; |
| 155 | for (std::string F : InputFilenames) { |
| 156 | std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F); |
| 157 | std::unique_ptr<InputFile> Input = |
| 158 | check(InputFile::create(MB->getMemBufferRef()), F); |
| 159 | |
| 160 | std::vector<SymbolResolution> Res; |
| 161 | for (const InputFile::Symbol &Sym : Input->symbols()) { |
| 162 | auto I = CommandLineResolutions.find({F, Sym.getName()}); |
| 163 | if (I == CommandLineResolutions.end()) { |
| 164 | llvm::errs() << argv[0] << ": missing symbol resolution for " << F |
| 165 | << ',' << Sym.getName() << '\n'; |
| 166 | HasErrors = true; |
| 167 | } else { |
| 168 | Res.push_back(I->second); |
| 169 | CommandLineResolutions.erase(I); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (HasErrors) |
| 174 | continue; |
| 175 | |
| 176 | MBs.push_back(std::move(MB)); |
| 177 | check(Lto.add(std::move(Input), Res), F); |
| 178 | } |
| 179 | |
| 180 | if (!CommandLineResolutions.empty()) { |
| 181 | HasErrors = true; |
| 182 | for (auto UnusedRes : CommandLineResolutions) |
| 183 | llvm::errs() << argv[0] << ": unused symbol resolution for " |
| 184 | << UnusedRes.first.first << ',' << UnusedRes.first.second |
| 185 | << '\n'; |
| 186 | } |
| 187 | if (HasErrors) |
| 188 | return 1; |
| 189 | |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 190 | auto AddOutput = [&](size_t Task) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 191 | std::string Path = OutputFilename + "." + utostr(Task); |
Chandler Carruth | 5f6d73b | 2016-08-17 07:48:34 +0000 | [diff] [blame] | 192 | return llvm::make_unique<LTOOutput>(std::move(Path)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 195 | check(Lto.run(AddOutput), "LTO::run failed"); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 196 | } |