David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1 | // Copyright 2017 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifdef _MSC_VER |
| 16 | #pragma warning(push, 0) |
| 17 | #endif |
| 18 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 19 | #include <cassert> |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 20 | #include <cstring> |
| 21 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 22 | #include <unordered_set> |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 23 | #include <memory> |
| 24 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 25 | #include <clspv/Option.h> |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 26 | #include <clspv/Passes.h> |
| 27 | |
| 28 | #include <llvm/ADT/StringSwitch.h> |
| 29 | #include <llvm/ADT/UniqueVector.h> |
| 30 | #include <llvm/Analysis/LoopInfo.h> |
| 31 | #include <llvm/IR/Constants.h> |
| 32 | #include <llvm/IR/Dominators.h> |
| 33 | #include <llvm/IR/Instructions.h> |
| 34 | #include <llvm/IR/Metadata.h> |
| 35 | #include <llvm/IR/Module.h> |
| 36 | #include <llvm/Pass.h> |
David Neto | cd8ca5f | 2017-10-02 23:34:11 -0400 | [diff] [blame] | 37 | #include <llvm/Support/CommandLine.h> |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 38 | #include <llvm/Support/raw_ostream.h> |
| 39 | #include <llvm/Transforms/Utils/Cloning.h> |
| 40 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 41 | #include "spirv/1.0/spirv.hpp" |
| 42 | #include "clspv/AddressSpace.h" |
| 43 | #include "clspv/spirv_c_strings.hpp" |
| 44 | #include "clspv/spirv_glsl.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 45 | |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 46 | #include "ArgKind.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 47 | #include "ConstantEmitter.h" |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 48 | #include "Constants.h" |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 49 | #include "DescriptorCounter.h" |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 50 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 51 | #include <list> |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 52 | #include <iomanip> |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 53 | #include <set> |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 54 | #include <sstream> |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 55 | #include <string> |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 56 | #include <tuple> |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 57 | #include <utility> |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 58 | |
| 59 | #if defined(_MSC_VER) |
| 60 | #pragma warning(pop) |
| 61 | #endif |
| 62 | |
| 63 | using namespace llvm; |
| 64 | using namespace clspv; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 65 | using namespace mdconst; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 66 | |
| 67 | namespace { |
David Neto | cd8ca5f | 2017-10-02 23:34:11 -0400 | [diff] [blame] | 68 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 69 | cl::opt<bool> ShowResourceVars("show-rv", cl::init(false), cl::Hidden, |
| 70 | cl::desc("Show resource variable creation")); |
| 71 | |
| 72 | // These hacks exist to help transition code generation algorithms |
| 73 | // without making huge noise in detailed test output. |
| 74 | const bool Hack_generate_runtime_array_stride_early = true; |
| 75 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 76 | // The value of 1/pi. This value is from MSDN |
| 77 | // https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx |
| 78 | const double kOneOverPi = 0.318309886183790671538; |
| 79 | const glsl::ExtInst kGlslExtInstBad = static_cast<glsl::ExtInst>(0); |
| 80 | |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 81 | const char* kCompositeConstructFunctionPrefix = "clspv.composite_construct."; |
| 82 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 83 | enum SPIRVOperandType { |
| 84 | NUMBERID, |
| 85 | LITERAL_INTEGER, |
| 86 | LITERAL_STRING, |
| 87 | LITERAL_FLOAT |
| 88 | }; |
| 89 | |
| 90 | struct SPIRVOperand { |
| 91 | explicit SPIRVOperand(SPIRVOperandType Ty, uint32_t Num) |
| 92 | : Type(Ty), LiteralNum(1, Num) {} |
| 93 | explicit SPIRVOperand(SPIRVOperandType Ty, const char *Str) |
| 94 | : Type(Ty), LiteralStr(Str) {} |
| 95 | explicit SPIRVOperand(SPIRVOperandType Ty, StringRef Str) |
| 96 | : Type(Ty), LiteralStr(Str) {} |
| 97 | explicit SPIRVOperand(SPIRVOperandType Ty, ArrayRef<uint32_t> NumVec) |
| 98 | : Type(Ty), LiteralNum(NumVec.begin(), NumVec.end()) {} |
| 99 | |
| 100 | SPIRVOperandType getType() { return Type; }; |
| 101 | uint32_t getNumID() { return LiteralNum[0]; }; |
| 102 | std::string getLiteralStr() { return LiteralStr; }; |
| 103 | ArrayRef<uint32_t> getLiteralNum() { return LiteralNum; }; |
| 104 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 105 | uint32_t GetNumWords() const { |
| 106 | switch (Type) { |
| 107 | case NUMBERID: |
| 108 | return 1; |
| 109 | case LITERAL_INTEGER: |
| 110 | case LITERAL_FLOAT: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 111 | return uint32_t(LiteralNum.size()); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 112 | case LITERAL_STRING: |
| 113 | // Account for the terminating null character. |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 114 | return uint32_t((LiteralStr.size() + 4) / 4); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 115 | } |
| 116 | llvm_unreachable("Unhandled case in SPIRVOperand::GetNumWords()"); |
| 117 | } |
| 118 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 119 | private: |
| 120 | SPIRVOperandType Type; |
| 121 | std::string LiteralStr; |
| 122 | SmallVector<uint32_t, 4> LiteralNum; |
| 123 | }; |
| 124 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 125 | class SPIRVOperandList { |
| 126 | public: |
| 127 | SPIRVOperandList() {} |
| 128 | SPIRVOperandList(const SPIRVOperandList& other) = delete; |
| 129 | SPIRVOperandList(SPIRVOperandList&& other) { |
| 130 | contents_ = std::move(other.contents_); |
| 131 | other.contents_.clear(); |
| 132 | } |
| 133 | SPIRVOperandList(ArrayRef<SPIRVOperand *> init) |
| 134 | : contents_(init.begin(), init.end()) {} |
| 135 | operator ArrayRef<SPIRVOperand *>() { return contents_; } |
| 136 | void push_back(SPIRVOperand *op) { contents_.push_back(op); } |
| 137 | void clear() { contents_.clear();} |
| 138 | size_t size() const { return contents_.size(); } |
| 139 | SPIRVOperand *&operator[](size_t i) { return contents_[i]; } |
| 140 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 141 | const SmallVector<SPIRVOperand *, 8> &getOperands() const { |
| 142 | return contents_; |
| 143 | } |
| 144 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 145 | private: |
| 146 | SmallVector<SPIRVOperand *,8> contents_; |
| 147 | }; |
| 148 | |
| 149 | SPIRVOperandList &operator<<(SPIRVOperandList &list, SPIRVOperand *elem) { |
| 150 | list.push_back(elem); |
| 151 | return list; |
| 152 | } |
| 153 | |
| 154 | SPIRVOperand* MkNum(uint32_t num) { |
| 155 | return new SPIRVOperand(LITERAL_INTEGER, num); |
| 156 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 157 | SPIRVOperand* MkInteger(ArrayRef<uint32_t> num_vec) { |
| 158 | return new SPIRVOperand(LITERAL_INTEGER, num_vec); |
| 159 | } |
| 160 | SPIRVOperand* MkFloat(ArrayRef<uint32_t> num_vec) { |
| 161 | return new SPIRVOperand(LITERAL_FLOAT, num_vec); |
| 162 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 163 | SPIRVOperand* MkId(uint32_t id) { |
| 164 | return new SPIRVOperand(NUMBERID, id); |
| 165 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 166 | SPIRVOperand* MkString(StringRef str) { |
| 167 | return new SPIRVOperand(LITERAL_STRING, str); |
| 168 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 169 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 170 | struct SPIRVInstruction { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 171 | // Create an instruction with an opcode and no result ID, and with the given |
| 172 | // operands. This computes its own word count. |
| 173 | explicit SPIRVInstruction(spv::Op Opc, ArrayRef<SPIRVOperand *> Ops) |
| 174 | : WordCount(1), Opcode(static_cast<uint16_t>(Opc)), ResultID(0), |
| 175 | Operands(Ops.begin(), Ops.end()) { |
| 176 | for (auto *operand : Ops) { |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 177 | WordCount += uint16_t(operand->GetNumWords()); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | // Create an instruction with an opcode and a no-zero result ID, and |
| 181 | // with the given operands. This computes its own word count. |
| 182 | explicit SPIRVInstruction(spv::Op Opc, uint32_t ResID, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 183 | ArrayRef<SPIRVOperand *> Ops) |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 184 | : WordCount(2), Opcode(static_cast<uint16_t>(Opc)), ResultID(ResID), |
| 185 | Operands(Ops.begin(), Ops.end()) { |
| 186 | if (ResID == 0) { |
| 187 | llvm_unreachable("Result ID of 0 was provided"); |
| 188 | } |
| 189 | for (auto *operand : Ops) { |
| 190 | WordCount += operand->GetNumWords(); |
| 191 | } |
| 192 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 193 | |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 194 | uint32_t getWordCount() const { return WordCount; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 195 | uint16_t getOpcode() const { return Opcode; } |
| 196 | uint32_t getResultID() const { return ResultID; } |
| 197 | ArrayRef<SPIRVOperand *> getOperands() const { return Operands; } |
| 198 | |
| 199 | private: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 200 | uint32_t WordCount; // Check the 16-bit bound at code generation time. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 201 | uint16_t Opcode; |
| 202 | uint32_t ResultID; |
| 203 | SmallVector<SPIRVOperand *, 4> Operands; |
| 204 | }; |
| 205 | |
| 206 | struct SPIRVProducerPass final : public ModulePass { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 207 | typedef DenseMap<Type *, uint32_t> TypeMapType; |
| 208 | typedef UniqueVector<Type *> TypeList; |
| 209 | typedef DenseMap<Value *, uint32_t> ValueMapType; |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 210 | typedef UniqueVector<Value *> ValueList; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 211 | typedef std::vector<std::pair<Value *, uint32_t>> EntryPointVecType; |
| 212 | typedef std::list<SPIRVInstruction *> SPIRVInstructionList; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 213 | // A vector of tuples, each of which is: |
| 214 | // - the LLVM instruction that we will later generate SPIR-V code for |
| 215 | // - where the SPIR-V instruction should be inserted |
| 216 | // - the result ID of the SPIR-V instruction |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 217 | typedef std::vector< |
| 218 | std::tuple<Value *, SPIRVInstructionList::iterator, uint32_t>> |
| 219 | DeferredInstVecType; |
| 220 | typedef DenseMap<FunctionType *, std::pair<FunctionType *, uint32_t>> |
| 221 | GlobalConstFuncMapType; |
| 222 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 223 | explicit SPIRVProducerPass( |
| 224 | raw_pwrite_stream &out, raw_ostream &descriptor_map_out, |
| 225 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, bool outputAsm, |
| 226 | bool outputCInitList) |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 227 | : ModulePass(ID), samplerMap(samplerMap), out(out), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 228 | binaryTempOut(binaryTempUnderlyingVector), binaryOut(&out), |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 229 | descriptorMapOut(descriptor_map_out), outputAsm(outputAsm), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 230 | outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1), |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 231 | OpExtInstImportID(0), HasVariablePointers(false), SamplerTy(nullptr), |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 232 | WorkgroupSizeValueID(0), WorkgroupSizeVarID(0), |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 233 | max_local_spec_id_(0), constant_i32_zero_id_(0) {} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 234 | |
| 235 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 236 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 237 | AU.addRequired<LoopInfoWrapperPass>(); |
| 238 | } |
| 239 | |
| 240 | virtual bool runOnModule(Module &module) override; |
| 241 | |
| 242 | // output the SPIR-V header block |
| 243 | void outputHeader(); |
| 244 | |
| 245 | // patch the SPIR-V header block |
| 246 | void patchHeader(); |
| 247 | |
| 248 | uint32_t lookupType(Type *Ty) { |
| 249 | if (Ty->isPointerTy() && |
| 250 | (Ty->getPointerAddressSpace() != AddressSpace::UniformConstant)) { |
| 251 | auto PointeeTy = Ty->getPointerElementType(); |
| 252 | if (PointeeTy->isStructTy() && |
| 253 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 254 | Ty = PointeeTy; |
| 255 | } |
| 256 | } |
| 257 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 258 | auto where = TypeMap.find(Ty); |
| 259 | if (where == TypeMap.end()) { |
| 260 | if (Ty) { |
| 261 | errs() << "Unhandled type " << *Ty << "\n"; |
| 262 | } else { |
| 263 | errs() << "Unhandled type (null)\n"; |
| 264 | } |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 265 | llvm_unreachable("\nUnhandled type!"); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 266 | } |
| 267 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 268 | return where->second; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 269 | } |
| 270 | TypeMapType &getImageTypeMap() { return ImageTypeMap; } |
| 271 | TypeList &getTypeList() { return Types; }; |
| 272 | ValueList &getConstantList() { return Constants; }; |
| 273 | ValueMapType &getValueMap() { return ValueMap; } |
| 274 | ValueMapType &getAllocatedValueMap() { return AllocatedValueMap; } |
| 275 | SPIRVInstructionList &getSPIRVInstList() { return SPIRVInsts; }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 276 | EntryPointVecType &getEntryPointVec() { return EntryPointVec; }; |
| 277 | DeferredInstVecType &getDeferredInstVec() { return DeferredInstVec; }; |
| 278 | ValueList &getEntryPointInterfacesVec() { return EntryPointInterfacesVec; }; |
| 279 | uint32_t &getOpExtInstImportID() { return OpExtInstImportID; }; |
| 280 | std::vector<uint32_t> &getBuiltinDimVec() { return BuiltinDimensionVec; }; |
| 281 | bool hasVariablePointers() { return true; /* We use StorageBuffer everywhere */ }; |
| 282 | void setVariablePointers(bool Val) { HasVariablePointers = Val; }; |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 283 | ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() { return samplerMap; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 284 | GlobalConstFuncMapType &getGlobalConstFuncTypeMap() { |
| 285 | return GlobalConstFuncTypeMap; |
| 286 | } |
| 287 | SmallPtrSet<Value *, 16> &getGlobalConstArgSet() { |
| 288 | return GlobalConstArgumentSet; |
| 289 | } |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 290 | TypeList &getTypesNeedingArrayStride() { |
| 291 | return TypesNeedingArrayStride; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 292 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 293 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 294 | void GenerateLLVMIRInfo(Module &M, const DataLayout &DL); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 295 | // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will *not* |
| 296 | // be converted to a storage buffer, replace each such global variable with |
| 297 | // one in the storage class expecgted by SPIR-V. |
| 298 | void FindGlobalConstVars(Module &M, const DataLayout &DL); |
| 299 | // Populate ResourceVarInfoList, FunctionToResourceVarsMap, and |
| 300 | // ModuleOrderedResourceVars. |
| 301 | void FindResourceVars(Module &M, const DataLayout &DL); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 302 | void FindWorkgroupVars(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 303 | bool FindExtInst(Module &M); |
| 304 | void FindTypePerGlobalVar(GlobalVariable &GV); |
| 305 | void FindTypePerFunc(Function &F); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 306 | void FindTypesForSamplerMap(Module &M); |
| 307 | void FindTypesForResourceVars(Module &M); |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 308 | // Inserts |Ty| and relevant sub-types into the |Types| member, indicating that |
| 309 | // |Ty| and its subtypes will need a corresponding SPIR-V type. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 310 | void FindType(Type *Ty); |
| 311 | void FindConstantPerGlobalVar(GlobalVariable &GV); |
| 312 | void FindConstantPerFunc(Function &F); |
| 313 | void FindConstant(Value *V); |
| 314 | void GenerateExtInstImport(); |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 315 | // Generates instructions for SPIR-V types corresponding to the LLVM types |
| 316 | // saved in the |Types| member. A type follows its subtypes. IDs are |
| 317 | // allocated sequentially starting with the current value of nextID, and |
| 318 | // with a type following its subtypes. Also updates nextID to just beyond |
| 319 | // the last generated ID. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 320 | void GenerateSPIRVTypes(LLVMContext& context, const DataLayout &DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 321 | void GenerateSPIRVConstants(); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 322 | void GenerateModuleInfo(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 323 | void GenerateGlobalVar(GlobalVariable &GV); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 324 | void GenerateWorkgroupVars(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 325 | // Generate descriptor map entries for resource variables associated with |
| 326 | // arguments to F. |
| 327 | void GenerateDescriptorMapInfo(const DataLayout& DL, Function& F); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 328 | void GenerateSamplers(Module &M); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 329 | // Generate OpVariables for %clspv.resource.var.* calls. |
| 330 | void GenerateResourceVars(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 331 | void GenerateFuncPrologue(Function &F); |
| 332 | void GenerateFuncBody(Function &F); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 333 | void GenerateEntryPointInitialStores(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 334 | spv::Op GetSPIRVCmpOpcode(CmpInst *CmpI); |
| 335 | spv::Op GetSPIRVCastOpcode(Instruction &I); |
| 336 | spv::Op GetSPIRVBinaryOpcode(Instruction &I); |
| 337 | void GenerateInstruction(Instruction &I); |
| 338 | void GenerateFuncEpilogue(); |
| 339 | void HandleDeferredInstruction(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 340 | void HandleDeferredDecorations(const DataLayout& DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 341 | bool is4xi8vec(Type *Ty) const; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 342 | // Return the SPIR-V Id for 32-bit constant zero. The constant must already |
| 343 | // have been created. |
| 344 | uint32_t GetI32Zero(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 345 | spv::StorageClass GetStorageClass(unsigned AddrSpace) const; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 346 | spv::StorageClass GetStorageClassForArgKind(clspv::ArgKind arg_kind) const; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 347 | spv::BuiltIn GetBuiltin(StringRef globalVarName) const; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 348 | // Returns the GLSL extended instruction enum that the given function |
| 349 | // call maps to. If none, then returns the 0 value, i.e. GLSLstd4580Bad. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 350 | glsl::ExtInst getExtInstEnum(StringRef Name); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 351 | // Returns the GLSL extended instruction enum indirectly used by the given |
| 352 | // function. That is, to implement the given function, we use an extended |
| 353 | // instruction plus one more instruction. If none, then returns the 0 value, |
| 354 | // i.e. GLSLstd4580Bad. |
| 355 | glsl::ExtInst getIndirectExtInstEnum(StringRef Name); |
| 356 | // Returns the single GLSL extended instruction used directly or |
| 357 | // indirectly by the given function call. |
| 358 | glsl::ExtInst getDirectOrIndirectExtInstEnum(StringRef Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 359 | void PrintResID(SPIRVInstruction *Inst); |
| 360 | void PrintOpcode(SPIRVInstruction *Inst); |
| 361 | void PrintOperand(SPIRVOperand *Op); |
| 362 | void PrintCapability(SPIRVOperand *Op); |
| 363 | void PrintExtInst(SPIRVOperand *Op); |
| 364 | void PrintAddrModel(SPIRVOperand *Op); |
| 365 | void PrintMemModel(SPIRVOperand *Op); |
| 366 | void PrintExecModel(SPIRVOperand *Op); |
| 367 | void PrintExecMode(SPIRVOperand *Op); |
| 368 | void PrintSourceLanguage(SPIRVOperand *Op); |
| 369 | void PrintFuncCtrl(SPIRVOperand *Op); |
| 370 | void PrintStorageClass(SPIRVOperand *Op); |
| 371 | void PrintDecoration(SPIRVOperand *Op); |
| 372 | void PrintBuiltIn(SPIRVOperand *Op); |
| 373 | void PrintSelectionControl(SPIRVOperand *Op); |
| 374 | void PrintLoopControl(SPIRVOperand *Op); |
| 375 | void PrintDimensionality(SPIRVOperand *Op); |
| 376 | void PrintImageFormat(SPIRVOperand *Op); |
| 377 | void PrintMemoryAccess(SPIRVOperand *Op); |
| 378 | void PrintImageOperandsType(SPIRVOperand *Op); |
| 379 | void WriteSPIRVAssembly(); |
| 380 | void WriteOneWord(uint32_t Word); |
| 381 | void WriteResultID(SPIRVInstruction *Inst); |
| 382 | void WriteWordCountAndOpcode(SPIRVInstruction *Inst); |
| 383 | void WriteOperand(SPIRVOperand *Op); |
| 384 | void WriteSPIRVBinary(); |
| 385 | |
| 386 | private: |
| 387 | static char ID; |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 388 | ArrayRef<std::pair<unsigned, std::string>> samplerMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 389 | raw_pwrite_stream &out; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 390 | |
| 391 | // TODO(dneto): Wouldn't it be better to always just emit a binary, and then |
| 392 | // convert to other formats on demand? |
| 393 | |
| 394 | // When emitting a C initialization list, the WriteSPIRVBinary method |
| 395 | // will actually write its words to this vector via binaryTempOut. |
| 396 | SmallVector<char, 100> binaryTempUnderlyingVector; |
| 397 | raw_svector_ostream binaryTempOut; |
| 398 | |
| 399 | // Binary output writes to this stream, which might be |out| or |
| 400 | // |binaryTempOut|. It's the latter when we really want to write a C |
| 401 | // initializer list. |
| 402 | raw_pwrite_stream* binaryOut; |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 403 | raw_ostream &descriptorMapOut; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 404 | const bool outputAsm; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 405 | const bool outputCInitList; // If true, output look like {0x7023, ... , 5} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 406 | uint64_t patchBoundOffset; |
| 407 | uint32_t nextID; |
| 408 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 409 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 410 | TypeMapType TypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 411 | // Maps an LLVM image type to its SPIR-V ID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 412 | TypeMapType ImageTypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 413 | // A unique-vector of LLVM types that map to a SPIR-V type. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 414 | TypeList Types; |
| 415 | ValueList Constants; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 416 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 417 | ValueMapType ValueMap; |
| 418 | ValueMapType AllocatedValueMap; |
| 419 | SPIRVInstructionList SPIRVInsts; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 420 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 421 | EntryPointVecType EntryPointVec; |
| 422 | DeferredInstVecType DeferredInstVec; |
| 423 | ValueList EntryPointInterfacesVec; |
| 424 | uint32_t OpExtInstImportID; |
| 425 | std::vector<uint32_t> BuiltinDimensionVec; |
| 426 | bool HasVariablePointers; |
| 427 | Type *SamplerTy; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 428 | DenseMap<unsigned,uint32_t> SamplerMapIndexToIDMap; |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 429 | |
| 430 | // If a function F has a pointer-to-__constant parameter, then this variable |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 431 | // will map F's type to (G, index of the parameter), where in a first phase |
| 432 | // G is F's type. During FindTypePerFunc, G will be changed to F's type |
| 433 | // but replacing the pointer-to-constant parameter with |
| 434 | // pointer-to-ModuleScopePrivate. |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 435 | // TODO(dneto): This doesn't seem general enough? A function might have |
| 436 | // more than one such parameter. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 437 | GlobalConstFuncMapType GlobalConstFuncTypeMap; |
| 438 | SmallPtrSet<Value *, 16> GlobalConstArgumentSet; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 439 | // An ordered set of pointer types of Base arguments to OpPtrAccessChain, |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 440 | // or array types, and which point into transparent memory (StorageBuffer |
| 441 | // storage class). These will require an ArrayStride decoration. |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 442 | // See SPV_KHR_variable_pointers rev 13. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 443 | TypeList TypesNeedingArrayStride; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 444 | |
| 445 | // This is truly ugly, but works around what look like driver bugs. |
| 446 | // For get_local_size, an earlier part of the flow has created a module-scope |
| 447 | // variable in Private address space to hold the value for the workgroup |
| 448 | // size. Its intializer is a uint3 value marked as builtin WorkgroupSize. |
| 449 | // When this is present, save the IDs of the initializer value and variable |
| 450 | // in these two variables. We only ever do a vector load from it, and |
| 451 | // when we see one of those, substitute just the value of the intializer. |
| 452 | // This mimics what Glslang does, and that's what drivers are used to. |
David Neto | 66cfe64 | 2018-03-24 06:13:56 -0700 | [diff] [blame] | 453 | // TODO(dneto): Remove this once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 454 | uint32_t WorkgroupSizeValueID; |
| 455 | uint32_t WorkgroupSizeVarID; |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 456 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 457 | // Bookkeeping for mapping kernel arguments to resource variables. |
| 458 | struct ResourceVarInfo { |
| 459 | ResourceVarInfo(int index_arg, unsigned set_arg, unsigned binding_arg, |
| 460 | Function *fn, clspv::ArgKind arg_kind_arg) |
| 461 | : index(index_arg), descriptor_set(set_arg), binding(binding_arg), |
| 462 | var_fn(fn), arg_kind(arg_kind_arg), |
| 463 | addr_space(fn->getReturnType()->getPointerAddressSpace()) {} |
| 464 | const int index; // Index into ResourceVarInfoList |
| 465 | const unsigned descriptor_set; |
| 466 | const unsigned binding; |
| 467 | Function *const var_fn; // The @clspv.resource.var.* function. |
| 468 | const clspv::ArgKind arg_kind; |
| 469 | const unsigned addr_space; // The LLVM address space |
| 470 | // The SPIR-V ID of the OpVariable. Not populated at construction time. |
| 471 | uint32_t var_id = 0; |
| 472 | }; |
| 473 | // A list of resource var info. Each one correponds to a module-scope |
| 474 | // resource variable we will have to create. Resource var indices are |
| 475 | // indices into this vector. |
| 476 | SmallVector<std::unique_ptr<ResourceVarInfo>, 8> ResourceVarInfoList; |
| 477 | // This is a vector of pointers of all the resource vars, but ordered by |
| 478 | // kernel function, and then by argument. |
| 479 | UniqueVector<ResourceVarInfo*> ModuleOrderedResourceVars; |
| 480 | // Map a function to the ordered list of resource variables it uses, one for |
| 481 | // each argument. If an argument does not use a resource variable, it |
| 482 | // will have a null pointer entry. |
| 483 | using FunctionToResourceVarsMapType = |
| 484 | DenseMap<Function *, SmallVector<ResourceVarInfo *, 8>>; |
| 485 | FunctionToResourceVarsMapType FunctionToResourceVarsMap; |
| 486 | |
| 487 | // What LLVM types map to SPIR-V types needing layout? These are the |
| 488 | // arrays and structures supporting storage buffers and uniform buffers. |
| 489 | TypeList TypesNeedingLayout; |
| 490 | // What LLVM struct types map to a SPIR-V struct type with Block decoration? |
| 491 | UniqueVector<StructType *> StructTypesNeedingBlock; |
| 492 | // For a call that represents a load from an opaque type (samplers, images), |
| 493 | // map it to the variable id it should load from. |
| 494 | DenseMap<CallInst *, uint32_t> ResourceVarDeferredLoadCalls; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 495 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 496 | // One larger than the maximum used SpecId for pointer-to-local arguments. |
| 497 | int max_local_spec_id_; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 498 | // An ordered list of the kernel arguments of type pointer-to-local. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 499 | using LocalArgList = SmallVector<Argument*, 8>; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 500 | LocalArgList LocalArgs; |
| 501 | // Information about a pointer-to-local argument. |
| 502 | struct LocalArgInfo { |
| 503 | // The SPIR-V ID of the array variable. |
| 504 | uint32_t variable_id; |
| 505 | // The element type of the |
| 506 | Type* elem_type; |
| 507 | // The ID of the array type. |
| 508 | uint32_t array_size_id; |
| 509 | // The ID of the array type. |
| 510 | uint32_t array_type_id; |
| 511 | // The ID of the pointer to the array type. |
| 512 | uint32_t ptr_array_type_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 513 | // The specialization constant ID of the array size. |
| 514 | int spec_id; |
| 515 | }; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 516 | // A mapping from Argument to its assigned SpecId. |
| 517 | DenseMap<const Argument*, int> LocalArgSpecIds; |
| 518 | // A mapping from SpecId to its LocalArgInfo. |
| 519 | DenseMap<int, LocalArgInfo> LocalSpecIdInfoMap; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 520 | |
| 521 | // The ID of 32-bit integer zero constant. This is only valid after |
| 522 | // GenerateSPIRVConstants has run. |
| 523 | uint32_t constant_i32_zero_id_; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | char SPIRVProducerPass::ID; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 527 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | namespace clspv { |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 531 | ModulePass * |
| 532 | createSPIRVProducerPass(raw_pwrite_stream &out, raw_ostream &descriptor_map_out, |
| 533 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
| 534 | bool outputAsm, bool outputCInitList) { |
| 535 | return new SPIRVProducerPass(out, descriptor_map_out, samplerMap, outputAsm, |
| 536 | outputCInitList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 537 | } |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 538 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 539 | |
| 540 | bool SPIRVProducerPass::runOnModule(Module &module) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 541 | binaryOut = outputCInitList ? &binaryTempOut : &out; |
| 542 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 543 | constant_i32_zero_id_ = 0; // Reset, for the benefit of validity checks. |
| 544 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 545 | // SPIR-V always begins with its header information |
| 546 | outputHeader(); |
| 547 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 548 | const DataLayout &DL = module.getDataLayout(); |
| 549 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 550 | // Gather information from the LLVM IR that we require. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 551 | GenerateLLVMIRInfo(module, DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 552 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 553 | // Collect information on global variables too. |
| 554 | for (GlobalVariable &GV : module.globals()) { |
| 555 | // If the GV is one of our special __spirv_* variables, remove the |
| 556 | // initializer as it was only placed there to force LLVM to not throw the |
| 557 | // value away. |
| 558 | if (GV.getName().startswith("__spirv_")) { |
| 559 | GV.setInitializer(nullptr); |
| 560 | } |
| 561 | |
| 562 | // Collect types' information from global variable. |
| 563 | FindTypePerGlobalVar(GV); |
| 564 | |
| 565 | // Collect constant information from global variable. |
| 566 | FindConstantPerGlobalVar(GV); |
| 567 | |
| 568 | // If the variable is an input, entry points need to know about it. |
| 569 | if (AddressSpace::Input == GV.getType()->getPointerAddressSpace()) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 570 | getEntryPointInterfacesVec().insert(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | // If there are extended instructions, generate OpExtInstImport. |
| 575 | if (FindExtInst(module)) { |
| 576 | GenerateExtInstImport(); |
| 577 | } |
| 578 | |
| 579 | // Generate SPIRV instructions for types. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 580 | GenerateSPIRVTypes(module.getContext(), DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 581 | |
| 582 | // Generate SPIRV constants. |
| 583 | GenerateSPIRVConstants(); |
| 584 | |
| 585 | // If we have a sampler map, we might have literal samplers to generate. |
| 586 | if (0 < getSamplerMap().size()) { |
| 587 | GenerateSamplers(module); |
| 588 | } |
| 589 | |
| 590 | // Generate SPIRV variables. |
| 591 | for (GlobalVariable &GV : module.globals()) { |
| 592 | GenerateGlobalVar(GV); |
| 593 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 594 | GenerateResourceVars(module); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 595 | GenerateWorkgroupVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 596 | |
| 597 | // Generate SPIRV instructions for each function. |
| 598 | for (Function &F : module) { |
| 599 | if (F.isDeclaration()) { |
| 600 | continue; |
| 601 | } |
| 602 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 603 | GenerateDescriptorMapInfo(DL, F); |
| 604 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 605 | // Generate Function Prologue. |
| 606 | GenerateFuncPrologue(F); |
| 607 | |
| 608 | // Generate SPIRV instructions for function body. |
| 609 | GenerateFuncBody(F); |
| 610 | |
| 611 | // Generate Function Epilogue. |
| 612 | GenerateFuncEpilogue(); |
| 613 | } |
| 614 | |
| 615 | HandleDeferredInstruction(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 616 | HandleDeferredDecorations(DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 617 | |
| 618 | // Generate SPIRV module information. |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 619 | GenerateModuleInfo(module); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 620 | |
| 621 | if (outputAsm) { |
| 622 | WriteSPIRVAssembly(); |
| 623 | } else { |
| 624 | WriteSPIRVBinary(); |
| 625 | } |
| 626 | |
| 627 | // We need to patch the SPIR-V header to set bound correctly. |
| 628 | patchHeader(); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 629 | |
| 630 | if (outputCInitList) { |
| 631 | bool first = true; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 632 | std::ostringstream os; |
| 633 | |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 634 | auto emit_word = [&os, &first](uint32_t word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 635 | if (!first) |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 636 | os << ",\n"; |
| 637 | os << word; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 638 | first = false; |
| 639 | }; |
| 640 | |
| 641 | os << "{"; |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 642 | const std::string str(binaryTempOut.str()); |
| 643 | for (unsigned i = 0; i < str.size(); i += 4) { |
| 644 | const uint32_t a = static_cast<unsigned char>(str[i]); |
| 645 | const uint32_t b = static_cast<unsigned char>(str[i + 1]); |
| 646 | const uint32_t c = static_cast<unsigned char>(str[i + 2]); |
| 647 | const uint32_t d = static_cast<unsigned char>(str[i + 3]); |
| 648 | emit_word(a | (b << 8) | (c << 16) | (d << 24)); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 649 | } |
| 650 | os << "}\n"; |
| 651 | out << os.str(); |
| 652 | } |
| 653 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 654 | return false; |
| 655 | } |
| 656 | |
| 657 | void SPIRVProducerPass::outputHeader() { |
| 658 | if (outputAsm) { |
| 659 | // for ASM output the header goes into 5 comments at the beginning of the |
| 660 | // file |
| 661 | out << "; SPIR-V\n"; |
| 662 | |
| 663 | // the major version number is in the 2nd highest byte |
| 664 | const uint32_t major = (spv::Version >> 16) & 0xFF; |
| 665 | |
| 666 | // the minor version number is in the 2nd lowest byte |
| 667 | const uint32_t minor = (spv::Version >> 8) & 0xFF; |
| 668 | out << "; Version: " << major << "." << minor << "\n"; |
| 669 | |
| 670 | // use Codeplay's vendor ID |
| 671 | out << "; Generator: Codeplay; 0\n"; |
| 672 | |
| 673 | out << "; Bound: "; |
| 674 | |
| 675 | // we record where we need to come back to and patch in the bound value |
| 676 | patchBoundOffset = out.tell(); |
| 677 | |
| 678 | // output one space per digit for the max size of a 32 bit unsigned integer |
| 679 | // (which is the maximum ID we could possibly be using) |
| 680 | for (uint32_t i = std::numeric_limits<uint32_t>::max(); 0 != i; i /= 10) { |
| 681 | out << " "; |
| 682 | } |
| 683 | |
| 684 | out << "\n"; |
| 685 | |
| 686 | out << "; Schema: 0\n"; |
| 687 | } else { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 688 | binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber), |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 689 | sizeof(spv::MagicNumber)); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 690 | binaryOut->write(reinterpret_cast<const char *>(&spv::Version), |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 691 | sizeof(spv::Version)); |
| 692 | |
| 693 | // use Codeplay's vendor ID |
| 694 | const uint32_t vendor = 3 << 16; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 695 | binaryOut->write(reinterpret_cast<const char *>(&vendor), sizeof(vendor)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 696 | |
| 697 | // we record where we need to come back to and patch in the bound value |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 698 | patchBoundOffset = binaryOut->tell(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 699 | |
| 700 | // output a bad bound for now |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 701 | binaryOut->write(reinterpret_cast<const char *>(&nextID), sizeof(nextID)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 702 | |
| 703 | // output the schema (reserved for use and must be 0) |
| 704 | const uint32_t schema = 0; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 705 | binaryOut->write(reinterpret_cast<const char *>(&schema), sizeof(schema)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | void SPIRVProducerPass::patchHeader() { |
| 710 | if (outputAsm) { |
| 711 | // get the string representation of the max bound used (nextID will be the |
| 712 | // max ID used) |
| 713 | auto asString = std::to_string(nextID); |
| 714 | out.pwrite(asString.c_str(), asString.size(), patchBoundOffset); |
| 715 | } else { |
| 716 | // for a binary we just write the value of nextID over bound |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 717 | binaryOut->pwrite(reinterpret_cast<char *>(&nextID), sizeof(nextID), |
| 718 | patchBoundOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 722 | void SPIRVProducerPass::GenerateLLVMIRInfo(Module &M, const DataLayout &DL) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 723 | // This function generates LLVM IR for function such as global variable for |
| 724 | // argument, constant and pointer type for argument access. These information |
| 725 | // is artificial one because we need Vulkan SPIR-V output. This function is |
| 726 | // executed ahead of FindType and FindConstant. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 727 | LLVMContext &Context = M.getContext(); |
| 728 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 729 | FindGlobalConstVars(M, DL); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 730 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 731 | FindResourceVars(M, DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 732 | |
| 733 | bool HasWorkGroupBuiltin = false; |
| 734 | for (GlobalVariable &GV : M.globals()) { |
| 735 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 736 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 737 | HasWorkGroupBuiltin = true; |
| 738 | } |
| 739 | } |
| 740 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 741 | FindTypesForSamplerMap(M); |
| 742 | FindTypesForResourceVars(M); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 743 | FindWorkgroupVars(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 744 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 745 | // TODO(dneto): Delete the next 3 vars. |
| 746 | |
| 747 | //#error "remove arg handling from this code" |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 748 | // Map kernel functions to their ordinal number in the compilation unit. |
| 749 | UniqueVector<Function*> KernelOrdinal; |
| 750 | |
| 751 | // Map the global variables created for kernel args to their creation |
| 752 | // order. |
| 753 | UniqueVector<GlobalVariable*> KernelArgVarOrdinal; |
| 754 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 755 | // For each kernel argument type, record the kernel arg global resource |
| 756 | // variables generated for that type, the function in which that variable |
| 757 | // was most recently used, and the binding number it took. For |
| 758 | // reproducibility, we track things by ordinal number (rather than pointer), |
| 759 | // and we use a std::set rather than DenseSet since std::set maintains an |
| 760 | // ordering. Each tuple is the ordinals of the kernel function, the binding |
| 761 | // number, and the ordinal of the kernal-arg-var. |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 762 | // |
| 763 | // This table lets us reuse module-scope StorageBuffer variables between |
| 764 | // different kernels. |
| 765 | DenseMap<Type *, std::set<std::tuple<unsigned, unsigned, unsigned>>> |
| 766 | GVarsForType; |
| 767 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 768 | // These function calls need a <2 x i32> as an intermediate result but not |
| 769 | // the final result. |
| 770 | std::unordered_set<std::string> NeedsIVec2{ |
| 771 | "_Z15get_image_width14ocl_image2d_ro", |
| 772 | "_Z15get_image_width14ocl_image2d_wo", |
| 773 | "_Z16get_image_height14ocl_image2d_ro", |
| 774 | "_Z16get_image_height14ocl_image2d_wo", |
| 775 | }; |
| 776 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 777 | for (Function &F : M) { |
| 778 | // Handle kernel function first. |
| 779 | if (F.isDeclaration() || F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 780 | continue; |
| 781 | } |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 782 | KernelOrdinal.insert(&F); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 783 | |
| 784 | for (BasicBlock &BB : F) { |
| 785 | for (Instruction &I : BB) { |
| 786 | if (I.getOpcode() == Instruction::ZExt || |
| 787 | I.getOpcode() == Instruction::SExt || |
| 788 | I.getOpcode() == Instruction::UIToFP) { |
| 789 | // If there is zext with i1 type, it will be changed to OpSelect. The |
| 790 | // OpSelect needs constant 0 and 1 so the constants are added here. |
| 791 | |
| 792 | auto OpTy = I.getOperand(0)->getType(); |
| 793 | |
| 794 | if (OpTy->isIntegerTy(1) || |
| 795 | (OpTy->isVectorTy() && |
| 796 | OpTy->getVectorElementType()->isIntegerTy(1))) { |
| 797 | if (I.getOpcode() == Instruction::ZExt) { |
| 798 | APInt One(32, 1); |
| 799 | FindConstant(Constant::getNullValue(I.getType())); |
| 800 | FindConstant(Constant::getIntegerValue(I.getType(), One)); |
| 801 | } else if (I.getOpcode() == Instruction::SExt) { |
| 802 | APInt MinusOne(32, UINT64_MAX, true); |
| 803 | FindConstant(Constant::getNullValue(I.getType())); |
| 804 | FindConstant(Constant::getIntegerValue(I.getType(), MinusOne)); |
| 805 | } else { |
| 806 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 807 | FindConstant(ConstantFP::get(Context, APFloat(1.0f))); |
| 808 | } |
| 809 | } |
| 810 | } else if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 811 | StringRef callee_name = Call->getCalledFunction()->getName(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 812 | |
| 813 | // Handle image type specially. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 814 | if (callee_name.equals( |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 815 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") || |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 816 | callee_name.equals( |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 817 | "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) { |
| 818 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 819 | Type *ImageTy = |
| 820 | Call->getArgOperand(0)->getType()->getPointerElementType(); |
| 821 | OpImageTypeMap[ImageTy] = 0; |
| 822 | |
| 823 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 824 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 825 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 826 | if (NeedsIVec2.find(callee_name) != NeedsIVec2.end()) { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 827 | FindType(VectorType::get(Type::getInt32Ty(Context), 2)); |
| 828 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 833 | if (const MDNode *MD = |
| 834 | dyn_cast<Function>(&F)->getMetadata("reqd_work_group_size")) { |
| 835 | // We generate constants if the WorkgroupSize builtin is being used. |
| 836 | if (HasWorkGroupBuiltin) { |
| 837 | // Collect constant information for work group size. |
| 838 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(0))); |
| 839 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(1))); |
| 840 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(2))); |
| 841 | } |
| 842 | } |
| 843 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 844 | // Collect types' information from function. |
| 845 | FindTypePerFunc(F); |
| 846 | |
| 847 | // Collect constant information from function. |
| 848 | FindConstantPerFunc(F); |
| 849 | } |
| 850 | |
| 851 | for (Function &F : M) { |
| 852 | // Handle non-kernel functions. |
| 853 | if (F.isDeclaration() || F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 854 | continue; |
| 855 | } |
| 856 | |
| 857 | for (BasicBlock &BB : F) { |
| 858 | for (Instruction &I : BB) { |
| 859 | if (I.getOpcode() == Instruction::ZExt || |
| 860 | I.getOpcode() == Instruction::SExt || |
| 861 | I.getOpcode() == Instruction::UIToFP) { |
| 862 | // If there is zext with i1 type, it will be changed to OpSelect. The |
| 863 | // OpSelect needs constant 0 and 1 so the constants are added here. |
| 864 | |
| 865 | auto OpTy = I.getOperand(0)->getType(); |
| 866 | |
| 867 | if (OpTy->isIntegerTy(1) || |
| 868 | (OpTy->isVectorTy() && |
| 869 | OpTy->getVectorElementType()->isIntegerTy(1))) { |
| 870 | if (I.getOpcode() == Instruction::ZExt) { |
| 871 | APInt One(32, 1); |
| 872 | FindConstant(Constant::getNullValue(I.getType())); |
| 873 | FindConstant(Constant::getIntegerValue(I.getType(), One)); |
| 874 | } else if (I.getOpcode() == Instruction::SExt) { |
| 875 | APInt MinusOne(32, UINT64_MAX, true); |
| 876 | FindConstant(Constant::getNullValue(I.getType())); |
| 877 | FindConstant(Constant::getIntegerValue(I.getType(), MinusOne)); |
| 878 | } else { |
| 879 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 880 | FindConstant(ConstantFP::get(Context, APFloat(1.0f))); |
| 881 | } |
| 882 | } |
| 883 | } else if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
| 884 | Function *Callee = Call->getCalledFunction(); |
| 885 | |
| 886 | // Handle image type specially. |
| 887 | if (Callee->getName().equals( |
| 888 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") || |
| 889 | Callee->getName().equals( |
| 890 | "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) { |
| 891 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 892 | Type *ImageTy = |
| 893 | Call->getArgOperand(0)->getType()->getPointerElementType(); |
| 894 | OpImageTypeMap[ImageTy] = 0; |
| 895 | |
| 896 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | if (M.getTypeByName("opencl.image2d_ro_t") || |
| 903 | M.getTypeByName("opencl.image2d_wo_t") || |
| 904 | M.getTypeByName("opencl.image3d_ro_t") || |
| 905 | M.getTypeByName("opencl.image3d_wo_t")) { |
| 906 | // Assume Image type's sampled type is float type. |
| 907 | FindType(Type::getFloatTy(Context)); |
| 908 | } |
| 909 | |
| 910 | // Collect types' information from function. |
| 911 | FindTypePerFunc(F); |
| 912 | |
| 913 | // Collect constant information from function. |
| 914 | FindConstantPerFunc(F); |
| 915 | } |
| 916 | } |
| 917 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 918 | void SPIRVProducerPass::FindGlobalConstVars(Module &M, const DataLayout &DL) { |
| 919 | SmallVector<GlobalVariable *, 8> GVList; |
| 920 | SmallVector<GlobalVariable *, 8> DeadGVList; |
| 921 | for (GlobalVariable &GV : M.globals()) { |
| 922 | if (GV.getType()->getAddressSpace() == AddressSpace::Constant) { |
| 923 | if (GV.use_empty()) { |
| 924 | DeadGVList.push_back(&GV); |
| 925 | } else { |
| 926 | GVList.push_back(&GV); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | // Remove dead global __constant variables. |
| 932 | for (auto GV : DeadGVList) { |
| 933 | GV->eraseFromParent(); |
| 934 | } |
| 935 | DeadGVList.clear(); |
| 936 | |
| 937 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 938 | // For now, we only support a single storage buffer. |
| 939 | if (GVList.size() > 0) { |
| 940 | assert(GVList.size() == 1); |
| 941 | const auto *GV = GVList[0]; |
| 942 | const auto constants_byte_size = |
| 943 | (DL.getTypeSizeInBits(GV->getInitializer()->getType())) / 8; |
| 944 | const size_t kConstantMaxSize = 65536; |
| 945 | if (constants_byte_size > kConstantMaxSize) { |
| 946 | outs() << "Max __constant capacity of " << kConstantMaxSize |
| 947 | << " bytes exceeded: " << constants_byte_size << " bytes used\n"; |
| 948 | llvm_unreachable("Max __constant capacity exceeded"); |
| 949 | } |
| 950 | } |
| 951 | } else { |
| 952 | // Change global constant variable's address space to ModuleScopePrivate. |
| 953 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 954 | for (auto GV : GVList) { |
| 955 | // Create new gv with ModuleScopePrivate address space. |
| 956 | Type *NewGVTy = GV->getType()->getPointerElementType(); |
| 957 | GlobalVariable *NewGV = new GlobalVariable( |
| 958 | M, NewGVTy, false, GV->getLinkage(), GV->getInitializer(), "", |
| 959 | nullptr, GV->getThreadLocalMode(), AddressSpace::ModuleScopePrivate); |
| 960 | NewGV->takeName(GV); |
| 961 | |
| 962 | const SmallVector<User *, 8> GVUsers(GV->user_begin(), GV->user_end()); |
| 963 | SmallVector<User *, 8> CandidateUsers; |
| 964 | |
| 965 | auto record_called_function_type_as_user = |
| 966 | [&GlobalConstFuncTyMap](Value *gv, CallInst *call) { |
| 967 | // Find argument index. |
| 968 | unsigned index = 0; |
| 969 | for (unsigned i = 0; i < call->getNumArgOperands(); i++) { |
| 970 | if (gv == call->getOperand(i)) { |
| 971 | // TODO(dneto): Should we break here? |
| 972 | index = i; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | // Record function type with global constant. |
| 977 | GlobalConstFuncTyMap[call->getFunctionType()] = |
| 978 | std::make_pair(call->getFunctionType(), index); |
| 979 | }; |
| 980 | |
| 981 | for (User *GVU : GVUsers) { |
| 982 | if (CallInst *Call = dyn_cast<CallInst>(GVU)) { |
| 983 | record_called_function_type_as_user(GV, Call); |
| 984 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(GVU)) { |
| 985 | // Check GEP users. |
| 986 | for (User *GEPU : GEP->users()) { |
| 987 | if (CallInst *GEPCall = dyn_cast<CallInst>(GEPU)) { |
| 988 | record_called_function_type_as_user(GEP, GEPCall); |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | CandidateUsers.push_back(GVU); |
| 994 | } |
| 995 | |
| 996 | for (User *U : CandidateUsers) { |
| 997 | // Update users of gv with new gv. |
| 998 | U->replaceUsesOfWith(GV, NewGV); |
| 999 | } |
| 1000 | |
| 1001 | // Delete original gv. |
| 1002 | GV->eraseFromParent(); |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | void SPIRVProducerPass::FindResourceVars(Module &M, const DataLayout &DL) { |
| 1008 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 1009 | ValueMapType &VMap = getValueMap(); |
| 1010 | |
| 1011 | ResourceVarInfoList.clear(); |
| 1012 | FunctionToResourceVarsMap.clear(); |
| 1013 | ModuleOrderedResourceVars.reset(); |
| 1014 | // Normally, there is one resource variable per clspv.resource.var.* |
| 1015 | // function, since that is unique'd by arg type and index. By design, |
| 1016 | // we can share these resource variables across kernels because all |
| 1017 | // kernels use the same descriptor set. |
| 1018 | // |
| 1019 | // But if the user requested distinct descriptor sets per kernel, then |
| 1020 | // the descriptor allocator has made different (set,binding) pairs for |
| 1021 | // the same (type,arg_index) pair. Since we can decorate a resource |
| 1022 | // variable with only exactly one DescriptorSet and Binding, we are |
| 1023 | // forced in this case to make distinct resource variables whenever |
| 1024 | // the same clspv.reource.var.X function is seen with disintct |
| 1025 | // (set,binding) values. |
| 1026 | const bool always_distinct_sets = |
| 1027 | clspv::Option::DistinctKernelDescriptorSets(); |
| 1028 | for (Function &F : M) { |
| 1029 | // Rely on the fact the resource var functions have a stable ordering |
| 1030 | // in the module. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 1031 | if (F.getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1032 | // Find all calls to this function with distinct set and binding pairs. |
| 1033 | // Save them in ResourceVarInfoList. |
| 1034 | |
| 1035 | // Determine uniqueness of the (set,binding) pairs only withing this |
| 1036 | // one resource-var builtin function. |
| 1037 | using SetAndBinding = std::pair<unsigned, unsigned>; |
| 1038 | // Maps set and binding to the resource var info. |
| 1039 | DenseMap<SetAndBinding, ResourceVarInfo *> set_and_binding_map; |
| 1040 | bool first_use = true; |
| 1041 | for (auto &U : F.uses()) { |
| 1042 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 1043 | const auto set = unsigned( |
| 1044 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 1045 | const auto binding = unsigned( |
| 1046 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 1047 | const auto arg_kind = clspv::ArgKind( |
| 1048 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 1049 | const auto arg_index = unsigned( |
| 1050 | dyn_cast<ConstantInt>(call->getArgOperand(3))->getZExtValue()); |
| 1051 | |
| 1052 | // Find or make the resource var info for this combination. |
| 1053 | ResourceVarInfo *rv = nullptr; |
| 1054 | if (always_distinct_sets) { |
| 1055 | // Make a new resource var any time we see a different |
| 1056 | // (set,binding) pair. |
| 1057 | SetAndBinding key{set, binding}; |
| 1058 | auto where = set_and_binding_map.find(key); |
| 1059 | if (where == set_and_binding_map.end()) { |
| 1060 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
| 1061 | binding, &F, arg_kind); |
| 1062 | ResourceVarInfoList.emplace_back(rv); |
| 1063 | set_and_binding_map[key] = rv; |
| 1064 | } else { |
| 1065 | rv = where->second; |
| 1066 | } |
| 1067 | } else { |
| 1068 | // The default is to make exactly one resource for each |
| 1069 | // clspv.resource.var.* function. |
| 1070 | if (first_use) { |
| 1071 | first_use = false; |
| 1072 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
| 1073 | binding, &F, arg_kind); |
| 1074 | ResourceVarInfoList.emplace_back(rv); |
| 1075 | } else { |
| 1076 | rv = ResourceVarInfoList.back().get(); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | // Now populate FunctionToResourceVarsMap. |
| 1081 | auto &mapping = |
| 1082 | FunctionToResourceVarsMap[call->getParent()->getParent()]; |
| 1083 | while (mapping.size() <= arg_index) { |
| 1084 | mapping.push_back(nullptr); |
| 1085 | } |
| 1086 | mapping[arg_index] = rv; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | // Populate ModuleOrderedResourceVars. |
| 1093 | for (Function &F : M) { |
| 1094 | auto where = FunctionToResourceVarsMap.find(&F); |
| 1095 | if (where != FunctionToResourceVarsMap.end()) { |
| 1096 | for (auto &rv : where->second) { |
| 1097 | if (rv != nullptr) { |
| 1098 | ModuleOrderedResourceVars.insert(rv); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | if (ShowResourceVars) { |
| 1104 | for (auto *info : ModuleOrderedResourceVars) { |
| 1105 | outs() << "MORV index " << info->index << " (" << info->descriptor_set |
| 1106 | << "," << info->binding << ") " << *(info->var_fn->getReturnType()) |
| 1107 | << "\n"; |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1112 | bool SPIRVProducerPass::FindExtInst(Module &M) { |
| 1113 | LLVMContext &Context = M.getContext(); |
| 1114 | bool HasExtInst = false; |
| 1115 | |
| 1116 | for (Function &F : M) { |
| 1117 | for (BasicBlock &BB : F) { |
| 1118 | for (Instruction &I : BB) { |
| 1119 | if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
| 1120 | Function *Callee = Call->getCalledFunction(); |
| 1121 | // Check whether this call is for extend instructions. |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1122 | auto callee_name = Callee->getName(); |
| 1123 | const glsl::ExtInst EInst = getExtInstEnum(callee_name); |
| 1124 | const glsl::ExtInst IndirectEInst = |
| 1125 | getIndirectExtInstEnum(callee_name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1126 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1127 | HasExtInst |= |
| 1128 | (EInst != kGlslExtInstBad) || (IndirectEInst != kGlslExtInstBad); |
| 1129 | |
| 1130 | if (IndirectEInst) { |
| 1131 | // Register extra constants if needed. |
| 1132 | |
| 1133 | // Registers a type and constant for computing the result of the |
| 1134 | // given instruction. If the result of the instruction is a vector, |
| 1135 | // then make a splat vector constant with the same number of |
| 1136 | // elements. |
| 1137 | auto register_constant = [this, &I](Constant *constant) { |
| 1138 | FindType(constant->getType()); |
| 1139 | FindConstant(constant); |
| 1140 | if (auto *vectorTy = dyn_cast<VectorType>(I.getType())) { |
| 1141 | // Register the splat vector of the value with the same |
| 1142 | // width as the result of the instruction. |
| 1143 | auto *vec_constant = ConstantVector::getSplat( |
| 1144 | static_cast<unsigned>(vectorTy->getNumElements()), |
| 1145 | constant); |
| 1146 | FindConstant(vec_constant); |
| 1147 | FindType(vec_constant->getType()); |
| 1148 | } |
| 1149 | }; |
| 1150 | switch (IndirectEInst) { |
| 1151 | case glsl::ExtInstFindUMsb: |
| 1152 | // clz needs OpExtInst and OpISub with constant 31, or splat |
| 1153 | // vector of 31. Add it to the constant list here. |
| 1154 | register_constant( |
| 1155 | ConstantInt::get(Type::getInt32Ty(Context), 31)); |
| 1156 | break; |
| 1157 | case glsl::ExtInstAcos: |
| 1158 | case glsl::ExtInstAsin: |
| 1159 | case glsl::ExtInstAtan2: |
| 1160 | // We need 1/pi for acospi, asinpi, atan2pi. |
| 1161 | register_constant( |
| 1162 | ConstantFP::get(Type::getFloatTy(Context), kOneOverPi)); |
| 1163 | break; |
| 1164 | default: |
| 1165 | assert(false && "internally inconsistent"); |
| 1166 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | return HasExtInst; |
| 1174 | } |
| 1175 | |
| 1176 | void SPIRVProducerPass::FindTypePerGlobalVar(GlobalVariable &GV) { |
| 1177 | // Investigate global variable's type. |
| 1178 | FindType(GV.getType()); |
| 1179 | } |
| 1180 | |
| 1181 | void SPIRVProducerPass::FindTypePerFunc(Function &F) { |
| 1182 | // Investigate function's type. |
| 1183 | FunctionType *FTy = F.getFunctionType(); |
| 1184 | |
| 1185 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 1186 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1187 | // Handle a regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1188 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 1189 | uint32_t GVCstArgIdx = GlobalConstFuncTypeMap[FTy].second; |
| 1190 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1191 | for (unsigned i = 0; i < FTy->getNumParams(); i++) { |
| 1192 | Type *ParamTy = FTy->getParamType(i); |
| 1193 | if (i == GVCstArgIdx) { |
| 1194 | Type *EleTy = ParamTy->getPointerElementType(); |
| 1195 | ParamTy = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1196 | } |
| 1197 | |
| 1198 | NewFuncParamTys.push_back(ParamTy); |
| 1199 | } |
| 1200 | |
| 1201 | FunctionType *NewFTy = |
| 1202 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1203 | GlobalConstFuncTyMap[FTy] = std::make_pair(NewFTy, GVCstArgIdx); |
| 1204 | FTy = NewFTy; |
| 1205 | } |
| 1206 | |
| 1207 | FindType(FTy); |
| 1208 | } else { |
| 1209 | // As kernel functions do not have parameters, create new function type and |
| 1210 | // add it to type map. |
| 1211 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1212 | FunctionType *NewFTy = |
| 1213 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1214 | FindType(NewFTy); |
| 1215 | } |
| 1216 | |
| 1217 | // Investigate instructions' type in function body. |
| 1218 | for (BasicBlock &BB : F) { |
| 1219 | for (Instruction &I : BB) { |
| 1220 | if (isa<ShuffleVectorInst>(I)) { |
| 1221 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1222 | // Ignore type for mask of shuffle vector instruction. |
| 1223 | if (i == 2) { |
| 1224 | continue; |
| 1225 | } |
| 1226 | |
| 1227 | Value *Op = I.getOperand(i); |
| 1228 | if (!isa<MetadataAsValue>(Op)) { |
| 1229 | FindType(Op->getType()); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | FindType(I.getType()); |
| 1234 | continue; |
| 1235 | } |
| 1236 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1237 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 1238 | |
| 1239 | if (Call && Call->getCalledFunction()->getName().startswith( |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 1240 | clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1241 | // This is a fake call representing access to a resource variable. |
| 1242 | // We handle that elsewhere. |
| 1243 | continue; |
| 1244 | } |
| 1245 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 1246 | if (Call && Call->getCalledFunction()->getName().startswith( |
| 1247 | clspv::WorkgroupAccessorFunction())) { |
| 1248 | // This is a fake call representing access to a workgroup variable. |
| 1249 | // We handle that elsewhere. |
| 1250 | continue; |
| 1251 | } |
| 1252 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1253 | // Work through the operands of the instruction. |
| 1254 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1255 | Value *const Op = I.getOperand(i); |
| 1256 | // If any of the operands is a constant, find the type! |
| 1257 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1258 | FindType(Op->getType()); |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | for (Use &Op : I.operands()) { |
| 1263 | if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
| 1264 | // Avoid to check call instruction's type. |
| 1265 | break; |
| 1266 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 1267 | if (CallInst *OpCall = dyn_cast<CallInst>(Op)) { |
| 1268 | if (OpCall && OpCall->getCalledFunction()->getName().startswith( |
| 1269 | clspv::WorkgroupAccessorFunction())) { |
| 1270 | // This is a fake call representing access to a workgroup variable. |
| 1271 | // We handle that elsewhere. |
| 1272 | continue; |
| 1273 | } |
| 1274 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1275 | if (!isa<MetadataAsValue>(&Op)) { |
| 1276 | FindType(Op->getType()); |
| 1277 | continue; |
| 1278 | } |
| 1279 | } |
| 1280 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1281 | // We don't want to track the type of this call as we are going to replace |
| 1282 | // it. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1283 | if (Call && ("clspv.sampler.var.literal" == |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1284 | Call->getCalledFunction()->getName())) { |
| 1285 | continue; |
| 1286 | } |
| 1287 | |
| 1288 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 1289 | // If gep's base operand has ModuleScopePrivate address space, make gep |
| 1290 | // return ModuleScopePrivate address space. |
| 1291 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate) { |
| 1292 | // Add pointer type with private address space for global constant to |
| 1293 | // type list. |
| 1294 | Type *EleTy = I.getType()->getPointerElementType(); |
| 1295 | Type *NewPTy = |
| 1296 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1297 | |
| 1298 | FindType(NewPTy); |
| 1299 | continue; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | FindType(I.getType()); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1308 | void SPIRVProducerPass::FindTypesForSamplerMap(Module &M) { |
| 1309 | // If we are using a sampler map, find the type of the sampler. |
| 1310 | if (M.getFunction("clspv.sampler.var.literal") || |
| 1311 | 0 < getSamplerMap().size()) { |
| 1312 | auto SamplerStructTy = M.getTypeByName("opencl.sampler_t"); |
| 1313 | if (!SamplerStructTy) { |
| 1314 | SamplerStructTy = StructType::create(M.getContext(), "opencl.sampler_t"); |
| 1315 | } |
| 1316 | |
| 1317 | SamplerTy = SamplerStructTy->getPointerTo(AddressSpace::UniformConstant); |
| 1318 | |
| 1319 | FindType(SamplerTy); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | void SPIRVProducerPass::FindTypesForResourceVars(Module &M) { |
| 1324 | // Record types so they are generated. |
| 1325 | TypesNeedingLayout.reset(); |
| 1326 | StructTypesNeedingBlock.reset(); |
| 1327 | |
| 1328 | // To match older clspv codegen, generate the float type first if required |
| 1329 | // for images. |
| 1330 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1331 | if (info->arg_kind == clspv::ArgKind::ReadOnlyImage || |
| 1332 | info->arg_kind == clspv::ArgKind::WriteOnlyImage) { |
| 1333 | // We need "float" for the sampled component type. |
| 1334 | FindType(Type::getFloatTy(M.getContext())); |
| 1335 | // We only need to find it once. |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1341 | Type *type = info->var_fn->getReturnType(); |
| 1342 | |
| 1343 | switch (info->arg_kind) { |
| 1344 | case clspv::ArgKind::Buffer: |
| 1345 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1346 | StructTypesNeedingBlock.insert(sty); |
| 1347 | } else { |
| 1348 | errs() << *type << "\n"; |
| 1349 | llvm_unreachable("Buffer arguments must map to structures!"); |
| 1350 | } |
| 1351 | break; |
| 1352 | case clspv::ArgKind::Pod: |
| 1353 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1354 | StructTypesNeedingBlock.insert(sty); |
| 1355 | } else { |
| 1356 | errs() << *type << "\n"; |
| 1357 | llvm_unreachable("POD arguments must map to structures!"); |
| 1358 | } |
| 1359 | break; |
| 1360 | case clspv::ArgKind::ReadOnlyImage: |
| 1361 | case clspv::ArgKind::WriteOnlyImage: |
| 1362 | case clspv::ArgKind::Sampler: |
| 1363 | // Sampler and image types map to the pointee type but |
| 1364 | // in the uniform constant address space. |
| 1365 | type = PointerType::get(type->getPointerElementType(), |
| 1366 | clspv::AddressSpace::UniformConstant); |
| 1367 | break; |
| 1368 | default: |
| 1369 | break; |
| 1370 | } |
| 1371 | |
| 1372 | // The converted type is the type of the OpVariable we will generate. |
| 1373 | // If the pointee type is an array of size zero, FindType will convert it |
| 1374 | // to a runtime array. |
| 1375 | FindType(type); |
| 1376 | } |
| 1377 | |
| 1378 | // Traverse the arrays and structures underneath each Block, and |
| 1379 | // mark them as needing layout. |
| 1380 | std::vector<Type *> work_list(StructTypesNeedingBlock.begin(), |
| 1381 | StructTypesNeedingBlock.end()); |
| 1382 | while (!work_list.empty()) { |
| 1383 | Type *type = work_list.back(); |
| 1384 | work_list.pop_back(); |
| 1385 | TypesNeedingLayout.insert(type); |
| 1386 | switch (type->getTypeID()) { |
| 1387 | case Type::ArrayTyID: |
| 1388 | work_list.push_back(type->getArrayElementType()); |
| 1389 | if (!Hack_generate_runtime_array_stride_early) { |
| 1390 | // Remember this array type for deferred decoration. |
| 1391 | TypesNeedingArrayStride.insert(type); |
| 1392 | } |
| 1393 | break; |
| 1394 | case Type::StructTyID: |
| 1395 | for (auto *elem_ty : cast<StructType>(type)->elements()) { |
| 1396 | work_list.push_back(elem_ty); |
| 1397 | } |
| 1398 | default: |
| 1399 | // This type and its contained types don't get layout. |
| 1400 | break; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 1405 | void SPIRVProducerPass::FindWorkgroupVars(Module &M) { |
| 1406 | // The SpecId assignment for pointer-to-local arguments is recorded in |
| 1407 | // module-level metadata. Translate that information into local argument |
| 1408 | // information. |
| 1409 | NamedMDNode *nmd = M.getNamedMetadata(clspv::LocalSpecIdMetadataName()); |
| 1410 | if (!nmd) return; |
| 1411 | for (auto operand : nmd->operands()) { |
| 1412 | MDTuple *tuple = cast<MDTuple>(operand); |
| 1413 | ValueAsMetadata *fn_md = cast<ValueAsMetadata>(tuple->getOperand(0)); |
| 1414 | Function *func = cast<Function>(fn_md->getValue()); |
| 1415 | ConstantAsMetadata *arg_index_md = cast<ConstantAsMetadata>(tuple->getOperand(1)); |
| 1416 | int arg_index = cast<ConstantInt>(arg_index_md->getValue())->getSExtValue(); |
| 1417 | Argument* arg = &*(func->arg_begin() + arg_index); |
| 1418 | |
| 1419 | ConstantAsMetadata *spec_id_md = |
| 1420 | cast<ConstantAsMetadata>(tuple->getOperand(2)); |
| 1421 | int spec_id = cast<ConstantInt>(spec_id_md->getValue())->getSExtValue(); |
| 1422 | |
| 1423 | max_local_spec_id_ = std::max(max_local_spec_id_, spec_id + 1); |
| 1424 | LocalArgSpecIds[arg] = spec_id; |
| 1425 | if (LocalSpecIdInfoMap.count(spec_id)) continue; |
| 1426 | |
| 1427 | // We haven't seen this SpecId yet, so generate the LocalArgInfo for it. |
| 1428 | LocalArgInfo info{nextID, arg->getType()->getPointerElementType(), |
| 1429 | nextID + 1, nextID + 2, |
| 1430 | nextID + 3, spec_id}; |
| 1431 | LocalSpecIdInfoMap[spec_id] = info; |
| 1432 | nextID += 4; |
| 1433 | |
| 1434 | // Ensure the types necessary for this argument get generated. |
| 1435 | Type *IdxTy = Type::getInt32Ty(M.getContext()); |
| 1436 | FindConstant(ConstantInt::get(IdxTy, 0)); |
| 1437 | FindType(IdxTy); |
| 1438 | FindType(arg->getType()); |
| 1439 | } |
| 1440 | } |
| 1441 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1442 | void SPIRVProducerPass::FindType(Type *Ty) { |
| 1443 | TypeList &TyList = getTypeList(); |
| 1444 | |
| 1445 | if (0 != TyList.idFor(Ty)) { |
| 1446 | return; |
| 1447 | } |
| 1448 | |
| 1449 | if (Ty->isPointerTy()) { |
| 1450 | auto AddrSpace = Ty->getPointerAddressSpace(); |
| 1451 | if ((AddressSpace::Constant == AddrSpace) || |
| 1452 | (AddressSpace::Global == AddrSpace)) { |
| 1453 | auto PointeeTy = Ty->getPointerElementType(); |
| 1454 | |
| 1455 | if (PointeeTy->isStructTy() && |
| 1456 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1457 | FindType(PointeeTy); |
| 1458 | auto ActualPointerTy = |
| 1459 | PointeeTy->getPointerTo(AddressSpace::UniformConstant); |
| 1460 | FindType(ActualPointerTy); |
| 1461 | return; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1466 | // By convention, LLVM array type with 0 elements will map to |
| 1467 | // OpTypeRuntimeArray. Otherwise, it will map to OpTypeArray, which |
| 1468 | // has a constant number of elements. We need to support type of the |
| 1469 | // constant. |
| 1470 | if (auto *arrayTy = dyn_cast<ArrayType>(Ty)) { |
| 1471 | if (arrayTy->getNumElements() > 0) { |
| 1472 | LLVMContext &Context = Ty->getContext(); |
| 1473 | FindType(Type::getInt32Ty(Context)); |
| 1474 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | for (Type *SubTy : Ty->subtypes()) { |
| 1478 | FindType(SubTy); |
| 1479 | } |
| 1480 | |
| 1481 | TyList.insert(Ty); |
| 1482 | } |
| 1483 | |
| 1484 | void SPIRVProducerPass::FindConstantPerGlobalVar(GlobalVariable &GV) { |
| 1485 | // If the global variable has a (non undef) initializer. |
| 1486 | if (GV.hasInitializer() && !isa<UndefValue>(GV.getInitializer())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1487 | // Generate the constant if it's not the initializer to a module scope |
| 1488 | // constant that we will expect in a storage buffer. |
| 1489 | const bool module_scope_constant_external_init = |
| 1490 | (GV.getType()->getPointerAddressSpace() == AddressSpace::Constant) && |
| 1491 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 1492 | if (!module_scope_constant_external_init) { |
| 1493 | FindConstant(GV.getInitializer()); |
| 1494 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | void SPIRVProducerPass::FindConstantPerFunc(Function &F) { |
| 1499 | // Investigate constants in function body. |
| 1500 | for (BasicBlock &BB : F) { |
| 1501 | for (Instruction &I : BB) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1502 | if (auto *call = dyn_cast<CallInst>(&I)) { |
| 1503 | auto name = call->getCalledFunction()->getName(); |
| 1504 | if (name == "clspv.sampler.var.literal") { |
| 1505 | // We've handled these constants elsewhere, so skip it. |
| 1506 | continue; |
| 1507 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 1508 | if (name.startswith(clspv::ResourceAccessorFunction())) { |
| 1509 | continue; |
| 1510 | } |
| 1511 | if (name.startswith(clspv::WorkgroupAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1512 | continue; |
| 1513 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | if (isa<AllocaInst>(I)) { |
| 1517 | // Alloca instruction has constant for the number of element. Ignore it. |
| 1518 | continue; |
| 1519 | } else if (isa<ShuffleVectorInst>(I)) { |
| 1520 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1521 | // Ignore constant for mask of shuffle vector instruction. |
| 1522 | if (i == 2) { |
| 1523 | continue; |
| 1524 | } |
| 1525 | |
| 1526 | if (isa<Constant>(I.getOperand(i)) && |
| 1527 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1528 | FindConstant(I.getOperand(i)); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | continue; |
| 1533 | } else if (isa<InsertElementInst>(I)) { |
| 1534 | // Handle InsertElement with <4 x i8> specially. |
| 1535 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 1536 | if (is4xi8vec(CompositeTy)) { |
| 1537 | LLVMContext &Context = CompositeTy->getContext(); |
| 1538 | if (isa<Constant>(I.getOperand(0))) { |
| 1539 | FindConstant(I.getOperand(0)); |
| 1540 | } |
| 1541 | |
| 1542 | if (isa<Constant>(I.getOperand(1))) { |
| 1543 | FindConstant(I.getOperand(1)); |
| 1544 | } |
| 1545 | |
| 1546 | // Add mask constant 0xFF. |
| 1547 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 1548 | FindConstant(CstFF); |
| 1549 | |
| 1550 | // Add shift amount constant. |
| 1551 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 1552 | uint64_t Idx = CI->getZExtValue(); |
| 1553 | Constant *CstShiftAmount = |
| 1554 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 1555 | FindConstant(CstShiftAmount); |
| 1556 | } |
| 1557 | |
| 1558 | continue; |
| 1559 | } |
| 1560 | |
| 1561 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1562 | // Ignore constant for index of InsertElement instruction. |
| 1563 | if (i == 2) { |
| 1564 | continue; |
| 1565 | } |
| 1566 | |
| 1567 | if (isa<Constant>(I.getOperand(i)) && |
| 1568 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1569 | FindConstant(I.getOperand(i)); |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | continue; |
| 1574 | } else if (isa<ExtractElementInst>(I)) { |
| 1575 | // Handle ExtractElement with <4 x i8> specially. |
| 1576 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 1577 | if (is4xi8vec(CompositeTy)) { |
| 1578 | LLVMContext &Context = CompositeTy->getContext(); |
| 1579 | if (isa<Constant>(I.getOperand(0))) { |
| 1580 | FindConstant(I.getOperand(0)); |
| 1581 | } |
| 1582 | |
| 1583 | // Add mask constant 0xFF. |
| 1584 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 1585 | FindConstant(CstFF); |
| 1586 | |
| 1587 | // Add shift amount constant. |
| 1588 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 1589 | uint64_t Idx = CI->getZExtValue(); |
| 1590 | Constant *CstShiftAmount = |
| 1591 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 1592 | FindConstant(CstShiftAmount); |
| 1593 | } else { |
| 1594 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
| 1595 | FindConstant(Cst8); |
| 1596 | } |
| 1597 | |
| 1598 | continue; |
| 1599 | } |
| 1600 | |
| 1601 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1602 | // Ignore constant for index of ExtractElement instruction. |
| 1603 | if (i == 1) { |
| 1604 | continue; |
| 1605 | } |
| 1606 | |
| 1607 | if (isa<Constant>(I.getOperand(i)) && |
| 1608 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1609 | FindConstant(I.getOperand(i)); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | continue; |
| 1614 | } else if ((Instruction::Xor == I.getOpcode()) && I.getType()->isIntegerTy(1)) { |
| 1615 | // We special case for Xor where the type is i1 and one of the arguments is a constant 1 (true), this is an OpLogicalNot in SPIR-V, and we don't need the constant |
| 1616 | bool foundConstantTrue = false; |
| 1617 | for (Use &Op : I.operands()) { |
| 1618 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1619 | auto CI = cast<ConstantInt>(Op); |
| 1620 | |
| 1621 | if (CI->isZero() || foundConstantTrue) { |
| 1622 | // If we already found the true constant, we might (probably only on -O0) have an OpLogicalNot which is taking a constant argument, so discover it anyway. |
| 1623 | FindConstant(Op); |
| 1624 | } else { |
| 1625 | foundConstantTrue = true; |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | continue; |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 1631 | } else if (isa<TruncInst>(I)) { |
| 1632 | // For truncation to i8 we mask against 255. |
| 1633 | Type *ToTy = I.getType(); |
| 1634 | if (8u == ToTy->getPrimitiveSizeInBits()) { |
| 1635 | LLVMContext &Context = ToTy->getContext(); |
| 1636 | Constant *Cst255 = ConstantInt::get(Type::getInt32Ty(Context), 0xff); |
| 1637 | FindConstant(Cst255); |
| 1638 | } |
| 1639 | // Fall through. |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 1640 | } else if (isa<AtomicRMWInst>(I)) { |
| 1641 | LLVMContext &Context = I.getContext(); |
| 1642 | |
| 1643 | FindConstant( |
| 1644 | ConstantInt::get(Type::getInt32Ty(Context), spv::ScopeDevice)); |
| 1645 | FindConstant(ConstantInt::get( |
| 1646 | Type::getInt32Ty(Context), |
| 1647 | spv::MemorySemanticsUniformMemoryMask | |
| 1648 | spv::MemorySemanticsSequentiallyConsistentMask)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | for (Use &Op : I.operands()) { |
| 1652 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1653 | FindConstant(Op); |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | void SPIRVProducerPass::FindConstant(Value *V) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1661 | ValueList &CstList = getConstantList(); |
| 1662 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1663 | // If V is already tracked, ignore it. |
| 1664 | if (0 != CstList.idFor(V)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1665 | return; |
| 1666 | } |
| 1667 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1668 | if (isa<GlobalValue>(V) && clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 1669 | return; |
| 1670 | } |
| 1671 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1672 | Constant *Cst = cast<Constant>(V); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1673 | Type *CstTy = Cst->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1674 | |
| 1675 | // Handle constant with <4 x i8> type specially. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1676 | if (is4xi8vec(CstTy)) { |
| 1677 | if (!isa<GlobalValue>(V)) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1678 | CstList.insert(V); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | if (Cst->getNumOperands()) { |
| 1683 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); I != E; |
| 1684 | ++I) { |
| 1685 | FindConstant(*I); |
| 1686 | } |
| 1687 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1688 | CstList.insert(Cst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1689 | return; |
| 1690 | } else if (const ConstantDataSequential *CDS = |
| 1691 | dyn_cast<ConstantDataSequential>(Cst)) { |
| 1692 | // Add constants for each element to constant list. |
| 1693 | for (unsigned i = 0; i < CDS->getNumElements(); i++) { |
| 1694 | Constant *EleCst = CDS->getElementAsConstant(i); |
| 1695 | FindConstant(EleCst); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | if (!isa<GlobalValue>(V)) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1700 | CstList.insert(V); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | spv::StorageClass SPIRVProducerPass::GetStorageClass(unsigned AddrSpace) const { |
| 1705 | switch (AddrSpace) { |
| 1706 | default: |
| 1707 | llvm_unreachable("Unsupported OpenCL address space"); |
| 1708 | case AddressSpace::Private: |
| 1709 | return spv::StorageClassFunction; |
| 1710 | case AddressSpace::Global: |
| 1711 | case AddressSpace::Constant: |
| 1712 | return spv::StorageClassStorageBuffer; |
| 1713 | case AddressSpace::Input: |
| 1714 | return spv::StorageClassInput; |
| 1715 | case AddressSpace::Local: |
| 1716 | return spv::StorageClassWorkgroup; |
| 1717 | case AddressSpace::UniformConstant: |
| 1718 | return spv::StorageClassUniformConstant; |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1719 | case AddressSpace::Uniform: |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 1720 | return spv::StorageClassUniform; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1721 | case AddressSpace::ModuleScopePrivate: |
| 1722 | return spv::StorageClassPrivate; |
| 1723 | } |
| 1724 | } |
| 1725 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1726 | spv::StorageClass |
| 1727 | SPIRVProducerPass::GetStorageClassForArgKind(clspv::ArgKind arg_kind) const { |
| 1728 | switch (arg_kind) { |
| 1729 | case clspv::ArgKind::Buffer: |
| 1730 | return spv::StorageClassStorageBuffer; |
| 1731 | case clspv::ArgKind::Pod: |
| 1732 | return clspv::Option::PodArgsInUniformBuffer() |
| 1733 | ? spv::StorageClassUniform |
| 1734 | : spv::StorageClassStorageBuffer; |
| 1735 | case clspv::ArgKind::Local: |
| 1736 | return spv::StorageClassWorkgroup; |
| 1737 | case clspv::ArgKind::ReadOnlyImage: |
| 1738 | case clspv::ArgKind::WriteOnlyImage: |
| 1739 | case clspv::ArgKind::Sampler: |
| 1740 | return spv::StorageClassUniformConstant; |
| 1741 | } |
| 1742 | } |
| 1743 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1744 | spv::BuiltIn SPIRVProducerPass::GetBuiltin(StringRef Name) const { |
| 1745 | return StringSwitch<spv::BuiltIn>(Name) |
| 1746 | .Case("__spirv_GlobalInvocationId", spv::BuiltInGlobalInvocationId) |
| 1747 | .Case("__spirv_LocalInvocationId", spv::BuiltInLocalInvocationId) |
| 1748 | .Case("__spirv_WorkgroupSize", spv::BuiltInWorkgroupSize) |
| 1749 | .Case("__spirv_NumWorkgroups", spv::BuiltInNumWorkgroups) |
| 1750 | .Case("__spirv_WorkgroupId", spv::BuiltInWorkgroupId) |
| 1751 | .Default(spv::BuiltInMax); |
| 1752 | } |
| 1753 | |
| 1754 | void SPIRVProducerPass::GenerateExtInstImport() { |
| 1755 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 1756 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 1757 | |
| 1758 | // |
| 1759 | // Generate OpExtInstImport. |
| 1760 | // |
| 1761 | // Ops[0] ... Ops[n] = Name (Literal String) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1762 | ExtInstImportID = nextID; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1763 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpExtInstImport, nextID++, |
| 1764 | MkString("GLSL.std.450"))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1765 | } |
| 1766 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 1767 | void SPIRVProducerPass::GenerateSPIRVTypes(LLVMContext& Context, const DataLayout &DL) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1768 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 1769 | ValueMapType &VMap = getValueMap(); |
| 1770 | ValueMapType &AllocatedVMap = getAllocatedValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1771 | |
| 1772 | // Map for OpTypeRuntimeArray. If argument has pointer type, 2 spirv type |
| 1773 | // instructions are generated. They are OpTypePointer and OpTypeRuntimeArray. |
| 1774 | DenseMap<Type *, uint32_t> OpRuntimeTyMap; |
| 1775 | |
| 1776 | for (Type *Ty : getTypeList()) { |
| 1777 | // Update TypeMap with nextID for reference later. |
| 1778 | TypeMap[Ty] = nextID; |
| 1779 | |
| 1780 | switch (Ty->getTypeID()) { |
| 1781 | default: { |
| 1782 | Ty->print(errs()); |
| 1783 | llvm_unreachable("Unsupported type???"); |
| 1784 | break; |
| 1785 | } |
| 1786 | case Type::MetadataTyID: |
| 1787 | case Type::LabelTyID: { |
| 1788 | // Ignore these types. |
| 1789 | break; |
| 1790 | } |
| 1791 | case Type::PointerTyID: { |
| 1792 | PointerType *PTy = cast<PointerType>(Ty); |
| 1793 | unsigned AddrSpace = PTy->getAddressSpace(); |
| 1794 | |
| 1795 | // For the purposes of our Vulkan SPIR-V type system, constant and global |
| 1796 | // are conflated. |
| 1797 | bool UseExistingOpTypePointer = false; |
| 1798 | if (AddressSpace::Constant == AddrSpace) { |
| 1799 | AddrSpace = AddressSpace::Global; |
| 1800 | |
| 1801 | // Check to see if we already created this type (for instance, if we had |
| 1802 | // a constant <type>* and a global <type>*, the type would be created by |
| 1803 | // one of these types, and shared by both). |
| 1804 | auto GlobalTy = PTy->getPointerElementType()->getPointerTo(AddrSpace); |
| 1805 | if (0 < TypeMap.count(GlobalTy)) { |
| 1806 | TypeMap[PTy] = TypeMap[GlobalTy]; |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 1807 | UseExistingOpTypePointer = true; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1808 | break; |
| 1809 | } |
| 1810 | } else if (AddressSpace::Global == AddrSpace) { |
| 1811 | AddrSpace = AddressSpace::Constant; |
| 1812 | |
| 1813 | // Check to see if we already created this type (for instance, if we had |
| 1814 | // a constant <type>* and a global <type>*, the type would be created by |
| 1815 | // one of these types, and shared by both). |
| 1816 | auto ConstantTy = PTy->getPointerElementType()->getPointerTo(AddrSpace); |
| 1817 | if (0 < TypeMap.count(ConstantTy)) { |
| 1818 | TypeMap[PTy] = TypeMap[ConstantTy]; |
| 1819 | UseExistingOpTypePointer = true; |
| 1820 | } |
| 1821 | } |
| 1822 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1823 | const bool HasArgUser = true; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1824 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1825 | if (HasArgUser && !UseExistingOpTypePointer) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1826 | // |
| 1827 | // Generate OpTypePointer. |
| 1828 | // |
| 1829 | |
| 1830 | // OpTypePointer |
| 1831 | // Ops[0] = Storage Class |
| 1832 | // Ops[1] = Element Type ID |
| 1833 | SPIRVOperandList Ops; |
| 1834 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1835 | Ops << MkNum(GetStorageClass(AddrSpace)) |
| 1836 | << MkId(lookupType(PTy->getElementType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1837 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1838 | auto *Inst = new SPIRVInstruction(spv::OpTypePointer, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1839 | SPIRVInstList.push_back(Inst); |
| 1840 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1841 | break; |
| 1842 | } |
| 1843 | case Type::StructTyID: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1844 | StructType *STy = cast<StructType>(Ty); |
| 1845 | |
| 1846 | // Handle sampler type. |
| 1847 | if (STy->isOpaque()) { |
| 1848 | if (STy->getName().equals("opencl.sampler_t")) { |
| 1849 | // |
| 1850 | // Generate OpTypeSampler |
| 1851 | // |
| 1852 | // Empty Ops. |
| 1853 | SPIRVOperandList Ops; |
| 1854 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1855 | auto *Inst = new SPIRVInstruction(spv::OpTypeSampler, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1856 | SPIRVInstList.push_back(Inst); |
| 1857 | break; |
| 1858 | } else if (STy->getName().equals("opencl.image2d_ro_t") || |
| 1859 | STy->getName().equals("opencl.image2d_wo_t") || |
| 1860 | STy->getName().equals("opencl.image3d_ro_t") || |
| 1861 | STy->getName().equals("opencl.image3d_wo_t")) { |
| 1862 | // |
| 1863 | // Generate OpTypeImage |
| 1864 | // |
| 1865 | // Ops[0] = Sampled Type ID |
| 1866 | // Ops[1] = Dim ID |
| 1867 | // Ops[2] = Depth (Literal Number) |
| 1868 | // Ops[3] = Arrayed (Literal Number) |
| 1869 | // Ops[4] = MS (Literal Number) |
| 1870 | // Ops[5] = Sampled (Literal Number) |
| 1871 | // Ops[6] = Image Format ID |
| 1872 | // |
| 1873 | SPIRVOperandList Ops; |
| 1874 | |
| 1875 | // TODO: Changed Sampled Type according to situations. |
| 1876 | uint32_t SampledTyID = lookupType(Type::getFloatTy(Context)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1877 | Ops << MkId(SampledTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1878 | |
| 1879 | spv::Dim DimID = spv::Dim2D; |
| 1880 | if (STy->getName().equals("opencl.image3d_ro_t") || |
| 1881 | STy->getName().equals("opencl.image3d_wo_t")) { |
| 1882 | DimID = spv::Dim3D; |
| 1883 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1884 | Ops << MkNum(DimID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1885 | |
| 1886 | // TODO: Set up Depth. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1887 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1888 | |
| 1889 | // TODO: Set up Arrayed. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1890 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1891 | |
| 1892 | // TODO: Set up MS. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1893 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1894 | |
| 1895 | // TODO: Set up Sampled. |
| 1896 | // |
| 1897 | // From Spec |
| 1898 | // |
| 1899 | // 0 indicates this is only known at run time, not at compile time |
| 1900 | // 1 indicates will be used with sampler |
| 1901 | // 2 indicates will be used without a sampler (a storage image) |
| 1902 | uint32_t Sampled = 1; |
| 1903 | if (STy->getName().equals("opencl.image2d_wo_t") || |
| 1904 | STy->getName().equals("opencl.image3d_wo_t")) { |
| 1905 | Sampled = 2; |
| 1906 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1907 | Ops << MkNum(Sampled); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1908 | |
| 1909 | // TODO: Set up Image Format. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1910 | Ops << MkNum(spv::ImageFormatUnknown); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1911 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1912 | auto *Inst = new SPIRVInstruction(spv::OpTypeImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1913 | SPIRVInstList.push_back(Inst); |
| 1914 | break; |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | // |
| 1919 | // Generate OpTypeStruct |
| 1920 | // |
| 1921 | // Ops[0] ... Ops[n] = Member IDs |
| 1922 | SPIRVOperandList Ops; |
| 1923 | |
| 1924 | for (auto *EleTy : STy->elements()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1925 | Ops << MkId(lookupType(EleTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1928 | uint32_t STyID = nextID; |
| 1929 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1930 | auto *Inst = |
| 1931 | new SPIRVInstruction(spv::OpTypeStruct, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1932 | SPIRVInstList.push_back(Inst); |
| 1933 | |
| 1934 | // Generate OpMemberDecorate. |
| 1935 | auto DecoInsertPoint = |
| 1936 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 1937 | [](SPIRVInstruction *Inst) -> bool { |
| 1938 | return Inst->getOpcode() != spv::OpDecorate && |
| 1939 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 1940 | Inst->getOpcode() != spv::OpExtInstImport; |
| 1941 | }); |
| 1942 | |
David Neto | c463b37 | 2017-08-10 15:32:21 -0400 | [diff] [blame] | 1943 | const auto StructLayout = DL.getStructLayout(STy); |
| 1944 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1945 | // #error TODO(dneto): Only do this if in TypesNeedingLayout. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1946 | for (unsigned MemberIdx = 0; MemberIdx < STy->getNumElements(); |
| 1947 | MemberIdx++) { |
| 1948 | // Ops[0] = Structure Type ID |
| 1949 | // Ops[1] = Member Index(Literal Number) |
| 1950 | // Ops[2] = Decoration (Offset) |
| 1951 | // Ops[3] = Byte Offset (Literal Number) |
| 1952 | Ops.clear(); |
| 1953 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1954 | Ops << MkId(STyID) << MkNum(MemberIdx) << MkNum(spv::DecorationOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1955 | |
David Neto | c463b37 | 2017-08-10 15:32:21 -0400 | [diff] [blame] | 1956 | const auto ByteOffset = |
| 1957 | uint32_t(StructLayout->getElementOffset(MemberIdx)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1958 | Ops << MkNum(ByteOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1959 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1960 | auto *DecoInst = new SPIRVInstruction(spv::OpMemberDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1961 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | // Generate OpDecorate. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1965 | if (StructTypesNeedingBlock.idFor(STy)) { |
| 1966 | Ops.clear(); |
| 1967 | // Use Block decorations with StorageBuffer storage class. |
| 1968 | Ops << MkId(STyID) << MkNum(spv::DecorationBlock); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1969 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1970 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
| 1971 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1972 | } |
| 1973 | break; |
| 1974 | } |
| 1975 | case Type::IntegerTyID: { |
| 1976 | unsigned BitWidth = Ty->getPrimitiveSizeInBits(); |
| 1977 | |
| 1978 | if (BitWidth == 1) { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1979 | auto *Inst = new SPIRVInstruction(spv::OpTypeBool, nextID++, {}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1980 | SPIRVInstList.push_back(Inst); |
| 1981 | } else { |
| 1982 | // i8 is added to TypeMap as i32. |
David Neto | 391aeb1 | 2017-08-26 15:51:58 -0400 | [diff] [blame] | 1983 | // No matter what LLVM type is requested first, always alias the |
| 1984 | // second one's SPIR-V type to be the same as the one we generated |
| 1985 | // first. |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 1986 | unsigned aliasToWidth = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1987 | if (BitWidth == 8) { |
David Neto | 391aeb1 | 2017-08-26 15:51:58 -0400 | [diff] [blame] | 1988 | aliasToWidth = 32; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1989 | BitWidth = 32; |
David Neto | 391aeb1 | 2017-08-26 15:51:58 -0400 | [diff] [blame] | 1990 | } else if (BitWidth == 32) { |
| 1991 | aliasToWidth = 8; |
| 1992 | } |
| 1993 | if (aliasToWidth) { |
| 1994 | Type* otherType = Type::getIntNTy(Ty->getContext(), aliasToWidth); |
| 1995 | auto where = TypeMap.find(otherType); |
| 1996 | if (where == TypeMap.end()) { |
| 1997 | // Go ahead and make it, but also map the other type to it. |
| 1998 | TypeMap[otherType] = nextID; |
| 1999 | } else { |
| 2000 | // Alias this SPIR-V type the existing type. |
| 2001 | TypeMap[Ty] = where->second; |
| 2002 | break; |
| 2003 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2004 | } |
| 2005 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2006 | SPIRVOperandList Ops; |
| 2007 | Ops << MkNum(BitWidth) << MkNum(0 /* not signed */); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2008 | |
| 2009 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2010 | new SPIRVInstruction(spv::OpTypeInt, nextID++, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2011 | } |
| 2012 | break; |
| 2013 | } |
| 2014 | case Type::HalfTyID: |
| 2015 | case Type::FloatTyID: |
| 2016 | case Type::DoubleTyID: { |
| 2017 | SPIRVOperand *WidthOp = new SPIRVOperand( |
| 2018 | SPIRVOperandType::LITERAL_INTEGER, Ty->getPrimitiveSizeInBits()); |
| 2019 | |
| 2020 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2021 | new SPIRVInstruction(spv::OpTypeFloat, nextID++, WidthOp)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2022 | break; |
| 2023 | } |
| 2024 | case Type::ArrayTyID: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2025 | ArrayType *ArrTy = cast<ArrayType>(Ty); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2026 | const uint64_t Length = ArrTy->getArrayNumElements(); |
| 2027 | if (Length == 0) { |
| 2028 | // By convention, map it to a RuntimeArray. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2029 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2030 | // Only generate the type once. |
| 2031 | // TODO(dneto): Can it ever be generated more than once? |
| 2032 | // Doesn't LLVM type uniqueness guarantee we'll only see this |
| 2033 | // once? |
| 2034 | Type *EleTy = ArrTy->getArrayElementType(); |
| 2035 | if (OpRuntimeTyMap.count(EleTy) == 0) { |
| 2036 | uint32_t OpTypeRuntimeArrayID = nextID; |
| 2037 | OpRuntimeTyMap[Ty] = nextID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2038 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2039 | // |
| 2040 | // Generate OpTypeRuntimeArray. |
| 2041 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2042 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2043 | // OpTypeRuntimeArray |
| 2044 | // Ops[0] = Element Type ID |
| 2045 | SPIRVOperandList Ops; |
| 2046 | Ops << MkId(lookupType(EleTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2047 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2048 | SPIRVInstList.push_back( |
| 2049 | new SPIRVInstruction(spv::OpTypeRuntimeArray, nextID++, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2050 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2051 | if (Hack_generate_runtime_array_stride_early) { |
| 2052 | // Generate OpDecorate. |
| 2053 | auto DecoInsertPoint = std::find_if( |
| 2054 | SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2055 | [](SPIRVInstruction *Inst) -> bool { |
| 2056 | return Inst->getOpcode() != spv::OpDecorate && |
| 2057 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2058 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2059 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2060 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2061 | // Ops[0] = Target ID |
| 2062 | // Ops[1] = Decoration (ArrayStride) |
| 2063 | // Ops[2] = Stride Number(Literal Number) |
| 2064 | Ops.clear(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2065 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2066 | Ops << MkId(OpTypeRuntimeArrayID) |
| 2067 | << MkNum(spv::DecorationArrayStride) |
| 2068 | << MkNum(static_cast<uint32_t>(DL.getTypeAllocSize(EleTy))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2069 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2070 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
| 2071 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
| 2072 | } |
| 2073 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2074 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2075 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2076 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2077 | // |
| 2078 | // Generate OpConstant and OpTypeArray. |
| 2079 | // |
| 2080 | |
| 2081 | // |
| 2082 | // Generate OpConstant for array length. |
| 2083 | // |
| 2084 | // Ops[0] = Result Type ID |
| 2085 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 2086 | SPIRVOperandList Ops; |
| 2087 | |
| 2088 | Type *LengthTy = Type::getInt32Ty(Context); |
| 2089 | uint32_t ResTyID = lookupType(LengthTy); |
| 2090 | Ops << MkId(ResTyID); |
| 2091 | |
| 2092 | assert(Length < UINT32_MAX); |
| 2093 | Ops << MkNum(static_cast<uint32_t>(Length)); |
| 2094 | |
| 2095 | // Add constant for length to constant list. |
| 2096 | Constant *CstLength = ConstantInt::get(LengthTy, Length); |
| 2097 | AllocatedVMap[CstLength] = nextID; |
| 2098 | VMap[CstLength] = nextID; |
| 2099 | uint32_t LengthID = nextID; |
| 2100 | |
| 2101 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
| 2102 | SPIRVInstList.push_back(CstInst); |
| 2103 | |
| 2104 | // Remember to generate ArrayStride later |
| 2105 | getTypesNeedingArrayStride().insert(Ty); |
| 2106 | |
| 2107 | // |
| 2108 | // Generate OpTypeArray. |
| 2109 | // |
| 2110 | // Ops[0] = Element Type ID |
| 2111 | // Ops[1] = Array Length Constant ID |
| 2112 | Ops.clear(); |
| 2113 | |
| 2114 | uint32_t EleTyID = lookupType(ArrTy->getElementType()); |
| 2115 | Ops << MkId(EleTyID) << MkId(LengthID); |
| 2116 | |
| 2117 | // Update TypeMap with nextID. |
| 2118 | TypeMap[Ty] = nextID; |
| 2119 | |
| 2120 | auto *ArrayInst = new SPIRVInstruction(spv::OpTypeArray, nextID++, Ops); |
| 2121 | SPIRVInstList.push_back(ArrayInst); |
| 2122 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2123 | break; |
| 2124 | } |
| 2125 | case Type::VectorTyID: { |
| 2126 | // <4 x i8> is changed to i32. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2127 | if (Ty->getVectorElementType() == Type::getInt8Ty(Context)) { |
| 2128 | if (Ty->getVectorNumElements() == 4) { |
| 2129 | TypeMap[Ty] = lookupType(Ty->getVectorElementType()); |
| 2130 | break; |
| 2131 | } else { |
| 2132 | Ty->print(errs()); |
| 2133 | llvm_unreachable("Support above i8 vector type"); |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | // Ops[0] = Component Type ID |
| 2138 | // Ops[1] = Component Count (Literal Number) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2139 | SPIRVOperandList Ops; |
| 2140 | Ops << MkId(lookupType(Ty->getVectorElementType())) |
| 2141 | << MkNum(Ty->getVectorNumElements()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2142 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2143 | SPIRVInstruction* inst = new SPIRVInstruction(spv::OpTypeVector, nextID++, Ops); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2144 | SPIRVInstList.push_back(inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2145 | break; |
| 2146 | } |
| 2147 | case Type::VoidTyID: { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2148 | auto *Inst = new SPIRVInstruction(spv::OpTypeVoid, nextID++, {}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2149 | SPIRVInstList.push_back(Inst); |
| 2150 | break; |
| 2151 | } |
| 2152 | case Type::FunctionTyID: { |
| 2153 | // Generate SPIRV instruction for function type. |
| 2154 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 2155 | |
| 2156 | // Ops[0] = Return Type ID |
| 2157 | // Ops[1] ... Ops[n] = Parameter Type IDs |
| 2158 | SPIRVOperandList Ops; |
| 2159 | |
| 2160 | // Find SPIRV instruction for return type |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2161 | Ops << MkId(lookupType(FTy->getReturnType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2162 | |
| 2163 | // Find SPIRV instructions for parameter types |
| 2164 | for (unsigned k = 0; k < FTy->getNumParams(); k++) { |
| 2165 | // Find SPIRV instruction for parameter type. |
| 2166 | auto ParamTy = FTy->getParamType(k); |
| 2167 | if (ParamTy->isPointerTy()) { |
| 2168 | auto PointeeTy = ParamTy->getPointerElementType(); |
| 2169 | if (PointeeTy->isStructTy() && |
| 2170 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 2171 | ParamTy = PointeeTy; |
| 2172 | } |
| 2173 | } |
| 2174 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2175 | Ops << MkId(lookupType(ParamTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2176 | } |
| 2177 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2178 | auto *Inst = new SPIRVInstruction(spv::OpTypeFunction, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2179 | SPIRVInstList.push_back(Inst); |
| 2180 | break; |
| 2181 | } |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | // Generate OpTypeSampledImage. |
| 2186 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 2187 | for (auto &ImageType : OpImageTypeMap) { |
| 2188 | // |
| 2189 | // Generate OpTypeSampledImage. |
| 2190 | // |
| 2191 | // Ops[0] = Image Type ID |
| 2192 | // |
| 2193 | SPIRVOperandList Ops; |
| 2194 | |
| 2195 | Type *ImgTy = ImageType.first; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2196 | Ops << MkId(TypeMap[ImgTy]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2197 | |
| 2198 | // Update OpImageTypeMap. |
| 2199 | ImageType.second = nextID; |
| 2200 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2201 | auto *Inst = new SPIRVInstruction(spv::OpTypeSampledImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2202 | SPIRVInstList.push_back(Inst); |
| 2203 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2204 | |
| 2205 | // Generate types for pointer-to-local arguments. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 2206 | for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_; |
| 2207 | ++spec_id) { |
| 2208 | LocalArgInfo& arg_info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2209 | |
| 2210 | // Generate the spec constant. |
| 2211 | SPIRVOperandList Ops; |
| 2212 | Ops << MkId(lookupType(Type::getInt32Ty(Context))) << MkNum(1); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2213 | SPIRVInstList.push_back( |
| 2214 | new SPIRVInstruction(spv::OpSpecConstant, arg_info.array_size_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2215 | |
| 2216 | // Generate the array type. |
| 2217 | Ops.clear(); |
| 2218 | // The element type must have been created. |
| 2219 | uint32_t elem_ty_id = lookupType(arg_info.elem_type); |
| 2220 | assert(elem_ty_id); |
| 2221 | Ops << MkId(elem_ty_id) << MkId(arg_info.array_size_id); |
| 2222 | |
| 2223 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2224 | new SPIRVInstruction(spv::OpTypeArray, arg_info.array_type_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2225 | |
| 2226 | Ops.clear(); |
| 2227 | Ops << MkNum(spv::StorageClassWorkgroup) << MkId(arg_info.array_type_id); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2228 | SPIRVInstList.push_back(new SPIRVInstruction( |
| 2229 | spv::OpTypePointer, arg_info.ptr_array_type_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2230 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | void SPIRVProducerPass::GenerateSPIRVConstants() { |
| 2234 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 2235 | ValueMapType &VMap = getValueMap(); |
| 2236 | ValueMapType &AllocatedVMap = getAllocatedValueMap(); |
| 2237 | ValueList &CstList = getConstantList(); |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 2238 | const bool hack_undef = clspv::Option::HackUndef(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2239 | |
| 2240 | for (uint32_t i = 0; i < CstList.size(); i++) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2241 | // UniqueVector ids are 1-based. |
| 2242 | Constant *Cst = cast<Constant>(CstList[i+1]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2243 | |
| 2244 | // OpTypeArray's constant was already generated. |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2245 | if (AllocatedVMap.find_as(Cst) != AllocatedVMap.end()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2246 | continue; |
| 2247 | } |
| 2248 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2249 | // Set ValueMap with nextID for reference later. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2250 | VMap[Cst] = nextID; |
| 2251 | |
| 2252 | // |
| 2253 | // Generate OpConstant. |
| 2254 | // |
| 2255 | |
| 2256 | // Ops[0] = Result Type ID |
| 2257 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 2258 | SPIRVOperandList Ops; |
| 2259 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2260 | Ops << MkId(lookupType(Cst->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2261 | |
| 2262 | std::vector<uint32_t> LiteralNum; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2263 | spv::Op Opcode = spv::OpNop; |
| 2264 | |
| 2265 | if (isa<UndefValue>(Cst)) { |
| 2266 | // Ops[0] = Result Type ID |
David Neto | c66b335 | 2017-10-20 14:28:46 -0400 | [diff] [blame] | 2267 | Opcode = spv::OpUndef; |
| 2268 | if (hack_undef) { |
| 2269 | Type *type = Cst->getType(); |
| 2270 | if (type->isFPOrFPVectorTy() || type->isIntOrIntVectorTy()) { |
| 2271 | Opcode = spv::OpConstantNull; |
| 2272 | } |
| 2273 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2274 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Cst)) { |
| 2275 | unsigned BitWidth = CI->getBitWidth(); |
| 2276 | if (BitWidth == 1) { |
| 2277 | // If the bitwidth of constant is 1, generate OpConstantTrue or |
| 2278 | // OpConstantFalse. |
| 2279 | if (CI->getZExtValue()) { |
| 2280 | // Ops[0] = Result Type ID |
| 2281 | Opcode = spv::OpConstantTrue; |
| 2282 | } else { |
| 2283 | // Ops[0] = Result Type ID |
| 2284 | Opcode = spv::OpConstantFalse; |
| 2285 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2286 | } else { |
| 2287 | auto V = CI->getZExtValue(); |
| 2288 | LiteralNum.push_back(V & 0xFFFFFFFF); |
| 2289 | |
| 2290 | if (BitWidth > 32) { |
| 2291 | LiteralNum.push_back(V >> 32); |
| 2292 | } |
| 2293 | |
| 2294 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2295 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2296 | Ops << MkInteger(LiteralNum); |
| 2297 | |
| 2298 | if (BitWidth == 32 && V == 0) { |
| 2299 | constant_i32_zero_id_ = nextID; |
| 2300 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2301 | } |
| 2302 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Cst)) { |
| 2303 | uint64_t FPVal = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 2304 | Type *CFPTy = CFP->getType(); |
| 2305 | if (CFPTy->isFloatTy()) { |
| 2306 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
| 2307 | } else { |
| 2308 | CFPTy->print(errs()); |
| 2309 | llvm_unreachable("Implement this ConstantFP Type"); |
| 2310 | } |
| 2311 | |
| 2312 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2313 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2314 | Ops << MkFloat(LiteralNum); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2315 | } else if (isa<ConstantDataSequential>(Cst) && |
| 2316 | cast<ConstantDataSequential>(Cst)->isString()) { |
| 2317 | Cst->print(errs()); |
| 2318 | llvm_unreachable("Implement this Constant"); |
| 2319 | |
| 2320 | } else if (const ConstantDataSequential *CDS = |
| 2321 | dyn_cast<ConstantDataSequential>(Cst)) { |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2322 | // Let's convert <4 x i8> constant to int constant specially. |
| 2323 | // This case occurs when all the values are specified as constant |
| 2324 | // ints. |
| 2325 | Type *CstTy = Cst->getType(); |
| 2326 | if (is4xi8vec(CstTy)) { |
| 2327 | LLVMContext &Context = CstTy->getContext(); |
| 2328 | |
| 2329 | // |
| 2330 | // Generate OpConstant with OpTypeInt 32 0. |
| 2331 | // |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2332 | uint32_t IntValue = 0; |
| 2333 | for (unsigned k = 0; k < 4; k++) { |
| 2334 | const uint64_t Val = CDS->getElementAsInteger(k); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2335 | IntValue = (IntValue << 8) | (Val & 0xffu); |
| 2336 | } |
| 2337 | |
| 2338 | Type *i32 = Type::getInt32Ty(Context); |
| 2339 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
| 2340 | // If this constant is already registered on VMap, use it. |
| 2341 | if (VMap.count(CstInt)) { |
| 2342 | uint32_t CstID = VMap[CstInt]; |
| 2343 | VMap[Cst] = CstID; |
| 2344 | continue; |
| 2345 | } |
| 2346 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2347 | Ops << MkNum(IntValue); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2348 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2349 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2350 | SPIRVInstList.push_back(CstInst); |
| 2351 | |
| 2352 | continue; |
| 2353 | } |
| 2354 | |
| 2355 | // A normal constant-data-sequential case. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2356 | for (unsigned k = 0; k < CDS->getNumElements(); k++) { |
| 2357 | Constant *EleCst = CDS->getElementAsConstant(k); |
| 2358 | uint32_t EleCstID = VMap[EleCst]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2359 | Ops << MkId(EleCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2363 | } else if (const ConstantAggregate *CA = dyn_cast<ConstantAggregate>(Cst)) { |
| 2364 | // Let's convert <4 x i8> constant to int constant specially. |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2365 | // This case occurs when at least one of the values is an undef. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2366 | Type *CstTy = Cst->getType(); |
| 2367 | if (is4xi8vec(CstTy)) { |
| 2368 | LLVMContext &Context = CstTy->getContext(); |
| 2369 | |
| 2370 | // |
| 2371 | // Generate OpConstant with OpTypeInt 32 0. |
| 2372 | // |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2373 | uint32_t IntValue = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2374 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); |
| 2375 | I != E; ++I) { |
| 2376 | uint64_t Val = 0; |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2377 | const Value* CV = *I; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2378 | if (auto *CI2 = dyn_cast<ConstantInt>(CV)) { |
| 2379 | Val = CI2->getZExtValue(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2380 | } |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2381 | IntValue = (IntValue << 8) | (Val & 0xffu); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2382 | } |
| 2383 | |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2384 | Type *i32 = Type::getInt32Ty(Context); |
| 2385 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2386 | // If this constant is already registered on VMap, use it. |
| 2387 | if (VMap.count(CstInt)) { |
| 2388 | uint32_t CstID = VMap[CstInt]; |
| 2389 | VMap[Cst] = CstID; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 2390 | continue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2391 | } |
| 2392 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2393 | Ops << MkNum(IntValue); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2394 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2395 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2396 | SPIRVInstList.push_back(CstInst); |
| 2397 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 2398 | continue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | // We use a constant composite in SPIR-V for our constant aggregate in |
| 2402 | // LLVM. |
| 2403 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2404 | |
| 2405 | for (unsigned k = 0; k < CA->getNumOperands(); k++) { |
| 2406 | // Look up the ID of the element of this aggregate (which we will |
| 2407 | // previously have created a constant for). |
| 2408 | uint32_t ElementConstantID = VMap[CA->getAggregateElement(k)]; |
| 2409 | |
| 2410 | // And add an operand to the composite we are constructing |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2411 | Ops << MkId(ElementConstantID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2412 | } |
| 2413 | } else if (Cst->isNullValue()) { |
| 2414 | Opcode = spv::OpConstantNull; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2415 | } else { |
| 2416 | Cst->print(errs()); |
| 2417 | llvm_unreachable("Unsupported Constant???"); |
| 2418 | } |
| 2419 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2420 | auto *CstInst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2421 | SPIRVInstList.push_back(CstInst); |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | void SPIRVProducerPass::GenerateSamplers(Module &M) { |
| 2426 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 2427 | ValueMapType &VMap = getValueMap(); |
| 2428 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2429 | auto& sampler_map = getSamplerMap(); |
| 2430 | SamplerMapIndexToIDMap.clear(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2431 | DenseMap<unsigned, unsigned> SamplerLiteralToIDMap; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2432 | DenseMap<unsigned, unsigned> SamplerLiteralToDescriptorSetMap; |
| 2433 | DenseMap<unsigned, unsigned> SamplerLiteralToBindingMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2434 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2435 | // We might have samplers in the sampler map that are not used |
| 2436 | // in the translation unit. We need to allocate variables |
| 2437 | // for them and bindings too. |
| 2438 | DenseSet<unsigned> used_bindings; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2439 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2440 | auto* var_fn = M.getFunction("clspv.sampler.var.literal"); |
| 2441 | if (!var_fn) return; |
| 2442 | for (auto user : var_fn->users()) { |
| 2443 | // Populate SamplerLiteralToDescriptorSetMap and |
| 2444 | // SamplerLiteralToBindingMap. |
| 2445 | // |
| 2446 | // Look for calls like |
| 2447 | // call %opencl.sampler_t addrspace(2)* |
| 2448 | // @clspv.sampler.var.literal( |
| 2449 | // i32 descriptor, |
| 2450 | // i32 binding, |
| 2451 | // i32 index-into-sampler-map) |
| 2452 | if (auto* call = dyn_cast<CallInst>(user)) { |
| 2453 | const auto index_into_sampler_map = |
| 2454 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue(); |
| 2455 | if (index_into_sampler_map >= sampler_map.size()) { |
| 2456 | errs() << "Out of bounds index to sampler map: " << index_into_sampler_map; |
| 2457 | llvm_unreachable("bad sampler init: out of bounds"); |
| 2458 | } |
| 2459 | |
| 2460 | auto sampler_value = sampler_map[index_into_sampler_map].first; |
| 2461 | const auto descriptor_set = static_cast<unsigned>( |
| 2462 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 2463 | const auto binding = static_cast<unsigned>( |
| 2464 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 2465 | |
| 2466 | SamplerLiteralToDescriptorSetMap[sampler_value] = descriptor_set; |
| 2467 | SamplerLiteralToBindingMap[sampler_value] = binding; |
| 2468 | used_bindings.insert(binding); |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | unsigned index = 0; |
| 2473 | for (auto SamplerLiteral : sampler_map) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2474 | // Generate OpVariable. |
| 2475 | // |
| 2476 | // GIDOps[0] : Result Type ID |
| 2477 | // GIDOps[1] : Storage Class |
| 2478 | SPIRVOperandList Ops; |
| 2479 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2480 | Ops << MkId(lookupType(SamplerTy)) |
| 2481 | << MkNum(spv::StorageClassUniformConstant); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2482 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2483 | auto sampler_var_id = nextID++; |
| 2484 | auto *Inst = new SPIRVInstruction(spv::OpVariable, sampler_var_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2485 | SPIRVInstList.push_back(Inst); |
| 2486 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2487 | SamplerMapIndexToIDMap[index] = sampler_var_id; |
| 2488 | SamplerLiteralToIDMap[SamplerLiteral.first] = sampler_var_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2489 | |
| 2490 | // Find Insert Point for OpDecorate. |
| 2491 | auto DecoInsertPoint = |
| 2492 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2493 | [](SPIRVInstruction *Inst) -> bool { |
| 2494 | return Inst->getOpcode() != spv::OpDecorate && |
| 2495 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2496 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2497 | }); |
| 2498 | |
| 2499 | // Ops[0] = Target ID |
| 2500 | // Ops[1] = Decoration (DescriptorSet) |
| 2501 | // Ops[2] = LiteralNumber according to Decoration |
| 2502 | Ops.clear(); |
| 2503 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2504 | unsigned descriptor_set; |
| 2505 | unsigned binding; |
| 2506 | if(SamplerLiteralToBindingMap.find(SamplerLiteral.first) == SamplerLiteralToBindingMap.end()) { |
| 2507 | // This sampler is not actually used. Find the next one. |
| 2508 | for (binding = 0; used_bindings.count(binding); binding++) |
| 2509 | ; |
| 2510 | descriptor_set = 0; // Literal samplers always use descriptor set 0. |
| 2511 | used_bindings.insert(binding); |
| 2512 | } else { |
| 2513 | descriptor_set = SamplerLiteralToDescriptorSetMap[SamplerLiteral.first]; |
| 2514 | binding = SamplerLiteralToBindingMap[SamplerLiteral.first]; |
| 2515 | } |
| 2516 | |
| 2517 | Ops << MkId(sampler_var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2518 | << MkNum(descriptor_set); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2519 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 2520 | descriptorMapOut << "sampler," << SamplerLiteral.first << ",samplerExpr,\"" |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2521 | << SamplerLiteral.second << "\",descriptorSet," |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2522 | << descriptor_set << ",binding," << binding << "\n"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2523 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2524 | auto *DescDecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2525 | SPIRVInstList.insert(DecoInsertPoint, DescDecoInst); |
| 2526 | |
| 2527 | // Ops[0] = Target ID |
| 2528 | // Ops[1] = Decoration (Binding) |
| 2529 | // Ops[2] = LiteralNumber according to Decoration |
| 2530 | Ops.clear(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2531 | Ops << MkId(sampler_var_id) << MkNum(spv::DecorationBinding) |
| 2532 | << MkNum(binding); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2533 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2534 | auto *BindDecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2535 | SPIRVInstList.insert(DecoInsertPoint, BindDecoInst); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2536 | |
| 2537 | index++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2538 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2539 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2540 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2541 | void SPIRVProducerPass::GenerateResourceVars(Module &M) { |
| 2542 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 2543 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2544 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2545 | // Generate variables. Make one for each of resource var info object. |
| 2546 | for (auto *info : ModuleOrderedResourceVars) { |
| 2547 | Type *type = info->var_fn->getReturnType(); |
| 2548 | // Remap the address space for opaque types. |
| 2549 | switch (info->arg_kind) { |
| 2550 | case clspv::ArgKind::Sampler: |
| 2551 | case clspv::ArgKind::ReadOnlyImage: |
| 2552 | case clspv::ArgKind::WriteOnlyImage: |
| 2553 | type = PointerType::get(type->getPointerElementType(), |
| 2554 | clspv::AddressSpace::UniformConstant); |
| 2555 | break; |
| 2556 | default: |
| 2557 | break; |
| 2558 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2559 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2560 | info->var_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2561 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2562 | const auto type_id = lookupType(type); |
| 2563 | const auto sc = GetStorageClassForArgKind(info->arg_kind); |
| 2564 | SPIRVOperandList Ops; |
| 2565 | Ops << MkId(type_id) << MkNum(sc); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2566 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2567 | auto *Inst = new SPIRVInstruction(spv::OpVariable, info->var_id, Ops); |
| 2568 | SPIRVInstList.push_back(Inst); |
| 2569 | |
| 2570 | // Map calls to the variable-builtin-function. |
| 2571 | for (auto &U : info->var_fn->uses()) { |
| 2572 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 2573 | const auto set = unsigned( |
| 2574 | dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue()); |
| 2575 | const auto binding = unsigned( |
| 2576 | dyn_cast<ConstantInt>(call->getOperand(1))->getZExtValue()); |
| 2577 | if (set == info->descriptor_set && binding == info->binding) { |
| 2578 | switch (info->arg_kind) { |
| 2579 | case clspv::ArgKind::Buffer: |
| 2580 | case clspv::ArgKind::Pod: |
| 2581 | // The call maps to the variable directly. |
| 2582 | VMap[call] = info->var_id; |
| 2583 | break; |
| 2584 | case clspv::ArgKind::Sampler: |
| 2585 | case clspv::ArgKind::ReadOnlyImage: |
| 2586 | case clspv::ArgKind::WriteOnlyImage: |
| 2587 | // The call maps to a load we generate later. |
| 2588 | ResourceVarDeferredLoadCalls[call] = info->var_id; |
| 2589 | break; |
| 2590 | default: |
| 2591 | llvm_unreachable("Unhandled arg kind"); |
| 2592 | } |
| 2593 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2594 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2595 | } |
| 2596 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2597 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2598 | // Generate associated decorations. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2599 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2600 | // Find Insert Point for OpDecorate. |
| 2601 | auto DecoInsertPoint = |
| 2602 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2603 | [](SPIRVInstruction *Inst) -> bool { |
| 2604 | return Inst->getOpcode() != spv::OpDecorate && |
| 2605 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2606 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2607 | }); |
| 2608 | |
| 2609 | SPIRVOperandList Ops; |
| 2610 | for (auto *info : ModuleOrderedResourceVars) { |
| 2611 | // Decorate with DescriptorSet and Binding. |
| 2612 | Ops.clear(); |
| 2613 | Ops << MkId(info->var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2614 | << MkNum(info->descriptor_set); |
| 2615 | SPIRVInstList.insert(DecoInsertPoint, |
| 2616 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2617 | |
| 2618 | Ops.clear(); |
| 2619 | Ops << MkId(info->var_id) << MkNum(spv::DecorationBinding) |
| 2620 | << MkNum(info->binding); |
| 2621 | SPIRVInstList.insert(DecoInsertPoint, |
| 2622 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2623 | |
| 2624 | // Generate NonWritable and NonReadable |
| 2625 | switch (info->arg_kind) { |
| 2626 | case clspv::ArgKind::Buffer: |
| 2627 | if (info->var_fn->getReturnType()->getPointerAddressSpace() == |
| 2628 | clspv::AddressSpace::Constant) { |
| 2629 | Ops.clear(); |
| 2630 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonWritable); |
| 2631 | SPIRVInstList.insert(DecoInsertPoint, |
| 2632 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2633 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2634 | break; |
| 2635 | case clspv::ArgKind::ReadOnlyImage: |
| 2636 | Ops.clear(); |
| 2637 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonWritable); |
| 2638 | SPIRVInstList.insert(DecoInsertPoint, |
| 2639 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2640 | break; |
| 2641 | case clspv::ArgKind::WriteOnlyImage: |
| 2642 | Ops.clear(); |
| 2643 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonReadable); |
| 2644 | SPIRVInstList.insert(DecoInsertPoint, |
| 2645 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2646 | break; |
| 2647 | default: |
| 2648 | break; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) { |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 2654 | Module& M = *GV.getParent(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2655 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 2656 | ValueMapType &VMap = getValueMap(); |
| 2657 | std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2658 | const DataLayout &DL = GV.getParent()->getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2659 | |
| 2660 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 2661 | Type *Ty = GV.getType(); |
| 2662 | PointerType *PTy = cast<PointerType>(Ty); |
| 2663 | |
| 2664 | uint32_t InitializerID = 0; |
| 2665 | |
| 2666 | // Workgroup size is handled differently (it goes into a constant) |
| 2667 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 2668 | std::vector<bool> HasMDVec; |
| 2669 | uint32_t PrevXDimCst = 0xFFFFFFFF; |
| 2670 | uint32_t PrevYDimCst = 0xFFFFFFFF; |
| 2671 | uint32_t PrevZDimCst = 0xFFFFFFFF; |
| 2672 | for (Function &Func : *GV.getParent()) { |
| 2673 | if (Func.isDeclaration()) { |
| 2674 | continue; |
| 2675 | } |
| 2676 | |
| 2677 | // We only need to check kernels. |
| 2678 | if (Func.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 2679 | continue; |
| 2680 | } |
| 2681 | |
| 2682 | if (const MDNode *MD = |
| 2683 | dyn_cast<Function>(&Func)->getMetadata("reqd_work_group_size")) { |
| 2684 | uint32_t CurXDimCst = static_cast<uint32_t>( |
| 2685 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 2686 | uint32_t CurYDimCst = static_cast<uint32_t>( |
| 2687 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 2688 | uint32_t CurZDimCst = static_cast<uint32_t>( |
| 2689 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 2690 | |
| 2691 | if (PrevXDimCst == 0xFFFFFFFF && PrevYDimCst == 0xFFFFFFFF && |
| 2692 | PrevZDimCst == 0xFFFFFFFF) { |
| 2693 | PrevXDimCst = CurXDimCst; |
| 2694 | PrevYDimCst = CurYDimCst; |
| 2695 | PrevZDimCst = CurZDimCst; |
| 2696 | } else if (CurXDimCst != PrevXDimCst || CurYDimCst != PrevYDimCst || |
| 2697 | CurZDimCst != PrevZDimCst) { |
| 2698 | llvm_unreachable( |
| 2699 | "reqd_work_group_size must be the same across all kernels"); |
| 2700 | } else { |
| 2701 | continue; |
| 2702 | } |
| 2703 | |
| 2704 | // |
| 2705 | // Generate OpConstantComposite. |
| 2706 | // |
| 2707 | // Ops[0] : Result Type ID |
| 2708 | // Ops[1] : Constant size for x dimension. |
| 2709 | // Ops[2] : Constant size for y dimension. |
| 2710 | // Ops[3] : Constant size for z dimension. |
| 2711 | SPIRVOperandList Ops; |
| 2712 | |
| 2713 | uint32_t XDimCstID = |
| 2714 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(0))]; |
| 2715 | uint32_t YDimCstID = |
| 2716 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(1))]; |
| 2717 | uint32_t ZDimCstID = |
| 2718 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(2))]; |
| 2719 | |
| 2720 | InitializerID = nextID; |
| 2721 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2722 | Ops << MkId(lookupType(Ty->getPointerElementType())) << MkId(XDimCstID) |
| 2723 | << MkId(YDimCstID) << MkId(ZDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2724 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2725 | auto *Inst = |
| 2726 | new SPIRVInstruction(spv::OpConstantComposite, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2727 | SPIRVInstList.push_back(Inst); |
| 2728 | |
| 2729 | HasMDVec.push_back(true); |
| 2730 | } else { |
| 2731 | HasMDVec.push_back(false); |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | // Check all kernels have same definitions for work_group_size. |
| 2736 | bool HasMD = false; |
| 2737 | if (!HasMDVec.empty()) { |
| 2738 | HasMD = HasMDVec[0]; |
| 2739 | for (uint32_t i = 1; i < HasMDVec.size(); i++) { |
| 2740 | if (HasMD != HasMDVec[i]) { |
| 2741 | llvm_unreachable( |
| 2742 | "Kernels should have consistent work group size definition"); |
| 2743 | } |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | // If all kernels do not have metadata for reqd_work_group_size, generate |
| 2748 | // OpSpecConstants for x/y/z dimension. |
| 2749 | if (!HasMD) { |
| 2750 | // |
| 2751 | // Generate OpSpecConstants for x/y/z dimension. |
| 2752 | // |
| 2753 | // Ops[0] : Result Type ID |
| 2754 | // Ops[1] : Constant size for x/y/z dimension (Literal Number). |
| 2755 | uint32_t XDimCstID = 0; |
| 2756 | uint32_t YDimCstID = 0; |
| 2757 | uint32_t ZDimCstID = 0; |
| 2758 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2759 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2760 | uint32_t result_type_id = |
| 2761 | lookupType(Ty->getPointerElementType()->getSequentialElementType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2762 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2763 | // X Dimension |
| 2764 | Ops << MkId(result_type_id) << MkNum(1); |
| 2765 | XDimCstID = nextID++; |
| 2766 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2767 | new SPIRVInstruction(spv::OpSpecConstant, XDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2768 | |
| 2769 | // Y Dimension |
| 2770 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2771 | Ops << MkId(result_type_id) << MkNum(1); |
| 2772 | YDimCstID = nextID++; |
| 2773 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2774 | new SPIRVInstruction(spv::OpSpecConstant, YDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2775 | |
| 2776 | // Z Dimension |
| 2777 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2778 | Ops << MkId(result_type_id) << MkNum(1); |
| 2779 | ZDimCstID = nextID++; |
| 2780 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2781 | new SPIRVInstruction(spv::OpSpecConstant, ZDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2782 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2783 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2784 | BuiltinDimVec.push_back(XDimCstID); |
| 2785 | BuiltinDimVec.push_back(YDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2786 | BuiltinDimVec.push_back(ZDimCstID); |
| 2787 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2788 | |
| 2789 | // |
| 2790 | // Generate OpSpecConstantComposite. |
| 2791 | // |
| 2792 | // Ops[0] : Result Type ID |
| 2793 | // Ops[1] : Constant size for x dimension. |
| 2794 | // Ops[2] : Constant size for y dimension. |
| 2795 | // Ops[3] : Constant size for z dimension. |
| 2796 | InitializerID = nextID; |
| 2797 | |
| 2798 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2799 | Ops << MkId(lookupType(Ty->getPointerElementType())) << MkId(XDimCstID) |
| 2800 | << MkId(YDimCstID) << MkId(ZDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2801 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2802 | auto *Inst = |
| 2803 | new SPIRVInstruction(spv::OpSpecConstantComposite, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2804 | SPIRVInstList.push_back(Inst); |
| 2805 | } |
| 2806 | } |
| 2807 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2808 | VMap[&GV] = nextID; |
| 2809 | |
| 2810 | // |
| 2811 | // Generate OpVariable. |
| 2812 | // |
| 2813 | // GIDOps[0] : Result Type ID |
| 2814 | // GIDOps[1] : Storage Class |
| 2815 | SPIRVOperandList Ops; |
| 2816 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2817 | const auto AS = PTy->getAddressSpace(); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2818 | Ops << MkId(lookupType(Ty)) << MkNum(GetStorageClass(AS)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2819 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2820 | if (GV.hasInitializer()) { |
| 2821 | InitializerID = VMap[GV.getInitializer()]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2822 | } |
| 2823 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2824 | const bool module_scope_constant_external_init = |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2825 | (AS == AddressSpace::Constant) && GV.hasInitializer() && |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2826 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 2827 | |
| 2828 | if (0 != InitializerID) { |
| 2829 | if (!module_scope_constant_external_init) { |
| 2830 | // Emit the ID of the intiializer as part of the variable definition. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2831 | Ops << MkId(InitializerID); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2832 | } |
| 2833 | } |
| 2834 | const uint32_t var_id = nextID++; |
| 2835 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2836 | auto *Inst = new SPIRVInstruction(spv::OpVariable, var_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2837 | SPIRVInstList.push_back(Inst); |
| 2838 | |
| 2839 | // If we have a builtin. |
| 2840 | if (spv::BuiltInMax != BuiltinType) { |
| 2841 | // Find Insert Point for OpDecorate. |
| 2842 | auto DecoInsertPoint = |
| 2843 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2844 | [](SPIRVInstruction *Inst) -> bool { |
| 2845 | return Inst->getOpcode() != spv::OpDecorate && |
| 2846 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2847 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2848 | }); |
| 2849 | // |
| 2850 | // Generate OpDecorate. |
| 2851 | // |
| 2852 | // DOps[0] = Target ID |
| 2853 | // DOps[1] = Decoration (Builtin) |
| 2854 | // DOps[2] = BuiltIn ID |
| 2855 | uint32_t ResultID; |
| 2856 | |
| 2857 | // WorkgroupSize is different, we decorate the constant composite that has |
| 2858 | // its value, rather than the variable that we use to access the value. |
| 2859 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 2860 | ResultID = InitializerID; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 2861 | // Save both the value and variable IDs for later. |
| 2862 | WorkgroupSizeValueID = InitializerID; |
| 2863 | WorkgroupSizeVarID = VMap[&GV]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2864 | } else { |
| 2865 | ResultID = VMap[&GV]; |
| 2866 | } |
| 2867 | |
| 2868 | SPIRVOperandList DOps; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2869 | DOps << MkId(ResultID) << MkNum(spv::DecorationBuiltIn) |
| 2870 | << MkNum(BuiltinType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2871 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2872 | auto *DescDecoInst = new SPIRVInstruction(spv::OpDecorate, DOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2873 | SPIRVInstList.insert(DecoInsertPoint, DescDecoInst); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2874 | } else if (module_scope_constant_external_init) { |
| 2875 | // This module scope constant is initialized from a storage buffer with data |
| 2876 | // provided by the host at binding 0 of the next descriptor set. |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 2877 | const uint32_t descriptor_set = TakeDescriptorIndex(&M); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2878 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2879 | // Emit the intializer to the descriptor map file. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2880 | // Use "kind,buffer" to indicate storage buffer. We might want to expand |
| 2881 | // that later to other types, like uniform buffer. |
| 2882 | descriptorMapOut << "constant,descriptorSet," << descriptor_set |
| 2883 | << ",binding,0,kind,buffer,hexbytes,"; |
| 2884 | clspv::ConstantEmitter(DL, descriptorMapOut).Emit(GV.getInitializer()); |
| 2885 | descriptorMapOut << "\n"; |
| 2886 | |
| 2887 | // Find Insert Point for OpDecorate. |
| 2888 | auto DecoInsertPoint = |
| 2889 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2890 | [](SPIRVInstruction *Inst) -> bool { |
| 2891 | return Inst->getOpcode() != spv::OpDecorate && |
| 2892 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2893 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2894 | }); |
| 2895 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2896 | // OpDecorate %var Binding <binding> |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2897 | SPIRVOperandList DOps; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2898 | DOps << MkId(var_id) << MkNum(spv::DecorationBinding) << MkNum(0); |
| 2899 | DecoInsertPoint = SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2900 | DecoInsertPoint, new SPIRVInstruction(spv::OpDecorate, DOps)); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2901 | |
| 2902 | // OpDecorate %var DescriptorSet <descriptor_set> |
| 2903 | DOps.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2904 | DOps << MkId(var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2905 | << MkNum(descriptor_set); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2906 | SPIRVInstList.insert(DecoInsertPoint, |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2907 | new SPIRVInstruction(spv::OpDecorate, DOps)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2911 | void SPIRVProducerPass::GenerateWorkgroupVars() { |
| 2912 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 2913 | for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_; |
| 2914 | ++spec_id) { |
| 2915 | LocalArgInfo& info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2916 | |
| 2917 | // Generate OpVariable. |
| 2918 | // |
| 2919 | // GIDOps[0] : Result Type ID |
| 2920 | // GIDOps[1] : Storage Class |
| 2921 | SPIRVOperandList Ops; |
| 2922 | Ops << MkId(info.ptr_array_type_id) << MkNum(spv::StorageClassWorkgroup); |
| 2923 | |
| 2924 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2925 | new SPIRVInstruction(spv::OpVariable, info.variable_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2926 | } |
| 2927 | } |
| 2928 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2929 | void SPIRVProducerPass::GenerateDescriptorMapInfo(const DataLayout &DL, |
| 2930 | Function &F) { |
David Neto | c5fb524 | 2018-07-30 13:28:31 -0400 | [diff] [blame] | 2931 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 2932 | return; |
| 2933 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2934 | // Gather the list of resources that are used by this function's arguments. |
| 2935 | auto &resource_var_at_index = FunctionToResourceVarsMap[&F]; |
| 2936 | |
| 2937 | auto remap_arg_kind = [](StringRef argKind) { |
| 2938 | return clspv::Option::PodArgsInUniformBuffer() && argKind.equals("pod") |
| 2939 | ? "pod_ubo" |
| 2940 | : argKind; |
| 2941 | }; |
| 2942 | |
| 2943 | auto *fty = F.getType()->getPointerElementType(); |
| 2944 | auto *func_ty = dyn_cast<FunctionType>(fty); |
| 2945 | |
| 2946 | // If we've clustereed POD arguments, then argument details are in metadata. |
| 2947 | // If an argument maps to a resource variable, then get descriptor set and |
| 2948 | // binding from the resoure variable. Other info comes from the metadata. |
| 2949 | const auto *arg_map = F.getMetadata("kernel_arg_map"); |
| 2950 | if (arg_map) { |
| 2951 | for (const auto &arg : arg_map->operands()) { |
| 2952 | const MDNode *arg_node = dyn_cast<MDNode>(arg.get()); |
| 2953 | assert(arg_node->getNumOperands() == 6); |
| 2954 | const auto name = |
| 2955 | dyn_cast<MDString>(arg_node->getOperand(0))->getString(); |
| 2956 | const auto old_index = |
| 2957 | dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue(); |
| 2958 | // Remapped argument index |
| 2959 | const auto new_index = |
| 2960 | dyn_extract<ConstantInt>(arg_node->getOperand(2))->getZExtValue(); |
| 2961 | const auto offset = |
| 2962 | dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue(); |
| 2963 | const auto argKind = remap_arg_kind( |
| 2964 | dyn_cast<MDString>(arg_node->getOperand(4))->getString()); |
| 2965 | const auto spec_id = |
| 2966 | dyn_extract<ConstantInt>(arg_node->getOperand(5))->getSExtValue(); |
| 2967 | if (spec_id > 0) { |
| 2968 | // This was a pointer-to-local argument. It is not associated with a |
| 2969 | // resource variable. |
| 2970 | descriptorMapOut << "kernel," << F.getName() << ",arg," << name |
| 2971 | << ",argOrdinal," << old_index << ",argKind," |
| 2972 | << argKind << ",arrayElemSize," |
| 2973 | << DL.getTypeAllocSize( |
| 2974 | func_ty->getParamType(unsigned(new_index)) |
| 2975 | ->getPointerElementType()) |
| 2976 | << ",arrayNumElemSpecId," << spec_id << "\n"; |
| 2977 | } else { |
| 2978 | auto *info = resource_var_at_index[new_index]; |
| 2979 | assert(info); |
| 2980 | descriptorMapOut << "kernel," << F.getName() << ",arg," << name |
| 2981 | << ",argOrdinal," << old_index << ",descriptorSet," |
| 2982 | << info->descriptor_set << ",binding," << info->binding |
| 2983 | << ",offset," << offset << ",argKind," << argKind |
| 2984 | << "\n"; |
| 2985 | } |
| 2986 | } |
| 2987 | } else { |
| 2988 | // There is no argument map. |
| 2989 | // Take descriptor info from the resource variable calls. |
| 2990 | // Take argument name from the arguments list. |
| 2991 | |
| 2992 | SmallVector<Argument *, 4> arguments; |
| 2993 | for (auto &arg : F.args()) { |
| 2994 | arguments.push_back(&arg); |
| 2995 | } |
| 2996 | |
| 2997 | unsigned arg_index = 0; |
| 2998 | for (auto *info : resource_var_at_index) { |
| 2999 | if (info) { |
| 3000 | descriptorMapOut << "kernel," << F.getName() << ",arg," |
| 3001 | << arguments[arg_index]->getName() << ",argOrdinal," |
| 3002 | << arg_index << ",descriptorSet," |
| 3003 | << info->descriptor_set << ",binding," << info->binding |
| 3004 | << ",offset," << 0 << ",argKind," |
| 3005 | << remap_arg_kind( |
| 3006 | clspv::GetArgKindName(info->arg_kind)) |
| 3007 | << "\n"; |
| 3008 | } |
| 3009 | arg_index++; |
| 3010 | } |
| 3011 | // Generate mappings for pointer-to-local arguments. |
| 3012 | for (arg_index = 0; arg_index < arguments.size(); ++arg_index) { |
| 3013 | Argument *arg = arguments[arg_index]; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 3014 | auto where = LocalArgSpecIds.find(arg); |
| 3015 | if (where != LocalArgSpecIds.end()) { |
| 3016 | auto &local_arg_info = LocalSpecIdInfoMap[where->second]; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3017 | descriptorMapOut << "kernel," << F.getName() << ",arg," |
| 3018 | << arg->getName() << ",argOrdinal," << arg_index |
| 3019 | << ",argKind," |
| 3020 | << "local" |
| 3021 | << ",arrayElemSize," |
| 3022 | << DL.getTypeAllocSize(local_arg_info.elem_type) |
| 3023 | << ",arrayNumElemSpecId," << local_arg_info.spec_id |
| 3024 | << "\n"; |
| 3025 | } |
| 3026 | } |
| 3027 | } |
| 3028 | } |
| 3029 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3030 | void SPIRVProducerPass::GenerateFuncPrologue(Function &F) { |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 3031 | Module& M = *F.getParent(); |
| 3032 | const DataLayout &DL = M.getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3033 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3034 | ValueMapType &VMap = getValueMap(); |
| 3035 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3036 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 3037 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 3038 | |
| 3039 | FunctionType *FTy = F.getFunctionType(); |
| 3040 | |
| 3041 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3042 | // Generate OPFunction. |
| 3043 | // |
| 3044 | |
| 3045 | // FOps[0] : Result Type ID |
| 3046 | // FOps[1] : Function Control |
| 3047 | // FOps[2] : Function Type ID |
| 3048 | SPIRVOperandList FOps; |
| 3049 | |
| 3050 | // Find SPIRV instruction for return type. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3051 | FOps << MkId(lookupType(FTy->getReturnType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3052 | |
| 3053 | // Check function attributes for SPIRV Function Control. |
| 3054 | uint32_t FuncControl = spv::FunctionControlMaskNone; |
| 3055 | if (F.hasFnAttribute(Attribute::AlwaysInline)) { |
| 3056 | FuncControl |= spv::FunctionControlInlineMask; |
| 3057 | } |
| 3058 | if (F.hasFnAttribute(Attribute::NoInline)) { |
| 3059 | FuncControl |= spv::FunctionControlDontInlineMask; |
| 3060 | } |
| 3061 | // TODO: Check llvm attribute for Function Control Pure. |
| 3062 | if (F.hasFnAttribute(Attribute::ReadOnly)) { |
| 3063 | FuncControl |= spv::FunctionControlPureMask; |
| 3064 | } |
| 3065 | // TODO: Check llvm attribute for Function Control Const. |
| 3066 | if (F.hasFnAttribute(Attribute::ReadNone)) { |
| 3067 | FuncControl |= spv::FunctionControlConstMask; |
| 3068 | } |
| 3069 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3070 | FOps << MkNum(FuncControl); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3071 | |
| 3072 | uint32_t FTyID; |
| 3073 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 3074 | SmallVector<Type *, 4> NewFuncParamTys; |
| 3075 | FunctionType *NewFTy = |
| 3076 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 3077 | FTyID = lookupType(NewFTy); |
| 3078 | } else { |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 3079 | // Handle regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3080 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 3081 | FTyID = lookupType(GlobalConstFuncTyMap[FTy].first); |
| 3082 | } else { |
| 3083 | FTyID = lookupType(FTy); |
| 3084 | } |
| 3085 | } |
| 3086 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3087 | FOps << MkId(FTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3088 | |
| 3089 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 3090 | EntryPoints.push_back(std::make_pair(&F, nextID)); |
| 3091 | } |
| 3092 | |
| 3093 | VMap[&F] = nextID; |
| 3094 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 3095 | if (clspv::Option::ShowIDs()) { |
David Neto | b05675d | 2018-02-16 12:37:49 -0500 | [diff] [blame] | 3096 | errs() << "Function " << F.getName() << " is " << nextID << "\n"; |
| 3097 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3098 | // Generate SPIRV instruction for function. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3099 | auto *FuncInst = new SPIRVInstruction(spv::OpFunction, nextID++, FOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3100 | SPIRVInstList.push_back(FuncInst); |
| 3101 | |
| 3102 | // |
| 3103 | // Generate OpFunctionParameter for Normal function. |
| 3104 | // |
| 3105 | |
| 3106 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 3107 | // Iterate Argument for name instead of param type from function type. |
| 3108 | unsigned ArgIdx = 0; |
| 3109 | for (Argument &Arg : F.args()) { |
| 3110 | VMap[&Arg] = nextID; |
| 3111 | |
| 3112 | // ParamOps[0] : Result Type ID |
| 3113 | SPIRVOperandList ParamOps; |
| 3114 | |
| 3115 | // Find SPIRV instruction for parameter type. |
| 3116 | uint32_t ParamTyID = lookupType(Arg.getType()); |
| 3117 | if (PointerType *PTy = dyn_cast<PointerType>(Arg.getType())) { |
| 3118 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 3119 | if (ArgIdx == GlobalConstFuncTyMap[FTy].second) { |
| 3120 | Type *EleTy = PTy->getPointerElementType(); |
| 3121 | Type *ArgTy = |
| 3122 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 3123 | ParamTyID = lookupType(ArgTy); |
| 3124 | GlobalConstArgSet.insert(&Arg); |
| 3125 | } |
| 3126 | } |
| 3127 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3128 | ParamOps << MkId(ParamTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3129 | |
| 3130 | // Generate SPIRV instruction for parameter. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3131 | auto *ParamInst = |
| 3132 | new SPIRVInstruction(spv::OpFunctionParameter, nextID++, ParamOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3133 | SPIRVInstList.push_back(ParamInst); |
| 3134 | |
| 3135 | ArgIdx++; |
| 3136 | } |
| 3137 | } |
| 3138 | } |
| 3139 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3140 | void SPIRVProducerPass::GenerateModuleInfo(Module& module) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3141 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3142 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
| 3143 | ValueMapType &VMap = getValueMap(); |
| 3144 | ValueList &EntryPointInterfaces = getEntryPointInterfacesVec(); |
| 3145 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 3146 | std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec(); |
| 3147 | |
| 3148 | // Set up insert point. |
| 3149 | auto InsertPoint = SPIRVInstList.begin(); |
| 3150 | |
| 3151 | // |
| 3152 | // Generate OpCapability |
| 3153 | // |
| 3154 | // TODO: Which llvm information is mapped to SPIRV Capapbility? |
| 3155 | |
| 3156 | // Ops[0] = Capability |
| 3157 | SPIRVOperandList Ops; |
| 3158 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3159 | auto *CapInst = |
| 3160 | new SPIRVInstruction(spv::OpCapability, {MkNum(spv::CapabilityShader)}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3161 | SPIRVInstList.insert(InsertPoint, CapInst); |
| 3162 | |
| 3163 | for (Type *Ty : getTypeList()) { |
| 3164 | // Find the i16 type. |
| 3165 | if (Ty->isIntegerTy(16)) { |
| 3166 | // Generate OpCapability for i16 type. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3167 | SPIRVInstList.insert(InsertPoint, |
| 3168 | new SPIRVInstruction(spv::OpCapability, |
| 3169 | {MkNum(spv::CapabilityInt16)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3170 | } else if (Ty->isIntegerTy(64)) { |
| 3171 | // Generate OpCapability for i64 type. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3172 | SPIRVInstList.insert(InsertPoint, |
| 3173 | new SPIRVInstruction(spv::OpCapability, |
| 3174 | {MkNum(spv::CapabilityInt64)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3175 | } else if (Ty->isHalfTy()) { |
| 3176 | // Generate OpCapability for half type. |
| 3177 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3178 | InsertPoint, new SPIRVInstruction(spv::OpCapability, |
| 3179 | {MkNum(spv::CapabilityFloat16)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3180 | } else if (Ty->isDoubleTy()) { |
| 3181 | // Generate OpCapability for double type. |
| 3182 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3183 | InsertPoint, new SPIRVInstruction(spv::OpCapability, |
| 3184 | {MkNum(spv::CapabilityFloat64)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3185 | } else if (auto *STy = dyn_cast<StructType>(Ty)) { |
| 3186 | if (STy->isOpaque()) { |
David Neto | 565571c | 2017-08-21 12:00:05 -0400 | [diff] [blame] | 3187 | if (STy->getName().equals("opencl.image2d_wo_t") || |
| 3188 | STy->getName().equals("opencl.image3d_wo_t")) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3189 | // Generate OpCapability for write only image type. |
| 3190 | SPIRVInstList.insert( |
| 3191 | InsertPoint, |
| 3192 | new SPIRVInstruction( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3193 | spv::OpCapability, |
| 3194 | {MkNum(spv::CapabilityStorageImageWriteWithoutFormat)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | } |
| 3198 | } |
| 3199 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3200 | { // OpCapability ImageQuery |
| 3201 | bool hasImageQuery = false; |
| 3202 | for (const char *imageQuery : { |
| 3203 | "_Z15get_image_width14ocl_image2d_ro", |
| 3204 | "_Z15get_image_width14ocl_image2d_wo", |
| 3205 | "_Z16get_image_height14ocl_image2d_ro", |
| 3206 | "_Z16get_image_height14ocl_image2d_wo", |
| 3207 | }) { |
| 3208 | if (module.getFunction(imageQuery)) { |
| 3209 | hasImageQuery = true; |
| 3210 | break; |
| 3211 | } |
| 3212 | } |
| 3213 | if (hasImageQuery) { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3214 | auto *ImageQueryCapInst = new SPIRVInstruction( |
| 3215 | spv::OpCapability, {MkNum(spv::CapabilityImageQuery)}); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3216 | SPIRVInstList.insert(InsertPoint, ImageQueryCapInst); |
| 3217 | } |
| 3218 | } |
| 3219 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3220 | if (hasVariablePointers()) { |
| 3221 | // |
| 3222 | // Generate OpCapability and OpExtension |
| 3223 | // |
| 3224 | |
| 3225 | // |
| 3226 | // Generate OpCapability. |
| 3227 | // |
| 3228 | // Ops[0] = Capability |
| 3229 | // |
| 3230 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3231 | Ops << MkNum(spv::CapabilityVariablePointers); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3232 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3233 | SPIRVInstList.insert(InsertPoint, |
| 3234 | new SPIRVInstruction(spv::OpCapability, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3235 | |
| 3236 | // |
| 3237 | // Generate OpExtension. |
| 3238 | // |
| 3239 | // Ops[0] = Name (Literal String) |
| 3240 | // |
David Neto | a772fd1 | 2017-08-04 14:17:33 -0400 | [diff] [blame] | 3241 | for (auto extension : {"SPV_KHR_storage_buffer_storage_class", |
| 3242 | "SPV_KHR_variable_pointers"}) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3243 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3244 | auto *ExtensionInst = |
| 3245 | new SPIRVInstruction(spv::OpExtension, {MkString(extension)}); |
David Neto | a772fd1 | 2017-08-04 14:17:33 -0400 | [diff] [blame] | 3246 | SPIRVInstList.insert(InsertPoint, ExtensionInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3247 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | if (ExtInstImportID) { |
| 3251 | ++InsertPoint; |
| 3252 | } |
| 3253 | |
| 3254 | // |
| 3255 | // Generate OpMemoryModel |
| 3256 | // |
| 3257 | // Memory model for Vulkan will always be GLSL450. |
| 3258 | |
| 3259 | // Ops[0] = Addressing Model |
| 3260 | // Ops[1] = Memory Model |
| 3261 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3262 | Ops << MkNum(spv::AddressingModelLogical) << MkNum(spv::MemoryModelGLSL450); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3263 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3264 | auto *MemModelInst = new SPIRVInstruction(spv::OpMemoryModel, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3265 | SPIRVInstList.insert(InsertPoint, MemModelInst); |
| 3266 | |
| 3267 | // |
| 3268 | // Generate OpEntryPoint |
| 3269 | // |
| 3270 | for (auto EntryPoint : EntryPoints) { |
| 3271 | // Ops[0] = Execution Model |
| 3272 | // Ops[1] = EntryPoint ID |
| 3273 | // Ops[2] = Name (Literal String) |
| 3274 | // ... |
| 3275 | // |
| 3276 | // TODO: Do we need to consider Interface ID for forward references??? |
| 3277 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3278 | const StringRef& name = EntryPoint.first->getName(); |
| 3279 | Ops << MkNum(spv::ExecutionModelGLCompute) << MkId(EntryPoint.second) |
| 3280 | << MkString(name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3281 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3282 | for (Value *Interface : EntryPointInterfaces) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3283 | Ops << MkId(VMap[Interface]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3284 | } |
| 3285 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3286 | auto *EntryPointInst = new SPIRVInstruction(spv::OpEntryPoint, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3287 | SPIRVInstList.insert(InsertPoint, EntryPointInst); |
| 3288 | } |
| 3289 | |
| 3290 | for (auto EntryPoint : EntryPoints) { |
| 3291 | if (const MDNode *MD = dyn_cast<Function>(EntryPoint.first) |
| 3292 | ->getMetadata("reqd_work_group_size")) { |
| 3293 | |
| 3294 | if (!BuiltinDimVec.empty()) { |
| 3295 | llvm_unreachable( |
| 3296 | "Kernels should have consistent work group size definition"); |
| 3297 | } |
| 3298 | |
| 3299 | // |
| 3300 | // Generate OpExecutionMode |
| 3301 | // |
| 3302 | |
| 3303 | // Ops[0] = Entry Point ID |
| 3304 | // Ops[1] = Execution Mode |
| 3305 | // Ops[2] ... Ops[n] = Optional literals according to Execution Mode |
| 3306 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3307 | Ops << MkId(EntryPoint.second) |
| 3308 | << MkNum(spv::ExecutionModeLocalSize); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3309 | |
| 3310 | uint32_t XDim = static_cast<uint32_t>( |
| 3311 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 3312 | uint32_t YDim = static_cast<uint32_t>( |
| 3313 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 3314 | uint32_t ZDim = static_cast<uint32_t>( |
| 3315 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 3316 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3317 | Ops << MkNum(XDim) << MkNum(YDim) << MkNum(ZDim); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3318 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3319 | auto *ExecModeInst = new SPIRVInstruction(spv::OpExecutionMode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3320 | SPIRVInstList.insert(InsertPoint, ExecModeInst); |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | // |
| 3325 | // Generate OpSource. |
| 3326 | // |
| 3327 | // Ops[0] = SourceLanguage ID |
| 3328 | // Ops[1] = Version (LiteralNum) |
| 3329 | // |
| 3330 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3331 | Ops << MkNum(spv::SourceLanguageOpenCL_C) << MkNum(120); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3332 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3333 | auto *OpenSourceInst = new SPIRVInstruction(spv::OpSource, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3334 | SPIRVInstList.insert(InsertPoint, OpenSourceInst); |
| 3335 | |
| 3336 | if (!BuiltinDimVec.empty()) { |
| 3337 | // |
| 3338 | // Generate OpDecorates for x/y/z dimension. |
| 3339 | // |
| 3340 | // Ops[0] = Target ID |
| 3341 | // Ops[1] = Decoration (SpecId) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3342 | // Ops[2] = Specialization Constant ID (Literal Number) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3343 | |
| 3344 | // X Dimension |
| 3345 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3346 | Ops << MkId(BuiltinDimVec[0]) << MkNum(spv::DecorationSpecId) << MkNum(0); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3347 | SPIRVInstList.insert(InsertPoint, |
| 3348 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3349 | |
| 3350 | // Y Dimension |
| 3351 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3352 | Ops << MkId(BuiltinDimVec[1]) << MkNum(spv::DecorationSpecId) << MkNum(1); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3353 | SPIRVInstList.insert(InsertPoint, |
| 3354 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3355 | |
| 3356 | // Z Dimension |
| 3357 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3358 | Ops << MkId(BuiltinDimVec[2]) << MkNum(spv::DecorationSpecId) << MkNum(2); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3359 | SPIRVInstList.insert(InsertPoint, |
| 3360 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3364 | void SPIRVProducerPass::GenerateEntryPointInitialStores() { |
| 3365 | // Work around a driver bug. Initializers on Private variables might not |
| 3366 | // work. So the start of the kernel should store the initializer value to the |
| 3367 | // variables. Yes, *every* entry point pays this cost if *any* entry point |
| 3368 | // uses this builtin. At this point I judge this to be an acceptable tradeoff |
| 3369 | // of complexity vs. runtime, for a broken driver. |
| 3370 | // TODO(dneto): Remove this at some point once fixed drivers are widely available. |
| 3371 | if (WorkgroupSizeVarID) { |
| 3372 | assert(WorkgroupSizeValueID); |
| 3373 | |
| 3374 | SPIRVOperandList Ops; |
| 3375 | Ops << MkId(WorkgroupSizeVarID) << MkId(WorkgroupSizeValueID); |
| 3376 | |
| 3377 | auto *Inst = new SPIRVInstruction(spv::OpStore, Ops); |
| 3378 | getSPIRVInstList().push_back(Inst); |
| 3379 | } |
| 3380 | } |
| 3381 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3382 | void SPIRVProducerPass::GenerateFuncBody(Function &F) { |
| 3383 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3384 | ValueMapType &VMap = getValueMap(); |
| 3385 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3386 | const bool IsKernel = F.getCallingConv() == CallingConv::SPIR_KERNEL; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3387 | |
| 3388 | for (BasicBlock &BB : F) { |
| 3389 | // Register BasicBlock to ValueMap. |
| 3390 | VMap[&BB] = nextID; |
| 3391 | |
| 3392 | // |
| 3393 | // Generate OpLabel for Basic Block. |
| 3394 | // |
| 3395 | SPIRVOperandList Ops; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3396 | auto *Inst = new SPIRVInstruction(spv::OpLabel, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3397 | SPIRVInstList.push_back(Inst); |
| 3398 | |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3399 | // OpVariable instructions must come first. |
| 3400 | for (Instruction &I : BB) { |
| 3401 | if (isa<AllocaInst>(I)) { |
| 3402 | GenerateInstruction(I); |
| 3403 | } |
| 3404 | } |
| 3405 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3406 | if (&BB == &F.getEntryBlock() && IsKernel) { |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3407 | if (clspv::Option::HackInitializers()) { |
| 3408 | GenerateEntryPointInitialStores(); |
| 3409 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | for (Instruction &I : BB) { |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3413 | if (!isa<AllocaInst>(I)) { |
| 3414 | GenerateInstruction(I); |
| 3415 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | spv::Op SPIRVProducerPass::GetSPIRVCmpOpcode(CmpInst *I) { |
| 3421 | const std::map<CmpInst::Predicate, spv::Op> Map = { |
| 3422 | {CmpInst::ICMP_EQ, spv::OpIEqual}, |
| 3423 | {CmpInst::ICMP_NE, spv::OpINotEqual}, |
| 3424 | {CmpInst::ICMP_UGT, spv::OpUGreaterThan}, |
| 3425 | {CmpInst::ICMP_UGE, spv::OpUGreaterThanEqual}, |
| 3426 | {CmpInst::ICMP_ULT, spv::OpULessThan}, |
| 3427 | {CmpInst::ICMP_ULE, spv::OpULessThanEqual}, |
| 3428 | {CmpInst::ICMP_SGT, spv::OpSGreaterThan}, |
| 3429 | {CmpInst::ICMP_SGE, spv::OpSGreaterThanEqual}, |
| 3430 | {CmpInst::ICMP_SLT, spv::OpSLessThan}, |
| 3431 | {CmpInst::ICMP_SLE, spv::OpSLessThanEqual}, |
| 3432 | {CmpInst::FCMP_OEQ, spv::OpFOrdEqual}, |
| 3433 | {CmpInst::FCMP_OGT, spv::OpFOrdGreaterThan}, |
| 3434 | {CmpInst::FCMP_OGE, spv::OpFOrdGreaterThanEqual}, |
| 3435 | {CmpInst::FCMP_OLT, spv::OpFOrdLessThan}, |
| 3436 | {CmpInst::FCMP_OLE, spv::OpFOrdLessThanEqual}, |
| 3437 | {CmpInst::FCMP_ONE, spv::OpFOrdNotEqual}, |
| 3438 | {CmpInst::FCMP_UEQ, spv::OpFUnordEqual}, |
| 3439 | {CmpInst::FCMP_UGT, spv::OpFUnordGreaterThan}, |
| 3440 | {CmpInst::FCMP_UGE, spv::OpFUnordGreaterThanEqual}, |
| 3441 | {CmpInst::FCMP_ULT, spv::OpFUnordLessThan}, |
| 3442 | {CmpInst::FCMP_ULE, spv::OpFUnordLessThanEqual}, |
| 3443 | {CmpInst::FCMP_UNE, spv::OpFUnordNotEqual}}; |
| 3444 | |
| 3445 | assert(0 != Map.count(I->getPredicate())); |
| 3446 | |
| 3447 | return Map.at(I->getPredicate()); |
| 3448 | } |
| 3449 | |
| 3450 | spv::Op SPIRVProducerPass::GetSPIRVCastOpcode(Instruction &I) { |
| 3451 | const std::map<unsigned, spv::Op> Map{ |
| 3452 | {Instruction::Trunc, spv::OpUConvert}, |
| 3453 | {Instruction::ZExt, spv::OpUConvert}, |
| 3454 | {Instruction::SExt, spv::OpSConvert}, |
| 3455 | {Instruction::FPToUI, spv::OpConvertFToU}, |
| 3456 | {Instruction::FPToSI, spv::OpConvertFToS}, |
| 3457 | {Instruction::UIToFP, spv::OpConvertUToF}, |
| 3458 | {Instruction::SIToFP, spv::OpConvertSToF}, |
| 3459 | {Instruction::FPTrunc, spv::OpFConvert}, |
| 3460 | {Instruction::FPExt, spv::OpFConvert}, |
| 3461 | {Instruction::BitCast, spv::OpBitcast}}; |
| 3462 | |
| 3463 | assert(0 != Map.count(I.getOpcode())); |
| 3464 | |
| 3465 | return Map.at(I.getOpcode()); |
| 3466 | } |
| 3467 | |
| 3468 | spv::Op SPIRVProducerPass::GetSPIRVBinaryOpcode(Instruction &I) { |
| 3469 | if (I.getType()->isIntegerTy(1)) { |
| 3470 | switch (I.getOpcode()) { |
| 3471 | default: |
| 3472 | break; |
| 3473 | case Instruction::Or: |
| 3474 | return spv::OpLogicalOr; |
| 3475 | case Instruction::And: |
| 3476 | return spv::OpLogicalAnd; |
| 3477 | case Instruction::Xor: |
| 3478 | return spv::OpLogicalNotEqual; |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | const std::map<unsigned, spv::Op> Map { |
| 3483 | {Instruction::Add, spv::OpIAdd}, |
| 3484 | {Instruction::FAdd, spv::OpFAdd}, |
| 3485 | {Instruction::Sub, spv::OpISub}, |
| 3486 | {Instruction::FSub, spv::OpFSub}, |
| 3487 | {Instruction::Mul, spv::OpIMul}, |
| 3488 | {Instruction::FMul, spv::OpFMul}, |
| 3489 | {Instruction::UDiv, spv::OpUDiv}, |
| 3490 | {Instruction::SDiv, spv::OpSDiv}, |
| 3491 | {Instruction::FDiv, spv::OpFDiv}, |
| 3492 | {Instruction::URem, spv::OpUMod}, |
| 3493 | {Instruction::SRem, spv::OpSRem}, |
| 3494 | {Instruction::FRem, spv::OpFRem}, |
| 3495 | {Instruction::Or, spv::OpBitwiseOr}, |
| 3496 | {Instruction::Xor, spv::OpBitwiseXor}, |
| 3497 | {Instruction::And, spv::OpBitwiseAnd}, |
| 3498 | {Instruction::Shl, spv::OpShiftLeftLogical}, |
| 3499 | {Instruction::LShr, spv::OpShiftRightLogical}, |
| 3500 | {Instruction::AShr, spv::OpShiftRightArithmetic}}; |
| 3501 | |
| 3502 | assert(0 != Map.count(I.getOpcode())); |
| 3503 | |
| 3504 | return Map.at(I.getOpcode()); |
| 3505 | } |
| 3506 | |
| 3507 | void SPIRVProducerPass::GenerateInstruction(Instruction &I) { |
| 3508 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3509 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3510 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 3511 | LLVMContext &Context = I.getParent()->getParent()->getParent()->getContext(); |
| 3512 | |
| 3513 | // Register Instruction to ValueMap. |
| 3514 | if (0 == VMap[&I]) { |
| 3515 | VMap[&I] = nextID; |
| 3516 | } |
| 3517 | |
| 3518 | switch (I.getOpcode()) { |
| 3519 | default: { |
| 3520 | if (Instruction::isCast(I.getOpcode())) { |
| 3521 | // |
| 3522 | // Generate SPIRV instructions for cast operators. |
| 3523 | // |
| 3524 | |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3525 | |
| 3526 | auto Ty = I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3527 | auto OpTy = I.getOperand(0)->getType(); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3528 | auto toI8 = Ty == Type::getInt8Ty(Context); |
| 3529 | auto fromI32 = OpTy == Type::getInt32Ty(Context); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3530 | // Handle zext, sext and uitofp with i1 type specially. |
| 3531 | if ((I.getOpcode() == Instruction::ZExt || |
| 3532 | I.getOpcode() == Instruction::SExt || |
| 3533 | I.getOpcode() == Instruction::UIToFP) && |
| 3534 | (OpTy->isIntegerTy(1) || |
| 3535 | (OpTy->isVectorTy() && |
| 3536 | OpTy->getVectorElementType()->isIntegerTy(1)))) { |
| 3537 | // |
| 3538 | // Generate OpSelect. |
| 3539 | // |
| 3540 | |
| 3541 | // Ops[0] = Result Type ID |
| 3542 | // Ops[1] = Condition ID |
| 3543 | // Ops[2] = True Constant ID |
| 3544 | // Ops[3] = False Constant ID |
| 3545 | SPIRVOperandList Ops; |
| 3546 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3547 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3548 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3549 | uint32_t CondID = VMap[I.getOperand(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3550 | Ops << MkId(CondID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3551 | |
| 3552 | uint32_t TrueID = 0; |
| 3553 | if (I.getOpcode() == Instruction::ZExt) { |
| 3554 | APInt One(32, 1); |
| 3555 | TrueID = VMap[Constant::getIntegerValue(I.getType(), One)]; |
| 3556 | } else if (I.getOpcode() == Instruction::SExt) { |
| 3557 | APInt MinusOne(32, UINT64_MAX, true); |
| 3558 | TrueID = VMap[Constant::getIntegerValue(I.getType(), MinusOne)]; |
| 3559 | } else { |
| 3560 | TrueID = VMap[ConstantFP::get(Context, APFloat(1.0f))]; |
| 3561 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3562 | Ops << MkId(TrueID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3563 | |
| 3564 | uint32_t FalseID = 0; |
| 3565 | if (I.getOpcode() == Instruction::ZExt) { |
| 3566 | FalseID = VMap[Constant::getNullValue(I.getType())]; |
| 3567 | } else if (I.getOpcode() == Instruction::SExt) { |
| 3568 | FalseID = VMap[Constant::getNullValue(I.getType())]; |
| 3569 | } else { |
| 3570 | FalseID = VMap[ConstantFP::get(Context, APFloat(0.0f))]; |
| 3571 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3572 | Ops << MkId(FalseID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3573 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3574 | auto *Inst = new SPIRVInstruction(spv::OpSelect, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3575 | SPIRVInstList.push_back(Inst); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3576 | } else if (I.getOpcode() == Instruction::Trunc && fromI32 && toI8) { |
| 3577 | // The SPIR-V target type is a 32-bit int. Keep only the bottom |
| 3578 | // 8 bits. |
| 3579 | // Before: |
| 3580 | // %result = trunc i32 %a to i8 |
| 3581 | // After |
| 3582 | // %result = OpBitwiseAnd %uint %a %uint_255 |
| 3583 | |
| 3584 | SPIRVOperandList Ops; |
| 3585 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3586 | Ops << MkId(lookupType(OpTy)) << MkId(VMap[I.getOperand(0)]); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3587 | |
| 3588 | Type *UintTy = Type::getInt32Ty(Context); |
| 3589 | uint32_t MaskID = VMap[ConstantInt::get(UintTy, 255)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3590 | Ops << MkId(MaskID); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3591 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3592 | auto *Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3593 | SPIRVInstList.push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3594 | } else { |
| 3595 | // Ops[0] = Result Type ID |
| 3596 | // Ops[1] = Source Value ID |
| 3597 | SPIRVOperandList Ops; |
| 3598 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3599 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3600 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3601 | auto *Inst = new SPIRVInstruction(GetSPIRVCastOpcode(I), nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3602 | SPIRVInstList.push_back(Inst); |
| 3603 | } |
| 3604 | } else if (isa<BinaryOperator>(I)) { |
| 3605 | // |
| 3606 | // Generate SPIRV instructions for binary operators. |
| 3607 | // |
| 3608 | |
| 3609 | // Handle xor with i1 type specially. |
| 3610 | if (I.getOpcode() == Instruction::Xor && |
| 3611 | I.getType() == Type::getInt1Ty(Context) && |
| 3612 | (isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1)))) { |
| 3613 | // |
| 3614 | // Generate OpLogicalNot. |
| 3615 | // |
| 3616 | // Ops[0] = Result Type ID |
| 3617 | // Ops[1] = Operand |
| 3618 | SPIRVOperandList Ops; |
| 3619 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3620 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3621 | |
| 3622 | Value *CondV = I.getOperand(0); |
| 3623 | if (isa<Constant>(I.getOperand(0))) { |
| 3624 | CondV = I.getOperand(1); |
| 3625 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3626 | Ops << MkId(VMap[CondV]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3627 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3628 | auto *Inst = new SPIRVInstruction(spv::OpLogicalNot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3629 | SPIRVInstList.push_back(Inst); |
| 3630 | } else { |
| 3631 | // Ops[0] = Result Type ID |
| 3632 | // Ops[1] = Operand 0 |
| 3633 | // Ops[2] = Operand 1 |
| 3634 | SPIRVOperandList Ops; |
| 3635 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3636 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]) |
| 3637 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3638 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3639 | auto *Inst = |
| 3640 | new SPIRVInstruction(GetSPIRVBinaryOpcode(I), nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3641 | SPIRVInstList.push_back(Inst); |
| 3642 | } |
| 3643 | } else { |
| 3644 | I.print(errs()); |
| 3645 | llvm_unreachable("Unsupported instruction???"); |
| 3646 | } |
| 3647 | break; |
| 3648 | } |
| 3649 | case Instruction::GetElementPtr: { |
| 3650 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 3651 | |
| 3652 | // |
| 3653 | // Generate OpAccessChain. |
| 3654 | // |
| 3655 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(&I); |
| 3656 | |
| 3657 | // |
| 3658 | // Generate OpAccessChain. |
| 3659 | // |
| 3660 | |
| 3661 | // Ops[0] = Result Type ID |
| 3662 | // Ops[1] = Base ID |
| 3663 | // Ops[2] ... Ops[n] = Indexes ID |
| 3664 | SPIRVOperandList Ops; |
| 3665 | |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3666 | PointerType* ResultType = cast<PointerType>(GEP->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3667 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate || |
| 3668 | GlobalConstArgSet.count(GEP->getPointerOperand())) { |
| 3669 | // Use pointer type with private address space for global constant. |
| 3670 | Type *EleTy = I.getType()->getPointerElementType(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3671 | ResultType = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3672 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3673 | |
| 3674 | Ops << MkId(lookupType(ResultType)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3675 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3676 | // Generate the base pointer. |
| 3677 | Ops << MkId(VMap[GEP->getPointerOperand()]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3678 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3679 | // TODO(dneto): Simplify the following? |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3680 | |
| 3681 | // |
| 3682 | // Follows below rules for gep. |
| 3683 | // |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3684 | // 1. If gep's first index is 0 generate OpAccessChain and ignore gep's |
| 3685 | // first index. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3686 | // 2. If gep's first index is not 0, generate OpPtrAccessChain and use gep's |
| 3687 | // first index. |
| 3688 | // 3. If gep's first index is not constant, generate OpPtrAccessChain and |
| 3689 | // use gep's first index. |
| 3690 | // 4. If it is not above case 1, 2 and 3, generate OpAccessChain and use |
| 3691 | // gep's first index. |
| 3692 | // |
| 3693 | spv::Op Opcode = spv::OpAccessChain; |
| 3694 | unsigned offset = 0; |
| 3695 | if (ConstantInt *CstInt = dyn_cast<ConstantInt>(GEP->getOperand(1))) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3696 | if (CstInt->getZExtValue() == 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3697 | offset = 1; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3698 | } else if (CstInt->getZExtValue() != 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3699 | Opcode = spv::OpPtrAccessChain; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3700 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3701 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3702 | Opcode = spv::OpPtrAccessChain; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | if (Opcode == spv::OpPtrAccessChain) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3706 | setVariablePointers(true); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3707 | // Do we need to generate ArrayStride? Check against the GEP result type |
| 3708 | // rather than the pointer type of the base because when indexing into |
| 3709 | // an OpenCL program-scope constant, we'll swap out the LLVM base pointer |
| 3710 | // for something else in the SPIR-V. |
| 3711 | // E.g. see test/PointerAccessChain/pointer_index_is_constant_1.cl |
| 3712 | if (GetStorageClass(ResultType->getAddressSpace()) == |
| 3713 | spv::StorageClassStorageBuffer) { |
| 3714 | // Save the need to generate an ArrayStride decoration. But defer |
| 3715 | // generation until later, so we only make one decoration. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3716 | getTypesNeedingArrayStride().insert(ResultType); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3717 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | for (auto II = GEP->idx_begin() + offset; II != GEP->idx_end(); II++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3721 | Ops << MkId(VMap[*II]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3722 | } |
| 3723 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3724 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3725 | SPIRVInstList.push_back(Inst); |
| 3726 | break; |
| 3727 | } |
| 3728 | case Instruction::ExtractValue: { |
| 3729 | ExtractValueInst *EVI = cast<ExtractValueInst>(&I); |
| 3730 | // Ops[0] = Result Type ID |
| 3731 | // Ops[1] = Composite ID |
| 3732 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
| 3733 | SPIRVOperandList Ops; |
| 3734 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3735 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3736 | |
| 3737 | uint32_t CompositeID = VMap[EVI->getAggregateOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3738 | Ops << MkId(CompositeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3739 | |
| 3740 | for (auto &Index : EVI->indices()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3741 | Ops << MkNum(Index); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3742 | } |
| 3743 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3744 | auto *Inst = new SPIRVInstruction(spv::OpCompositeExtract, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3745 | SPIRVInstList.push_back(Inst); |
| 3746 | break; |
| 3747 | } |
| 3748 | case Instruction::InsertValue: { |
| 3749 | InsertValueInst *IVI = cast<InsertValueInst>(&I); |
| 3750 | // Ops[0] = Result Type ID |
| 3751 | // Ops[1] = Object ID |
| 3752 | // Ops[2] = Composite ID |
| 3753 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
| 3754 | SPIRVOperandList Ops; |
| 3755 | |
| 3756 | uint32_t ResTyID = lookupType(I.getType()); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3757 | Ops << MkId(ResTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3758 | |
| 3759 | uint32_t ObjectID = VMap[IVI->getInsertedValueOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3760 | Ops << MkId(ObjectID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3761 | |
| 3762 | uint32_t CompositeID = VMap[IVI->getAggregateOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3763 | Ops << MkId(CompositeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3764 | |
| 3765 | for (auto &Index : IVI->indices()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3766 | Ops << MkNum(Index); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3767 | } |
| 3768 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3769 | auto *Inst = new SPIRVInstruction(spv::OpCompositeInsert, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3770 | SPIRVInstList.push_back(Inst); |
| 3771 | break; |
| 3772 | } |
| 3773 | case Instruction::Select: { |
| 3774 | // |
| 3775 | // Generate OpSelect. |
| 3776 | // |
| 3777 | |
| 3778 | // Ops[0] = Result Type ID |
| 3779 | // Ops[1] = Condition ID |
| 3780 | // Ops[2] = True Constant ID |
| 3781 | // Ops[3] = False Constant ID |
| 3782 | SPIRVOperandList Ops; |
| 3783 | |
| 3784 | // Find SPIRV instruction for parameter type. |
| 3785 | auto Ty = I.getType(); |
| 3786 | if (Ty->isPointerTy()) { |
| 3787 | auto PointeeTy = Ty->getPointerElementType(); |
| 3788 | if (PointeeTy->isStructTy() && |
| 3789 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 3790 | Ty = PointeeTy; |
| 3791 | } |
| 3792 | } |
| 3793 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3794 | Ops << MkId(lookupType(Ty)) << MkId(VMap[I.getOperand(0)]) |
| 3795 | << MkId(VMap[I.getOperand(1)]) << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3796 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3797 | auto *Inst = new SPIRVInstruction(spv::OpSelect, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3798 | SPIRVInstList.push_back(Inst); |
| 3799 | break; |
| 3800 | } |
| 3801 | case Instruction::ExtractElement: { |
| 3802 | // Handle <4 x i8> type manually. |
| 3803 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 3804 | if (is4xi8vec(CompositeTy)) { |
| 3805 | // |
| 3806 | // Generate OpShiftRightLogical and OpBitwiseAnd for extractelement with |
| 3807 | // <4 x i8>. |
| 3808 | // |
| 3809 | |
| 3810 | // |
| 3811 | // Generate OpShiftRightLogical |
| 3812 | // |
| 3813 | // Ops[0] = Result Type ID |
| 3814 | // Ops[1] = Operand 0 |
| 3815 | // Ops[2] = Operand 1 |
| 3816 | // |
| 3817 | SPIRVOperandList Ops; |
| 3818 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3819 | Ops << MkId(lookupType(CompositeTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3820 | |
| 3821 | uint32_t Op0ID = VMap[I.getOperand(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3822 | Ops << MkId(Op0ID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3823 | |
| 3824 | uint32_t Op1ID = 0; |
| 3825 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 3826 | // Handle constant index. |
| 3827 | uint64_t Idx = CI->getZExtValue(); |
| 3828 | Value *ShiftAmount = |
| 3829 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 3830 | Op1ID = VMap[ShiftAmount]; |
| 3831 | } else { |
| 3832 | // Handle variable index. |
| 3833 | SPIRVOperandList TmpOps; |
| 3834 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3835 | TmpOps << MkId(lookupType(Type::getInt32Ty(Context))) |
| 3836 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3837 | |
| 3838 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3839 | TmpOps << MkId(VMap[Cst8]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3840 | |
| 3841 | Op1ID = nextID; |
| 3842 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3843 | auto *TmpInst = new SPIRVInstruction(spv::OpIMul, nextID++, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3844 | SPIRVInstList.push_back(TmpInst); |
| 3845 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3846 | Ops << MkId(Op1ID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3847 | |
| 3848 | uint32_t ShiftID = nextID; |
| 3849 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3850 | auto *Inst = |
| 3851 | new SPIRVInstruction(spv::OpShiftRightLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3852 | SPIRVInstList.push_back(Inst); |
| 3853 | |
| 3854 | // |
| 3855 | // Generate OpBitwiseAnd |
| 3856 | // |
| 3857 | // Ops[0] = Result Type ID |
| 3858 | // Ops[1] = Operand 0 |
| 3859 | // Ops[2] = Operand 1 |
| 3860 | // |
| 3861 | Ops.clear(); |
| 3862 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3863 | Ops << MkId(lookupType(CompositeTy)) << MkId(ShiftID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3864 | |
| 3865 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3866 | Ops << MkId(VMap[CstFF]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3867 | |
David Neto | 9b2d625 | 2017-09-06 15:47:37 -0400 | [diff] [blame] | 3868 | // Reset mapping for this value to the result of the bitwise and. |
| 3869 | VMap[&I] = nextID; |
| 3870 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3871 | Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3872 | SPIRVInstList.push_back(Inst); |
| 3873 | break; |
| 3874 | } |
| 3875 | |
| 3876 | // Ops[0] = Result Type ID |
| 3877 | // Ops[1] = Composite ID |
| 3878 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
| 3879 | SPIRVOperandList Ops; |
| 3880 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3881 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3882 | |
| 3883 | spv::Op Opcode = spv::OpCompositeExtract; |
| 3884 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3885 | Ops << MkNum(static_cast<uint32_t>(CI->getZExtValue())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3886 | } else { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3887 | Ops << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3888 | Opcode = spv::OpVectorExtractDynamic; |
| 3889 | } |
| 3890 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3891 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3892 | SPIRVInstList.push_back(Inst); |
| 3893 | break; |
| 3894 | } |
| 3895 | case Instruction::InsertElement: { |
| 3896 | // Handle <4 x i8> type manually. |
| 3897 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 3898 | if (is4xi8vec(CompositeTy)) { |
| 3899 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 3900 | uint32_t CstFFID = VMap[CstFF]; |
| 3901 | |
| 3902 | uint32_t ShiftAmountID = 0; |
| 3903 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 3904 | // Handle constant index. |
| 3905 | uint64_t Idx = CI->getZExtValue(); |
| 3906 | Value *ShiftAmount = |
| 3907 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 3908 | ShiftAmountID = VMap[ShiftAmount]; |
| 3909 | } else { |
| 3910 | // Handle variable index. |
| 3911 | SPIRVOperandList TmpOps; |
| 3912 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3913 | TmpOps << MkId(lookupType(Type::getInt32Ty(Context))) |
| 3914 | << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3915 | |
| 3916 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3917 | TmpOps << MkId(VMap[Cst8]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3918 | |
| 3919 | ShiftAmountID = nextID; |
| 3920 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3921 | auto *TmpInst = new SPIRVInstruction(spv::OpIMul, nextID++, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3922 | SPIRVInstList.push_back(TmpInst); |
| 3923 | } |
| 3924 | |
| 3925 | // |
| 3926 | // Generate mask operations. |
| 3927 | // |
| 3928 | |
| 3929 | // ShiftLeft mask according to index of insertelement. |
| 3930 | SPIRVOperandList Ops; |
| 3931 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3932 | const uint32_t ResTyID = lookupType(CompositeTy); |
| 3933 | Ops << MkId(ResTyID) << MkId(CstFFID) << MkId(ShiftAmountID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3934 | |
| 3935 | uint32_t MaskID = nextID; |
| 3936 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3937 | auto *Inst = new SPIRVInstruction(spv::OpShiftLeftLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3938 | SPIRVInstList.push_back(Inst); |
| 3939 | |
| 3940 | // Inverse mask. |
| 3941 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3942 | Ops << MkId(ResTyID) << MkId(MaskID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3943 | |
| 3944 | uint32_t InvMaskID = nextID; |
| 3945 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3946 | Inst = new SPIRVInstruction(spv::OpNot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3947 | SPIRVInstList.push_back(Inst); |
| 3948 | |
| 3949 | // Apply mask. |
| 3950 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3951 | Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(0)]) << MkId(InvMaskID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3952 | |
| 3953 | uint32_t OrgValID = nextID; |
| 3954 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3955 | Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3956 | SPIRVInstList.push_back(Inst); |
| 3957 | |
| 3958 | // Create correct value according to index of insertelement. |
| 3959 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3960 | Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(1)]) << MkId(ShiftAmountID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3961 | |
| 3962 | uint32_t InsertValID = nextID; |
| 3963 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3964 | Inst = new SPIRVInstruction(spv::OpShiftLeftLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3965 | SPIRVInstList.push_back(Inst); |
| 3966 | |
| 3967 | // Insert value to original value. |
| 3968 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3969 | Ops << MkId(ResTyID) << MkId(OrgValID) << MkId(InsertValID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3970 | |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 3971 | VMap[&I] = nextID; |
| 3972 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3973 | Inst = new SPIRVInstruction(spv::OpBitwiseOr, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3974 | SPIRVInstList.push_back(Inst); |
| 3975 | |
| 3976 | break; |
| 3977 | } |
| 3978 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3979 | SPIRVOperandList Ops; |
| 3980 | |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 3981 | // Ops[0] = Result Type ID |
| 3982 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3983 | |
| 3984 | spv::Op Opcode = spv::OpCompositeInsert; |
| 3985 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3986 | const auto value = CI->getZExtValue(); |
| 3987 | assert(value <= UINT32_MAX); |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 3988 | // Ops[1] = Object ID |
| 3989 | // Ops[2] = Composite ID |
| 3990 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
| 3991 | Ops << MkId(VMap[I.getOperand(1)]) |
| 3992 | << MkId(VMap[I.getOperand(0)]) |
| 3993 | << MkNum(static_cast<uint32_t>(value)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3994 | } else { |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 3995 | // Ops[1] = Composite ID |
| 3996 | // Ops[2] = Object ID |
| 3997 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
| 3998 | Ops << MkId(VMap[I.getOperand(0)]) |
| 3999 | << MkId(VMap[I.getOperand(1)]) |
| 4000 | << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4001 | Opcode = spv::OpVectorInsertDynamic; |
| 4002 | } |
| 4003 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4004 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4005 | SPIRVInstList.push_back(Inst); |
| 4006 | break; |
| 4007 | } |
| 4008 | case Instruction::ShuffleVector: { |
| 4009 | // Ops[0] = Result Type ID |
| 4010 | // Ops[1] = Vector 1 ID |
| 4011 | // Ops[2] = Vector 2 ID |
| 4012 | // Ops[3] ... Ops[n] = Components (Literal Number) |
| 4013 | SPIRVOperandList Ops; |
| 4014 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4015 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]) |
| 4016 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4017 | |
| 4018 | uint64_t NumElements = 0; |
| 4019 | if (Constant *Cst = dyn_cast<Constant>(I.getOperand(2))) { |
| 4020 | NumElements = cast<VectorType>(Cst->getType())->getNumElements(); |
| 4021 | |
| 4022 | if (Cst->isNullValue()) { |
| 4023 | for (unsigned i = 0; i < NumElements; i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4024 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4025 | } |
| 4026 | } else if (const ConstantDataSequential *CDS = |
| 4027 | dyn_cast<ConstantDataSequential>(Cst)) { |
| 4028 | for (unsigned i = 0; i < CDS->getNumElements(); i++) { |
| 4029 | std::vector<uint32_t> LiteralNum; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4030 | const auto value = CDS->getElementAsInteger(i); |
| 4031 | assert(value <= UINT32_MAX); |
| 4032 | Ops << MkNum(static_cast<uint32_t>(value)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4033 | } |
| 4034 | } else if (const ConstantVector *CV = dyn_cast<ConstantVector>(Cst)) { |
| 4035 | for (unsigned i = 0; i < CV->getNumOperands(); i++) { |
| 4036 | auto Op = CV->getOperand(i); |
| 4037 | |
| 4038 | uint32_t literal = 0; |
| 4039 | |
| 4040 | if (auto CI = dyn_cast<ConstantInt>(Op)) { |
| 4041 | literal = static_cast<uint32_t>(CI->getZExtValue()); |
| 4042 | } else if (auto UI = dyn_cast<UndefValue>(Op)) { |
| 4043 | literal = 0xFFFFFFFFu; |
| 4044 | } else { |
| 4045 | Op->print(errs()); |
| 4046 | llvm_unreachable("Unsupported element in ConstantVector!"); |
| 4047 | } |
| 4048 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4049 | Ops << MkNum(literal); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4050 | } |
| 4051 | } else { |
| 4052 | Cst->print(errs()); |
| 4053 | llvm_unreachable("Unsupported constant mask in ShuffleVector!"); |
| 4054 | } |
| 4055 | } |
| 4056 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4057 | auto *Inst = new SPIRVInstruction(spv::OpVectorShuffle, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4058 | SPIRVInstList.push_back(Inst); |
| 4059 | break; |
| 4060 | } |
| 4061 | case Instruction::ICmp: |
| 4062 | case Instruction::FCmp: { |
| 4063 | CmpInst *CmpI = cast<CmpInst>(&I); |
| 4064 | |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4065 | // Pointer equality is invalid. |
| 4066 | Type* ArgTy = CmpI->getOperand(0)->getType(); |
| 4067 | if (isa<PointerType>(ArgTy)) { |
| 4068 | CmpI->print(errs()); |
| 4069 | std::string name = I.getParent()->getParent()->getName(); |
| 4070 | errs() |
| 4071 | << "\nPointer equality test is not supported by SPIR-V for Vulkan, " |
| 4072 | << "in function " << name << "\n"; |
| 4073 | llvm_unreachable("Pointer equality check is invalid"); |
| 4074 | break; |
| 4075 | } |
| 4076 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4077 | // Ops[0] = Result Type ID |
| 4078 | // Ops[1] = Operand 1 ID |
| 4079 | // Ops[2] = Operand 2 ID |
| 4080 | SPIRVOperandList Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4081 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4082 | Ops << MkId(lookupType(CmpI->getType())) << MkId(VMap[CmpI->getOperand(0)]) |
| 4083 | << MkId(VMap[CmpI->getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4084 | |
| 4085 | spv::Op Opcode = GetSPIRVCmpOpcode(CmpI); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4086 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4087 | SPIRVInstList.push_back(Inst); |
| 4088 | break; |
| 4089 | } |
| 4090 | case Instruction::Br: { |
| 4091 | // Branch instrucion is deferred because it needs label's ID. Record slot's |
| 4092 | // location on SPIRVInstructionList. |
| 4093 | DeferredInsts.push_back( |
| 4094 | std::make_tuple(&I, --SPIRVInstList.end(), 0 /* No id */)); |
| 4095 | break; |
| 4096 | } |
| 4097 | case Instruction::Switch: { |
| 4098 | I.print(errs()); |
| 4099 | llvm_unreachable("Unsupported instruction???"); |
| 4100 | break; |
| 4101 | } |
| 4102 | case Instruction::IndirectBr: { |
| 4103 | I.print(errs()); |
| 4104 | llvm_unreachable("Unsupported instruction???"); |
| 4105 | break; |
| 4106 | } |
| 4107 | case Instruction::PHI: { |
| 4108 | // Branch instrucion is deferred because it needs label's ID. Record slot's |
| 4109 | // location on SPIRVInstructionList. |
| 4110 | DeferredInsts.push_back( |
| 4111 | std::make_tuple(&I, --SPIRVInstList.end(), nextID++)); |
| 4112 | break; |
| 4113 | } |
| 4114 | case Instruction::Alloca: { |
| 4115 | // |
| 4116 | // Generate OpVariable. |
| 4117 | // |
| 4118 | // Ops[0] : Result Type ID |
| 4119 | // Ops[1] : Storage Class |
| 4120 | SPIRVOperandList Ops; |
| 4121 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4122 | Ops << MkId(lookupType(I.getType())) << MkNum(spv::StorageClassFunction); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4123 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4124 | auto *Inst = new SPIRVInstruction(spv::OpVariable, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4125 | SPIRVInstList.push_back(Inst); |
| 4126 | break; |
| 4127 | } |
| 4128 | case Instruction::Load: { |
| 4129 | LoadInst *LD = cast<LoadInst>(&I); |
| 4130 | // |
| 4131 | // Generate OpLoad. |
| 4132 | // |
| 4133 | |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4134 | uint32_t ResTyID = lookupType(LD->getType()); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4135 | uint32_t PointerID = VMap[LD->getPointerOperand()]; |
| 4136 | |
| 4137 | // This is a hack to work around what looks like a driver bug. |
| 4138 | // When we're loading from the special variable holding the WorkgroupSize |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4139 | // builtin value, use an OpBitWiseAnd of the value's ID rather than |
| 4140 | // generating a load. |
David Neto | 66cfe64 | 2018-03-24 06:13:56 -0700 | [diff] [blame] | 4141 | // TODO(dneto): Remove this awful hack once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4142 | if (PointerID == WorkgroupSizeVarID) { |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4143 | // Generate a bitwise-and of the original value with itself. |
| 4144 | // We should have been able to get away with just an OpCopyObject, |
| 4145 | // but we need something more complex to get past certain driver bugs. |
| 4146 | // This is ridiculous, but necessary. |
| 4147 | // TODO(dneto): Revisit this once drivers fix their bugs. |
| 4148 | |
| 4149 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4150 | Ops << MkId(ResTyID) << MkId(WorkgroupSizeValueID) |
| 4151 | << MkId(WorkgroupSizeValueID); |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4152 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4153 | auto *Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4154 | SPIRVInstList.push_back(Inst); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4155 | break; |
| 4156 | } |
| 4157 | |
| 4158 | // This is the normal path. Generate a load. |
| 4159 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4160 | // Ops[0] = Result Type ID |
| 4161 | // Ops[1] = Pointer ID |
| 4162 | // Ops[2] ... Ops[n] = Optional Memory Access |
| 4163 | // |
| 4164 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4165 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4166 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4167 | Ops << MkId(ResTyID) << MkId(PointerID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4168 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4169 | auto *Inst = new SPIRVInstruction(spv::OpLoad, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4170 | SPIRVInstList.push_back(Inst); |
| 4171 | break; |
| 4172 | } |
| 4173 | case Instruction::Store: { |
| 4174 | StoreInst *ST = cast<StoreInst>(&I); |
| 4175 | // |
| 4176 | // Generate OpStore. |
| 4177 | // |
| 4178 | |
| 4179 | // Ops[0] = Pointer ID |
| 4180 | // Ops[1] = Object ID |
| 4181 | // Ops[2] ... Ops[n] = Optional Memory Access (later???) |
| 4182 | // |
| 4183 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4184 | SPIRVOperandList Ops; |
| 4185 | Ops << MkId(VMap[ST->getPointerOperand()]) |
| 4186 | << MkId(VMap[ST->getValueOperand()]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4187 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4188 | auto *Inst = new SPIRVInstruction(spv::OpStore, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4189 | SPIRVInstList.push_back(Inst); |
| 4190 | break; |
| 4191 | } |
| 4192 | case Instruction::AtomicCmpXchg: { |
| 4193 | I.print(errs()); |
| 4194 | llvm_unreachable("Unsupported instruction???"); |
| 4195 | break; |
| 4196 | } |
| 4197 | case Instruction::AtomicRMW: { |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4198 | AtomicRMWInst *AtomicRMW = dyn_cast<AtomicRMWInst>(&I); |
| 4199 | |
| 4200 | spv::Op opcode; |
| 4201 | |
| 4202 | switch (AtomicRMW->getOperation()) { |
| 4203 | default: |
| 4204 | I.print(errs()); |
| 4205 | llvm_unreachable("Unsupported instruction???"); |
| 4206 | case llvm::AtomicRMWInst::Add: |
| 4207 | opcode = spv::OpAtomicIAdd; |
| 4208 | break; |
| 4209 | case llvm::AtomicRMWInst::Sub: |
| 4210 | opcode = spv::OpAtomicISub; |
| 4211 | break; |
| 4212 | case llvm::AtomicRMWInst::Xchg: |
| 4213 | opcode = spv::OpAtomicExchange; |
| 4214 | break; |
| 4215 | case llvm::AtomicRMWInst::Min: |
| 4216 | opcode = spv::OpAtomicSMin; |
| 4217 | break; |
| 4218 | case llvm::AtomicRMWInst::Max: |
| 4219 | opcode = spv::OpAtomicSMax; |
| 4220 | break; |
| 4221 | case llvm::AtomicRMWInst::UMin: |
| 4222 | opcode = spv::OpAtomicUMin; |
| 4223 | break; |
| 4224 | case llvm::AtomicRMWInst::UMax: |
| 4225 | opcode = spv::OpAtomicUMax; |
| 4226 | break; |
| 4227 | case llvm::AtomicRMWInst::And: |
| 4228 | opcode = spv::OpAtomicAnd; |
| 4229 | break; |
| 4230 | case llvm::AtomicRMWInst::Or: |
| 4231 | opcode = spv::OpAtomicOr; |
| 4232 | break; |
| 4233 | case llvm::AtomicRMWInst::Xor: |
| 4234 | opcode = spv::OpAtomicXor; |
| 4235 | break; |
| 4236 | } |
| 4237 | |
| 4238 | // |
| 4239 | // Generate OpAtomic*. |
| 4240 | // |
| 4241 | SPIRVOperandList Ops; |
| 4242 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4243 | Ops << MkId(lookupType(I.getType())) |
| 4244 | << MkId(VMap[AtomicRMW->getPointerOperand()]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4245 | |
| 4246 | auto IntTy = Type::getInt32Ty(I.getContext()); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4247 | const auto ConstantScopeDevice = ConstantInt::get(IntTy, spv::ScopeDevice); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4248 | Ops << MkId(VMap[ConstantScopeDevice]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4249 | |
| 4250 | const auto ConstantMemorySemantics = ConstantInt::get( |
| 4251 | IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 4252 | spv::MemorySemanticsSequentiallyConsistentMask); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4253 | Ops << MkId(VMap[ConstantMemorySemantics]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4254 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4255 | Ops << MkId(VMap[AtomicRMW->getValOperand()]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4256 | |
| 4257 | VMap[&I] = nextID; |
| 4258 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4259 | auto *Inst = new SPIRVInstruction(opcode, nextID++, Ops); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4260 | SPIRVInstList.push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4261 | break; |
| 4262 | } |
| 4263 | case Instruction::Fence: { |
| 4264 | I.print(errs()); |
| 4265 | llvm_unreachable("Unsupported instruction???"); |
| 4266 | break; |
| 4267 | } |
| 4268 | case Instruction::Call: { |
| 4269 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 4270 | Function *Callee = Call->getCalledFunction(); |
| 4271 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 4272 | if (Callee->getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4273 | if (ResourceVarDeferredLoadCalls.count(Call) && Call->hasNUsesOrMore(1)) { |
| 4274 | // Generate an OpLoad |
| 4275 | SPIRVOperandList Ops; |
| 4276 | const auto load_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4277 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4278 | Ops << MkId(lookupType(Call->getType()->getPointerElementType())) |
| 4279 | << MkId(ResourceVarDeferredLoadCalls[Call]); |
| 4280 | |
| 4281 | auto *Inst = new SPIRVInstruction(spv::OpLoad, load_id, Ops); |
| 4282 | SPIRVInstList.push_back(Inst); |
| 4283 | VMap[Call] = load_id; |
| 4284 | break; |
| 4285 | |
| 4286 | } else { |
| 4287 | // This maps to an OpVariable we've already generated. |
| 4288 | // No code is generated for the call. |
| 4289 | } |
| 4290 | break; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 4291 | } else if (Callee->getName().startswith(clspv::WorkgroupAccessorFunction())) { |
| 4292 | // Don't codegen an instruction here, but instead map this call directly |
| 4293 | // to the workgroup variable id. |
| 4294 | int spec_id = cast<ConstantInt>(Call->getOperand(0))->getSExtValue(); |
| 4295 | const auto &info = LocalSpecIdInfoMap[spec_id]; |
| 4296 | VMap[Call] = info.variable_id; |
| 4297 | break; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | // Sampler initializers become a load of the corresponding sampler. |
| 4301 | |
| 4302 | if (Callee->getName().equals("clspv.sampler.var.literal")) { |
| 4303 | // Map this to a load from the variable. |
| 4304 | const auto index_into_sampler_map = |
| 4305 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue(); |
| 4306 | |
| 4307 | // Generate an OpLoad |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4308 | SPIRVOperandList Ops; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4309 | const auto load_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4310 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4311 | Ops << MkId(lookupType(SamplerTy->getPointerElementType())) |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4312 | << MkId(SamplerMapIndexToIDMap[index_into_sampler_map]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4313 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4314 | auto *Inst = new SPIRVInstruction(spv::OpLoad, load_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4315 | SPIRVInstList.push_back(Inst); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4316 | VMap[Call] = load_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4317 | break; |
| 4318 | } |
| 4319 | |
| 4320 | if (Callee->getName().startswith("spirv.atomic")) { |
| 4321 | spv::Op opcode = StringSwitch<spv::Op>(Callee->getName()) |
| 4322 | .Case("spirv.atomic_add", spv::OpAtomicIAdd) |
| 4323 | .Case("spirv.atomic_sub", spv::OpAtomicISub) |
| 4324 | .Case("spirv.atomic_exchange", spv::OpAtomicExchange) |
| 4325 | .Case("spirv.atomic_inc", spv::OpAtomicIIncrement) |
| 4326 | .Case("spirv.atomic_dec", spv::OpAtomicIDecrement) |
| 4327 | .Case("spirv.atomic_compare_exchange", |
| 4328 | spv::OpAtomicCompareExchange) |
| 4329 | .Case("spirv.atomic_umin", spv::OpAtomicUMin) |
| 4330 | .Case("spirv.atomic_smin", spv::OpAtomicSMin) |
| 4331 | .Case("spirv.atomic_umax", spv::OpAtomicUMax) |
| 4332 | .Case("spirv.atomic_smax", spv::OpAtomicSMax) |
| 4333 | .Case("spirv.atomic_and", spv::OpAtomicAnd) |
| 4334 | .Case("spirv.atomic_or", spv::OpAtomicOr) |
| 4335 | .Case("spirv.atomic_xor", spv::OpAtomicXor) |
| 4336 | .Default(spv::OpNop); |
| 4337 | |
| 4338 | // |
| 4339 | // Generate OpAtomic*. |
| 4340 | // |
| 4341 | SPIRVOperandList Ops; |
| 4342 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4343 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4344 | |
| 4345 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4346 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | VMap[&I] = nextID; |
| 4350 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4351 | auto *Inst = new SPIRVInstruction(opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4352 | SPIRVInstList.push_back(Inst); |
| 4353 | break; |
| 4354 | } |
| 4355 | |
| 4356 | if (Callee->getName().startswith("_Z3dot")) { |
| 4357 | // If the argument is a vector type, generate OpDot |
| 4358 | if (Call->getArgOperand(0)->getType()->isVectorTy()) { |
| 4359 | // |
| 4360 | // Generate OpDot. |
| 4361 | // |
| 4362 | SPIRVOperandList Ops; |
| 4363 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4364 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4365 | |
| 4366 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4367 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | VMap[&I] = nextID; |
| 4371 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4372 | auto *Inst = new SPIRVInstruction(spv::OpDot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4373 | SPIRVInstList.push_back(Inst); |
| 4374 | } else { |
| 4375 | // |
| 4376 | // Generate OpFMul. |
| 4377 | // |
| 4378 | SPIRVOperandList Ops; |
| 4379 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4380 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4381 | |
| 4382 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4383 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | VMap[&I] = nextID; |
| 4387 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4388 | auto *Inst = new SPIRVInstruction(spv::OpFMul, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4389 | SPIRVInstList.push_back(Inst); |
| 4390 | } |
| 4391 | break; |
| 4392 | } |
| 4393 | |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4394 | if (Callee->getName().startswith("_Z4fmod")) { |
| 4395 | // OpenCL fmod(x,y) is x - y * trunc(x/y) |
| 4396 | // The sign for a non-zero result is taken from x. |
| 4397 | // (Try an example.) |
| 4398 | // So translate to OpFRem |
| 4399 | |
| 4400 | SPIRVOperandList Ops; |
| 4401 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4402 | Ops << MkId(lookupType(I.getType())); |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4403 | |
| 4404 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4405 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4406 | } |
| 4407 | |
| 4408 | VMap[&I] = nextID; |
| 4409 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4410 | auto *Inst = new SPIRVInstruction(spv::OpFRem, nextID++, Ops); |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4411 | SPIRVInstList.push_back(Inst); |
| 4412 | break; |
| 4413 | } |
| 4414 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4415 | // spirv.store_null.* intrinsics become OpStore's. |
| 4416 | if (Callee->getName().startswith("spirv.store_null")) { |
| 4417 | // |
| 4418 | // Generate OpStore. |
| 4419 | // |
| 4420 | |
| 4421 | // Ops[0] = Pointer ID |
| 4422 | // Ops[1] = Object ID |
| 4423 | // Ops[2] ... Ops[n] |
| 4424 | SPIRVOperandList Ops; |
| 4425 | |
| 4426 | uint32_t PointerID = VMap[Call->getArgOperand(0)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4427 | uint32_t ObjectID = VMap[Call->getArgOperand(1)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4428 | Ops << MkId(PointerID) << MkId(ObjectID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4429 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4430 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpStore, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4431 | |
| 4432 | break; |
| 4433 | } |
| 4434 | |
| 4435 | // spirv.copy_memory.* intrinsics become OpMemoryMemory's. |
| 4436 | if (Callee->getName().startswith("spirv.copy_memory")) { |
| 4437 | // |
| 4438 | // Generate OpCopyMemory. |
| 4439 | // |
| 4440 | |
| 4441 | // Ops[0] = Dst ID |
| 4442 | // Ops[1] = Src ID |
| 4443 | // Ops[2] = Memory Access |
| 4444 | // Ops[3] = Alignment |
| 4445 | |
| 4446 | auto IsVolatile = |
| 4447 | dyn_cast<ConstantInt>(Call->getArgOperand(3))->getZExtValue() != 0; |
| 4448 | |
| 4449 | auto VolatileMemoryAccess = (IsVolatile) ? spv::MemoryAccessVolatileMask |
| 4450 | : spv::MemoryAccessMaskNone; |
| 4451 | |
| 4452 | auto MemoryAccess = VolatileMemoryAccess | spv::MemoryAccessAlignedMask; |
| 4453 | |
| 4454 | auto Alignment = |
| 4455 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue(); |
| 4456 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4457 | SPIRVOperandList Ops; |
| 4458 | Ops << MkId(VMap[Call->getArgOperand(0)]) |
| 4459 | << MkId(VMap[Call->getArgOperand(1)]) << MkNum(MemoryAccess) |
| 4460 | << MkNum(static_cast<uint32_t>(Alignment)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4461 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4462 | auto *Inst = new SPIRVInstruction(spv::OpCopyMemory, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4463 | |
| 4464 | SPIRVInstList.push_back(Inst); |
| 4465 | |
| 4466 | break; |
| 4467 | } |
| 4468 | |
| 4469 | // Nothing to do for abs with uint. Map abs's operand ID to VMap for abs |
| 4470 | // with unit. |
| 4471 | if (Callee->getName().equals("_Z3absj") || |
| 4472 | Callee->getName().equals("_Z3absDv2_j") || |
| 4473 | Callee->getName().equals("_Z3absDv3_j") || |
| 4474 | Callee->getName().equals("_Z3absDv4_j")) { |
| 4475 | VMap[&I] = VMap[Call->getOperand(0)]; |
| 4476 | break; |
| 4477 | } |
| 4478 | |
| 4479 | // barrier is converted to OpControlBarrier |
| 4480 | if (Callee->getName().equals("__spirv_control_barrier")) { |
| 4481 | // |
| 4482 | // Generate OpControlBarrier. |
| 4483 | // |
| 4484 | // Ops[0] = Execution Scope ID |
| 4485 | // Ops[1] = Memory Scope ID |
| 4486 | // Ops[2] = Memory Semantics ID |
| 4487 | // |
| 4488 | Value *ExecutionScope = Call->getArgOperand(0); |
| 4489 | Value *MemoryScope = Call->getArgOperand(1); |
| 4490 | Value *MemorySemantics = Call->getArgOperand(2); |
| 4491 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4492 | SPIRVOperandList Ops; |
| 4493 | Ops << MkId(VMap[ExecutionScope]) << MkId(VMap[MemoryScope]) |
| 4494 | << MkId(VMap[MemorySemantics]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4495 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4496 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpControlBarrier, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4497 | break; |
| 4498 | } |
| 4499 | |
| 4500 | // memory barrier is converted to OpMemoryBarrier |
| 4501 | if (Callee->getName().equals("__spirv_memory_barrier")) { |
| 4502 | // |
| 4503 | // Generate OpMemoryBarrier. |
| 4504 | // |
| 4505 | // Ops[0] = Memory Scope ID |
| 4506 | // Ops[1] = Memory Semantics ID |
| 4507 | // |
| 4508 | SPIRVOperandList Ops; |
| 4509 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4510 | uint32_t MemoryScopeID = VMap[Call->getArgOperand(0)]; |
| 4511 | uint32_t MemorySemanticsID = VMap[Call->getArgOperand(1)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4512 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4513 | Ops << MkId(MemoryScopeID) << MkId(MemorySemanticsID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4514 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4515 | auto *Inst = new SPIRVInstruction(spv::OpMemoryBarrier, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4516 | SPIRVInstList.push_back(Inst); |
| 4517 | break; |
| 4518 | } |
| 4519 | |
| 4520 | // isinf is converted to OpIsInf |
| 4521 | if (Callee->getName().equals("__spirv_isinff") || |
| 4522 | Callee->getName().equals("__spirv_isinfDv2_f") || |
| 4523 | Callee->getName().equals("__spirv_isinfDv3_f") || |
| 4524 | Callee->getName().equals("__spirv_isinfDv4_f")) { |
| 4525 | // |
| 4526 | // Generate OpIsInf. |
| 4527 | // |
| 4528 | // Ops[0] = Result Type ID |
| 4529 | // Ops[1] = X ID |
| 4530 | // |
| 4531 | SPIRVOperandList Ops; |
| 4532 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4533 | Ops << MkId(lookupType(I.getType())) |
| 4534 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4535 | |
| 4536 | VMap[&I] = nextID; |
| 4537 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4538 | auto *Inst = new SPIRVInstruction(spv::OpIsInf, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4539 | SPIRVInstList.push_back(Inst); |
| 4540 | break; |
| 4541 | } |
| 4542 | |
| 4543 | // isnan is converted to OpIsNan |
| 4544 | if (Callee->getName().equals("__spirv_isnanf") || |
| 4545 | Callee->getName().equals("__spirv_isnanDv2_f") || |
| 4546 | Callee->getName().equals("__spirv_isnanDv3_f") || |
| 4547 | Callee->getName().equals("__spirv_isnanDv4_f")) { |
| 4548 | // |
| 4549 | // Generate OpIsInf. |
| 4550 | // |
| 4551 | // Ops[0] = Result Type ID |
| 4552 | // Ops[1] = X ID |
| 4553 | // |
| 4554 | SPIRVOperandList Ops; |
| 4555 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4556 | Ops << MkId(lookupType(I.getType())) |
| 4557 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4558 | |
| 4559 | VMap[&I] = nextID; |
| 4560 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4561 | auto *Inst = new SPIRVInstruction(spv::OpIsNan, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4562 | SPIRVInstList.push_back(Inst); |
| 4563 | break; |
| 4564 | } |
| 4565 | |
| 4566 | // all is converted to OpAll |
| 4567 | if (Callee->getName().equals("__spirv_allDv2_i") || |
| 4568 | Callee->getName().equals("__spirv_allDv3_i") || |
| 4569 | Callee->getName().equals("__spirv_allDv4_i")) { |
| 4570 | // |
| 4571 | // Generate OpAll. |
| 4572 | // |
| 4573 | // Ops[0] = Result Type ID |
| 4574 | // Ops[1] = Vector ID |
| 4575 | // |
| 4576 | SPIRVOperandList Ops; |
| 4577 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4578 | Ops << MkId(lookupType(I.getType())) |
| 4579 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4580 | |
| 4581 | VMap[&I] = nextID; |
| 4582 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4583 | auto *Inst = new SPIRVInstruction(spv::OpAll, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4584 | SPIRVInstList.push_back(Inst); |
| 4585 | break; |
| 4586 | } |
| 4587 | |
| 4588 | // any is converted to OpAny |
| 4589 | if (Callee->getName().equals("__spirv_anyDv2_i") || |
| 4590 | Callee->getName().equals("__spirv_anyDv3_i") || |
| 4591 | Callee->getName().equals("__spirv_anyDv4_i")) { |
| 4592 | // |
| 4593 | // Generate OpAny. |
| 4594 | // |
| 4595 | // Ops[0] = Result Type ID |
| 4596 | // Ops[1] = Vector ID |
| 4597 | // |
| 4598 | SPIRVOperandList Ops; |
| 4599 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4600 | Ops << MkId(lookupType(I.getType())) |
| 4601 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4602 | |
| 4603 | VMap[&I] = nextID; |
| 4604 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4605 | auto *Inst = new SPIRVInstruction(spv::OpAny, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4606 | SPIRVInstList.push_back(Inst); |
| 4607 | break; |
| 4608 | } |
| 4609 | |
| 4610 | // read_image is converted to OpSampledImage and OpImageSampleExplicitLod. |
| 4611 | // Additionally, OpTypeSampledImage is generated. |
| 4612 | if (Callee->getName().equals( |
| 4613 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") || |
| 4614 | Callee->getName().equals( |
| 4615 | "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) { |
| 4616 | // |
| 4617 | // Generate OpSampledImage. |
| 4618 | // |
| 4619 | // Ops[0] = Result Type ID |
| 4620 | // Ops[1] = Image ID |
| 4621 | // Ops[2] = Sampler ID |
| 4622 | // |
| 4623 | SPIRVOperandList Ops; |
| 4624 | |
| 4625 | Value *Image = Call->getArgOperand(0); |
| 4626 | Value *Sampler = Call->getArgOperand(1); |
| 4627 | Value *Coordinate = Call->getArgOperand(2); |
| 4628 | |
| 4629 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 4630 | Type *ImageTy = Image->getType()->getPointerElementType(); |
| 4631 | uint32_t ImageTyID = OpImageTypeMap[ImageTy]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4632 | uint32_t ImageID = VMap[Image]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4633 | uint32_t SamplerID = VMap[Sampler]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4634 | |
| 4635 | Ops << MkId(ImageTyID) << MkId(ImageID) << MkId(SamplerID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4636 | |
| 4637 | uint32_t SampledImageID = nextID; |
| 4638 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4639 | auto *Inst = new SPIRVInstruction(spv::OpSampledImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4640 | SPIRVInstList.push_back(Inst); |
| 4641 | |
| 4642 | // |
| 4643 | // Generate OpImageSampleExplicitLod. |
| 4644 | // |
| 4645 | // Ops[0] = Result Type ID |
| 4646 | // Ops[1] = Sampled Image ID |
| 4647 | // Ops[2] = Coordinate ID |
| 4648 | // Ops[3] = Image Operands Type ID |
| 4649 | // Ops[4] ... Ops[n] = Operands ID |
| 4650 | // |
| 4651 | Ops.clear(); |
| 4652 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4653 | Ops << MkId(lookupType(Call->getType())) << MkId(SampledImageID) |
| 4654 | << MkId(VMap[Coordinate]) << MkNum(spv::ImageOperandsLodMask); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4655 | |
| 4656 | Constant *CstFP0 = ConstantFP::get(Context, APFloat(0.0f)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4657 | Ops << MkId(VMap[CstFP0]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4658 | |
| 4659 | VMap[&I] = nextID; |
| 4660 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4661 | Inst = new SPIRVInstruction(spv::OpImageSampleExplicitLod, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4662 | SPIRVInstList.push_back(Inst); |
| 4663 | break; |
| 4664 | } |
| 4665 | |
| 4666 | // write_imagef is mapped to OpImageWrite. |
| 4667 | if (Callee->getName().equals( |
| 4668 | "_Z12write_imagef14ocl_image2d_woDv2_iDv4_f") || |
| 4669 | Callee->getName().equals( |
| 4670 | "_Z12write_imagef14ocl_image3d_woDv4_iDv4_f")) { |
| 4671 | // |
| 4672 | // Generate OpImageWrite. |
| 4673 | // |
| 4674 | // Ops[0] = Image ID |
| 4675 | // Ops[1] = Coordinate ID |
| 4676 | // Ops[2] = Texel ID |
| 4677 | // Ops[3] = (Optional) Image Operands Type (Literal Number) |
| 4678 | // Ops[4] ... Ops[n] = (Optional) Operands ID |
| 4679 | // |
| 4680 | SPIRVOperandList Ops; |
| 4681 | |
| 4682 | Value *Image = Call->getArgOperand(0); |
| 4683 | Value *Coordinate = Call->getArgOperand(1); |
| 4684 | Value *Texel = Call->getArgOperand(2); |
| 4685 | |
| 4686 | uint32_t ImageID = VMap[Image]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4687 | uint32_t CoordinateID = VMap[Coordinate]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4688 | uint32_t TexelID = VMap[Texel]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4689 | Ops << MkId(ImageID) << MkId(CoordinateID) << MkId(TexelID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4690 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4691 | auto *Inst = new SPIRVInstruction(spv::OpImageWrite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4692 | SPIRVInstList.push_back(Inst); |
| 4693 | break; |
| 4694 | } |
| 4695 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4696 | // get_image_width is mapped to OpImageQuerySize |
| 4697 | if (Callee->getName().equals("_Z15get_image_width14ocl_image2d_ro") || |
| 4698 | Callee->getName().equals("_Z15get_image_width14ocl_image2d_wo") || |
| 4699 | Callee->getName().equals("_Z16get_image_height14ocl_image2d_ro") || |
| 4700 | Callee->getName().equals("_Z16get_image_height14ocl_image2d_wo")) { |
| 4701 | // |
| 4702 | // Generate OpImageQuerySize, then pull out the right component. |
| 4703 | // Assume 2D image for now. |
| 4704 | // |
| 4705 | // Ops[0] = Image ID |
| 4706 | // |
| 4707 | // %sizes = OpImageQuerySizes %uint2 %im |
| 4708 | // %result = OpCompositeExtract %uint %sizes 0-or-1 |
| 4709 | SPIRVOperandList Ops; |
| 4710 | |
| 4711 | // Implement: |
| 4712 | // %sizes = OpImageQuerySizes %uint2 %im |
| 4713 | uint32_t SizesTypeID = |
| 4714 | TypeMap[VectorType::get(Type::getInt32Ty(Context), 2)]; |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4715 | Value *Image = Call->getArgOperand(0); |
| 4716 | uint32_t ImageID = VMap[Image]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4717 | Ops << MkId(SizesTypeID) << MkId(ImageID); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4718 | |
| 4719 | uint32_t SizesID = nextID++; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4720 | auto *QueryInst = |
| 4721 | new SPIRVInstruction(spv::OpImageQuerySize, SizesID, Ops); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4722 | SPIRVInstList.push_back(QueryInst); |
| 4723 | |
| 4724 | // Reset value map entry since we generated an intermediate instruction. |
| 4725 | VMap[&I] = nextID; |
| 4726 | |
| 4727 | // Implement: |
| 4728 | // %result = OpCompositeExtract %uint %sizes 0-or-1 |
| 4729 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4730 | Ops << MkId(TypeMap[I.getType()]) << MkId(SizesID); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4731 | |
| 4732 | uint32_t component = Callee->getName().contains("height") ? 1 : 0; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4733 | Ops << MkNum(component); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4734 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4735 | auto *Inst = new SPIRVInstruction(spv::OpCompositeExtract, nextID++, Ops); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4736 | SPIRVInstList.push_back(Inst); |
| 4737 | break; |
| 4738 | } |
| 4739 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4740 | // Call instrucion is deferred because it needs function's ID. Record |
| 4741 | // slot's location on SPIRVInstructionList. |
| 4742 | DeferredInsts.push_back( |
| 4743 | std::make_tuple(&I, --SPIRVInstList.end(), nextID++)); |
| 4744 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4745 | // Check whether the implementation of this call uses an extended |
| 4746 | // instruction plus one more value-producing instruction. If so, then |
| 4747 | // reserve the id for the extra value-producing slot. |
| 4748 | glsl::ExtInst EInst = getIndirectExtInstEnum(Callee->getName()); |
| 4749 | if (EInst != kGlslExtInstBad) { |
| 4750 | // Reserve a spot for the extra value. |
David Neto | 4d02a53 | 2017-09-17 12:57:44 -0400 | [diff] [blame] | 4751 | // Increase nextID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4752 | VMap[&I] = nextID; |
| 4753 | nextID++; |
| 4754 | } |
| 4755 | break; |
| 4756 | } |
| 4757 | case Instruction::Ret: { |
| 4758 | unsigned NumOps = I.getNumOperands(); |
| 4759 | if (NumOps == 0) { |
| 4760 | // |
| 4761 | // Generate OpReturn. |
| 4762 | // |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4763 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpReturn, {})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4764 | } else { |
| 4765 | // |
| 4766 | // Generate OpReturnValue. |
| 4767 | // |
| 4768 | |
| 4769 | // Ops[0] = Return Value ID |
| 4770 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4771 | |
| 4772 | Ops << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4773 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4774 | auto *Inst = new SPIRVInstruction(spv::OpReturnValue, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4775 | SPIRVInstList.push_back(Inst); |
| 4776 | break; |
| 4777 | } |
| 4778 | break; |
| 4779 | } |
| 4780 | } |
| 4781 | } |
| 4782 | |
| 4783 | void SPIRVProducerPass::GenerateFuncEpilogue() { |
| 4784 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 4785 | |
| 4786 | // |
| 4787 | // Generate OpFunctionEnd |
| 4788 | // |
| 4789 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4790 | auto *Inst = new SPIRVInstruction(spv::OpFunctionEnd, {}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4791 | SPIRVInstList.push_back(Inst); |
| 4792 | } |
| 4793 | |
| 4794 | bool SPIRVProducerPass::is4xi8vec(Type *Ty) const { |
| 4795 | LLVMContext &Context = Ty->getContext(); |
| 4796 | if (Ty->isVectorTy()) { |
| 4797 | if (Ty->getVectorElementType() == Type::getInt8Ty(Context) && |
| 4798 | Ty->getVectorNumElements() == 4) { |
| 4799 | return true; |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | return false; |
| 4804 | } |
| 4805 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4806 | uint32_t SPIRVProducerPass::GetI32Zero() { |
| 4807 | if (0 == constant_i32_zero_id_) { |
| 4808 | llvm_unreachable("Requesting a 32-bit integer constant but it is not " |
| 4809 | "defined in the SPIR-V module"); |
| 4810 | } |
| 4811 | return constant_i32_zero_id_; |
| 4812 | } |
| 4813 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4814 | void SPIRVProducerPass::HandleDeferredInstruction() { |
| 4815 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 4816 | ValueMapType &VMap = getValueMap(); |
| 4817 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 4818 | |
| 4819 | for (auto DeferredInst = DeferredInsts.rbegin(); |
| 4820 | DeferredInst != DeferredInsts.rend(); ++DeferredInst) { |
| 4821 | Value *Inst = std::get<0>(*DeferredInst); |
| 4822 | SPIRVInstructionList::iterator InsertPoint = ++std::get<1>(*DeferredInst); |
| 4823 | if (InsertPoint != SPIRVInstList.end()) { |
| 4824 | while ((*InsertPoint)->getOpcode() == spv::OpPhi) { |
| 4825 | ++InsertPoint; |
| 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | if (BranchInst *Br = dyn_cast<BranchInst>(Inst)) { |
| 4830 | // Check whether basic block, which has this branch instruction, is loop |
| 4831 | // header or not. If it is loop header, generate OpLoopMerge and |
| 4832 | // OpBranchConditional. |
| 4833 | Function *Func = Br->getParent()->getParent(); |
| 4834 | DominatorTree &DT = |
| 4835 | getAnalysis<DominatorTreeWrapperPass>(*Func).getDomTree(); |
| 4836 | const LoopInfo &LI = |
| 4837 | getAnalysis<LoopInfoWrapperPass>(*Func).getLoopInfo(); |
| 4838 | |
| 4839 | BasicBlock *BrBB = Br->getParent(); |
| 4840 | if (LI.isLoopHeader(BrBB)) { |
| 4841 | Value *ContinueBB = nullptr; |
| 4842 | Value *MergeBB = nullptr; |
| 4843 | |
| 4844 | Loop *L = LI.getLoopFor(BrBB); |
| 4845 | MergeBB = L->getExitBlock(); |
| 4846 | if (!MergeBB) { |
| 4847 | // StructurizeCFG pass converts CFG into triangle shape and the cfg |
| 4848 | // has regions with single entry/exit. As a result, loop should not |
| 4849 | // have multiple exits. |
| 4850 | llvm_unreachable("Loop has multiple exits???"); |
| 4851 | } |
| 4852 | |
| 4853 | if (L->isLoopLatch(BrBB)) { |
| 4854 | ContinueBB = BrBB; |
| 4855 | } else { |
| 4856 | // From SPIR-V spec 2.11, Continue Target must dominate that back-edge |
| 4857 | // block. |
| 4858 | BasicBlock *Header = L->getHeader(); |
| 4859 | BasicBlock *Latch = L->getLoopLatch(); |
| 4860 | for (BasicBlock *BB : L->blocks()) { |
| 4861 | if (BB == Header) { |
| 4862 | continue; |
| 4863 | } |
| 4864 | |
| 4865 | // Check whether block dominates block with back-edge. |
| 4866 | if (DT.dominates(BB, Latch)) { |
| 4867 | ContinueBB = BB; |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | if (!ContinueBB) { |
| 4872 | llvm_unreachable("Wrong continue block from loop"); |
| 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | // |
| 4877 | // Generate OpLoopMerge. |
| 4878 | // |
| 4879 | // Ops[0] = Merge Block ID |
| 4880 | // Ops[1] = Continue Target ID |
| 4881 | // Ops[2] = Selection Control |
| 4882 | SPIRVOperandList Ops; |
| 4883 | |
| 4884 | // StructurizeCFG pass already manipulated CFG. Just use false block of |
| 4885 | // branch instruction as merge block. |
| 4886 | uint32_t MergeBBID = VMap[MergeBB]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4887 | uint32_t ContinueBBID = VMap[ContinueBB]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4888 | Ops << MkId(MergeBBID) << MkId(ContinueBBID) |
| 4889 | << MkNum(spv::SelectionControlMaskNone); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4890 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4891 | auto *MergeInst = new SPIRVInstruction(spv::OpLoopMerge, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4892 | SPIRVInstList.insert(InsertPoint, MergeInst); |
| 4893 | |
| 4894 | } else if (Br->isConditional()) { |
| 4895 | bool HasBackEdge = false; |
| 4896 | |
| 4897 | for (unsigned i = 0; i < Br->getNumSuccessors(); i++) { |
| 4898 | if (LI.isLoopHeader(Br->getSuccessor(i))) { |
| 4899 | HasBackEdge = true; |
| 4900 | } |
| 4901 | } |
| 4902 | if (!HasBackEdge) { |
| 4903 | // |
| 4904 | // Generate OpSelectionMerge. |
| 4905 | // |
| 4906 | // Ops[0] = Merge Block ID |
| 4907 | // Ops[1] = Selection Control |
| 4908 | SPIRVOperandList Ops; |
| 4909 | |
| 4910 | // StructurizeCFG pass already manipulated CFG. Just use false block |
| 4911 | // of branch instruction as merge block. |
| 4912 | uint32_t MergeBBID = VMap[Br->getSuccessor(1)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4913 | Ops << MkId(MergeBBID) << MkNum(spv::SelectionControlMaskNone); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4914 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4915 | auto *MergeInst = new SPIRVInstruction(spv::OpSelectionMerge, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4916 | SPIRVInstList.insert(InsertPoint, MergeInst); |
| 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | if (Br->isConditional()) { |
| 4921 | // |
| 4922 | // Generate OpBranchConditional. |
| 4923 | // |
| 4924 | // Ops[0] = Condition ID |
| 4925 | // Ops[1] = True Label ID |
| 4926 | // Ops[2] = False Label ID |
| 4927 | // Ops[3] ... Ops[n] = Branch weights (Literal Number) |
| 4928 | SPIRVOperandList Ops; |
| 4929 | |
| 4930 | uint32_t CondID = VMap[Br->getCondition()]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4931 | uint32_t TrueBBID = VMap[Br->getSuccessor(0)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4932 | uint32_t FalseBBID = VMap[Br->getSuccessor(1)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4933 | |
| 4934 | Ops << MkId(CondID) << MkId(TrueBBID) << MkId(FalseBBID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4935 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4936 | auto *BrInst = new SPIRVInstruction(spv::OpBranchConditional, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4937 | SPIRVInstList.insert(InsertPoint, BrInst); |
| 4938 | } else { |
| 4939 | // |
| 4940 | // Generate OpBranch. |
| 4941 | // |
| 4942 | // Ops[0] = Target Label ID |
| 4943 | SPIRVOperandList Ops; |
| 4944 | |
| 4945 | uint32_t TargetID = VMap[Br->getSuccessor(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4946 | Ops << MkId(TargetID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4947 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4948 | SPIRVInstList.insert(InsertPoint, |
| 4949 | new SPIRVInstruction(spv::OpBranch, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4950 | } |
| 4951 | } else if (PHINode *PHI = dyn_cast<PHINode>(Inst)) { |
| 4952 | // |
| 4953 | // Generate OpPhi. |
| 4954 | // |
| 4955 | // Ops[0] = Result Type ID |
| 4956 | // Ops[1] ... Ops[n] = (Variable ID, Parent ID) pairs |
| 4957 | SPIRVOperandList Ops; |
| 4958 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4959 | Ops << MkId(lookupType(PHI->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4960 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4961 | for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) { |
| 4962 | uint32_t VarID = VMap[PHI->getIncomingValue(i)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4963 | uint32_t ParentID = VMap[PHI->getIncomingBlock(i)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4964 | Ops << MkId(VarID) << MkId(ParentID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4968 | InsertPoint, |
| 4969 | new SPIRVInstruction(spv::OpPhi, std::get<2>(*DeferredInst), Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4970 | } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { |
| 4971 | Function *Callee = Call->getCalledFunction(); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4972 | auto callee_name = Callee->getName(); |
| 4973 | glsl::ExtInst EInst = getDirectOrIndirectExtInstEnum(callee_name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4974 | |
| 4975 | if (EInst) { |
| 4976 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 4977 | |
| 4978 | // |
| 4979 | // Generate OpExtInst. |
| 4980 | // |
| 4981 | |
| 4982 | // Ops[0] = Result Type ID |
| 4983 | // Ops[1] = Set ID (OpExtInstImport ID) |
| 4984 | // Ops[2] = Instruction Number (Literal Number) |
| 4985 | // Ops[3] ... Ops[n] = Operand 1, ... , Operand n |
| 4986 | SPIRVOperandList Ops; |
| 4987 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4988 | Ops << MkId(lookupType(Call->getType())) << MkId(ExtInstImportID) |
| 4989 | << MkNum(EInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4990 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4991 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
| 4992 | for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4993 | Ops << MkId(VMap[Call->getOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4994 | } |
| 4995 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4996 | auto *ExtInst = new SPIRVInstruction(spv::OpExtInst, |
| 4997 | std::get<2>(*DeferredInst), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4998 | SPIRVInstList.insert(InsertPoint, ExtInst); |
| 4999 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5000 | const auto IndirectExtInst = getIndirectExtInstEnum(callee_name); |
| 5001 | if (IndirectExtInst != kGlslExtInstBad) { |
| 5002 | // Generate one more instruction that uses the result of the extended |
| 5003 | // instruction. Its result id is one more than the id of the |
| 5004 | // extended instruction. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5005 | LLVMContext &Context = |
| 5006 | Call->getParent()->getParent()->getParent()->getContext(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5007 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5008 | auto generate_extra_inst = [this, &Context, &Call, &DeferredInst, |
| 5009 | &VMap, &SPIRVInstList, &InsertPoint]( |
| 5010 | spv::Op opcode, Constant *constant) { |
| 5011 | // |
| 5012 | // Generate instruction like: |
| 5013 | // result = opcode constant <extinst-result> |
| 5014 | // |
| 5015 | // Ops[0] = Result Type ID |
| 5016 | // Ops[1] = Operand 0 ;; the constant, suitably splatted |
| 5017 | // Ops[2] = Operand 1 ;; the result of the extended instruction |
| 5018 | SPIRVOperandList Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5019 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5020 | Type *resultTy = Call->getType(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5021 | Ops << MkId(lookupType(resultTy)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5022 | |
| 5023 | if (auto *vectorTy = dyn_cast<VectorType>(resultTy)) { |
| 5024 | constant = ConstantVector::getSplat( |
| 5025 | static_cast<unsigned>(vectorTy->getNumElements()), constant); |
| 5026 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5027 | Ops << MkId(VMap[constant]) << MkId(std::get<2>(*DeferredInst)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5028 | |
| 5029 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5030 | InsertPoint, new SPIRVInstruction( |
| 5031 | opcode, std::get<2>(*DeferredInst) + 1, Ops)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5032 | }; |
| 5033 | |
| 5034 | switch (IndirectExtInst) { |
| 5035 | case glsl::ExtInstFindUMsb: // Implementing clz |
| 5036 | generate_extra_inst( |
| 5037 | spv::OpISub, ConstantInt::get(Type::getInt32Ty(Context), 31)); |
| 5038 | break; |
| 5039 | case glsl::ExtInstAcos: // Implementing acospi |
| 5040 | case glsl::ExtInstAsin: // Implementing asinpi |
| 5041 | case glsl::ExtInstAtan2: // Implementing atan2pi |
| 5042 | generate_extra_inst( |
| 5043 | spv::OpFMul, |
| 5044 | ConstantFP::get(Type::getFloatTy(Context), kOneOverPi)); |
| 5045 | break; |
| 5046 | |
| 5047 | default: |
| 5048 | assert(false && "internally inconsistent"); |
David Neto | 4d02a53 | 2017-09-17 12:57:44 -0400 | [diff] [blame] | 5049 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5050 | } |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5051 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5052 | } else if (callee_name.equals("_Z8popcounti") || |
| 5053 | callee_name.equals("_Z8popcountj") || |
| 5054 | callee_name.equals("_Z8popcountDv2_i") || |
| 5055 | callee_name.equals("_Z8popcountDv3_i") || |
| 5056 | callee_name.equals("_Z8popcountDv4_i") || |
| 5057 | callee_name.equals("_Z8popcountDv2_j") || |
| 5058 | callee_name.equals("_Z8popcountDv3_j") || |
| 5059 | callee_name.equals("_Z8popcountDv4_j")) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5060 | // |
| 5061 | // Generate OpBitCount |
| 5062 | // |
| 5063 | // Ops[0] = Result Type ID |
| 5064 | // Ops[1] = Base ID |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5065 | SPIRVOperandList Ops; |
| 5066 | Ops << MkId(lookupType(Call->getType())) |
| 5067 | << MkId(VMap[Call->getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5068 | |
| 5069 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5070 | InsertPoint, new SPIRVInstruction(spv::OpBitCount, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5071 | std::get<2>(*DeferredInst), Ops)); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5072 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5073 | } else if (callee_name.startswith(kCompositeConstructFunctionPrefix)) { |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5074 | |
| 5075 | // Generate an OpCompositeConstruct |
| 5076 | SPIRVOperandList Ops; |
| 5077 | |
| 5078 | // The result type. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5079 | Ops << MkId(lookupType(Call->getType())); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5080 | |
| 5081 | for (Use &use : Call->arg_operands()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5082 | Ops << MkId(VMap[use.get()]); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5083 | } |
| 5084 | |
| 5085 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5086 | InsertPoint, new SPIRVInstruction(spv::OpCompositeConstruct, |
| 5087 | std::get<2>(*DeferredInst), Ops)); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5088 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 5089 | } else if (callee_name.startswith(clspv::ResourceAccessorFunction())) { |
| 5090 | |
| 5091 | // We have already mapped the call's result value to an ID. |
| 5092 | // Don't generate any code now. |
| 5093 | |
| 5094 | } else if (callee_name.startswith(clspv::WorkgroupAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5095 | |
| 5096 | // We have already mapped the call's result value to an ID. |
| 5097 | // Don't generate any code now. |
| 5098 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5099 | } else { |
| 5100 | // |
| 5101 | // Generate OpFunctionCall. |
| 5102 | // |
| 5103 | |
| 5104 | // Ops[0] = Result Type ID |
| 5105 | // Ops[1] = Callee Function ID |
| 5106 | // Ops[2] ... Ops[n] = Argument 0, ... , Argument n |
| 5107 | SPIRVOperandList Ops; |
| 5108 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5109 | Ops << MkId(lookupType(Call->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5110 | |
| 5111 | uint32_t CalleeID = VMap[Callee]; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 5112 | if (CalleeID == 0) { |
| 5113 | errs() << "Can't translate function call. Missing builtin? " |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5114 | << callee_name << " in: " << *Call << "\n"; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 5115 | // TODO(dneto): Can we error out? Enabling this llvm_unreachable |
| 5116 | // causes an infinite loop. Instead, go ahead and generate |
| 5117 | // the bad function call. A validator will catch the 0-Id. |
| 5118 | // llvm_unreachable("Can't translate function call"); |
| 5119 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5120 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5121 | Ops << MkId(CalleeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5122 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5123 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
| 5124 | for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5125 | Ops << MkId(VMap[Call->getOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5126 | } |
| 5127 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5128 | auto *CallInst = new SPIRVInstruction(spv::OpFunctionCall, |
| 5129 | std::get<2>(*DeferredInst), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5130 | SPIRVInstList.insert(InsertPoint, CallInst); |
| 5131 | } |
| 5132 | } |
| 5133 | } |
| 5134 | } |
| 5135 | |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5136 | void SPIRVProducerPass::HandleDeferredDecorations(const DataLayout &DL) { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 5137 | if (getTypesNeedingArrayStride().empty() && LocalArgSpecIds.empty()) { |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5138 | return; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5139 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5140 | |
| 5141 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5142 | |
| 5143 | // Find an iterator pointing just past the last decoration. |
| 5144 | bool seen_decorations = false; |
| 5145 | auto DecoInsertPoint = |
| 5146 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 5147 | [&seen_decorations](SPIRVInstruction *Inst) -> bool { |
| 5148 | const bool is_decoration = |
| 5149 | Inst->getOpcode() == spv::OpDecorate || |
| 5150 | Inst->getOpcode() == spv::OpMemberDecorate; |
| 5151 | if (is_decoration) { |
| 5152 | seen_decorations = true; |
| 5153 | return false; |
| 5154 | } else { |
| 5155 | return seen_decorations; |
| 5156 | } |
| 5157 | }); |
| 5158 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5159 | // Insert ArrayStride decorations on pointer types, due to OpPtrAccessChain |
| 5160 | // instructions we generated earlier. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 5161 | for (auto *type : getTypesNeedingArrayStride()) { |
| 5162 | Type *elemTy = nullptr; |
| 5163 | if (auto *ptrTy = dyn_cast<PointerType>(type)) { |
| 5164 | elemTy = ptrTy->getElementType(); |
| 5165 | } else if (auto* arrayTy = dyn_cast<ArrayType>(type)) { |
| 5166 | elemTy = arrayTy->getArrayElementType(); |
| 5167 | } else if (auto* seqTy = dyn_cast<SequentialType>(type)) { |
| 5168 | elemTy = seqTy->getSequentialElementType(); |
| 5169 | } else { |
| 5170 | errs() << "Unhandled strided type " << *type << "\n"; |
| 5171 | llvm_unreachable("Unhandled strided type"); |
| 5172 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5173 | |
| 5174 | // Ops[0] = Target ID |
| 5175 | // Ops[1] = Decoration (ArrayStride) |
| 5176 | // Ops[2] = Stride number (Literal Number) |
| 5177 | SPIRVOperandList Ops; |
| 5178 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 5179 | // Same as DL.getIndexedOffsetInType( elemTy, { 1 } ); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 5180 | const uint32_t stride = static_cast<uint32_t>(DL.getTypeAllocSize(elemTy)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5181 | |
| 5182 | Ops << MkId(lookupType(type)) << MkNum(spv::DecorationArrayStride) |
| 5183 | << MkNum(stride); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5184 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5185 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5186 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
| 5187 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5188 | |
| 5189 | // Emit SpecId decorations targeting the array size value. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame^] | 5190 | for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_; |
| 5191 | ++spec_id) { |
| 5192 | LocalArgInfo& arg_info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5193 | SPIRVOperandList Ops; |
| 5194 | Ops << MkId(arg_info.array_size_id) << MkNum(spv::DecorationSpecId) |
| 5195 | << MkNum(arg_info.spec_id); |
| 5196 | SPIRVInstList.insert(DecoInsertPoint, |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5197 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5198 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5199 | } |
| 5200 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5201 | glsl::ExtInst SPIRVProducerPass::getExtInstEnum(StringRef Name) { |
| 5202 | return StringSwitch<glsl::ExtInst>(Name) |
| 5203 | .Case("_Z3absi", glsl::ExtInst::ExtInstSAbs) |
| 5204 | .Case("_Z3absDv2_i", glsl::ExtInst::ExtInstSAbs) |
| 5205 | .Case("_Z3absDv3_i", glsl::ExtInst::ExtInstSAbs) |
| 5206 | .Case("_Z3absDv4_i", glsl::ExtInst::ExtInstSAbs) |
| 5207 | .Case("_Z5clampiii", glsl::ExtInst::ExtInstSClamp) |
| 5208 | .Case("_Z5clampDv2_iS_S_", glsl::ExtInst::ExtInstSClamp) |
| 5209 | .Case("_Z5clampDv3_iS_S_", glsl::ExtInst::ExtInstSClamp) |
| 5210 | .Case("_Z5clampDv4_iS_S_", glsl::ExtInst::ExtInstSClamp) |
| 5211 | .Case("_Z5clampjjj", glsl::ExtInst::ExtInstUClamp) |
| 5212 | .Case("_Z5clampDv2_jS_S_", glsl::ExtInst::ExtInstUClamp) |
| 5213 | .Case("_Z5clampDv3_jS_S_", glsl::ExtInst::ExtInstUClamp) |
| 5214 | .Case("_Z5clampDv4_jS_S_", glsl::ExtInst::ExtInstUClamp) |
| 5215 | .Case("_Z5clampfff", glsl::ExtInst::ExtInstFClamp) |
| 5216 | .Case("_Z5clampDv2_fS_S_", glsl::ExtInst::ExtInstFClamp) |
| 5217 | .Case("_Z5clampDv3_fS_S_", glsl::ExtInst::ExtInstFClamp) |
| 5218 | .Case("_Z5clampDv4_fS_S_", glsl::ExtInst::ExtInstFClamp) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5219 | .Case("_Z3maxii", glsl::ExtInst::ExtInstSMax) |
| 5220 | .Case("_Z3maxDv2_iS_", glsl::ExtInst::ExtInstSMax) |
| 5221 | .Case("_Z3maxDv3_iS_", glsl::ExtInst::ExtInstSMax) |
| 5222 | .Case("_Z3maxDv4_iS_", glsl::ExtInst::ExtInstSMax) |
| 5223 | .Case("_Z3maxjj", glsl::ExtInst::ExtInstUMax) |
| 5224 | .Case("_Z3maxDv2_jS_", glsl::ExtInst::ExtInstUMax) |
| 5225 | .Case("_Z3maxDv3_jS_", glsl::ExtInst::ExtInstUMax) |
| 5226 | .Case("_Z3maxDv4_jS_", glsl::ExtInst::ExtInstUMax) |
| 5227 | .Case("_Z3maxff", glsl::ExtInst::ExtInstFMax) |
| 5228 | .Case("_Z3maxDv2_fS_", glsl::ExtInst::ExtInstFMax) |
| 5229 | .Case("_Z3maxDv3_fS_", glsl::ExtInst::ExtInstFMax) |
| 5230 | .Case("_Z3maxDv4_fS_", glsl::ExtInst::ExtInstFMax) |
| 5231 | .StartsWith("_Z4fmax", glsl::ExtInst::ExtInstFMax) |
| 5232 | .Case("_Z3minii", glsl::ExtInst::ExtInstSMin) |
| 5233 | .Case("_Z3minDv2_iS_", glsl::ExtInst::ExtInstSMin) |
| 5234 | .Case("_Z3minDv3_iS_", glsl::ExtInst::ExtInstSMin) |
| 5235 | .Case("_Z3minDv4_iS_", glsl::ExtInst::ExtInstSMin) |
| 5236 | .Case("_Z3minjj", glsl::ExtInst::ExtInstUMin) |
| 5237 | .Case("_Z3minDv2_jS_", glsl::ExtInst::ExtInstUMin) |
| 5238 | .Case("_Z3minDv3_jS_", glsl::ExtInst::ExtInstUMin) |
| 5239 | .Case("_Z3minDv4_jS_", glsl::ExtInst::ExtInstUMin) |
| 5240 | .Case("_Z3minff", glsl::ExtInst::ExtInstFMin) |
| 5241 | .Case("_Z3minDv2_fS_", glsl::ExtInst::ExtInstFMin) |
| 5242 | .Case("_Z3minDv3_fS_", glsl::ExtInst::ExtInstFMin) |
| 5243 | .Case("_Z3minDv4_fS_", glsl::ExtInst::ExtInstFMin) |
| 5244 | .StartsWith("_Z4fmin", glsl::ExtInst::ExtInstFMin) |
| 5245 | .StartsWith("_Z7degrees", glsl::ExtInst::ExtInstDegrees) |
| 5246 | .StartsWith("_Z7radians", glsl::ExtInst::ExtInstRadians) |
| 5247 | .StartsWith("_Z3mix", glsl::ExtInst::ExtInstFMix) |
| 5248 | .StartsWith("_Z4acos", glsl::ExtInst::ExtInstAcos) |
| 5249 | .StartsWith("_Z5acosh", glsl::ExtInst::ExtInstAcosh) |
| 5250 | .StartsWith("_Z4asin", glsl::ExtInst::ExtInstAsin) |
| 5251 | .StartsWith("_Z5asinh", glsl::ExtInst::ExtInstAsinh) |
| 5252 | .StartsWith("_Z4atan", glsl::ExtInst::ExtInstAtan) |
| 5253 | .StartsWith("_Z5atan2", glsl::ExtInst::ExtInstAtan2) |
| 5254 | .StartsWith("_Z5atanh", glsl::ExtInst::ExtInstAtanh) |
| 5255 | .StartsWith("_Z4ceil", glsl::ExtInst::ExtInstCeil) |
| 5256 | .StartsWith("_Z3sin", glsl::ExtInst::ExtInstSin) |
| 5257 | .StartsWith("_Z4sinh", glsl::ExtInst::ExtInstSinh) |
| 5258 | .StartsWith("_Z8half_sin", glsl::ExtInst::ExtInstSin) |
| 5259 | .StartsWith("_Z10native_sin", glsl::ExtInst::ExtInstSin) |
| 5260 | .StartsWith("_Z3cos", glsl::ExtInst::ExtInstCos) |
| 5261 | .StartsWith("_Z4cosh", glsl::ExtInst::ExtInstCosh) |
| 5262 | .StartsWith("_Z8half_cos", glsl::ExtInst::ExtInstCos) |
| 5263 | .StartsWith("_Z10native_cos", glsl::ExtInst::ExtInstCos) |
| 5264 | .StartsWith("_Z3tan", glsl::ExtInst::ExtInstTan) |
| 5265 | .StartsWith("_Z4tanh", glsl::ExtInst::ExtInstTanh) |
| 5266 | .StartsWith("_Z8half_tan", glsl::ExtInst::ExtInstTan) |
| 5267 | .StartsWith("_Z10native_tan", glsl::ExtInst::ExtInstTan) |
| 5268 | .StartsWith("_Z3exp", glsl::ExtInst::ExtInstExp) |
| 5269 | .StartsWith("_Z8half_exp", glsl::ExtInst::ExtInstExp) |
| 5270 | .StartsWith("_Z10native_exp", glsl::ExtInst::ExtInstExp) |
| 5271 | .StartsWith("_Z4exp2", glsl::ExtInst::ExtInstExp2) |
| 5272 | .StartsWith("_Z9half_exp2", glsl::ExtInst::ExtInstExp2) |
| 5273 | .StartsWith("_Z11native_exp2", glsl::ExtInst::ExtInstExp2) |
| 5274 | .StartsWith("_Z3log", glsl::ExtInst::ExtInstLog) |
| 5275 | .StartsWith("_Z8half_log", glsl::ExtInst::ExtInstLog) |
| 5276 | .StartsWith("_Z10native_log", glsl::ExtInst::ExtInstLog) |
| 5277 | .StartsWith("_Z4log2", glsl::ExtInst::ExtInstLog2) |
| 5278 | .StartsWith("_Z9half_log2", glsl::ExtInst::ExtInstLog2) |
| 5279 | .StartsWith("_Z11native_log2", glsl::ExtInst::ExtInstLog2) |
| 5280 | .StartsWith("_Z4fabs", glsl::ExtInst::ExtInstFAbs) |
| 5281 | .StartsWith("_Z5floor", glsl::ExtInst::ExtInstFloor) |
| 5282 | .StartsWith("_Z5ldexp", glsl::ExtInst::ExtInstLdexp) |
| 5283 | .StartsWith("_Z3pow", glsl::ExtInst::ExtInstPow) |
| 5284 | .StartsWith("_Z4powr", glsl::ExtInst::ExtInstPow) |
| 5285 | .StartsWith("_Z9half_powr", glsl::ExtInst::ExtInstPow) |
| 5286 | .StartsWith("_Z11native_powr", glsl::ExtInst::ExtInstPow) |
| 5287 | .StartsWith("_Z5round", glsl::ExtInst::ExtInstRound) |
| 5288 | .StartsWith("_Z4sqrt", glsl::ExtInst::ExtInstSqrt) |
| 5289 | .StartsWith("_Z9half_sqrt", glsl::ExtInst::ExtInstSqrt) |
| 5290 | .StartsWith("_Z11native_sqrt", glsl::ExtInst::ExtInstSqrt) |
| 5291 | .StartsWith("_Z5rsqrt", glsl::ExtInst::ExtInstInverseSqrt) |
| 5292 | .StartsWith("_Z10half_rsqrt", glsl::ExtInst::ExtInstInverseSqrt) |
| 5293 | .StartsWith("_Z12native_rsqrt", glsl::ExtInst::ExtInstInverseSqrt) |
| 5294 | .StartsWith("_Z5trunc", glsl::ExtInst::ExtInstTrunc) |
| 5295 | .StartsWith("_Z5frexp", glsl::ExtInst::ExtInstFrexp) |
| 5296 | .StartsWith("_Z4sign", glsl::ExtInst::ExtInstFSign) |
| 5297 | .StartsWith("_Z6length", glsl::ExtInst::ExtInstLength) |
| 5298 | .StartsWith("_Z8distance", glsl::ExtInst::ExtInstDistance) |
David Neto | e9a0351 | 2017-10-16 10:08:27 -0400 | [diff] [blame] | 5299 | .StartsWith("_Z4step", glsl::ExtInst::ExtInstStep) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5300 | .Case("_Z5crossDv3_fS_", glsl::ExtInst::ExtInstCross) |
| 5301 | .StartsWith("_Z9normalize", glsl::ExtInst::ExtInstNormalize) |
| 5302 | .StartsWith("llvm.fmuladd.", glsl::ExtInst::ExtInstFma) |
| 5303 | .Case("spirv.unpack.v2f16", glsl::ExtInst::ExtInstUnpackHalf2x16) |
| 5304 | .Case("spirv.pack.v2f16", glsl::ExtInst::ExtInstPackHalf2x16) |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 5305 | .Case("clspv.fract.f", glsl::ExtInst::ExtInstFract) |
| 5306 | .Case("clspv.fract.v2f", glsl::ExtInst::ExtInstFract) |
| 5307 | .Case("clspv.fract.v3f", glsl::ExtInst::ExtInstFract) |
| 5308 | .Case("clspv.fract.v4f", glsl::ExtInst::ExtInstFract) |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5309 | .Default(kGlslExtInstBad); |
| 5310 | } |
| 5311 | |
| 5312 | glsl::ExtInst SPIRVProducerPass::getIndirectExtInstEnum(StringRef Name) { |
| 5313 | // Check indirect cases. |
| 5314 | return StringSwitch<glsl::ExtInst>(Name) |
| 5315 | .StartsWith("_Z3clz", glsl::ExtInst::ExtInstFindUMsb) |
| 5316 | // Use exact match on float arg because these need a multiply |
| 5317 | // of a constant of the right floating point type. |
| 5318 | .Case("_Z6acospif", glsl::ExtInst::ExtInstAcos) |
| 5319 | .Case("_Z6acospiDv2_f", glsl::ExtInst::ExtInstAcos) |
| 5320 | .Case("_Z6acospiDv3_f", glsl::ExtInst::ExtInstAcos) |
| 5321 | .Case("_Z6acospiDv4_f", glsl::ExtInst::ExtInstAcos) |
| 5322 | .Case("_Z6asinpif", glsl::ExtInst::ExtInstAsin) |
| 5323 | .Case("_Z6asinpiDv2_f", glsl::ExtInst::ExtInstAsin) |
| 5324 | .Case("_Z6asinpiDv3_f", glsl::ExtInst::ExtInstAsin) |
| 5325 | .Case("_Z6asinpiDv4_f", glsl::ExtInst::ExtInstAsin) |
| 5326 | .Case("_Z7atan2piff", glsl::ExtInst::ExtInstAtan2) |
| 5327 | .Case("_Z7atan2piDv2_fS_", glsl::ExtInst::ExtInstAtan2) |
| 5328 | .Case("_Z7atan2piDv3_fS_", glsl::ExtInst::ExtInstAtan2) |
| 5329 | .Case("_Z7atan2piDv4_fS_", glsl::ExtInst::ExtInstAtan2) |
| 5330 | .Default(kGlslExtInstBad); |
| 5331 | } |
| 5332 | |
| 5333 | glsl::ExtInst SPIRVProducerPass::getDirectOrIndirectExtInstEnum(StringRef Name) { |
| 5334 | auto direct = getExtInstEnum(Name); |
| 5335 | if (direct != kGlslExtInstBad) |
| 5336 | return direct; |
| 5337 | return getIndirectExtInstEnum(Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | void SPIRVProducerPass::PrintResID(SPIRVInstruction *Inst) { |
| 5341 | out << "%" << Inst->getResultID(); |
| 5342 | } |
| 5343 | |
| 5344 | void SPIRVProducerPass::PrintOpcode(SPIRVInstruction *Inst) { |
| 5345 | spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode()); |
| 5346 | out << "\t" << spv::getOpName(Opcode); |
| 5347 | } |
| 5348 | |
| 5349 | void SPIRVProducerPass::PrintOperand(SPIRVOperand *Op) { |
| 5350 | SPIRVOperandType OpTy = Op->getType(); |
| 5351 | switch (OpTy) { |
| 5352 | default: { |
| 5353 | llvm_unreachable("Unsupported SPIRV Operand Type???"); |
| 5354 | break; |
| 5355 | } |
| 5356 | case SPIRVOperandType::NUMBERID: { |
| 5357 | out << "%" << Op->getNumID(); |
| 5358 | break; |
| 5359 | } |
| 5360 | case SPIRVOperandType::LITERAL_STRING: { |
| 5361 | out << "\"" << Op->getLiteralStr() << "\""; |
| 5362 | break; |
| 5363 | } |
| 5364 | case SPIRVOperandType::LITERAL_INTEGER: { |
| 5365 | // TODO: Handle LiteralNum carefully. |
| 5366 | for (auto Word : Op->getLiteralNum()) { |
| 5367 | out << Word; |
| 5368 | } |
| 5369 | break; |
| 5370 | } |
| 5371 | case SPIRVOperandType::LITERAL_FLOAT: { |
| 5372 | // TODO: Handle LiteralNum carefully. |
| 5373 | for (auto Word : Op->getLiteralNum()) { |
| 5374 | APFloat APF = APFloat(APFloat::IEEEsingle(), APInt(32, Word)); |
| 5375 | SmallString<8> Str; |
| 5376 | APF.toString(Str, 6, 2); |
| 5377 | out << Str; |
| 5378 | } |
| 5379 | break; |
| 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | |
| 5384 | void SPIRVProducerPass::PrintCapability(SPIRVOperand *Op) { |
| 5385 | spv::Capability Cap = static_cast<spv::Capability>(Op->getNumID()); |
| 5386 | out << spv::getCapabilityName(Cap); |
| 5387 | } |
| 5388 | |
| 5389 | void SPIRVProducerPass::PrintExtInst(SPIRVOperand *Op) { |
| 5390 | auto LiteralNum = Op->getLiteralNum(); |
| 5391 | glsl::ExtInst Ext = static_cast<glsl::ExtInst>(LiteralNum[0]); |
| 5392 | out << glsl::getExtInstName(Ext); |
| 5393 | } |
| 5394 | |
| 5395 | void SPIRVProducerPass::PrintAddrModel(SPIRVOperand *Op) { |
| 5396 | spv::AddressingModel AddrModel = |
| 5397 | static_cast<spv::AddressingModel>(Op->getNumID()); |
| 5398 | out << spv::getAddressingModelName(AddrModel); |
| 5399 | } |
| 5400 | |
| 5401 | void SPIRVProducerPass::PrintMemModel(SPIRVOperand *Op) { |
| 5402 | spv::MemoryModel MemModel = static_cast<spv::MemoryModel>(Op->getNumID()); |
| 5403 | out << spv::getMemoryModelName(MemModel); |
| 5404 | } |
| 5405 | |
| 5406 | void SPIRVProducerPass::PrintExecModel(SPIRVOperand *Op) { |
| 5407 | spv::ExecutionModel ExecModel = |
| 5408 | static_cast<spv::ExecutionModel>(Op->getNumID()); |
| 5409 | out << spv::getExecutionModelName(ExecModel); |
| 5410 | } |
| 5411 | |
| 5412 | void SPIRVProducerPass::PrintExecMode(SPIRVOperand *Op) { |
| 5413 | spv::ExecutionMode ExecMode = static_cast<spv::ExecutionMode>(Op->getNumID()); |
| 5414 | out << spv::getExecutionModeName(ExecMode); |
| 5415 | } |
| 5416 | |
| 5417 | void SPIRVProducerPass::PrintSourceLanguage(SPIRVOperand *Op) { |
| 5418 | spv::SourceLanguage SourceLang = static_cast<spv::SourceLanguage>(Op->getNumID()); |
| 5419 | out << spv::getSourceLanguageName(SourceLang); |
| 5420 | } |
| 5421 | |
| 5422 | void SPIRVProducerPass::PrintFuncCtrl(SPIRVOperand *Op) { |
| 5423 | spv::FunctionControlMask FuncCtrl = |
| 5424 | static_cast<spv::FunctionControlMask>(Op->getNumID()); |
| 5425 | out << spv::getFunctionControlName(FuncCtrl); |
| 5426 | } |
| 5427 | |
| 5428 | void SPIRVProducerPass::PrintStorageClass(SPIRVOperand *Op) { |
| 5429 | spv::StorageClass StClass = static_cast<spv::StorageClass>(Op->getNumID()); |
| 5430 | out << getStorageClassName(StClass); |
| 5431 | } |
| 5432 | |
| 5433 | void SPIRVProducerPass::PrintDecoration(SPIRVOperand *Op) { |
| 5434 | spv::Decoration Deco = static_cast<spv::Decoration>(Op->getNumID()); |
| 5435 | out << getDecorationName(Deco); |
| 5436 | } |
| 5437 | |
| 5438 | void SPIRVProducerPass::PrintBuiltIn(SPIRVOperand *Op) { |
| 5439 | spv::BuiltIn BIn = static_cast<spv::BuiltIn>(Op->getNumID()); |
| 5440 | out << getBuiltInName(BIn); |
| 5441 | } |
| 5442 | |
| 5443 | void SPIRVProducerPass::PrintSelectionControl(SPIRVOperand *Op) { |
| 5444 | spv::SelectionControlMask BIn = |
| 5445 | static_cast<spv::SelectionControlMask>(Op->getNumID()); |
| 5446 | out << getSelectionControlName(BIn); |
| 5447 | } |
| 5448 | |
| 5449 | void SPIRVProducerPass::PrintLoopControl(SPIRVOperand *Op) { |
| 5450 | spv::LoopControlMask BIn = static_cast<spv::LoopControlMask>(Op->getNumID()); |
| 5451 | out << getLoopControlName(BIn); |
| 5452 | } |
| 5453 | |
| 5454 | void SPIRVProducerPass::PrintDimensionality(SPIRVOperand *Op) { |
| 5455 | spv::Dim DIM = static_cast<spv::Dim>(Op->getNumID()); |
| 5456 | out << getDimName(DIM); |
| 5457 | } |
| 5458 | |
| 5459 | void SPIRVProducerPass::PrintImageFormat(SPIRVOperand *Op) { |
| 5460 | spv::ImageFormat Format = static_cast<spv::ImageFormat>(Op->getNumID()); |
| 5461 | out << getImageFormatName(Format); |
| 5462 | } |
| 5463 | |
| 5464 | void SPIRVProducerPass::PrintMemoryAccess(SPIRVOperand *Op) { |
| 5465 | out << spv::getMemoryAccessName( |
| 5466 | static_cast<spv::MemoryAccessMask>(Op->getNumID())); |
| 5467 | } |
| 5468 | |
| 5469 | void SPIRVProducerPass::PrintImageOperandsType(SPIRVOperand *Op) { |
| 5470 | auto LiteralNum = Op->getLiteralNum(); |
| 5471 | spv::ImageOperandsMask Type = |
| 5472 | static_cast<spv::ImageOperandsMask>(LiteralNum[0]); |
| 5473 | out << getImageOperandsName(Type); |
| 5474 | } |
| 5475 | |
| 5476 | void SPIRVProducerPass::WriteSPIRVAssembly() { |
| 5477 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 5478 | |
| 5479 | for (auto Inst : SPIRVInstList) { |
| 5480 | SPIRVOperandList Ops = Inst->getOperands(); |
| 5481 | spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode()); |
| 5482 | |
| 5483 | switch (Opcode) { |
| 5484 | default: { |
| 5485 | llvm_unreachable("Unsupported SPIRV instruction"); |
| 5486 | break; |
| 5487 | } |
| 5488 | case spv::OpCapability: { |
| 5489 | // Ops[0] = Capability |
| 5490 | PrintOpcode(Inst); |
| 5491 | out << " "; |
| 5492 | PrintCapability(Ops[0]); |
| 5493 | out << "\n"; |
| 5494 | break; |
| 5495 | } |
| 5496 | case spv::OpMemoryModel: { |
| 5497 | // Ops[0] = Addressing Model |
| 5498 | // Ops[1] = Memory Model |
| 5499 | PrintOpcode(Inst); |
| 5500 | out << " "; |
| 5501 | PrintAddrModel(Ops[0]); |
| 5502 | out << " "; |
| 5503 | PrintMemModel(Ops[1]); |
| 5504 | out << "\n"; |
| 5505 | break; |
| 5506 | } |
| 5507 | case spv::OpEntryPoint: { |
| 5508 | // Ops[0] = Execution Model |
| 5509 | // Ops[1] = EntryPoint ID |
| 5510 | // Ops[2] = Name (Literal String) |
| 5511 | // Ops[3] ... Ops[n] = Interface ID |
| 5512 | PrintOpcode(Inst); |
| 5513 | out << " "; |
| 5514 | PrintExecModel(Ops[0]); |
| 5515 | for (uint32_t i = 1; i < Ops.size(); i++) { |
| 5516 | out << " "; |
| 5517 | PrintOperand(Ops[i]); |
| 5518 | } |
| 5519 | out << "\n"; |
| 5520 | break; |
| 5521 | } |
| 5522 | case spv::OpExecutionMode: { |
| 5523 | // Ops[0] = Entry Point ID |
| 5524 | // Ops[1] = Execution Mode |
| 5525 | // Ops[2] ... Ops[n] = Optional literals according to Execution Mode |
| 5526 | PrintOpcode(Inst); |
| 5527 | out << " "; |
| 5528 | PrintOperand(Ops[0]); |
| 5529 | out << " "; |
| 5530 | PrintExecMode(Ops[1]); |
| 5531 | for (uint32_t i = 2; i < Ops.size(); i++) { |
| 5532 | out << " "; |
| 5533 | PrintOperand(Ops[i]); |
| 5534 | } |
| 5535 | out << "\n"; |
| 5536 | break; |
| 5537 | } |
| 5538 | case spv::OpSource: { |
| 5539 | // Ops[0] = SourceLanguage ID |
| 5540 | // Ops[1] = Version (LiteralNum) |
| 5541 | PrintOpcode(Inst); |
| 5542 | out << " "; |
| 5543 | PrintSourceLanguage(Ops[0]); |
| 5544 | out << " "; |
| 5545 | PrintOperand(Ops[1]); |
| 5546 | out << "\n"; |
| 5547 | break; |
| 5548 | } |
| 5549 | case spv::OpDecorate: { |
| 5550 | // Ops[0] = Target ID |
| 5551 | // Ops[1] = Decoration (Block or BufferBlock) |
| 5552 | // Ops[2] ... Ops[n] = Optional literals according to Decoration |
| 5553 | PrintOpcode(Inst); |
| 5554 | out << " "; |
| 5555 | PrintOperand(Ops[0]); |
| 5556 | out << " "; |
| 5557 | PrintDecoration(Ops[1]); |
| 5558 | // Handle BuiltIn OpDecorate specially. |
| 5559 | if (Ops[1]->getNumID() == spv::DecorationBuiltIn) { |
| 5560 | out << " "; |
| 5561 | PrintBuiltIn(Ops[2]); |
| 5562 | } else { |
| 5563 | for (uint32_t i = 2; i < Ops.size(); i++) { |
| 5564 | out << " "; |
| 5565 | PrintOperand(Ops[i]); |
| 5566 | } |
| 5567 | } |
| 5568 | out << "\n"; |
| 5569 | break; |
| 5570 | } |
| 5571 | case spv::OpMemberDecorate: { |
| 5572 | // Ops[0] = Structure Type ID |
| 5573 | // Ops[1] = Member Index(Literal Number) |
| 5574 | // Ops[2] = Decoration |
| 5575 | // Ops[3] ... Ops[n] = Optional literals according to Decoration |
| 5576 | PrintOpcode(Inst); |
| 5577 | out << " "; |
| 5578 | PrintOperand(Ops[0]); |
| 5579 | out << " "; |
| 5580 | PrintOperand(Ops[1]); |
| 5581 | out << " "; |
| 5582 | PrintDecoration(Ops[2]); |
| 5583 | for (uint32_t i = 3; i < Ops.size(); i++) { |
| 5584 | out << " "; |
| 5585 | PrintOperand(Ops[i]); |
| 5586 | } |
| 5587 | out << "\n"; |
| 5588 | break; |
| 5589 | } |
| 5590 | case spv::OpTypePointer: { |
| 5591 | // Ops[0] = Storage Class |
| 5592 | // Ops[1] = Element Type ID |
| 5593 | PrintResID(Inst); |
| 5594 | out << " = "; |
| 5595 | PrintOpcode(Inst); |
| 5596 | out << " "; |
| 5597 | PrintStorageClass(Ops[0]); |
| 5598 | out << " "; |
| 5599 | PrintOperand(Ops[1]); |
| 5600 | out << "\n"; |
| 5601 | break; |
| 5602 | } |
| 5603 | case spv::OpTypeImage: { |
| 5604 | // Ops[0] = Sampled Type ID |
| 5605 | // Ops[1] = Dim ID |
| 5606 | // Ops[2] = Depth (Literal Number) |
| 5607 | // Ops[3] = Arrayed (Literal Number) |
| 5608 | // Ops[4] = MS (Literal Number) |
| 5609 | // Ops[5] = Sampled (Literal Number) |
| 5610 | // Ops[6] = Image Format ID |
| 5611 | PrintResID(Inst); |
| 5612 | out << " = "; |
| 5613 | PrintOpcode(Inst); |
| 5614 | out << " "; |
| 5615 | PrintOperand(Ops[0]); |
| 5616 | out << " "; |
| 5617 | PrintDimensionality(Ops[1]); |
| 5618 | out << " "; |
| 5619 | PrintOperand(Ops[2]); |
| 5620 | out << " "; |
| 5621 | PrintOperand(Ops[3]); |
| 5622 | out << " "; |
| 5623 | PrintOperand(Ops[4]); |
| 5624 | out << " "; |
| 5625 | PrintOperand(Ops[5]); |
| 5626 | out << " "; |
| 5627 | PrintImageFormat(Ops[6]); |
| 5628 | out << "\n"; |
| 5629 | break; |
| 5630 | } |
| 5631 | case spv::OpFunction: { |
| 5632 | // Ops[0] : Result Type ID |
| 5633 | // Ops[1] : Function Control |
| 5634 | // Ops[2] : Function Type ID |
| 5635 | PrintResID(Inst); |
| 5636 | out << " = "; |
| 5637 | PrintOpcode(Inst); |
| 5638 | out << " "; |
| 5639 | PrintOperand(Ops[0]); |
| 5640 | out << " "; |
| 5641 | PrintFuncCtrl(Ops[1]); |
| 5642 | out << " "; |
| 5643 | PrintOperand(Ops[2]); |
| 5644 | out << "\n"; |
| 5645 | break; |
| 5646 | } |
| 5647 | case spv::OpSelectionMerge: { |
| 5648 | // Ops[0] = Merge Block ID |
| 5649 | // Ops[1] = Selection Control |
| 5650 | PrintOpcode(Inst); |
| 5651 | out << " "; |
| 5652 | PrintOperand(Ops[0]); |
| 5653 | out << " "; |
| 5654 | PrintSelectionControl(Ops[1]); |
| 5655 | out << "\n"; |
| 5656 | break; |
| 5657 | } |
| 5658 | case spv::OpLoopMerge: { |
| 5659 | // Ops[0] = Merge Block ID |
| 5660 | // Ops[1] = Continue Target ID |
| 5661 | // Ops[2] = Selection Control |
| 5662 | PrintOpcode(Inst); |
| 5663 | out << " "; |
| 5664 | PrintOperand(Ops[0]); |
| 5665 | out << " "; |
| 5666 | PrintOperand(Ops[1]); |
| 5667 | out << " "; |
| 5668 | PrintLoopControl(Ops[2]); |
| 5669 | out << "\n"; |
| 5670 | break; |
| 5671 | } |
| 5672 | case spv::OpImageSampleExplicitLod: { |
| 5673 | // Ops[0] = Result Type ID |
| 5674 | // Ops[1] = Sampled Image ID |
| 5675 | // Ops[2] = Coordinate ID |
| 5676 | // Ops[3] = Image Operands Type ID |
| 5677 | // Ops[4] ... Ops[n] = Operands ID |
| 5678 | PrintResID(Inst); |
| 5679 | out << " = "; |
| 5680 | PrintOpcode(Inst); |
| 5681 | for (uint32_t i = 0; i < 3; i++) { |
| 5682 | out << " "; |
| 5683 | PrintOperand(Ops[i]); |
| 5684 | } |
| 5685 | out << " "; |
| 5686 | PrintImageOperandsType(Ops[3]); |
| 5687 | for (uint32_t i = 4; i < Ops.size(); i++) { |
| 5688 | out << " "; |
| 5689 | PrintOperand(Ops[i]); |
| 5690 | } |
| 5691 | out << "\n"; |
| 5692 | break; |
| 5693 | } |
| 5694 | case spv::OpVariable: { |
| 5695 | // Ops[0] : Result Type ID |
| 5696 | // Ops[1] : Storage Class |
| 5697 | // Ops[2] ... Ops[n] = Initializer IDs |
| 5698 | PrintResID(Inst); |
| 5699 | out << " = "; |
| 5700 | PrintOpcode(Inst); |
| 5701 | out << " "; |
| 5702 | PrintOperand(Ops[0]); |
| 5703 | out << " "; |
| 5704 | PrintStorageClass(Ops[1]); |
| 5705 | for (uint32_t i = 2; i < Ops.size(); i++) { |
| 5706 | out << " "; |
| 5707 | PrintOperand(Ops[i]); |
| 5708 | } |
| 5709 | out << "\n"; |
| 5710 | break; |
| 5711 | } |
| 5712 | case spv::OpExtInst: { |
| 5713 | // Ops[0] = Result Type ID |
| 5714 | // Ops[1] = Set ID (OpExtInstImport ID) |
| 5715 | // Ops[2] = Instruction Number (Literal Number) |
| 5716 | // Ops[3] ... Ops[n] = Operand 1, ... , Operand n |
| 5717 | PrintResID(Inst); |
| 5718 | out << " = "; |
| 5719 | PrintOpcode(Inst); |
| 5720 | out << " "; |
| 5721 | PrintOperand(Ops[0]); |
| 5722 | out << " "; |
| 5723 | PrintOperand(Ops[1]); |
| 5724 | out << " "; |
| 5725 | PrintExtInst(Ops[2]); |
| 5726 | for (uint32_t i = 3; i < Ops.size(); i++) { |
| 5727 | out << " "; |
| 5728 | PrintOperand(Ops[i]); |
| 5729 | } |
| 5730 | out << "\n"; |
| 5731 | break; |
| 5732 | } |
| 5733 | case spv::OpCopyMemory: { |
| 5734 | // Ops[0] = Addressing Model |
| 5735 | // Ops[1] = Memory Model |
| 5736 | PrintOpcode(Inst); |
| 5737 | out << " "; |
| 5738 | PrintOperand(Ops[0]); |
| 5739 | out << " "; |
| 5740 | PrintOperand(Ops[1]); |
| 5741 | out << " "; |
| 5742 | PrintMemoryAccess(Ops[2]); |
| 5743 | out << " "; |
| 5744 | PrintOperand(Ops[3]); |
| 5745 | out << "\n"; |
| 5746 | break; |
| 5747 | } |
| 5748 | case spv::OpExtension: |
| 5749 | case spv::OpControlBarrier: |
| 5750 | case spv::OpMemoryBarrier: |
| 5751 | case spv::OpBranch: |
| 5752 | case spv::OpBranchConditional: |
| 5753 | case spv::OpStore: |
| 5754 | case spv::OpImageWrite: |
| 5755 | case spv::OpReturnValue: |
| 5756 | case spv::OpReturn: |
| 5757 | case spv::OpFunctionEnd: { |
| 5758 | PrintOpcode(Inst); |
| 5759 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5760 | out << " "; |
| 5761 | PrintOperand(Ops[i]); |
| 5762 | } |
| 5763 | out << "\n"; |
| 5764 | break; |
| 5765 | } |
| 5766 | case spv::OpExtInstImport: |
| 5767 | case spv::OpTypeRuntimeArray: |
| 5768 | case spv::OpTypeStruct: |
| 5769 | case spv::OpTypeSampler: |
| 5770 | case spv::OpTypeSampledImage: |
| 5771 | case spv::OpTypeInt: |
| 5772 | case spv::OpTypeFloat: |
| 5773 | case spv::OpTypeArray: |
| 5774 | case spv::OpTypeVector: |
| 5775 | case spv::OpTypeBool: |
| 5776 | case spv::OpTypeVoid: |
| 5777 | case spv::OpTypeFunction: |
| 5778 | case spv::OpFunctionParameter: |
| 5779 | case spv::OpLabel: |
| 5780 | case spv::OpPhi: |
| 5781 | case spv::OpLoad: |
| 5782 | case spv::OpSelect: |
| 5783 | case spv::OpAccessChain: |
| 5784 | case spv::OpPtrAccessChain: |
| 5785 | case spv::OpInBoundsAccessChain: |
| 5786 | case spv::OpUConvert: |
| 5787 | case spv::OpSConvert: |
| 5788 | case spv::OpConvertFToU: |
| 5789 | case spv::OpConvertFToS: |
| 5790 | case spv::OpConvertUToF: |
| 5791 | case spv::OpConvertSToF: |
| 5792 | case spv::OpFConvert: |
| 5793 | case spv::OpConvertPtrToU: |
| 5794 | case spv::OpConvertUToPtr: |
| 5795 | case spv::OpBitcast: |
| 5796 | case spv::OpIAdd: |
| 5797 | case spv::OpFAdd: |
| 5798 | case spv::OpISub: |
| 5799 | case spv::OpFSub: |
| 5800 | case spv::OpIMul: |
| 5801 | case spv::OpFMul: |
| 5802 | case spv::OpUDiv: |
| 5803 | case spv::OpSDiv: |
| 5804 | case spv::OpFDiv: |
| 5805 | case spv::OpUMod: |
| 5806 | case spv::OpSRem: |
| 5807 | case spv::OpFRem: |
| 5808 | case spv::OpBitwiseOr: |
| 5809 | case spv::OpBitwiseXor: |
| 5810 | case spv::OpBitwiseAnd: |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 5811 | case spv::OpNot: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5812 | case spv::OpShiftLeftLogical: |
| 5813 | case spv::OpShiftRightLogical: |
| 5814 | case spv::OpShiftRightArithmetic: |
| 5815 | case spv::OpBitCount: |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5816 | case spv::OpCompositeConstruct: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5817 | case spv::OpCompositeExtract: |
| 5818 | case spv::OpVectorExtractDynamic: |
| 5819 | case spv::OpCompositeInsert: |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 5820 | case spv::OpCopyObject: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5821 | case spv::OpVectorInsertDynamic: |
| 5822 | case spv::OpVectorShuffle: |
| 5823 | case spv::OpIEqual: |
| 5824 | case spv::OpINotEqual: |
| 5825 | case spv::OpUGreaterThan: |
| 5826 | case spv::OpUGreaterThanEqual: |
| 5827 | case spv::OpULessThan: |
| 5828 | case spv::OpULessThanEqual: |
| 5829 | case spv::OpSGreaterThan: |
| 5830 | case spv::OpSGreaterThanEqual: |
| 5831 | case spv::OpSLessThan: |
| 5832 | case spv::OpSLessThanEqual: |
| 5833 | case spv::OpFOrdEqual: |
| 5834 | case spv::OpFOrdGreaterThan: |
| 5835 | case spv::OpFOrdGreaterThanEqual: |
| 5836 | case spv::OpFOrdLessThan: |
| 5837 | case spv::OpFOrdLessThanEqual: |
| 5838 | case spv::OpFOrdNotEqual: |
| 5839 | case spv::OpFUnordEqual: |
| 5840 | case spv::OpFUnordGreaterThan: |
| 5841 | case spv::OpFUnordGreaterThanEqual: |
| 5842 | case spv::OpFUnordLessThan: |
| 5843 | case spv::OpFUnordLessThanEqual: |
| 5844 | case spv::OpFUnordNotEqual: |
| 5845 | case spv::OpSampledImage: |
| 5846 | case spv::OpFunctionCall: |
| 5847 | case spv::OpConstantTrue: |
| 5848 | case spv::OpConstantFalse: |
| 5849 | case spv::OpConstant: |
| 5850 | case spv::OpSpecConstant: |
| 5851 | case spv::OpConstantComposite: |
| 5852 | case spv::OpSpecConstantComposite: |
| 5853 | case spv::OpConstantNull: |
| 5854 | case spv::OpLogicalOr: |
| 5855 | case spv::OpLogicalAnd: |
| 5856 | case spv::OpLogicalNot: |
| 5857 | case spv::OpLogicalNotEqual: |
| 5858 | case spv::OpUndef: |
| 5859 | case spv::OpIsInf: |
| 5860 | case spv::OpIsNan: |
| 5861 | case spv::OpAny: |
| 5862 | case spv::OpAll: |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5863 | case spv::OpImageQuerySize: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5864 | case spv::OpAtomicIAdd: |
| 5865 | case spv::OpAtomicISub: |
| 5866 | case spv::OpAtomicExchange: |
| 5867 | case spv::OpAtomicIIncrement: |
| 5868 | case spv::OpAtomicIDecrement: |
| 5869 | case spv::OpAtomicCompareExchange: |
| 5870 | case spv::OpAtomicUMin: |
| 5871 | case spv::OpAtomicSMin: |
| 5872 | case spv::OpAtomicUMax: |
| 5873 | case spv::OpAtomicSMax: |
| 5874 | case spv::OpAtomicAnd: |
| 5875 | case spv::OpAtomicOr: |
| 5876 | case spv::OpAtomicXor: |
| 5877 | case spv::OpDot: { |
| 5878 | PrintResID(Inst); |
| 5879 | out << " = "; |
| 5880 | PrintOpcode(Inst); |
| 5881 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5882 | out << " "; |
| 5883 | PrintOperand(Ops[i]); |
| 5884 | } |
| 5885 | out << "\n"; |
| 5886 | break; |
| 5887 | } |
| 5888 | } |
| 5889 | } |
| 5890 | } |
| 5891 | |
| 5892 | void SPIRVProducerPass::WriteOneWord(uint32_t Word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 5893 | binaryOut->write(reinterpret_cast<const char *>(&Word), sizeof(uint32_t)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5894 | } |
| 5895 | |
| 5896 | void SPIRVProducerPass::WriteResultID(SPIRVInstruction *Inst) { |
| 5897 | WriteOneWord(Inst->getResultID()); |
| 5898 | } |
| 5899 | |
| 5900 | void SPIRVProducerPass::WriteWordCountAndOpcode(SPIRVInstruction *Inst) { |
| 5901 | // High 16 bit : Word Count |
| 5902 | // Low 16 bit : Opcode |
| 5903 | uint32_t Word = Inst->getOpcode(); |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 5904 | const uint32_t count = Inst->getWordCount(); |
| 5905 | if (count > 65535) { |
| 5906 | errs() << "Word count limit of 65535 exceeded: " << count << "\n"; |
| 5907 | llvm_unreachable("Word count too high"); |
| 5908 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5909 | Word |= Inst->getWordCount() << 16; |
| 5910 | WriteOneWord(Word); |
| 5911 | } |
| 5912 | |
| 5913 | void SPIRVProducerPass::WriteOperand(SPIRVOperand *Op) { |
| 5914 | SPIRVOperandType OpTy = Op->getType(); |
| 5915 | switch (OpTy) { |
| 5916 | default: { |
| 5917 | llvm_unreachable("Unsupported SPIRV Operand Type???"); |
| 5918 | break; |
| 5919 | } |
| 5920 | case SPIRVOperandType::NUMBERID: { |
| 5921 | WriteOneWord(Op->getNumID()); |
| 5922 | break; |
| 5923 | } |
| 5924 | case SPIRVOperandType::LITERAL_STRING: { |
| 5925 | std::string Str = Op->getLiteralStr(); |
| 5926 | const char *Data = Str.c_str(); |
| 5927 | size_t WordSize = Str.size() / 4; |
| 5928 | for (unsigned Idx = 0; Idx < WordSize; Idx++) { |
| 5929 | WriteOneWord(*reinterpret_cast<const uint32_t *>(&Data[4 * Idx])); |
| 5930 | } |
| 5931 | |
| 5932 | uint32_t Remainder = Str.size() % 4; |
| 5933 | uint32_t LastWord = 0; |
| 5934 | if (Remainder) { |
| 5935 | for (unsigned Idx = 0; Idx < Remainder; Idx++) { |
| 5936 | LastWord |= Data[4 * WordSize + Idx] << 8 * Idx; |
| 5937 | } |
| 5938 | } |
| 5939 | |
| 5940 | WriteOneWord(LastWord); |
| 5941 | break; |
| 5942 | } |
| 5943 | case SPIRVOperandType::LITERAL_INTEGER: |
| 5944 | case SPIRVOperandType::LITERAL_FLOAT: { |
| 5945 | auto LiteralNum = Op->getLiteralNum(); |
| 5946 | // TODO: Handle LiteranNum carefully. |
| 5947 | for (auto Word : LiteralNum) { |
| 5948 | WriteOneWord(Word); |
| 5949 | } |
| 5950 | break; |
| 5951 | } |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | void SPIRVProducerPass::WriteSPIRVBinary() { |
| 5956 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 5957 | |
| 5958 | for (auto Inst : SPIRVInstList) { |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5959 | SPIRVOperandList Ops{Inst->getOperands()}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5960 | spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode()); |
| 5961 | |
| 5962 | switch (Opcode) { |
| 5963 | default: { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5964 | errs() << "Unsupported SPIR-V instruction opcode " << int(Opcode) << "\n"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5965 | llvm_unreachable("Unsupported SPIRV instruction"); |
| 5966 | break; |
| 5967 | } |
| 5968 | case spv::OpCapability: |
| 5969 | case spv::OpExtension: |
| 5970 | case spv::OpMemoryModel: |
| 5971 | case spv::OpEntryPoint: |
| 5972 | case spv::OpExecutionMode: |
| 5973 | case spv::OpSource: |
| 5974 | case spv::OpDecorate: |
| 5975 | case spv::OpMemberDecorate: |
| 5976 | case spv::OpBranch: |
| 5977 | case spv::OpBranchConditional: |
| 5978 | case spv::OpSelectionMerge: |
| 5979 | case spv::OpLoopMerge: |
| 5980 | case spv::OpStore: |
| 5981 | case spv::OpImageWrite: |
| 5982 | case spv::OpReturnValue: |
| 5983 | case spv::OpControlBarrier: |
| 5984 | case spv::OpMemoryBarrier: |
| 5985 | case spv::OpReturn: |
| 5986 | case spv::OpFunctionEnd: |
| 5987 | case spv::OpCopyMemory: { |
| 5988 | WriteWordCountAndOpcode(Inst); |
| 5989 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5990 | WriteOperand(Ops[i]); |
| 5991 | } |
| 5992 | break; |
| 5993 | } |
| 5994 | case spv::OpTypeBool: |
| 5995 | case spv::OpTypeVoid: |
| 5996 | case spv::OpTypeSampler: |
| 5997 | case spv::OpLabel: |
| 5998 | case spv::OpExtInstImport: |
| 5999 | case spv::OpTypePointer: |
| 6000 | case spv::OpTypeRuntimeArray: |
| 6001 | case spv::OpTypeStruct: |
| 6002 | case spv::OpTypeImage: |
| 6003 | case spv::OpTypeSampledImage: |
| 6004 | case spv::OpTypeInt: |
| 6005 | case spv::OpTypeFloat: |
| 6006 | case spv::OpTypeArray: |
| 6007 | case spv::OpTypeVector: |
| 6008 | case spv::OpTypeFunction: { |
| 6009 | WriteWordCountAndOpcode(Inst); |
| 6010 | WriteResultID(Inst); |
| 6011 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 6012 | WriteOperand(Ops[i]); |
| 6013 | } |
| 6014 | break; |
| 6015 | } |
| 6016 | case spv::OpFunction: |
| 6017 | case spv::OpFunctionParameter: |
| 6018 | case spv::OpAccessChain: |
| 6019 | case spv::OpPtrAccessChain: |
| 6020 | case spv::OpInBoundsAccessChain: |
| 6021 | case spv::OpUConvert: |
| 6022 | case spv::OpSConvert: |
| 6023 | case spv::OpConvertFToU: |
| 6024 | case spv::OpConvertFToS: |
| 6025 | case spv::OpConvertUToF: |
| 6026 | case spv::OpConvertSToF: |
| 6027 | case spv::OpFConvert: |
| 6028 | case spv::OpConvertPtrToU: |
| 6029 | case spv::OpConvertUToPtr: |
| 6030 | case spv::OpBitcast: |
| 6031 | case spv::OpIAdd: |
| 6032 | case spv::OpFAdd: |
| 6033 | case spv::OpISub: |
| 6034 | case spv::OpFSub: |
| 6035 | case spv::OpIMul: |
| 6036 | case spv::OpFMul: |
| 6037 | case spv::OpUDiv: |
| 6038 | case spv::OpSDiv: |
| 6039 | case spv::OpFDiv: |
| 6040 | case spv::OpUMod: |
| 6041 | case spv::OpSRem: |
| 6042 | case spv::OpFRem: |
| 6043 | case spv::OpBitwiseOr: |
| 6044 | case spv::OpBitwiseXor: |
| 6045 | case spv::OpBitwiseAnd: |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 6046 | case spv::OpNot: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6047 | case spv::OpShiftLeftLogical: |
| 6048 | case spv::OpShiftRightLogical: |
| 6049 | case spv::OpShiftRightArithmetic: |
| 6050 | case spv::OpBitCount: |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 6051 | case spv::OpCompositeConstruct: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6052 | case spv::OpCompositeExtract: |
| 6053 | case spv::OpVectorExtractDynamic: |
| 6054 | case spv::OpCompositeInsert: |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 6055 | case spv::OpCopyObject: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6056 | case spv::OpVectorInsertDynamic: |
| 6057 | case spv::OpVectorShuffle: |
| 6058 | case spv::OpIEqual: |
| 6059 | case spv::OpINotEqual: |
| 6060 | case spv::OpUGreaterThan: |
| 6061 | case spv::OpUGreaterThanEqual: |
| 6062 | case spv::OpULessThan: |
| 6063 | case spv::OpULessThanEqual: |
| 6064 | case spv::OpSGreaterThan: |
| 6065 | case spv::OpSGreaterThanEqual: |
| 6066 | case spv::OpSLessThan: |
| 6067 | case spv::OpSLessThanEqual: |
| 6068 | case spv::OpFOrdEqual: |
| 6069 | case spv::OpFOrdGreaterThan: |
| 6070 | case spv::OpFOrdGreaterThanEqual: |
| 6071 | case spv::OpFOrdLessThan: |
| 6072 | case spv::OpFOrdLessThanEqual: |
| 6073 | case spv::OpFOrdNotEqual: |
| 6074 | case spv::OpFUnordEqual: |
| 6075 | case spv::OpFUnordGreaterThan: |
| 6076 | case spv::OpFUnordGreaterThanEqual: |
| 6077 | case spv::OpFUnordLessThan: |
| 6078 | case spv::OpFUnordLessThanEqual: |
| 6079 | case spv::OpFUnordNotEqual: |
| 6080 | case spv::OpExtInst: |
| 6081 | case spv::OpIsInf: |
| 6082 | case spv::OpIsNan: |
| 6083 | case spv::OpAny: |
| 6084 | case spv::OpAll: |
| 6085 | case spv::OpUndef: |
| 6086 | case spv::OpConstantNull: |
| 6087 | case spv::OpLogicalOr: |
| 6088 | case spv::OpLogicalAnd: |
| 6089 | case spv::OpLogicalNot: |
| 6090 | case spv::OpLogicalNotEqual: |
| 6091 | case spv::OpConstantComposite: |
| 6092 | case spv::OpSpecConstantComposite: |
| 6093 | case spv::OpConstantTrue: |
| 6094 | case spv::OpConstantFalse: |
| 6095 | case spv::OpConstant: |
| 6096 | case spv::OpSpecConstant: |
| 6097 | case spv::OpVariable: |
| 6098 | case spv::OpFunctionCall: |
| 6099 | case spv::OpSampledImage: |
| 6100 | case spv::OpImageSampleExplicitLod: |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 6101 | case spv::OpImageQuerySize: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6102 | case spv::OpSelect: |
| 6103 | case spv::OpPhi: |
| 6104 | case spv::OpLoad: |
| 6105 | case spv::OpAtomicIAdd: |
| 6106 | case spv::OpAtomicISub: |
| 6107 | case spv::OpAtomicExchange: |
| 6108 | case spv::OpAtomicIIncrement: |
| 6109 | case spv::OpAtomicIDecrement: |
| 6110 | case spv::OpAtomicCompareExchange: |
| 6111 | case spv::OpAtomicUMin: |
| 6112 | case spv::OpAtomicSMin: |
| 6113 | case spv::OpAtomicUMax: |
| 6114 | case spv::OpAtomicSMax: |
| 6115 | case spv::OpAtomicAnd: |
| 6116 | case spv::OpAtomicOr: |
| 6117 | case spv::OpAtomicXor: |
| 6118 | case spv::OpDot: { |
| 6119 | WriteWordCountAndOpcode(Inst); |
| 6120 | WriteOperand(Ops[0]); |
| 6121 | WriteResultID(Inst); |
| 6122 | for (uint32_t i = 1; i < Ops.size(); i++) { |
| 6123 | WriteOperand(Ops[i]); |
| 6124 | } |
| 6125 | break; |
| 6126 | } |
| 6127 | } |
| 6128 | } |
| 6129 | } |