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