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