Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 1 | //===-- llvm-lto: a simple command-line program to link modules with LTO --===// |
| 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, performs link-time |
| 11 | // optimization, and outputs an object file. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSet.h" |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/ReaderWriter.h" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/CommandFlags.h" |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
Mehdi Amini | 3c0e64c | 2016-04-20 01:04:26 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Verifier.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 21 | #include "llvm/IRReader/IRReader.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 22 | #include "llvm/LTO/LTOCodeGenerator.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 23 | #include "llvm/LTO/LTOModule.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 24 | #include "llvm/LTO/ThinLTOCodeGenerator.h" |
| 25 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame^] | 29 | #include "llvm/Support/Path.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 30 | #include "llvm/Support/PrettyStackTrace.h" |
| 31 | #include "llvm/Support/Signals.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 32 | #include "llvm/Support/SourceMgr.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TargetSelect.h" |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ToolOutputFile.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 36 | #include <list> |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| 39 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 40 | static cl::opt<char> |
Davide Italiano | b10e893 | 2016-04-13 21:41:35 +0000 | [diff] [blame] | 41 | OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 42 | "(default = '-O2')"), |
| 43 | cl::Prefix, cl::ZeroOrMore, cl::init('2')); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 44 | |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 45 | static cl::opt<bool> DisableVerify( |
| 46 | "disable-verify", cl::init(false), |
| 47 | cl::desc("Do not run the verifier during the optimization pipeline")); |
| 48 | |
Davide Italiano | b10e893 | 2016-04-13 21:41:35 +0000 | [diff] [blame] | 49 | static cl::opt<bool> DisableInline("disable-inlining", cl::init(false), |
| 50 | cl::desc("Do not run the inliner pass")); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 51 | |
| 52 | static cl::opt<bool> |
Davide Italiano | b10e893 | 2016-04-13 21:41:35 +0000 | [diff] [blame] | 53 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 54 | cl::desc("Do not run the GVN load PRE pass")); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 55 | |
Davide Italiano | b10e893 | 2016-04-13 21:41:35 +0000 | [diff] [blame] | 56 | static cl::opt<bool> DisableLTOVectorization( |
| 57 | "disable-lto-vectorization", cl::init(false), |
| 58 | cl::desc("Do not run loop or slp vectorization during LTO")); |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 59 | |
Davide Italiano | b10e893 | 2016-04-13 21:41:35 +0000 | [diff] [blame] | 60 | static cl::opt<bool> UseDiagnosticHandler( |
| 61 | "use-diagnostic-handler", cl::init(false), |
| 62 | cl::desc("Use a diagnostic handler to test the handler interface")); |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 63 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 64 | static cl::opt<bool> |
| 65 | ThinLTO("thinlto", cl::init(false), |
| 66 | cl::desc("Only write combined global index for ThinLTO backends")); |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 67 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 68 | enum ThinLTOModes { |
| 69 | THINLINK, |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 70 | THINDISTRIBUTE, |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 71 | THINEMITIMPORTS, |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 72 | THINPROMOTE, |
| 73 | THINIMPORT, |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 74 | THININTERNALIZE, |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 75 | THINOPT, |
| 76 | THINCODEGEN, |
| 77 | THINALL |
| 78 | }; |
| 79 | |
| 80 | cl::opt<ThinLTOModes> ThinLTOMode( |
| 81 | "thinlto-action", cl::desc("Perform a single ThinLTO stage:"), |
| 82 | cl::values( |
| 83 | clEnumValN( |
| 84 | THINLINK, "thinlink", |
| 85 | "ThinLink: produces the index by linking only the summaries."), |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 86 | clEnumValN(THINDISTRIBUTE, "distributedindexes", |
| 87 | "Produces individual indexes for distributed backends."), |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 88 | clEnumValN(THINEMITIMPORTS, "emitimports", |
| 89 | "Emit imports files for distributed backends."), |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 90 | clEnumValN(THINPROMOTE, "promote", |
| 91 | "Perform pre-import promotion (requires -thinlto-index)."), |
| 92 | clEnumValN(THINIMPORT, "import", "Perform both promotion and " |
| 93 | "cross-module importing (requires " |
| 94 | "-thinlto-index)."), |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 95 | clEnumValN(THININTERNALIZE, "internalize", |
| 96 | "Perform internalization driven by -exported-symbol " |
| 97 | "(requires -thinlto-index)."), |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 98 | clEnumValN(THINOPT, "optimize", "Perform ThinLTO optimizations."), |
| 99 | clEnumValN(THINCODEGEN, "codegen", "CodeGen (expected to match llc)"), |
| 100 | clEnumValN(THINALL, "run", "Perform ThinLTO end-to-end"), |
| 101 | clEnumValEnd)); |
| 102 | |
| 103 | static cl::opt<std::string> |
| 104 | ThinLTOIndex("thinlto-index", |
| 105 | cl::desc("Provide the index produced by a ThinLink, required " |
| 106 | "to perform the promotion and/or importing.")); |
| 107 | |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame^] | 108 | static cl::opt<std::string> ThinLTOPrefixReplace( |
| 109 | "thinlto-prefix-replace", |
| 110 | cl::desc("Control where files for distributed backends are " |
| 111 | "created. Expects 'oldprefix:newprefix' and if path " |
| 112 | "prefix of output file is oldprefix it will be " |
| 113 | "replaced with newprefix.")); |
| 114 | |
Mehdi Amini | 03abce9 | 2016-05-05 16:33:51 +0000 | [diff] [blame] | 115 | static cl::opt<std::string> ThinLTOModuleId( |
| 116 | "thinlto-module-id", |
| 117 | cl::desc("For the module ID for the file to process, useful to " |
| 118 | "match what is in the index.")); |
| 119 | |
Mehdi Amini | ab4a8b6 | 2016-05-14 05:16:41 +0000 | [diff] [blame] | 120 | static cl::opt<std::string> |
| 121 | ThinLTOCacheDir("thinlto-cache-dir", cl::desc("Enable ThinLTO caching.")); |
| 122 | |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 123 | static cl::opt<bool> |
Davide Italiano | b10e893 | 2016-04-13 21:41:35 +0000 | [diff] [blame] | 124 | SaveModuleFile("save-merged-module", cl::init(false), |
| 125 | cl::desc("Write merged LTO module to file before CodeGen")); |
| 126 | |
| 127 | static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore, |
| 128 | cl::desc("<input bitcode files>")); |
| 129 | |
| 130 | static cl::opt<std::string> OutputFilename("o", cl::init(""), |
| 131 | cl::desc("Override output filename"), |
| 132 | cl::value_desc("filename")); |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 133 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 134 | static cl::list<std::string> ExportedSymbols( |
| 135 | "exported-symbol", |
| 136 | cl::desc("List of symbols to export from the resulting object file"), |
| 137 | cl::ZeroOrMore); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 138 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 139 | static cl::list<std::string> |
Davide Italiano | b10e893 | 2016-04-13 21:41:35 +0000 | [diff] [blame] | 140 | DSOSymbols("dso-symbol", |
| 141 | cl::desc("Symbol to put in the symtab in the resulting dso"), |
| 142 | cl::ZeroOrMore); |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 143 | |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 144 | static cl::opt<bool> ListSymbolsOnly( |
| 145 | "list-symbols-only", cl::init(false), |
| 146 | cl::desc("Instead of running LTO, list the symbols in each IR file")); |
| 147 | |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 148 | static cl::opt<bool> SetMergedModule( |
| 149 | "set-merged-module", cl::init(false), |
| 150 | cl::desc("Use the first input module as the merged module")); |
| 151 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 152 | static cl::opt<unsigned> Parallelism("j", cl::Prefix, cl::init(1), |
| 153 | cl::desc("Number of backend threads")); |
| 154 | |
Tobias Edler von Koch | 3f4f6f3e | 2016-01-18 23:35:24 +0000 | [diff] [blame] | 155 | static cl::opt<bool> RestoreGlobalsLinkage( |
| 156 | "restore-linkage", cl::init(false), |
| 157 | cl::desc("Restore original linkage of globals prior to CodeGen")); |
| 158 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 159 | namespace { |
| 160 | struct ModuleInfo { |
| 161 | std::vector<bool> CanBeHidden; |
| 162 | }; |
| 163 | } |
| 164 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 165 | static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity, |
| 166 | const char *Msg, void *) { |
Yunzhong Gao | ef436f0 | 2015-11-10 18:52:48 +0000 | [diff] [blame] | 167 | errs() << "llvm-lto: "; |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 168 | switch (Severity) { |
| 169 | case LTO_DS_NOTE: |
| 170 | errs() << "note: "; |
| 171 | break; |
| 172 | case LTO_DS_REMARK: |
| 173 | errs() << "remark: "; |
| 174 | break; |
| 175 | case LTO_DS_ERROR: |
| 176 | errs() << "error: "; |
| 177 | break; |
| 178 | case LTO_DS_WARNING: |
| 179 | errs() << "warning: "; |
| 180 | break; |
| 181 | } |
| 182 | errs() << Msg << "\n"; |
| 183 | } |
| 184 | |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 185 | static std::string CurrentActivity; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 186 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 187 | raw_ostream &OS = errs(); |
| 188 | OS << "llvm-lto: "; |
| 189 | switch (DI.getSeverity()) { |
| 190 | case DS_Error: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 191 | OS << "error"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 192 | break; |
| 193 | case DS_Warning: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 194 | OS << "warning"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 195 | break; |
| 196 | case DS_Remark: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 197 | OS << "remark"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 198 | break; |
| 199 | case DS_Note: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 200 | OS << "note"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 201 | break; |
| 202 | } |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 203 | if (!CurrentActivity.empty()) |
| 204 | OS << ' ' << CurrentActivity; |
| 205 | OS << ": "; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 206 | |
| 207 | DiagnosticPrinterRawOStream DP(OS); |
| 208 | DI.print(DP); |
| 209 | OS << '\n'; |
| 210 | |
| 211 | if (DI.getSeverity() == DS_Error) |
| 212 | exit(1); |
| 213 | } |
| 214 | |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 215 | static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, |
| 216 | void *Context) { |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 217 | diagnosticHandler(DI); |
| 218 | } |
| 219 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 220 | static void error(const Twine &Msg) { |
| 221 | errs() << "llvm-lto: " << Msg << '\n'; |
| 222 | exit(1); |
| 223 | } |
| 224 | |
| 225 | static void error(std::error_code EC, const Twine &Prefix) { |
| 226 | if (EC) |
| 227 | error(Prefix + ": " + EC.message()); |
| 228 | } |
| 229 | |
| 230 | template <typename T> |
| 231 | static void error(const ErrorOr<T> &V, const Twine &Prefix) { |
| 232 | error(V.getError(), Prefix); |
| 233 | } |
| 234 | |
Mehdi Amini | 3c0e64c | 2016-04-20 01:04:26 +0000 | [diff] [blame] | 235 | static void maybeVerifyModule(const Module &Mod) { |
| 236 | if (!DisableVerify && verifyModule(Mod)) |
| 237 | error("Broken Module"); |
| 238 | } |
| 239 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 240 | static std::unique_ptr<LTOModule> |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 241 | getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer, |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 242 | const TargetOptions &Options) { |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 243 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 244 | MemoryBuffer::getFile(Path); |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 245 | error(BufferOrErr, "error loading file '" + Path + "'"); |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 246 | Buffer = std::move(BufferOrErr.get()); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 247 | CurrentActivity = ("loading file '" + Path + "'").str(); |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 248 | std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>(); |
| 249 | Context->setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 250 | ErrorOr<std::unique_ptr<LTOModule>> Ret = LTOModule::createInLocalContext( |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 251 | std::move(Context), Buffer->getBufferStart(), Buffer->getBufferSize(), |
| 252 | Options, Path); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 253 | CurrentActivity = ""; |
Mehdi Amini | 3c0e64c | 2016-04-20 01:04:26 +0000 | [diff] [blame] | 254 | maybeVerifyModule((*Ret)->getModule()); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 255 | return std::move(*Ret); |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /// \brief List symbols in each IR file. |
| 259 | /// |
| 260 | /// The main point here is to provide lit-testable coverage for the LTOModule |
| 261 | /// functionality that's exposed by the C API to list symbols. Moreover, this |
| 262 | /// provides testing coverage for modules that have been created in their own |
| 263 | /// contexts. |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 264 | static void listSymbols(const TargetOptions &Options) { |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 265 | for (auto &Filename : InputFilenames) { |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 266 | std::unique_ptr<MemoryBuffer> Buffer; |
| 267 | std::unique_ptr<LTOModule> Module = |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 268 | getLocalLTOModule(Filename, Buffer, Options); |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 269 | |
| 270 | // List the symbols. |
| 271 | outs() << Filename << ":\n"; |
| 272 | for (int I = 0, E = Module->getSymbolCount(); I != E; ++I) |
| 273 | outs() << Module->getSymbolName(I) << "\n"; |
| 274 | } |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 277 | /// Create a combined index file from the input IR files and write it. |
| 278 | /// |
| 279 | /// This is meant to enable testing of ThinLTO combined index generation, |
| 280 | /// currently available via the gold plugin via -thinlto. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 281 | static void createCombinedModuleSummaryIndex() { |
| 282 | ModuleSummaryIndex CombinedIndex; |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 283 | uint64_t NextModuleId = 0; |
| 284 | for (auto &Filename : InputFilenames) { |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 285 | CurrentActivity = "loading file '" + Filename + "'"; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 286 | ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr = |
| 287 | llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler); |
Mehdi Amini | 155da5b | 2016-03-19 00:17:32 +0000 | [diff] [blame] | 288 | error(IndexOrErr, "error " + CurrentActivity); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 289 | std::unique_ptr<ModuleSummaryIndex> Index = std::move(IndexOrErr.get()); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 290 | CurrentActivity = ""; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 291 | // Skip files without a module summary. |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 292 | if (!Index) |
| 293 | continue; |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 294 | CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId); |
| 295 | } |
| 296 | std::error_code EC; |
| 297 | assert(!OutputFilename.empty()); |
| 298 | raw_fd_ostream OS(OutputFilename + ".thinlto.bc", EC, |
| 299 | sys::fs::OpenFlags::F_None); |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 300 | error(EC, "error opening the file '" + OutputFilename + ".thinlto.bc'"); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 301 | WriteIndexToFile(CombinedIndex, OS); |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 302 | OS.close(); |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame^] | 305 | /// Parse the thinlto_prefix_replace option into the \p OldPrefix and |
| 306 | /// \p NewPrefix strings, if it was specified. |
| 307 | static void getThinLTOOldAndNewPrefix(std::string &OldPrefix, |
| 308 | std::string &NewPrefix) { |
| 309 | assert(ThinLTOPrefixReplace.empty() || |
| 310 | ThinLTOPrefixReplace.find(":") != StringRef::npos); |
| 311 | StringRef PrefixReplace = ThinLTOPrefixReplace; |
| 312 | std::pair<StringRef, StringRef> Split = PrefixReplace.split(":"); |
| 313 | OldPrefix = Split.first.str(); |
| 314 | NewPrefix = Split.second.str(); |
| 315 | } |
| 316 | |
| 317 | /// Given the original \p Path to an output file, replace any path |
| 318 | /// prefix matching \p OldPrefix with \p NewPrefix. Also, create the |
| 319 | /// resulting directory if it does not yet exist. |
| 320 | static std::string getThinLTOOutputFile(const std::string &Path, |
| 321 | const std::string &OldPrefix, |
| 322 | const std::string &NewPrefix) { |
| 323 | if (OldPrefix.empty() && NewPrefix.empty()) |
| 324 | return Path; |
| 325 | SmallString<128> NewPath(Path); |
| 326 | llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix); |
| 327 | StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str()); |
| 328 | if (!ParentPath.empty()) { |
| 329 | // Make sure the new directory exists, creating it if necessary. |
| 330 | if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath)) |
| 331 | error(EC, "error creating the directory '" + ParentPath + "'"); |
| 332 | } |
| 333 | return NewPath.str(); |
| 334 | } |
| 335 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 336 | namespace thinlto { |
| 337 | |
| 338 | std::vector<std::unique_ptr<MemoryBuffer>> |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 339 | loadAllFilesForIndex(const ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 340 | std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers; |
| 341 | |
Mehdi Amini | 385cf28 | 2016-03-26 03:35:38 +0000 | [diff] [blame] | 342 | for (auto &ModPath : Index.modulePaths()) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 343 | const auto &Filename = ModPath.first(); |
| 344 | auto CurrentActivity = "loading file '" + Filename + "'"; |
| 345 | auto InputOrErr = MemoryBuffer::getFile(Filename); |
| 346 | error(InputOrErr, "error " + CurrentActivity); |
| 347 | InputBuffers.push_back(std::move(*InputOrErr)); |
| 348 | } |
| 349 | return InputBuffers; |
| 350 | } |
| 351 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 352 | std::unique_ptr<ModuleSummaryIndex> loadCombinedIndex() { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 353 | if (ThinLTOIndex.empty()) |
| 354 | report_fatal_error("Missing -thinlto-index for ThinLTO promotion stage"); |
| 355 | auto CurrentActivity = "loading file '" + ThinLTOIndex + "'"; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 356 | ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr = |
| 357 | llvm::getModuleSummaryIndexForFile(ThinLTOIndex, diagnosticHandler); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 358 | error(IndexOrErr, "error " + CurrentActivity); |
| 359 | return std::move(IndexOrErr.get()); |
| 360 | } |
| 361 | |
| 362 | static std::unique_ptr<Module> loadModule(StringRef Filename, |
| 363 | LLVMContext &Ctx) { |
| 364 | SMDiagnostic Err; |
| 365 | std::unique_ptr<Module> M(parseIRFile(Filename, Err, Ctx)); |
| 366 | if (!M) { |
| 367 | Err.print("llvm-lto", errs()); |
| 368 | report_fatal_error("Can't load module for file " + Filename); |
| 369 | } |
Mehdi Amini | 3c0e64c | 2016-04-20 01:04:26 +0000 | [diff] [blame] | 370 | maybeVerifyModule(*M); |
Mehdi Amini | 03abce9 | 2016-05-05 16:33:51 +0000 | [diff] [blame] | 371 | |
| 372 | if (ThinLTOModuleId.getNumOccurrences()) { |
| 373 | if (InputFilenames.size() != 1) |
| 374 | report_fatal_error("Can't override the module id for multiple files"); |
| 375 | M->setModuleIdentifier(ThinLTOModuleId); |
| 376 | } |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 377 | return M; |
| 378 | } |
| 379 | |
| 380 | static void writeModuleToFile(Module &TheModule, StringRef Filename) { |
| 381 | std::error_code EC; |
| 382 | raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None); |
| 383 | error(EC, "error opening the file '" + Filename + "'"); |
Mehdi Amini | 3c0e64c | 2016-04-20 01:04:26 +0000 | [diff] [blame] | 384 | maybeVerifyModule(TheModule); |
Teresa Johnson | 3c35e09 | 2016-04-04 21:19:31 +0000 | [diff] [blame] | 385 | WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | class ThinLTOProcessing { |
| 389 | public: |
| 390 | ThinLTOCodeGenerator ThinGenerator; |
| 391 | |
| 392 | ThinLTOProcessing(const TargetOptions &Options) { |
| 393 | ThinGenerator.setCodePICModel(RelocModel); |
| 394 | ThinGenerator.setTargetOptions(Options); |
Mehdi Amini | ab4a8b6 | 2016-05-14 05:16:41 +0000 | [diff] [blame] | 395 | ThinGenerator.setCacheDir(ThinLTOCacheDir); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 396 | |
| 397 | // Add all the exported symbols to the table of symbols to preserve. |
| 398 | for (unsigned i = 0; i < ExportedSymbols.size(); ++i) |
| 399 | ThinGenerator.preserveSymbol(ExportedSymbols[i]); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void run() { |
| 403 | switch (ThinLTOMode) { |
| 404 | case THINLINK: |
| 405 | return thinLink(); |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 406 | case THINDISTRIBUTE: |
| 407 | return distributedIndexes(); |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 408 | case THINEMITIMPORTS: |
| 409 | return emitImports(); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 410 | case THINPROMOTE: |
| 411 | return promote(); |
| 412 | case THINIMPORT: |
| 413 | return import(); |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 414 | case THININTERNALIZE: |
| 415 | return internalize(); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 416 | case THINOPT: |
| 417 | return optimize(); |
| 418 | case THINCODEGEN: |
| 419 | return codegen(); |
| 420 | case THINALL: |
| 421 | return runAll(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | private: |
| 426 | /// Load the input files, create the combined index, and write it out. |
| 427 | void thinLink() { |
| 428 | // Perform "ThinLink": just produce the index |
| 429 | if (OutputFilename.empty()) |
| 430 | report_fatal_error( |
| 431 | "OutputFilename is necessary to store the combined index.\n"); |
| 432 | |
| 433 | LLVMContext Ctx; |
| 434 | std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers; |
| 435 | for (unsigned i = 0; i < InputFilenames.size(); ++i) { |
| 436 | auto &Filename = InputFilenames[i]; |
| 437 | StringRef CurrentActivity = "loading file '" + Filename + "'"; |
| 438 | auto InputOrErr = MemoryBuffer::getFile(Filename); |
| 439 | error(InputOrErr, "error " + CurrentActivity); |
| 440 | InputBuffers.push_back(std::move(*InputOrErr)); |
| 441 | ThinGenerator.addModule(Filename, InputBuffers.back()->getBuffer()); |
| 442 | } |
| 443 | |
| 444 | auto CombinedIndex = ThinGenerator.linkCombinedIndex(); |
| 445 | std::error_code EC; |
| 446 | raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::F_None); |
| 447 | error(EC, "error opening the file '" + OutputFilename + "'"); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 448 | WriteIndexToFile(*CombinedIndex, OS); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 452 | /// Load the combined index from disk, then compute and generate |
| 453 | /// individual index files suitable for ThinLTO distributed backend builds |
| 454 | /// on the files mentioned on the command line (these must match the index |
| 455 | /// content). |
| 456 | void distributedIndexes() { |
| 457 | if (InputFilenames.size() != 1 && !OutputFilename.empty()) |
| 458 | report_fatal_error("Can't handle a single output filename and multiple " |
| 459 | "input files, do not provide an output filename and " |
| 460 | "the output files will be suffixed from the input " |
| 461 | "ones."); |
| 462 | |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame^] | 463 | std::string OldPrefix, NewPrefix; |
| 464 | getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix); |
| 465 | |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 466 | auto Index = loadCombinedIndex(); |
| 467 | for (auto &Filename : InputFilenames) { |
| 468 | // Build a map of module to the GUIDs and summary objects that should |
| 469 | // be written to its index. |
| 470 | std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; |
| 471 | ThinLTOCodeGenerator::gatherImportedSummariesForModule( |
| 472 | Filename, *Index, ModuleToSummariesForIndex); |
| 473 | |
| 474 | std::string OutputName = OutputFilename; |
| 475 | if (OutputName.empty()) { |
| 476 | OutputName = Filename + ".thinlto.bc"; |
| 477 | } |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame^] | 478 | OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix); |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 479 | std::error_code EC; |
| 480 | raw_fd_ostream OS(OutputName, EC, sys::fs::OpenFlags::F_None); |
| 481 | error(EC, "error opening the file '" + OutputName + "'"); |
| 482 | WriteIndexToFile(*Index, OS, &ModuleToSummariesForIndex); |
| 483 | } |
| 484 | } |
| 485 | |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 486 | /// Load the combined index from disk, compute the imports, and emit |
| 487 | /// the import file lists for each module to disk. |
| 488 | void emitImports() { |
| 489 | if (InputFilenames.size() != 1 && !OutputFilename.empty()) |
| 490 | report_fatal_error("Can't handle a single output filename and multiple " |
| 491 | "input files, do not provide an output filename and " |
| 492 | "the output files will be suffixed from the input " |
| 493 | "ones."); |
| 494 | |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame^] | 495 | std::string OldPrefix, NewPrefix; |
| 496 | getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix); |
| 497 | |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 498 | auto Index = loadCombinedIndex(); |
| 499 | for (auto &Filename : InputFilenames) { |
| 500 | std::string OutputName = OutputFilename; |
| 501 | if (OutputName.empty()) { |
| 502 | OutputName = Filename + ".imports"; |
| 503 | } |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame^] | 504 | OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix); |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 505 | ThinLTOCodeGenerator::emitImports(Filename, OutputName, *Index); |
| 506 | } |
| 507 | } |
| 508 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 509 | /// Load the combined index from disk, then load every file referenced by |
| 510 | /// the index and add them to the generator, finally perform the promotion |
| 511 | /// on the files mentioned on the command line (these must match the index |
| 512 | /// content). |
| 513 | void promote() { |
| 514 | if (InputFilenames.size() != 1 && !OutputFilename.empty()) |
| 515 | report_fatal_error("Can't handle a single output filename and multiple " |
| 516 | "input files, do not provide an output filename and " |
| 517 | "the output files will be suffixed from the input " |
| 518 | "ones."); |
| 519 | |
| 520 | auto Index = loadCombinedIndex(); |
| 521 | for (auto &Filename : InputFilenames) { |
| 522 | LLVMContext Ctx; |
| 523 | auto TheModule = loadModule(Filename, Ctx); |
| 524 | |
| 525 | ThinGenerator.promote(*TheModule, *Index); |
| 526 | |
| 527 | std::string OutputName = OutputFilename; |
| 528 | if (OutputName.empty()) { |
| 529 | OutputName = Filename + ".thinlto.promoted.bc"; |
| 530 | } |
| 531 | writeModuleToFile(*TheModule, OutputName); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /// Load the combined index from disk, then load every file referenced by |
| 536 | /// the index and add them to the generator, then performs the promotion and |
| 537 | /// cross module importing on the files mentioned on the command line |
| 538 | /// (these must match the index content). |
| 539 | void import() { |
| 540 | if (InputFilenames.size() != 1 && !OutputFilename.empty()) |
| 541 | report_fatal_error("Can't handle a single output filename and multiple " |
| 542 | "input files, do not provide an output filename and " |
| 543 | "the output files will be suffixed from the input " |
| 544 | "ones."); |
| 545 | |
| 546 | auto Index = loadCombinedIndex(); |
| 547 | auto InputBuffers = loadAllFilesForIndex(*Index); |
| 548 | for (auto &MemBuffer : InputBuffers) |
| 549 | ThinGenerator.addModule(MemBuffer->getBufferIdentifier(), |
| 550 | MemBuffer->getBuffer()); |
| 551 | |
| 552 | for (auto &Filename : InputFilenames) { |
| 553 | LLVMContext Ctx; |
| 554 | auto TheModule = loadModule(Filename, Ctx); |
| 555 | |
| 556 | ThinGenerator.crossModuleImport(*TheModule, *Index); |
| 557 | |
| 558 | std::string OutputName = OutputFilename; |
| 559 | if (OutputName.empty()) { |
| 560 | OutputName = Filename + ".thinlto.imported.bc"; |
| 561 | } |
| 562 | writeModuleToFile(*TheModule, OutputName); |
| 563 | } |
| 564 | } |
| 565 | |
Mehdi Amini | 059464f | 2016-04-24 03:18:01 +0000 | [diff] [blame] | 566 | void internalize() { |
| 567 | if (InputFilenames.size() != 1 && !OutputFilename.empty()) |
| 568 | report_fatal_error("Can't handle a single output filename and multiple " |
| 569 | "input files, do not provide an output filename and " |
| 570 | "the output files will be suffixed from the input " |
| 571 | "ones."); |
| 572 | |
| 573 | if (ExportedSymbols.empty()) |
| 574 | errs() << "Warning: -internalize will not perform without " |
| 575 | "-exported-symbol\n"; |
| 576 | |
| 577 | auto Index = loadCombinedIndex(); |
| 578 | auto InputBuffers = loadAllFilesForIndex(*Index); |
| 579 | for (auto &MemBuffer : InputBuffers) |
| 580 | ThinGenerator.addModule(MemBuffer->getBufferIdentifier(), |
| 581 | MemBuffer->getBuffer()); |
| 582 | |
| 583 | for (auto &Filename : InputFilenames) { |
| 584 | LLVMContext Ctx; |
| 585 | auto TheModule = loadModule(Filename, Ctx); |
| 586 | |
| 587 | ThinGenerator.internalize(*TheModule, *Index); |
| 588 | |
| 589 | std::string OutputName = OutputFilename; |
| 590 | if (OutputName.empty()) { |
| 591 | OutputName = Filename + ".thinlto.internalized.bc"; |
| 592 | } |
| 593 | writeModuleToFile(*TheModule, OutputName); |
| 594 | } |
| 595 | } |
| 596 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 597 | void optimize() { |
| 598 | if (InputFilenames.size() != 1 && !OutputFilename.empty()) |
| 599 | report_fatal_error("Can't handle a single output filename and multiple " |
| 600 | "input files, do not provide an output filename and " |
| 601 | "the output files will be suffixed from the input " |
| 602 | "ones."); |
| 603 | if (!ThinLTOIndex.empty()) |
| 604 | errs() << "Warning: -thinlto-index ignored for optimize stage"; |
| 605 | |
| 606 | for (auto &Filename : InputFilenames) { |
| 607 | LLVMContext Ctx; |
| 608 | auto TheModule = loadModule(Filename, Ctx); |
| 609 | |
| 610 | ThinGenerator.optimize(*TheModule); |
| 611 | |
| 612 | std::string OutputName = OutputFilename; |
| 613 | if (OutputName.empty()) { |
| 614 | OutputName = Filename + ".thinlto.imported.bc"; |
| 615 | } |
| 616 | writeModuleToFile(*TheModule, OutputName); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void codegen() { |
| 621 | if (InputFilenames.size() != 1 && !OutputFilename.empty()) |
| 622 | report_fatal_error("Can't handle a single output filename and multiple " |
| 623 | "input files, do not provide an output filename and " |
| 624 | "the output files will be suffixed from the input " |
| 625 | "ones."); |
| 626 | if (!ThinLTOIndex.empty()) |
| 627 | errs() << "Warning: -thinlto-index ignored for codegen stage"; |
| 628 | |
| 629 | for (auto &Filename : InputFilenames) { |
| 630 | LLVMContext Ctx; |
| 631 | auto TheModule = loadModule(Filename, Ctx); |
| 632 | |
| 633 | auto Buffer = ThinGenerator.codegen(*TheModule); |
| 634 | std::string OutputName = OutputFilename; |
| 635 | if (OutputName.empty()) { |
| 636 | OutputName = Filename + ".thinlto.o"; |
| 637 | } |
| 638 | if (OutputName == "-") { |
| 639 | outs() << Buffer->getBuffer(); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | std::error_code EC; |
| 644 | raw_fd_ostream OS(OutputName, EC, sys::fs::OpenFlags::F_None); |
| 645 | error(EC, "error opening the file '" + OutputName + "'"); |
| 646 | OS << Buffer->getBuffer(); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /// Full ThinLTO process |
| 651 | void runAll() { |
| 652 | if (!OutputFilename.empty()) |
| 653 | report_fatal_error("Do not provide an output filename for ThinLTO " |
| 654 | " processing, the output files will be suffixed from " |
| 655 | "the input ones."); |
| 656 | |
| 657 | if (!ThinLTOIndex.empty()) |
| 658 | errs() << "Warning: -thinlto-index ignored for full ThinLTO process"; |
| 659 | |
| 660 | LLVMContext Ctx; |
| 661 | std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers; |
| 662 | for (unsigned i = 0; i < InputFilenames.size(); ++i) { |
| 663 | auto &Filename = InputFilenames[i]; |
| 664 | StringRef CurrentActivity = "loading file '" + Filename + "'"; |
| 665 | auto InputOrErr = MemoryBuffer::getFile(Filename); |
| 666 | error(InputOrErr, "error " + CurrentActivity); |
| 667 | InputBuffers.push_back(std::move(*InputOrErr)); |
| 668 | ThinGenerator.addModule(Filename, InputBuffers.back()->getBuffer()); |
| 669 | } |
| 670 | |
| 671 | ThinGenerator.run(); |
| 672 | |
| 673 | auto &Binaries = ThinGenerator.getProducedBinaries(); |
| 674 | if (Binaries.size() != InputFilenames.size()) |
| 675 | report_fatal_error("Number of output objects does not match the number " |
| 676 | "of inputs"); |
| 677 | |
| 678 | for (unsigned BufID = 0; BufID < Binaries.size(); ++BufID) { |
| 679 | auto OutputName = InputFilenames[BufID] + ".thinlto.o"; |
| 680 | std::error_code EC; |
| 681 | raw_fd_ostream OS(OutputName, EC, sys::fs::OpenFlags::F_None); |
| 682 | error(EC, "error opening the file '" + OutputName + "'"); |
| 683 | OS << Binaries[BufID]->getBuffer(); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /// Load the combined index from disk, then load every file referenced by |
| 688 | }; |
| 689 | |
| 690 | } // namespace thinlto |
| 691 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 692 | int main(int argc, char **argv) { |
| 693 | // Print a stack trace if we signal out. |
| 694 | sys::PrintStackTraceOnErrorSignal(); |
| 695 | PrettyStackTraceProgram X(argc, argv); |
| 696 | |
| 697 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 698 | cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n"); |
| 699 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 700 | if (OptLevel < '0' || OptLevel > '3') |
| 701 | error("optimization level must be between 0 and 3"); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 702 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 703 | // Initialize the configured targets. |
| 704 | InitializeAllTargets(); |
| 705 | InitializeAllTargetMCs(); |
| 706 | InitializeAllAsmPrinters(); |
| 707 | InitializeAllAsmParsers(); |
| 708 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 709 | // set up the TargetOptions for the machine |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 710 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 711 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 712 | if (ListSymbolsOnly) { |
| 713 | listSymbols(Options); |
| 714 | return 0; |
| 715 | } |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 716 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 717 | if (ThinLTOMode.getNumOccurrences()) { |
| 718 | if (ThinLTOMode.getNumOccurrences() > 1) |
| 719 | report_fatal_error("You can't specify more than one -thinlto-action"); |
| 720 | thinlto::ThinLTOProcessing ThinLTOProcessor(Options); |
| 721 | ThinLTOProcessor.run(); |
| 722 | return 0; |
| 723 | } |
| 724 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 725 | if (ThinLTO) { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 726 | createCombinedModuleSummaryIndex(); |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 727 | return 0; |
| 728 | } |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 729 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 730 | unsigned BaseArg = 0; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 731 | |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 732 | LLVMContext Context; |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 733 | Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 734 | |
| 735 | LTOCodeGenerator CodeGen(Context); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 736 | |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 737 | if (UseDiagnosticHandler) |
| 738 | CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr); |
| 739 | |
Peter Collingbourne | 44ee84e | 2015-08-21 22:57:17 +0000 | [diff] [blame] | 740 | CodeGen.setCodePICModel(RelocModel); |
James Molloy | 951e529 | 2014-04-14 13:54:16 +0000 | [diff] [blame] | 741 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 742 | CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 743 | CodeGen.setTargetOptions(Options); |
Tobias Edler von Koch | 3f4f6f3e | 2016-01-18 23:35:24 +0000 | [diff] [blame] | 744 | CodeGen.setShouldRestoreGlobalsLinkage(RestoreGlobalsLinkage); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 745 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 746 | llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet; |
| 747 | for (unsigned i = 0; i < DSOSymbols.size(); ++i) |
| 748 | DSOSymbolsSet.insert(DSOSymbols[i]); |
| 749 | |
| 750 | std::vector<std::string> KeptDSOSyms; |
| 751 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 752 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 753 | CurrentActivity = "loading file '" + InputFilenames[i] + "'"; |
| 754 | ErrorOr<std::unique_ptr<LTOModule>> ModuleOrErr = |
| 755 | LTOModule::createFromFile(Context, InputFilenames[i].c_str(), Options); |
| 756 | std::unique_ptr<LTOModule> &Module = *ModuleOrErr; |
| 757 | CurrentActivity = ""; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 758 | |
Peter Collingbourne | 55217439 | 2015-08-21 19:09:42 +0000 | [diff] [blame] | 759 | unsigned NumSyms = Module->getSymbolCount(); |
| 760 | for (unsigned I = 0; I < NumSyms; ++I) { |
| 761 | StringRef Name = Module->getSymbolName(I); |
| 762 | if (!DSOSymbolsSet.count(Name)) |
| 763 | continue; |
| 764 | lto_symbol_attributes Attrs = Module->getSymbolAttributes(I); |
| 765 | unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK; |
| 766 | if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN) |
| 767 | KeptDSOSyms.push_back(Name); |
| 768 | } |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 769 | |
| 770 | // We use the first input module as the destination module when |
| 771 | // SetMergedModule is true. |
| 772 | if (SetMergedModule && i == BaseArg) { |
| 773 | // Transfer ownership to the code generator. |
Peter Collingbourne | 9c8909d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 774 | CodeGen.setModule(std::move(Module)); |
Yunzhong Gao | 46261a7 | 2015-09-11 20:01:53 +0000 | [diff] [blame] | 775 | } else if (!CodeGen.addModule(Module.get())) { |
| 776 | // Print a message here so that we know addModule() did not abort. |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 777 | error("error adding file '" + InputFilenames[i] + "'"); |
Yunzhong Gao | 46261a7 | 2015-09-11 20:01:53 +0000 | [diff] [blame] | 778 | } |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 781 | // Add all the exported symbols to the table of symbols to preserve. |
| 782 | for (unsigned i = 0; i < ExportedSymbols.size(); ++i) |
| 783 | CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str()); |
| 784 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 785 | // Add all the dso symbols to the table of symbols to expose. |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 786 | for (unsigned i = 0; i < KeptDSOSyms.size(); ++i) |
| 787 | CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str()); |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 788 | |
Akira Hatanaka | 23b5f67 | 2015-01-30 01:14:28 +0000 | [diff] [blame] | 789 | // Set cpu and attrs strings for the default target/subtarget. |
| 790 | CodeGen.setCpu(MCPU.c_str()); |
| 791 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 792 | CodeGen.setOptLevel(OptLevel - '0'); |
| 793 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 794 | std::string attrs; |
| 795 | for (unsigned i = 0; i < MAttrs.size(); ++i) { |
| 796 | if (i > 0) |
| 797 | attrs.append(","); |
| 798 | attrs.append(MAttrs[i]); |
| 799 | } |
| 800 | |
| 801 | if (!attrs.empty()) |
| 802 | CodeGen.setAttr(attrs.c_str()); |
| 803 | |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 804 | if (FileType.getNumOccurrences()) |
| 805 | CodeGen.setFileType(FileType); |
| 806 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 807 | if (!OutputFilename.empty()) { |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 808 | if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 809 | DisableLTOVectorization)) { |
| 810 | // Diagnostic messages should have been printed by the handler. |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 811 | error("error optimizing the code"); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 814 | if (SaveModuleFile) { |
| 815 | std::string ModuleFilename = OutputFilename; |
| 816 | ModuleFilename += ".merged.bc"; |
| 817 | std::string ErrMsg; |
| 818 | |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 819 | if (!CodeGen.writeMergedModules(ModuleFilename.c_str())) |
| 820 | error("writing merged module failed."); |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 823 | std::list<tool_output_file> OSs; |
| 824 | std::vector<raw_pwrite_stream *> OSPtrs; |
| 825 | for (unsigned I = 0; I != Parallelism; ++I) { |
| 826 | std::string PartFilename = OutputFilename; |
| 827 | if (Parallelism != 1) |
| 828 | PartFilename += "." + utostr(I); |
| 829 | std::error_code EC; |
| 830 | OSs.emplace_back(PartFilename, EC, sys::fs::F_None); |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 831 | if (EC) |
| 832 | error("error opening the file '" + PartFilename + "': " + EC.message()); |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 833 | OSPtrs.push_back(&OSs.back().os()); |
| 834 | } |
| 835 | |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 836 | if (!CodeGen.compileOptimized(OSPtrs)) |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 837 | // Diagnostic messages should have been printed by the handler. |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 838 | error("error compiling the code"); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 839 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 840 | for (tool_output_file &OS : OSs) |
| 841 | OS.keep(); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 842 | } else { |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 843 | if (Parallelism != 1) |
| 844 | error("-j must be specified together with -o"); |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 845 | |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 846 | if (SaveModuleFile) |
| 847 | error(": -save-merged-module must be specified with -o"); |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 848 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 849 | const char *OutputName = nullptr; |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 850 | if (!CodeGen.compile_to_file(&OutputName, DisableVerify, DisableInline, |
Davide Italiano | 1eea9bd | 2016-04-13 22:08:26 +0000 | [diff] [blame] | 851 | DisableGVNLoadPRE, DisableLTOVectorization)) |
| 852 | error("error compiling the code"); |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 853 | // Diagnostic messages should have been printed by the handler. |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 854 | |
| 855 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 856 | } |
| 857 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 858 | return 0; |
| 859 | } |