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