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" |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/ReaderWriter.h" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/CommandFlags.h" |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 20 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 21 | #include "llvm/LTO/LTOModule.h" |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 22 | #include "llvm/Object/FunctionIndexObjectFile.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
| 26 | #include "llvm/Support/PrettyStackTrace.h" |
| 27 | #include "llvm/Support/Signals.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetSelect.h" |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ToolOutputFile.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 31 | #include <list> |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 35 | static cl::opt<char> |
| 36 | OptLevel("O", |
| 37 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 38 | "(default = '-O2')"), |
| 39 | cl::Prefix, |
| 40 | cl::ZeroOrMore, |
| 41 | cl::init('2')); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 42 | |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 43 | static cl::opt<bool> DisableVerify( |
| 44 | "disable-verify", cl::init(false), |
| 45 | cl::desc("Do not run the verifier during the optimization pipeline")); |
| 46 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
| 48 | DisableInline("disable-inlining", cl::init(false), |
| 49 | cl::desc("Do not run the inliner pass")); |
| 50 | |
| 51 | static cl::opt<bool> |
| 52 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 53 | cl::desc("Do not run the GVN load PRE pass")); |
| 54 | |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 55 | static cl::opt<bool> |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 56 | DisableLTOVectorization("disable-lto-vectorization", cl::init(false), |
| 57 | cl::desc("Do not run loop or slp vectorization during LTO")); |
| 58 | |
| 59 | static cl::opt<bool> |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 60 | UseDiagnosticHandler("use-diagnostic-handler", cl::init(false), |
| 61 | cl::desc("Use a diagnostic handler to test the handler interface")); |
| 62 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 63 | static cl::opt<bool> |
| 64 | ThinLTO("thinlto", cl::init(false), |
| 65 | cl::desc("Only write combined global index for ThinLTO backends")); |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 66 | |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 67 | static cl::opt<bool> |
| 68 | SaveModuleFile("save-merged-module", cl::init(false), |
| 69 | cl::desc("Write merged LTO module to file before CodeGen")); |
| 70 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 71 | static cl::list<std::string> |
| 72 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 73 | cl::desc("<input bitcode files>")); |
| 74 | |
| 75 | static cl::opt<std::string> |
| 76 | OutputFilename("o", cl::init(""), |
| 77 | cl::desc("Override output filename"), |
| 78 | cl::value_desc("filename")); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 79 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 80 | static cl::list<std::string> |
| 81 | ExportedSymbols("exported-symbol", |
| 82 | cl::desc("Symbol to export from the resulting object file"), |
| 83 | cl::ZeroOrMore); |
| 84 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 85 | static cl::list<std::string> |
| 86 | DSOSymbols("dso-symbol", |
| 87 | cl::desc("Symbol to put in the symtab in the resulting dso"), |
| 88 | cl::ZeroOrMore); |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 89 | |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 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")); |
| 93 | |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 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")); |
| 97 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 98 | static cl::opt<unsigned> Parallelism("j", cl::Prefix, cl::init(1), |
| 99 | cl::desc("Number of backend threads")); |
| 100 | |
Tobias Edler von Koch | 3f4f6f3e | 2016-01-18 23:35:24 +0000 | [diff] [blame] | 101 | static cl::opt<bool> RestoreGlobalsLinkage( |
| 102 | "restore-linkage", cl::init(false), |
| 103 | cl::desc("Restore original linkage of globals prior to CodeGen")); |
| 104 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 105 | namespace { |
| 106 | struct ModuleInfo { |
| 107 | std::vector<bool> CanBeHidden; |
| 108 | }; |
| 109 | } |
| 110 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 111 | static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity, |
| 112 | const char *Msg, void *) { |
Yunzhong Gao | ef436f0 | 2015-11-10 18:52:48 +0000 | [diff] [blame] | 113 | errs() << "llvm-lto: "; |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 114 | switch (Severity) { |
| 115 | case LTO_DS_NOTE: |
| 116 | errs() << "note: "; |
| 117 | break; |
| 118 | case LTO_DS_REMARK: |
| 119 | errs() << "remark: "; |
| 120 | break; |
| 121 | case LTO_DS_ERROR: |
| 122 | errs() << "error: "; |
| 123 | break; |
| 124 | case LTO_DS_WARNING: |
| 125 | errs() << "warning: "; |
| 126 | break; |
| 127 | } |
| 128 | errs() << Msg << "\n"; |
| 129 | } |
| 130 | |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 131 | static std::string CurrentActivity; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 132 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 133 | raw_ostream &OS = errs(); |
| 134 | OS << "llvm-lto: "; |
| 135 | switch (DI.getSeverity()) { |
| 136 | case DS_Error: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 137 | OS << "error"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 138 | break; |
| 139 | case DS_Warning: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 140 | OS << "warning"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 141 | break; |
| 142 | case DS_Remark: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 143 | OS << "remark"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 144 | break; |
| 145 | case DS_Note: |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 146 | OS << "note"; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 147 | break; |
| 148 | } |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 149 | if (!CurrentActivity.empty()) |
| 150 | OS << ' ' << CurrentActivity; |
| 151 | OS << ": "; |
Mehdi Amini | 354f520 | 2015-11-19 05:52:29 +0000 | [diff] [blame] | 152 | |
| 153 | DiagnosticPrinterRawOStream DP(OS); |
| 154 | DI.print(DP); |
| 155 | OS << '\n'; |
| 156 | |
| 157 | if (DI.getSeverity() == DS_Error) |
| 158 | exit(1); |
| 159 | } |
| 160 | |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame^] | 161 | static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, |
| 162 | void *Context) { |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 163 | diagnosticHandler(DI); |
| 164 | } |
| 165 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 166 | static void error(const Twine &Msg) { |
| 167 | errs() << "llvm-lto: " << Msg << '\n'; |
| 168 | exit(1); |
| 169 | } |
| 170 | |
| 171 | static void error(std::error_code EC, const Twine &Prefix) { |
| 172 | if (EC) |
| 173 | error(Prefix + ": " + EC.message()); |
| 174 | } |
| 175 | |
| 176 | template <typename T> |
| 177 | static void error(const ErrorOr<T> &V, const Twine &Prefix) { |
| 178 | error(V.getError(), Prefix); |
| 179 | } |
| 180 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 181 | static std::unique_ptr<LTOModule> |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 182 | getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer, |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 183 | const TargetOptions &Options) { |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 184 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 185 | MemoryBuffer::getFile(Path); |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 186 | error(BufferOrErr, "error loading file '" + Path + "'"); |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 187 | Buffer = std::move(BufferOrErr.get()); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 188 | CurrentActivity = ("loading file '" + Path + "'").str(); |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame^] | 189 | std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>(); |
| 190 | Context->setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 191 | ErrorOr<std::unique_ptr<LTOModule>> Ret = LTOModule::createInLocalContext( |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame^] | 192 | std::move(Context), Buffer->getBufferStart(), Buffer->getBufferSize(), |
| 193 | Options, Path); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 194 | CurrentActivity = ""; |
| 195 | return std::move(*Ret); |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /// \brief List symbols in each IR file. |
| 199 | /// |
| 200 | /// The main point here is to provide lit-testable coverage for the LTOModule |
| 201 | /// functionality that's exposed by the C API to list symbols. Moreover, this |
| 202 | /// provides testing coverage for modules that have been created in their own |
| 203 | /// contexts. |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 204 | static void listSymbols(const TargetOptions &Options) { |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 205 | for (auto &Filename : InputFilenames) { |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 206 | std::unique_ptr<MemoryBuffer> Buffer; |
| 207 | std::unique_ptr<LTOModule> Module = |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 208 | getLocalLTOModule(Filename, Buffer, Options); |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 209 | |
| 210 | // List the symbols. |
| 211 | outs() << Filename << ":\n"; |
| 212 | for (int I = 0, E = Module->getSymbolCount(); I != E; ++I) |
| 213 | outs() << Module->getSymbolName(I) << "\n"; |
| 214 | } |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 217 | /// Create a combined index file from the input IR files and write it. |
| 218 | /// |
| 219 | /// This is meant to enable testing of ThinLTO combined index generation, |
| 220 | /// currently available via the gold plugin via -thinlto. |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 221 | static void createCombinedFunctionIndex() { |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 222 | FunctionInfoIndex CombinedIndex; |
| 223 | uint64_t NextModuleId = 0; |
| 224 | for (auto &Filename : InputFilenames) { |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 225 | CurrentActivity = "loading file '" + Filename + "'"; |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 226 | ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr = |
Teresa Johnson | 6b92316 | 2015-11-23 19:19:11 +0000 | [diff] [blame] | 227 | llvm::getFunctionIndexForFile(Filename, diagnosticHandler); |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 228 | std::unique_ptr<FunctionInfoIndex> Index = std::move(IndexOrErr.get()); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 229 | CurrentActivity = ""; |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 230 | // Skip files without a function summary. |
| 231 | if (!Index) |
| 232 | continue; |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 233 | CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId); |
| 234 | } |
| 235 | std::error_code EC; |
| 236 | assert(!OutputFilename.empty()); |
| 237 | raw_fd_ostream OS(OutputFilename + ".thinlto.bc", EC, |
| 238 | sys::fs::OpenFlags::F_None); |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 239 | error(EC, "error opening the file '" + OutputFilename + ".thinlto.bc'"); |
Teresa Johnson | 3da931f | 2015-10-19 19:06:06 +0000 | [diff] [blame] | 240 | WriteFunctionSummaryToFile(CombinedIndex, OS); |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 241 | OS.close(); |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 244 | int main(int argc, char **argv) { |
| 245 | // Print a stack trace if we signal out. |
| 246 | sys::PrintStackTraceOnErrorSignal(); |
| 247 | PrettyStackTraceProgram X(argc, argv); |
| 248 | |
| 249 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 250 | cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n"); |
| 251 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 252 | if (OptLevel < '0' || OptLevel > '3') |
| 253 | error("optimization level must be between 0 and 3"); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 254 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 255 | // Initialize the configured targets. |
| 256 | InitializeAllTargets(); |
| 257 | InitializeAllTargetMCs(); |
| 258 | InitializeAllAsmPrinters(); |
| 259 | InitializeAllAsmParsers(); |
| 260 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 261 | // set up the TargetOptions for the machine |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 262 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 263 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 264 | if (ListSymbolsOnly) { |
| 265 | listSymbols(Options); |
| 266 | return 0; |
| 267 | } |
Duncan P. N. Exon Smith | f9abf4f | 2014-12-17 02:00:38 +0000 | [diff] [blame] | 268 | |
Rafael Espindola | 5e128db | 2015-12-04 00:45:57 +0000 | [diff] [blame] | 269 | if (ThinLTO) { |
| 270 | createCombinedFunctionIndex(); |
| 271 | return 0; |
| 272 | } |
Teresa Johnson | 91a88bb | 2015-10-19 14:30:44 +0000 | [diff] [blame] | 273 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 274 | unsigned BaseArg = 0; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 275 | |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 276 | LLVMContext Context; |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame^] | 277 | Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 278 | |
| 279 | LTOCodeGenerator CodeGen(Context); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 280 | |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 281 | if (UseDiagnosticHandler) |
| 282 | CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr); |
| 283 | |
Peter Collingbourne | 44ee84e | 2015-08-21 22:57:17 +0000 | [diff] [blame] | 284 | CodeGen.setCodePICModel(RelocModel); |
James Molloy | 951e529 | 2014-04-14 13:54:16 +0000 | [diff] [blame] | 285 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 286 | CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 287 | CodeGen.setTargetOptions(Options); |
Tobias Edler von Koch | 3f4f6f3e | 2016-01-18 23:35:24 +0000 | [diff] [blame] | 288 | CodeGen.setShouldRestoreGlobalsLinkage(RestoreGlobalsLinkage); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 289 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 290 | llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet; |
| 291 | for (unsigned i = 0; i < DSOSymbols.size(); ++i) |
| 292 | DSOSymbolsSet.insert(DSOSymbols[i]); |
| 293 | |
| 294 | std::vector<std::string> KeptDSOSyms; |
| 295 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 296 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 297 | CurrentActivity = "loading file '" + InputFilenames[i] + "'"; |
| 298 | ErrorOr<std::unique_ptr<LTOModule>> ModuleOrErr = |
| 299 | LTOModule::createFromFile(Context, InputFilenames[i].c_str(), Options); |
| 300 | std::unique_ptr<LTOModule> &Module = *ModuleOrErr; |
| 301 | CurrentActivity = ""; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 302 | |
Peter Collingbourne | 55217439 | 2015-08-21 19:09:42 +0000 | [diff] [blame] | 303 | unsigned NumSyms = Module->getSymbolCount(); |
| 304 | for (unsigned I = 0; I < NumSyms; ++I) { |
| 305 | StringRef Name = Module->getSymbolName(I); |
| 306 | if (!DSOSymbolsSet.count(Name)) |
| 307 | continue; |
| 308 | lto_symbol_attributes Attrs = Module->getSymbolAttributes(I); |
| 309 | unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK; |
| 310 | if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN) |
| 311 | KeptDSOSyms.push_back(Name); |
| 312 | } |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 313 | |
| 314 | // We use the first input module as the destination module when |
| 315 | // SetMergedModule is true. |
| 316 | if (SetMergedModule && i == BaseArg) { |
| 317 | // Transfer ownership to the code generator. |
Peter Collingbourne | 9c8909d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 318 | CodeGen.setModule(std::move(Module)); |
Yunzhong Gao | 46261a7 | 2015-09-11 20:01:53 +0000 | [diff] [blame] | 319 | } else if (!CodeGen.addModule(Module.get())) { |
| 320 | // Print a message here so that we know addModule() did not abort. |
| 321 | errs() << argv[0] << ": error adding file '" << InputFilenames[i] << "'\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 322 | return 1; |
Yunzhong Gao | 46261a7 | 2015-09-11 20:01:53 +0000 | [diff] [blame] | 323 | } |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Rafael Espindola | dafc53d | 2013-10-02 14:12:56 +0000 | [diff] [blame] | 326 | // Add all the exported symbols to the table of symbols to preserve. |
| 327 | for (unsigned i = 0; i < ExportedSymbols.size(); ++i) |
| 328 | CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str()); |
| 329 | |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 330 | // Add all the dso symbols to the table of symbols to expose. |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 331 | for (unsigned i = 0; i < KeptDSOSyms.size(); ++i) |
| 332 | CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str()); |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 333 | |
Akira Hatanaka | 23b5f67 | 2015-01-30 01:14:28 +0000 | [diff] [blame] | 334 | // Set cpu and attrs strings for the default target/subtarget. |
| 335 | CodeGen.setCpu(MCPU.c_str()); |
| 336 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 337 | CodeGen.setOptLevel(OptLevel - '0'); |
| 338 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 339 | std::string attrs; |
| 340 | for (unsigned i = 0; i < MAttrs.size(); ++i) { |
| 341 | if (i > 0) |
| 342 | attrs.append(","); |
| 343 | attrs.append(MAttrs[i]); |
| 344 | } |
| 345 | |
| 346 | if (!attrs.empty()) |
| 347 | CodeGen.setAttr(attrs.c_str()); |
| 348 | |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 349 | if (FileType.getNumOccurrences()) |
| 350 | CodeGen.setFileType(FileType); |
| 351 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 352 | if (!OutputFilename.empty()) { |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 353 | if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 354 | DisableLTOVectorization)) { |
| 355 | // Diagnostic messages should have been printed by the handler. |
| 356 | errs() << argv[0] << ": error optimizing the code\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 357 | return 1; |
| 358 | } |
| 359 | |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 360 | if (SaveModuleFile) { |
| 361 | std::string ModuleFilename = OutputFilename; |
| 362 | ModuleFilename += ".merged.bc"; |
| 363 | std::string ErrMsg; |
| 364 | |
| 365 | if (!CodeGen.writeMergedModules(ModuleFilename.c_str())) { |
| 366 | errs() << argv[0] << ": writing merged module failed.\n"; |
| 367 | return 1; |
| 368 | } |
| 369 | } |
| 370 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 371 | std::list<tool_output_file> OSs; |
| 372 | std::vector<raw_pwrite_stream *> OSPtrs; |
| 373 | for (unsigned I = 0; I != Parallelism; ++I) { |
| 374 | std::string PartFilename = OutputFilename; |
| 375 | if (Parallelism != 1) |
| 376 | PartFilename += "." + utostr(I); |
| 377 | std::error_code EC; |
| 378 | OSs.emplace_back(PartFilename, EC, sys::fs::F_None); |
| 379 | if (EC) { |
| 380 | errs() << argv[0] << ": error opening the file '" << PartFilename |
| 381 | << "': " << EC.message() << "\n"; |
| 382 | return 1; |
| 383 | } |
| 384 | OSPtrs.push_back(&OSs.back().os()); |
| 385 | } |
| 386 | |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 387 | if (!CodeGen.compileOptimized(OSPtrs)) { |
| 388 | // Diagnostic messages should have been printed by the handler. |
| 389 | errs() << argv[0] << ": error compiling the code\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 390 | return 1; |
| 391 | } |
| 392 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 393 | for (tool_output_file &OS : OSs) |
| 394 | OS.keep(); |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 395 | } else { |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 396 | if (Parallelism != 1) { |
| 397 | errs() << argv[0] << ": -j must be specified together with -o\n"; |
| 398 | return 1; |
| 399 | } |
| 400 | |
Tobias Edler von Koch | 49c9a6e | 2015-11-20 00:13:05 +0000 | [diff] [blame] | 401 | if (SaveModuleFile) { |
| 402 | errs() << argv[0] << ": -save-merged-module must be specified with -o\n"; |
| 403 | return 1; |
| 404 | } |
| 405 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 406 | const char *OutputName = nullptr; |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 407 | if (!CodeGen.compile_to_file(&OutputName, DisableVerify, DisableInline, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 408 | DisableGVNLoadPRE, DisableLTOVectorization)) { |
| 409 | // Diagnostic messages should have been printed by the handler. |
| 410 | errs() << argv[0] << ": error compiling the code\n"; |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 411 | return 1; |
| 412 | } |
| 413 | |
| 414 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 415 | } |
| 416 | |
Peter Collingbourne | 4e380b0 | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 417 | return 0; |
| 418 | } |