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