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