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" |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/CommandFlags.h" |
Mehdi Amini | 14f19bd | 2016-12-23 23:54:17 +0000 | [diff] [blame^] | 21 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 22 | #include "llvm/LTO/LTO.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/TargetSelect.h" |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Threading.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | using namespace lto; |
| 29 | using namespace object; |
| 30 | |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 31 | static cl::opt<char> |
| 32 | OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 33 | "(default = '-O2')"), |
| 34 | cl::Prefix, cl::ZeroOrMore, cl::init('2')); |
| 35 | |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 36 | static cl::opt<char> CGOptLevel( |
| 37 | "cg-opt-level", |
| 38 | cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"), |
| 39 | cl::init('2')); |
| 40 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 41 | static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore, |
| 42 | cl::desc("<input bitcode files>")); |
| 43 | |
| 44 | static cl::opt<std::string> OutputFilename("o", cl::Required, |
| 45 | cl::desc("Output filename"), |
| 46 | cl::value_desc("filename")); |
| 47 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 48 | static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"), |
| 49 | cl::value_desc("directory")); |
| 50 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 51 | static cl::opt<std::string> OptPipeline("opt-pipeline", |
| 52 | cl::desc("Optimizer Pipeline"), |
| 53 | cl::value_desc("pipeline")); |
| 54 | |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 55 | static cl::opt<std::string> AAPipeline("aa-pipeline", |
| 56 | cl::desc("Alias Analysis Pipeline"), |
| 57 | cl::value_desc("aapipeline")); |
| 58 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 59 | static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files")); |
| 60 | |
Mehdi Amini | 458f805 | 2016-08-19 23:54:40 +0000 | [diff] [blame] | 61 | static cl::opt<bool> |
| 62 | ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false), |
| 63 | cl::desc("Write out individual index and " |
| 64 | "import files for the " |
| 65 | "distributed backend case")); |
| 66 | |
| 67 | static cl::opt<int> Threads("-thinlto-threads", |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 68 | cl::init(llvm::heavyweight_hardware_concurrency())); |
Mehdi Amini | 458f805 | 2016-08-19 23:54:40 +0000 | [diff] [blame] | 69 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 70 | static cl::list<std::string> SymbolResolutions( |
| 71 | "r", |
| 72 | cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n" |
| 73 | "where \"resolution\" is a sequence (which may be empty) of the\n" |
| 74 | "following characters:\n" |
| 75 | " p - prevailing: the linker has chosen this definition of the\n" |
| 76 | " symbol\n" |
| 77 | " l - local: the definition of this symbol is unpreemptable at\n" |
| 78 | " runtime and is known to be in this linkage unit\n" |
| 79 | " x - externally visible: the definition of this symbol is\n" |
| 80 | " visible outside of the LTO unit\n" |
| 81 | "A resolution for each symbol must be specified."), |
| 82 | cl::ZeroOrMore); |
| 83 | |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 84 | static cl::opt<std::string> OverrideTriple( |
| 85 | "override-triple", |
| 86 | cl::desc("Replace target triples in input files with this triple")); |
| 87 | |
| 88 | static cl::opt<std::string> DefaultTriple( |
| 89 | "default-triple", |
| 90 | cl::desc( |
| 91 | "Replace unspecified target triples in input files with this triple")); |
| 92 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 93 | static void check(Error E, std::string Msg) { |
| 94 | if (!E) |
| 95 | return; |
| 96 | handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { |
| 97 | errs() << "llvm-lto: " << Msg << ": " << EIB.message().c_str() << '\n'; |
| 98 | }); |
| 99 | exit(1); |
| 100 | } |
| 101 | |
| 102 | template <typename T> static T check(Expected<T> E, std::string Msg) { |
| 103 | if (E) |
| 104 | return std::move(*E); |
| 105 | check(E.takeError(), Msg); |
| 106 | return T(); |
| 107 | } |
| 108 | |
| 109 | static void check(std::error_code EC, std::string Msg) { |
| 110 | check(errorCodeToError(EC), Msg); |
| 111 | } |
| 112 | |
| 113 | template <typename T> static T check(ErrorOr<T> E, std::string Msg) { |
| 114 | if (E) |
| 115 | return std::move(*E); |
| 116 | check(E.getError(), Msg); |
| 117 | return T(); |
| 118 | } |
| 119 | |
| 120 | int main(int argc, char **argv) { |
| 121 | InitializeAllTargets(); |
| 122 | InitializeAllTargetMCs(); |
| 123 | InitializeAllAsmPrinters(); |
| 124 | InitializeAllAsmParsers(); |
| 125 | |
| 126 | cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness"); |
| 127 | |
Peter Collingbourne | a5b7164 | 2016-11-30 23:19:05 +0000 | [diff] [blame] | 128 | // FIXME: Workaround PR30396 which means that a symbol can appear |
| 129 | // more than once if it is defined in module-level assembly and |
| 130 | // has a GV declaration. We allow (file, symbol) pairs to have multiple |
| 131 | // resolutions and apply them in the order observed. |
| 132 | std::map<std::pair<std::string, std::string>, std::list<SymbolResolution>> |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 133 | CommandLineResolutions; |
| 134 | for (std::string R : SymbolResolutions) { |
| 135 | StringRef Rest = R; |
| 136 | StringRef FileName, SymbolName; |
| 137 | std::tie(FileName, Rest) = Rest.split(','); |
| 138 | if (Rest.empty()) { |
| 139 | llvm::errs() << "invalid resolution: " << R << '\n'; |
| 140 | return 1; |
| 141 | } |
| 142 | std::tie(SymbolName, Rest) = Rest.split(','); |
| 143 | SymbolResolution Res; |
| 144 | for (char C : Rest) { |
| 145 | if (C == 'p') |
| 146 | Res.Prevailing = true; |
| 147 | else if (C == 'l') |
| 148 | Res.FinalDefinitionInLinkageUnit = true; |
| 149 | else if (C == 'x') |
| 150 | Res.VisibleToRegularObj = true; |
| 151 | else |
| 152 | llvm::errs() << "invalid character " << C << " in resolution: " << R |
| 153 | << '\n'; |
| 154 | } |
Peter Collingbourne | a5b7164 | 2016-11-30 23:19:05 +0000 | [diff] [blame] | 155 | CommandLineResolutions[{FileName, SymbolName}].push_back(Res); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | std::vector<std::unique_ptr<MemoryBuffer>> MBs; |
| 159 | |
| 160 | Config Conf; |
Mehdi Amini | 14f19bd | 2016-12-23 23:54:17 +0000 | [diff] [blame^] | 161 | Conf.DiagHandler = [](const DiagnosticInfo &DI) { |
| 162 | DiagnosticPrinterRawOStream DP(errs()); |
| 163 | DI.print(DP); |
| 164 | errs() << '\n'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 165 | exit(1); |
| 166 | }; |
| 167 | |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 168 | Conf.CPU = MCPU; |
| 169 | Conf.Options = InitTargetOptionsFromCodeGenFlags(); |
| 170 | Conf.MAttrs = MAttrs; |
| 171 | if (auto RM = getRelocModel()) |
| 172 | Conf.RelocModel = *RM; |
| 173 | Conf.CodeModel = CMModel; |
| 174 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 175 | if (SaveTemps) |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 176 | check(Conf.addSaveTemps(OutputFilename + "."), |
| 177 | "Config::addSaveTemps failed"); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 178 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 179 | // Run a custom pipeline, if asked for. |
| 180 | Conf.OptPipeline = OptPipeline; |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 181 | Conf.AAPipeline = AAPipeline; |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 182 | |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 183 | Conf.OptLevel = OptLevel - '0'; |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 184 | switch (CGOptLevel) { |
| 185 | case '0': |
| 186 | Conf.CGOptLevel = CodeGenOpt::None; |
| 187 | break; |
| 188 | case '1': |
| 189 | Conf.CGOptLevel = CodeGenOpt::Less; |
| 190 | break; |
| 191 | case '2': |
| 192 | Conf.CGOptLevel = CodeGenOpt::Default; |
| 193 | break; |
| 194 | case '3': |
| 195 | Conf.CGOptLevel = CodeGenOpt::Aggressive; |
| 196 | break; |
| 197 | default: |
| 198 | llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n'; |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | Conf.OverrideTriple = OverrideTriple; |
| 203 | Conf.DefaultTriple = DefaultTriple; |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 204 | |
Mehdi Amini | 458f805 | 2016-08-19 23:54:40 +0000 | [diff] [blame] | 205 | ThinBackend Backend; |
| 206 | if (ThinLTODistributedIndexes) |
| 207 | Backend = createWriteIndexesThinBackend("", "", true, ""); |
| 208 | else |
| 209 | Backend = createInProcessThinBackend(Threads); |
| 210 | LTO Lto(std::move(Conf), std::move(Backend)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 211 | |
| 212 | bool HasErrors = false; |
| 213 | for (std::string F : InputFilenames) { |
| 214 | std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F); |
| 215 | std::unique_ptr<InputFile> Input = |
| 216 | check(InputFile::create(MB->getMemBufferRef()), F); |
| 217 | |
| 218 | std::vector<SymbolResolution> Res; |
| 219 | for (const InputFile::Symbol &Sym : Input->symbols()) { |
| 220 | auto I = CommandLineResolutions.find({F, Sym.getName()}); |
| 221 | if (I == CommandLineResolutions.end()) { |
| 222 | llvm::errs() << argv[0] << ": missing symbol resolution for " << F |
| 223 | << ',' << Sym.getName() << '\n'; |
| 224 | HasErrors = true; |
| 225 | } else { |
Peter Collingbourne | a5b7164 | 2016-11-30 23:19:05 +0000 | [diff] [blame] | 226 | Res.push_back(I->second.front()); |
| 227 | I->second.pop_front(); |
| 228 | if (I->second.empty()) |
| 229 | CommandLineResolutions.erase(I); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | if (HasErrors) |
| 234 | continue; |
| 235 | |
| 236 | MBs.push_back(std::move(MB)); |
| 237 | check(Lto.add(std::move(Input), Res), F); |
| 238 | } |
| 239 | |
| 240 | if (!CommandLineResolutions.empty()) { |
| 241 | HasErrors = true; |
| 242 | for (auto UnusedRes : CommandLineResolutions) |
| 243 | llvm::errs() << argv[0] << ": unused symbol resolution for " |
| 244 | << UnusedRes.first.first << ',' << UnusedRes.first.second |
| 245 | << '\n'; |
| 246 | } |
| 247 | if (HasErrors) |
| 248 | return 1; |
| 249 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 250 | auto AddStream = |
| 251 | [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 252 | std::string Path = OutputFilename + "." + utostr(Task); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 253 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 254 | std::error_code EC; |
| 255 | auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None); |
| 256 | check(EC, Path); |
| 257 | return llvm::make_unique<lto::NativeObjectStream>(std::move(S)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 260 | auto AddFile = [&](size_t Task, StringRef Path) { |
| 261 | auto ReloadedBufferOrErr = MemoryBuffer::getFile(Path); |
| 262 | if (auto EC = ReloadedBufferOrErr.getError()) |
| 263 | report_fatal_error(Twine("Can't reload cached file '") + Path + "': " + |
| 264 | EC.message() + "\n"); |
| 265 | |
| 266 | *AddStream(Task)->OS << (*ReloadedBufferOrErr)->getBuffer(); |
| 267 | }; |
| 268 | |
| 269 | NativeObjectCache Cache; |
| 270 | if (!CacheDir.empty()) |
| 271 | Cache = localCache(CacheDir, AddFile); |
| 272 | |
| 273 | check(Lto.run(AddStream, Cache), "LTO::run failed"); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 274 | } |