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" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/CommandFlags.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 17 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 18 | #include "llvm/LTO/LTOModule.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
| 22 | #include "llvm/Support/PrettyStackTrace.h" |
| 23 | #include "llvm/Support/Signals.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 24 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 29 | static cl::opt<bool> |
| 30 | DisableOpt("disable-opt", cl::init(false), |
| 31 | cl::desc("Do not run any optimization passes")); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 32 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 33 | static cl::opt<bool> |
| 34 | DisableInline("disable-inlining", cl::init(false), |
| 35 | cl::desc("Do not run the inliner pass")); |
| 36 | |
| 37 | static cl::opt<bool> |
| 38 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 39 | cl::desc("Do not run the GVN load PRE pass")); |
| 40 | |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 41 | static cl::opt<bool> |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame^] | 42 | DisableLTOVectorization("disable-lto-vectorization", cl::init(false), |
| 43 | cl::desc("Do not run loop or slp vectorization during LTO")); |
| 44 | |
| 45 | static cl::opt<bool> |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 46 | UseDiagnosticHandler("use-diagnostic-handler", cl::init(false), |
| 47 | cl::desc("Use a diagnostic handler to test the handler interface")); |
| 48 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 49 | static cl::list<std::string> |
| 50 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 51 | cl::desc("<input bitcode files>")); |
| 52 | |
| 53 | static cl::opt<std::string> |
| 54 | OutputFilename("o", cl::init(""), |
| 55 | cl::desc("Override output filename"), |
| 56 | cl::value_desc("filename")); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 57 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 58 | static cl::list<std::string> |
| 59 | ExportedSymbols("exported-symbol", |
| 60 | cl::desc("Symbol to export from the resulting object file"), |
| 61 | cl::ZeroOrMore); |
| 62 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 63 | static cl::list<std::string> |
| 64 | DSOSymbols("dso-symbol", |
| 65 | cl::desc("Symbol to put in the symtab in the resulting dso"), |
| 66 | cl::ZeroOrMore); |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 67 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 68 | namespace { |
| 69 | struct ModuleInfo { |
| 70 | std::vector<bool> CanBeHidden; |
| 71 | }; |
| 72 | } |
| 73 | |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 74 | void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity, |
| 75 | const char *Msg, void *) { |
| 76 | switch (Severity) { |
| 77 | case LTO_DS_NOTE: |
| 78 | errs() << "note: "; |
| 79 | break; |
| 80 | case LTO_DS_REMARK: |
| 81 | errs() << "remark: "; |
| 82 | break; |
| 83 | case LTO_DS_ERROR: |
| 84 | errs() << "error: "; |
| 85 | break; |
| 86 | case LTO_DS_WARNING: |
| 87 | errs() << "warning: "; |
| 88 | break; |
| 89 | } |
| 90 | errs() << Msg << "\n"; |
| 91 | } |
| 92 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 93 | int main(int argc, char **argv) { |
| 94 | // Print a stack trace if we signal out. |
| 95 | sys::PrintStackTraceOnErrorSignal(); |
| 96 | PrettyStackTraceProgram X(argc, argv); |
| 97 | |
| 98 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 99 | cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n"); |
| 100 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 101 | // Initialize the configured targets. |
| 102 | InitializeAllTargets(); |
| 103 | InitializeAllTargetMCs(); |
| 104 | InitializeAllAsmPrinters(); |
| 105 | InitializeAllAsmParsers(); |
| 106 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 107 | // set up the TargetOptions for the machine |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 108 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 109 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 110 | unsigned BaseArg = 0; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 111 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 112 | LTOCodeGenerator CodeGen; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 113 | |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 114 | if (UseDiagnosticHandler) |
| 115 | CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr); |
| 116 | |
James Molloy | 951e529 | 2014-04-14 13:54:16 +0000 | [diff] [blame] | 117 | switch (RelocModel) { |
| 118 | case Reloc::Static: |
| 119 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC); |
| 120 | break; |
| 121 | case Reloc::PIC_: |
| 122 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC); |
| 123 | break; |
| 124 | case Reloc::DynamicNoPIC: |
| 125 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC); |
| 126 | break; |
| 127 | default: |
| 128 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT); |
| 129 | } |
| 130 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 131 | CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 132 | CodeGen.setTargetOptions(Options); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 133 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 134 | llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet; |
| 135 | for (unsigned i = 0; i < DSOSymbols.size(); ++i) |
| 136 | DSOSymbolsSet.insert(DSOSymbols[i]); |
| 137 | |
| 138 | std::vector<std::string> KeptDSOSyms; |
| 139 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 140 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 141 | std::string error; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 142 | std::unique_ptr<LTOModule> Module( |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame] | 143 | LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error)); |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 144 | if (!error.empty()) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 145 | errs() << argv[0] << ": error loading file '" << InputFilenames[i] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 146 | << "': " << error << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 147 | return 1; |
| 148 | } |
| 149 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 150 | if (!CodeGen.addModule(Module.get())) |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 151 | return 1; |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 152 | |
| 153 | unsigned NumSyms = Module->getSymbolCount(); |
| 154 | for (unsigned I = 0; I < NumSyms; ++I) { |
| 155 | StringRef Name = Module->getSymbolName(I); |
| 156 | if (!DSOSymbolsSet.count(Name)) |
| 157 | continue; |
| 158 | lto_symbol_attributes Attrs = Module->getSymbolAttributes(I); |
| 159 | unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK; |
| 160 | if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN) |
| 161 | KeptDSOSyms.push_back(Name); |
| 162 | } |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 165 | // Add all the exported symbols to the table of symbols to preserve. |
| 166 | for (unsigned i = 0; i < ExportedSymbols.size(); ++i) |
| 167 | CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str()); |
| 168 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 169 | // Add all the dso symbols to the table of symbols to expose. |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 170 | for (unsigned i = 0; i < KeptDSOSyms.size(); ++i) |
| 171 | CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str()); |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 172 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 173 | std::string attrs; |
| 174 | for (unsigned i = 0; i < MAttrs.size(); ++i) { |
| 175 | if (i > 0) |
| 176 | attrs.append(","); |
| 177 | attrs.append(MAttrs[i]); |
| 178 | } |
| 179 | |
| 180 | if (!attrs.empty()) |
| 181 | CodeGen.setAttr(attrs.c_str()); |
| 182 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 183 | if (!OutputFilename.empty()) { |
| 184 | size_t len = 0; |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 185 | std::string ErrorInfo; |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame^] | 186 | const void *Code = |
| 187 | CodeGen.compile(&len, DisableOpt, DisableInline, DisableGVNLoadPRE, |
| 188 | DisableLTOVectorization, ErrorInfo); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 189 | if (!Code) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 190 | errs() << argv[0] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 191 | << ": error compiling the code: " << ErrorInfo << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 192 | return 1; |
| 193 | } |
| 194 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 195 | std::error_code EC; |
| 196 | raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None); |
| 197 | if (EC) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 198 | errs() << argv[0] << ": error opening the file '" << OutputFilename |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 199 | << "': " << EC.message() << "\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | FileStream.write(reinterpret_cast<const char *>(Code), len); |
| 204 | } else { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 205 | std::string ErrorInfo; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 206 | const char *OutputName = nullptr; |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 207 | if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline, |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame^] | 208 | DisableGVNLoadPRE, DisableLTOVectorization, |
| 209 | ErrorInfo)) { |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 210 | errs() << argv[0] |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 211 | << ": error compiling the code: " << ErrorInfo |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 212 | << "\n"; |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 217 | } |
| 218 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 219 | return 0; |
| 220 | } |