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