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