1 //===-- llvm-lto: a simple command-line program to link modules with LTO --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes in a list of bitcode files, links them, performs link-time
11 // optimization, and outputs an object file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/CodeGen/CommandFlags.h"
18 #include "llvm/IR/DiagnosticPrinter.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/LTO/LTOCodeGenerator.h"
21 #include "llvm/LTO/LTOModule.h"
22 #include "llvm/Object/FunctionIndexObjectFile.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/TargetSelect.h"
29 #include "llvm/Support/ToolOutputFile.h"
30 #include "llvm/Support/raw_ostream.h"
37 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
43 static cl::opt<bool> DisableVerify(
44 "disable-verify", cl::init(false),
45 cl::desc("Do not run the verifier during the optimization pipeline"));
48 DisableInline("disable-inlining", cl::init(false),
49 cl::desc("Do not run the inliner pass"));
52 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
53 cl::desc("Do not run the GVN load PRE pass"));
56 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
57 cl::desc("Do not run loop or slp vectorization during LTO"));
60 UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
61 cl::desc("Use a diagnostic handler to test the handler interface"));
64 ThinLTO("thinlto", cl::init(false),
65 cl::desc("Only write combined global index for ThinLTO backends"));
68 SaveModuleFile("save-merged-module", cl::init(false),
69 cl::desc("Write merged LTO module to file before CodeGen"));
71 static cl::list<std::string>
72 InputFilenames(cl::Positional, cl::OneOrMore,
73 cl::desc("<input bitcode files>"));
75 static cl::opt<std::string>
76 OutputFilename("o", cl::init(""),
77 cl::desc("Override output filename"),
78 cl::value_desc("filename"));
80 static cl::list<std::string>
81 ExportedSymbols("exported-symbol",
82 cl::desc("Symbol to export from the resulting object file"),
85 static cl::list<std::string>
86 DSOSymbols("dso-symbol",
87 cl::desc("Symbol to put in the symtab in the resulting dso"),
90 static cl::opt<bool> ListSymbolsOnly(
91 "list-symbols-only", cl::init(false),
92 cl::desc("Instead of running LTO, list the symbols in each IR file"));
94 static cl::opt<bool> SetMergedModule(
95 "set-merged-module", cl::init(false),
96 cl::desc("Use the first input module as the merged module"));
98 static cl::opt<unsigned> Parallelism("j", cl::Prefix, cl::init(1),
99 cl::desc("Number of backend threads"));
103 std::vector<bool> CanBeHidden;
107 static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
108 const char *Msg, void *) {
109 errs() << "llvm-lto: ";
115 errs() << "remark: ";
121 errs() << "warning: ";
124 errs() << Msg << "\n";
127 static std::string CurrentActivity;
128 static void diagnosticHandler(const DiagnosticInfo &DI) {
129 raw_ostream &OS = errs();
131 switch (DI.getSeverity()) {
145 if (!CurrentActivity.empty())
146 OS << ' ' << CurrentActivity;
149 DiagnosticPrinterRawOStream DP(OS);
153 if (DI.getSeverity() == DS_Error)
157 static void diagnosticHandlerWithContenxt(const DiagnosticInfo &DI,
159 diagnosticHandler(DI);
162 static void error(const Twine &Msg) {
163 errs() << "llvm-lto: " << Msg << '\n';
167 static void error(std::error_code EC, const Twine &Prefix) {
169 error(Prefix + ": " + EC.message());
172 template <typename T>
173 static void error(const ErrorOr<T> &V, const Twine &Prefix) {
174 error(V.getError(), Prefix);
177 static std::unique_ptr<LTOModule>
178 getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer,
179 const TargetOptions &Options) {
180 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
181 MemoryBuffer::getFile(Path);
182 error(BufferOrErr, "error loading file '" + Path + "'");
183 Buffer = std::move(BufferOrErr.get());
184 CurrentActivity = ("loading file '" + Path + "'").str();
185 ErrorOr<std::unique_ptr<LTOModule>> Ret = LTOModule::createInLocalContext(
186 Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Path);
187 CurrentActivity = "";
188 return std::move(*Ret);
191 /// \brief List symbols in each IR file.
193 /// The main point here is to provide lit-testable coverage for the LTOModule
194 /// functionality that's exposed by the C API to list symbols. Moreover, this
195 /// provides testing coverage for modules that have been created in their own
197 static void listSymbols(const TargetOptions &Options) {
198 for (auto &Filename : InputFilenames) {
199 std::unique_ptr<MemoryBuffer> Buffer;
200 std::unique_ptr<LTOModule> Module =
201 getLocalLTOModule(Filename, Buffer, Options);
204 outs() << Filename << ":\n";
205 for (int I = 0, E = Module->getSymbolCount(); I != E; ++I)
206 outs() << Module->getSymbolName(I) << "\n";
210 /// Create a combined index file from the input IR files and write it.
212 /// This is meant to enable testing of ThinLTO combined index generation,
213 /// currently available via the gold plugin via -thinlto.
214 static void createCombinedFunctionIndex() {
215 FunctionInfoIndex CombinedIndex;
216 uint64_t NextModuleId = 0;
217 for (auto &Filename : InputFilenames) {
218 CurrentActivity = "loading file '" + Filename + "'";
219 ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
220 llvm::getFunctionIndexForFile(Filename, diagnosticHandler);
221 std::unique_ptr<FunctionInfoIndex> Index = std::move(IndexOrErr.get());
222 CurrentActivity = "";
223 // Skip files without a function summary.
226 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
229 assert(!OutputFilename.empty());
230 raw_fd_ostream OS(OutputFilename + ".thinlto.bc", EC,
231 sys::fs::OpenFlags::F_None);
232 error(EC, "error opening the file '" + OutputFilename + ".thinlto.bc'");
233 WriteFunctionSummaryToFile(CombinedIndex, OS);
237 int main(int argc, char **argv) {
238 // Print a stack trace if we signal out.
239 sys::PrintStackTraceOnErrorSignal();
240 PrettyStackTraceProgram X(argc, argv);
242 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
243 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
245 if (OptLevel < '0' || OptLevel > '3')
246 error("optimization level must be between 0 and 3");
248 // Initialize the configured targets.
249 InitializeAllTargets();
250 InitializeAllTargetMCs();
251 InitializeAllAsmPrinters();
252 InitializeAllAsmParsers();
254 // set up the TargetOptions for the machine
255 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
257 if (ListSymbolsOnly) {
258 listSymbols(Options);
263 createCombinedFunctionIndex();
267 unsigned BaseArg = 0;
270 Context.setDiagnosticHandler(diagnosticHandlerWithContenxt, nullptr, true);
272 LTOCodeGenerator CodeGen(Context);
274 if (UseDiagnosticHandler)
275 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
277 CodeGen.setCodePICModel(RelocModel);
279 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
280 CodeGen.setTargetOptions(Options);
282 llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
283 for (unsigned i = 0; i < DSOSymbols.size(); ++i)
284 DSOSymbolsSet.insert(DSOSymbols[i]);
286 std::vector<std::string> KeptDSOSyms;
288 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
289 CurrentActivity = "loading file '" + InputFilenames[i] + "'";
290 ErrorOr<std::unique_ptr<LTOModule>> ModuleOrErr =
291 LTOModule::createFromFile(Context, InputFilenames[i].c_str(), Options);
292 std::unique_ptr<LTOModule> &Module = *ModuleOrErr;
293 CurrentActivity = "";
295 unsigned NumSyms = Module->getSymbolCount();
296 for (unsigned I = 0; I < NumSyms; ++I) {
297 StringRef Name = Module->getSymbolName(I);
298 if (!DSOSymbolsSet.count(Name))
300 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
301 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
302 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
303 KeptDSOSyms.push_back(Name);
306 // We use the first input module as the destination module when
307 // SetMergedModule is true.
308 if (SetMergedModule && i == BaseArg) {
309 // Transfer ownership to the code generator.
310 CodeGen.setModule(std::move(Module));
311 } else if (!CodeGen.addModule(Module.get())) {
312 // Print a message here so that we know addModule() did not abort.
313 errs() << argv[0] << ": error adding file '" << InputFilenames[i] << "'\n";
318 // Add all the exported symbols to the table of symbols to preserve.
319 for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
320 CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
322 // Add all the dso symbols to the table of symbols to expose.
323 for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
324 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
326 // Set cpu and attrs strings for the default target/subtarget.
327 CodeGen.setCpu(MCPU.c_str());
329 CodeGen.setOptLevel(OptLevel - '0');
332 for (unsigned i = 0; i < MAttrs.size(); ++i) {
335 attrs.append(MAttrs[i]);
339 CodeGen.setAttr(attrs.c_str());
341 if (FileType.getNumOccurrences())
342 CodeGen.setFileType(FileType);
344 if (!OutputFilename.empty()) {
345 if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
346 DisableLTOVectorization)) {
347 // Diagnostic messages should have been printed by the handler.
348 errs() << argv[0] << ": error optimizing the code\n";
352 if (SaveModuleFile) {
353 std::string ModuleFilename = OutputFilename;
354 ModuleFilename += ".merged.bc";
357 if (!CodeGen.writeMergedModules(ModuleFilename.c_str())) {
358 errs() << argv[0] << ": writing merged module failed.\n";
363 std::list<tool_output_file> OSs;
364 std::vector<raw_pwrite_stream *> OSPtrs;
365 for (unsigned I = 0; I != Parallelism; ++I) {
366 std::string PartFilename = OutputFilename;
367 if (Parallelism != 1)
368 PartFilename += "." + utostr(I);
370 OSs.emplace_back(PartFilename, EC, sys::fs::F_None);
372 errs() << argv[0] << ": error opening the file '" << PartFilename
373 << "': " << EC.message() << "\n";
376 OSPtrs.push_back(&OSs.back().os());
379 if (!CodeGen.compileOptimized(OSPtrs)) {
380 // Diagnostic messages should have been printed by the handler.
381 errs() << argv[0] << ": error compiling the code\n";
385 for (tool_output_file &OS : OSs)
388 if (Parallelism != 1) {
389 errs() << argv[0] << ": -j must be specified together with -o\n";
393 if (SaveModuleFile) {
394 errs() << argv[0] << ": -save-merged-module must be specified with -o\n";
398 const char *OutputName = nullptr;
399 if (!CodeGen.compile_to_file(&OutputName, DisableVerify, DisableInline,
400 DisableGVNLoadPRE, DisableLTOVectorization)) {
401 // Diagnostic messages should have been printed by the handler.
402 errs() << argv[0] << ": error compiling the code\n";
406 outs() << "Wrote native object file '" << OutputName << "'\n";