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