| OLD | NEW |
| 1 //===- subzero/src/llvm2ice.cpp - Driver for testing ----------------------===// | 1 //===- subzero/src/llvm2ice.cpp - Driver for testing ----------------------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file defines a driver that uses LLVM capabilities to parse a | 10 // This file defines a driver that uses LLVM capabilities to parse a |
| 11 // bitcode file and build the LLVM IR, and then convert the LLVM basic | 11 // bitcode file and build the LLVM IR, and then convert the LLVM basic |
| 12 // blocks, instructions, and operands into their Subzero equivalents. | 12 // blocks, instructions, and operands into their Subzero equivalents. |
| 13 // | 13 // |
| 14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
| 15 | 15 |
| 16 #include "IceCfg.h" |
| 16 #include "IceConverter.h" | 17 #include "IceConverter.h" |
| 17 #include "IceDefs.h" | 18 #include "IceDefs.h" |
| 19 #include "IceTargetLowering.h" |
| 18 #include "IceTypes.h" | 20 #include "IceTypes.h" |
| 19 | 21 |
| 22 #include "llvm/IR/Constant.h" |
| 23 #include "llvm/IR/Constants.h" |
| 20 #include "llvm/IR/LLVMContext.h" | 24 #include "llvm/IR/LLVMContext.h" |
| 25 #include "llvm/IR/Module.h" |
| 21 #include "llvm/IRReader/IRReader.h" | 26 #include "llvm/IRReader/IRReader.h" |
| 22 #include "llvm/Support/CommandLine.h" | 27 #include "llvm/Support/CommandLine.h" |
| 23 #include "llvm/Support/raw_os_ostream.h" | 28 #include "llvm/Support/raw_os_ostream.h" |
| 24 #include "llvm/Support/SourceMgr.h" | 29 #include "llvm/Support/SourceMgr.h" |
| 25 | 30 |
| 26 #include <fstream> | 31 #include <fstream> |
| 27 #include <iostream> | 32 #include <iostream> |
| 28 | 33 |
| 29 using namespace llvm; | 34 using namespace llvm; |
| 30 | 35 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 static cl::opt<bool> SubzeroTimingEnabled( | 92 static cl::opt<bool> SubzeroTimingEnabled( |
| 88 "timing", cl::desc("Enable breakdown timing of Subzero translation")); | 93 "timing", cl::desc("Enable breakdown timing of Subzero translation")); |
| 89 | 94 |
| 90 static cl::opt<NaClFileFormat> InputFileFormat( | 95 static cl::opt<NaClFileFormat> InputFileFormat( |
| 91 "bitcode-format", cl::desc("Define format of input file:"), | 96 "bitcode-format", cl::desc("Define format of input file:"), |
| 92 cl::values(clEnumValN(LLVMFormat, "llvm", "LLVM file (default)"), | 97 cl::values(clEnumValN(LLVMFormat, "llvm", "LLVM file (default)"), |
| 93 clEnumValN(PNaClFormat, "pnacl", "PNaCl bitcode file"), | 98 clEnumValN(PNaClFormat, "pnacl", "PNaCl bitcode file"), |
| 94 clEnumValEnd), | 99 clEnumValEnd), |
| 95 cl::init(LLVMFormat)); | 100 cl::init(LLVMFormat)); |
| 96 | 101 |
| 97 static cl::opt<bool> BuildOnRead( | 102 static cl::opt<bool> |
| 98 "build-on-read", cl::desc("Build ICE instructions when reading bitcode"), | 103 BuildOnRead("build-on-read", |
| 99 cl::init(false)); | 104 cl::desc("Build ICE instructions when reading bitcode"), |
| 105 cl::init(false)); |
| 100 | 106 |
| 101 int main(int argc, char **argv) { | 107 int main(int argc, char **argv) { |
| 102 | 108 |
| 103 cl::ParseCommandLineOptions(argc, argv); | 109 cl::ParseCommandLineOptions(argc, argv); |
| 104 | 110 |
| 105 Ice::VerboseMask VMask = Ice::IceV_None; | 111 Ice::VerboseMask VMask = Ice::IceV_None; |
| 106 for (unsigned i = 0; i != VerboseList.size(); ++i) | 112 for (unsigned i = 0; i != VerboseList.size(); ++i) |
| 107 VMask |= VerboseList[i]; | 113 VMask |= VerboseList[i]; |
| 108 | 114 |
| 109 std::ofstream Ofs; | 115 std::ofstream Ofs; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 122 | 128 |
| 123 Ice::GlobalContext Ctx(Ls, Os, VMask, TargetArch, OptLevel, TestPrefix); | 129 Ice::GlobalContext Ctx(Ls, Os, VMask, TargetArch, OptLevel, TestPrefix); |
| 124 | 130 |
| 125 if (BuildOnRead) { | 131 if (BuildOnRead) { |
| 126 std::cerr << "Direct build from bitcode not implemented yet!\n"; | 132 std::cerr << "Direct build from bitcode not implemented yet!\n"; |
| 127 return 1; | 133 return 1; |
| 128 } else { | 134 } else { |
| 129 // Parse the input LLVM IR file into a module. | 135 // Parse the input LLVM IR file into a module. |
| 130 SMDiagnostic Err; | 136 SMDiagnostic Err; |
| 131 Ice::Timer T; | 137 Ice::Timer T; |
| 132 Module *Mod = NaClParseIRFile(IRFilename, InputFileFormat, Err, | 138 Module *Mod = |
| 133 getGlobalContext()); | 139 NaClParseIRFile(IRFilename, InputFileFormat, Err, getGlobalContext()); |
| 134 | 140 |
| 135 if (SubzeroTimingEnabled) { | 141 if (SubzeroTimingEnabled) { |
| 136 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec() | 142 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec() |
| 137 << " sec\n"; | 143 << " sec\n"; |
| 138 } | 144 } |
| 139 | 145 |
| 140 if (!Mod) { | 146 if (!Mod) { |
| 141 Err.print(argv[0], errs()); | 147 Err.print(argv[0], errs()); |
| 142 return 1; | 148 return 1; |
| 143 } | 149 } |
| 144 | 150 |
| 151 // TODO(stichnot): Move this into IceConverter.cpp. |
| 152 OwningPtr<Ice::TargetGlobalInitLowering> GlobalLowering( |
| 153 Ice::TargetGlobalInitLowering::createLowering(TargetArch, &Ctx)); |
| 154 for (Module::const_global_iterator I = Mod->global_begin(), |
| 155 E = Mod->global_end(); |
| 156 I != E; ++I) { |
| 157 if (!I->hasInitializer()) |
| 158 continue; |
| 159 const Constant *Initializer = I->getInitializer(); |
| 160 Ice::IceString Name = I->getName(); |
| 161 unsigned Align = I->getAlignment(); |
| 162 uint64_t NumElements = 0; |
| 163 const char *Data = NULL; |
| 164 bool IsInternal = I->hasInternalLinkage(); |
| 165 bool IsConst = I->isConstant(); |
| 166 bool IsZeroInitializer = false; |
| 167 |
| 168 if (const ConstantDataArray *CDA = |
| 169 dyn_cast<ConstantDataArray>(Initializer)) { |
| 170 NumElements = CDA->getNumElements(); |
| 171 assert(isa<IntegerType>(CDA->getElementType()) && |
| 172 cast<IntegerType>(CDA->getElementType())->getBitWidth() == 8); |
| 173 Data = CDA->getRawDataValues().data(); |
| 174 } else if (isa<ConstantAggregateZero>(Initializer)) { |
| 175 if (const ArrayType *AT = dyn_cast<ArrayType>(Initializer->getType())) { |
| 176 assert(isa<IntegerType>(AT->getElementType()) && |
| 177 cast<IntegerType>(AT->getElementType())->getBitWidth() == 8); |
| 178 NumElements = AT->getNumElements(); |
| 179 IsZeroInitializer = true; |
| 180 } else { |
| 181 llvm_unreachable("Unhandled constant aggregate zero type"); |
| 182 } |
| 183 } else { |
| 184 llvm_unreachable("Unhandled global initializer"); |
| 185 } |
| 186 |
| 187 GlobalLowering->lower(Name, Align, IsInternal, IsConst, IsZeroInitializer, |
| 188 NumElements, Data, DisableTranslation); |
| 189 } |
| 190 GlobalLowering.reset(); |
| 191 |
| 145 Ice::Converter Converter(&Ctx, DisableInternal, SubzeroTimingEnabled, | 192 Ice::Converter Converter(&Ctx, DisableInternal, SubzeroTimingEnabled, |
| 146 DisableTranslation); | 193 DisableTranslation); |
| 147 return Converter.convertToIce(Mod); | 194 return Converter.convertToIce(Mod); |
| 148 } | 195 } |
| 149 } | 196 } |
| OLD | NEW |