blob: 9cd6587c26223a8c4176e8b3532a64b8e3b3c69e [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"
Rafael Espindola0b385c72013-09-30 16:39:19 +000016#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000017#include "llvm/LTO/LTOCodeGenerator.h"
18#include "llvm/LTO/LTOModule.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000019#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000020#include "llvm/Support/FileSystem.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000021#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/PrettyStackTrace.h"
23#include "llvm/Support/Signals.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000024#include "llvm/Support/TargetSelect.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000025#include "llvm/Support/raw_ostream.h"
Peter Collingbourne4e380b02013-09-19 22:15:52 +000026
27using namespace llvm;
28
Peter Collingbourne070843d2015-03-19 22:01:00 +000029static cl::opt<char>
30OptLevel("O",
31 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
32 "(default = '-O2')"),
33 cl::Prefix,
34 cl::ZeroOrMore,
35 cl::init('2'));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000036
Rafael Espindola0b385c72013-09-30 16:39:19 +000037static cl::opt<bool>
38DisableInline("disable-inlining", cl::init(false),
39 cl::desc("Do not run the inliner pass"));
40
41static cl::opt<bool>
42DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
43 cl::desc("Do not run the GVN load PRE pass"));
44
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000045static cl::opt<bool>
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000046DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
47 cl::desc("Do not run loop or slp vectorization during LTO"));
48
49static cl::opt<bool>
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000050UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
51 cl::desc("Use a diagnostic handler to test the handler interface"));
52
Rafael Espindola0b385c72013-09-30 16:39:19 +000053static cl::list<std::string>
54InputFilenames(cl::Positional, cl::OneOrMore,
55 cl::desc("<input bitcode files>"));
56
57static cl::opt<std::string>
58OutputFilename("o", cl::init(""),
59 cl::desc("Override output filename"),
60 cl::value_desc("filename"));
Peter Collingbourne4e380b02013-09-19 22:15:52 +000061
Rafael Espindoladafc53d2013-10-02 14:12:56 +000062static cl::list<std::string>
63ExportedSymbols("exported-symbol",
64 cl::desc("Symbol to export from the resulting object file"),
65 cl::ZeroOrMore);
66
Rafael Espindolacda29112013-10-03 18:29:09 +000067static cl::list<std::string>
68DSOSymbols("dso-symbol",
69 cl::desc("Symbol to put in the symtab in the resulting dso"),
70 cl::ZeroOrMore);
Rafael Espindoladafc53d2013-10-02 14:12:56 +000071
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +000072static cl::opt<bool> ListSymbolsOnly(
73 "list-symbols-only", cl::init(false),
74 cl::desc("Instead of running LTO, list the symbols in each IR file"));
75
Manman Ren6487ce92015-02-24 00:45:56 +000076static cl::opt<bool> SetMergedModule(
77 "set-merged-module", cl::init(false),
78 cl::desc("Use the first input module as the merged module"));
79
Rafael Espindola282a4702013-10-31 20:51:58 +000080namespace {
81struct ModuleInfo {
82 std::vector<bool> CanBeHidden;
83};
84}
85
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000086static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
87 const char *Msg, void *) {
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000088 switch (Severity) {
89 case LTO_DS_NOTE:
90 errs() << "note: ";
91 break;
92 case LTO_DS_REMARK:
93 errs() << "remark: ";
94 break;
95 case LTO_DS_ERROR:
96 errs() << "error: ";
97 break;
98 case LTO_DS_WARNING:
99 errs() << "warning: ";
100 break;
101 }
102 errs() << Msg << "\n";
103}
104
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000105static std::unique_ptr<LTOModule>
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000106getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer,
107 const TargetOptions &Options, std::string &Error) {
108 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
109 MemoryBuffer::getFile(Path);
110 if (std::error_code EC = BufferOrErr.getError()) {
111 Error = EC.message();
112 return nullptr;
113 }
114 Buffer = std::move(BufferOrErr.get());
115 return std::unique_ptr<LTOModule>(LTOModule::createInLocalContext(
116 Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Error, Path));
117}
118
119/// \brief List symbols in each IR file.
120///
121/// The main point here is to provide lit-testable coverage for the LTOModule
122/// functionality that's exposed by the C API to list symbols. Moreover, this
123/// provides testing coverage for modules that have been created in their own
124/// contexts.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000125static int listSymbols(StringRef Command, const TargetOptions &Options) {
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000126 for (auto &Filename : InputFilenames) {
127 std::string Error;
128 std::unique_ptr<MemoryBuffer> Buffer;
129 std::unique_ptr<LTOModule> Module =
130 getLocalLTOModule(Filename, Buffer, Options, Error);
131 if (!Module) {
132 errs() << Command << ": error loading file '" << Filename
133 << "': " << Error << "\n";
134 return 1;
135 }
136
137 // List the symbols.
138 outs() << Filename << ":\n";
139 for (int I = 0, E = Module->getSymbolCount(); I != E; ++I)
140 outs() << Module->getSymbolName(I) << "\n";
141 }
142 return 0;
143}
144
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000145int main(int argc, char **argv) {
146 // Print a stack trace if we signal out.
147 sys::PrintStackTraceOnErrorSignal();
148 PrettyStackTraceProgram X(argc, argv);
149
150 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
151 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
152
Peter Collingbourne070843d2015-03-19 22:01:00 +0000153 if (OptLevel < '0' || OptLevel > '3') {
154 errs() << argv[0] << ": optimization level must be between 0 and 3\n";
155 return 1;
156 }
157
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000158 // Initialize the configured targets.
159 InitializeAllTargets();
160 InitializeAllTargetMCs();
161 InitializeAllAsmPrinters();
162 InitializeAllAsmParsers();
163
Rafael Espindola0b385c72013-09-30 16:39:19 +0000164 // set up the TargetOptions for the machine
Eli Benderskyf0f21002014-02-19 17:09:35 +0000165 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000166
Duncan P. N. Exon Smithf9abf4f2014-12-17 02:00:38 +0000167 if (ListSymbolsOnly)
168 return listSymbols(argv[0], Options);
169
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000170 unsigned BaseArg = 0;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000171
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000172 LTOCodeGenerator CodeGen;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000173
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000174 if (UseDiagnosticHandler)
175 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
176
James Molloy951e5292014-04-14 13:54:16 +0000177 switch (RelocModel) {
178 case Reloc::Static:
179 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC);
180 break;
181 case Reloc::PIC_:
182 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
183 break;
184 case Reloc::DynamicNoPIC:
185 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC);
186 break;
187 default:
188 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT);
189 }
190
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000191 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindola0b385c72013-09-30 16:39:19 +0000192 CodeGen.setTargetOptions(Options);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000193
Rafael Espindola282a4702013-10-31 20:51:58 +0000194 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
195 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
196 DSOSymbolsSet.insert(DSOSymbols[i]);
197
198 std::vector<std::string> KeptDSOSyms;
199
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000200 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000201 std::string error;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000202 std::unique_ptr<LTOModule> Module(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000203 LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000204 if (!error.empty()) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000205 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000206 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000207 return 1;
208 }
209
Manman Ren6487ce92015-02-24 00:45:56 +0000210 LTOModule *LTOMod = Module.get();
211
212 // We use the first input module as the destination module when
213 // SetMergedModule is true.
214 if (SetMergedModule && i == BaseArg) {
215 // Transfer ownership to the code generator.
216 CodeGen.setModule(Module.release());
217 } else if (!CodeGen.addModule(Module.get()))
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000218 return 1;
Rafael Espindola282a4702013-10-31 20:51:58 +0000219
Manman Ren6487ce92015-02-24 00:45:56 +0000220 unsigned NumSyms = LTOMod->getSymbolCount();
Rafael Espindola282a4702013-10-31 20:51:58 +0000221 for (unsigned I = 0; I < NumSyms; ++I) {
Manman Ren6487ce92015-02-24 00:45:56 +0000222 StringRef Name = LTOMod->getSymbolName(I);
Rafael Espindola282a4702013-10-31 20:51:58 +0000223 if (!DSOSymbolsSet.count(Name))
224 continue;
Manman Ren6487ce92015-02-24 00:45:56 +0000225 lto_symbol_attributes Attrs = LTOMod->getSymbolAttributes(I);
Rafael Espindola282a4702013-10-31 20:51:58 +0000226 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
227 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
228 KeptDSOSyms.push_back(Name);
229 }
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000230 }
231
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000232 // Add all the exported symbols to the table of symbols to preserve.
233 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
234 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
235
Rafael Espindolacda29112013-10-03 18:29:09 +0000236 // Add all the dso symbols to the table of symbols to expose.
Rafael Espindola282a4702013-10-31 20:51:58 +0000237 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
238 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
Rafael Espindolacda29112013-10-03 18:29:09 +0000239
Akira Hatanaka23b5f672015-01-30 01:14:28 +0000240 // Set cpu and attrs strings for the default target/subtarget.
241 CodeGen.setCpu(MCPU.c_str());
242
Peter Collingbourne070843d2015-03-19 22:01:00 +0000243 CodeGen.setOptLevel(OptLevel - '0');
244
Tom Roederfd1bc602014-04-25 21:46:51 +0000245 std::string attrs;
246 for (unsigned i = 0; i < MAttrs.size(); ++i) {
247 if (i > 0)
248 attrs.append(",");
249 attrs.append(MAttrs[i]);
250 }
251
252 if (!attrs.empty())
253 CodeGen.setAttr(attrs.c_str());
254
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000255 if (!OutputFilename.empty()) {
256 size_t len = 0;
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000257 std::string ErrorInfo;
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000258 const void *Code =
Peter Collingbourne070843d2015-03-19 22:01:00 +0000259 CodeGen.compile(&len, DisableInline, DisableGVNLoadPRE,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000260 DisableLTOVectorization, ErrorInfo);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000261 if (!Code) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000262 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000263 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000264 return 1;
265 }
266
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000267 std::error_code EC;
268 raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None);
269 if (EC) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000270 errs() << argv[0] << ": error opening the file '" << OutputFilename
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000271 << "': " << EC.message() << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000272 return 1;
273 }
274
275 FileStream.write(reinterpret_cast<const char *>(Code), len);
276 } else {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000277 std::string ErrorInfo;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000278 const char *OutputName = nullptr;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000279 if (!CodeGen.compile_to_file(&OutputName, DisableInline,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000280 DisableGVNLoadPRE, DisableLTOVectorization,
281 ErrorInfo)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000282 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000283 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000284 << "\n";
285 return 1;
286 }
287
288 outs() << "Wrote native object file '" << OutputName << "'\n";
289 }
290
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000291 return 0;
292}