Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceCompiler.cpp - Driver for bitcode translation -------===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// This file defines a driver for translating PNaCl bitcode into native code. |
| 12 | /// It can either directly parse the binary bitcode file, or use LLVM routines |
| 13 | /// to parse a textual bitcode file into LLVM IR and then convert LLVM IR into |
| 14 | /// ICE. In either case, the high-level ICE is then compiled down to native |
| 15 | /// code, as either an ELF object file or a textual asm file. |
| 16 | /// |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 19 | #include "IceCompiler.h" |
| 20 | |
| 21 | #include "IceCfg.h" |
| 22 | #include "IceClFlags.h" |
| 23 | #include "IceClFlagsExtra.h" |
| 24 | #include "IceConverter.h" |
| 25 | #include "IceELFObjectWriter.h" |
| 26 | #include "PNaClTranslator.h" |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 27 | |
| 28 | #pragma clang diagnostic push |
| 29 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Karl Schimpf | 25529f7 | 2015-08-25 13:47:27 -0700 | [diff] [blame] | 31 | #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 32 | #include "llvm/IR/LLVMContext.h" |
| 33 | #include "llvm/IR/Module.h" |
| 34 | #include "llvm/IRReader/IRReader.h" |
| 35 | #include "llvm/Support/SourceMgr.h" |
| 36 | #include "llvm/Support/StreamingMemoryObject.h" |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 37 | #pragma clang diagnostic pop |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 38 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 39 | namespace Ice { |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | struct { |
| 44 | const char *FlagName; |
| 45 | int FlagValue; |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 46 | } ConditionalBuildAttributes[] = { |
| 47 | {"dump", BuildDefs::dump()}, |
| 48 | {"disable_ir_gen", BuildDefs::disableIrGen()}, |
| 49 | {"llvm_cl", BuildDefs::llvmCl()}, |
| 50 | {"llvm_ir", BuildDefs::llvmIr()}, |
| 51 | {"llvm_ir_as_input", BuildDefs::llvmIrAsInput()}, |
| 52 | {"minimal_build", BuildDefs::minimal()}, |
| 53 | {"browser_mode", PNACL_BROWSER_TRANSLATOR}}; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 54 | |
| 55 | // Validates values of build attributes. Prints them to Stream if |
| 56 | // Stream is non-null. |
Jim Stichnoth | 0769299 | 2015-06-01 13:39:24 -0700 | [diff] [blame] | 57 | void validateAndGenerateBuildAttributes(Ostream *Stream) { |
Jan Voung | 33492e7 | 2015-05-15 17:02:52 -0700 | [diff] [blame] | 58 | // List the supported targets. |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 59 | if (Stream) { |
Jan Voung | b36ad9b | 2015-04-21 17:01:49 -0700 | [diff] [blame] | 60 | #define SUBZERO_TARGET(TARGET) *Stream << "target_" #TARGET << "\n"; |
| 61 | #include "llvm/Config/SZTargets.def" |
| 62 | } |
| 63 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 64 | for (size_t i = 0; i < llvm::array_lengthof(ConditionalBuildAttributes); |
| 65 | ++i) { |
| 66 | switch (ConditionalBuildAttributes[i].FlagValue) { |
| 67 | case 0: |
| 68 | if (Stream) |
| 69 | *Stream << "no_" << ConditionalBuildAttributes[i].FlagName << "\n"; |
| 70 | break; |
| 71 | case 1: |
| 72 | if (Stream) |
| 73 | *Stream << "allow_" << ConditionalBuildAttributes[i].FlagName << "\n"; |
| 74 | break; |
| 75 | default: { |
| 76 | std::string Buffer; |
| 77 | llvm::raw_string_ostream StrBuf(Buffer); |
| 78 | StrBuf << "Flag " << ConditionalBuildAttributes[i].FlagName |
| 79 | << " must be defined as 0/1. Found: " |
| 80 | << ConditionalBuildAttributes[i].FlagValue; |
| 81 | llvm::report_fatal_error(StrBuf.str()); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } // end of anonymous namespace |
| 88 | |
| 89 | void Compiler::run(const Ice::ClFlagsExtra &ExtraFlags, GlobalContext &Ctx, |
| 90 | std::unique_ptr<llvm::DataStreamer> &&InputStream) { |
Jim Stichnoth | 0769299 | 2015-06-01 13:39:24 -0700 | [diff] [blame] | 91 | validateAndGenerateBuildAttributes( |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 92 | ExtraFlags.getGenerateBuildAtts() ? &Ctx.getStrDump() : nullptr); |
| 93 | if (ExtraFlags.getGenerateBuildAtts()) |
| 94 | return Ctx.getErrorStatus()->assign(EC_None); |
| 95 | |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 96 | if (!BuildDefs::disableIrGen() && Ctx.getFlags().getDisableIRGeneration()) { |
Jim Stichnoth | 992f91d | 2015-08-10 11:18:38 -0700 | [diff] [blame] | 97 | Ctx.getStrError() << "Error: Build doesn't allow --no-ir-gen when not " |
| 98 | << "ALLOW_DISABLE_IR_GEN!\n"; |
| 99 | return Ctx.getErrorStatus()->assign(EC_Args); |
| 100 | } |
| 101 | |
| 102 | // The Minimal build (specifically, when dump()/emit() are not implemented) |
| 103 | // allows only --filetype=obj. Check here to avoid cryptic error messages |
| 104 | // downstream. |
| 105 | if (!BuildDefs::dump() && Ctx.getFlags().getOutFileType() != FT_Elf) { |
| 106 | // TODO(stichnot): Access the actual command-line argument via |
| 107 | // llvm::Option.ArgStr and .ValueStr . |
| 108 | Ctx.getStrError() |
| 109 | << "Error: only --filetype=obj is supported in this build.\n"; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 110 | return Ctx.getErrorStatus()->assign(EC_Args); |
| 111 | } |
| 112 | |
| 113 | // Force -build-on-read=0 for .ll files. |
| 114 | const std::string LLSuffix = ".ll"; |
| 115 | const IceString &IRFilename = ExtraFlags.getIRFilename(); |
| 116 | bool BuildOnRead = ExtraFlags.getBuildOnRead(); |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 117 | if (BuildDefs::llvmIrAsInput() && IRFilename.length() >= LLSuffix.length() && |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 118 | IRFilename.compare(IRFilename.length() - LLSuffix.length(), |
| 119 | LLSuffix.length(), LLSuffix) == 0) |
| 120 | BuildOnRead = false; |
| 121 | |
| 122 | TimerMarker T(Ice::TimerStack::TT_szmain, &Ctx); |
| 123 | |
Jan Voung | fb79284 | 2015-06-11 15:27:50 -0700 | [diff] [blame] | 124 | Ctx.emitFileHeader(); |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 125 | Ctx.startWorkerThreads(); |
| 126 | |
| 127 | std::unique_ptr<Translator> Translator; |
| 128 | if (BuildOnRead) { |
| 129 | std::unique_ptr<PNaClTranslator> PTranslator(new PNaClTranslator(&Ctx)); |
| 130 | std::unique_ptr<llvm::StreamingMemoryObject> MemObj( |
| 131 | new llvm::StreamingMemoryObjectImpl(InputStream.release())); |
| 132 | PTranslator->translate(IRFilename, std::move(MemObj)); |
| 133 | Translator.reset(PTranslator.release()); |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 134 | } else if (BuildDefs::llvmIr()) { |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 135 | if (PNACL_BROWSER_TRANSLATOR) { |
Jim Stichnoth | 992f91d | 2015-08-10 11:18:38 -0700 | [diff] [blame] | 136 | Ctx.getStrError() |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 137 | << "non BuildOnRead is not supported w/ PNACL_BROWSER_TRANSLATOR\n"; |
| 138 | return Ctx.getErrorStatus()->assign(EC_Args); |
| 139 | } |
| 140 | // Parse the input LLVM IR file into a module. |
| 141 | llvm::SMDiagnostic Err; |
| 142 | TimerMarker T1(Ice::TimerStack::TT_parse, &Ctx); |
Karl Schimpf | 25529f7 | 2015-08-25 13:47:27 -0700 | [diff] [blame] | 143 | llvm::DiagnosticHandlerFunction DiagnosticHandler = |
| 144 | ExtraFlags.getLLVMVerboseErrors() |
| 145 | ? redirectNaClDiagnosticToStream(llvm::errs()) |
| 146 | : nullptr; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 147 | std::unique_ptr<llvm::Module> Mod = |
| 148 | NaClParseIRFile(IRFilename, ExtraFlags.getInputFileFormat(), Err, |
Karl Schimpf | 25529f7 | 2015-08-25 13:47:27 -0700 | [diff] [blame] | 149 | llvm::getGlobalContext(), DiagnosticHandler); |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 150 | if (!Mod) { |
| 151 | Err.print(ExtraFlags.getAppName().c_str(), llvm::errs()); |
| 152 | return Ctx.getErrorStatus()->assign(EC_Bitcode); |
| 153 | } |
| 154 | |
| 155 | std::unique_ptr<Converter> Converter(new class Converter(Mod.get(), &Ctx)); |
| 156 | Converter->convertToIce(); |
| 157 | Translator.reset(Converter.release()); |
| 158 | } else { |
Jim Stichnoth | 992f91d | 2015-08-10 11:18:38 -0700 | [diff] [blame] | 159 | Ctx.getStrError() << "Error: Build doesn't allow LLVM IR, " |
| 160 | << "--build-on-read=0 not allowed\n"; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 161 | return Ctx.getErrorStatus()->assign(EC_Args); |
| 162 | } |
| 163 | |
| 164 | Ctx.waitForWorkerThreads(); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 165 | if (Translator->getErrorStatus()) { |
| 166 | Ctx.getErrorStatus()->assign(Translator->getErrorStatus().value()); |
| 167 | } else { |
| 168 | Ctx.lowerGlobals("last"); |
| 169 | Ctx.lowerProfileData(); |
| 170 | Ctx.lowerConstants(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 171 | Ctx.lowerJumpTables(); |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 172 | |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 173 | if (Ctx.getFlags().getOutFileType() == FT_Elf) { |
| 174 | TimerMarker T1(Ice::TimerStack::TT_emit, &Ctx); |
| 175 | Ctx.getObjectWriter()->setUndefinedSyms(Ctx.getConstantExternSyms()); |
| 176 | Ctx.getObjectWriter()->writeNonUserSections(); |
| 177 | } |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 178 | } |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 179 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 180 | if (Ctx.getFlags().getSubzeroTimingEnabled()) |
| 181 | Ctx.dumpTimers(); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 182 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 183 | if (Ctx.getFlags().getTimeEachFunction()) { |
| 184 | const bool DumpCumulative = false; |
| 185 | Ctx.dumpTimers(GlobalContext::TSK_Funcs, DumpCumulative); |
| 186 | } |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 187 | constexpr bool FinalStats = true; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 188 | Ctx.dumpStats("_FINAL_", FinalStats); |
| 189 | } |
| 190 | |
| 191 | } // end of namespace Ice |