blob: fe3f1b7de91227b18870cc070acbf037a509945d [file] [log] [blame]
Peter Collingbourne4e380b02013-09-19 22:15:52 +00001//===-- 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 Espindola282a4702013-10-31 20:51:58 +000015#include "llvm/ADT/StringSet.h"
Teresa Johnson91a88bb2015-10-19 14:30:44 +000016#include "llvm/Bitcode/ReaderWriter.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000017#include "llvm/CodeGen/CommandFlags.h"
Mehdi Amini354f5202015-11-19 05:52:29 +000018#include "llvm/IR/DiagnosticPrinter.h"
Teresa Johnson91a88bb2015-10-19 14:30:44 +000019#include "llvm/IR/LLVMContext.h"
Mehdi Amini3c0e64c2016-04-20 01:04:26 +000020#include "llvm/IR/Verifier.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000021#include "llvm/IRReader/IRReader.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000022#include "llvm/LTO/LTOCodeGenerator.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000023#include "llvm/LTO/LTOModule.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000024#include "llvm/LTO/ThinLTOCodeGenerator.h"
25#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000026#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000027#include "llvm/Support/FileSystem.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000028#include "llvm/Support/ManagedStatic.h"
Teresa Johnsonbbd10b42016-05-17 14:45:30 +000029#include "llvm/Support/Path.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000030#include "llvm/Support/PrettyStackTrace.h"
31#include "llvm/Support/Signals.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000032#include "llvm/Support/SourceMgr.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000033#include "llvm/Support/TargetSelect.h"
Peter Collingbournec269ed52015-08-27 23:37:36 +000034#include "llvm/Support/ToolOutputFile.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000035#include "llvm/Support/raw_ostream.h"
Peter Collingbournec269ed52015-08-27 23:37:36 +000036#include <list>
Peter Collingbourne4e380b02013-09-19 22:15:52 +000037
38using namespace llvm;
39
Peter Collingbourne070843d2015-03-19 22:01:00 +000040static cl::opt<char>
Davide Italianob10e8932016-04-13 21:41:35 +000041 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
42 "(default = '-O2')"),
43 cl::Prefix, cl::ZeroOrMore, cl::init('2'));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000044
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +000045static cl::opt<bool> DisableVerify(
46 "disable-verify", cl::init(false),
47 cl::desc("Do not run the verifier during the optimization pipeline"));
48
Davide Italianob10e8932016-04-13 21:41:35 +000049static cl::opt<bool> DisableInline("disable-inlining", cl::init(false),
50 cl::desc("Do not run the inliner pass"));
Rafael Espindola0b385c72013-09-30 16:39:19 +000051
52static cl::opt<bool>
Davide Italianob10e8932016-04-13 21:41:35 +000053 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
54 cl::desc("Do not run the GVN load PRE pass"));
Rafael Espindola0b385c72013-09-30 16:39:19 +000055
Davide Italianob10e8932016-04-13 21:41:35 +000056static cl::opt<bool> DisableLTOVectorization(
57 "disable-lto-vectorization", cl::init(false),
58 cl::desc("Do not run loop or slp vectorization during LTO"));
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000059
Davide Italianob10e8932016-04-13 21:41:35 +000060static cl::opt<bool> UseDiagnosticHandler(
61 "use-diagnostic-handler", cl::init(false),
62 cl::desc("Use a diagnostic handler to test the handler interface"));
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000063
Teresa Johnsonf72278f2015-11-02 18:02:11 +000064static cl::opt<bool>
65 ThinLTO("thinlto", cl::init(false),
66 cl::desc("Only write combined global index for ThinLTO backends"));
Teresa Johnson91a88bb2015-10-19 14:30:44 +000067
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000068enum ThinLTOModes {
69 THINLINK,
Teresa Johnson84174c32016-05-10 13:48:23 +000070 THINDISTRIBUTE,
Teresa Johnson8570fe42016-05-10 15:54:09 +000071 THINEMITIMPORTS,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000072 THINPROMOTE,
73 THINIMPORT,
Mehdi Amini059464f2016-04-24 03:18:01 +000074 THININTERNALIZE,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000075 THINOPT,
76 THINCODEGEN,
77 THINALL
78};
79
80cl::opt<ThinLTOModes> ThinLTOMode(
81 "thinlto-action", cl::desc("Perform a single ThinLTO stage:"),
82 cl::values(
83 clEnumValN(
84 THINLINK, "thinlink",
85 "ThinLink: produces the index by linking only the summaries."),
Teresa Johnson84174c32016-05-10 13:48:23 +000086 clEnumValN(THINDISTRIBUTE, "distributedindexes",
87 "Produces individual indexes for distributed backends."),
Teresa Johnson8570fe42016-05-10 15:54:09 +000088 clEnumValN(THINEMITIMPORTS, "emitimports",
89 "Emit imports files for distributed backends."),
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000090 clEnumValN(THINPROMOTE, "promote",
91 "Perform pre-import promotion (requires -thinlto-index)."),
92 clEnumValN(THINIMPORT, "import", "Perform both promotion and "
93 "cross-module importing (requires "
94 "-thinlto-index)."),
Mehdi Amini059464f2016-04-24 03:18:01 +000095 clEnumValN(THININTERNALIZE, "internalize",
96 "Perform internalization driven by -exported-symbol "
97 "(requires -thinlto-index)."),
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000098 clEnumValN(THINOPT, "optimize", "Perform ThinLTO optimizations."),
99 clEnumValN(THINCODEGEN, "codegen", "CodeGen (expected to match llc)"),
100 clEnumValN(THINALL, "run", "Perform ThinLTO end-to-end"),
101 clEnumValEnd));
102
103static cl::opt<std::string>
104 ThinLTOIndex("thinlto-index",
105 cl::desc("Provide the index produced by a ThinLink, required "
106 "to perform the promotion and/or importing."));
107
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000108static cl::opt<std::string> ThinLTOPrefixReplace(
109 "thinlto-prefix-replace",
110 cl::desc("Control where files for distributed backends are "
111 "created. Expects 'oldprefix:newprefix' and if path "
112 "prefix of output file is oldprefix it will be "
113 "replaced with newprefix."));
114
Mehdi Amini03abce92016-05-05 16:33:51 +0000115static cl::opt<std::string> ThinLTOModuleId(
116 "thinlto-module-id",
117 cl::desc("For the module ID for the file to process, useful to "
118 "match what is in the index."));
119
Mehdi Aminiab4a8b62016-05-14 05:16:41 +0000120static cl::opt<std::string>
121 ThinLTOCacheDir("thinlto-cache-dir", cl::desc("Enable ThinLTO caching."));
122
Tobias Edler von Koch49c9a6e2015-11-20 00:13:05 +0000123static cl::opt<bool>
Davide Italianob10e8932016-04-13 21:41:35 +0000124 SaveModuleFile("save-merged-module", cl::init(false),
125 cl::desc("Write merged LTO module to file before CodeGen"));
126
127static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
128 cl::desc("<input bitcode files>"));
129
130static cl::opt<std::string> OutputFilename("o", cl::init(""),
131 cl::desc("Override output filename"),
132 cl::value_desc("filename"));
Tobias Edler von Koch49c9a6e2015-11-20 00:13:05 +0000133
Mehdi Amini059464f2016-04-24 03:18:01 +0000134static cl::list<std::string> ExportedSymbols(
135 "exported-symbol",
136 cl::desc("List of symbols to export from the resulting object file"),
137 cl::ZeroOrMore);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000138
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000139static cl::list<std::string>
Davide Italianob10e8932016-04-13 21:41:35 +0000140 DSOSymbols("dso-symbol",
141 cl::desc("Symbol to put in the symtab in the resulting dso"),
142 cl::ZeroOrMore);
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000143
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000144static cl::opt<bool> ListSymbolsOnly(
145 "list-symbols-only", cl::init(false),
146 cl::desc("Instead of running LTO, list the symbols in each IR file"));
147
Manman Ren6487ce92015-02-24 00:45:56 +0000148static cl::opt<bool> SetMergedModule(
149 "set-merged-module", cl::init(false),
150 cl::desc("Use the first input module as the merged module"));
151
Peter Collingbournec269ed52015-08-27 23:37:36 +0000152static cl::opt<unsigned> Parallelism("j", cl::Prefix, cl::init(1),
153 cl::desc("Number of backend threads"));
154
Tobias Edler von Koch3f4f6f3e2016-01-18 23:35:24 +0000155static cl::opt<bool> RestoreGlobalsLinkage(
156 "restore-linkage", cl::init(false),
157 cl::desc("Restore original linkage of globals prior to CodeGen"));
158
Rafael Espindola282a4702013-10-31 20:51:58 +0000159namespace {
160struct ModuleInfo {
161 std::vector<bool> CanBeHidden;
162};
163}
164
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000165static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
166 const char *Msg, void *) {
Yunzhong Gaoef436f02015-11-10 18:52:48 +0000167 errs() << "llvm-lto: ";
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000168 switch (Severity) {
169 case LTO_DS_NOTE:
170 errs() << "note: ";
171 break;
172 case LTO_DS_REMARK:
173 errs() << "remark: ";
174 break;
175 case LTO_DS_ERROR:
176 errs() << "error: ";
177 break;
178 case LTO_DS_WARNING:
179 errs() << "warning: ";
180 break;
181 }
182 errs() << Msg << "\n";
183}
184
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000185static std::string CurrentActivity;
Mehdi Amini354f5202015-11-19 05:52:29 +0000186static void diagnosticHandler(const DiagnosticInfo &DI) {
187 raw_ostream &OS = errs();
188 OS << "llvm-lto: ";
189 switch (DI.getSeverity()) {
190 case DS_Error:
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000191 OS << "error";
Mehdi Amini354f5202015-11-19 05:52:29 +0000192 break;
193 case DS_Warning:
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000194 OS << "warning";
Mehdi Amini354f5202015-11-19 05:52:29 +0000195 break;
196 case DS_Remark:
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000197 OS << "remark";
Mehdi Amini354f5202015-11-19 05:52:29 +0000198 break;
199 case DS_Note:
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000200 OS << "note";
Mehdi Amini354f5202015-11-19 05:52:29 +0000201 break;
202 }
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000203 if (!CurrentActivity.empty())
204 OS << ' ' << CurrentActivity;
205 OS << ": ";
Mehdi Amini354f5202015-11-19 05:52:29 +0000206
207 DiagnosticPrinterRawOStream DP(OS);
208 DI.print(DP);
209 OS << '\n';
210
211 if (DI.getSeverity() == DS_Error)
212 exit(1);
213}
214
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000215static void diagnosticHandlerWithContext(const DiagnosticInfo &DI,
216 void *Context) {
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000217 diagnosticHandler(DI);
218}
219
Rafael Espindola5e128db2015-12-04 00:45:57 +0000220static void error(const Twine &Msg) {
221 errs() << "llvm-lto: " << Msg << '\n';
222 exit(1);
223}
224
225static void error(std::error_code EC, const Twine &Prefix) {
226 if (EC)
227 error(Prefix + ": " + EC.message());
228}
229
230template <typename T>
231static void error(const ErrorOr<T> &V, const Twine &Prefix) {
232 error(V.getError(), Prefix);
233}
234
Mehdi Amini3c0e64c2016-04-20 01:04:26 +0000235static void maybeVerifyModule(const Module &Mod) {
236 if (!DisableVerify && verifyModule(Mod))
237 error("Broken Module");
238}
239
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000240static std::unique_ptr<LTOModule>
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000241getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer,
Rafael Espindola5e128db2015-12-04 00:45:57 +0000242 const TargetOptions &Options) {
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000243 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
244 MemoryBuffer::getFile(Path);
Rafael Espindola5e128db2015-12-04 00:45:57 +0000245 error(BufferOrErr, "error loading file '" + Path + "'");
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000246 Buffer = std::move(BufferOrErr.get());
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000247 CurrentActivity = ("loading file '" + Path + "'").str();
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000248 std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>();
249 Context->setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000250 ErrorOr<std::unique_ptr<LTOModule>> Ret = LTOModule::createInLocalContext(
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000251 std::move(Context), Buffer->getBufferStart(), Buffer->getBufferSize(),
252 Options, Path);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000253 CurrentActivity = "";
Mehdi Amini3c0e64c2016-04-20 01:04:26 +0000254 maybeVerifyModule((*Ret)->getModule());
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000255 return std::move(*Ret);
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000256}
257
258/// \brief List symbols in each IR file.
259///
260/// The main point here is to provide lit-testable coverage for the LTOModule
261/// functionality that's exposed by the C API to list symbols. Moreover, this
262/// provides testing coverage for modules that have been created in their own
263/// contexts.
Rafael Espindola5e128db2015-12-04 00:45:57 +0000264static void listSymbols(const TargetOptions &Options) {
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000265 for (auto &Filename : InputFilenames) {
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000266 std::unique_ptr<MemoryBuffer> Buffer;
267 std::unique_ptr<LTOModule> Module =
Rafael Espindola5e128db2015-12-04 00:45:57 +0000268 getLocalLTOModule(Filename, Buffer, Options);
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000269
270 // List the symbols.
271 outs() << Filename << ":\n";
272 for (int I = 0, E = Module->getSymbolCount(); I != E; ++I)
273 outs() << Module->getSymbolName(I) << "\n";
274 }
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000275}
276
Teresa Johnson91a88bb2015-10-19 14:30:44 +0000277/// Create a combined index file from the input IR files and write it.
278///
279/// This is meant to enable testing of ThinLTO combined index generation,
280/// currently available via the gold plugin via -thinlto.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000281static void createCombinedModuleSummaryIndex() {
282 ModuleSummaryIndex CombinedIndex;
Teresa Johnson91a88bb2015-10-19 14:30:44 +0000283 uint64_t NextModuleId = 0;
284 for (auto &Filename : InputFilenames) {
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000285 CurrentActivity = "loading file '" + Filename + "'";
Teresa Johnson26ab5772016-03-15 00:04:37 +0000286 ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
287 llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler);
Mehdi Amini155da5b2016-03-19 00:17:32 +0000288 error(IndexOrErr, "error " + CurrentActivity);
Teresa Johnson26ab5772016-03-15 00:04:37 +0000289 std::unique_ptr<ModuleSummaryIndex> Index = std::move(IndexOrErr.get());
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000290 CurrentActivity = "";
Teresa Johnson26ab5772016-03-15 00:04:37 +0000291 // Skip files without a module summary.
Teresa Johnson6290dbc2015-11-21 21:55:48 +0000292 if (!Index)
293 continue;
Teresa Johnson91a88bb2015-10-19 14:30:44 +0000294 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
295 }
296 std::error_code EC;
297 assert(!OutputFilename.empty());
298 raw_fd_ostream OS(OutputFilename + ".thinlto.bc", EC,
299 sys::fs::OpenFlags::F_None);
Rafael Espindola5e128db2015-12-04 00:45:57 +0000300 error(EC, "error opening the file '" + OutputFilename + ".thinlto.bc'");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000301 WriteIndexToFile(CombinedIndex, OS);
Teresa Johnson91a88bb2015-10-19 14:30:44 +0000302 OS.close();
Teresa Johnson91a88bb2015-10-19 14:30:44 +0000303}
304
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000305/// Parse the thinlto_prefix_replace option into the \p OldPrefix and
306/// \p NewPrefix strings, if it was specified.
307static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
308 std::string &NewPrefix) {
309 assert(ThinLTOPrefixReplace.empty() ||
310 ThinLTOPrefixReplace.find(":") != StringRef::npos);
311 StringRef PrefixReplace = ThinLTOPrefixReplace;
312 std::pair<StringRef, StringRef> Split = PrefixReplace.split(":");
313 OldPrefix = Split.first.str();
314 NewPrefix = Split.second.str();
315}
316
317/// Given the original \p Path to an output file, replace any path
318/// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
319/// resulting directory if it does not yet exist.
320static std::string getThinLTOOutputFile(const std::string &Path,
321 const std::string &OldPrefix,
322 const std::string &NewPrefix) {
323 if (OldPrefix.empty() && NewPrefix.empty())
324 return Path;
325 SmallString<128> NewPath(Path);
326 llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
327 StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
328 if (!ParentPath.empty()) {
329 // Make sure the new directory exists, creating it if necessary.
330 if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
331 error(EC, "error creating the directory '" + ParentPath + "'");
332 }
333 return NewPath.str();
334}
335
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000336namespace thinlto {
337
338std::vector<std::unique_ptr<MemoryBuffer>>
Teresa Johnson26ab5772016-03-15 00:04:37 +0000339loadAllFilesForIndex(const ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000340 std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
341
Mehdi Amini385cf282016-03-26 03:35:38 +0000342 for (auto &ModPath : Index.modulePaths()) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000343 const auto &Filename = ModPath.first();
344 auto CurrentActivity = "loading file '" + Filename + "'";
345 auto InputOrErr = MemoryBuffer::getFile(Filename);
346 error(InputOrErr, "error " + CurrentActivity);
347 InputBuffers.push_back(std::move(*InputOrErr));
348 }
349 return InputBuffers;
350}
351
Teresa Johnson26ab5772016-03-15 00:04:37 +0000352std::unique_ptr<ModuleSummaryIndex> loadCombinedIndex() {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000353 if (ThinLTOIndex.empty())
354 report_fatal_error("Missing -thinlto-index for ThinLTO promotion stage");
355 auto CurrentActivity = "loading file '" + ThinLTOIndex + "'";
Teresa Johnson26ab5772016-03-15 00:04:37 +0000356 ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
357 llvm::getModuleSummaryIndexForFile(ThinLTOIndex, diagnosticHandler);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000358 error(IndexOrErr, "error " + CurrentActivity);
359 return std::move(IndexOrErr.get());
360}
361
362static std::unique_ptr<Module> loadModule(StringRef Filename,
363 LLVMContext &Ctx) {
364 SMDiagnostic Err;
365 std::unique_ptr<Module> M(parseIRFile(Filename, Err, Ctx));
366 if (!M) {
367 Err.print("llvm-lto", errs());
368 report_fatal_error("Can't load module for file " + Filename);
369 }
Mehdi Amini3c0e64c2016-04-20 01:04:26 +0000370 maybeVerifyModule(*M);
Mehdi Amini03abce92016-05-05 16:33:51 +0000371
372 if (ThinLTOModuleId.getNumOccurrences()) {
373 if (InputFilenames.size() != 1)
374 report_fatal_error("Can't override the module id for multiple files");
375 M->setModuleIdentifier(ThinLTOModuleId);
376 }
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000377 return M;
378}
379
380static void writeModuleToFile(Module &TheModule, StringRef Filename) {
381 std::error_code EC;
382 raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None);
383 error(EC, "error opening the file '" + Filename + "'");
Mehdi Amini3c0e64c2016-04-20 01:04:26 +0000384 maybeVerifyModule(TheModule);
Teresa Johnson3c35e092016-04-04 21:19:31 +0000385 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000386}
387
388class ThinLTOProcessing {
389public:
390 ThinLTOCodeGenerator ThinGenerator;
391
392 ThinLTOProcessing(const TargetOptions &Options) {
393 ThinGenerator.setCodePICModel(RelocModel);
394 ThinGenerator.setTargetOptions(Options);
Mehdi Aminiab4a8b62016-05-14 05:16:41 +0000395 ThinGenerator.setCacheDir(ThinLTOCacheDir);
Mehdi Amini059464f2016-04-24 03:18:01 +0000396
397 // Add all the exported symbols to the table of symbols to preserve.
398 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
399 ThinGenerator.preserveSymbol(ExportedSymbols[i]);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000400 }
401
402 void run() {
403 switch (ThinLTOMode) {
404 case THINLINK:
405 return thinLink();
Teresa Johnson84174c32016-05-10 13:48:23 +0000406 case THINDISTRIBUTE:
407 return distributedIndexes();
Teresa Johnson8570fe42016-05-10 15:54:09 +0000408 case THINEMITIMPORTS:
409 return emitImports();
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000410 case THINPROMOTE:
411 return promote();
412 case THINIMPORT:
413 return import();
Mehdi Amini059464f2016-04-24 03:18:01 +0000414 case THININTERNALIZE:
415 return internalize();
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000416 case THINOPT:
417 return optimize();
418 case THINCODEGEN:
419 return codegen();
420 case THINALL:
421 return runAll();
422 }
423 }
424
425private:
426 /// Load the input files, create the combined index, and write it out.
427 void thinLink() {
428 // Perform "ThinLink": just produce the index
429 if (OutputFilename.empty())
430 report_fatal_error(
431 "OutputFilename is necessary to store the combined index.\n");
432
433 LLVMContext Ctx;
434 std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
435 for (unsigned i = 0; i < InputFilenames.size(); ++i) {
436 auto &Filename = InputFilenames[i];
437 StringRef CurrentActivity = "loading file '" + Filename + "'";
438 auto InputOrErr = MemoryBuffer::getFile(Filename);
439 error(InputOrErr, "error " + CurrentActivity);
440 InputBuffers.push_back(std::move(*InputOrErr));
441 ThinGenerator.addModule(Filename, InputBuffers.back()->getBuffer());
442 }
443
444 auto CombinedIndex = ThinGenerator.linkCombinedIndex();
445 std::error_code EC;
446 raw_fd_ostream OS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
447 error(EC, "error opening the file '" + OutputFilename + "'");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000448 WriteIndexToFile(*CombinedIndex, OS);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000449 return;
450 }
451
Teresa Johnson84174c32016-05-10 13:48:23 +0000452 /// Load the combined index from disk, then compute and generate
453 /// individual index files suitable for ThinLTO distributed backend builds
454 /// on the files mentioned on the command line (these must match the index
455 /// content).
456 void distributedIndexes() {
457 if (InputFilenames.size() != 1 && !OutputFilename.empty())
458 report_fatal_error("Can't handle a single output filename and multiple "
459 "input files, do not provide an output filename and "
460 "the output files will be suffixed from the input "
461 "ones.");
462
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000463 std::string OldPrefix, NewPrefix;
464 getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
465
Teresa Johnson84174c32016-05-10 13:48:23 +0000466 auto Index = loadCombinedIndex();
467 for (auto &Filename : InputFilenames) {
468 // Build a map of module to the GUIDs and summary objects that should
469 // be written to its index.
470 std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
471 ThinLTOCodeGenerator::gatherImportedSummariesForModule(
472 Filename, *Index, ModuleToSummariesForIndex);
473
474 std::string OutputName = OutputFilename;
475 if (OutputName.empty()) {
476 OutputName = Filename + ".thinlto.bc";
477 }
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000478 OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
Teresa Johnson84174c32016-05-10 13:48:23 +0000479 std::error_code EC;
480 raw_fd_ostream OS(OutputName, EC, sys::fs::OpenFlags::F_None);
481 error(EC, "error opening the file '" + OutputName + "'");
482 WriteIndexToFile(*Index, OS, &ModuleToSummariesForIndex);
483 }
484 }
485
Teresa Johnson8570fe42016-05-10 15:54:09 +0000486 /// Load the combined index from disk, compute the imports, and emit
487 /// the import file lists for each module to disk.
488 void emitImports() {
489 if (InputFilenames.size() != 1 && !OutputFilename.empty())
490 report_fatal_error("Can't handle a single output filename and multiple "
491 "input files, do not provide an output filename and "
492 "the output files will be suffixed from the input "
493 "ones.");
494
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000495 std::string OldPrefix, NewPrefix;
496 getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
497
Teresa Johnson8570fe42016-05-10 15:54:09 +0000498 auto Index = loadCombinedIndex();
499 for (auto &Filename : InputFilenames) {
500 std::string OutputName = OutputFilename;
501 if (OutputName.empty()) {
502 OutputName = Filename + ".imports";
503 }
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000504 OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
Teresa Johnson8570fe42016-05-10 15:54:09 +0000505 ThinLTOCodeGenerator::emitImports(Filename, OutputName, *Index);
506 }
507 }
508
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000509 /// Load the combined index from disk, then load every file referenced by
510 /// the index and add them to the generator, finally perform the promotion
511 /// on the files mentioned on the command line (these must match the index
512 /// content).
513 void promote() {
514 if (InputFilenames.size() != 1 && !OutputFilename.empty())
515 report_fatal_error("Can't handle a single output filename and multiple "
516 "input files, do not provide an output filename and "
517 "the output files will be suffixed from the input "
518 "ones.");
519
520 auto Index = loadCombinedIndex();
521 for (auto &Filename : InputFilenames) {
522 LLVMContext Ctx;
523 auto TheModule = loadModule(Filename, Ctx);
524
525 ThinGenerator.promote(*TheModule, *Index);
526
527 std::string OutputName = OutputFilename;
528 if (OutputName.empty()) {
529 OutputName = Filename + ".thinlto.promoted.bc";
530 }
531 writeModuleToFile(*TheModule, OutputName);
532 }
533 }
534
535 /// Load the combined index from disk, then load every file referenced by
536 /// the index and add them to the generator, then performs the promotion and
537 /// cross module importing on the files mentioned on the command line
538 /// (these must match the index content).
539 void import() {
540 if (InputFilenames.size() != 1 && !OutputFilename.empty())
541 report_fatal_error("Can't handle a single output filename and multiple "
542 "input files, do not provide an output filename and "
543 "the output files will be suffixed from the input "
544 "ones.");
545
546 auto Index = loadCombinedIndex();
547 auto InputBuffers = loadAllFilesForIndex(*Index);
548 for (auto &MemBuffer : InputBuffers)
549 ThinGenerator.addModule(MemBuffer->getBufferIdentifier(),
550 MemBuffer->getBuffer());
551
552 for (auto &Filename : InputFilenames) {
553 LLVMContext Ctx;
554 auto TheModule = loadModule(Filename, Ctx);
555
556 ThinGenerator.crossModuleImport(*TheModule, *Index);
557
558 std::string OutputName = OutputFilename;
559 if (OutputName.empty()) {
560 OutputName = Filename + ".thinlto.imported.bc";
561 }
562 writeModuleToFile(*TheModule, OutputName);
563 }
564 }
565
Mehdi Amini059464f2016-04-24 03:18:01 +0000566 void internalize() {
567 if (InputFilenames.size() != 1 && !OutputFilename.empty())
568 report_fatal_error("Can't handle a single output filename and multiple "
569 "input files, do not provide an output filename and "
570 "the output files will be suffixed from the input "
571 "ones.");
572
573 if (ExportedSymbols.empty())
574 errs() << "Warning: -internalize will not perform without "
575 "-exported-symbol\n";
576
577 auto Index = loadCombinedIndex();
578 auto InputBuffers = loadAllFilesForIndex(*Index);
579 for (auto &MemBuffer : InputBuffers)
580 ThinGenerator.addModule(MemBuffer->getBufferIdentifier(),
581 MemBuffer->getBuffer());
582
583 for (auto &Filename : InputFilenames) {
584 LLVMContext Ctx;
585 auto TheModule = loadModule(Filename, Ctx);
586
587 ThinGenerator.internalize(*TheModule, *Index);
588
589 std::string OutputName = OutputFilename;
590 if (OutputName.empty()) {
591 OutputName = Filename + ".thinlto.internalized.bc";
592 }
593 writeModuleToFile(*TheModule, OutputName);
594 }
595 }
596
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000597 void optimize() {
598 if (InputFilenames.size() != 1 && !OutputFilename.empty())
599 report_fatal_error("Can't handle a single output filename and multiple "
600 "input files, do not provide an output filename and "
601 "the output files will be suffixed from the input "
602 "ones.");
603 if (!ThinLTOIndex.empty())
604 errs() << "Warning: -thinlto-index ignored for optimize stage";
605
606 for (auto &Filename : InputFilenames) {
607 LLVMContext Ctx;
608 auto TheModule = loadModule(Filename, Ctx);
609
610 ThinGenerator.optimize(*TheModule);
611
612 std::string OutputName = OutputFilename;
613 if (OutputName.empty()) {
614 OutputName = Filename + ".thinlto.imported.bc";
615 }
616 writeModuleToFile(*TheModule, OutputName);
617 }
618 }
619
620 void codegen() {
621 if (InputFilenames.size() != 1 && !OutputFilename.empty())
622 report_fatal_error("Can't handle a single output filename and multiple "
623 "input files, do not provide an output filename and "
624 "the output files will be suffixed from the input "
625 "ones.");
626 if (!ThinLTOIndex.empty())
627 errs() << "Warning: -thinlto-index ignored for codegen stage";
628
629 for (auto &Filename : InputFilenames) {
630 LLVMContext Ctx;
631 auto TheModule = loadModule(Filename, Ctx);
632
633 auto Buffer = ThinGenerator.codegen(*TheModule);
634 std::string OutputName = OutputFilename;
635 if (OutputName.empty()) {
636 OutputName = Filename + ".thinlto.o";
637 }
638 if (OutputName == "-") {
639 outs() << Buffer->getBuffer();
640 return;
641 }
642
643 std::error_code EC;
644 raw_fd_ostream OS(OutputName, EC, sys::fs::OpenFlags::F_None);
645 error(EC, "error opening the file '" + OutputName + "'");
646 OS << Buffer->getBuffer();
647 }
648 }
649
650 /// Full ThinLTO process
651 void runAll() {
652 if (!OutputFilename.empty())
653 report_fatal_error("Do not provide an output filename for ThinLTO "
654 " processing, the output files will be suffixed from "
655 "the input ones.");
656
657 if (!ThinLTOIndex.empty())
658 errs() << "Warning: -thinlto-index ignored for full ThinLTO process";
659
660 LLVMContext Ctx;
661 std::vector<std::unique_ptr<MemoryBuffer>> InputBuffers;
662 for (unsigned i = 0; i < InputFilenames.size(); ++i) {
663 auto &Filename = InputFilenames[i];
664 StringRef CurrentActivity = "loading file '" + Filename + "'";
665 auto InputOrErr = MemoryBuffer::getFile(Filename);
666 error(InputOrErr, "error " + CurrentActivity);
667 InputBuffers.push_back(std::move(*InputOrErr));
668 ThinGenerator.addModule(Filename, InputBuffers.back()->getBuffer());
669 }
670
671 ThinGenerator.run();
672
673 auto &Binaries = ThinGenerator.getProducedBinaries();
674 if (Binaries.size() != InputFilenames.size())
675 report_fatal_error("Number of output objects does not match the number "
676 "of inputs");
677
678 for (unsigned BufID = 0; BufID < Binaries.size(); ++BufID) {
679 auto OutputName = InputFilenames[BufID] + ".thinlto.o";
680 std::error_code EC;
681 raw_fd_ostream OS(OutputName, EC, sys::fs::OpenFlags::F_None);
682 error(EC, "error opening the file '" + OutputName + "'");
683 OS << Binaries[BufID]->getBuffer();
684 }
685 }
686
687 /// Load the combined index from disk, then load every file referenced by
688};
689
690} // namespace thinlto
691
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000692int main(int argc, char **argv) {
693 // Print a stack trace if we signal out.
694 sys::PrintStackTraceOnErrorSignal();
695 PrettyStackTraceProgram X(argc, argv);
696
697 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
698 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
699
Rafael Espindola5e128db2015-12-04 00:45:57 +0000700 if (OptLevel < '0' || OptLevel > '3')
701 error("optimization level must be between 0 and 3");
Peter Collingbourne070843d2015-03-19 22:01:00 +0000702
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000703 // Initialize the configured targets.
704 InitializeAllTargets();
705 InitializeAllTargetMCs();
706 InitializeAllAsmPrinters();
707 InitializeAllAsmParsers();
708
Rafael Espindola0b385c72013-09-30 16:39:19 +0000709 // set up the TargetOptions for the machine
Eli Benderskyf0f21002014-02-19 17:09:35 +0000710 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000711
Rafael Espindola5e128db2015-12-04 00:45:57 +0000712 if (ListSymbolsOnly) {
713 listSymbols(Options);
714 return 0;
715 }
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000716
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000717 if (ThinLTOMode.getNumOccurrences()) {
718 if (ThinLTOMode.getNumOccurrences() > 1)
719 report_fatal_error("You can't specify more than one -thinlto-action");
720 thinlto::ThinLTOProcessing ThinLTOProcessor(Options);
721 ThinLTOProcessor.run();
722 return 0;
723 }
724
Rafael Espindola5e128db2015-12-04 00:45:57 +0000725 if (ThinLTO) {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000726 createCombinedModuleSummaryIndex();
Rafael Espindola5e128db2015-12-04 00:45:57 +0000727 return 0;
728 }
Teresa Johnson91a88bb2015-10-19 14:30:44 +0000729
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000730 unsigned BaseArg = 0;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000731
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000732 LLVMContext Context;
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000733 Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000734
735 LTOCodeGenerator CodeGen(Context);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000736
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000737 if (UseDiagnosticHandler)
738 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
739
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000740 CodeGen.setCodePICModel(RelocModel);
James Molloy951e5292014-04-14 13:54:16 +0000741
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000742 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindola0b385c72013-09-30 16:39:19 +0000743 CodeGen.setTargetOptions(Options);
Tobias Edler von Koch3f4f6f3e2016-01-18 23:35:24 +0000744 CodeGen.setShouldRestoreGlobalsLinkage(RestoreGlobalsLinkage);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000745
Rafael Espindola282a4702013-10-31 20:51:58 +0000746 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
747 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
748 DSOSymbolsSet.insert(DSOSymbols[i]);
749
750 std::vector<std::string> KeptDSOSyms;
751
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000752 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000753 CurrentActivity = "loading file '" + InputFilenames[i] + "'";
754 ErrorOr<std::unique_ptr<LTOModule>> ModuleOrErr =
755 LTOModule::createFromFile(Context, InputFilenames[i].c_str(), Options);
756 std::unique_ptr<LTOModule> &Module = *ModuleOrErr;
757 CurrentActivity = "";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000758
Peter Collingbourne552174392015-08-21 19:09:42 +0000759 unsigned NumSyms = Module->getSymbolCount();
760 for (unsigned I = 0; I < NumSyms; ++I) {
761 StringRef Name = Module->getSymbolName(I);
762 if (!DSOSymbolsSet.count(Name))
763 continue;
764 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
765 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
766 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
767 KeptDSOSyms.push_back(Name);
768 }
Manman Ren6487ce92015-02-24 00:45:56 +0000769
770 // We use the first input module as the destination module when
771 // SetMergedModule is true.
772 if (SetMergedModule && i == BaseArg) {
773 // Transfer ownership to the code generator.
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000774 CodeGen.setModule(std::move(Module));
Yunzhong Gao46261a72015-09-11 20:01:53 +0000775 } else if (!CodeGen.addModule(Module.get())) {
776 // Print a message here so that we know addModule() did not abort.
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000777 error("error adding file '" + InputFilenames[i] + "'");
Yunzhong Gao46261a72015-09-11 20:01:53 +0000778 }
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000779 }
780
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000781 // Add all the exported symbols to the table of symbols to preserve.
782 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
783 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
784
Rafael Espindolacda29112013-10-03 18:29:09 +0000785 // Add all the dso symbols to the table of symbols to expose.
Rafael Espindola282a4702013-10-31 20:51:58 +0000786 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
787 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
Rafael Espindolacda29112013-10-03 18:29:09 +0000788
Akira Hatanaka23b5f672015-01-30 01:14:28 +0000789 // Set cpu and attrs strings for the default target/subtarget.
790 CodeGen.setCpu(MCPU.c_str());
791
Peter Collingbourne070843d2015-03-19 22:01:00 +0000792 CodeGen.setOptLevel(OptLevel - '0');
793
Tom Roederfd1bc602014-04-25 21:46:51 +0000794 std::string attrs;
795 for (unsigned i = 0; i < MAttrs.size(); ++i) {
796 if (i > 0)
797 attrs.append(",");
798 attrs.append(MAttrs[i]);
799 }
800
801 if (!attrs.empty())
802 CodeGen.setAttr(attrs.c_str());
803
Tobias Edler von Koch49c9a6e2015-11-20 00:13:05 +0000804 if (FileType.getNumOccurrences())
805 CodeGen.setFileType(FileType);
806
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000807 if (!OutputFilename.empty()) {
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000808 if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000809 DisableLTOVectorization)) {
810 // Diagnostic messages should have been printed by the handler.
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000811 error("error optimizing the code");
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000812 }
813
Tobias Edler von Koch49c9a6e2015-11-20 00:13:05 +0000814 if (SaveModuleFile) {
815 std::string ModuleFilename = OutputFilename;
816 ModuleFilename += ".merged.bc";
817 std::string ErrMsg;
818
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000819 if (!CodeGen.writeMergedModules(ModuleFilename.c_str()))
820 error("writing merged module failed.");
Tobias Edler von Koch49c9a6e2015-11-20 00:13:05 +0000821 }
822
Peter Collingbournec269ed52015-08-27 23:37:36 +0000823 std::list<tool_output_file> OSs;
824 std::vector<raw_pwrite_stream *> OSPtrs;
825 for (unsigned I = 0; I != Parallelism; ++I) {
826 std::string PartFilename = OutputFilename;
827 if (Parallelism != 1)
828 PartFilename += "." + utostr(I);
829 std::error_code EC;
830 OSs.emplace_back(PartFilename, EC, sys::fs::F_None);
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000831 if (EC)
832 error("error opening the file '" + PartFilename + "': " + EC.message());
Peter Collingbournec269ed52015-08-27 23:37:36 +0000833 OSPtrs.push_back(&OSs.back().os());
834 }
835
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000836 if (!CodeGen.compileOptimized(OSPtrs))
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000837 // Diagnostic messages should have been printed by the handler.
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000838 error("error compiling the code");
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000839
Peter Collingbournec269ed52015-08-27 23:37:36 +0000840 for (tool_output_file &OS : OSs)
841 OS.keep();
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000842 } else {
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000843 if (Parallelism != 1)
844 error("-j must be specified together with -o");
Peter Collingbournec269ed52015-08-27 23:37:36 +0000845
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000846 if (SaveModuleFile)
847 error(": -save-merged-module must be specified with -o");
Tobias Edler von Koch49c9a6e2015-11-20 00:13:05 +0000848
Craig Toppere6cb63e2014-04-25 04:24:47 +0000849 const char *OutputName = nullptr;
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000850 if (!CodeGen.compile_to_file(&OutputName, DisableVerify, DisableInline,
Davide Italiano1eea9bd2016-04-13 22:08:26 +0000851 DisableGVNLoadPRE, DisableLTOVectorization))
852 error("error compiling the code");
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000853 // Diagnostic messages should have been printed by the handler.
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000854
855 outs() << "Wrote native object file '" << OutputName << "'\n";
856 }
857
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000858 return 0;
859}