blob: 52e45296126475d1fd5eb273a01e998f89d2edda [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
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000177 CodeGen.setCodePICModel(RelocModel);
James Molloy951e5292014-04-14 13:54:16 +0000178
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000179 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Rafael Espindola0b385c72013-09-30 16:39:19 +0000180 CodeGen.setTargetOptions(Options);
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000181
Rafael Espindola282a4702013-10-31 20:51:58 +0000182 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
183 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
184 DSOSymbolsSet.insert(DSOSymbols[i]);
185
186 std::vector<std::string> KeptDSOSyms;
187
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000188 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000189 std::string error;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000190 std::unique_ptr<LTOModule> Module(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000191 LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000192 if (!error.empty()) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000193 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000194 << "': " << error << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000195 return 1;
196 }
197
Peter Collingbourne552174392015-08-21 19:09:42 +0000198 unsigned NumSyms = Module->getSymbolCount();
199 for (unsigned I = 0; I < NumSyms; ++I) {
200 StringRef Name = Module->getSymbolName(I);
201 if (!DSOSymbolsSet.count(Name))
202 continue;
203 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
204 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
205 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
206 KeptDSOSyms.push_back(Name);
207 }
Manman Ren6487ce92015-02-24 00:45:56 +0000208
209 // We use the first input module as the destination module when
210 // SetMergedModule is true.
211 if (SetMergedModule && i == BaseArg) {
212 // Transfer ownership to the code generator.
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000213 CodeGen.setModule(std::move(Module));
Manman Ren6487ce92015-02-24 00:45:56 +0000214 } else if (!CodeGen.addModule(Module.get()))
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000215 return 1;
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000216 }
217
Rafael Espindoladafc53d2013-10-02 14:12:56 +0000218 // Add all the exported symbols to the table of symbols to preserve.
219 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
220 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
221
Rafael Espindolacda29112013-10-03 18:29:09 +0000222 // Add all the dso symbols to the table of symbols to expose.
Rafael Espindola282a4702013-10-31 20:51:58 +0000223 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
224 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
Rafael Espindolacda29112013-10-03 18:29:09 +0000225
Akira Hatanaka23b5f672015-01-30 01:14:28 +0000226 // Set cpu and attrs strings for the default target/subtarget.
227 CodeGen.setCpu(MCPU.c_str());
228
Peter Collingbourne070843d2015-03-19 22:01:00 +0000229 CodeGen.setOptLevel(OptLevel - '0');
230
Tom Roederfd1bc602014-04-25 21:46:51 +0000231 std::string attrs;
232 for (unsigned i = 0; i < MAttrs.size(); ++i) {
233 if (i > 0)
234 attrs.append(",");
235 attrs.append(MAttrs[i]);
236 }
237
238 if (!attrs.empty())
239 CodeGen.setAttr(attrs.c_str());
240
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000241 if (!OutputFilename.empty()) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000242 std::string ErrorInfo;
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000243 std::unique_ptr<MemoryBuffer> Code = CodeGen.compile(
244 DisableInline, DisableGVNLoadPRE, DisableLTOVectorization, ErrorInfo);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000245 if (!Code) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000246 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000247 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000248 return 1;
249 }
250
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000251 std::error_code EC;
252 raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None);
253 if (EC) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000254 errs() << argv[0] << ": error opening the file '" << OutputFilename
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000255 << "': " << EC.message() << "\n";
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000256 return 1;
257 }
258
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000259 FileStream.write(Code->getBufferStart(), Code->getBufferSize());
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000260 } else {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000261 std::string ErrorInfo;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000262 const char *OutputName = nullptr;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000263 if (!CodeGen.compile_to_file(&OutputName, DisableInline,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000264 DisableGVNLoadPRE, DisableLTOVectorization,
265 ErrorInfo)) {
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000266 errs() << argv[0]
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000267 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000268 << "\n";
269 return 1;
270 }
271
272 outs() << "Wrote native object file '" << OutputName << "'\n";
273 }
274
Peter Collingbourne4e380b02013-09-19 22:15:52 +0000275 return 0;
276}