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> |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <list> |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 23 | #include <memory> |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 24 | #include <set> |
| 25 | #include <sstream> |
| 26 | #include <string> |
| 27 | #include <tuple> |
| 28 | #include <unordered_set> |
| 29 | #include <utility> |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 30 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 31 | |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 32 | #include "llvm/ADT/StringSwitch.h" |
| 33 | #include "llvm/ADT/UniqueVector.h" |
| 34 | #include "llvm/Analysis/LoopInfo.h" |
| 35 | #include "llvm/IR/Constants.h" |
| 36 | #include "llvm/IR/Dominators.h" |
| 37 | #include "llvm/IR/Instructions.h" |
| 38 | #include "llvm/IR/Metadata.h" |
| 39 | #include "llvm/IR/Module.h" |
| 40 | #include "llvm/Pass.h" |
| 41 | #include "llvm/Support/CommandLine.h" |
| 42 | #include "llvm/Support/raw_ostream.h" |
| 43 | #include "llvm/Transforms/Utils/Cloning.h" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 44 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 45 | #include "spirv/1.0/spirv.hpp" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 46 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 47 | #include "clspv/AddressSpace.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 48 | #include "clspv/Option.h" |
| 49 | #include "clspv/Passes.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 50 | #include "clspv/spirv_c_strings.hpp" |
| 51 | #include "clspv/spirv_glsl.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 52 | |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 53 | #include "ArgKind.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 54 | #include "ConstantEmitter.h" |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 55 | #include "Constants.h" |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 56 | #include "DescriptorCounter.h" |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 57 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 58 | #if defined(_MSC_VER) |
| 59 | #pragma warning(pop) |
| 60 | #endif |
| 61 | |
| 62 | using namespace llvm; |
| 63 | using namespace clspv; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 64 | using namespace mdconst; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 65 | |
| 66 | namespace { |
David Neto | cd8ca5f | 2017-10-02 23:34:11 -0400 | [diff] [blame] | 67 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 68 | cl::opt<bool> ShowResourceVars("show-rv", cl::init(false), cl::Hidden, |
| 69 | cl::desc("Show resource variable creation")); |
| 70 | |
| 71 | // These hacks exist to help transition code generation algorithms |
| 72 | // without making huge noise in detailed test output. |
| 73 | const bool Hack_generate_runtime_array_stride_early = true; |
| 74 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 75 | // The value of 1/pi. This value is from MSDN |
| 76 | // https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx |
| 77 | const double kOneOverPi = 0.318309886183790671538; |
| 78 | const glsl::ExtInst kGlslExtInstBad = static_cast<glsl::ExtInst>(0); |
| 79 | |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 80 | const char* kCompositeConstructFunctionPrefix = "clspv.composite_construct."; |
| 81 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 82 | enum SPIRVOperandType { |
| 83 | NUMBERID, |
| 84 | LITERAL_INTEGER, |
| 85 | LITERAL_STRING, |
| 86 | LITERAL_FLOAT |
| 87 | }; |
| 88 | |
| 89 | struct SPIRVOperand { |
| 90 | explicit SPIRVOperand(SPIRVOperandType Ty, uint32_t Num) |
| 91 | : Type(Ty), LiteralNum(1, Num) {} |
| 92 | explicit SPIRVOperand(SPIRVOperandType Ty, const char *Str) |
| 93 | : Type(Ty), LiteralStr(Str) {} |
| 94 | explicit SPIRVOperand(SPIRVOperandType Ty, StringRef Str) |
| 95 | : Type(Ty), LiteralStr(Str) {} |
| 96 | explicit SPIRVOperand(SPIRVOperandType Ty, ArrayRef<uint32_t> NumVec) |
| 97 | : Type(Ty), LiteralNum(NumVec.begin(), NumVec.end()) {} |
| 98 | |
| 99 | SPIRVOperandType getType() { return Type; }; |
| 100 | uint32_t getNumID() { return LiteralNum[0]; }; |
| 101 | std::string getLiteralStr() { return LiteralStr; }; |
| 102 | ArrayRef<uint32_t> getLiteralNum() { return LiteralNum; }; |
| 103 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 104 | uint32_t GetNumWords() const { |
| 105 | switch (Type) { |
| 106 | case NUMBERID: |
| 107 | return 1; |
| 108 | case LITERAL_INTEGER: |
| 109 | case LITERAL_FLOAT: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 110 | return uint32_t(LiteralNum.size()); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 111 | case LITERAL_STRING: |
| 112 | // Account for the terminating null character. |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 113 | return uint32_t((LiteralStr.size() + 4) / 4); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 114 | } |
| 115 | llvm_unreachable("Unhandled case in SPIRVOperand::GetNumWords()"); |
| 116 | } |
| 117 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 118 | private: |
| 119 | SPIRVOperandType Type; |
| 120 | std::string LiteralStr; |
| 121 | SmallVector<uint32_t, 4> LiteralNum; |
| 122 | }; |
| 123 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 124 | class SPIRVOperandList { |
| 125 | public: |
| 126 | SPIRVOperandList() {} |
| 127 | SPIRVOperandList(const SPIRVOperandList& other) = delete; |
| 128 | SPIRVOperandList(SPIRVOperandList&& other) { |
| 129 | contents_ = std::move(other.contents_); |
| 130 | other.contents_.clear(); |
| 131 | } |
| 132 | SPIRVOperandList(ArrayRef<SPIRVOperand *> init) |
| 133 | : contents_(init.begin(), init.end()) {} |
| 134 | operator ArrayRef<SPIRVOperand *>() { return contents_; } |
| 135 | void push_back(SPIRVOperand *op) { contents_.push_back(op); } |
| 136 | void clear() { contents_.clear();} |
| 137 | size_t size() const { return contents_.size(); } |
| 138 | SPIRVOperand *&operator[](size_t i) { return contents_[i]; } |
| 139 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 140 | const SmallVector<SPIRVOperand *, 8> &getOperands() const { |
| 141 | return contents_; |
| 142 | } |
| 143 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 144 | private: |
| 145 | SmallVector<SPIRVOperand *,8> contents_; |
| 146 | }; |
| 147 | |
| 148 | SPIRVOperandList &operator<<(SPIRVOperandList &list, SPIRVOperand *elem) { |
| 149 | list.push_back(elem); |
| 150 | return list; |
| 151 | } |
| 152 | |
| 153 | SPIRVOperand* MkNum(uint32_t num) { |
| 154 | return new SPIRVOperand(LITERAL_INTEGER, num); |
| 155 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 156 | SPIRVOperand* MkInteger(ArrayRef<uint32_t> num_vec) { |
| 157 | return new SPIRVOperand(LITERAL_INTEGER, num_vec); |
| 158 | } |
| 159 | SPIRVOperand* MkFloat(ArrayRef<uint32_t> num_vec) { |
| 160 | return new SPIRVOperand(LITERAL_FLOAT, num_vec); |
| 161 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 162 | SPIRVOperand* MkId(uint32_t id) { |
| 163 | return new SPIRVOperand(NUMBERID, id); |
| 164 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 165 | SPIRVOperand* MkString(StringRef str) { |
| 166 | return new SPIRVOperand(LITERAL_STRING, str); |
| 167 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 168 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 169 | struct SPIRVInstruction { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 170 | // Create an instruction with an opcode and no result ID, and with the given |
| 171 | // operands. This computes its own word count. |
| 172 | explicit SPIRVInstruction(spv::Op Opc, ArrayRef<SPIRVOperand *> Ops) |
| 173 | : WordCount(1), Opcode(static_cast<uint16_t>(Opc)), ResultID(0), |
| 174 | Operands(Ops.begin(), Ops.end()) { |
| 175 | for (auto *operand : Ops) { |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 176 | WordCount += uint16_t(operand->GetNumWords()); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | // Create an instruction with an opcode and a no-zero result ID, and |
| 180 | // with the given operands. This computes its own word count. |
| 181 | explicit SPIRVInstruction(spv::Op Opc, uint32_t ResID, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 182 | ArrayRef<SPIRVOperand *> Ops) |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 183 | : WordCount(2), Opcode(static_cast<uint16_t>(Opc)), ResultID(ResID), |
| 184 | Operands(Ops.begin(), Ops.end()) { |
| 185 | if (ResID == 0) { |
| 186 | llvm_unreachable("Result ID of 0 was provided"); |
| 187 | } |
| 188 | for (auto *operand : Ops) { |
| 189 | WordCount += operand->GetNumWords(); |
| 190 | } |
| 191 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 192 | |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 193 | uint32_t getWordCount() const { return WordCount; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 194 | uint16_t getOpcode() const { return Opcode; } |
| 195 | uint32_t getResultID() const { return ResultID; } |
| 196 | ArrayRef<SPIRVOperand *> getOperands() const { return Operands; } |
| 197 | |
| 198 | private: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 199 | uint32_t WordCount; // Check the 16-bit bound at code generation time. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 200 | uint16_t Opcode; |
| 201 | uint32_t ResultID; |
| 202 | SmallVector<SPIRVOperand *, 4> Operands; |
| 203 | }; |
| 204 | |
| 205 | struct SPIRVProducerPass final : public ModulePass { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 206 | typedef DenseMap<Type *, uint32_t> TypeMapType; |
| 207 | typedef UniqueVector<Type *> TypeList; |
| 208 | typedef DenseMap<Value *, uint32_t> ValueMapType; |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 209 | typedef UniqueVector<Value *> ValueList; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 210 | typedef std::vector<std::pair<Value *, uint32_t>> EntryPointVecType; |
| 211 | typedef std::list<SPIRVInstruction *> SPIRVInstructionList; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 212 | // A vector of tuples, each of which is: |
| 213 | // - the LLVM instruction that we will later generate SPIR-V code for |
| 214 | // - where the SPIR-V instruction should be inserted |
| 215 | // - the result ID of the SPIR-V instruction |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 216 | typedef std::vector< |
| 217 | std::tuple<Value *, SPIRVInstructionList::iterator, uint32_t>> |
| 218 | DeferredInstVecType; |
| 219 | typedef DenseMap<FunctionType *, std::pair<FunctionType *, uint32_t>> |
| 220 | GlobalConstFuncMapType; |
| 221 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 222 | explicit SPIRVProducerPass( |
| 223 | raw_pwrite_stream &out, raw_ostream &descriptor_map_out, |
| 224 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, bool outputAsm, |
| 225 | bool outputCInitList) |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 226 | : ModulePass(ID), samplerMap(samplerMap), out(out), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 227 | binaryTempOut(binaryTempUnderlyingVector), binaryOut(&out), |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 228 | descriptorMapOut(descriptor_map_out), outputAsm(outputAsm), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 229 | outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1), |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 230 | OpExtInstImportID(0), HasVariablePointers(false), SamplerTy(nullptr), |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 231 | WorkgroupSizeValueID(0), WorkgroupSizeVarID(0), |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 232 | max_local_spec_id_(0), constant_i32_zero_id_(0) {} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 233 | |
| 234 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 235 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 236 | AU.addRequired<LoopInfoWrapperPass>(); |
| 237 | } |
| 238 | |
| 239 | virtual bool runOnModule(Module &module) override; |
| 240 | |
| 241 | // output the SPIR-V header block |
| 242 | void outputHeader(); |
| 243 | |
| 244 | // patch the SPIR-V header block |
| 245 | void patchHeader(); |
| 246 | |
| 247 | uint32_t lookupType(Type *Ty) { |
| 248 | if (Ty->isPointerTy() && |
| 249 | (Ty->getPointerAddressSpace() != AddressSpace::UniformConstant)) { |
| 250 | auto PointeeTy = Ty->getPointerElementType(); |
| 251 | if (PointeeTy->isStructTy() && |
| 252 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 253 | Ty = PointeeTy; |
| 254 | } |
| 255 | } |
| 256 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 257 | auto where = TypeMap.find(Ty); |
| 258 | if (where == TypeMap.end()) { |
| 259 | if (Ty) { |
| 260 | errs() << "Unhandled type " << *Ty << "\n"; |
| 261 | } else { |
| 262 | errs() << "Unhandled type (null)\n"; |
| 263 | } |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 264 | llvm_unreachable("\nUnhandled type!"); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 265 | } |
| 266 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 267 | return where->second; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 268 | } |
| 269 | TypeMapType &getImageTypeMap() { return ImageTypeMap; } |
| 270 | TypeList &getTypeList() { return Types; }; |
| 271 | ValueList &getConstantList() { return Constants; }; |
| 272 | ValueMapType &getValueMap() { return ValueMap; } |
| 273 | ValueMapType &getAllocatedValueMap() { return AllocatedValueMap; } |
| 274 | SPIRVInstructionList &getSPIRVInstList() { return SPIRVInsts; }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 275 | EntryPointVecType &getEntryPointVec() { return EntryPointVec; }; |
| 276 | DeferredInstVecType &getDeferredInstVec() { return DeferredInstVec; }; |
| 277 | ValueList &getEntryPointInterfacesVec() { return EntryPointInterfacesVec; }; |
| 278 | uint32_t &getOpExtInstImportID() { return OpExtInstImportID; }; |
| 279 | std::vector<uint32_t> &getBuiltinDimVec() { return BuiltinDimensionVec; }; |
| 280 | bool hasVariablePointers() { return true; /* We use StorageBuffer everywhere */ }; |
| 281 | void setVariablePointers(bool Val) { HasVariablePointers = Val; }; |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 282 | ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() { return samplerMap; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 283 | GlobalConstFuncMapType &getGlobalConstFuncTypeMap() { |
| 284 | return GlobalConstFuncTypeMap; |
| 285 | } |
| 286 | SmallPtrSet<Value *, 16> &getGlobalConstArgSet() { |
| 287 | return GlobalConstArgumentSet; |
| 288 | } |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 289 | TypeList &getTypesNeedingArrayStride() { |
| 290 | return TypesNeedingArrayStride; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 291 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 292 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 293 | void GenerateLLVMIRInfo(Module &M, const DataLayout &DL); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 294 | // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will *not* |
| 295 | // be converted to a storage buffer, replace each such global variable with |
| 296 | // one in the storage class expecgted by SPIR-V. |
| 297 | void FindGlobalConstVars(Module &M, const DataLayout &DL); |
| 298 | // Populate ResourceVarInfoList, FunctionToResourceVarsMap, and |
| 299 | // ModuleOrderedResourceVars. |
| 300 | void FindResourceVars(Module &M, const DataLayout &DL); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 301 | void FindWorkgroupVars(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 302 | bool FindExtInst(Module &M); |
| 303 | void FindTypePerGlobalVar(GlobalVariable &GV); |
| 304 | void FindTypePerFunc(Function &F); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 305 | void FindTypesForSamplerMap(Module &M); |
| 306 | void FindTypesForResourceVars(Module &M); |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 307 | // Inserts |Ty| and relevant sub-types into the |Types| member, indicating that |
| 308 | // |Ty| and its subtypes will need a corresponding SPIR-V type. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 309 | void FindType(Type *Ty); |
| 310 | void FindConstantPerGlobalVar(GlobalVariable &GV); |
| 311 | void FindConstantPerFunc(Function &F); |
| 312 | void FindConstant(Value *V); |
| 313 | void GenerateExtInstImport(); |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 314 | // Generates instructions for SPIR-V types corresponding to the LLVM types |
| 315 | // saved in the |Types| member. A type follows its subtypes. IDs are |
| 316 | // allocated sequentially starting with the current value of nextID, and |
| 317 | // with a type following its subtypes. Also updates nextID to just beyond |
| 318 | // the last generated ID. |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 319 | void GenerateSPIRVTypes(LLVMContext& context, Module &module); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 320 | void GenerateSPIRVConstants(); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 321 | void GenerateModuleInfo(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 322 | void GenerateGlobalVar(GlobalVariable &GV); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 323 | void GenerateWorkgroupVars(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 324 | // Generate descriptor map entries for resource variables associated with |
| 325 | // arguments to F. |
| 326 | void GenerateDescriptorMapInfo(const DataLayout& DL, Function& F); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 327 | void GenerateSamplers(Module &M); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 328 | // Generate OpVariables for %clspv.resource.var.* calls. |
| 329 | void GenerateResourceVars(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 330 | void GenerateFuncPrologue(Function &F); |
| 331 | void GenerateFuncBody(Function &F); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 332 | void GenerateEntryPointInitialStores(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 333 | spv::Op GetSPIRVCmpOpcode(CmpInst *CmpI); |
| 334 | spv::Op GetSPIRVCastOpcode(Instruction &I); |
| 335 | spv::Op GetSPIRVBinaryOpcode(Instruction &I); |
| 336 | void GenerateInstruction(Instruction &I); |
| 337 | void GenerateFuncEpilogue(); |
| 338 | void HandleDeferredInstruction(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 339 | void HandleDeferredDecorations(const DataLayout& DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 340 | bool is4xi8vec(Type *Ty) const; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 341 | // Return the SPIR-V Id for 32-bit constant zero. The constant must already |
| 342 | // have been created. |
| 343 | uint32_t GetI32Zero(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 344 | spv::StorageClass GetStorageClass(unsigned AddrSpace) const; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 345 | spv::StorageClass GetStorageClassForArgKind(clspv::ArgKind arg_kind) const; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 346 | spv::BuiltIn GetBuiltin(StringRef globalVarName) const; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 347 | // Returns the GLSL extended instruction enum that the given function |
| 348 | // 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] | 349 | glsl::ExtInst getExtInstEnum(StringRef Name); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 350 | // Returns the GLSL extended instruction enum indirectly used by the given |
| 351 | // function. That is, to implement the given function, we use an extended |
| 352 | // instruction plus one more instruction. If none, then returns the 0 value, |
| 353 | // i.e. GLSLstd4580Bad. |
| 354 | glsl::ExtInst getIndirectExtInstEnum(StringRef Name); |
| 355 | // Returns the single GLSL extended instruction used directly or |
| 356 | // indirectly by the given function call. |
| 357 | glsl::ExtInst getDirectOrIndirectExtInstEnum(StringRef Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 358 | void PrintResID(SPIRVInstruction *Inst); |
| 359 | void PrintOpcode(SPIRVInstruction *Inst); |
| 360 | void PrintOperand(SPIRVOperand *Op); |
| 361 | void PrintCapability(SPIRVOperand *Op); |
| 362 | void PrintExtInst(SPIRVOperand *Op); |
| 363 | void PrintAddrModel(SPIRVOperand *Op); |
| 364 | void PrintMemModel(SPIRVOperand *Op); |
| 365 | void PrintExecModel(SPIRVOperand *Op); |
| 366 | void PrintExecMode(SPIRVOperand *Op); |
| 367 | void PrintSourceLanguage(SPIRVOperand *Op); |
| 368 | void PrintFuncCtrl(SPIRVOperand *Op); |
| 369 | void PrintStorageClass(SPIRVOperand *Op); |
| 370 | void PrintDecoration(SPIRVOperand *Op); |
| 371 | void PrintBuiltIn(SPIRVOperand *Op); |
| 372 | void PrintSelectionControl(SPIRVOperand *Op); |
| 373 | void PrintLoopControl(SPIRVOperand *Op); |
| 374 | void PrintDimensionality(SPIRVOperand *Op); |
| 375 | void PrintImageFormat(SPIRVOperand *Op); |
| 376 | void PrintMemoryAccess(SPIRVOperand *Op); |
| 377 | void PrintImageOperandsType(SPIRVOperand *Op); |
| 378 | void WriteSPIRVAssembly(); |
| 379 | void WriteOneWord(uint32_t Word); |
| 380 | void WriteResultID(SPIRVInstruction *Inst); |
| 381 | void WriteWordCountAndOpcode(SPIRVInstruction *Inst); |
| 382 | void WriteOperand(SPIRVOperand *Op); |
| 383 | void WriteSPIRVBinary(); |
| 384 | |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 385 | // Returns true if |type| is compatible with OpConstantNull. |
| 386 | bool IsTypeNullable(const Type* type) const; |
| 387 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 388 | // Populate UBO remapped type maps. |
| 389 | void PopulateUBOTypeMaps(Module &module); |
| 390 | |
| 391 | // Wrapped methods of DataLayout accessors. If |type| was remapped for UBOs, |
| 392 | // uses the internal map, otherwise it falls back on the data layout. |
| 393 | uint64_t GetTypeSizeInBits(Type *type, const DataLayout &DL); |
| 394 | uint64_t GetTypeStoreSize(Type *type, const DataLayout &DL); |
| 395 | uint64_t GetTypeAllocSize(Type *type, const DataLayout &DL); |
| 396 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 397 | private: |
| 398 | static char ID; |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 399 | ArrayRef<std::pair<unsigned, std::string>> samplerMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 400 | raw_pwrite_stream &out; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 401 | |
| 402 | // TODO(dneto): Wouldn't it be better to always just emit a binary, and then |
| 403 | // convert to other formats on demand? |
| 404 | |
| 405 | // When emitting a C initialization list, the WriteSPIRVBinary method |
| 406 | // will actually write its words to this vector via binaryTempOut. |
| 407 | SmallVector<char, 100> binaryTempUnderlyingVector; |
| 408 | raw_svector_ostream binaryTempOut; |
| 409 | |
| 410 | // Binary output writes to this stream, which might be |out| or |
| 411 | // |binaryTempOut|. It's the latter when we really want to write a C |
| 412 | // initializer list. |
| 413 | raw_pwrite_stream* binaryOut; |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 414 | raw_ostream &descriptorMapOut; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 415 | const bool outputAsm; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 416 | const bool outputCInitList; // If true, output look like {0x7023, ... , 5} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 417 | uint64_t patchBoundOffset; |
| 418 | uint32_t nextID; |
| 419 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 420 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 421 | TypeMapType TypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 422 | // Maps an LLVM image type to its SPIR-V ID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 423 | TypeMapType ImageTypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 424 | // 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] | 425 | TypeList Types; |
| 426 | ValueList Constants; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 427 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 428 | ValueMapType ValueMap; |
| 429 | ValueMapType AllocatedValueMap; |
| 430 | SPIRVInstructionList SPIRVInsts; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 431 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 432 | EntryPointVecType EntryPointVec; |
| 433 | DeferredInstVecType DeferredInstVec; |
| 434 | ValueList EntryPointInterfacesVec; |
| 435 | uint32_t OpExtInstImportID; |
| 436 | std::vector<uint32_t> BuiltinDimensionVec; |
| 437 | bool HasVariablePointers; |
| 438 | Type *SamplerTy; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 439 | DenseMap<unsigned,uint32_t> SamplerMapIndexToIDMap; |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 440 | |
| 441 | // 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] | 442 | // will map F's type to (G, index of the parameter), where in a first phase |
| 443 | // G is F's type. During FindTypePerFunc, G will be changed to F's type |
| 444 | // but replacing the pointer-to-constant parameter with |
| 445 | // pointer-to-ModuleScopePrivate. |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 446 | // TODO(dneto): This doesn't seem general enough? A function might have |
| 447 | // more than one such parameter. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 448 | GlobalConstFuncMapType GlobalConstFuncTypeMap; |
| 449 | SmallPtrSet<Value *, 16> GlobalConstArgumentSet; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 450 | // An ordered set of pointer types of Base arguments to OpPtrAccessChain, |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 451 | // or array types, and which point into transparent memory (StorageBuffer |
| 452 | // storage class). These will require an ArrayStride decoration. |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 453 | // See SPV_KHR_variable_pointers rev 13. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 454 | TypeList TypesNeedingArrayStride; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 455 | |
| 456 | // This is truly ugly, but works around what look like driver bugs. |
| 457 | // For get_local_size, an earlier part of the flow has created a module-scope |
| 458 | // variable in Private address space to hold the value for the workgroup |
| 459 | // size. Its intializer is a uint3 value marked as builtin WorkgroupSize. |
| 460 | // When this is present, save the IDs of the initializer value and variable |
| 461 | // in these two variables. We only ever do a vector load from it, and |
| 462 | // when we see one of those, substitute just the value of the intializer. |
| 463 | // 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] | 464 | // TODO(dneto): Remove this once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 465 | uint32_t WorkgroupSizeValueID; |
| 466 | uint32_t WorkgroupSizeVarID; |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 467 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 468 | // Bookkeeping for mapping kernel arguments to resource variables. |
| 469 | struct ResourceVarInfo { |
| 470 | ResourceVarInfo(int index_arg, unsigned set_arg, unsigned binding_arg, |
| 471 | Function *fn, clspv::ArgKind arg_kind_arg) |
| 472 | : index(index_arg), descriptor_set(set_arg), binding(binding_arg), |
| 473 | var_fn(fn), arg_kind(arg_kind_arg), |
| 474 | addr_space(fn->getReturnType()->getPointerAddressSpace()) {} |
| 475 | const int index; // Index into ResourceVarInfoList |
| 476 | const unsigned descriptor_set; |
| 477 | const unsigned binding; |
| 478 | Function *const var_fn; // The @clspv.resource.var.* function. |
| 479 | const clspv::ArgKind arg_kind; |
| 480 | const unsigned addr_space; // The LLVM address space |
| 481 | // The SPIR-V ID of the OpVariable. Not populated at construction time. |
| 482 | uint32_t var_id = 0; |
| 483 | }; |
| 484 | // A list of resource var info. Each one correponds to a module-scope |
| 485 | // resource variable we will have to create. Resource var indices are |
| 486 | // indices into this vector. |
| 487 | SmallVector<std::unique_ptr<ResourceVarInfo>, 8> ResourceVarInfoList; |
| 488 | // This is a vector of pointers of all the resource vars, but ordered by |
| 489 | // kernel function, and then by argument. |
| 490 | UniqueVector<ResourceVarInfo*> ModuleOrderedResourceVars; |
| 491 | // Map a function to the ordered list of resource variables it uses, one for |
| 492 | // each argument. If an argument does not use a resource variable, it |
| 493 | // will have a null pointer entry. |
| 494 | using FunctionToResourceVarsMapType = |
| 495 | DenseMap<Function *, SmallVector<ResourceVarInfo *, 8>>; |
| 496 | FunctionToResourceVarsMapType FunctionToResourceVarsMap; |
| 497 | |
| 498 | // What LLVM types map to SPIR-V types needing layout? These are the |
| 499 | // arrays and structures supporting storage buffers and uniform buffers. |
| 500 | TypeList TypesNeedingLayout; |
| 501 | // What LLVM struct types map to a SPIR-V struct type with Block decoration? |
| 502 | UniqueVector<StructType *> StructTypesNeedingBlock; |
| 503 | // For a call that represents a load from an opaque type (samplers, images), |
| 504 | // map it to the variable id it should load from. |
| 505 | DenseMap<CallInst *, uint32_t> ResourceVarDeferredLoadCalls; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 506 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 507 | // One larger than the maximum used SpecId for pointer-to-local arguments. |
| 508 | int max_local_spec_id_; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 509 | // An ordered list of the kernel arguments of type pointer-to-local. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 510 | using LocalArgList = SmallVector<Argument*, 8>; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 511 | LocalArgList LocalArgs; |
| 512 | // Information about a pointer-to-local argument. |
| 513 | struct LocalArgInfo { |
| 514 | // The SPIR-V ID of the array variable. |
| 515 | uint32_t variable_id; |
| 516 | // The element type of the |
| 517 | Type* elem_type; |
| 518 | // The ID of the array type. |
| 519 | uint32_t array_size_id; |
| 520 | // The ID of the array type. |
| 521 | uint32_t array_type_id; |
| 522 | // The ID of the pointer to the array type. |
| 523 | uint32_t ptr_array_type_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 524 | // The specialization constant ID of the array size. |
| 525 | int spec_id; |
| 526 | }; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 527 | // A mapping from Argument to its assigned SpecId. |
| 528 | DenseMap<const Argument*, int> LocalArgSpecIds; |
| 529 | // A mapping from SpecId to its LocalArgInfo. |
| 530 | DenseMap<int, LocalArgInfo> LocalSpecIdInfoMap; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 531 | // A mapping from a remapped type to its real offsets. |
| 532 | DenseMap<Type*, std::vector<uint32_t>> RemappedUBOTypeOffsets; |
| 533 | // A mapping from a remapped type to its real sizes. |
| 534 | DenseMap<Type*, std::tuple<uint64_t, uint64_t, uint64_t>> RemappedUBOTypeSizes; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 535 | |
| 536 | // The ID of 32-bit integer zero constant. This is only valid after |
| 537 | // GenerateSPIRVConstants has run. |
| 538 | uint32_t constant_i32_zero_id_; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 539 | }; |
| 540 | |
| 541 | char SPIRVProducerPass::ID; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 542 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | namespace clspv { |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 546 | ModulePass * |
| 547 | createSPIRVProducerPass(raw_pwrite_stream &out, raw_ostream &descriptor_map_out, |
| 548 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
| 549 | bool outputAsm, bool outputCInitList) { |
| 550 | return new SPIRVProducerPass(out, descriptor_map_out, samplerMap, outputAsm, |
| 551 | outputCInitList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 552 | } |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 553 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 554 | |
| 555 | bool SPIRVProducerPass::runOnModule(Module &module) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 556 | binaryOut = outputCInitList ? &binaryTempOut : &out; |
| 557 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 558 | constant_i32_zero_id_ = 0; // Reset, for the benefit of validity checks. |
| 559 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 560 | PopulateUBOTypeMaps(module); |
| 561 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 562 | // SPIR-V always begins with its header information |
| 563 | outputHeader(); |
| 564 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 565 | const DataLayout &DL = module.getDataLayout(); |
| 566 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 567 | // Gather information from the LLVM IR that we require. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 568 | GenerateLLVMIRInfo(module, DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 569 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 570 | // Collect information on global variables too. |
| 571 | for (GlobalVariable &GV : module.globals()) { |
| 572 | // If the GV is one of our special __spirv_* variables, remove the |
| 573 | // initializer as it was only placed there to force LLVM to not throw the |
| 574 | // value away. |
| 575 | if (GV.getName().startswith("__spirv_")) { |
| 576 | GV.setInitializer(nullptr); |
| 577 | } |
| 578 | |
| 579 | // Collect types' information from global variable. |
| 580 | FindTypePerGlobalVar(GV); |
| 581 | |
| 582 | // Collect constant information from global variable. |
| 583 | FindConstantPerGlobalVar(GV); |
| 584 | |
| 585 | // If the variable is an input, entry points need to know about it. |
| 586 | if (AddressSpace::Input == GV.getType()->getPointerAddressSpace()) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 587 | getEntryPointInterfacesVec().insert(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | // If there are extended instructions, generate OpExtInstImport. |
| 592 | if (FindExtInst(module)) { |
| 593 | GenerateExtInstImport(); |
| 594 | } |
| 595 | |
| 596 | // Generate SPIRV instructions for types. |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 597 | GenerateSPIRVTypes(module.getContext(), module); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 598 | |
| 599 | // Generate SPIRV constants. |
| 600 | GenerateSPIRVConstants(); |
| 601 | |
| 602 | // If we have a sampler map, we might have literal samplers to generate. |
| 603 | if (0 < getSamplerMap().size()) { |
| 604 | GenerateSamplers(module); |
| 605 | } |
| 606 | |
| 607 | // Generate SPIRV variables. |
| 608 | for (GlobalVariable &GV : module.globals()) { |
| 609 | GenerateGlobalVar(GV); |
| 610 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 611 | GenerateResourceVars(module); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 612 | GenerateWorkgroupVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 613 | |
| 614 | // Generate SPIRV instructions for each function. |
| 615 | for (Function &F : module) { |
| 616 | if (F.isDeclaration()) { |
| 617 | continue; |
| 618 | } |
| 619 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 620 | GenerateDescriptorMapInfo(DL, F); |
| 621 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 622 | // Generate Function Prologue. |
| 623 | GenerateFuncPrologue(F); |
| 624 | |
| 625 | // Generate SPIRV instructions for function body. |
| 626 | GenerateFuncBody(F); |
| 627 | |
| 628 | // Generate Function Epilogue. |
| 629 | GenerateFuncEpilogue(); |
| 630 | } |
| 631 | |
| 632 | HandleDeferredInstruction(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 633 | HandleDeferredDecorations(DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 634 | |
| 635 | // Generate SPIRV module information. |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 636 | GenerateModuleInfo(module); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 637 | |
| 638 | if (outputAsm) { |
| 639 | WriteSPIRVAssembly(); |
| 640 | } else { |
| 641 | WriteSPIRVBinary(); |
| 642 | } |
| 643 | |
| 644 | // We need to patch the SPIR-V header to set bound correctly. |
| 645 | patchHeader(); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 646 | |
| 647 | if (outputCInitList) { |
| 648 | bool first = true; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 649 | std::ostringstream os; |
| 650 | |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 651 | auto emit_word = [&os, &first](uint32_t word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 652 | if (!first) |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 653 | os << ",\n"; |
| 654 | os << word; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 655 | first = false; |
| 656 | }; |
| 657 | |
| 658 | os << "{"; |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 659 | const std::string str(binaryTempOut.str()); |
| 660 | for (unsigned i = 0; i < str.size(); i += 4) { |
| 661 | const uint32_t a = static_cast<unsigned char>(str[i]); |
| 662 | const uint32_t b = static_cast<unsigned char>(str[i + 1]); |
| 663 | const uint32_t c = static_cast<unsigned char>(str[i + 2]); |
| 664 | const uint32_t d = static_cast<unsigned char>(str[i + 3]); |
| 665 | emit_word(a | (b << 8) | (c << 16) | (d << 24)); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 666 | } |
| 667 | os << "}\n"; |
| 668 | out << os.str(); |
| 669 | } |
| 670 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | |
| 674 | void SPIRVProducerPass::outputHeader() { |
| 675 | if (outputAsm) { |
| 676 | // for ASM output the header goes into 5 comments at the beginning of the |
| 677 | // file |
| 678 | out << "; SPIR-V\n"; |
| 679 | |
| 680 | // the major version number is in the 2nd highest byte |
| 681 | const uint32_t major = (spv::Version >> 16) & 0xFF; |
| 682 | |
| 683 | // the minor version number is in the 2nd lowest byte |
| 684 | const uint32_t minor = (spv::Version >> 8) & 0xFF; |
| 685 | out << "; Version: " << major << "." << minor << "\n"; |
| 686 | |
| 687 | // use Codeplay's vendor ID |
| 688 | out << "; Generator: Codeplay; 0\n"; |
| 689 | |
| 690 | out << "; Bound: "; |
| 691 | |
| 692 | // we record where we need to come back to and patch in the bound value |
| 693 | patchBoundOffset = out.tell(); |
| 694 | |
| 695 | // output one space per digit for the max size of a 32 bit unsigned integer |
| 696 | // (which is the maximum ID we could possibly be using) |
| 697 | for (uint32_t i = std::numeric_limits<uint32_t>::max(); 0 != i; i /= 10) { |
| 698 | out << " "; |
| 699 | } |
| 700 | |
| 701 | out << "\n"; |
| 702 | |
| 703 | out << "; Schema: 0\n"; |
| 704 | } else { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 705 | binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber), |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 706 | sizeof(spv::MagicNumber)); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 707 | binaryOut->write(reinterpret_cast<const char *>(&spv::Version), |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 708 | sizeof(spv::Version)); |
| 709 | |
| 710 | // use Codeplay's vendor ID |
| 711 | const uint32_t vendor = 3 << 16; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 712 | binaryOut->write(reinterpret_cast<const char *>(&vendor), sizeof(vendor)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 713 | |
| 714 | // 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] | 715 | patchBoundOffset = binaryOut->tell(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 716 | |
| 717 | // output a bad bound for now |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 718 | binaryOut->write(reinterpret_cast<const char *>(&nextID), sizeof(nextID)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 719 | |
| 720 | // output the schema (reserved for use and must be 0) |
| 721 | const uint32_t schema = 0; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 722 | binaryOut->write(reinterpret_cast<const char *>(&schema), sizeof(schema)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | void SPIRVProducerPass::patchHeader() { |
| 727 | if (outputAsm) { |
| 728 | // get the string representation of the max bound used (nextID will be the |
| 729 | // max ID used) |
| 730 | auto asString = std::to_string(nextID); |
| 731 | out.pwrite(asString.c_str(), asString.size(), patchBoundOffset); |
| 732 | } else { |
| 733 | // for a binary we just write the value of nextID over bound |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 734 | binaryOut->pwrite(reinterpret_cast<char *>(&nextID), sizeof(nextID), |
| 735 | patchBoundOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 739 | void SPIRVProducerPass::GenerateLLVMIRInfo(Module &M, const DataLayout &DL) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 740 | // This function generates LLVM IR for function such as global variable for |
| 741 | // argument, constant and pointer type for argument access. These information |
| 742 | // is artificial one because we need Vulkan SPIR-V output. This function is |
| 743 | // executed ahead of FindType and FindConstant. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 744 | LLVMContext &Context = M.getContext(); |
| 745 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 746 | FindGlobalConstVars(M, DL); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 747 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 748 | FindResourceVars(M, DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 749 | |
| 750 | bool HasWorkGroupBuiltin = false; |
| 751 | for (GlobalVariable &GV : M.globals()) { |
| 752 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 753 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 754 | HasWorkGroupBuiltin = true; |
| 755 | } |
| 756 | } |
| 757 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 758 | FindTypesForSamplerMap(M); |
| 759 | FindTypesForResourceVars(M); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 760 | FindWorkgroupVars(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 761 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 762 | // TODO(dneto): Delete the next 3 vars. |
| 763 | |
| 764 | //#error "remove arg handling from this code" |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 765 | // Map kernel functions to their ordinal number in the compilation unit. |
| 766 | UniqueVector<Function*> KernelOrdinal; |
| 767 | |
| 768 | // Map the global variables created for kernel args to their creation |
| 769 | // order. |
| 770 | UniqueVector<GlobalVariable*> KernelArgVarOrdinal; |
| 771 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 772 | // For each kernel argument type, record the kernel arg global resource |
| 773 | // variables generated for that type, the function in which that variable |
| 774 | // was most recently used, and the binding number it took. For |
| 775 | // reproducibility, we track things by ordinal number (rather than pointer), |
| 776 | // and we use a std::set rather than DenseSet since std::set maintains an |
| 777 | // ordering. Each tuple is the ordinals of the kernel function, the binding |
| 778 | // number, and the ordinal of the kernal-arg-var. |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 779 | // |
| 780 | // This table lets us reuse module-scope StorageBuffer variables between |
| 781 | // different kernels. |
| 782 | DenseMap<Type *, std::set<std::tuple<unsigned, unsigned, unsigned>>> |
| 783 | GVarsForType; |
| 784 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 785 | // These function calls need a <2 x i32> as an intermediate result but not |
| 786 | // the final result. |
| 787 | std::unordered_set<std::string> NeedsIVec2{ |
| 788 | "_Z15get_image_width14ocl_image2d_ro", |
| 789 | "_Z15get_image_width14ocl_image2d_wo", |
| 790 | "_Z16get_image_height14ocl_image2d_ro", |
| 791 | "_Z16get_image_height14ocl_image2d_wo", |
| 792 | }; |
| 793 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 794 | for (Function &F : M) { |
| 795 | // Handle kernel function first. |
| 796 | if (F.isDeclaration() || F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 797 | continue; |
| 798 | } |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 799 | KernelOrdinal.insert(&F); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 800 | |
| 801 | for (BasicBlock &BB : F) { |
| 802 | for (Instruction &I : BB) { |
| 803 | if (I.getOpcode() == Instruction::ZExt || |
| 804 | I.getOpcode() == Instruction::SExt || |
| 805 | I.getOpcode() == Instruction::UIToFP) { |
| 806 | // If there is zext with i1 type, it will be changed to OpSelect. The |
| 807 | // OpSelect needs constant 0 and 1 so the constants are added here. |
| 808 | |
| 809 | auto OpTy = I.getOperand(0)->getType(); |
| 810 | |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 811 | if (OpTy->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 812 | if (I.getOpcode() == Instruction::ZExt) { |
| 813 | APInt One(32, 1); |
| 814 | FindConstant(Constant::getNullValue(I.getType())); |
| 815 | FindConstant(Constant::getIntegerValue(I.getType(), One)); |
| 816 | } else if (I.getOpcode() == Instruction::SExt) { |
| 817 | APInt MinusOne(32, UINT64_MAX, true); |
| 818 | FindConstant(Constant::getNullValue(I.getType())); |
| 819 | FindConstant(Constant::getIntegerValue(I.getType(), MinusOne)); |
| 820 | } else { |
| 821 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 822 | FindConstant(ConstantFP::get(Context, APFloat(1.0f))); |
| 823 | } |
| 824 | } |
| 825 | } else if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 826 | StringRef callee_name = Call->getCalledFunction()->getName(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 827 | |
| 828 | // Handle image type specially. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 829 | if (callee_name.equals( |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 830 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") || |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 831 | callee_name.equals( |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 832 | "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) { |
| 833 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 834 | Type *ImageTy = |
| 835 | Call->getArgOperand(0)->getType()->getPointerElementType(); |
| 836 | OpImageTypeMap[ImageTy] = 0; |
| 837 | |
| 838 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 839 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 840 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 841 | if (NeedsIVec2.find(callee_name) != NeedsIVec2.end()) { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 842 | FindType(VectorType::get(Type::getInt32Ty(Context), 2)); |
| 843 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 848 | if (const MDNode *MD = |
| 849 | dyn_cast<Function>(&F)->getMetadata("reqd_work_group_size")) { |
| 850 | // We generate constants if the WorkgroupSize builtin is being used. |
| 851 | if (HasWorkGroupBuiltin) { |
| 852 | // Collect constant information for work group size. |
| 853 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(0))); |
| 854 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(1))); |
| 855 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(2))); |
| 856 | } |
| 857 | } |
| 858 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 859 | // Collect types' information from function. |
| 860 | FindTypePerFunc(F); |
| 861 | |
| 862 | // Collect constant information from function. |
| 863 | FindConstantPerFunc(F); |
| 864 | } |
| 865 | |
| 866 | for (Function &F : M) { |
| 867 | // Handle non-kernel functions. |
| 868 | if (F.isDeclaration() || F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 869 | continue; |
| 870 | } |
| 871 | |
| 872 | for (BasicBlock &BB : F) { |
| 873 | for (Instruction &I : BB) { |
| 874 | if (I.getOpcode() == Instruction::ZExt || |
| 875 | I.getOpcode() == Instruction::SExt || |
| 876 | I.getOpcode() == Instruction::UIToFP) { |
| 877 | // If there is zext with i1 type, it will be changed to OpSelect. The |
| 878 | // OpSelect needs constant 0 and 1 so the constants are added here. |
| 879 | |
| 880 | auto OpTy = I.getOperand(0)->getType(); |
| 881 | |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 882 | if (OpTy->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 883 | if (I.getOpcode() == Instruction::ZExt) { |
| 884 | APInt One(32, 1); |
| 885 | FindConstant(Constant::getNullValue(I.getType())); |
| 886 | FindConstant(Constant::getIntegerValue(I.getType(), One)); |
| 887 | } else if (I.getOpcode() == Instruction::SExt) { |
| 888 | APInt MinusOne(32, UINT64_MAX, true); |
| 889 | FindConstant(Constant::getNullValue(I.getType())); |
| 890 | FindConstant(Constant::getIntegerValue(I.getType(), MinusOne)); |
| 891 | } else { |
| 892 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 893 | FindConstant(ConstantFP::get(Context, APFloat(1.0f))); |
| 894 | } |
| 895 | } |
| 896 | } else if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
| 897 | Function *Callee = Call->getCalledFunction(); |
| 898 | |
| 899 | // Handle image type specially. |
| 900 | if (Callee->getName().equals( |
| 901 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") || |
| 902 | Callee->getName().equals( |
| 903 | "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) { |
| 904 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 905 | Type *ImageTy = |
| 906 | Call->getArgOperand(0)->getType()->getPointerElementType(); |
| 907 | OpImageTypeMap[ImageTy] = 0; |
| 908 | |
| 909 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (M.getTypeByName("opencl.image2d_ro_t") || |
| 916 | M.getTypeByName("opencl.image2d_wo_t") || |
| 917 | M.getTypeByName("opencl.image3d_ro_t") || |
| 918 | M.getTypeByName("opencl.image3d_wo_t")) { |
| 919 | // Assume Image type's sampled type is float type. |
| 920 | FindType(Type::getFloatTy(Context)); |
| 921 | } |
| 922 | |
| 923 | // Collect types' information from function. |
| 924 | FindTypePerFunc(F); |
| 925 | |
| 926 | // Collect constant information from function. |
| 927 | FindConstantPerFunc(F); |
| 928 | } |
| 929 | } |
| 930 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 931 | void SPIRVProducerPass::FindGlobalConstVars(Module &M, const DataLayout &DL) { |
| 932 | SmallVector<GlobalVariable *, 8> GVList; |
| 933 | SmallVector<GlobalVariable *, 8> DeadGVList; |
| 934 | for (GlobalVariable &GV : M.globals()) { |
| 935 | if (GV.getType()->getAddressSpace() == AddressSpace::Constant) { |
| 936 | if (GV.use_empty()) { |
| 937 | DeadGVList.push_back(&GV); |
| 938 | } else { |
| 939 | GVList.push_back(&GV); |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | // Remove dead global __constant variables. |
| 945 | for (auto GV : DeadGVList) { |
| 946 | GV->eraseFromParent(); |
| 947 | } |
| 948 | DeadGVList.clear(); |
| 949 | |
| 950 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 951 | // For now, we only support a single storage buffer. |
| 952 | if (GVList.size() > 0) { |
| 953 | assert(GVList.size() == 1); |
| 954 | const auto *GV = GVList[0]; |
| 955 | const auto constants_byte_size = |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 956 | (GetTypeSizeInBits(GV->getInitializer()->getType(), DL)) / 8; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 957 | const size_t kConstantMaxSize = 65536; |
| 958 | if (constants_byte_size > kConstantMaxSize) { |
| 959 | outs() << "Max __constant capacity of " << kConstantMaxSize |
| 960 | << " bytes exceeded: " << constants_byte_size << " bytes used\n"; |
| 961 | llvm_unreachable("Max __constant capacity exceeded"); |
| 962 | } |
| 963 | } |
| 964 | } else { |
| 965 | // Change global constant variable's address space to ModuleScopePrivate. |
| 966 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 967 | for (auto GV : GVList) { |
| 968 | // Create new gv with ModuleScopePrivate address space. |
| 969 | Type *NewGVTy = GV->getType()->getPointerElementType(); |
| 970 | GlobalVariable *NewGV = new GlobalVariable( |
| 971 | M, NewGVTy, false, GV->getLinkage(), GV->getInitializer(), "", |
| 972 | nullptr, GV->getThreadLocalMode(), AddressSpace::ModuleScopePrivate); |
| 973 | NewGV->takeName(GV); |
| 974 | |
| 975 | const SmallVector<User *, 8> GVUsers(GV->user_begin(), GV->user_end()); |
| 976 | SmallVector<User *, 8> CandidateUsers; |
| 977 | |
| 978 | auto record_called_function_type_as_user = |
| 979 | [&GlobalConstFuncTyMap](Value *gv, CallInst *call) { |
| 980 | // Find argument index. |
| 981 | unsigned index = 0; |
| 982 | for (unsigned i = 0; i < call->getNumArgOperands(); i++) { |
| 983 | if (gv == call->getOperand(i)) { |
| 984 | // TODO(dneto): Should we break here? |
| 985 | index = i; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | // Record function type with global constant. |
| 990 | GlobalConstFuncTyMap[call->getFunctionType()] = |
| 991 | std::make_pair(call->getFunctionType(), index); |
| 992 | }; |
| 993 | |
| 994 | for (User *GVU : GVUsers) { |
| 995 | if (CallInst *Call = dyn_cast<CallInst>(GVU)) { |
| 996 | record_called_function_type_as_user(GV, Call); |
| 997 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(GVU)) { |
| 998 | // Check GEP users. |
| 999 | for (User *GEPU : GEP->users()) { |
| 1000 | if (CallInst *GEPCall = dyn_cast<CallInst>(GEPU)) { |
| 1001 | record_called_function_type_as_user(GEP, GEPCall); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | CandidateUsers.push_back(GVU); |
| 1007 | } |
| 1008 | |
| 1009 | for (User *U : CandidateUsers) { |
| 1010 | // Update users of gv with new gv. |
| 1011 | U->replaceUsesOfWith(GV, NewGV); |
| 1012 | } |
| 1013 | |
| 1014 | // Delete original gv. |
| 1015 | GV->eraseFromParent(); |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 1020 | void SPIRVProducerPass::FindResourceVars(Module &M, const DataLayout &) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1021 | ResourceVarInfoList.clear(); |
| 1022 | FunctionToResourceVarsMap.clear(); |
| 1023 | ModuleOrderedResourceVars.reset(); |
| 1024 | // Normally, there is one resource variable per clspv.resource.var.* |
| 1025 | // function, since that is unique'd by arg type and index. By design, |
| 1026 | // we can share these resource variables across kernels because all |
| 1027 | // kernels use the same descriptor set. |
| 1028 | // |
| 1029 | // But if the user requested distinct descriptor sets per kernel, then |
| 1030 | // the descriptor allocator has made different (set,binding) pairs for |
| 1031 | // the same (type,arg_index) pair. Since we can decorate a resource |
| 1032 | // variable with only exactly one DescriptorSet and Binding, we are |
| 1033 | // forced in this case to make distinct resource variables whenever |
| 1034 | // the same clspv.reource.var.X function is seen with disintct |
| 1035 | // (set,binding) values. |
| 1036 | const bool always_distinct_sets = |
| 1037 | clspv::Option::DistinctKernelDescriptorSets(); |
| 1038 | for (Function &F : M) { |
| 1039 | // Rely on the fact the resource var functions have a stable ordering |
| 1040 | // in the module. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1041 | if (F.getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1042 | // Find all calls to this function with distinct set and binding pairs. |
| 1043 | // Save them in ResourceVarInfoList. |
| 1044 | |
| 1045 | // Determine uniqueness of the (set,binding) pairs only withing this |
| 1046 | // one resource-var builtin function. |
| 1047 | using SetAndBinding = std::pair<unsigned, unsigned>; |
| 1048 | // Maps set and binding to the resource var info. |
| 1049 | DenseMap<SetAndBinding, ResourceVarInfo *> set_and_binding_map; |
| 1050 | bool first_use = true; |
| 1051 | for (auto &U : F.uses()) { |
| 1052 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 1053 | const auto set = unsigned( |
| 1054 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 1055 | const auto binding = unsigned( |
| 1056 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 1057 | const auto arg_kind = clspv::ArgKind( |
| 1058 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 1059 | const auto arg_index = unsigned( |
| 1060 | dyn_cast<ConstantInt>(call->getArgOperand(3))->getZExtValue()); |
| 1061 | |
| 1062 | // Find or make the resource var info for this combination. |
| 1063 | ResourceVarInfo *rv = nullptr; |
| 1064 | if (always_distinct_sets) { |
| 1065 | // Make a new resource var any time we see a different |
| 1066 | // (set,binding) pair. |
| 1067 | SetAndBinding key{set, binding}; |
| 1068 | auto where = set_and_binding_map.find(key); |
| 1069 | if (where == set_and_binding_map.end()) { |
| 1070 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
| 1071 | binding, &F, arg_kind); |
| 1072 | ResourceVarInfoList.emplace_back(rv); |
| 1073 | set_and_binding_map[key] = rv; |
| 1074 | } else { |
| 1075 | rv = where->second; |
| 1076 | } |
| 1077 | } else { |
| 1078 | // The default is to make exactly one resource for each |
| 1079 | // clspv.resource.var.* function. |
| 1080 | if (first_use) { |
| 1081 | first_use = false; |
| 1082 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
| 1083 | binding, &F, arg_kind); |
| 1084 | ResourceVarInfoList.emplace_back(rv); |
| 1085 | } else { |
| 1086 | rv = ResourceVarInfoList.back().get(); |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | // Now populate FunctionToResourceVarsMap. |
| 1091 | auto &mapping = |
| 1092 | FunctionToResourceVarsMap[call->getParent()->getParent()]; |
| 1093 | while (mapping.size() <= arg_index) { |
| 1094 | mapping.push_back(nullptr); |
| 1095 | } |
| 1096 | mapping[arg_index] = rv; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // Populate ModuleOrderedResourceVars. |
| 1103 | for (Function &F : M) { |
| 1104 | auto where = FunctionToResourceVarsMap.find(&F); |
| 1105 | if (where != FunctionToResourceVarsMap.end()) { |
| 1106 | for (auto &rv : where->second) { |
| 1107 | if (rv != nullptr) { |
| 1108 | ModuleOrderedResourceVars.insert(rv); |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | if (ShowResourceVars) { |
| 1114 | for (auto *info : ModuleOrderedResourceVars) { |
| 1115 | outs() << "MORV index " << info->index << " (" << info->descriptor_set |
| 1116 | << "," << info->binding << ") " << *(info->var_fn->getReturnType()) |
| 1117 | << "\n"; |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1122 | bool SPIRVProducerPass::FindExtInst(Module &M) { |
| 1123 | LLVMContext &Context = M.getContext(); |
| 1124 | bool HasExtInst = false; |
| 1125 | |
| 1126 | for (Function &F : M) { |
| 1127 | for (BasicBlock &BB : F) { |
| 1128 | for (Instruction &I : BB) { |
| 1129 | if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
| 1130 | Function *Callee = Call->getCalledFunction(); |
| 1131 | // Check whether this call is for extend instructions. |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1132 | auto callee_name = Callee->getName(); |
| 1133 | const glsl::ExtInst EInst = getExtInstEnum(callee_name); |
| 1134 | const glsl::ExtInst IndirectEInst = |
| 1135 | getIndirectExtInstEnum(callee_name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1136 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1137 | HasExtInst |= |
| 1138 | (EInst != kGlslExtInstBad) || (IndirectEInst != kGlslExtInstBad); |
| 1139 | |
| 1140 | if (IndirectEInst) { |
| 1141 | // Register extra constants if needed. |
| 1142 | |
| 1143 | // Registers a type and constant for computing the result of the |
| 1144 | // given instruction. If the result of the instruction is a vector, |
| 1145 | // then make a splat vector constant with the same number of |
| 1146 | // elements. |
| 1147 | auto register_constant = [this, &I](Constant *constant) { |
| 1148 | FindType(constant->getType()); |
| 1149 | FindConstant(constant); |
| 1150 | if (auto *vectorTy = dyn_cast<VectorType>(I.getType())) { |
| 1151 | // Register the splat vector of the value with the same |
| 1152 | // width as the result of the instruction. |
| 1153 | auto *vec_constant = ConstantVector::getSplat( |
| 1154 | static_cast<unsigned>(vectorTy->getNumElements()), |
| 1155 | constant); |
| 1156 | FindConstant(vec_constant); |
| 1157 | FindType(vec_constant->getType()); |
| 1158 | } |
| 1159 | }; |
| 1160 | switch (IndirectEInst) { |
| 1161 | case glsl::ExtInstFindUMsb: |
| 1162 | // clz needs OpExtInst and OpISub with constant 31, or splat |
| 1163 | // vector of 31. Add it to the constant list here. |
| 1164 | register_constant( |
| 1165 | ConstantInt::get(Type::getInt32Ty(Context), 31)); |
| 1166 | break; |
| 1167 | case glsl::ExtInstAcos: |
| 1168 | case glsl::ExtInstAsin: |
Kévin Petit | eb9f90a | 2018-09-29 12:29:34 +0100 | [diff] [blame] | 1169 | case glsl::ExtInstAtan: |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1170 | case glsl::ExtInstAtan2: |
| 1171 | // We need 1/pi for acospi, asinpi, atan2pi. |
| 1172 | register_constant( |
| 1173 | ConstantFP::get(Type::getFloatTy(Context), kOneOverPi)); |
| 1174 | break; |
| 1175 | default: |
| 1176 | assert(false && "internally inconsistent"); |
| 1177 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | return HasExtInst; |
| 1185 | } |
| 1186 | |
| 1187 | void SPIRVProducerPass::FindTypePerGlobalVar(GlobalVariable &GV) { |
| 1188 | // Investigate global variable's type. |
| 1189 | FindType(GV.getType()); |
| 1190 | } |
| 1191 | |
| 1192 | void SPIRVProducerPass::FindTypePerFunc(Function &F) { |
| 1193 | // Investigate function's type. |
| 1194 | FunctionType *FTy = F.getFunctionType(); |
| 1195 | |
| 1196 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 1197 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1198 | // Handle a regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1199 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 1200 | uint32_t GVCstArgIdx = GlobalConstFuncTypeMap[FTy].second; |
| 1201 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1202 | for (unsigned i = 0; i < FTy->getNumParams(); i++) { |
| 1203 | Type *ParamTy = FTy->getParamType(i); |
| 1204 | if (i == GVCstArgIdx) { |
| 1205 | Type *EleTy = ParamTy->getPointerElementType(); |
| 1206 | ParamTy = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1207 | } |
| 1208 | |
| 1209 | NewFuncParamTys.push_back(ParamTy); |
| 1210 | } |
| 1211 | |
| 1212 | FunctionType *NewFTy = |
| 1213 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1214 | GlobalConstFuncTyMap[FTy] = std::make_pair(NewFTy, GVCstArgIdx); |
| 1215 | FTy = NewFTy; |
| 1216 | } |
| 1217 | |
| 1218 | FindType(FTy); |
| 1219 | } else { |
| 1220 | // As kernel functions do not have parameters, create new function type and |
| 1221 | // add it to type map. |
| 1222 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1223 | FunctionType *NewFTy = |
| 1224 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1225 | FindType(NewFTy); |
| 1226 | } |
| 1227 | |
| 1228 | // Investigate instructions' type in function body. |
| 1229 | for (BasicBlock &BB : F) { |
| 1230 | for (Instruction &I : BB) { |
| 1231 | if (isa<ShuffleVectorInst>(I)) { |
| 1232 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1233 | // Ignore type for mask of shuffle vector instruction. |
| 1234 | if (i == 2) { |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | Value *Op = I.getOperand(i); |
| 1239 | if (!isa<MetadataAsValue>(Op)) { |
| 1240 | FindType(Op->getType()); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | FindType(I.getType()); |
| 1245 | continue; |
| 1246 | } |
| 1247 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1248 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 1249 | |
| 1250 | if (Call && Call->getCalledFunction()->getName().startswith( |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1251 | clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1252 | // This is a fake call representing access to a resource variable. |
| 1253 | // We handle that elsewhere. |
| 1254 | continue; |
| 1255 | } |
| 1256 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1257 | if (Call && Call->getCalledFunction()->getName().startswith( |
| 1258 | clspv::WorkgroupAccessorFunction())) { |
| 1259 | // This is a fake call representing access to a workgroup variable. |
| 1260 | // We handle that elsewhere. |
| 1261 | continue; |
| 1262 | } |
| 1263 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1264 | // Work through the operands of the instruction. |
| 1265 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1266 | Value *const Op = I.getOperand(i); |
| 1267 | // If any of the operands is a constant, find the type! |
| 1268 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1269 | FindType(Op->getType()); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | for (Use &Op : I.operands()) { |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 1274 | if (isa<CallInst>(&I)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1275 | // Avoid to check call instruction's type. |
| 1276 | break; |
| 1277 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1278 | if (CallInst *OpCall = dyn_cast<CallInst>(Op)) { |
| 1279 | if (OpCall && OpCall->getCalledFunction()->getName().startswith( |
| 1280 | clspv::WorkgroupAccessorFunction())) { |
| 1281 | // This is a fake call representing access to a workgroup variable. |
| 1282 | // We handle that elsewhere. |
| 1283 | continue; |
| 1284 | } |
| 1285 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1286 | if (!isa<MetadataAsValue>(&Op)) { |
| 1287 | FindType(Op->getType()); |
| 1288 | continue; |
| 1289 | } |
| 1290 | } |
| 1291 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1292 | // We don't want to track the type of this call as we are going to replace |
| 1293 | // it. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1294 | if (Call && ("clspv.sampler.var.literal" == |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1295 | Call->getCalledFunction()->getName())) { |
| 1296 | continue; |
| 1297 | } |
| 1298 | |
| 1299 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 1300 | // If gep's base operand has ModuleScopePrivate address space, make gep |
| 1301 | // return ModuleScopePrivate address space. |
| 1302 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate) { |
| 1303 | // Add pointer type with private address space for global constant to |
| 1304 | // type list. |
| 1305 | Type *EleTy = I.getType()->getPointerElementType(); |
| 1306 | Type *NewPTy = |
| 1307 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1308 | |
| 1309 | FindType(NewPTy); |
| 1310 | continue; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | FindType(I.getType()); |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1319 | void SPIRVProducerPass::FindTypesForSamplerMap(Module &M) { |
| 1320 | // If we are using a sampler map, find the type of the sampler. |
| 1321 | if (M.getFunction("clspv.sampler.var.literal") || |
| 1322 | 0 < getSamplerMap().size()) { |
| 1323 | auto SamplerStructTy = M.getTypeByName("opencl.sampler_t"); |
| 1324 | if (!SamplerStructTy) { |
| 1325 | SamplerStructTy = StructType::create(M.getContext(), "opencl.sampler_t"); |
| 1326 | } |
| 1327 | |
| 1328 | SamplerTy = SamplerStructTy->getPointerTo(AddressSpace::UniformConstant); |
| 1329 | |
| 1330 | FindType(SamplerTy); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | void SPIRVProducerPass::FindTypesForResourceVars(Module &M) { |
| 1335 | // Record types so they are generated. |
| 1336 | TypesNeedingLayout.reset(); |
| 1337 | StructTypesNeedingBlock.reset(); |
| 1338 | |
| 1339 | // To match older clspv codegen, generate the float type first if required |
| 1340 | // for images. |
| 1341 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1342 | if (info->arg_kind == clspv::ArgKind::ReadOnlyImage || |
| 1343 | info->arg_kind == clspv::ArgKind::WriteOnlyImage) { |
| 1344 | // We need "float" for the sampled component type. |
| 1345 | FindType(Type::getFloatTy(M.getContext())); |
| 1346 | // We only need to find it once. |
| 1347 | break; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1352 | Type *type = info->var_fn->getReturnType(); |
| 1353 | |
| 1354 | switch (info->arg_kind) { |
| 1355 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1356 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1357 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1358 | StructTypesNeedingBlock.insert(sty); |
| 1359 | } else { |
| 1360 | errs() << *type << "\n"; |
| 1361 | llvm_unreachable("Buffer arguments must map to structures!"); |
| 1362 | } |
| 1363 | break; |
| 1364 | case clspv::ArgKind::Pod: |
| 1365 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1366 | StructTypesNeedingBlock.insert(sty); |
| 1367 | } else { |
| 1368 | errs() << *type << "\n"; |
| 1369 | llvm_unreachable("POD arguments must map to structures!"); |
| 1370 | } |
| 1371 | break; |
| 1372 | case clspv::ArgKind::ReadOnlyImage: |
| 1373 | case clspv::ArgKind::WriteOnlyImage: |
| 1374 | case clspv::ArgKind::Sampler: |
| 1375 | // Sampler and image types map to the pointee type but |
| 1376 | // in the uniform constant address space. |
| 1377 | type = PointerType::get(type->getPointerElementType(), |
| 1378 | clspv::AddressSpace::UniformConstant); |
| 1379 | break; |
| 1380 | default: |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | // The converted type is the type of the OpVariable we will generate. |
| 1385 | // If the pointee type is an array of size zero, FindType will convert it |
| 1386 | // to a runtime array. |
| 1387 | FindType(type); |
| 1388 | } |
| 1389 | |
| 1390 | // Traverse the arrays and structures underneath each Block, and |
| 1391 | // mark them as needing layout. |
| 1392 | std::vector<Type *> work_list(StructTypesNeedingBlock.begin(), |
| 1393 | StructTypesNeedingBlock.end()); |
| 1394 | while (!work_list.empty()) { |
| 1395 | Type *type = work_list.back(); |
| 1396 | work_list.pop_back(); |
| 1397 | TypesNeedingLayout.insert(type); |
| 1398 | switch (type->getTypeID()) { |
| 1399 | case Type::ArrayTyID: |
| 1400 | work_list.push_back(type->getArrayElementType()); |
| 1401 | if (!Hack_generate_runtime_array_stride_early) { |
| 1402 | // Remember this array type for deferred decoration. |
| 1403 | TypesNeedingArrayStride.insert(type); |
| 1404 | } |
| 1405 | break; |
| 1406 | case Type::StructTyID: |
| 1407 | for (auto *elem_ty : cast<StructType>(type)->elements()) { |
| 1408 | work_list.push_back(elem_ty); |
| 1409 | } |
| 1410 | default: |
| 1411 | // This type and its contained types don't get layout. |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1417 | void SPIRVProducerPass::FindWorkgroupVars(Module &M) { |
| 1418 | // The SpecId assignment for pointer-to-local arguments is recorded in |
| 1419 | // module-level metadata. Translate that information into local argument |
| 1420 | // information. |
| 1421 | NamedMDNode *nmd = M.getNamedMetadata(clspv::LocalSpecIdMetadataName()); |
| 1422 | if (!nmd) return; |
| 1423 | for (auto operand : nmd->operands()) { |
| 1424 | MDTuple *tuple = cast<MDTuple>(operand); |
| 1425 | ValueAsMetadata *fn_md = cast<ValueAsMetadata>(tuple->getOperand(0)); |
| 1426 | Function *func = cast<Function>(fn_md->getValue()); |
| 1427 | ConstantAsMetadata *arg_index_md = cast<ConstantAsMetadata>(tuple->getOperand(1)); |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 1428 | int arg_index = static_cast<int>(cast<ConstantInt>(arg_index_md->getValue())->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1429 | Argument* arg = &*(func->arg_begin() + arg_index); |
| 1430 | |
| 1431 | ConstantAsMetadata *spec_id_md = |
| 1432 | cast<ConstantAsMetadata>(tuple->getOperand(2)); |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 1433 | int spec_id = static_cast<int>(cast<ConstantInt>(spec_id_md->getValue())->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1434 | |
| 1435 | max_local_spec_id_ = std::max(max_local_spec_id_, spec_id + 1); |
| 1436 | LocalArgSpecIds[arg] = spec_id; |
| 1437 | if (LocalSpecIdInfoMap.count(spec_id)) continue; |
| 1438 | |
| 1439 | // We haven't seen this SpecId yet, so generate the LocalArgInfo for it. |
| 1440 | LocalArgInfo info{nextID, arg->getType()->getPointerElementType(), |
| 1441 | nextID + 1, nextID + 2, |
| 1442 | nextID + 3, spec_id}; |
| 1443 | LocalSpecIdInfoMap[spec_id] = info; |
| 1444 | nextID += 4; |
| 1445 | |
| 1446 | // Ensure the types necessary for this argument get generated. |
| 1447 | Type *IdxTy = Type::getInt32Ty(M.getContext()); |
| 1448 | FindConstant(ConstantInt::get(IdxTy, 0)); |
| 1449 | FindType(IdxTy); |
| 1450 | FindType(arg->getType()); |
| 1451 | } |
| 1452 | } |
| 1453 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1454 | void SPIRVProducerPass::FindType(Type *Ty) { |
| 1455 | TypeList &TyList = getTypeList(); |
| 1456 | |
| 1457 | if (0 != TyList.idFor(Ty)) { |
| 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | if (Ty->isPointerTy()) { |
| 1462 | auto AddrSpace = Ty->getPointerAddressSpace(); |
| 1463 | if ((AddressSpace::Constant == AddrSpace) || |
| 1464 | (AddressSpace::Global == AddrSpace)) { |
| 1465 | auto PointeeTy = Ty->getPointerElementType(); |
| 1466 | |
| 1467 | if (PointeeTy->isStructTy() && |
| 1468 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1469 | FindType(PointeeTy); |
| 1470 | auto ActualPointerTy = |
| 1471 | PointeeTy->getPointerTo(AddressSpace::UniformConstant); |
| 1472 | FindType(ActualPointerTy); |
| 1473 | return; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1478 | // By convention, LLVM array type with 0 elements will map to |
| 1479 | // OpTypeRuntimeArray. Otherwise, it will map to OpTypeArray, which |
| 1480 | // has a constant number of elements. We need to support type of the |
| 1481 | // constant. |
| 1482 | if (auto *arrayTy = dyn_cast<ArrayType>(Ty)) { |
| 1483 | if (arrayTy->getNumElements() > 0) { |
| 1484 | LLVMContext &Context = Ty->getContext(); |
| 1485 | FindType(Type::getInt32Ty(Context)); |
| 1486 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | for (Type *SubTy : Ty->subtypes()) { |
| 1490 | FindType(SubTy); |
| 1491 | } |
| 1492 | |
| 1493 | TyList.insert(Ty); |
| 1494 | } |
| 1495 | |
| 1496 | void SPIRVProducerPass::FindConstantPerGlobalVar(GlobalVariable &GV) { |
| 1497 | // If the global variable has a (non undef) initializer. |
| 1498 | if (GV.hasInitializer() && !isa<UndefValue>(GV.getInitializer())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1499 | // Generate the constant if it's not the initializer to a module scope |
| 1500 | // constant that we will expect in a storage buffer. |
| 1501 | const bool module_scope_constant_external_init = |
| 1502 | (GV.getType()->getPointerAddressSpace() == AddressSpace::Constant) && |
| 1503 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 1504 | if (!module_scope_constant_external_init) { |
| 1505 | FindConstant(GV.getInitializer()); |
| 1506 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | void SPIRVProducerPass::FindConstantPerFunc(Function &F) { |
| 1511 | // Investigate constants in function body. |
| 1512 | for (BasicBlock &BB : F) { |
| 1513 | for (Instruction &I : BB) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1514 | if (auto *call = dyn_cast<CallInst>(&I)) { |
| 1515 | auto name = call->getCalledFunction()->getName(); |
| 1516 | if (name == "clspv.sampler.var.literal") { |
| 1517 | // We've handled these constants elsewhere, so skip it. |
| 1518 | continue; |
| 1519 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1520 | if (name.startswith(clspv::ResourceAccessorFunction())) { |
| 1521 | continue; |
| 1522 | } |
| 1523 | if (name.startswith(clspv::WorkgroupAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1524 | continue; |
| 1525 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | if (isa<AllocaInst>(I)) { |
| 1529 | // Alloca instruction has constant for the number of element. Ignore it. |
| 1530 | continue; |
| 1531 | } else if (isa<ShuffleVectorInst>(I)) { |
| 1532 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1533 | // Ignore constant for mask of shuffle vector instruction. |
| 1534 | if (i == 2) { |
| 1535 | continue; |
| 1536 | } |
| 1537 | |
| 1538 | if (isa<Constant>(I.getOperand(i)) && |
| 1539 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1540 | FindConstant(I.getOperand(i)); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | continue; |
| 1545 | } else if (isa<InsertElementInst>(I)) { |
| 1546 | // Handle InsertElement with <4 x i8> specially. |
| 1547 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 1548 | if (is4xi8vec(CompositeTy)) { |
| 1549 | LLVMContext &Context = CompositeTy->getContext(); |
| 1550 | if (isa<Constant>(I.getOperand(0))) { |
| 1551 | FindConstant(I.getOperand(0)); |
| 1552 | } |
| 1553 | |
| 1554 | if (isa<Constant>(I.getOperand(1))) { |
| 1555 | FindConstant(I.getOperand(1)); |
| 1556 | } |
| 1557 | |
| 1558 | // Add mask constant 0xFF. |
| 1559 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 1560 | FindConstant(CstFF); |
| 1561 | |
| 1562 | // Add shift amount constant. |
| 1563 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 1564 | uint64_t Idx = CI->getZExtValue(); |
| 1565 | Constant *CstShiftAmount = |
| 1566 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 1567 | FindConstant(CstShiftAmount); |
| 1568 | } |
| 1569 | |
| 1570 | continue; |
| 1571 | } |
| 1572 | |
| 1573 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1574 | // Ignore constant for index of InsertElement instruction. |
| 1575 | if (i == 2) { |
| 1576 | continue; |
| 1577 | } |
| 1578 | |
| 1579 | if (isa<Constant>(I.getOperand(i)) && |
| 1580 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1581 | FindConstant(I.getOperand(i)); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | continue; |
| 1586 | } else if (isa<ExtractElementInst>(I)) { |
| 1587 | // Handle ExtractElement with <4 x i8> specially. |
| 1588 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 1589 | if (is4xi8vec(CompositeTy)) { |
| 1590 | LLVMContext &Context = CompositeTy->getContext(); |
| 1591 | if (isa<Constant>(I.getOperand(0))) { |
| 1592 | FindConstant(I.getOperand(0)); |
| 1593 | } |
| 1594 | |
| 1595 | // Add mask constant 0xFF. |
| 1596 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 1597 | FindConstant(CstFF); |
| 1598 | |
| 1599 | // Add shift amount constant. |
| 1600 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 1601 | uint64_t Idx = CI->getZExtValue(); |
| 1602 | Constant *CstShiftAmount = |
| 1603 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 1604 | FindConstant(CstShiftAmount); |
| 1605 | } else { |
| 1606 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
| 1607 | FindConstant(Cst8); |
| 1608 | } |
| 1609 | |
| 1610 | continue; |
| 1611 | } |
| 1612 | |
| 1613 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1614 | // Ignore constant for index of ExtractElement instruction. |
| 1615 | if (i == 1) { |
| 1616 | continue; |
| 1617 | } |
| 1618 | |
| 1619 | if (isa<Constant>(I.getOperand(i)) && |
| 1620 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1621 | FindConstant(I.getOperand(i)); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | continue; |
| 1626 | } else if ((Instruction::Xor == I.getOpcode()) && I.getType()->isIntegerTy(1)) { |
| 1627 | // 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 |
| 1628 | bool foundConstantTrue = false; |
| 1629 | for (Use &Op : I.operands()) { |
| 1630 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1631 | auto CI = cast<ConstantInt>(Op); |
| 1632 | |
| 1633 | if (CI->isZero() || foundConstantTrue) { |
| 1634 | // 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. |
| 1635 | FindConstant(Op); |
| 1636 | } else { |
| 1637 | foundConstantTrue = true; |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | continue; |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 1643 | } else if (isa<TruncInst>(I)) { |
| 1644 | // For truncation to i8 we mask against 255. |
| 1645 | Type *ToTy = I.getType(); |
| 1646 | if (8u == ToTy->getPrimitiveSizeInBits()) { |
| 1647 | LLVMContext &Context = ToTy->getContext(); |
| 1648 | Constant *Cst255 = ConstantInt::get(Type::getInt32Ty(Context), 0xff); |
| 1649 | FindConstant(Cst255); |
| 1650 | } |
| 1651 | // Fall through. |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 1652 | } else if (isa<AtomicRMWInst>(I)) { |
| 1653 | LLVMContext &Context = I.getContext(); |
| 1654 | |
| 1655 | FindConstant( |
| 1656 | ConstantInt::get(Type::getInt32Ty(Context), spv::ScopeDevice)); |
| 1657 | FindConstant(ConstantInt::get( |
| 1658 | Type::getInt32Ty(Context), |
| 1659 | spv::MemorySemanticsUniformMemoryMask | |
| 1660 | spv::MemorySemanticsSequentiallyConsistentMask)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | for (Use &Op : I.operands()) { |
| 1664 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1665 | FindConstant(Op); |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | void SPIRVProducerPass::FindConstant(Value *V) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1673 | ValueList &CstList = getConstantList(); |
| 1674 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1675 | // If V is already tracked, ignore it. |
| 1676 | if (0 != CstList.idFor(V)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1677 | return; |
| 1678 | } |
| 1679 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1680 | if (isa<GlobalValue>(V) && clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 1681 | return; |
| 1682 | } |
| 1683 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1684 | Constant *Cst = cast<Constant>(V); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1685 | Type *CstTy = Cst->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1686 | |
| 1687 | // Handle constant with <4 x i8> type specially. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1688 | if (is4xi8vec(CstTy)) { |
| 1689 | if (!isa<GlobalValue>(V)) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1690 | CstList.insert(V); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | if (Cst->getNumOperands()) { |
| 1695 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); I != E; |
| 1696 | ++I) { |
| 1697 | FindConstant(*I); |
| 1698 | } |
| 1699 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1700 | CstList.insert(Cst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1701 | return; |
| 1702 | } else if (const ConstantDataSequential *CDS = |
| 1703 | dyn_cast<ConstantDataSequential>(Cst)) { |
| 1704 | // Add constants for each element to constant list. |
| 1705 | for (unsigned i = 0; i < CDS->getNumElements(); i++) { |
| 1706 | Constant *EleCst = CDS->getElementAsConstant(i); |
| 1707 | FindConstant(EleCst); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | if (!isa<GlobalValue>(V)) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1712 | CstList.insert(V); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | spv::StorageClass SPIRVProducerPass::GetStorageClass(unsigned AddrSpace) const { |
| 1717 | switch (AddrSpace) { |
| 1718 | default: |
| 1719 | llvm_unreachable("Unsupported OpenCL address space"); |
| 1720 | case AddressSpace::Private: |
| 1721 | return spv::StorageClassFunction; |
| 1722 | case AddressSpace::Global: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1723 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1724 | case AddressSpace::Constant: |
| 1725 | return clspv::Option::ConstantArgsInUniformBuffer() |
| 1726 | ? spv::StorageClassUniform |
| 1727 | : spv::StorageClassStorageBuffer; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1728 | case AddressSpace::Input: |
| 1729 | return spv::StorageClassInput; |
| 1730 | case AddressSpace::Local: |
| 1731 | return spv::StorageClassWorkgroup; |
| 1732 | case AddressSpace::UniformConstant: |
| 1733 | return spv::StorageClassUniformConstant; |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1734 | case AddressSpace::Uniform: |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 1735 | return spv::StorageClassUniform; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1736 | case AddressSpace::ModuleScopePrivate: |
| 1737 | return spv::StorageClassPrivate; |
| 1738 | } |
| 1739 | } |
| 1740 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1741 | spv::StorageClass |
| 1742 | SPIRVProducerPass::GetStorageClassForArgKind(clspv::ArgKind arg_kind) const { |
| 1743 | switch (arg_kind) { |
| 1744 | case clspv::ArgKind::Buffer: |
| 1745 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1746 | case clspv::ArgKind::BufferUBO: |
| 1747 | return spv::StorageClassUniform; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1748 | case clspv::ArgKind::Pod: |
| 1749 | return clspv::Option::PodArgsInUniformBuffer() |
| 1750 | ? spv::StorageClassUniform |
| 1751 | : spv::StorageClassStorageBuffer; |
| 1752 | case clspv::ArgKind::Local: |
| 1753 | return spv::StorageClassWorkgroup; |
| 1754 | case clspv::ArgKind::ReadOnlyImage: |
| 1755 | case clspv::ArgKind::WriteOnlyImage: |
| 1756 | case clspv::ArgKind::Sampler: |
| 1757 | return spv::StorageClassUniformConstant; |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 1758 | default: |
| 1759 | llvm_unreachable("Unsupported storage class for argument kind"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1763 | spv::BuiltIn SPIRVProducerPass::GetBuiltin(StringRef Name) const { |
| 1764 | return StringSwitch<spv::BuiltIn>(Name) |
| 1765 | .Case("__spirv_GlobalInvocationId", spv::BuiltInGlobalInvocationId) |
| 1766 | .Case("__spirv_LocalInvocationId", spv::BuiltInLocalInvocationId) |
| 1767 | .Case("__spirv_WorkgroupSize", spv::BuiltInWorkgroupSize) |
| 1768 | .Case("__spirv_NumWorkgroups", spv::BuiltInNumWorkgroups) |
| 1769 | .Case("__spirv_WorkgroupId", spv::BuiltInWorkgroupId) |
| 1770 | .Default(spv::BuiltInMax); |
| 1771 | } |
| 1772 | |
| 1773 | void SPIRVProducerPass::GenerateExtInstImport() { |
| 1774 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 1775 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 1776 | |
| 1777 | // |
| 1778 | // Generate OpExtInstImport. |
| 1779 | // |
| 1780 | // Ops[0] ... Ops[n] = Name (Literal String) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1781 | ExtInstImportID = nextID; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1782 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpExtInstImport, nextID++, |
| 1783 | MkString("GLSL.std.450"))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1784 | } |
| 1785 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1786 | void SPIRVProducerPass::GenerateSPIRVTypes(LLVMContext& Context, Module &module) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1787 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 1788 | ValueMapType &VMap = getValueMap(); |
| 1789 | ValueMapType &AllocatedVMap = getAllocatedValueMap(); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1790 | const auto &DL = module.getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1791 | |
| 1792 | // Map for OpTypeRuntimeArray. If argument has pointer type, 2 spirv type |
| 1793 | // instructions are generated. They are OpTypePointer and OpTypeRuntimeArray. |
| 1794 | DenseMap<Type *, uint32_t> OpRuntimeTyMap; |
| 1795 | |
| 1796 | for (Type *Ty : getTypeList()) { |
| 1797 | // Update TypeMap with nextID for reference later. |
| 1798 | TypeMap[Ty] = nextID; |
| 1799 | |
| 1800 | switch (Ty->getTypeID()) { |
| 1801 | default: { |
| 1802 | Ty->print(errs()); |
| 1803 | llvm_unreachable("Unsupported type???"); |
| 1804 | break; |
| 1805 | } |
| 1806 | case Type::MetadataTyID: |
| 1807 | case Type::LabelTyID: { |
| 1808 | // Ignore these types. |
| 1809 | break; |
| 1810 | } |
| 1811 | case Type::PointerTyID: { |
| 1812 | PointerType *PTy = cast<PointerType>(Ty); |
| 1813 | unsigned AddrSpace = PTy->getAddressSpace(); |
| 1814 | |
| 1815 | // For the purposes of our Vulkan SPIR-V type system, constant and global |
| 1816 | // are conflated. |
| 1817 | bool UseExistingOpTypePointer = false; |
| 1818 | if (AddressSpace::Constant == AddrSpace) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1819 | if (!clspv::Option::ConstantArgsInUniformBuffer()) { |
| 1820 | AddrSpace = AddressSpace::Global; |
| 1821 | // Check to see if we already created this type (for instance, if we had |
| 1822 | // a constant <type>* and a global <type>*, the type would be created by |
| 1823 | // one of these types, and shared by both). |
| 1824 | auto GlobalTy = PTy->getPointerElementType()->getPointerTo(AddrSpace); |
| 1825 | if (0 < TypeMap.count(GlobalTy)) { |
| 1826 | TypeMap[PTy] = TypeMap[GlobalTy]; |
| 1827 | UseExistingOpTypePointer = true; |
| 1828 | break; |
| 1829 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1830 | } |
| 1831 | } else if (AddressSpace::Global == AddrSpace) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1832 | if (!clspv::Option::ConstantArgsInUniformBuffer()) { |
| 1833 | AddrSpace = AddressSpace::Constant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1834 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1835 | // Check to see if we already created this type (for instance, if we had |
| 1836 | // a constant <type>* and a global <type>*, the type would be created by |
| 1837 | // one of these types, and shared by both). |
| 1838 | auto ConstantTy = PTy->getPointerElementType()->getPointerTo(AddrSpace); |
| 1839 | if (0 < TypeMap.count(ConstantTy)) { |
| 1840 | TypeMap[PTy] = TypeMap[ConstantTy]; |
| 1841 | UseExistingOpTypePointer = true; |
| 1842 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1846 | const bool HasArgUser = true; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1847 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1848 | if (HasArgUser && !UseExistingOpTypePointer) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1849 | // |
| 1850 | // Generate OpTypePointer. |
| 1851 | // |
| 1852 | |
| 1853 | // OpTypePointer |
| 1854 | // Ops[0] = Storage Class |
| 1855 | // Ops[1] = Element Type ID |
| 1856 | SPIRVOperandList Ops; |
| 1857 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1858 | Ops << MkNum(GetStorageClass(AddrSpace)) |
| 1859 | << MkId(lookupType(PTy->getElementType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1860 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1861 | auto *Inst = new SPIRVInstruction(spv::OpTypePointer, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1862 | SPIRVInstList.push_back(Inst); |
| 1863 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1864 | break; |
| 1865 | } |
| 1866 | case Type::StructTyID: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1867 | StructType *STy = cast<StructType>(Ty); |
| 1868 | |
| 1869 | // Handle sampler type. |
| 1870 | if (STy->isOpaque()) { |
| 1871 | if (STy->getName().equals("opencl.sampler_t")) { |
| 1872 | // |
| 1873 | // Generate OpTypeSampler |
| 1874 | // |
| 1875 | // Empty Ops. |
| 1876 | SPIRVOperandList Ops; |
| 1877 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1878 | auto *Inst = new SPIRVInstruction(spv::OpTypeSampler, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1879 | SPIRVInstList.push_back(Inst); |
| 1880 | break; |
| 1881 | } else if (STy->getName().equals("opencl.image2d_ro_t") || |
| 1882 | STy->getName().equals("opencl.image2d_wo_t") || |
| 1883 | STy->getName().equals("opencl.image3d_ro_t") || |
| 1884 | STy->getName().equals("opencl.image3d_wo_t")) { |
| 1885 | // |
| 1886 | // Generate OpTypeImage |
| 1887 | // |
| 1888 | // Ops[0] = Sampled Type ID |
| 1889 | // Ops[1] = Dim ID |
| 1890 | // Ops[2] = Depth (Literal Number) |
| 1891 | // Ops[3] = Arrayed (Literal Number) |
| 1892 | // Ops[4] = MS (Literal Number) |
| 1893 | // Ops[5] = Sampled (Literal Number) |
| 1894 | // Ops[6] = Image Format ID |
| 1895 | // |
| 1896 | SPIRVOperandList Ops; |
| 1897 | |
| 1898 | // TODO: Changed Sampled Type according to situations. |
| 1899 | uint32_t SampledTyID = lookupType(Type::getFloatTy(Context)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1900 | Ops << MkId(SampledTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1901 | |
| 1902 | spv::Dim DimID = spv::Dim2D; |
| 1903 | if (STy->getName().equals("opencl.image3d_ro_t") || |
| 1904 | STy->getName().equals("opencl.image3d_wo_t")) { |
| 1905 | DimID = spv::Dim3D; |
| 1906 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1907 | Ops << MkNum(DimID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1908 | |
| 1909 | // TODO: Set up Depth. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1910 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1911 | |
| 1912 | // TODO: Set up Arrayed. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1913 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1914 | |
| 1915 | // TODO: Set up MS. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1916 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1917 | |
| 1918 | // TODO: Set up Sampled. |
| 1919 | // |
| 1920 | // From Spec |
| 1921 | // |
| 1922 | // 0 indicates this is only known at run time, not at compile time |
| 1923 | // 1 indicates will be used with sampler |
| 1924 | // 2 indicates will be used without a sampler (a storage image) |
| 1925 | uint32_t Sampled = 1; |
| 1926 | if (STy->getName().equals("opencl.image2d_wo_t") || |
| 1927 | STy->getName().equals("opencl.image3d_wo_t")) { |
| 1928 | Sampled = 2; |
| 1929 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1930 | Ops << MkNum(Sampled); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1931 | |
| 1932 | // TODO: Set up Image Format. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1933 | Ops << MkNum(spv::ImageFormatUnknown); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1934 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1935 | auto *Inst = new SPIRVInstruction(spv::OpTypeImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1936 | SPIRVInstList.push_back(Inst); |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | // |
| 1942 | // Generate OpTypeStruct |
| 1943 | // |
| 1944 | // Ops[0] ... Ops[n] = Member IDs |
| 1945 | SPIRVOperandList Ops; |
| 1946 | |
| 1947 | for (auto *EleTy : STy->elements()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1948 | Ops << MkId(lookupType(EleTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1949 | } |
| 1950 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1951 | uint32_t STyID = nextID; |
| 1952 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1953 | auto *Inst = |
| 1954 | new SPIRVInstruction(spv::OpTypeStruct, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1955 | SPIRVInstList.push_back(Inst); |
| 1956 | |
| 1957 | // Generate OpMemberDecorate. |
| 1958 | auto DecoInsertPoint = |
| 1959 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 1960 | [](SPIRVInstruction *Inst) -> bool { |
| 1961 | return Inst->getOpcode() != spv::OpDecorate && |
| 1962 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 1963 | Inst->getOpcode() != spv::OpExtInstImport; |
| 1964 | }); |
| 1965 | |
David Neto | c463b37 | 2017-08-10 15:32:21 -0400 | [diff] [blame] | 1966 | const auto StructLayout = DL.getStructLayout(STy); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1967 | // Search for the correct offsets if this type was remapped. |
| 1968 | std::vector<uint32_t> *offsets = nullptr; |
| 1969 | auto iter = RemappedUBOTypeOffsets.find(STy); |
| 1970 | if (iter != RemappedUBOTypeOffsets.end()) { |
| 1971 | offsets = &iter->second; |
| 1972 | } |
David Neto | c463b37 | 2017-08-10 15:32:21 -0400 | [diff] [blame] | 1973 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1974 | // #error TODO(dneto): Only do this if in TypesNeedingLayout. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1975 | for (unsigned MemberIdx = 0; MemberIdx < STy->getNumElements(); |
| 1976 | MemberIdx++) { |
| 1977 | // Ops[0] = Structure Type ID |
| 1978 | // Ops[1] = Member Index(Literal Number) |
| 1979 | // Ops[2] = Decoration (Offset) |
| 1980 | // Ops[3] = Byte Offset (Literal Number) |
| 1981 | Ops.clear(); |
| 1982 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1983 | Ops << MkId(STyID) << MkNum(MemberIdx) << MkNum(spv::DecorationOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1984 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1985 | auto ByteOffset = StructLayout->getElementOffset(MemberIdx); |
| 1986 | if (offsets) { |
| 1987 | ByteOffset = (*offsets)[MemberIdx]; |
| 1988 | } |
| 1989 | //const auto ByteOffset = |
| 1990 | // uint32_t(StructLayout->getElementOffset(MemberIdx)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 1991 | Ops << MkNum(ByteOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1992 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1993 | auto *DecoInst = new SPIRVInstruction(spv::OpMemberDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1994 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | // Generate OpDecorate. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1998 | if (StructTypesNeedingBlock.idFor(STy)) { |
| 1999 | Ops.clear(); |
| 2000 | // Use Block decorations with StorageBuffer storage class. |
| 2001 | Ops << MkId(STyID) << MkNum(spv::DecorationBlock); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2002 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2003 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
| 2004 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2005 | } |
| 2006 | break; |
| 2007 | } |
| 2008 | case Type::IntegerTyID: { |
| 2009 | unsigned BitWidth = Ty->getPrimitiveSizeInBits(); |
| 2010 | |
| 2011 | if (BitWidth == 1) { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2012 | auto *Inst = new SPIRVInstruction(spv::OpTypeBool, nextID++, {}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2013 | SPIRVInstList.push_back(Inst); |
| 2014 | } else { |
| 2015 | // i8 is added to TypeMap as i32. |
David Neto | 391aeb1 | 2017-08-26 15:51:58 -0400 | [diff] [blame] | 2016 | // No matter what LLVM type is requested first, always alias the |
| 2017 | // second one's SPIR-V type to be the same as the one we generated |
| 2018 | // first. |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2019 | unsigned aliasToWidth = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2020 | if (BitWidth == 8) { |
David Neto | 391aeb1 | 2017-08-26 15:51:58 -0400 | [diff] [blame] | 2021 | aliasToWidth = 32; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2022 | BitWidth = 32; |
David Neto | 391aeb1 | 2017-08-26 15:51:58 -0400 | [diff] [blame] | 2023 | } else if (BitWidth == 32) { |
| 2024 | aliasToWidth = 8; |
| 2025 | } |
| 2026 | if (aliasToWidth) { |
| 2027 | Type* otherType = Type::getIntNTy(Ty->getContext(), aliasToWidth); |
| 2028 | auto where = TypeMap.find(otherType); |
| 2029 | if (where == TypeMap.end()) { |
| 2030 | // Go ahead and make it, but also map the other type to it. |
| 2031 | TypeMap[otherType] = nextID; |
| 2032 | } else { |
| 2033 | // Alias this SPIR-V type the existing type. |
| 2034 | TypeMap[Ty] = where->second; |
| 2035 | break; |
| 2036 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2037 | } |
| 2038 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2039 | SPIRVOperandList Ops; |
| 2040 | Ops << MkNum(BitWidth) << MkNum(0 /* not signed */); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2041 | |
| 2042 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2043 | new SPIRVInstruction(spv::OpTypeInt, nextID++, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2044 | } |
| 2045 | break; |
| 2046 | } |
| 2047 | case Type::HalfTyID: |
| 2048 | case Type::FloatTyID: |
| 2049 | case Type::DoubleTyID: { |
| 2050 | SPIRVOperand *WidthOp = new SPIRVOperand( |
| 2051 | SPIRVOperandType::LITERAL_INTEGER, Ty->getPrimitiveSizeInBits()); |
| 2052 | |
| 2053 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2054 | new SPIRVInstruction(spv::OpTypeFloat, nextID++, WidthOp)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2055 | break; |
| 2056 | } |
| 2057 | case Type::ArrayTyID: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2058 | ArrayType *ArrTy = cast<ArrayType>(Ty); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2059 | const uint64_t Length = ArrTy->getArrayNumElements(); |
| 2060 | if (Length == 0) { |
| 2061 | // By convention, map it to a RuntimeArray. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2062 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2063 | // Only generate the type once. |
| 2064 | // TODO(dneto): Can it ever be generated more than once? |
| 2065 | // Doesn't LLVM type uniqueness guarantee we'll only see this |
| 2066 | // once? |
| 2067 | Type *EleTy = ArrTy->getArrayElementType(); |
| 2068 | if (OpRuntimeTyMap.count(EleTy) == 0) { |
| 2069 | uint32_t OpTypeRuntimeArrayID = nextID; |
| 2070 | OpRuntimeTyMap[Ty] = nextID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2071 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2072 | // |
| 2073 | // Generate OpTypeRuntimeArray. |
| 2074 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2075 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2076 | // OpTypeRuntimeArray |
| 2077 | // Ops[0] = Element Type ID |
| 2078 | SPIRVOperandList Ops; |
| 2079 | Ops << MkId(lookupType(EleTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2080 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2081 | SPIRVInstList.push_back( |
| 2082 | new SPIRVInstruction(spv::OpTypeRuntimeArray, nextID++, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2083 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2084 | if (Hack_generate_runtime_array_stride_early) { |
| 2085 | // Generate OpDecorate. |
| 2086 | auto DecoInsertPoint = std::find_if( |
| 2087 | SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2088 | [](SPIRVInstruction *Inst) -> bool { |
| 2089 | return Inst->getOpcode() != spv::OpDecorate && |
| 2090 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2091 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2092 | }); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2093 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2094 | // Ops[0] = Target ID |
| 2095 | // Ops[1] = Decoration (ArrayStride) |
| 2096 | // Ops[2] = Stride Number(Literal Number) |
| 2097 | Ops.clear(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2098 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2099 | Ops << MkId(OpTypeRuntimeArrayID) |
| 2100 | << MkNum(spv::DecorationArrayStride) |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2101 | << MkNum(static_cast<uint32_t>(GetTypeAllocSize(EleTy, DL))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2102 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2103 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
| 2104 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
| 2105 | } |
| 2106 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2107 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2108 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2109 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2110 | // |
| 2111 | // Generate OpConstant and OpTypeArray. |
| 2112 | // |
| 2113 | |
| 2114 | // |
| 2115 | // Generate OpConstant for array length. |
| 2116 | // |
| 2117 | // Ops[0] = Result Type ID |
| 2118 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 2119 | SPIRVOperandList Ops; |
| 2120 | |
| 2121 | Type *LengthTy = Type::getInt32Ty(Context); |
| 2122 | uint32_t ResTyID = lookupType(LengthTy); |
| 2123 | Ops << MkId(ResTyID); |
| 2124 | |
| 2125 | assert(Length < UINT32_MAX); |
| 2126 | Ops << MkNum(static_cast<uint32_t>(Length)); |
| 2127 | |
| 2128 | // Add constant for length to constant list. |
| 2129 | Constant *CstLength = ConstantInt::get(LengthTy, Length); |
| 2130 | AllocatedVMap[CstLength] = nextID; |
| 2131 | VMap[CstLength] = nextID; |
| 2132 | uint32_t LengthID = nextID; |
| 2133 | |
| 2134 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
| 2135 | SPIRVInstList.push_back(CstInst); |
| 2136 | |
| 2137 | // Remember to generate ArrayStride later |
| 2138 | getTypesNeedingArrayStride().insert(Ty); |
| 2139 | |
| 2140 | // |
| 2141 | // Generate OpTypeArray. |
| 2142 | // |
| 2143 | // Ops[0] = Element Type ID |
| 2144 | // Ops[1] = Array Length Constant ID |
| 2145 | Ops.clear(); |
| 2146 | |
| 2147 | uint32_t EleTyID = lookupType(ArrTy->getElementType()); |
| 2148 | Ops << MkId(EleTyID) << MkId(LengthID); |
| 2149 | |
| 2150 | // Update TypeMap with nextID. |
| 2151 | TypeMap[Ty] = nextID; |
| 2152 | |
| 2153 | auto *ArrayInst = new SPIRVInstruction(spv::OpTypeArray, nextID++, Ops); |
| 2154 | SPIRVInstList.push_back(ArrayInst); |
| 2155 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2156 | break; |
| 2157 | } |
| 2158 | case Type::VectorTyID: { |
| 2159 | // <4 x i8> is changed to i32. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2160 | if (Ty->getVectorElementType() == Type::getInt8Ty(Context)) { |
| 2161 | if (Ty->getVectorNumElements() == 4) { |
| 2162 | TypeMap[Ty] = lookupType(Ty->getVectorElementType()); |
| 2163 | break; |
| 2164 | } else { |
| 2165 | Ty->print(errs()); |
| 2166 | llvm_unreachable("Support above i8 vector type"); |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | // Ops[0] = Component Type ID |
| 2171 | // Ops[1] = Component Count (Literal Number) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2172 | SPIRVOperandList Ops; |
| 2173 | Ops << MkId(lookupType(Ty->getVectorElementType())) |
| 2174 | << MkNum(Ty->getVectorNumElements()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2175 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2176 | SPIRVInstruction* inst = new SPIRVInstruction(spv::OpTypeVector, nextID++, Ops); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2177 | SPIRVInstList.push_back(inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2178 | break; |
| 2179 | } |
| 2180 | case Type::VoidTyID: { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2181 | auto *Inst = new SPIRVInstruction(spv::OpTypeVoid, nextID++, {}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2182 | SPIRVInstList.push_back(Inst); |
| 2183 | break; |
| 2184 | } |
| 2185 | case Type::FunctionTyID: { |
| 2186 | // Generate SPIRV instruction for function type. |
| 2187 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 2188 | |
| 2189 | // Ops[0] = Return Type ID |
| 2190 | // Ops[1] ... Ops[n] = Parameter Type IDs |
| 2191 | SPIRVOperandList Ops; |
| 2192 | |
| 2193 | // Find SPIRV instruction for return type |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2194 | Ops << MkId(lookupType(FTy->getReturnType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2195 | |
| 2196 | // Find SPIRV instructions for parameter types |
| 2197 | for (unsigned k = 0; k < FTy->getNumParams(); k++) { |
| 2198 | // Find SPIRV instruction for parameter type. |
| 2199 | auto ParamTy = FTy->getParamType(k); |
| 2200 | if (ParamTy->isPointerTy()) { |
| 2201 | auto PointeeTy = ParamTy->getPointerElementType(); |
| 2202 | if (PointeeTy->isStructTy() && |
| 2203 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 2204 | ParamTy = PointeeTy; |
| 2205 | } |
| 2206 | } |
| 2207 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2208 | Ops << MkId(lookupType(ParamTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2209 | } |
| 2210 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2211 | auto *Inst = new SPIRVInstruction(spv::OpTypeFunction, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2212 | SPIRVInstList.push_back(Inst); |
| 2213 | break; |
| 2214 | } |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | // Generate OpTypeSampledImage. |
| 2219 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 2220 | for (auto &ImageType : OpImageTypeMap) { |
| 2221 | // |
| 2222 | // Generate OpTypeSampledImage. |
| 2223 | // |
| 2224 | // Ops[0] = Image Type ID |
| 2225 | // |
| 2226 | SPIRVOperandList Ops; |
| 2227 | |
| 2228 | Type *ImgTy = ImageType.first; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2229 | Ops << MkId(TypeMap[ImgTy]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2230 | |
| 2231 | // Update OpImageTypeMap. |
| 2232 | ImageType.second = nextID; |
| 2233 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2234 | auto *Inst = new SPIRVInstruction(spv::OpTypeSampledImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2235 | SPIRVInstList.push_back(Inst); |
| 2236 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2237 | |
| 2238 | // Generate types for pointer-to-local arguments. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 2239 | for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_; |
| 2240 | ++spec_id) { |
| 2241 | LocalArgInfo& arg_info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2242 | |
| 2243 | // Generate the spec constant. |
| 2244 | SPIRVOperandList Ops; |
| 2245 | Ops << MkId(lookupType(Type::getInt32Ty(Context))) << MkNum(1); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2246 | SPIRVInstList.push_back( |
| 2247 | new SPIRVInstruction(spv::OpSpecConstant, arg_info.array_size_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2248 | |
| 2249 | // Generate the array type. |
| 2250 | Ops.clear(); |
| 2251 | // The element type must have been created. |
| 2252 | uint32_t elem_ty_id = lookupType(arg_info.elem_type); |
| 2253 | assert(elem_ty_id); |
| 2254 | Ops << MkId(elem_ty_id) << MkId(arg_info.array_size_id); |
| 2255 | |
| 2256 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2257 | new SPIRVInstruction(spv::OpTypeArray, arg_info.array_type_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2258 | |
| 2259 | Ops.clear(); |
| 2260 | Ops << MkNum(spv::StorageClassWorkgroup) << MkId(arg_info.array_type_id); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2261 | SPIRVInstList.push_back(new SPIRVInstruction( |
| 2262 | spv::OpTypePointer, arg_info.ptr_array_type_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2263 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | void SPIRVProducerPass::GenerateSPIRVConstants() { |
| 2267 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 2268 | ValueMapType &VMap = getValueMap(); |
| 2269 | ValueMapType &AllocatedVMap = getAllocatedValueMap(); |
| 2270 | ValueList &CstList = getConstantList(); |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 2271 | const bool hack_undef = clspv::Option::HackUndef(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2272 | |
| 2273 | for (uint32_t i = 0; i < CstList.size(); i++) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2274 | // UniqueVector ids are 1-based. |
| 2275 | Constant *Cst = cast<Constant>(CstList[i+1]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2276 | |
| 2277 | // OpTypeArray's constant was already generated. |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2278 | if (AllocatedVMap.find_as(Cst) != AllocatedVMap.end()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2279 | continue; |
| 2280 | } |
| 2281 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2282 | // Set ValueMap with nextID for reference later. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2283 | VMap[Cst] = nextID; |
| 2284 | |
| 2285 | // |
| 2286 | // Generate OpConstant. |
| 2287 | // |
| 2288 | |
| 2289 | // Ops[0] = Result Type ID |
| 2290 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 2291 | SPIRVOperandList Ops; |
| 2292 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2293 | Ops << MkId(lookupType(Cst->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2294 | |
| 2295 | std::vector<uint32_t> LiteralNum; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2296 | spv::Op Opcode = spv::OpNop; |
| 2297 | |
| 2298 | if (isa<UndefValue>(Cst)) { |
| 2299 | // Ops[0] = Result Type ID |
David Neto | c66b335 | 2017-10-20 14:28:46 -0400 | [diff] [blame] | 2300 | Opcode = spv::OpUndef; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 2301 | if (hack_undef && IsTypeNullable(Cst->getType())) { |
| 2302 | Opcode = spv::OpConstantNull; |
David Neto | c66b335 | 2017-10-20 14:28:46 -0400 | [diff] [blame] | 2303 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2304 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Cst)) { |
| 2305 | unsigned BitWidth = CI->getBitWidth(); |
| 2306 | if (BitWidth == 1) { |
| 2307 | // If the bitwidth of constant is 1, generate OpConstantTrue or |
| 2308 | // OpConstantFalse. |
| 2309 | if (CI->getZExtValue()) { |
| 2310 | // Ops[0] = Result Type ID |
| 2311 | Opcode = spv::OpConstantTrue; |
| 2312 | } else { |
| 2313 | // Ops[0] = Result Type ID |
| 2314 | Opcode = spv::OpConstantFalse; |
| 2315 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2316 | } else { |
| 2317 | auto V = CI->getZExtValue(); |
| 2318 | LiteralNum.push_back(V & 0xFFFFFFFF); |
| 2319 | |
| 2320 | if (BitWidth > 32) { |
| 2321 | LiteralNum.push_back(V >> 32); |
| 2322 | } |
| 2323 | |
| 2324 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2325 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2326 | Ops << MkInteger(LiteralNum); |
| 2327 | |
| 2328 | if (BitWidth == 32 && V == 0) { |
| 2329 | constant_i32_zero_id_ = nextID; |
| 2330 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2331 | } |
| 2332 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Cst)) { |
| 2333 | uint64_t FPVal = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 2334 | Type *CFPTy = CFP->getType(); |
| 2335 | if (CFPTy->isFloatTy()) { |
| 2336 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
| 2337 | } else { |
| 2338 | CFPTy->print(errs()); |
| 2339 | llvm_unreachable("Implement this ConstantFP Type"); |
| 2340 | } |
| 2341 | |
| 2342 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2343 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2344 | Ops << MkFloat(LiteralNum); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2345 | } else if (isa<ConstantDataSequential>(Cst) && |
| 2346 | cast<ConstantDataSequential>(Cst)->isString()) { |
| 2347 | Cst->print(errs()); |
| 2348 | llvm_unreachable("Implement this Constant"); |
| 2349 | |
| 2350 | } else if (const ConstantDataSequential *CDS = |
| 2351 | dyn_cast<ConstantDataSequential>(Cst)) { |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2352 | // Let's convert <4 x i8> constant to int constant specially. |
| 2353 | // This case occurs when all the values are specified as constant |
| 2354 | // ints. |
| 2355 | Type *CstTy = Cst->getType(); |
| 2356 | if (is4xi8vec(CstTy)) { |
| 2357 | LLVMContext &Context = CstTy->getContext(); |
| 2358 | |
| 2359 | // |
| 2360 | // Generate OpConstant with OpTypeInt 32 0. |
| 2361 | // |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2362 | uint32_t IntValue = 0; |
| 2363 | for (unsigned k = 0; k < 4; k++) { |
| 2364 | const uint64_t Val = CDS->getElementAsInteger(k); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2365 | IntValue = (IntValue << 8) | (Val & 0xffu); |
| 2366 | } |
| 2367 | |
| 2368 | Type *i32 = Type::getInt32Ty(Context); |
| 2369 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
| 2370 | // If this constant is already registered on VMap, use it. |
| 2371 | if (VMap.count(CstInt)) { |
| 2372 | uint32_t CstID = VMap[CstInt]; |
| 2373 | VMap[Cst] = CstID; |
| 2374 | continue; |
| 2375 | } |
| 2376 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2377 | Ops << MkNum(IntValue); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2378 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2379 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2380 | SPIRVInstList.push_back(CstInst); |
| 2381 | |
| 2382 | continue; |
| 2383 | } |
| 2384 | |
| 2385 | // A normal constant-data-sequential case. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2386 | for (unsigned k = 0; k < CDS->getNumElements(); k++) { |
| 2387 | Constant *EleCst = CDS->getElementAsConstant(k); |
| 2388 | uint32_t EleCstID = VMap[EleCst]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2389 | Ops << MkId(EleCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2393 | } else if (const ConstantAggregate *CA = dyn_cast<ConstantAggregate>(Cst)) { |
| 2394 | // Let's convert <4 x i8> constant to int constant specially. |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2395 | // 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] | 2396 | Type *CstTy = Cst->getType(); |
| 2397 | if (is4xi8vec(CstTy)) { |
| 2398 | LLVMContext &Context = CstTy->getContext(); |
| 2399 | |
| 2400 | // |
| 2401 | // Generate OpConstant with OpTypeInt 32 0. |
| 2402 | // |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2403 | uint32_t IntValue = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2404 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); |
| 2405 | I != E; ++I) { |
| 2406 | uint64_t Val = 0; |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2407 | const Value* CV = *I; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2408 | if (auto *CI2 = dyn_cast<ConstantInt>(CV)) { |
| 2409 | Val = CI2->getZExtValue(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2410 | } |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2411 | IntValue = (IntValue << 8) | (Val & 0xffu); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2412 | } |
| 2413 | |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2414 | Type *i32 = Type::getInt32Ty(Context); |
| 2415 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2416 | // If this constant is already registered on VMap, use it. |
| 2417 | if (VMap.count(CstInt)) { |
| 2418 | uint32_t CstID = VMap[CstInt]; |
| 2419 | VMap[Cst] = CstID; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 2420 | continue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2421 | } |
| 2422 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2423 | Ops << MkNum(IntValue); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2424 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2425 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2426 | SPIRVInstList.push_back(CstInst); |
| 2427 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 2428 | continue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | // We use a constant composite in SPIR-V for our constant aggregate in |
| 2432 | // LLVM. |
| 2433 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2434 | |
| 2435 | for (unsigned k = 0; k < CA->getNumOperands(); k++) { |
| 2436 | // Look up the ID of the element of this aggregate (which we will |
| 2437 | // previously have created a constant for). |
| 2438 | uint32_t ElementConstantID = VMap[CA->getAggregateElement(k)]; |
| 2439 | |
| 2440 | // And add an operand to the composite we are constructing |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2441 | Ops << MkId(ElementConstantID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2442 | } |
| 2443 | } else if (Cst->isNullValue()) { |
| 2444 | Opcode = spv::OpConstantNull; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2445 | } else { |
| 2446 | Cst->print(errs()); |
| 2447 | llvm_unreachable("Unsupported Constant???"); |
| 2448 | } |
| 2449 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2450 | auto *CstInst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2451 | SPIRVInstList.push_back(CstInst); |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | void SPIRVProducerPass::GenerateSamplers(Module &M) { |
| 2456 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2457 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2458 | auto& sampler_map = getSamplerMap(); |
| 2459 | SamplerMapIndexToIDMap.clear(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2460 | DenseMap<unsigned, unsigned> SamplerLiteralToIDMap; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2461 | DenseMap<unsigned, unsigned> SamplerLiteralToDescriptorSetMap; |
| 2462 | DenseMap<unsigned, unsigned> SamplerLiteralToBindingMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2463 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2464 | // We might have samplers in the sampler map that are not used |
| 2465 | // in the translation unit. We need to allocate variables |
| 2466 | // for them and bindings too. |
| 2467 | DenseSet<unsigned> used_bindings; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2468 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2469 | auto* var_fn = M.getFunction("clspv.sampler.var.literal"); |
| 2470 | if (!var_fn) return; |
| 2471 | for (auto user : var_fn->users()) { |
| 2472 | // Populate SamplerLiteralToDescriptorSetMap and |
| 2473 | // SamplerLiteralToBindingMap. |
| 2474 | // |
| 2475 | // Look for calls like |
| 2476 | // call %opencl.sampler_t addrspace(2)* |
| 2477 | // @clspv.sampler.var.literal( |
| 2478 | // i32 descriptor, |
| 2479 | // i32 binding, |
| 2480 | // i32 index-into-sampler-map) |
| 2481 | if (auto* call = dyn_cast<CallInst>(user)) { |
| 2482 | const auto index_into_sampler_map = |
| 2483 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue(); |
| 2484 | if (index_into_sampler_map >= sampler_map.size()) { |
| 2485 | errs() << "Out of bounds index to sampler map: " << index_into_sampler_map; |
| 2486 | llvm_unreachable("bad sampler init: out of bounds"); |
| 2487 | } |
| 2488 | |
| 2489 | auto sampler_value = sampler_map[index_into_sampler_map].first; |
| 2490 | const auto descriptor_set = static_cast<unsigned>( |
| 2491 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 2492 | const auto binding = static_cast<unsigned>( |
| 2493 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 2494 | |
| 2495 | SamplerLiteralToDescriptorSetMap[sampler_value] = descriptor_set; |
| 2496 | SamplerLiteralToBindingMap[sampler_value] = binding; |
| 2497 | used_bindings.insert(binding); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | unsigned index = 0; |
| 2502 | for (auto SamplerLiteral : sampler_map) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2503 | // Generate OpVariable. |
| 2504 | // |
| 2505 | // GIDOps[0] : Result Type ID |
| 2506 | // GIDOps[1] : Storage Class |
| 2507 | SPIRVOperandList Ops; |
| 2508 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2509 | Ops << MkId(lookupType(SamplerTy)) |
| 2510 | << MkNum(spv::StorageClassUniformConstant); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2511 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2512 | auto sampler_var_id = nextID++; |
| 2513 | auto *Inst = new SPIRVInstruction(spv::OpVariable, sampler_var_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2514 | SPIRVInstList.push_back(Inst); |
| 2515 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2516 | SamplerMapIndexToIDMap[index] = sampler_var_id; |
| 2517 | SamplerLiteralToIDMap[SamplerLiteral.first] = sampler_var_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2518 | |
| 2519 | // Find Insert Point for OpDecorate. |
| 2520 | auto DecoInsertPoint = |
| 2521 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2522 | [](SPIRVInstruction *Inst) -> bool { |
| 2523 | return Inst->getOpcode() != spv::OpDecorate && |
| 2524 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2525 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2526 | }); |
| 2527 | |
| 2528 | // Ops[0] = Target ID |
| 2529 | // Ops[1] = Decoration (DescriptorSet) |
| 2530 | // Ops[2] = LiteralNumber according to Decoration |
| 2531 | Ops.clear(); |
| 2532 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2533 | unsigned descriptor_set; |
| 2534 | unsigned binding; |
| 2535 | if(SamplerLiteralToBindingMap.find(SamplerLiteral.first) == SamplerLiteralToBindingMap.end()) { |
| 2536 | // This sampler is not actually used. Find the next one. |
| 2537 | for (binding = 0; used_bindings.count(binding); binding++) |
| 2538 | ; |
| 2539 | descriptor_set = 0; // Literal samplers always use descriptor set 0. |
| 2540 | used_bindings.insert(binding); |
| 2541 | } else { |
| 2542 | descriptor_set = SamplerLiteralToDescriptorSetMap[SamplerLiteral.first]; |
| 2543 | binding = SamplerLiteralToBindingMap[SamplerLiteral.first]; |
| 2544 | } |
| 2545 | |
| 2546 | Ops << MkId(sampler_var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2547 | << MkNum(descriptor_set); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2548 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 2549 | descriptorMapOut << "sampler," << SamplerLiteral.first << ",samplerExpr,\"" |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2550 | << SamplerLiteral.second << "\",descriptorSet," |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2551 | << descriptor_set << ",binding," << binding << "\n"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2552 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2553 | auto *DescDecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2554 | SPIRVInstList.insert(DecoInsertPoint, DescDecoInst); |
| 2555 | |
| 2556 | // Ops[0] = Target ID |
| 2557 | // Ops[1] = Decoration (Binding) |
| 2558 | // Ops[2] = LiteralNumber according to Decoration |
| 2559 | Ops.clear(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2560 | Ops << MkId(sampler_var_id) << MkNum(spv::DecorationBinding) |
| 2561 | << MkNum(binding); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2562 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2563 | auto *BindDecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2564 | SPIRVInstList.insert(DecoInsertPoint, BindDecoInst); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2565 | |
| 2566 | index++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2567 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2568 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2569 | |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 2570 | void SPIRVProducerPass::GenerateResourceVars(Module &) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2571 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 2572 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2573 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2574 | // Generate variables. Make one for each of resource var info object. |
| 2575 | for (auto *info : ModuleOrderedResourceVars) { |
| 2576 | Type *type = info->var_fn->getReturnType(); |
| 2577 | // Remap the address space for opaque types. |
| 2578 | switch (info->arg_kind) { |
| 2579 | case clspv::ArgKind::Sampler: |
| 2580 | case clspv::ArgKind::ReadOnlyImage: |
| 2581 | case clspv::ArgKind::WriteOnlyImage: |
| 2582 | type = PointerType::get(type->getPointerElementType(), |
| 2583 | clspv::AddressSpace::UniformConstant); |
| 2584 | break; |
| 2585 | default: |
| 2586 | break; |
| 2587 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2588 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2589 | info->var_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2590 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2591 | const auto type_id = lookupType(type); |
| 2592 | const auto sc = GetStorageClassForArgKind(info->arg_kind); |
| 2593 | SPIRVOperandList Ops; |
| 2594 | Ops << MkId(type_id) << MkNum(sc); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2595 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2596 | auto *Inst = new SPIRVInstruction(spv::OpVariable, info->var_id, Ops); |
| 2597 | SPIRVInstList.push_back(Inst); |
| 2598 | |
| 2599 | // Map calls to the variable-builtin-function. |
| 2600 | for (auto &U : info->var_fn->uses()) { |
| 2601 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 2602 | const auto set = unsigned( |
| 2603 | dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue()); |
| 2604 | const auto binding = unsigned( |
| 2605 | dyn_cast<ConstantInt>(call->getOperand(1))->getZExtValue()); |
| 2606 | if (set == info->descriptor_set && binding == info->binding) { |
| 2607 | switch (info->arg_kind) { |
| 2608 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2609 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2610 | case clspv::ArgKind::Pod: |
| 2611 | // The call maps to the variable directly. |
| 2612 | VMap[call] = info->var_id; |
| 2613 | break; |
| 2614 | case clspv::ArgKind::Sampler: |
| 2615 | case clspv::ArgKind::ReadOnlyImage: |
| 2616 | case clspv::ArgKind::WriteOnlyImage: |
| 2617 | // The call maps to a load we generate later. |
| 2618 | ResourceVarDeferredLoadCalls[call] = info->var_id; |
| 2619 | break; |
| 2620 | default: |
| 2621 | llvm_unreachable("Unhandled arg kind"); |
| 2622 | } |
| 2623 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2624 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2625 | } |
| 2626 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2627 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2628 | // Generate associated decorations. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2629 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2630 | // Find Insert Point for OpDecorate. |
| 2631 | auto DecoInsertPoint = |
| 2632 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2633 | [](SPIRVInstruction *Inst) -> bool { |
| 2634 | return Inst->getOpcode() != spv::OpDecorate && |
| 2635 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2636 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2637 | }); |
| 2638 | |
| 2639 | SPIRVOperandList Ops; |
| 2640 | for (auto *info : ModuleOrderedResourceVars) { |
| 2641 | // Decorate with DescriptorSet and Binding. |
| 2642 | Ops.clear(); |
| 2643 | Ops << MkId(info->var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2644 | << MkNum(info->descriptor_set); |
| 2645 | SPIRVInstList.insert(DecoInsertPoint, |
| 2646 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2647 | |
| 2648 | Ops.clear(); |
| 2649 | Ops << MkId(info->var_id) << MkNum(spv::DecorationBinding) |
| 2650 | << MkNum(info->binding); |
| 2651 | SPIRVInstList.insert(DecoInsertPoint, |
| 2652 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2653 | |
| 2654 | // Generate NonWritable and NonReadable |
| 2655 | switch (info->arg_kind) { |
| 2656 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2657 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2658 | if (info->var_fn->getReturnType()->getPointerAddressSpace() == |
| 2659 | clspv::AddressSpace::Constant) { |
| 2660 | Ops.clear(); |
| 2661 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonWritable); |
| 2662 | SPIRVInstList.insert(DecoInsertPoint, |
| 2663 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2664 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2665 | break; |
| 2666 | case clspv::ArgKind::ReadOnlyImage: |
| 2667 | Ops.clear(); |
| 2668 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonWritable); |
| 2669 | SPIRVInstList.insert(DecoInsertPoint, |
| 2670 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2671 | break; |
| 2672 | case clspv::ArgKind::WriteOnlyImage: |
| 2673 | Ops.clear(); |
| 2674 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonReadable); |
| 2675 | SPIRVInstList.insert(DecoInsertPoint, |
| 2676 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
| 2677 | break; |
| 2678 | default: |
| 2679 | break; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) { |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 2685 | Module& M = *GV.getParent(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2686 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 2687 | ValueMapType &VMap = getValueMap(); |
| 2688 | std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2689 | const DataLayout &DL = GV.getParent()->getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2690 | |
| 2691 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 2692 | Type *Ty = GV.getType(); |
| 2693 | PointerType *PTy = cast<PointerType>(Ty); |
| 2694 | |
| 2695 | uint32_t InitializerID = 0; |
| 2696 | |
| 2697 | // Workgroup size is handled differently (it goes into a constant) |
| 2698 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 2699 | std::vector<bool> HasMDVec; |
| 2700 | uint32_t PrevXDimCst = 0xFFFFFFFF; |
| 2701 | uint32_t PrevYDimCst = 0xFFFFFFFF; |
| 2702 | uint32_t PrevZDimCst = 0xFFFFFFFF; |
| 2703 | for (Function &Func : *GV.getParent()) { |
| 2704 | if (Func.isDeclaration()) { |
| 2705 | continue; |
| 2706 | } |
| 2707 | |
| 2708 | // We only need to check kernels. |
| 2709 | if (Func.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 2710 | continue; |
| 2711 | } |
| 2712 | |
| 2713 | if (const MDNode *MD = |
| 2714 | dyn_cast<Function>(&Func)->getMetadata("reqd_work_group_size")) { |
| 2715 | uint32_t CurXDimCst = static_cast<uint32_t>( |
| 2716 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 2717 | uint32_t CurYDimCst = static_cast<uint32_t>( |
| 2718 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 2719 | uint32_t CurZDimCst = static_cast<uint32_t>( |
| 2720 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 2721 | |
| 2722 | if (PrevXDimCst == 0xFFFFFFFF && PrevYDimCst == 0xFFFFFFFF && |
| 2723 | PrevZDimCst == 0xFFFFFFFF) { |
| 2724 | PrevXDimCst = CurXDimCst; |
| 2725 | PrevYDimCst = CurYDimCst; |
| 2726 | PrevZDimCst = CurZDimCst; |
| 2727 | } else if (CurXDimCst != PrevXDimCst || CurYDimCst != PrevYDimCst || |
| 2728 | CurZDimCst != PrevZDimCst) { |
| 2729 | llvm_unreachable( |
| 2730 | "reqd_work_group_size must be the same across all kernels"); |
| 2731 | } else { |
| 2732 | continue; |
| 2733 | } |
| 2734 | |
| 2735 | // |
| 2736 | // Generate OpConstantComposite. |
| 2737 | // |
| 2738 | // Ops[0] : Result Type ID |
| 2739 | // Ops[1] : Constant size for x dimension. |
| 2740 | // Ops[2] : Constant size for y dimension. |
| 2741 | // Ops[3] : Constant size for z dimension. |
| 2742 | SPIRVOperandList Ops; |
| 2743 | |
| 2744 | uint32_t XDimCstID = |
| 2745 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(0))]; |
| 2746 | uint32_t YDimCstID = |
| 2747 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(1))]; |
| 2748 | uint32_t ZDimCstID = |
| 2749 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(2))]; |
| 2750 | |
| 2751 | InitializerID = nextID; |
| 2752 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2753 | Ops << MkId(lookupType(Ty->getPointerElementType())) << MkId(XDimCstID) |
| 2754 | << MkId(YDimCstID) << MkId(ZDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2755 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2756 | auto *Inst = |
| 2757 | new SPIRVInstruction(spv::OpConstantComposite, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2758 | SPIRVInstList.push_back(Inst); |
| 2759 | |
| 2760 | HasMDVec.push_back(true); |
| 2761 | } else { |
| 2762 | HasMDVec.push_back(false); |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | // Check all kernels have same definitions for work_group_size. |
| 2767 | bool HasMD = false; |
| 2768 | if (!HasMDVec.empty()) { |
| 2769 | HasMD = HasMDVec[0]; |
| 2770 | for (uint32_t i = 1; i < HasMDVec.size(); i++) { |
| 2771 | if (HasMD != HasMDVec[i]) { |
| 2772 | llvm_unreachable( |
| 2773 | "Kernels should have consistent work group size definition"); |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | // If all kernels do not have metadata for reqd_work_group_size, generate |
| 2779 | // OpSpecConstants for x/y/z dimension. |
| 2780 | if (!HasMD) { |
| 2781 | // |
| 2782 | // Generate OpSpecConstants for x/y/z dimension. |
| 2783 | // |
| 2784 | // Ops[0] : Result Type ID |
| 2785 | // Ops[1] : Constant size for x/y/z dimension (Literal Number). |
| 2786 | uint32_t XDimCstID = 0; |
| 2787 | uint32_t YDimCstID = 0; |
| 2788 | uint32_t ZDimCstID = 0; |
| 2789 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2790 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2791 | uint32_t result_type_id = |
| 2792 | lookupType(Ty->getPointerElementType()->getSequentialElementType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2793 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2794 | // X Dimension |
| 2795 | Ops << MkId(result_type_id) << MkNum(1); |
| 2796 | XDimCstID = nextID++; |
| 2797 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2798 | new SPIRVInstruction(spv::OpSpecConstant, XDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2799 | |
| 2800 | // Y Dimension |
| 2801 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2802 | Ops << MkId(result_type_id) << MkNum(1); |
| 2803 | YDimCstID = nextID++; |
| 2804 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2805 | new SPIRVInstruction(spv::OpSpecConstant, YDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2806 | |
| 2807 | // Z Dimension |
| 2808 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2809 | Ops << MkId(result_type_id) << MkNum(1); |
| 2810 | ZDimCstID = nextID++; |
| 2811 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2812 | new SPIRVInstruction(spv::OpSpecConstant, ZDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2813 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2814 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2815 | BuiltinDimVec.push_back(XDimCstID); |
| 2816 | BuiltinDimVec.push_back(YDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2817 | BuiltinDimVec.push_back(ZDimCstID); |
| 2818 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2819 | |
| 2820 | // |
| 2821 | // Generate OpSpecConstantComposite. |
| 2822 | // |
| 2823 | // Ops[0] : Result Type ID |
| 2824 | // Ops[1] : Constant size for x dimension. |
| 2825 | // Ops[2] : Constant size for y dimension. |
| 2826 | // Ops[3] : Constant size for z dimension. |
| 2827 | InitializerID = nextID; |
| 2828 | |
| 2829 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2830 | Ops << MkId(lookupType(Ty->getPointerElementType())) << MkId(XDimCstID) |
| 2831 | << MkId(YDimCstID) << MkId(ZDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2832 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2833 | auto *Inst = |
| 2834 | new SPIRVInstruction(spv::OpSpecConstantComposite, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2835 | SPIRVInstList.push_back(Inst); |
| 2836 | } |
| 2837 | } |
| 2838 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2839 | VMap[&GV] = nextID; |
| 2840 | |
| 2841 | // |
| 2842 | // Generate OpVariable. |
| 2843 | // |
| 2844 | // GIDOps[0] : Result Type ID |
| 2845 | // GIDOps[1] : Storage Class |
| 2846 | SPIRVOperandList Ops; |
| 2847 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2848 | const auto AS = PTy->getAddressSpace(); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2849 | Ops << MkId(lookupType(Ty)) << MkNum(GetStorageClass(AS)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2850 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2851 | if (GV.hasInitializer()) { |
| 2852 | InitializerID = VMap[GV.getInitializer()]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2853 | } |
| 2854 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2855 | const bool module_scope_constant_external_init = |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2856 | (AS == AddressSpace::Constant) && GV.hasInitializer() && |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2857 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 2858 | |
| 2859 | if (0 != InitializerID) { |
| 2860 | if (!module_scope_constant_external_init) { |
| 2861 | // Emit the ID of the intiializer as part of the variable definition. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2862 | Ops << MkId(InitializerID); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | const uint32_t var_id = nextID++; |
| 2866 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2867 | auto *Inst = new SPIRVInstruction(spv::OpVariable, var_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2868 | SPIRVInstList.push_back(Inst); |
| 2869 | |
| 2870 | // If we have a builtin. |
| 2871 | if (spv::BuiltInMax != BuiltinType) { |
| 2872 | // Find Insert Point for OpDecorate. |
| 2873 | auto DecoInsertPoint = |
| 2874 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2875 | [](SPIRVInstruction *Inst) -> bool { |
| 2876 | return Inst->getOpcode() != spv::OpDecorate && |
| 2877 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2878 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2879 | }); |
| 2880 | // |
| 2881 | // Generate OpDecorate. |
| 2882 | // |
| 2883 | // DOps[0] = Target ID |
| 2884 | // DOps[1] = Decoration (Builtin) |
| 2885 | // DOps[2] = BuiltIn ID |
| 2886 | uint32_t ResultID; |
| 2887 | |
| 2888 | // WorkgroupSize is different, we decorate the constant composite that has |
| 2889 | // its value, rather than the variable that we use to access the value. |
| 2890 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 2891 | ResultID = InitializerID; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 2892 | // Save both the value and variable IDs for later. |
| 2893 | WorkgroupSizeValueID = InitializerID; |
| 2894 | WorkgroupSizeVarID = VMap[&GV]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2895 | } else { |
| 2896 | ResultID = VMap[&GV]; |
| 2897 | } |
| 2898 | |
| 2899 | SPIRVOperandList DOps; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2900 | DOps << MkId(ResultID) << MkNum(spv::DecorationBuiltIn) |
| 2901 | << MkNum(BuiltinType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2902 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2903 | auto *DescDecoInst = new SPIRVInstruction(spv::OpDecorate, DOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2904 | SPIRVInstList.insert(DecoInsertPoint, DescDecoInst); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2905 | } else if (module_scope_constant_external_init) { |
| 2906 | // This module scope constant is initialized from a storage buffer with data |
| 2907 | // provided by the host at binding 0 of the next descriptor set. |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 2908 | const uint32_t descriptor_set = TakeDescriptorIndex(&M); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2909 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2910 | // Emit the intializer to the descriptor map file. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2911 | // Use "kind,buffer" to indicate storage buffer. We might want to expand |
| 2912 | // that later to other types, like uniform buffer. |
| 2913 | descriptorMapOut << "constant,descriptorSet," << descriptor_set |
| 2914 | << ",binding,0,kind,buffer,hexbytes,"; |
| 2915 | clspv::ConstantEmitter(DL, descriptorMapOut).Emit(GV.getInitializer()); |
| 2916 | descriptorMapOut << "\n"; |
| 2917 | |
| 2918 | // Find Insert Point for OpDecorate. |
| 2919 | auto DecoInsertPoint = |
| 2920 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 2921 | [](SPIRVInstruction *Inst) -> bool { |
| 2922 | return Inst->getOpcode() != spv::OpDecorate && |
| 2923 | Inst->getOpcode() != spv::OpMemberDecorate && |
| 2924 | Inst->getOpcode() != spv::OpExtInstImport; |
| 2925 | }); |
| 2926 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2927 | // OpDecorate %var Binding <binding> |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2928 | SPIRVOperandList DOps; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2929 | DOps << MkId(var_id) << MkNum(spv::DecorationBinding) << MkNum(0); |
| 2930 | DecoInsertPoint = SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2931 | DecoInsertPoint, new SPIRVInstruction(spv::OpDecorate, DOps)); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2932 | |
| 2933 | // OpDecorate %var DescriptorSet <descriptor_set> |
| 2934 | DOps.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2935 | DOps << MkId(var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2936 | << MkNum(descriptor_set); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2937 | SPIRVInstList.insert(DecoInsertPoint, |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2938 | new SPIRVInstruction(spv::OpDecorate, DOps)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2939 | } |
| 2940 | } |
| 2941 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2942 | void SPIRVProducerPass::GenerateWorkgroupVars() { |
| 2943 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 2944 | for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_; |
| 2945 | ++spec_id) { |
| 2946 | LocalArgInfo& info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2947 | |
| 2948 | // Generate OpVariable. |
| 2949 | // |
| 2950 | // GIDOps[0] : Result Type ID |
| 2951 | // GIDOps[1] : Storage Class |
| 2952 | SPIRVOperandList Ops; |
| 2953 | Ops << MkId(info.ptr_array_type_id) << MkNum(spv::StorageClassWorkgroup); |
| 2954 | |
| 2955 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2956 | new SPIRVInstruction(spv::OpVariable, info.variable_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2957 | } |
| 2958 | } |
| 2959 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2960 | void SPIRVProducerPass::GenerateDescriptorMapInfo(const DataLayout &DL, |
| 2961 | Function &F) { |
David Neto | c5fb524 | 2018-07-30 13:28:31 -0400 | [diff] [blame] | 2962 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 2963 | return; |
| 2964 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2965 | // Gather the list of resources that are used by this function's arguments. |
| 2966 | auto &resource_var_at_index = FunctionToResourceVarsMap[&F]; |
| 2967 | |
| 2968 | auto remap_arg_kind = [](StringRef argKind) { |
| 2969 | return clspv::Option::PodArgsInUniformBuffer() && argKind.equals("pod") |
| 2970 | ? "pod_ubo" |
| 2971 | : argKind; |
| 2972 | }; |
| 2973 | |
| 2974 | auto *fty = F.getType()->getPointerElementType(); |
| 2975 | auto *func_ty = dyn_cast<FunctionType>(fty); |
| 2976 | |
| 2977 | // If we've clustereed POD arguments, then argument details are in metadata. |
| 2978 | // If an argument maps to a resource variable, then get descriptor set and |
| 2979 | // binding from the resoure variable. Other info comes from the metadata. |
| 2980 | const auto *arg_map = F.getMetadata("kernel_arg_map"); |
| 2981 | if (arg_map) { |
| 2982 | for (const auto &arg : arg_map->operands()) { |
| 2983 | const MDNode *arg_node = dyn_cast<MDNode>(arg.get()); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2984 | assert(arg_node->getNumOperands() == 7); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2985 | const auto name = |
| 2986 | dyn_cast<MDString>(arg_node->getOperand(0))->getString(); |
| 2987 | const auto old_index = |
| 2988 | dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue(); |
| 2989 | // Remapped argument index |
| 2990 | const auto new_index = |
| 2991 | dyn_extract<ConstantInt>(arg_node->getOperand(2))->getZExtValue(); |
| 2992 | const auto offset = |
| 2993 | dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue(); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2994 | const auto arg_size = |
| 2995 | dyn_extract<ConstantInt>(arg_node->getOperand(4))->getZExtValue(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2996 | const auto argKind = remap_arg_kind( |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2997 | dyn_cast<MDString>(arg_node->getOperand(5))->getString()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2998 | const auto spec_id = |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2999 | dyn_extract<ConstantInt>(arg_node->getOperand(6))->getSExtValue(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3000 | if (spec_id > 0) { |
| 3001 | // This was a pointer-to-local argument. It is not associated with a |
| 3002 | // resource variable. |
| 3003 | descriptorMapOut << "kernel," << F.getName() << ",arg," << name |
| 3004 | << ",argOrdinal," << old_index << ",argKind," |
| 3005 | << argKind << ",arrayElemSize," |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 3006 | << GetTypeAllocSize( |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3007 | func_ty->getParamType(unsigned(new_index)) |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 3008 | ->getPointerElementType(), DL) |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3009 | << ",arrayNumElemSpecId," << spec_id << "\n"; |
| 3010 | } else { |
| 3011 | auto *info = resource_var_at_index[new_index]; |
| 3012 | assert(info); |
| 3013 | descriptorMapOut << "kernel," << F.getName() << ",arg," << name |
| 3014 | << ",argOrdinal," << old_index << ",descriptorSet," |
| 3015 | << info->descriptor_set << ",binding," << info->binding |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3016 | << ",offset," << offset << ",argKind," << argKind; |
| 3017 | if (argKind.startswith("pod")) { |
| 3018 | descriptorMapOut << ",argSize," << arg_size; |
| 3019 | } |
| 3020 | descriptorMapOut << "\n"; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3021 | } |
| 3022 | } |
| 3023 | } else { |
| 3024 | // There is no argument map. |
| 3025 | // Take descriptor info from the resource variable calls. |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3026 | // Take argument name and size from the arguments list. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3027 | |
| 3028 | SmallVector<Argument *, 4> arguments; |
| 3029 | for (auto &arg : F.args()) { |
| 3030 | arguments.push_back(&arg); |
| 3031 | } |
| 3032 | |
| 3033 | unsigned arg_index = 0; |
| 3034 | for (auto *info : resource_var_at_index) { |
| 3035 | if (info) { |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3036 | auto arg = arguments[arg_index]; |
| 3037 | unsigned arg_size; |
| 3038 | if (info->arg_kind == clspv::ArgKind::Pod) { |
| 3039 | arg_size = DL.getTypeStoreSize(arg->getType()); |
| 3040 | } |
| 3041 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3042 | descriptorMapOut << "kernel," << F.getName() << ",arg," |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3043 | << arg->getName() << ",argOrdinal," |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3044 | << arg_index << ",descriptorSet," |
| 3045 | << info->descriptor_set << ",binding," << info->binding |
| 3046 | << ",offset," << 0 << ",argKind," |
| 3047 | << remap_arg_kind( |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3048 | clspv::GetArgKindName(info->arg_kind)); |
| 3049 | if (info->arg_kind == clspv::ArgKind::Pod) { |
| 3050 | descriptorMapOut << ",argSize," << arg_size; |
| 3051 | } |
| 3052 | descriptorMapOut << "\n"; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3053 | } |
| 3054 | arg_index++; |
| 3055 | } |
| 3056 | // Generate mappings for pointer-to-local arguments. |
| 3057 | for (arg_index = 0; arg_index < arguments.size(); ++arg_index) { |
| 3058 | Argument *arg = arguments[arg_index]; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 3059 | auto where = LocalArgSpecIds.find(arg); |
| 3060 | if (where != LocalArgSpecIds.end()) { |
| 3061 | auto &local_arg_info = LocalSpecIdInfoMap[where->second]; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3062 | descriptorMapOut << "kernel," << F.getName() << ",arg," |
| 3063 | << arg->getName() << ",argOrdinal," << arg_index |
| 3064 | << ",argKind," |
| 3065 | << "local" |
| 3066 | << ",arrayElemSize," |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 3067 | << GetTypeAllocSize(local_arg_info.elem_type, DL) |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3068 | << ",arrayNumElemSpecId," << local_arg_info.spec_id |
| 3069 | << "\n"; |
| 3070 | } |
| 3071 | } |
| 3072 | } |
| 3073 | } |
| 3074 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3075 | void SPIRVProducerPass::GenerateFuncPrologue(Function &F) { |
| 3076 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3077 | ValueMapType &VMap = getValueMap(); |
| 3078 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3079 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 3080 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 3081 | |
| 3082 | FunctionType *FTy = F.getFunctionType(); |
| 3083 | |
| 3084 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3085 | // Generate OPFunction. |
| 3086 | // |
| 3087 | |
| 3088 | // FOps[0] : Result Type ID |
| 3089 | // FOps[1] : Function Control |
| 3090 | // FOps[2] : Function Type ID |
| 3091 | SPIRVOperandList FOps; |
| 3092 | |
| 3093 | // Find SPIRV instruction for return type. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3094 | FOps << MkId(lookupType(FTy->getReturnType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3095 | |
| 3096 | // Check function attributes for SPIRV Function Control. |
| 3097 | uint32_t FuncControl = spv::FunctionControlMaskNone; |
| 3098 | if (F.hasFnAttribute(Attribute::AlwaysInline)) { |
| 3099 | FuncControl |= spv::FunctionControlInlineMask; |
| 3100 | } |
| 3101 | if (F.hasFnAttribute(Attribute::NoInline)) { |
| 3102 | FuncControl |= spv::FunctionControlDontInlineMask; |
| 3103 | } |
| 3104 | // TODO: Check llvm attribute for Function Control Pure. |
| 3105 | if (F.hasFnAttribute(Attribute::ReadOnly)) { |
| 3106 | FuncControl |= spv::FunctionControlPureMask; |
| 3107 | } |
| 3108 | // TODO: Check llvm attribute for Function Control Const. |
| 3109 | if (F.hasFnAttribute(Attribute::ReadNone)) { |
| 3110 | FuncControl |= spv::FunctionControlConstMask; |
| 3111 | } |
| 3112 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3113 | FOps << MkNum(FuncControl); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3114 | |
| 3115 | uint32_t FTyID; |
| 3116 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 3117 | SmallVector<Type *, 4> NewFuncParamTys; |
| 3118 | FunctionType *NewFTy = |
| 3119 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 3120 | FTyID = lookupType(NewFTy); |
| 3121 | } else { |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 3122 | // Handle regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3123 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 3124 | FTyID = lookupType(GlobalConstFuncTyMap[FTy].first); |
| 3125 | } else { |
| 3126 | FTyID = lookupType(FTy); |
| 3127 | } |
| 3128 | } |
| 3129 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3130 | FOps << MkId(FTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3131 | |
| 3132 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 3133 | EntryPoints.push_back(std::make_pair(&F, nextID)); |
| 3134 | } |
| 3135 | |
| 3136 | VMap[&F] = nextID; |
| 3137 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 3138 | if (clspv::Option::ShowIDs()) { |
David Neto | b05675d | 2018-02-16 12:37:49 -0500 | [diff] [blame] | 3139 | errs() << "Function " << F.getName() << " is " << nextID << "\n"; |
| 3140 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3141 | // Generate SPIRV instruction for function. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3142 | auto *FuncInst = new SPIRVInstruction(spv::OpFunction, nextID++, FOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3143 | SPIRVInstList.push_back(FuncInst); |
| 3144 | |
| 3145 | // |
| 3146 | // Generate OpFunctionParameter for Normal function. |
| 3147 | // |
| 3148 | |
| 3149 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 3150 | // Iterate Argument for name instead of param type from function type. |
| 3151 | unsigned ArgIdx = 0; |
| 3152 | for (Argument &Arg : F.args()) { |
| 3153 | VMap[&Arg] = nextID; |
| 3154 | |
| 3155 | // ParamOps[0] : Result Type ID |
| 3156 | SPIRVOperandList ParamOps; |
| 3157 | |
| 3158 | // Find SPIRV instruction for parameter type. |
| 3159 | uint32_t ParamTyID = lookupType(Arg.getType()); |
| 3160 | if (PointerType *PTy = dyn_cast<PointerType>(Arg.getType())) { |
| 3161 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 3162 | if (ArgIdx == GlobalConstFuncTyMap[FTy].second) { |
| 3163 | Type *EleTy = PTy->getPointerElementType(); |
| 3164 | Type *ArgTy = |
| 3165 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 3166 | ParamTyID = lookupType(ArgTy); |
| 3167 | GlobalConstArgSet.insert(&Arg); |
| 3168 | } |
| 3169 | } |
| 3170 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3171 | ParamOps << MkId(ParamTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3172 | |
| 3173 | // Generate SPIRV instruction for parameter. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3174 | auto *ParamInst = |
| 3175 | new SPIRVInstruction(spv::OpFunctionParameter, nextID++, ParamOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3176 | SPIRVInstList.push_back(ParamInst); |
| 3177 | |
| 3178 | ArgIdx++; |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3183 | void SPIRVProducerPass::GenerateModuleInfo(Module& module) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3184 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3185 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
| 3186 | ValueMapType &VMap = getValueMap(); |
| 3187 | ValueList &EntryPointInterfaces = getEntryPointInterfacesVec(); |
| 3188 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 3189 | std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec(); |
| 3190 | |
| 3191 | // Set up insert point. |
| 3192 | auto InsertPoint = SPIRVInstList.begin(); |
| 3193 | |
| 3194 | // |
| 3195 | // Generate OpCapability |
| 3196 | // |
| 3197 | // TODO: Which llvm information is mapped to SPIRV Capapbility? |
| 3198 | |
| 3199 | // Ops[0] = Capability |
| 3200 | SPIRVOperandList Ops; |
| 3201 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3202 | auto *CapInst = |
| 3203 | new SPIRVInstruction(spv::OpCapability, {MkNum(spv::CapabilityShader)}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3204 | SPIRVInstList.insert(InsertPoint, CapInst); |
| 3205 | |
| 3206 | for (Type *Ty : getTypeList()) { |
| 3207 | // Find the i16 type. |
| 3208 | if (Ty->isIntegerTy(16)) { |
| 3209 | // Generate OpCapability for i16 type. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3210 | SPIRVInstList.insert(InsertPoint, |
| 3211 | new SPIRVInstruction(spv::OpCapability, |
| 3212 | {MkNum(spv::CapabilityInt16)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3213 | } else if (Ty->isIntegerTy(64)) { |
| 3214 | // Generate OpCapability for i64 type. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3215 | SPIRVInstList.insert(InsertPoint, |
| 3216 | new SPIRVInstruction(spv::OpCapability, |
| 3217 | {MkNum(spv::CapabilityInt64)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3218 | } else if (Ty->isHalfTy()) { |
| 3219 | // Generate OpCapability for half type. |
| 3220 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3221 | InsertPoint, new SPIRVInstruction(spv::OpCapability, |
| 3222 | {MkNum(spv::CapabilityFloat16)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3223 | } else if (Ty->isDoubleTy()) { |
| 3224 | // Generate OpCapability for double type. |
| 3225 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3226 | InsertPoint, new SPIRVInstruction(spv::OpCapability, |
| 3227 | {MkNum(spv::CapabilityFloat64)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3228 | } else if (auto *STy = dyn_cast<StructType>(Ty)) { |
| 3229 | if (STy->isOpaque()) { |
David Neto | 565571c | 2017-08-21 12:00:05 -0400 | [diff] [blame] | 3230 | if (STy->getName().equals("opencl.image2d_wo_t") || |
| 3231 | STy->getName().equals("opencl.image3d_wo_t")) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3232 | // Generate OpCapability for write only image type. |
| 3233 | SPIRVInstList.insert( |
| 3234 | InsertPoint, |
| 3235 | new SPIRVInstruction( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3236 | spv::OpCapability, |
| 3237 | {MkNum(spv::CapabilityStorageImageWriteWithoutFormat)})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | } |
| 3241 | } |
| 3242 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3243 | { // OpCapability ImageQuery |
| 3244 | bool hasImageQuery = false; |
| 3245 | for (const char *imageQuery : { |
| 3246 | "_Z15get_image_width14ocl_image2d_ro", |
| 3247 | "_Z15get_image_width14ocl_image2d_wo", |
| 3248 | "_Z16get_image_height14ocl_image2d_ro", |
| 3249 | "_Z16get_image_height14ocl_image2d_wo", |
| 3250 | }) { |
| 3251 | if (module.getFunction(imageQuery)) { |
| 3252 | hasImageQuery = true; |
| 3253 | break; |
| 3254 | } |
| 3255 | } |
| 3256 | if (hasImageQuery) { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3257 | auto *ImageQueryCapInst = new SPIRVInstruction( |
| 3258 | spv::OpCapability, {MkNum(spv::CapabilityImageQuery)}); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3259 | SPIRVInstList.insert(InsertPoint, ImageQueryCapInst); |
| 3260 | } |
| 3261 | } |
| 3262 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3263 | if (hasVariablePointers()) { |
| 3264 | // |
| 3265 | // Generate OpCapability and OpExtension |
| 3266 | // |
| 3267 | |
| 3268 | // |
| 3269 | // Generate OpCapability. |
| 3270 | // |
| 3271 | // Ops[0] = Capability |
| 3272 | // |
| 3273 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3274 | Ops << MkNum(spv::CapabilityVariablePointers); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3275 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3276 | SPIRVInstList.insert(InsertPoint, |
| 3277 | new SPIRVInstruction(spv::OpCapability, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3278 | |
| 3279 | // |
| 3280 | // Generate OpExtension. |
| 3281 | // |
| 3282 | // Ops[0] = Name (Literal String) |
| 3283 | // |
David Neto | a772fd1 | 2017-08-04 14:17:33 -0400 | [diff] [blame] | 3284 | for (auto extension : {"SPV_KHR_storage_buffer_storage_class", |
| 3285 | "SPV_KHR_variable_pointers"}) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3286 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3287 | auto *ExtensionInst = |
| 3288 | new SPIRVInstruction(spv::OpExtension, {MkString(extension)}); |
David Neto | a772fd1 | 2017-08-04 14:17:33 -0400 | [diff] [blame] | 3289 | SPIRVInstList.insert(InsertPoint, ExtensionInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3290 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | if (ExtInstImportID) { |
| 3294 | ++InsertPoint; |
| 3295 | } |
| 3296 | |
| 3297 | // |
| 3298 | // Generate OpMemoryModel |
| 3299 | // |
| 3300 | // Memory model for Vulkan will always be GLSL450. |
| 3301 | |
| 3302 | // Ops[0] = Addressing Model |
| 3303 | // Ops[1] = Memory Model |
| 3304 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3305 | Ops << MkNum(spv::AddressingModelLogical) << MkNum(spv::MemoryModelGLSL450); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3306 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3307 | auto *MemModelInst = new SPIRVInstruction(spv::OpMemoryModel, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3308 | SPIRVInstList.insert(InsertPoint, MemModelInst); |
| 3309 | |
| 3310 | // |
| 3311 | // Generate OpEntryPoint |
| 3312 | // |
| 3313 | for (auto EntryPoint : EntryPoints) { |
| 3314 | // Ops[0] = Execution Model |
| 3315 | // Ops[1] = EntryPoint ID |
| 3316 | // Ops[2] = Name (Literal String) |
| 3317 | // ... |
| 3318 | // |
| 3319 | // TODO: Do we need to consider Interface ID for forward references??? |
| 3320 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3321 | const StringRef& name = EntryPoint.first->getName(); |
| 3322 | Ops << MkNum(spv::ExecutionModelGLCompute) << MkId(EntryPoint.second) |
| 3323 | << MkString(name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3324 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3325 | for (Value *Interface : EntryPointInterfaces) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3326 | Ops << MkId(VMap[Interface]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3327 | } |
| 3328 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3329 | auto *EntryPointInst = new SPIRVInstruction(spv::OpEntryPoint, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3330 | SPIRVInstList.insert(InsertPoint, EntryPointInst); |
| 3331 | } |
| 3332 | |
| 3333 | for (auto EntryPoint : EntryPoints) { |
| 3334 | if (const MDNode *MD = dyn_cast<Function>(EntryPoint.first) |
| 3335 | ->getMetadata("reqd_work_group_size")) { |
| 3336 | |
| 3337 | if (!BuiltinDimVec.empty()) { |
| 3338 | llvm_unreachable( |
| 3339 | "Kernels should have consistent work group size definition"); |
| 3340 | } |
| 3341 | |
| 3342 | // |
| 3343 | // Generate OpExecutionMode |
| 3344 | // |
| 3345 | |
| 3346 | // Ops[0] = Entry Point ID |
| 3347 | // Ops[1] = Execution Mode |
| 3348 | // Ops[2] ... Ops[n] = Optional literals according to Execution Mode |
| 3349 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3350 | Ops << MkId(EntryPoint.second) |
| 3351 | << MkNum(spv::ExecutionModeLocalSize); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3352 | |
| 3353 | uint32_t XDim = static_cast<uint32_t>( |
| 3354 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 3355 | uint32_t YDim = static_cast<uint32_t>( |
| 3356 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 3357 | uint32_t ZDim = static_cast<uint32_t>( |
| 3358 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 3359 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3360 | Ops << MkNum(XDim) << MkNum(YDim) << MkNum(ZDim); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3361 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3362 | auto *ExecModeInst = new SPIRVInstruction(spv::OpExecutionMode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3363 | SPIRVInstList.insert(InsertPoint, ExecModeInst); |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | // |
| 3368 | // Generate OpSource. |
| 3369 | // |
| 3370 | // Ops[0] = SourceLanguage ID |
| 3371 | // Ops[1] = Version (LiteralNum) |
| 3372 | // |
| 3373 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3374 | Ops << MkNum(spv::SourceLanguageOpenCL_C) << MkNum(120); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3375 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3376 | auto *OpenSourceInst = new SPIRVInstruction(spv::OpSource, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3377 | SPIRVInstList.insert(InsertPoint, OpenSourceInst); |
| 3378 | |
| 3379 | if (!BuiltinDimVec.empty()) { |
| 3380 | // |
| 3381 | // Generate OpDecorates for x/y/z dimension. |
| 3382 | // |
| 3383 | // Ops[0] = Target ID |
| 3384 | // Ops[1] = Decoration (SpecId) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3385 | // Ops[2] = Specialization Constant ID (Literal Number) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3386 | |
| 3387 | // X Dimension |
| 3388 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3389 | Ops << MkId(BuiltinDimVec[0]) << MkNum(spv::DecorationSpecId) << MkNum(0); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3390 | SPIRVInstList.insert(InsertPoint, |
| 3391 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3392 | |
| 3393 | // Y Dimension |
| 3394 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3395 | Ops << MkId(BuiltinDimVec[1]) << MkNum(spv::DecorationSpecId) << MkNum(1); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3396 | SPIRVInstList.insert(InsertPoint, |
| 3397 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3398 | |
| 3399 | // Z Dimension |
| 3400 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3401 | Ops << MkId(BuiltinDimVec[2]) << MkNum(spv::DecorationSpecId) << MkNum(2); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3402 | SPIRVInstList.insert(InsertPoint, |
| 3403 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3404 | } |
| 3405 | } |
| 3406 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3407 | void SPIRVProducerPass::GenerateEntryPointInitialStores() { |
| 3408 | // Work around a driver bug. Initializers on Private variables might not |
| 3409 | // work. So the start of the kernel should store the initializer value to the |
| 3410 | // variables. Yes, *every* entry point pays this cost if *any* entry point |
| 3411 | // uses this builtin. At this point I judge this to be an acceptable tradeoff |
| 3412 | // of complexity vs. runtime, for a broken driver. |
| 3413 | // TODO(dneto): Remove this at some point once fixed drivers are widely available. |
| 3414 | if (WorkgroupSizeVarID) { |
| 3415 | assert(WorkgroupSizeValueID); |
| 3416 | |
| 3417 | SPIRVOperandList Ops; |
| 3418 | Ops << MkId(WorkgroupSizeVarID) << MkId(WorkgroupSizeValueID); |
| 3419 | |
| 3420 | auto *Inst = new SPIRVInstruction(spv::OpStore, Ops); |
| 3421 | getSPIRVInstList().push_back(Inst); |
| 3422 | } |
| 3423 | } |
| 3424 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3425 | void SPIRVProducerPass::GenerateFuncBody(Function &F) { |
| 3426 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3427 | ValueMapType &VMap = getValueMap(); |
| 3428 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3429 | const bool IsKernel = F.getCallingConv() == CallingConv::SPIR_KERNEL; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3430 | |
| 3431 | for (BasicBlock &BB : F) { |
| 3432 | // Register BasicBlock to ValueMap. |
| 3433 | VMap[&BB] = nextID; |
| 3434 | |
| 3435 | // |
| 3436 | // Generate OpLabel for Basic Block. |
| 3437 | // |
| 3438 | SPIRVOperandList Ops; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3439 | auto *Inst = new SPIRVInstruction(spv::OpLabel, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3440 | SPIRVInstList.push_back(Inst); |
| 3441 | |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3442 | // OpVariable instructions must come first. |
| 3443 | for (Instruction &I : BB) { |
| 3444 | if (isa<AllocaInst>(I)) { |
| 3445 | GenerateInstruction(I); |
| 3446 | } |
| 3447 | } |
| 3448 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3449 | if (&BB == &F.getEntryBlock() && IsKernel) { |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3450 | if (clspv::Option::HackInitializers()) { |
| 3451 | GenerateEntryPointInitialStores(); |
| 3452 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | for (Instruction &I : BB) { |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3456 | if (!isa<AllocaInst>(I)) { |
| 3457 | GenerateInstruction(I); |
| 3458 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | spv::Op SPIRVProducerPass::GetSPIRVCmpOpcode(CmpInst *I) { |
| 3464 | const std::map<CmpInst::Predicate, spv::Op> Map = { |
| 3465 | {CmpInst::ICMP_EQ, spv::OpIEqual}, |
| 3466 | {CmpInst::ICMP_NE, spv::OpINotEqual}, |
| 3467 | {CmpInst::ICMP_UGT, spv::OpUGreaterThan}, |
| 3468 | {CmpInst::ICMP_UGE, spv::OpUGreaterThanEqual}, |
| 3469 | {CmpInst::ICMP_ULT, spv::OpULessThan}, |
| 3470 | {CmpInst::ICMP_ULE, spv::OpULessThanEqual}, |
| 3471 | {CmpInst::ICMP_SGT, spv::OpSGreaterThan}, |
| 3472 | {CmpInst::ICMP_SGE, spv::OpSGreaterThanEqual}, |
| 3473 | {CmpInst::ICMP_SLT, spv::OpSLessThan}, |
| 3474 | {CmpInst::ICMP_SLE, spv::OpSLessThanEqual}, |
| 3475 | {CmpInst::FCMP_OEQ, spv::OpFOrdEqual}, |
| 3476 | {CmpInst::FCMP_OGT, spv::OpFOrdGreaterThan}, |
| 3477 | {CmpInst::FCMP_OGE, spv::OpFOrdGreaterThanEqual}, |
| 3478 | {CmpInst::FCMP_OLT, spv::OpFOrdLessThan}, |
| 3479 | {CmpInst::FCMP_OLE, spv::OpFOrdLessThanEqual}, |
| 3480 | {CmpInst::FCMP_ONE, spv::OpFOrdNotEqual}, |
| 3481 | {CmpInst::FCMP_UEQ, spv::OpFUnordEqual}, |
| 3482 | {CmpInst::FCMP_UGT, spv::OpFUnordGreaterThan}, |
| 3483 | {CmpInst::FCMP_UGE, spv::OpFUnordGreaterThanEqual}, |
| 3484 | {CmpInst::FCMP_ULT, spv::OpFUnordLessThan}, |
| 3485 | {CmpInst::FCMP_ULE, spv::OpFUnordLessThanEqual}, |
| 3486 | {CmpInst::FCMP_UNE, spv::OpFUnordNotEqual}}; |
| 3487 | |
| 3488 | assert(0 != Map.count(I->getPredicate())); |
| 3489 | |
| 3490 | return Map.at(I->getPredicate()); |
| 3491 | } |
| 3492 | |
| 3493 | spv::Op SPIRVProducerPass::GetSPIRVCastOpcode(Instruction &I) { |
| 3494 | const std::map<unsigned, spv::Op> Map{ |
| 3495 | {Instruction::Trunc, spv::OpUConvert}, |
| 3496 | {Instruction::ZExt, spv::OpUConvert}, |
| 3497 | {Instruction::SExt, spv::OpSConvert}, |
| 3498 | {Instruction::FPToUI, spv::OpConvertFToU}, |
| 3499 | {Instruction::FPToSI, spv::OpConvertFToS}, |
| 3500 | {Instruction::UIToFP, spv::OpConvertUToF}, |
| 3501 | {Instruction::SIToFP, spv::OpConvertSToF}, |
| 3502 | {Instruction::FPTrunc, spv::OpFConvert}, |
| 3503 | {Instruction::FPExt, spv::OpFConvert}, |
| 3504 | {Instruction::BitCast, spv::OpBitcast}}; |
| 3505 | |
| 3506 | assert(0 != Map.count(I.getOpcode())); |
| 3507 | |
| 3508 | return Map.at(I.getOpcode()); |
| 3509 | } |
| 3510 | |
| 3511 | spv::Op SPIRVProducerPass::GetSPIRVBinaryOpcode(Instruction &I) { |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 3512 | if (I.getType()->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3513 | switch (I.getOpcode()) { |
| 3514 | default: |
| 3515 | break; |
| 3516 | case Instruction::Or: |
| 3517 | return spv::OpLogicalOr; |
| 3518 | case Instruction::And: |
| 3519 | return spv::OpLogicalAnd; |
| 3520 | case Instruction::Xor: |
| 3521 | return spv::OpLogicalNotEqual; |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | const std::map<unsigned, spv::Op> Map { |
| 3526 | {Instruction::Add, spv::OpIAdd}, |
| 3527 | {Instruction::FAdd, spv::OpFAdd}, |
| 3528 | {Instruction::Sub, spv::OpISub}, |
| 3529 | {Instruction::FSub, spv::OpFSub}, |
| 3530 | {Instruction::Mul, spv::OpIMul}, |
| 3531 | {Instruction::FMul, spv::OpFMul}, |
| 3532 | {Instruction::UDiv, spv::OpUDiv}, |
| 3533 | {Instruction::SDiv, spv::OpSDiv}, |
| 3534 | {Instruction::FDiv, spv::OpFDiv}, |
| 3535 | {Instruction::URem, spv::OpUMod}, |
| 3536 | {Instruction::SRem, spv::OpSRem}, |
| 3537 | {Instruction::FRem, spv::OpFRem}, |
| 3538 | {Instruction::Or, spv::OpBitwiseOr}, |
| 3539 | {Instruction::Xor, spv::OpBitwiseXor}, |
| 3540 | {Instruction::And, spv::OpBitwiseAnd}, |
| 3541 | {Instruction::Shl, spv::OpShiftLeftLogical}, |
| 3542 | {Instruction::LShr, spv::OpShiftRightLogical}, |
| 3543 | {Instruction::AShr, spv::OpShiftRightArithmetic}}; |
| 3544 | |
| 3545 | assert(0 != Map.count(I.getOpcode())); |
| 3546 | |
| 3547 | return Map.at(I.getOpcode()); |
| 3548 | } |
| 3549 | |
| 3550 | void SPIRVProducerPass::GenerateInstruction(Instruction &I) { |
| 3551 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 3552 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3553 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 3554 | LLVMContext &Context = I.getParent()->getParent()->getParent()->getContext(); |
| 3555 | |
| 3556 | // Register Instruction to ValueMap. |
| 3557 | if (0 == VMap[&I]) { |
| 3558 | VMap[&I] = nextID; |
| 3559 | } |
| 3560 | |
| 3561 | switch (I.getOpcode()) { |
| 3562 | default: { |
| 3563 | if (Instruction::isCast(I.getOpcode())) { |
| 3564 | // |
| 3565 | // Generate SPIRV instructions for cast operators. |
| 3566 | // |
| 3567 | |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3568 | |
| 3569 | auto Ty = I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3570 | auto OpTy = I.getOperand(0)->getType(); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3571 | auto toI8 = Ty == Type::getInt8Ty(Context); |
| 3572 | auto fromI32 = OpTy == Type::getInt32Ty(Context); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3573 | // Handle zext, sext and uitofp with i1 type specially. |
| 3574 | if ((I.getOpcode() == Instruction::ZExt || |
| 3575 | I.getOpcode() == Instruction::SExt || |
| 3576 | I.getOpcode() == Instruction::UIToFP) && |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 3577 | OpTy->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3578 | // |
| 3579 | // Generate OpSelect. |
| 3580 | // |
| 3581 | |
| 3582 | // Ops[0] = Result Type ID |
| 3583 | // Ops[1] = Condition ID |
| 3584 | // Ops[2] = True Constant ID |
| 3585 | // Ops[3] = False Constant ID |
| 3586 | SPIRVOperandList Ops; |
| 3587 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3588 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3589 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3590 | uint32_t CondID = VMap[I.getOperand(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3591 | Ops << MkId(CondID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3592 | |
| 3593 | uint32_t TrueID = 0; |
| 3594 | if (I.getOpcode() == Instruction::ZExt) { |
| 3595 | APInt One(32, 1); |
| 3596 | TrueID = VMap[Constant::getIntegerValue(I.getType(), One)]; |
| 3597 | } else if (I.getOpcode() == Instruction::SExt) { |
| 3598 | APInt MinusOne(32, UINT64_MAX, true); |
| 3599 | TrueID = VMap[Constant::getIntegerValue(I.getType(), MinusOne)]; |
| 3600 | } else { |
| 3601 | TrueID = VMap[ConstantFP::get(Context, APFloat(1.0f))]; |
| 3602 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3603 | Ops << MkId(TrueID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3604 | |
| 3605 | uint32_t FalseID = 0; |
| 3606 | if (I.getOpcode() == Instruction::ZExt) { |
| 3607 | FalseID = VMap[Constant::getNullValue(I.getType())]; |
| 3608 | } else if (I.getOpcode() == Instruction::SExt) { |
| 3609 | FalseID = VMap[Constant::getNullValue(I.getType())]; |
| 3610 | } else { |
| 3611 | FalseID = VMap[ConstantFP::get(Context, APFloat(0.0f))]; |
| 3612 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3613 | Ops << MkId(FalseID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3614 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3615 | auto *Inst = new SPIRVInstruction(spv::OpSelect, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3616 | SPIRVInstList.push_back(Inst); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3617 | } else if (I.getOpcode() == Instruction::Trunc && fromI32 && toI8) { |
| 3618 | // The SPIR-V target type is a 32-bit int. Keep only the bottom |
| 3619 | // 8 bits. |
| 3620 | // Before: |
| 3621 | // %result = trunc i32 %a to i8 |
| 3622 | // After |
| 3623 | // %result = OpBitwiseAnd %uint %a %uint_255 |
| 3624 | |
| 3625 | SPIRVOperandList Ops; |
| 3626 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3627 | Ops << MkId(lookupType(OpTy)) << MkId(VMap[I.getOperand(0)]); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3628 | |
| 3629 | Type *UintTy = Type::getInt32Ty(Context); |
| 3630 | uint32_t MaskID = VMap[ConstantInt::get(UintTy, 255)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3631 | Ops << MkId(MaskID); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3632 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3633 | auto *Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3634 | SPIRVInstList.push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3635 | } else { |
| 3636 | // Ops[0] = Result Type ID |
| 3637 | // Ops[1] = Source Value ID |
| 3638 | SPIRVOperandList Ops; |
| 3639 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3640 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3641 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3642 | auto *Inst = new SPIRVInstruction(GetSPIRVCastOpcode(I), nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3643 | SPIRVInstList.push_back(Inst); |
| 3644 | } |
| 3645 | } else if (isa<BinaryOperator>(I)) { |
| 3646 | // |
| 3647 | // Generate SPIRV instructions for binary operators. |
| 3648 | // |
| 3649 | |
| 3650 | // Handle xor with i1 type specially. |
| 3651 | if (I.getOpcode() == Instruction::Xor && |
| 3652 | I.getType() == Type::getInt1Ty(Context) && |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 3653 | ((isa<ConstantInt>(I.getOperand(0)) && |
| 3654 | !cast<ConstantInt>(I.getOperand(0))->isZero()) || |
| 3655 | (isa<ConstantInt>(I.getOperand(1)) && |
| 3656 | !cast<ConstantInt>(I.getOperand(1))->isZero()))) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3657 | // |
| 3658 | // Generate OpLogicalNot. |
| 3659 | // |
| 3660 | // Ops[0] = Result Type ID |
| 3661 | // Ops[1] = Operand |
| 3662 | SPIRVOperandList Ops; |
| 3663 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3664 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3665 | |
| 3666 | Value *CondV = I.getOperand(0); |
| 3667 | if (isa<Constant>(I.getOperand(0))) { |
| 3668 | CondV = I.getOperand(1); |
| 3669 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3670 | Ops << MkId(VMap[CondV]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3671 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3672 | auto *Inst = new SPIRVInstruction(spv::OpLogicalNot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3673 | SPIRVInstList.push_back(Inst); |
| 3674 | } else { |
| 3675 | // Ops[0] = Result Type ID |
| 3676 | // Ops[1] = Operand 0 |
| 3677 | // Ops[2] = Operand 1 |
| 3678 | SPIRVOperandList Ops; |
| 3679 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3680 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]) |
| 3681 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3682 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3683 | auto *Inst = |
| 3684 | new SPIRVInstruction(GetSPIRVBinaryOpcode(I), nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3685 | SPIRVInstList.push_back(Inst); |
| 3686 | } |
| 3687 | } else { |
| 3688 | I.print(errs()); |
| 3689 | llvm_unreachable("Unsupported instruction???"); |
| 3690 | } |
| 3691 | break; |
| 3692 | } |
| 3693 | case Instruction::GetElementPtr: { |
| 3694 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 3695 | |
| 3696 | // |
| 3697 | // Generate OpAccessChain. |
| 3698 | // |
| 3699 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(&I); |
| 3700 | |
| 3701 | // |
| 3702 | // Generate OpAccessChain. |
| 3703 | // |
| 3704 | |
| 3705 | // Ops[0] = Result Type ID |
| 3706 | // Ops[1] = Base ID |
| 3707 | // Ops[2] ... Ops[n] = Indexes ID |
| 3708 | SPIRVOperandList Ops; |
| 3709 | |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3710 | PointerType* ResultType = cast<PointerType>(GEP->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3711 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate || |
| 3712 | GlobalConstArgSet.count(GEP->getPointerOperand())) { |
| 3713 | // Use pointer type with private address space for global constant. |
| 3714 | Type *EleTy = I.getType()->getPointerElementType(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3715 | ResultType = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3716 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3717 | |
| 3718 | Ops << MkId(lookupType(ResultType)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3719 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3720 | // Generate the base pointer. |
| 3721 | Ops << MkId(VMap[GEP->getPointerOperand()]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3722 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3723 | // TODO(dneto): Simplify the following? |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3724 | |
| 3725 | // |
| 3726 | // Follows below rules for gep. |
| 3727 | // |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3728 | // 1. If gep's first index is 0 generate OpAccessChain and ignore gep's |
| 3729 | // first index. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3730 | // 2. If gep's first index is not 0, generate OpPtrAccessChain and use gep's |
| 3731 | // first index. |
| 3732 | // 3. If gep's first index is not constant, generate OpPtrAccessChain and |
| 3733 | // use gep's first index. |
| 3734 | // 4. If it is not above case 1, 2 and 3, generate OpAccessChain and use |
| 3735 | // gep's first index. |
| 3736 | // |
| 3737 | spv::Op Opcode = spv::OpAccessChain; |
| 3738 | unsigned offset = 0; |
| 3739 | if (ConstantInt *CstInt = dyn_cast<ConstantInt>(GEP->getOperand(1))) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3740 | if (CstInt->getZExtValue() == 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3741 | offset = 1; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3742 | } else if (CstInt->getZExtValue() != 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3743 | Opcode = spv::OpPtrAccessChain; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3744 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3745 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3746 | Opcode = spv::OpPtrAccessChain; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | if (Opcode == spv::OpPtrAccessChain) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3750 | setVariablePointers(true); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3751 | // Do we need to generate ArrayStride? Check against the GEP result type |
| 3752 | // rather than the pointer type of the base because when indexing into |
| 3753 | // an OpenCL program-scope constant, we'll swap out the LLVM base pointer |
| 3754 | // for something else in the SPIR-V. |
| 3755 | // E.g. see test/PointerAccessChain/pointer_index_is_constant_1.cl |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 3756 | switch (GetStorageClass(ResultType->getAddressSpace())) { |
| 3757 | case spv::StorageClassStorageBuffer: |
| 3758 | case spv::StorageClassUniform: |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3759 | // Save the need to generate an ArrayStride decoration. But defer |
| 3760 | // generation until later, so we only make one decoration. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3761 | getTypesNeedingArrayStride().insert(ResultType); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 3762 | break; |
| 3763 | default: |
| 3764 | break; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3765 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | for (auto II = GEP->idx_begin() + offset; II != GEP->idx_end(); II++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3769 | Ops << MkId(VMap[*II]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3770 | } |
| 3771 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3772 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3773 | SPIRVInstList.push_back(Inst); |
| 3774 | break; |
| 3775 | } |
| 3776 | case Instruction::ExtractValue: { |
| 3777 | ExtractValueInst *EVI = cast<ExtractValueInst>(&I); |
| 3778 | // Ops[0] = Result Type ID |
| 3779 | // Ops[1] = Composite ID |
| 3780 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
| 3781 | SPIRVOperandList Ops; |
| 3782 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3783 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3784 | |
| 3785 | uint32_t CompositeID = VMap[EVI->getAggregateOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3786 | Ops << MkId(CompositeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3787 | |
| 3788 | for (auto &Index : EVI->indices()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3789 | Ops << MkNum(Index); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3790 | } |
| 3791 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3792 | auto *Inst = new SPIRVInstruction(spv::OpCompositeExtract, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3793 | SPIRVInstList.push_back(Inst); |
| 3794 | break; |
| 3795 | } |
| 3796 | case Instruction::InsertValue: { |
| 3797 | InsertValueInst *IVI = cast<InsertValueInst>(&I); |
| 3798 | // Ops[0] = Result Type ID |
| 3799 | // Ops[1] = Object ID |
| 3800 | // Ops[2] = Composite ID |
| 3801 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
| 3802 | SPIRVOperandList Ops; |
| 3803 | |
| 3804 | uint32_t ResTyID = lookupType(I.getType()); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3805 | Ops << MkId(ResTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3806 | |
| 3807 | uint32_t ObjectID = VMap[IVI->getInsertedValueOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3808 | Ops << MkId(ObjectID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3809 | |
| 3810 | uint32_t CompositeID = VMap[IVI->getAggregateOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3811 | Ops << MkId(CompositeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3812 | |
| 3813 | for (auto &Index : IVI->indices()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3814 | Ops << MkNum(Index); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3815 | } |
| 3816 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3817 | auto *Inst = new SPIRVInstruction(spv::OpCompositeInsert, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3818 | SPIRVInstList.push_back(Inst); |
| 3819 | break; |
| 3820 | } |
| 3821 | case Instruction::Select: { |
| 3822 | // |
| 3823 | // Generate OpSelect. |
| 3824 | // |
| 3825 | |
| 3826 | // Ops[0] = Result Type ID |
| 3827 | // Ops[1] = Condition ID |
| 3828 | // Ops[2] = True Constant ID |
| 3829 | // Ops[3] = False Constant ID |
| 3830 | SPIRVOperandList Ops; |
| 3831 | |
| 3832 | // Find SPIRV instruction for parameter type. |
| 3833 | auto Ty = I.getType(); |
| 3834 | if (Ty->isPointerTy()) { |
| 3835 | auto PointeeTy = Ty->getPointerElementType(); |
| 3836 | if (PointeeTy->isStructTy() && |
| 3837 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 3838 | Ty = PointeeTy; |
| 3839 | } |
| 3840 | } |
| 3841 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3842 | Ops << MkId(lookupType(Ty)) << MkId(VMap[I.getOperand(0)]) |
| 3843 | << MkId(VMap[I.getOperand(1)]) << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3844 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3845 | auto *Inst = new SPIRVInstruction(spv::OpSelect, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3846 | SPIRVInstList.push_back(Inst); |
| 3847 | break; |
| 3848 | } |
| 3849 | case Instruction::ExtractElement: { |
| 3850 | // Handle <4 x i8> type manually. |
| 3851 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 3852 | if (is4xi8vec(CompositeTy)) { |
| 3853 | // |
| 3854 | // Generate OpShiftRightLogical and OpBitwiseAnd for extractelement with |
| 3855 | // <4 x i8>. |
| 3856 | // |
| 3857 | |
| 3858 | // |
| 3859 | // Generate OpShiftRightLogical |
| 3860 | // |
| 3861 | // Ops[0] = Result Type ID |
| 3862 | // Ops[1] = Operand 0 |
| 3863 | // Ops[2] = Operand 1 |
| 3864 | // |
| 3865 | SPIRVOperandList Ops; |
| 3866 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3867 | Ops << MkId(lookupType(CompositeTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3868 | |
| 3869 | uint32_t Op0ID = VMap[I.getOperand(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3870 | Ops << MkId(Op0ID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3871 | |
| 3872 | uint32_t Op1ID = 0; |
| 3873 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 3874 | // Handle constant index. |
| 3875 | uint64_t Idx = CI->getZExtValue(); |
| 3876 | Value *ShiftAmount = |
| 3877 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 3878 | Op1ID = VMap[ShiftAmount]; |
| 3879 | } else { |
| 3880 | // Handle variable index. |
| 3881 | SPIRVOperandList TmpOps; |
| 3882 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3883 | TmpOps << MkId(lookupType(Type::getInt32Ty(Context))) |
| 3884 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3885 | |
| 3886 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3887 | TmpOps << MkId(VMap[Cst8]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3888 | |
| 3889 | Op1ID = nextID; |
| 3890 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3891 | auto *TmpInst = new SPIRVInstruction(spv::OpIMul, nextID++, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3892 | SPIRVInstList.push_back(TmpInst); |
| 3893 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3894 | Ops << MkId(Op1ID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3895 | |
| 3896 | uint32_t ShiftID = nextID; |
| 3897 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3898 | auto *Inst = |
| 3899 | new SPIRVInstruction(spv::OpShiftRightLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3900 | SPIRVInstList.push_back(Inst); |
| 3901 | |
| 3902 | // |
| 3903 | // Generate OpBitwiseAnd |
| 3904 | // |
| 3905 | // Ops[0] = Result Type ID |
| 3906 | // Ops[1] = Operand 0 |
| 3907 | // Ops[2] = Operand 1 |
| 3908 | // |
| 3909 | Ops.clear(); |
| 3910 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3911 | Ops << MkId(lookupType(CompositeTy)) << MkId(ShiftID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3912 | |
| 3913 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3914 | Ops << MkId(VMap[CstFF]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3915 | |
David Neto | 9b2d625 | 2017-09-06 15:47:37 -0400 | [diff] [blame] | 3916 | // Reset mapping for this value to the result of the bitwise and. |
| 3917 | VMap[&I] = nextID; |
| 3918 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3919 | Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3920 | SPIRVInstList.push_back(Inst); |
| 3921 | break; |
| 3922 | } |
| 3923 | |
| 3924 | // Ops[0] = Result Type ID |
| 3925 | // Ops[1] = Composite ID |
| 3926 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
| 3927 | SPIRVOperandList Ops; |
| 3928 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3929 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3930 | |
| 3931 | spv::Op Opcode = spv::OpCompositeExtract; |
| 3932 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3933 | Ops << MkNum(static_cast<uint32_t>(CI->getZExtValue())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3934 | } else { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3935 | Ops << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3936 | Opcode = spv::OpVectorExtractDynamic; |
| 3937 | } |
| 3938 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3939 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3940 | SPIRVInstList.push_back(Inst); |
| 3941 | break; |
| 3942 | } |
| 3943 | case Instruction::InsertElement: { |
| 3944 | // Handle <4 x i8> type manually. |
| 3945 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 3946 | if (is4xi8vec(CompositeTy)) { |
| 3947 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 3948 | uint32_t CstFFID = VMap[CstFF]; |
| 3949 | |
| 3950 | uint32_t ShiftAmountID = 0; |
| 3951 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 3952 | // Handle constant index. |
| 3953 | uint64_t Idx = CI->getZExtValue(); |
| 3954 | Value *ShiftAmount = |
| 3955 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 3956 | ShiftAmountID = VMap[ShiftAmount]; |
| 3957 | } else { |
| 3958 | // Handle variable index. |
| 3959 | SPIRVOperandList TmpOps; |
| 3960 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3961 | TmpOps << MkId(lookupType(Type::getInt32Ty(Context))) |
| 3962 | << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3963 | |
| 3964 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3965 | TmpOps << MkId(VMap[Cst8]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3966 | |
| 3967 | ShiftAmountID = nextID; |
| 3968 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3969 | auto *TmpInst = new SPIRVInstruction(spv::OpIMul, nextID++, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3970 | SPIRVInstList.push_back(TmpInst); |
| 3971 | } |
| 3972 | |
| 3973 | // |
| 3974 | // Generate mask operations. |
| 3975 | // |
| 3976 | |
| 3977 | // ShiftLeft mask according to index of insertelement. |
| 3978 | SPIRVOperandList Ops; |
| 3979 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3980 | const uint32_t ResTyID = lookupType(CompositeTy); |
| 3981 | Ops << MkId(ResTyID) << MkId(CstFFID) << MkId(ShiftAmountID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3982 | |
| 3983 | uint32_t MaskID = nextID; |
| 3984 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3985 | auto *Inst = new SPIRVInstruction(spv::OpShiftLeftLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3986 | SPIRVInstList.push_back(Inst); |
| 3987 | |
| 3988 | // Inverse mask. |
| 3989 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3990 | Ops << MkId(ResTyID) << MkId(MaskID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3991 | |
| 3992 | uint32_t InvMaskID = nextID; |
| 3993 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3994 | Inst = new SPIRVInstruction(spv::OpNot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3995 | SPIRVInstList.push_back(Inst); |
| 3996 | |
| 3997 | // Apply mask. |
| 3998 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3999 | Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(0)]) << MkId(InvMaskID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4000 | |
| 4001 | uint32_t OrgValID = nextID; |
| 4002 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4003 | Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4004 | SPIRVInstList.push_back(Inst); |
| 4005 | |
| 4006 | // Create correct value according to index of insertelement. |
| 4007 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4008 | Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(1)]) << MkId(ShiftAmountID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4009 | |
| 4010 | uint32_t InsertValID = nextID; |
| 4011 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4012 | Inst = new SPIRVInstruction(spv::OpShiftLeftLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4013 | SPIRVInstList.push_back(Inst); |
| 4014 | |
| 4015 | // Insert value to original value. |
| 4016 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4017 | Ops << MkId(ResTyID) << MkId(OrgValID) << MkId(InsertValID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4018 | |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 4019 | VMap[&I] = nextID; |
| 4020 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4021 | Inst = new SPIRVInstruction(spv::OpBitwiseOr, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4022 | SPIRVInstList.push_back(Inst); |
| 4023 | |
| 4024 | break; |
| 4025 | } |
| 4026 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4027 | SPIRVOperandList Ops; |
| 4028 | |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4029 | // Ops[0] = Result Type ID |
| 4030 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4031 | |
| 4032 | spv::Op Opcode = spv::OpCompositeInsert; |
| 4033 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4034 | const auto value = CI->getZExtValue(); |
| 4035 | assert(value <= UINT32_MAX); |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4036 | // Ops[1] = Object ID |
| 4037 | // Ops[2] = Composite ID |
| 4038 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
| 4039 | Ops << MkId(VMap[I.getOperand(1)]) |
| 4040 | << MkId(VMap[I.getOperand(0)]) |
| 4041 | << MkNum(static_cast<uint32_t>(value)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4042 | } else { |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4043 | // Ops[1] = Composite ID |
| 4044 | // Ops[2] = Object ID |
| 4045 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
| 4046 | Ops << MkId(VMap[I.getOperand(0)]) |
| 4047 | << MkId(VMap[I.getOperand(1)]) |
| 4048 | << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4049 | Opcode = spv::OpVectorInsertDynamic; |
| 4050 | } |
| 4051 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4052 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4053 | SPIRVInstList.push_back(Inst); |
| 4054 | break; |
| 4055 | } |
| 4056 | case Instruction::ShuffleVector: { |
| 4057 | // Ops[0] = Result Type ID |
| 4058 | // Ops[1] = Vector 1 ID |
| 4059 | // Ops[2] = Vector 2 ID |
| 4060 | // Ops[3] ... Ops[n] = Components (Literal Number) |
| 4061 | SPIRVOperandList Ops; |
| 4062 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4063 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]) |
| 4064 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4065 | |
| 4066 | uint64_t NumElements = 0; |
| 4067 | if (Constant *Cst = dyn_cast<Constant>(I.getOperand(2))) { |
| 4068 | NumElements = cast<VectorType>(Cst->getType())->getNumElements(); |
| 4069 | |
| 4070 | if (Cst->isNullValue()) { |
| 4071 | for (unsigned i = 0; i < NumElements; i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4072 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4073 | } |
| 4074 | } else if (const ConstantDataSequential *CDS = |
| 4075 | dyn_cast<ConstantDataSequential>(Cst)) { |
| 4076 | for (unsigned i = 0; i < CDS->getNumElements(); i++) { |
| 4077 | std::vector<uint32_t> LiteralNum; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4078 | const auto value = CDS->getElementAsInteger(i); |
| 4079 | assert(value <= UINT32_MAX); |
| 4080 | Ops << MkNum(static_cast<uint32_t>(value)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4081 | } |
| 4082 | } else if (const ConstantVector *CV = dyn_cast<ConstantVector>(Cst)) { |
| 4083 | for (unsigned i = 0; i < CV->getNumOperands(); i++) { |
| 4084 | auto Op = CV->getOperand(i); |
| 4085 | |
| 4086 | uint32_t literal = 0; |
| 4087 | |
| 4088 | if (auto CI = dyn_cast<ConstantInt>(Op)) { |
| 4089 | literal = static_cast<uint32_t>(CI->getZExtValue()); |
| 4090 | } else if (auto UI = dyn_cast<UndefValue>(Op)) { |
| 4091 | literal = 0xFFFFFFFFu; |
| 4092 | } else { |
| 4093 | Op->print(errs()); |
| 4094 | llvm_unreachable("Unsupported element in ConstantVector!"); |
| 4095 | } |
| 4096 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4097 | Ops << MkNum(literal); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4098 | } |
| 4099 | } else { |
| 4100 | Cst->print(errs()); |
| 4101 | llvm_unreachable("Unsupported constant mask in ShuffleVector!"); |
| 4102 | } |
| 4103 | } |
| 4104 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4105 | auto *Inst = new SPIRVInstruction(spv::OpVectorShuffle, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4106 | SPIRVInstList.push_back(Inst); |
| 4107 | break; |
| 4108 | } |
| 4109 | case Instruction::ICmp: |
| 4110 | case Instruction::FCmp: { |
| 4111 | CmpInst *CmpI = cast<CmpInst>(&I); |
| 4112 | |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4113 | // Pointer equality is invalid. |
| 4114 | Type* ArgTy = CmpI->getOperand(0)->getType(); |
| 4115 | if (isa<PointerType>(ArgTy)) { |
| 4116 | CmpI->print(errs()); |
| 4117 | std::string name = I.getParent()->getParent()->getName(); |
| 4118 | errs() |
| 4119 | << "\nPointer equality test is not supported by SPIR-V for Vulkan, " |
| 4120 | << "in function " << name << "\n"; |
| 4121 | llvm_unreachable("Pointer equality check is invalid"); |
| 4122 | break; |
| 4123 | } |
| 4124 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4125 | // Ops[0] = Result Type ID |
| 4126 | // Ops[1] = Operand 1 ID |
| 4127 | // Ops[2] = Operand 2 ID |
| 4128 | SPIRVOperandList Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4129 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4130 | Ops << MkId(lookupType(CmpI->getType())) << MkId(VMap[CmpI->getOperand(0)]) |
| 4131 | << MkId(VMap[CmpI->getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4132 | |
| 4133 | spv::Op Opcode = GetSPIRVCmpOpcode(CmpI); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4134 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4135 | SPIRVInstList.push_back(Inst); |
| 4136 | break; |
| 4137 | } |
| 4138 | case Instruction::Br: { |
| 4139 | // Branch instrucion is deferred because it needs label's ID. Record slot's |
| 4140 | // location on SPIRVInstructionList. |
| 4141 | DeferredInsts.push_back( |
| 4142 | std::make_tuple(&I, --SPIRVInstList.end(), 0 /* No id */)); |
| 4143 | break; |
| 4144 | } |
| 4145 | case Instruction::Switch: { |
| 4146 | I.print(errs()); |
| 4147 | llvm_unreachable("Unsupported instruction???"); |
| 4148 | break; |
| 4149 | } |
| 4150 | case Instruction::IndirectBr: { |
| 4151 | I.print(errs()); |
| 4152 | llvm_unreachable("Unsupported instruction???"); |
| 4153 | break; |
| 4154 | } |
| 4155 | case Instruction::PHI: { |
| 4156 | // Branch instrucion is deferred because it needs label's ID. Record slot's |
| 4157 | // location on SPIRVInstructionList. |
| 4158 | DeferredInsts.push_back( |
| 4159 | std::make_tuple(&I, --SPIRVInstList.end(), nextID++)); |
| 4160 | break; |
| 4161 | } |
| 4162 | case Instruction::Alloca: { |
| 4163 | // |
| 4164 | // Generate OpVariable. |
| 4165 | // |
| 4166 | // Ops[0] : Result Type ID |
| 4167 | // Ops[1] : Storage Class |
| 4168 | SPIRVOperandList Ops; |
| 4169 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4170 | Ops << MkId(lookupType(I.getType())) << MkNum(spv::StorageClassFunction); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4171 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4172 | auto *Inst = new SPIRVInstruction(spv::OpVariable, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4173 | SPIRVInstList.push_back(Inst); |
| 4174 | break; |
| 4175 | } |
| 4176 | case Instruction::Load: { |
| 4177 | LoadInst *LD = cast<LoadInst>(&I); |
| 4178 | // |
| 4179 | // Generate OpLoad. |
| 4180 | // |
| 4181 | |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4182 | uint32_t ResTyID = lookupType(LD->getType()); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4183 | uint32_t PointerID = VMap[LD->getPointerOperand()]; |
| 4184 | |
| 4185 | // This is a hack to work around what looks like a driver bug. |
| 4186 | // When we're loading from the special variable holding the WorkgroupSize |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4187 | // builtin value, use an OpBitWiseAnd of the value's ID rather than |
| 4188 | // generating a load. |
David Neto | 66cfe64 | 2018-03-24 06:13:56 -0700 | [diff] [blame] | 4189 | // TODO(dneto): Remove this awful hack once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4190 | if (PointerID == WorkgroupSizeVarID) { |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4191 | // Generate a bitwise-and of the original value with itself. |
| 4192 | // We should have been able to get away with just an OpCopyObject, |
| 4193 | // but we need something more complex to get past certain driver bugs. |
| 4194 | // This is ridiculous, but necessary. |
| 4195 | // TODO(dneto): Revisit this once drivers fix their bugs. |
| 4196 | |
| 4197 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4198 | Ops << MkId(ResTyID) << MkId(WorkgroupSizeValueID) |
| 4199 | << MkId(WorkgroupSizeValueID); |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4200 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4201 | auto *Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4202 | SPIRVInstList.push_back(Inst); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4203 | break; |
| 4204 | } |
| 4205 | |
| 4206 | // This is the normal path. Generate a load. |
| 4207 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4208 | // Ops[0] = Result Type ID |
| 4209 | // Ops[1] = Pointer ID |
| 4210 | // Ops[2] ... Ops[n] = Optional Memory Access |
| 4211 | // |
| 4212 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4213 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4214 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4215 | Ops << MkId(ResTyID) << MkId(PointerID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4216 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4217 | auto *Inst = new SPIRVInstruction(spv::OpLoad, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4218 | SPIRVInstList.push_back(Inst); |
| 4219 | break; |
| 4220 | } |
| 4221 | case Instruction::Store: { |
| 4222 | StoreInst *ST = cast<StoreInst>(&I); |
| 4223 | // |
| 4224 | // Generate OpStore. |
| 4225 | // |
| 4226 | |
| 4227 | // Ops[0] = Pointer ID |
| 4228 | // Ops[1] = Object ID |
| 4229 | // Ops[2] ... Ops[n] = Optional Memory Access (later???) |
| 4230 | // |
| 4231 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4232 | SPIRVOperandList Ops; |
| 4233 | Ops << MkId(VMap[ST->getPointerOperand()]) |
| 4234 | << MkId(VMap[ST->getValueOperand()]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4235 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4236 | auto *Inst = new SPIRVInstruction(spv::OpStore, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4237 | SPIRVInstList.push_back(Inst); |
| 4238 | break; |
| 4239 | } |
| 4240 | case Instruction::AtomicCmpXchg: { |
| 4241 | I.print(errs()); |
| 4242 | llvm_unreachable("Unsupported instruction???"); |
| 4243 | break; |
| 4244 | } |
| 4245 | case Instruction::AtomicRMW: { |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4246 | AtomicRMWInst *AtomicRMW = dyn_cast<AtomicRMWInst>(&I); |
| 4247 | |
| 4248 | spv::Op opcode; |
| 4249 | |
| 4250 | switch (AtomicRMW->getOperation()) { |
| 4251 | default: |
| 4252 | I.print(errs()); |
| 4253 | llvm_unreachable("Unsupported instruction???"); |
| 4254 | case llvm::AtomicRMWInst::Add: |
| 4255 | opcode = spv::OpAtomicIAdd; |
| 4256 | break; |
| 4257 | case llvm::AtomicRMWInst::Sub: |
| 4258 | opcode = spv::OpAtomicISub; |
| 4259 | break; |
| 4260 | case llvm::AtomicRMWInst::Xchg: |
| 4261 | opcode = spv::OpAtomicExchange; |
| 4262 | break; |
| 4263 | case llvm::AtomicRMWInst::Min: |
| 4264 | opcode = spv::OpAtomicSMin; |
| 4265 | break; |
| 4266 | case llvm::AtomicRMWInst::Max: |
| 4267 | opcode = spv::OpAtomicSMax; |
| 4268 | break; |
| 4269 | case llvm::AtomicRMWInst::UMin: |
| 4270 | opcode = spv::OpAtomicUMin; |
| 4271 | break; |
| 4272 | case llvm::AtomicRMWInst::UMax: |
| 4273 | opcode = spv::OpAtomicUMax; |
| 4274 | break; |
| 4275 | case llvm::AtomicRMWInst::And: |
| 4276 | opcode = spv::OpAtomicAnd; |
| 4277 | break; |
| 4278 | case llvm::AtomicRMWInst::Or: |
| 4279 | opcode = spv::OpAtomicOr; |
| 4280 | break; |
| 4281 | case llvm::AtomicRMWInst::Xor: |
| 4282 | opcode = spv::OpAtomicXor; |
| 4283 | break; |
| 4284 | } |
| 4285 | |
| 4286 | // |
| 4287 | // Generate OpAtomic*. |
| 4288 | // |
| 4289 | SPIRVOperandList Ops; |
| 4290 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4291 | Ops << MkId(lookupType(I.getType())) |
| 4292 | << MkId(VMap[AtomicRMW->getPointerOperand()]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4293 | |
| 4294 | auto IntTy = Type::getInt32Ty(I.getContext()); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4295 | const auto ConstantScopeDevice = ConstantInt::get(IntTy, spv::ScopeDevice); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4296 | Ops << MkId(VMap[ConstantScopeDevice]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4297 | |
| 4298 | const auto ConstantMemorySemantics = ConstantInt::get( |
| 4299 | IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 4300 | spv::MemorySemanticsSequentiallyConsistentMask); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4301 | Ops << MkId(VMap[ConstantMemorySemantics]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4302 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4303 | Ops << MkId(VMap[AtomicRMW->getValOperand()]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4304 | |
| 4305 | VMap[&I] = nextID; |
| 4306 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4307 | auto *Inst = new SPIRVInstruction(opcode, nextID++, Ops); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4308 | SPIRVInstList.push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4309 | break; |
| 4310 | } |
| 4311 | case Instruction::Fence: { |
| 4312 | I.print(errs()); |
| 4313 | llvm_unreachable("Unsupported instruction???"); |
| 4314 | break; |
| 4315 | } |
| 4316 | case Instruction::Call: { |
| 4317 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 4318 | Function *Callee = Call->getCalledFunction(); |
| 4319 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4320 | if (Callee->getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4321 | if (ResourceVarDeferredLoadCalls.count(Call) && Call->hasNUsesOrMore(1)) { |
| 4322 | // Generate an OpLoad |
| 4323 | SPIRVOperandList Ops; |
| 4324 | const auto load_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4325 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4326 | Ops << MkId(lookupType(Call->getType()->getPointerElementType())) |
| 4327 | << MkId(ResourceVarDeferredLoadCalls[Call]); |
| 4328 | |
| 4329 | auto *Inst = new SPIRVInstruction(spv::OpLoad, load_id, Ops); |
| 4330 | SPIRVInstList.push_back(Inst); |
| 4331 | VMap[Call] = load_id; |
| 4332 | break; |
| 4333 | |
| 4334 | } else { |
| 4335 | // This maps to an OpVariable we've already generated. |
| 4336 | // No code is generated for the call. |
| 4337 | } |
| 4338 | break; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4339 | } else if (Callee->getName().startswith(clspv::WorkgroupAccessorFunction())) { |
| 4340 | // Don't codegen an instruction here, but instead map this call directly |
| 4341 | // to the workgroup variable id. |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 4342 | int spec_id = static_cast<int>(cast<ConstantInt>(Call->getOperand(0))->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4343 | const auto &info = LocalSpecIdInfoMap[spec_id]; |
| 4344 | VMap[Call] = info.variable_id; |
| 4345 | break; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | // Sampler initializers become a load of the corresponding sampler. |
| 4349 | |
| 4350 | if (Callee->getName().equals("clspv.sampler.var.literal")) { |
| 4351 | // Map this to a load from the variable. |
| 4352 | const auto index_into_sampler_map = |
| 4353 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue(); |
| 4354 | |
| 4355 | // Generate an OpLoad |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4356 | SPIRVOperandList Ops; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4357 | const auto load_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4358 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4359 | Ops << MkId(lookupType(SamplerTy->getPointerElementType())) |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame^] | 4360 | << MkId(SamplerMapIndexToIDMap[static_cast<unsigned>(index_into_sampler_map)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4361 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4362 | auto *Inst = new SPIRVInstruction(spv::OpLoad, load_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4363 | SPIRVInstList.push_back(Inst); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4364 | VMap[Call] = load_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4365 | break; |
| 4366 | } |
| 4367 | |
| 4368 | if (Callee->getName().startswith("spirv.atomic")) { |
| 4369 | spv::Op opcode = StringSwitch<spv::Op>(Callee->getName()) |
| 4370 | .Case("spirv.atomic_add", spv::OpAtomicIAdd) |
| 4371 | .Case("spirv.atomic_sub", spv::OpAtomicISub) |
| 4372 | .Case("spirv.atomic_exchange", spv::OpAtomicExchange) |
| 4373 | .Case("spirv.atomic_inc", spv::OpAtomicIIncrement) |
| 4374 | .Case("spirv.atomic_dec", spv::OpAtomicIDecrement) |
| 4375 | .Case("spirv.atomic_compare_exchange", |
| 4376 | spv::OpAtomicCompareExchange) |
| 4377 | .Case("spirv.atomic_umin", spv::OpAtomicUMin) |
| 4378 | .Case("spirv.atomic_smin", spv::OpAtomicSMin) |
| 4379 | .Case("spirv.atomic_umax", spv::OpAtomicUMax) |
| 4380 | .Case("spirv.atomic_smax", spv::OpAtomicSMax) |
| 4381 | .Case("spirv.atomic_and", spv::OpAtomicAnd) |
| 4382 | .Case("spirv.atomic_or", spv::OpAtomicOr) |
| 4383 | .Case("spirv.atomic_xor", spv::OpAtomicXor) |
| 4384 | .Default(spv::OpNop); |
| 4385 | |
| 4386 | // |
| 4387 | // Generate OpAtomic*. |
| 4388 | // |
| 4389 | SPIRVOperandList Ops; |
| 4390 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4391 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4392 | |
| 4393 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4394 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | VMap[&I] = nextID; |
| 4398 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4399 | auto *Inst = new SPIRVInstruction(opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4400 | SPIRVInstList.push_back(Inst); |
| 4401 | break; |
| 4402 | } |
| 4403 | |
| 4404 | if (Callee->getName().startswith("_Z3dot")) { |
| 4405 | // If the argument is a vector type, generate OpDot |
| 4406 | if (Call->getArgOperand(0)->getType()->isVectorTy()) { |
| 4407 | // |
| 4408 | // Generate OpDot. |
| 4409 | // |
| 4410 | SPIRVOperandList Ops; |
| 4411 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4412 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4413 | |
| 4414 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4415 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | VMap[&I] = nextID; |
| 4419 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4420 | auto *Inst = new SPIRVInstruction(spv::OpDot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4421 | SPIRVInstList.push_back(Inst); |
| 4422 | } else { |
| 4423 | // |
| 4424 | // Generate OpFMul. |
| 4425 | // |
| 4426 | SPIRVOperandList Ops; |
| 4427 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4428 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4429 | |
| 4430 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4431 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | VMap[&I] = nextID; |
| 4435 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4436 | auto *Inst = new SPIRVInstruction(spv::OpFMul, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4437 | SPIRVInstList.push_back(Inst); |
| 4438 | } |
| 4439 | break; |
| 4440 | } |
| 4441 | |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4442 | if (Callee->getName().startswith("_Z4fmod")) { |
| 4443 | // OpenCL fmod(x,y) is x - y * trunc(x/y) |
| 4444 | // The sign for a non-zero result is taken from x. |
| 4445 | // (Try an example.) |
| 4446 | // So translate to OpFRem |
| 4447 | |
| 4448 | SPIRVOperandList Ops; |
| 4449 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4450 | Ops << MkId(lookupType(I.getType())); |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4451 | |
| 4452 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4453 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | VMap[&I] = nextID; |
| 4457 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4458 | auto *Inst = new SPIRVInstruction(spv::OpFRem, nextID++, Ops); |
David Neto | 8505ebf | 2017-10-13 18:50:50 -0400 | [diff] [blame] | 4459 | SPIRVInstList.push_back(Inst); |
| 4460 | break; |
| 4461 | } |
| 4462 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4463 | // spirv.store_null.* intrinsics become OpStore's. |
| 4464 | if (Callee->getName().startswith("spirv.store_null")) { |
| 4465 | // |
| 4466 | // Generate OpStore. |
| 4467 | // |
| 4468 | |
| 4469 | // Ops[0] = Pointer ID |
| 4470 | // Ops[1] = Object ID |
| 4471 | // Ops[2] ... Ops[n] |
| 4472 | SPIRVOperandList Ops; |
| 4473 | |
| 4474 | uint32_t PointerID = VMap[Call->getArgOperand(0)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4475 | uint32_t ObjectID = VMap[Call->getArgOperand(1)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4476 | Ops << MkId(PointerID) << MkId(ObjectID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4477 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4478 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpStore, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4479 | |
| 4480 | break; |
| 4481 | } |
| 4482 | |
| 4483 | // spirv.copy_memory.* intrinsics become OpMemoryMemory's. |
| 4484 | if (Callee->getName().startswith("spirv.copy_memory")) { |
| 4485 | // |
| 4486 | // Generate OpCopyMemory. |
| 4487 | // |
| 4488 | |
| 4489 | // Ops[0] = Dst ID |
| 4490 | // Ops[1] = Src ID |
| 4491 | // Ops[2] = Memory Access |
| 4492 | // Ops[3] = Alignment |
| 4493 | |
| 4494 | auto IsVolatile = |
| 4495 | dyn_cast<ConstantInt>(Call->getArgOperand(3))->getZExtValue() != 0; |
| 4496 | |
| 4497 | auto VolatileMemoryAccess = (IsVolatile) ? spv::MemoryAccessVolatileMask |
| 4498 | : spv::MemoryAccessMaskNone; |
| 4499 | |
| 4500 | auto MemoryAccess = VolatileMemoryAccess | spv::MemoryAccessAlignedMask; |
| 4501 | |
| 4502 | auto Alignment = |
| 4503 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue(); |
| 4504 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4505 | SPIRVOperandList Ops; |
| 4506 | Ops << MkId(VMap[Call->getArgOperand(0)]) |
| 4507 | << MkId(VMap[Call->getArgOperand(1)]) << MkNum(MemoryAccess) |
| 4508 | << MkNum(static_cast<uint32_t>(Alignment)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4509 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4510 | auto *Inst = new SPIRVInstruction(spv::OpCopyMemory, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4511 | |
| 4512 | SPIRVInstList.push_back(Inst); |
| 4513 | |
| 4514 | break; |
| 4515 | } |
| 4516 | |
| 4517 | // Nothing to do for abs with uint. Map abs's operand ID to VMap for abs |
| 4518 | // with unit. |
| 4519 | if (Callee->getName().equals("_Z3absj") || |
| 4520 | Callee->getName().equals("_Z3absDv2_j") || |
| 4521 | Callee->getName().equals("_Z3absDv3_j") || |
| 4522 | Callee->getName().equals("_Z3absDv4_j")) { |
| 4523 | VMap[&I] = VMap[Call->getOperand(0)]; |
| 4524 | break; |
| 4525 | } |
| 4526 | |
| 4527 | // barrier is converted to OpControlBarrier |
| 4528 | if (Callee->getName().equals("__spirv_control_barrier")) { |
| 4529 | // |
| 4530 | // Generate OpControlBarrier. |
| 4531 | // |
| 4532 | // Ops[0] = Execution Scope ID |
| 4533 | // Ops[1] = Memory Scope ID |
| 4534 | // Ops[2] = Memory Semantics ID |
| 4535 | // |
| 4536 | Value *ExecutionScope = Call->getArgOperand(0); |
| 4537 | Value *MemoryScope = Call->getArgOperand(1); |
| 4538 | Value *MemorySemantics = Call->getArgOperand(2); |
| 4539 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4540 | SPIRVOperandList Ops; |
| 4541 | Ops << MkId(VMap[ExecutionScope]) << MkId(VMap[MemoryScope]) |
| 4542 | << MkId(VMap[MemorySemantics]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4543 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4544 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpControlBarrier, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4545 | break; |
| 4546 | } |
| 4547 | |
| 4548 | // memory barrier is converted to OpMemoryBarrier |
| 4549 | if (Callee->getName().equals("__spirv_memory_barrier")) { |
| 4550 | // |
| 4551 | // Generate OpMemoryBarrier. |
| 4552 | // |
| 4553 | // Ops[0] = Memory Scope ID |
| 4554 | // Ops[1] = Memory Semantics ID |
| 4555 | // |
| 4556 | SPIRVOperandList Ops; |
| 4557 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4558 | uint32_t MemoryScopeID = VMap[Call->getArgOperand(0)]; |
| 4559 | uint32_t MemorySemanticsID = VMap[Call->getArgOperand(1)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4560 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4561 | Ops << MkId(MemoryScopeID) << MkId(MemorySemanticsID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4562 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4563 | auto *Inst = new SPIRVInstruction(spv::OpMemoryBarrier, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4564 | SPIRVInstList.push_back(Inst); |
| 4565 | break; |
| 4566 | } |
| 4567 | |
| 4568 | // isinf is converted to OpIsInf |
| 4569 | if (Callee->getName().equals("__spirv_isinff") || |
| 4570 | Callee->getName().equals("__spirv_isinfDv2_f") || |
| 4571 | Callee->getName().equals("__spirv_isinfDv3_f") || |
| 4572 | Callee->getName().equals("__spirv_isinfDv4_f")) { |
| 4573 | // |
| 4574 | // Generate OpIsInf. |
| 4575 | // |
| 4576 | // Ops[0] = Result Type ID |
| 4577 | // Ops[1] = X ID |
| 4578 | // |
| 4579 | SPIRVOperandList Ops; |
| 4580 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4581 | Ops << MkId(lookupType(I.getType())) |
| 4582 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4583 | |
| 4584 | VMap[&I] = nextID; |
| 4585 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4586 | auto *Inst = new SPIRVInstruction(spv::OpIsInf, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4587 | SPIRVInstList.push_back(Inst); |
| 4588 | break; |
| 4589 | } |
| 4590 | |
| 4591 | // isnan is converted to OpIsNan |
| 4592 | if (Callee->getName().equals("__spirv_isnanf") || |
| 4593 | Callee->getName().equals("__spirv_isnanDv2_f") || |
| 4594 | Callee->getName().equals("__spirv_isnanDv3_f") || |
| 4595 | Callee->getName().equals("__spirv_isnanDv4_f")) { |
| 4596 | // |
| 4597 | // Generate OpIsInf. |
| 4598 | // |
| 4599 | // Ops[0] = Result Type ID |
| 4600 | // Ops[1] = X ID |
| 4601 | // |
| 4602 | SPIRVOperandList Ops; |
| 4603 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4604 | Ops << MkId(lookupType(I.getType())) |
| 4605 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4606 | |
| 4607 | VMap[&I] = nextID; |
| 4608 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4609 | auto *Inst = new SPIRVInstruction(spv::OpIsNan, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4610 | SPIRVInstList.push_back(Inst); |
| 4611 | break; |
| 4612 | } |
| 4613 | |
| 4614 | // all is converted to OpAll |
| 4615 | if (Callee->getName().equals("__spirv_allDv2_i") || |
| 4616 | Callee->getName().equals("__spirv_allDv3_i") || |
| 4617 | Callee->getName().equals("__spirv_allDv4_i")) { |
| 4618 | // |
| 4619 | // Generate OpAll. |
| 4620 | // |
| 4621 | // Ops[0] = Result Type ID |
| 4622 | // Ops[1] = Vector ID |
| 4623 | // |
| 4624 | SPIRVOperandList Ops; |
| 4625 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4626 | Ops << MkId(lookupType(I.getType())) |
| 4627 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4628 | |
| 4629 | VMap[&I] = nextID; |
| 4630 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4631 | auto *Inst = new SPIRVInstruction(spv::OpAll, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4632 | SPIRVInstList.push_back(Inst); |
| 4633 | break; |
| 4634 | } |
| 4635 | |
| 4636 | // any is converted to OpAny |
| 4637 | if (Callee->getName().equals("__spirv_anyDv2_i") || |
| 4638 | Callee->getName().equals("__spirv_anyDv3_i") || |
| 4639 | Callee->getName().equals("__spirv_anyDv4_i")) { |
| 4640 | // |
| 4641 | // Generate OpAny. |
| 4642 | // |
| 4643 | // Ops[0] = Result Type ID |
| 4644 | // Ops[1] = Vector ID |
| 4645 | // |
| 4646 | SPIRVOperandList Ops; |
| 4647 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4648 | Ops << MkId(lookupType(I.getType())) |
| 4649 | << MkId(VMap[Call->getArgOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4650 | |
| 4651 | VMap[&I] = nextID; |
| 4652 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4653 | auto *Inst = new SPIRVInstruction(spv::OpAny, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4654 | SPIRVInstList.push_back(Inst); |
| 4655 | break; |
| 4656 | } |
| 4657 | |
| 4658 | // read_image is converted to OpSampledImage and OpImageSampleExplicitLod. |
| 4659 | // Additionally, OpTypeSampledImage is generated. |
| 4660 | if (Callee->getName().equals( |
| 4661 | "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") || |
| 4662 | Callee->getName().equals( |
| 4663 | "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) { |
| 4664 | // |
| 4665 | // Generate OpSampledImage. |
| 4666 | // |
| 4667 | // Ops[0] = Result Type ID |
| 4668 | // Ops[1] = Image ID |
| 4669 | // Ops[2] = Sampler ID |
| 4670 | // |
| 4671 | SPIRVOperandList Ops; |
| 4672 | |
| 4673 | Value *Image = Call->getArgOperand(0); |
| 4674 | Value *Sampler = Call->getArgOperand(1); |
| 4675 | Value *Coordinate = Call->getArgOperand(2); |
| 4676 | |
| 4677 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 4678 | Type *ImageTy = Image->getType()->getPointerElementType(); |
| 4679 | uint32_t ImageTyID = OpImageTypeMap[ImageTy]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4680 | uint32_t ImageID = VMap[Image]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4681 | uint32_t SamplerID = VMap[Sampler]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4682 | |
| 4683 | Ops << MkId(ImageTyID) << MkId(ImageID) << MkId(SamplerID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4684 | |
| 4685 | uint32_t SampledImageID = nextID; |
| 4686 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4687 | auto *Inst = new SPIRVInstruction(spv::OpSampledImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4688 | SPIRVInstList.push_back(Inst); |
| 4689 | |
| 4690 | // |
| 4691 | // Generate OpImageSampleExplicitLod. |
| 4692 | // |
| 4693 | // Ops[0] = Result Type ID |
| 4694 | // Ops[1] = Sampled Image ID |
| 4695 | // Ops[2] = Coordinate ID |
| 4696 | // Ops[3] = Image Operands Type ID |
| 4697 | // Ops[4] ... Ops[n] = Operands ID |
| 4698 | // |
| 4699 | Ops.clear(); |
| 4700 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4701 | Ops << MkId(lookupType(Call->getType())) << MkId(SampledImageID) |
| 4702 | << MkId(VMap[Coordinate]) << MkNum(spv::ImageOperandsLodMask); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4703 | |
| 4704 | Constant *CstFP0 = ConstantFP::get(Context, APFloat(0.0f)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4705 | Ops << MkId(VMap[CstFP0]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4706 | |
| 4707 | VMap[&I] = nextID; |
| 4708 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4709 | Inst = new SPIRVInstruction(spv::OpImageSampleExplicitLod, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4710 | SPIRVInstList.push_back(Inst); |
| 4711 | break; |
| 4712 | } |
| 4713 | |
| 4714 | // write_imagef is mapped to OpImageWrite. |
| 4715 | if (Callee->getName().equals( |
| 4716 | "_Z12write_imagef14ocl_image2d_woDv2_iDv4_f") || |
| 4717 | Callee->getName().equals( |
| 4718 | "_Z12write_imagef14ocl_image3d_woDv4_iDv4_f")) { |
| 4719 | // |
| 4720 | // Generate OpImageWrite. |
| 4721 | // |
| 4722 | // Ops[0] = Image ID |
| 4723 | // Ops[1] = Coordinate ID |
| 4724 | // Ops[2] = Texel ID |
| 4725 | // Ops[3] = (Optional) Image Operands Type (Literal Number) |
| 4726 | // Ops[4] ... Ops[n] = (Optional) Operands ID |
| 4727 | // |
| 4728 | SPIRVOperandList Ops; |
| 4729 | |
| 4730 | Value *Image = Call->getArgOperand(0); |
| 4731 | Value *Coordinate = Call->getArgOperand(1); |
| 4732 | Value *Texel = Call->getArgOperand(2); |
| 4733 | |
| 4734 | uint32_t ImageID = VMap[Image]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4735 | uint32_t CoordinateID = VMap[Coordinate]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4736 | uint32_t TexelID = VMap[Texel]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4737 | Ops << MkId(ImageID) << MkId(CoordinateID) << MkId(TexelID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4738 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4739 | auto *Inst = new SPIRVInstruction(spv::OpImageWrite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4740 | SPIRVInstList.push_back(Inst); |
| 4741 | break; |
| 4742 | } |
| 4743 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4744 | // get_image_width is mapped to OpImageQuerySize |
| 4745 | if (Callee->getName().equals("_Z15get_image_width14ocl_image2d_ro") || |
| 4746 | Callee->getName().equals("_Z15get_image_width14ocl_image2d_wo") || |
| 4747 | Callee->getName().equals("_Z16get_image_height14ocl_image2d_ro") || |
| 4748 | Callee->getName().equals("_Z16get_image_height14ocl_image2d_wo")) { |
| 4749 | // |
| 4750 | // Generate OpImageQuerySize, then pull out the right component. |
| 4751 | // Assume 2D image for now. |
| 4752 | // |
| 4753 | // Ops[0] = Image ID |
| 4754 | // |
| 4755 | // %sizes = OpImageQuerySizes %uint2 %im |
| 4756 | // %result = OpCompositeExtract %uint %sizes 0-or-1 |
| 4757 | SPIRVOperandList Ops; |
| 4758 | |
| 4759 | // Implement: |
| 4760 | // %sizes = OpImageQuerySizes %uint2 %im |
| 4761 | uint32_t SizesTypeID = |
| 4762 | TypeMap[VectorType::get(Type::getInt32Ty(Context), 2)]; |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4763 | Value *Image = Call->getArgOperand(0); |
| 4764 | uint32_t ImageID = VMap[Image]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4765 | Ops << MkId(SizesTypeID) << MkId(ImageID); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4766 | |
| 4767 | uint32_t SizesID = nextID++; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4768 | auto *QueryInst = |
| 4769 | new SPIRVInstruction(spv::OpImageQuerySize, SizesID, Ops); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4770 | SPIRVInstList.push_back(QueryInst); |
| 4771 | |
| 4772 | // Reset value map entry since we generated an intermediate instruction. |
| 4773 | VMap[&I] = nextID; |
| 4774 | |
| 4775 | // Implement: |
| 4776 | // %result = OpCompositeExtract %uint %sizes 0-or-1 |
| 4777 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4778 | Ops << MkId(TypeMap[I.getType()]) << MkId(SizesID); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4779 | |
| 4780 | uint32_t component = Callee->getName().contains("height") ? 1 : 0; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4781 | Ops << MkNum(component); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4782 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4783 | auto *Inst = new SPIRVInstruction(spv::OpCompositeExtract, nextID++, Ops); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4784 | SPIRVInstList.push_back(Inst); |
| 4785 | break; |
| 4786 | } |
| 4787 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4788 | // Call instrucion is deferred because it needs function's ID. Record |
| 4789 | // slot's location on SPIRVInstructionList. |
| 4790 | DeferredInsts.push_back( |
| 4791 | std::make_tuple(&I, --SPIRVInstList.end(), nextID++)); |
| 4792 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4793 | // Check whether the implementation of this call uses an extended |
| 4794 | // instruction plus one more value-producing instruction. If so, then |
| 4795 | // reserve the id for the extra value-producing slot. |
| 4796 | glsl::ExtInst EInst = getIndirectExtInstEnum(Callee->getName()); |
| 4797 | if (EInst != kGlslExtInstBad) { |
| 4798 | // Reserve a spot for the extra value. |
David Neto | 4d02a53 | 2017-09-17 12:57:44 -0400 | [diff] [blame] | 4799 | // Increase nextID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4800 | VMap[&I] = nextID; |
| 4801 | nextID++; |
| 4802 | } |
| 4803 | break; |
| 4804 | } |
| 4805 | case Instruction::Ret: { |
| 4806 | unsigned NumOps = I.getNumOperands(); |
| 4807 | if (NumOps == 0) { |
| 4808 | // |
| 4809 | // Generate OpReturn. |
| 4810 | // |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4811 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpReturn, {})); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4812 | } else { |
| 4813 | // |
| 4814 | // Generate OpReturnValue. |
| 4815 | // |
| 4816 | |
| 4817 | // Ops[0] = Return Value ID |
| 4818 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4819 | |
| 4820 | Ops << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4821 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4822 | auto *Inst = new SPIRVInstruction(spv::OpReturnValue, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4823 | SPIRVInstList.push_back(Inst); |
| 4824 | break; |
| 4825 | } |
| 4826 | break; |
| 4827 | } |
| 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | void SPIRVProducerPass::GenerateFuncEpilogue() { |
| 4832 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 4833 | |
| 4834 | // |
| 4835 | // Generate OpFunctionEnd |
| 4836 | // |
| 4837 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4838 | auto *Inst = new SPIRVInstruction(spv::OpFunctionEnd, {}); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4839 | SPIRVInstList.push_back(Inst); |
| 4840 | } |
| 4841 | |
| 4842 | bool SPIRVProducerPass::is4xi8vec(Type *Ty) const { |
| 4843 | LLVMContext &Context = Ty->getContext(); |
| 4844 | if (Ty->isVectorTy()) { |
| 4845 | if (Ty->getVectorElementType() == Type::getInt8Ty(Context) && |
| 4846 | Ty->getVectorNumElements() == 4) { |
| 4847 | return true; |
| 4848 | } |
| 4849 | } |
| 4850 | |
| 4851 | return false; |
| 4852 | } |
| 4853 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4854 | uint32_t SPIRVProducerPass::GetI32Zero() { |
| 4855 | if (0 == constant_i32_zero_id_) { |
| 4856 | llvm_unreachable("Requesting a 32-bit integer constant but it is not " |
| 4857 | "defined in the SPIR-V module"); |
| 4858 | } |
| 4859 | return constant_i32_zero_id_; |
| 4860 | } |
| 4861 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4862 | void SPIRVProducerPass::HandleDeferredInstruction() { |
| 4863 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 4864 | ValueMapType &VMap = getValueMap(); |
| 4865 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 4866 | |
| 4867 | for (auto DeferredInst = DeferredInsts.rbegin(); |
| 4868 | DeferredInst != DeferredInsts.rend(); ++DeferredInst) { |
| 4869 | Value *Inst = std::get<0>(*DeferredInst); |
| 4870 | SPIRVInstructionList::iterator InsertPoint = ++std::get<1>(*DeferredInst); |
| 4871 | if (InsertPoint != SPIRVInstList.end()) { |
| 4872 | while ((*InsertPoint)->getOpcode() == spv::OpPhi) { |
| 4873 | ++InsertPoint; |
| 4874 | } |
| 4875 | } |
| 4876 | |
| 4877 | if (BranchInst *Br = dyn_cast<BranchInst>(Inst)) { |
| 4878 | // Check whether basic block, which has this branch instruction, is loop |
| 4879 | // header or not. If it is loop header, generate OpLoopMerge and |
| 4880 | // OpBranchConditional. |
| 4881 | Function *Func = Br->getParent()->getParent(); |
| 4882 | DominatorTree &DT = |
| 4883 | getAnalysis<DominatorTreeWrapperPass>(*Func).getDomTree(); |
| 4884 | const LoopInfo &LI = |
| 4885 | getAnalysis<LoopInfoWrapperPass>(*Func).getLoopInfo(); |
| 4886 | |
| 4887 | BasicBlock *BrBB = Br->getParent(); |
| 4888 | if (LI.isLoopHeader(BrBB)) { |
| 4889 | Value *ContinueBB = nullptr; |
| 4890 | Value *MergeBB = nullptr; |
| 4891 | |
| 4892 | Loop *L = LI.getLoopFor(BrBB); |
| 4893 | MergeBB = L->getExitBlock(); |
| 4894 | if (!MergeBB) { |
| 4895 | // StructurizeCFG pass converts CFG into triangle shape and the cfg |
| 4896 | // has regions with single entry/exit. As a result, loop should not |
| 4897 | // have multiple exits. |
| 4898 | llvm_unreachable("Loop has multiple exits???"); |
| 4899 | } |
| 4900 | |
| 4901 | if (L->isLoopLatch(BrBB)) { |
| 4902 | ContinueBB = BrBB; |
| 4903 | } else { |
| 4904 | // From SPIR-V spec 2.11, Continue Target must dominate that back-edge |
| 4905 | // block. |
| 4906 | BasicBlock *Header = L->getHeader(); |
| 4907 | BasicBlock *Latch = L->getLoopLatch(); |
| 4908 | for (BasicBlock *BB : L->blocks()) { |
| 4909 | if (BB == Header) { |
| 4910 | continue; |
| 4911 | } |
| 4912 | |
| 4913 | // Check whether block dominates block with back-edge. |
| 4914 | if (DT.dominates(BB, Latch)) { |
| 4915 | ContinueBB = BB; |
| 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | if (!ContinueBB) { |
| 4920 | llvm_unreachable("Wrong continue block from loop"); |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | // |
| 4925 | // Generate OpLoopMerge. |
| 4926 | // |
| 4927 | // Ops[0] = Merge Block ID |
| 4928 | // Ops[1] = Continue Target ID |
| 4929 | // Ops[2] = Selection Control |
| 4930 | SPIRVOperandList Ops; |
| 4931 | |
| 4932 | // StructurizeCFG pass already manipulated CFG. Just use false block of |
| 4933 | // branch instruction as merge block. |
| 4934 | uint32_t MergeBBID = VMap[MergeBB]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4935 | uint32_t ContinueBBID = VMap[ContinueBB]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4936 | Ops << MkId(MergeBBID) << MkId(ContinueBBID) |
| 4937 | << MkNum(spv::SelectionControlMaskNone); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4938 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4939 | auto *MergeInst = new SPIRVInstruction(spv::OpLoopMerge, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4940 | SPIRVInstList.insert(InsertPoint, MergeInst); |
| 4941 | |
| 4942 | } else if (Br->isConditional()) { |
| 4943 | bool HasBackEdge = false; |
| 4944 | |
| 4945 | for (unsigned i = 0; i < Br->getNumSuccessors(); i++) { |
| 4946 | if (LI.isLoopHeader(Br->getSuccessor(i))) { |
| 4947 | HasBackEdge = true; |
| 4948 | } |
| 4949 | } |
| 4950 | if (!HasBackEdge) { |
| 4951 | // |
| 4952 | // Generate OpSelectionMerge. |
| 4953 | // |
| 4954 | // Ops[0] = Merge Block ID |
| 4955 | // Ops[1] = Selection Control |
| 4956 | SPIRVOperandList Ops; |
| 4957 | |
| 4958 | // StructurizeCFG pass already manipulated CFG. Just use false block |
| 4959 | // of branch instruction as merge block. |
| 4960 | uint32_t MergeBBID = VMap[Br->getSuccessor(1)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4961 | Ops << MkId(MergeBBID) << MkNum(spv::SelectionControlMaskNone); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4962 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4963 | auto *MergeInst = new SPIRVInstruction(spv::OpSelectionMerge, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4964 | SPIRVInstList.insert(InsertPoint, MergeInst); |
| 4965 | } |
| 4966 | } |
| 4967 | |
| 4968 | if (Br->isConditional()) { |
| 4969 | // |
| 4970 | // Generate OpBranchConditional. |
| 4971 | // |
| 4972 | // Ops[0] = Condition ID |
| 4973 | // Ops[1] = True Label ID |
| 4974 | // Ops[2] = False Label ID |
| 4975 | // Ops[3] ... Ops[n] = Branch weights (Literal Number) |
| 4976 | SPIRVOperandList Ops; |
| 4977 | |
| 4978 | uint32_t CondID = VMap[Br->getCondition()]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4979 | uint32_t TrueBBID = VMap[Br->getSuccessor(0)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4980 | uint32_t FalseBBID = VMap[Br->getSuccessor(1)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4981 | |
| 4982 | Ops << MkId(CondID) << MkId(TrueBBID) << MkId(FalseBBID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4983 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4984 | auto *BrInst = new SPIRVInstruction(spv::OpBranchConditional, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4985 | SPIRVInstList.insert(InsertPoint, BrInst); |
| 4986 | } else { |
| 4987 | // |
| 4988 | // Generate OpBranch. |
| 4989 | // |
| 4990 | // Ops[0] = Target Label ID |
| 4991 | SPIRVOperandList Ops; |
| 4992 | |
| 4993 | uint32_t TargetID = VMap[Br->getSuccessor(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4994 | Ops << MkId(TargetID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4995 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4996 | SPIRVInstList.insert(InsertPoint, |
| 4997 | new SPIRVInstruction(spv::OpBranch, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4998 | } |
| 4999 | } else if (PHINode *PHI = dyn_cast<PHINode>(Inst)) { |
| 5000 | // |
| 5001 | // Generate OpPhi. |
| 5002 | // |
| 5003 | // Ops[0] = Result Type ID |
| 5004 | // Ops[1] ... Ops[n] = (Variable ID, Parent ID) pairs |
| 5005 | SPIRVOperandList Ops; |
| 5006 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5007 | Ops << MkId(lookupType(PHI->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5008 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5009 | for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) { |
| 5010 | uint32_t VarID = VMap[PHI->getIncomingValue(i)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5011 | uint32_t ParentID = VMap[PHI->getIncomingBlock(i)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5012 | Ops << MkId(VarID) << MkId(ParentID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5016 | InsertPoint, |
| 5017 | new SPIRVInstruction(spv::OpPhi, std::get<2>(*DeferredInst), Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5018 | } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { |
| 5019 | Function *Callee = Call->getCalledFunction(); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5020 | auto callee_name = Callee->getName(); |
| 5021 | glsl::ExtInst EInst = getDirectOrIndirectExtInstEnum(callee_name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5022 | |
| 5023 | if (EInst) { |
| 5024 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 5025 | |
| 5026 | // |
| 5027 | // Generate OpExtInst. |
| 5028 | // |
| 5029 | |
| 5030 | // Ops[0] = Result Type ID |
| 5031 | // Ops[1] = Set ID (OpExtInstImport ID) |
| 5032 | // Ops[2] = Instruction Number (Literal Number) |
| 5033 | // Ops[3] ... Ops[n] = Operand 1, ... , Operand n |
| 5034 | SPIRVOperandList Ops; |
| 5035 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5036 | Ops << MkId(lookupType(Call->getType())) << MkId(ExtInstImportID) |
| 5037 | << MkNum(EInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5038 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5039 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
| 5040 | for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5041 | Ops << MkId(VMap[Call->getOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5042 | } |
| 5043 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5044 | auto *ExtInst = new SPIRVInstruction(spv::OpExtInst, |
| 5045 | std::get<2>(*DeferredInst), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5046 | SPIRVInstList.insert(InsertPoint, ExtInst); |
| 5047 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5048 | const auto IndirectExtInst = getIndirectExtInstEnum(callee_name); |
| 5049 | if (IndirectExtInst != kGlslExtInstBad) { |
| 5050 | // Generate one more instruction that uses the result of the extended |
| 5051 | // instruction. Its result id is one more than the id of the |
| 5052 | // extended instruction. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5053 | LLVMContext &Context = |
| 5054 | Call->getParent()->getParent()->getParent()->getContext(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5055 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5056 | auto generate_extra_inst = [this, &Context, &Call, &DeferredInst, |
| 5057 | &VMap, &SPIRVInstList, &InsertPoint]( |
| 5058 | spv::Op opcode, Constant *constant) { |
| 5059 | // |
| 5060 | // Generate instruction like: |
| 5061 | // result = opcode constant <extinst-result> |
| 5062 | // |
| 5063 | // Ops[0] = Result Type ID |
| 5064 | // Ops[1] = Operand 0 ;; the constant, suitably splatted |
| 5065 | // Ops[2] = Operand 1 ;; the result of the extended instruction |
| 5066 | SPIRVOperandList Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5067 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5068 | Type *resultTy = Call->getType(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5069 | Ops << MkId(lookupType(resultTy)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5070 | |
| 5071 | if (auto *vectorTy = dyn_cast<VectorType>(resultTy)) { |
| 5072 | constant = ConstantVector::getSplat( |
| 5073 | static_cast<unsigned>(vectorTy->getNumElements()), constant); |
| 5074 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5075 | Ops << MkId(VMap[constant]) << MkId(std::get<2>(*DeferredInst)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5076 | |
| 5077 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5078 | InsertPoint, new SPIRVInstruction( |
| 5079 | opcode, std::get<2>(*DeferredInst) + 1, Ops)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5080 | }; |
| 5081 | |
| 5082 | switch (IndirectExtInst) { |
| 5083 | case glsl::ExtInstFindUMsb: // Implementing clz |
| 5084 | generate_extra_inst( |
| 5085 | spv::OpISub, ConstantInt::get(Type::getInt32Ty(Context), 31)); |
| 5086 | break; |
| 5087 | case glsl::ExtInstAcos: // Implementing acospi |
| 5088 | case glsl::ExtInstAsin: // Implementing asinpi |
Kévin Petit | eb9f90a | 2018-09-29 12:29:34 +0100 | [diff] [blame] | 5089 | case glsl::ExtInstAtan: // Implementing atanpi |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5090 | case glsl::ExtInstAtan2: // Implementing atan2pi |
| 5091 | generate_extra_inst( |
| 5092 | spv::OpFMul, |
| 5093 | ConstantFP::get(Type::getFloatTy(Context), kOneOverPi)); |
| 5094 | break; |
| 5095 | |
| 5096 | default: |
| 5097 | assert(false && "internally inconsistent"); |
David Neto | 4d02a53 | 2017-09-17 12:57:44 -0400 | [diff] [blame] | 5098 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5099 | } |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5100 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5101 | } else if (callee_name.equals("_Z8popcounti") || |
| 5102 | callee_name.equals("_Z8popcountj") || |
| 5103 | callee_name.equals("_Z8popcountDv2_i") || |
| 5104 | callee_name.equals("_Z8popcountDv3_i") || |
| 5105 | callee_name.equals("_Z8popcountDv4_i") || |
| 5106 | callee_name.equals("_Z8popcountDv2_j") || |
| 5107 | callee_name.equals("_Z8popcountDv3_j") || |
| 5108 | callee_name.equals("_Z8popcountDv4_j")) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5109 | // |
| 5110 | // Generate OpBitCount |
| 5111 | // |
| 5112 | // Ops[0] = Result Type ID |
| 5113 | // Ops[1] = Base ID |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5114 | SPIRVOperandList Ops; |
| 5115 | Ops << MkId(lookupType(Call->getType())) |
| 5116 | << MkId(VMap[Call->getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5117 | |
| 5118 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5119 | InsertPoint, new SPIRVInstruction(spv::OpBitCount, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5120 | std::get<2>(*DeferredInst), Ops)); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5121 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5122 | } else if (callee_name.startswith(kCompositeConstructFunctionPrefix)) { |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5123 | |
| 5124 | // Generate an OpCompositeConstruct |
| 5125 | SPIRVOperandList Ops; |
| 5126 | |
| 5127 | // The result type. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5128 | Ops << MkId(lookupType(Call->getType())); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5129 | |
| 5130 | for (Use &use : Call->arg_operands()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5131 | Ops << MkId(VMap[use.get()]); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5135 | InsertPoint, new SPIRVInstruction(spv::OpCompositeConstruct, |
| 5136 | std::get<2>(*DeferredInst), Ops)); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5137 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 5138 | } else if (callee_name.startswith(clspv::ResourceAccessorFunction())) { |
| 5139 | |
| 5140 | // We have already mapped the call's result value to an ID. |
| 5141 | // Don't generate any code now. |
| 5142 | |
| 5143 | } else if (callee_name.startswith(clspv::WorkgroupAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5144 | |
| 5145 | // We have already mapped the call's result value to an ID. |
| 5146 | // Don't generate any code now. |
| 5147 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5148 | } else { |
| 5149 | // |
| 5150 | // Generate OpFunctionCall. |
| 5151 | // |
| 5152 | |
| 5153 | // Ops[0] = Result Type ID |
| 5154 | // Ops[1] = Callee Function ID |
| 5155 | // Ops[2] ... Ops[n] = Argument 0, ... , Argument n |
| 5156 | SPIRVOperandList Ops; |
| 5157 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5158 | Ops << MkId(lookupType(Call->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5159 | |
| 5160 | uint32_t CalleeID = VMap[Callee]; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 5161 | if (CalleeID == 0) { |
| 5162 | errs() << "Can't translate function call. Missing builtin? " |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5163 | << callee_name << " in: " << *Call << "\n"; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 5164 | // TODO(dneto): Can we error out? Enabling this llvm_unreachable |
| 5165 | // causes an infinite loop. Instead, go ahead and generate |
| 5166 | // the bad function call. A validator will catch the 0-Id. |
| 5167 | // llvm_unreachable("Can't translate function call"); |
| 5168 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5169 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5170 | Ops << MkId(CalleeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5171 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5172 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
| 5173 | for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5174 | Ops << MkId(VMap[Call->getOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5175 | } |
| 5176 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5177 | auto *CallInst = new SPIRVInstruction(spv::OpFunctionCall, |
| 5178 | std::get<2>(*DeferredInst), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5179 | SPIRVInstList.insert(InsertPoint, CallInst); |
| 5180 | } |
| 5181 | } |
| 5182 | } |
| 5183 | } |
| 5184 | |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5185 | void SPIRVProducerPass::HandleDeferredDecorations(const DataLayout &DL) { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 5186 | if (getTypesNeedingArrayStride().empty() && LocalArgSpecIds.empty()) { |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5187 | return; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5188 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5189 | |
| 5190 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5191 | |
| 5192 | // Find an iterator pointing just past the last decoration. |
| 5193 | bool seen_decorations = false; |
| 5194 | auto DecoInsertPoint = |
| 5195 | std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(), |
| 5196 | [&seen_decorations](SPIRVInstruction *Inst) -> bool { |
| 5197 | const bool is_decoration = |
| 5198 | Inst->getOpcode() == spv::OpDecorate || |
| 5199 | Inst->getOpcode() == spv::OpMemberDecorate; |
| 5200 | if (is_decoration) { |
| 5201 | seen_decorations = true; |
| 5202 | return false; |
| 5203 | } else { |
| 5204 | return seen_decorations; |
| 5205 | } |
| 5206 | }); |
| 5207 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5208 | // Insert ArrayStride decorations on pointer types, due to OpPtrAccessChain |
| 5209 | // instructions we generated earlier. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 5210 | for (auto *type : getTypesNeedingArrayStride()) { |
| 5211 | Type *elemTy = nullptr; |
| 5212 | if (auto *ptrTy = dyn_cast<PointerType>(type)) { |
| 5213 | elemTy = ptrTy->getElementType(); |
| 5214 | } else if (auto* arrayTy = dyn_cast<ArrayType>(type)) { |
| 5215 | elemTy = arrayTy->getArrayElementType(); |
| 5216 | } else if (auto* seqTy = dyn_cast<SequentialType>(type)) { |
| 5217 | elemTy = seqTy->getSequentialElementType(); |
| 5218 | } else { |
| 5219 | errs() << "Unhandled strided type " << *type << "\n"; |
| 5220 | llvm_unreachable("Unhandled strided type"); |
| 5221 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5222 | |
| 5223 | // Ops[0] = Target ID |
| 5224 | // Ops[1] = Decoration (ArrayStride) |
| 5225 | // Ops[2] = Stride number (Literal Number) |
| 5226 | SPIRVOperandList Ops; |
| 5227 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 5228 | // Same as DL.getIndexedOffsetInType( elemTy, { 1 } ); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5229 | const uint32_t stride = static_cast<uint32_t>(GetTypeAllocSize(elemTy, DL)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5230 | |
| 5231 | Ops << MkId(lookupType(type)) << MkNum(spv::DecorationArrayStride) |
| 5232 | << MkNum(stride); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5233 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5234 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5235 | SPIRVInstList.insert(DecoInsertPoint, DecoInst); |
| 5236 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5237 | |
| 5238 | // Emit SpecId decorations targeting the array size value. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 5239 | for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_; |
| 5240 | ++spec_id) { |
| 5241 | LocalArgInfo& arg_info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5242 | SPIRVOperandList Ops; |
| 5243 | Ops << MkId(arg_info.array_size_id) << MkNum(spv::DecorationSpecId) |
| 5244 | << MkNum(arg_info.spec_id); |
| 5245 | SPIRVInstList.insert(DecoInsertPoint, |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5246 | new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5247 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5248 | } |
| 5249 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5250 | glsl::ExtInst SPIRVProducerPass::getExtInstEnum(StringRef Name) { |
| 5251 | return StringSwitch<glsl::ExtInst>(Name) |
| 5252 | .Case("_Z3absi", glsl::ExtInst::ExtInstSAbs) |
| 5253 | .Case("_Z3absDv2_i", glsl::ExtInst::ExtInstSAbs) |
| 5254 | .Case("_Z3absDv3_i", glsl::ExtInst::ExtInstSAbs) |
| 5255 | .Case("_Z3absDv4_i", glsl::ExtInst::ExtInstSAbs) |
| 5256 | .Case("_Z5clampiii", glsl::ExtInst::ExtInstSClamp) |
| 5257 | .Case("_Z5clampDv2_iS_S_", glsl::ExtInst::ExtInstSClamp) |
| 5258 | .Case("_Z5clampDv3_iS_S_", glsl::ExtInst::ExtInstSClamp) |
| 5259 | .Case("_Z5clampDv4_iS_S_", glsl::ExtInst::ExtInstSClamp) |
| 5260 | .Case("_Z5clampjjj", glsl::ExtInst::ExtInstUClamp) |
| 5261 | .Case("_Z5clampDv2_jS_S_", glsl::ExtInst::ExtInstUClamp) |
| 5262 | .Case("_Z5clampDv3_jS_S_", glsl::ExtInst::ExtInstUClamp) |
| 5263 | .Case("_Z5clampDv4_jS_S_", glsl::ExtInst::ExtInstUClamp) |
| 5264 | .Case("_Z5clampfff", glsl::ExtInst::ExtInstFClamp) |
| 5265 | .Case("_Z5clampDv2_fS_S_", glsl::ExtInst::ExtInstFClamp) |
| 5266 | .Case("_Z5clampDv3_fS_S_", glsl::ExtInst::ExtInstFClamp) |
| 5267 | .Case("_Z5clampDv4_fS_S_", glsl::ExtInst::ExtInstFClamp) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5268 | .Case("_Z3maxii", glsl::ExtInst::ExtInstSMax) |
| 5269 | .Case("_Z3maxDv2_iS_", glsl::ExtInst::ExtInstSMax) |
| 5270 | .Case("_Z3maxDv3_iS_", glsl::ExtInst::ExtInstSMax) |
| 5271 | .Case("_Z3maxDv4_iS_", glsl::ExtInst::ExtInstSMax) |
| 5272 | .Case("_Z3maxjj", glsl::ExtInst::ExtInstUMax) |
| 5273 | .Case("_Z3maxDv2_jS_", glsl::ExtInst::ExtInstUMax) |
| 5274 | .Case("_Z3maxDv3_jS_", glsl::ExtInst::ExtInstUMax) |
| 5275 | .Case("_Z3maxDv4_jS_", glsl::ExtInst::ExtInstUMax) |
| 5276 | .Case("_Z3maxff", glsl::ExtInst::ExtInstFMax) |
| 5277 | .Case("_Z3maxDv2_fS_", glsl::ExtInst::ExtInstFMax) |
| 5278 | .Case("_Z3maxDv3_fS_", glsl::ExtInst::ExtInstFMax) |
| 5279 | .Case("_Z3maxDv4_fS_", glsl::ExtInst::ExtInstFMax) |
| 5280 | .StartsWith("_Z4fmax", glsl::ExtInst::ExtInstFMax) |
| 5281 | .Case("_Z3minii", glsl::ExtInst::ExtInstSMin) |
| 5282 | .Case("_Z3minDv2_iS_", glsl::ExtInst::ExtInstSMin) |
| 5283 | .Case("_Z3minDv3_iS_", glsl::ExtInst::ExtInstSMin) |
| 5284 | .Case("_Z3minDv4_iS_", glsl::ExtInst::ExtInstSMin) |
| 5285 | .Case("_Z3minjj", glsl::ExtInst::ExtInstUMin) |
| 5286 | .Case("_Z3minDv2_jS_", glsl::ExtInst::ExtInstUMin) |
| 5287 | .Case("_Z3minDv3_jS_", glsl::ExtInst::ExtInstUMin) |
| 5288 | .Case("_Z3minDv4_jS_", glsl::ExtInst::ExtInstUMin) |
| 5289 | .Case("_Z3minff", glsl::ExtInst::ExtInstFMin) |
| 5290 | .Case("_Z3minDv2_fS_", glsl::ExtInst::ExtInstFMin) |
| 5291 | .Case("_Z3minDv3_fS_", glsl::ExtInst::ExtInstFMin) |
| 5292 | .Case("_Z3minDv4_fS_", glsl::ExtInst::ExtInstFMin) |
| 5293 | .StartsWith("_Z4fmin", glsl::ExtInst::ExtInstFMin) |
| 5294 | .StartsWith("_Z7degrees", glsl::ExtInst::ExtInstDegrees) |
| 5295 | .StartsWith("_Z7radians", glsl::ExtInst::ExtInstRadians) |
| 5296 | .StartsWith("_Z3mix", glsl::ExtInst::ExtInstFMix) |
| 5297 | .StartsWith("_Z4acos", glsl::ExtInst::ExtInstAcos) |
| 5298 | .StartsWith("_Z5acosh", glsl::ExtInst::ExtInstAcosh) |
| 5299 | .StartsWith("_Z4asin", glsl::ExtInst::ExtInstAsin) |
| 5300 | .StartsWith("_Z5asinh", glsl::ExtInst::ExtInstAsinh) |
| 5301 | .StartsWith("_Z4atan", glsl::ExtInst::ExtInstAtan) |
| 5302 | .StartsWith("_Z5atan2", glsl::ExtInst::ExtInstAtan2) |
| 5303 | .StartsWith("_Z5atanh", glsl::ExtInst::ExtInstAtanh) |
| 5304 | .StartsWith("_Z4ceil", glsl::ExtInst::ExtInstCeil) |
| 5305 | .StartsWith("_Z3sin", glsl::ExtInst::ExtInstSin) |
| 5306 | .StartsWith("_Z4sinh", glsl::ExtInst::ExtInstSinh) |
| 5307 | .StartsWith("_Z8half_sin", glsl::ExtInst::ExtInstSin) |
| 5308 | .StartsWith("_Z10native_sin", glsl::ExtInst::ExtInstSin) |
| 5309 | .StartsWith("_Z3cos", glsl::ExtInst::ExtInstCos) |
| 5310 | .StartsWith("_Z4cosh", glsl::ExtInst::ExtInstCosh) |
| 5311 | .StartsWith("_Z8half_cos", glsl::ExtInst::ExtInstCos) |
| 5312 | .StartsWith("_Z10native_cos", glsl::ExtInst::ExtInstCos) |
| 5313 | .StartsWith("_Z3tan", glsl::ExtInst::ExtInstTan) |
| 5314 | .StartsWith("_Z4tanh", glsl::ExtInst::ExtInstTanh) |
| 5315 | .StartsWith("_Z8half_tan", glsl::ExtInst::ExtInstTan) |
| 5316 | .StartsWith("_Z10native_tan", glsl::ExtInst::ExtInstTan) |
| 5317 | .StartsWith("_Z3exp", glsl::ExtInst::ExtInstExp) |
| 5318 | .StartsWith("_Z8half_exp", glsl::ExtInst::ExtInstExp) |
| 5319 | .StartsWith("_Z10native_exp", glsl::ExtInst::ExtInstExp) |
| 5320 | .StartsWith("_Z4exp2", glsl::ExtInst::ExtInstExp2) |
| 5321 | .StartsWith("_Z9half_exp2", glsl::ExtInst::ExtInstExp2) |
| 5322 | .StartsWith("_Z11native_exp2", glsl::ExtInst::ExtInstExp2) |
| 5323 | .StartsWith("_Z3log", glsl::ExtInst::ExtInstLog) |
| 5324 | .StartsWith("_Z8half_log", glsl::ExtInst::ExtInstLog) |
| 5325 | .StartsWith("_Z10native_log", glsl::ExtInst::ExtInstLog) |
| 5326 | .StartsWith("_Z4log2", glsl::ExtInst::ExtInstLog2) |
| 5327 | .StartsWith("_Z9half_log2", glsl::ExtInst::ExtInstLog2) |
| 5328 | .StartsWith("_Z11native_log2", glsl::ExtInst::ExtInstLog2) |
| 5329 | .StartsWith("_Z4fabs", glsl::ExtInst::ExtInstFAbs) |
kpet | 3458e94 | 2018-10-03 14:35:21 +0100 | [diff] [blame] | 5330 | .StartsWith("_Z3fma", glsl::ExtInst::ExtInstFma) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5331 | .StartsWith("_Z5floor", glsl::ExtInst::ExtInstFloor) |
| 5332 | .StartsWith("_Z5ldexp", glsl::ExtInst::ExtInstLdexp) |
| 5333 | .StartsWith("_Z3pow", glsl::ExtInst::ExtInstPow) |
| 5334 | .StartsWith("_Z4powr", glsl::ExtInst::ExtInstPow) |
| 5335 | .StartsWith("_Z9half_powr", glsl::ExtInst::ExtInstPow) |
| 5336 | .StartsWith("_Z11native_powr", glsl::ExtInst::ExtInstPow) |
| 5337 | .StartsWith("_Z5round", glsl::ExtInst::ExtInstRound) |
| 5338 | .StartsWith("_Z4sqrt", glsl::ExtInst::ExtInstSqrt) |
| 5339 | .StartsWith("_Z9half_sqrt", glsl::ExtInst::ExtInstSqrt) |
| 5340 | .StartsWith("_Z11native_sqrt", glsl::ExtInst::ExtInstSqrt) |
| 5341 | .StartsWith("_Z5rsqrt", glsl::ExtInst::ExtInstInverseSqrt) |
| 5342 | .StartsWith("_Z10half_rsqrt", glsl::ExtInst::ExtInstInverseSqrt) |
| 5343 | .StartsWith("_Z12native_rsqrt", glsl::ExtInst::ExtInstInverseSqrt) |
| 5344 | .StartsWith("_Z5trunc", glsl::ExtInst::ExtInstTrunc) |
| 5345 | .StartsWith("_Z5frexp", glsl::ExtInst::ExtInstFrexp) |
| 5346 | .StartsWith("_Z4sign", glsl::ExtInst::ExtInstFSign) |
| 5347 | .StartsWith("_Z6length", glsl::ExtInst::ExtInstLength) |
Kévin Petit | 7d09cec | 2018-09-22 15:43:38 +0100 | [diff] [blame] | 5348 | .StartsWith("_Z11fast_length", glsl::ExtInst::ExtInstLength) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5349 | .StartsWith("_Z8distance", glsl::ExtInst::ExtInstDistance) |
Kévin Petit | 7d09cec | 2018-09-22 15:43:38 +0100 | [diff] [blame] | 5350 | .StartsWith("_Z13fast_distance", glsl::ExtInst::ExtInstDistance) |
David Neto | e9a0351 | 2017-10-16 10:08:27 -0400 | [diff] [blame] | 5351 | .StartsWith("_Z4step", glsl::ExtInst::ExtInstStep) |
kpet | 6fd2a26 | 2018-10-03 14:48:01 +0100 | [diff] [blame] | 5352 | .StartsWith("_Z10smoothstep", glsl::ExtInst::ExtInstSmoothStep) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5353 | .Case("_Z5crossDv3_fS_", glsl::ExtInst::ExtInstCross) |
| 5354 | .StartsWith("_Z9normalize", glsl::ExtInst::ExtInstNormalize) |
Kévin Petit | 7d09cec | 2018-09-22 15:43:38 +0100 | [diff] [blame] | 5355 | .StartsWith("_Z14fast_normalize", glsl::ExtInst::ExtInstNormalize) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5356 | .StartsWith("llvm.fmuladd.", glsl::ExtInst::ExtInstFma) |
| 5357 | .Case("spirv.unpack.v2f16", glsl::ExtInst::ExtInstUnpackHalf2x16) |
| 5358 | .Case("spirv.pack.v2f16", glsl::ExtInst::ExtInstPackHalf2x16) |
David Neto | 6265320 | 2017-10-16 19:05:18 -0400 | [diff] [blame] | 5359 | .Case("clspv.fract.f", glsl::ExtInst::ExtInstFract) |
| 5360 | .Case("clspv.fract.v2f", glsl::ExtInst::ExtInstFract) |
| 5361 | .Case("clspv.fract.v3f", glsl::ExtInst::ExtInstFract) |
| 5362 | .Case("clspv.fract.v4f", glsl::ExtInst::ExtInstFract) |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5363 | .Default(kGlslExtInstBad); |
| 5364 | } |
| 5365 | |
| 5366 | glsl::ExtInst SPIRVProducerPass::getIndirectExtInstEnum(StringRef Name) { |
| 5367 | // Check indirect cases. |
| 5368 | return StringSwitch<glsl::ExtInst>(Name) |
| 5369 | .StartsWith("_Z3clz", glsl::ExtInst::ExtInstFindUMsb) |
| 5370 | // Use exact match on float arg because these need a multiply |
| 5371 | // of a constant of the right floating point type. |
| 5372 | .Case("_Z6acospif", glsl::ExtInst::ExtInstAcos) |
| 5373 | .Case("_Z6acospiDv2_f", glsl::ExtInst::ExtInstAcos) |
| 5374 | .Case("_Z6acospiDv3_f", glsl::ExtInst::ExtInstAcos) |
| 5375 | .Case("_Z6acospiDv4_f", glsl::ExtInst::ExtInstAcos) |
| 5376 | .Case("_Z6asinpif", glsl::ExtInst::ExtInstAsin) |
| 5377 | .Case("_Z6asinpiDv2_f", glsl::ExtInst::ExtInstAsin) |
| 5378 | .Case("_Z6asinpiDv3_f", glsl::ExtInst::ExtInstAsin) |
| 5379 | .Case("_Z6asinpiDv4_f", glsl::ExtInst::ExtInstAsin) |
Kévin Petit | eb9f90a | 2018-09-29 12:29:34 +0100 | [diff] [blame] | 5380 | .Case("_Z6atanpif", glsl::ExtInst::ExtInstAtan) |
| 5381 | .Case("_Z6atanpiDv2_f", glsl::ExtInst::ExtInstAtan) |
| 5382 | .Case("_Z6atanpiDv3_f", glsl::ExtInst::ExtInstAtan) |
| 5383 | .Case("_Z6atanpiDv4_f", glsl::ExtInst::ExtInstAtan) |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5384 | .Case("_Z7atan2piff", glsl::ExtInst::ExtInstAtan2) |
| 5385 | .Case("_Z7atan2piDv2_fS_", glsl::ExtInst::ExtInstAtan2) |
| 5386 | .Case("_Z7atan2piDv3_fS_", glsl::ExtInst::ExtInstAtan2) |
| 5387 | .Case("_Z7atan2piDv4_fS_", glsl::ExtInst::ExtInstAtan2) |
| 5388 | .Default(kGlslExtInstBad); |
| 5389 | } |
| 5390 | |
| 5391 | glsl::ExtInst SPIRVProducerPass::getDirectOrIndirectExtInstEnum(StringRef Name) { |
| 5392 | auto direct = getExtInstEnum(Name); |
| 5393 | if (direct != kGlslExtInstBad) |
| 5394 | return direct; |
| 5395 | return getIndirectExtInstEnum(Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | void SPIRVProducerPass::PrintResID(SPIRVInstruction *Inst) { |
| 5399 | out << "%" << Inst->getResultID(); |
| 5400 | } |
| 5401 | |
| 5402 | void SPIRVProducerPass::PrintOpcode(SPIRVInstruction *Inst) { |
| 5403 | spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode()); |
| 5404 | out << "\t" << spv::getOpName(Opcode); |
| 5405 | } |
| 5406 | |
| 5407 | void SPIRVProducerPass::PrintOperand(SPIRVOperand *Op) { |
| 5408 | SPIRVOperandType OpTy = Op->getType(); |
| 5409 | switch (OpTy) { |
| 5410 | default: { |
| 5411 | llvm_unreachable("Unsupported SPIRV Operand Type???"); |
| 5412 | break; |
| 5413 | } |
| 5414 | case SPIRVOperandType::NUMBERID: { |
| 5415 | out << "%" << Op->getNumID(); |
| 5416 | break; |
| 5417 | } |
| 5418 | case SPIRVOperandType::LITERAL_STRING: { |
| 5419 | out << "\"" << Op->getLiteralStr() << "\""; |
| 5420 | break; |
| 5421 | } |
| 5422 | case SPIRVOperandType::LITERAL_INTEGER: { |
| 5423 | // TODO: Handle LiteralNum carefully. |
| 5424 | for (auto Word : Op->getLiteralNum()) { |
| 5425 | out << Word; |
| 5426 | } |
| 5427 | break; |
| 5428 | } |
| 5429 | case SPIRVOperandType::LITERAL_FLOAT: { |
| 5430 | // TODO: Handle LiteralNum carefully. |
| 5431 | for (auto Word : Op->getLiteralNum()) { |
| 5432 | APFloat APF = APFloat(APFloat::IEEEsingle(), APInt(32, Word)); |
| 5433 | SmallString<8> Str; |
| 5434 | APF.toString(Str, 6, 2); |
| 5435 | out << Str; |
| 5436 | } |
| 5437 | break; |
| 5438 | } |
| 5439 | } |
| 5440 | } |
| 5441 | |
| 5442 | void SPIRVProducerPass::PrintCapability(SPIRVOperand *Op) { |
| 5443 | spv::Capability Cap = static_cast<spv::Capability>(Op->getNumID()); |
| 5444 | out << spv::getCapabilityName(Cap); |
| 5445 | } |
| 5446 | |
| 5447 | void SPIRVProducerPass::PrintExtInst(SPIRVOperand *Op) { |
| 5448 | auto LiteralNum = Op->getLiteralNum(); |
| 5449 | glsl::ExtInst Ext = static_cast<glsl::ExtInst>(LiteralNum[0]); |
| 5450 | out << glsl::getExtInstName(Ext); |
| 5451 | } |
| 5452 | |
| 5453 | void SPIRVProducerPass::PrintAddrModel(SPIRVOperand *Op) { |
| 5454 | spv::AddressingModel AddrModel = |
| 5455 | static_cast<spv::AddressingModel>(Op->getNumID()); |
| 5456 | out << spv::getAddressingModelName(AddrModel); |
| 5457 | } |
| 5458 | |
| 5459 | void SPIRVProducerPass::PrintMemModel(SPIRVOperand *Op) { |
| 5460 | spv::MemoryModel MemModel = static_cast<spv::MemoryModel>(Op->getNumID()); |
| 5461 | out << spv::getMemoryModelName(MemModel); |
| 5462 | } |
| 5463 | |
| 5464 | void SPIRVProducerPass::PrintExecModel(SPIRVOperand *Op) { |
| 5465 | spv::ExecutionModel ExecModel = |
| 5466 | static_cast<spv::ExecutionModel>(Op->getNumID()); |
| 5467 | out << spv::getExecutionModelName(ExecModel); |
| 5468 | } |
| 5469 | |
| 5470 | void SPIRVProducerPass::PrintExecMode(SPIRVOperand *Op) { |
| 5471 | spv::ExecutionMode ExecMode = static_cast<spv::ExecutionMode>(Op->getNumID()); |
| 5472 | out << spv::getExecutionModeName(ExecMode); |
| 5473 | } |
| 5474 | |
| 5475 | void SPIRVProducerPass::PrintSourceLanguage(SPIRVOperand *Op) { |
| 5476 | spv::SourceLanguage SourceLang = static_cast<spv::SourceLanguage>(Op->getNumID()); |
| 5477 | out << spv::getSourceLanguageName(SourceLang); |
| 5478 | } |
| 5479 | |
| 5480 | void SPIRVProducerPass::PrintFuncCtrl(SPIRVOperand *Op) { |
| 5481 | spv::FunctionControlMask FuncCtrl = |
| 5482 | static_cast<spv::FunctionControlMask>(Op->getNumID()); |
| 5483 | out << spv::getFunctionControlName(FuncCtrl); |
| 5484 | } |
| 5485 | |
| 5486 | void SPIRVProducerPass::PrintStorageClass(SPIRVOperand *Op) { |
| 5487 | spv::StorageClass StClass = static_cast<spv::StorageClass>(Op->getNumID()); |
| 5488 | out << getStorageClassName(StClass); |
| 5489 | } |
| 5490 | |
| 5491 | void SPIRVProducerPass::PrintDecoration(SPIRVOperand *Op) { |
| 5492 | spv::Decoration Deco = static_cast<spv::Decoration>(Op->getNumID()); |
| 5493 | out << getDecorationName(Deco); |
| 5494 | } |
| 5495 | |
| 5496 | void SPIRVProducerPass::PrintBuiltIn(SPIRVOperand *Op) { |
| 5497 | spv::BuiltIn BIn = static_cast<spv::BuiltIn>(Op->getNumID()); |
| 5498 | out << getBuiltInName(BIn); |
| 5499 | } |
| 5500 | |
| 5501 | void SPIRVProducerPass::PrintSelectionControl(SPIRVOperand *Op) { |
| 5502 | spv::SelectionControlMask BIn = |
| 5503 | static_cast<spv::SelectionControlMask>(Op->getNumID()); |
| 5504 | out << getSelectionControlName(BIn); |
| 5505 | } |
| 5506 | |
| 5507 | void SPIRVProducerPass::PrintLoopControl(SPIRVOperand *Op) { |
| 5508 | spv::LoopControlMask BIn = static_cast<spv::LoopControlMask>(Op->getNumID()); |
| 5509 | out << getLoopControlName(BIn); |
| 5510 | } |
| 5511 | |
| 5512 | void SPIRVProducerPass::PrintDimensionality(SPIRVOperand *Op) { |
| 5513 | spv::Dim DIM = static_cast<spv::Dim>(Op->getNumID()); |
| 5514 | out << getDimName(DIM); |
| 5515 | } |
| 5516 | |
| 5517 | void SPIRVProducerPass::PrintImageFormat(SPIRVOperand *Op) { |
| 5518 | spv::ImageFormat Format = static_cast<spv::ImageFormat>(Op->getNumID()); |
| 5519 | out << getImageFormatName(Format); |
| 5520 | } |
| 5521 | |
| 5522 | void SPIRVProducerPass::PrintMemoryAccess(SPIRVOperand *Op) { |
| 5523 | out << spv::getMemoryAccessName( |
| 5524 | static_cast<spv::MemoryAccessMask>(Op->getNumID())); |
| 5525 | } |
| 5526 | |
| 5527 | void SPIRVProducerPass::PrintImageOperandsType(SPIRVOperand *Op) { |
| 5528 | auto LiteralNum = Op->getLiteralNum(); |
| 5529 | spv::ImageOperandsMask Type = |
| 5530 | static_cast<spv::ImageOperandsMask>(LiteralNum[0]); |
| 5531 | out << getImageOperandsName(Type); |
| 5532 | } |
| 5533 | |
| 5534 | void SPIRVProducerPass::WriteSPIRVAssembly() { |
| 5535 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 5536 | |
| 5537 | for (auto Inst : SPIRVInstList) { |
| 5538 | SPIRVOperandList Ops = Inst->getOperands(); |
| 5539 | spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode()); |
| 5540 | |
| 5541 | switch (Opcode) { |
| 5542 | default: { |
| 5543 | llvm_unreachable("Unsupported SPIRV instruction"); |
| 5544 | break; |
| 5545 | } |
| 5546 | case spv::OpCapability: { |
| 5547 | // Ops[0] = Capability |
| 5548 | PrintOpcode(Inst); |
| 5549 | out << " "; |
| 5550 | PrintCapability(Ops[0]); |
| 5551 | out << "\n"; |
| 5552 | break; |
| 5553 | } |
| 5554 | case spv::OpMemoryModel: { |
| 5555 | // Ops[0] = Addressing Model |
| 5556 | // Ops[1] = Memory Model |
| 5557 | PrintOpcode(Inst); |
| 5558 | out << " "; |
| 5559 | PrintAddrModel(Ops[0]); |
| 5560 | out << " "; |
| 5561 | PrintMemModel(Ops[1]); |
| 5562 | out << "\n"; |
| 5563 | break; |
| 5564 | } |
| 5565 | case spv::OpEntryPoint: { |
| 5566 | // Ops[0] = Execution Model |
| 5567 | // Ops[1] = EntryPoint ID |
| 5568 | // Ops[2] = Name (Literal String) |
| 5569 | // Ops[3] ... Ops[n] = Interface ID |
| 5570 | PrintOpcode(Inst); |
| 5571 | out << " "; |
| 5572 | PrintExecModel(Ops[0]); |
| 5573 | for (uint32_t i = 1; i < Ops.size(); i++) { |
| 5574 | out << " "; |
| 5575 | PrintOperand(Ops[i]); |
| 5576 | } |
| 5577 | out << "\n"; |
| 5578 | break; |
| 5579 | } |
| 5580 | case spv::OpExecutionMode: { |
| 5581 | // Ops[0] = Entry Point ID |
| 5582 | // Ops[1] = Execution Mode |
| 5583 | // Ops[2] ... Ops[n] = Optional literals according to Execution Mode |
| 5584 | PrintOpcode(Inst); |
| 5585 | out << " "; |
| 5586 | PrintOperand(Ops[0]); |
| 5587 | out << " "; |
| 5588 | PrintExecMode(Ops[1]); |
| 5589 | for (uint32_t i = 2; i < Ops.size(); i++) { |
| 5590 | out << " "; |
| 5591 | PrintOperand(Ops[i]); |
| 5592 | } |
| 5593 | out << "\n"; |
| 5594 | break; |
| 5595 | } |
| 5596 | case spv::OpSource: { |
| 5597 | // Ops[0] = SourceLanguage ID |
| 5598 | // Ops[1] = Version (LiteralNum) |
| 5599 | PrintOpcode(Inst); |
| 5600 | out << " "; |
| 5601 | PrintSourceLanguage(Ops[0]); |
| 5602 | out << " "; |
| 5603 | PrintOperand(Ops[1]); |
| 5604 | out << "\n"; |
| 5605 | break; |
| 5606 | } |
| 5607 | case spv::OpDecorate: { |
| 5608 | // Ops[0] = Target ID |
| 5609 | // Ops[1] = Decoration (Block or BufferBlock) |
| 5610 | // Ops[2] ... Ops[n] = Optional literals according to Decoration |
| 5611 | PrintOpcode(Inst); |
| 5612 | out << " "; |
| 5613 | PrintOperand(Ops[0]); |
| 5614 | out << " "; |
| 5615 | PrintDecoration(Ops[1]); |
| 5616 | // Handle BuiltIn OpDecorate specially. |
| 5617 | if (Ops[1]->getNumID() == spv::DecorationBuiltIn) { |
| 5618 | out << " "; |
| 5619 | PrintBuiltIn(Ops[2]); |
| 5620 | } else { |
| 5621 | for (uint32_t i = 2; i < Ops.size(); i++) { |
| 5622 | out << " "; |
| 5623 | PrintOperand(Ops[i]); |
| 5624 | } |
| 5625 | } |
| 5626 | out << "\n"; |
| 5627 | break; |
| 5628 | } |
| 5629 | case spv::OpMemberDecorate: { |
| 5630 | // Ops[0] = Structure Type ID |
| 5631 | // Ops[1] = Member Index(Literal Number) |
| 5632 | // Ops[2] = Decoration |
| 5633 | // Ops[3] ... Ops[n] = Optional literals according to Decoration |
| 5634 | PrintOpcode(Inst); |
| 5635 | out << " "; |
| 5636 | PrintOperand(Ops[0]); |
| 5637 | out << " "; |
| 5638 | PrintOperand(Ops[1]); |
| 5639 | out << " "; |
| 5640 | PrintDecoration(Ops[2]); |
| 5641 | for (uint32_t i = 3; i < Ops.size(); i++) { |
| 5642 | out << " "; |
| 5643 | PrintOperand(Ops[i]); |
| 5644 | } |
| 5645 | out << "\n"; |
| 5646 | break; |
| 5647 | } |
| 5648 | case spv::OpTypePointer: { |
| 5649 | // Ops[0] = Storage Class |
| 5650 | // Ops[1] = Element Type ID |
| 5651 | PrintResID(Inst); |
| 5652 | out << " = "; |
| 5653 | PrintOpcode(Inst); |
| 5654 | out << " "; |
| 5655 | PrintStorageClass(Ops[0]); |
| 5656 | out << " "; |
| 5657 | PrintOperand(Ops[1]); |
| 5658 | out << "\n"; |
| 5659 | break; |
| 5660 | } |
| 5661 | case spv::OpTypeImage: { |
| 5662 | // Ops[0] = Sampled Type ID |
| 5663 | // Ops[1] = Dim ID |
| 5664 | // Ops[2] = Depth (Literal Number) |
| 5665 | // Ops[3] = Arrayed (Literal Number) |
| 5666 | // Ops[4] = MS (Literal Number) |
| 5667 | // Ops[5] = Sampled (Literal Number) |
| 5668 | // Ops[6] = Image Format ID |
| 5669 | PrintResID(Inst); |
| 5670 | out << " = "; |
| 5671 | PrintOpcode(Inst); |
| 5672 | out << " "; |
| 5673 | PrintOperand(Ops[0]); |
| 5674 | out << " "; |
| 5675 | PrintDimensionality(Ops[1]); |
| 5676 | out << " "; |
| 5677 | PrintOperand(Ops[2]); |
| 5678 | out << " "; |
| 5679 | PrintOperand(Ops[3]); |
| 5680 | out << " "; |
| 5681 | PrintOperand(Ops[4]); |
| 5682 | out << " "; |
| 5683 | PrintOperand(Ops[5]); |
| 5684 | out << " "; |
| 5685 | PrintImageFormat(Ops[6]); |
| 5686 | out << "\n"; |
| 5687 | break; |
| 5688 | } |
| 5689 | case spv::OpFunction: { |
| 5690 | // Ops[0] : Result Type ID |
| 5691 | // Ops[1] : Function Control |
| 5692 | // Ops[2] : Function Type ID |
| 5693 | PrintResID(Inst); |
| 5694 | out << " = "; |
| 5695 | PrintOpcode(Inst); |
| 5696 | out << " "; |
| 5697 | PrintOperand(Ops[0]); |
| 5698 | out << " "; |
| 5699 | PrintFuncCtrl(Ops[1]); |
| 5700 | out << " "; |
| 5701 | PrintOperand(Ops[2]); |
| 5702 | out << "\n"; |
| 5703 | break; |
| 5704 | } |
| 5705 | case spv::OpSelectionMerge: { |
| 5706 | // Ops[0] = Merge Block ID |
| 5707 | // Ops[1] = Selection Control |
| 5708 | PrintOpcode(Inst); |
| 5709 | out << " "; |
| 5710 | PrintOperand(Ops[0]); |
| 5711 | out << " "; |
| 5712 | PrintSelectionControl(Ops[1]); |
| 5713 | out << "\n"; |
| 5714 | break; |
| 5715 | } |
| 5716 | case spv::OpLoopMerge: { |
| 5717 | // Ops[0] = Merge Block ID |
| 5718 | // Ops[1] = Continue Target ID |
| 5719 | // Ops[2] = Selection Control |
| 5720 | PrintOpcode(Inst); |
| 5721 | out << " "; |
| 5722 | PrintOperand(Ops[0]); |
| 5723 | out << " "; |
| 5724 | PrintOperand(Ops[1]); |
| 5725 | out << " "; |
| 5726 | PrintLoopControl(Ops[2]); |
| 5727 | out << "\n"; |
| 5728 | break; |
| 5729 | } |
| 5730 | case spv::OpImageSampleExplicitLod: { |
| 5731 | // Ops[0] = Result Type ID |
| 5732 | // Ops[1] = Sampled Image ID |
| 5733 | // Ops[2] = Coordinate ID |
| 5734 | // Ops[3] = Image Operands Type ID |
| 5735 | // Ops[4] ... Ops[n] = Operands ID |
| 5736 | PrintResID(Inst); |
| 5737 | out << " = "; |
| 5738 | PrintOpcode(Inst); |
| 5739 | for (uint32_t i = 0; i < 3; i++) { |
| 5740 | out << " "; |
| 5741 | PrintOperand(Ops[i]); |
| 5742 | } |
| 5743 | out << " "; |
| 5744 | PrintImageOperandsType(Ops[3]); |
| 5745 | for (uint32_t i = 4; i < Ops.size(); i++) { |
| 5746 | out << " "; |
| 5747 | PrintOperand(Ops[i]); |
| 5748 | } |
| 5749 | out << "\n"; |
| 5750 | break; |
| 5751 | } |
| 5752 | case spv::OpVariable: { |
| 5753 | // Ops[0] : Result Type ID |
| 5754 | // Ops[1] : Storage Class |
| 5755 | // Ops[2] ... Ops[n] = Initializer IDs |
| 5756 | PrintResID(Inst); |
| 5757 | out << " = "; |
| 5758 | PrintOpcode(Inst); |
| 5759 | out << " "; |
| 5760 | PrintOperand(Ops[0]); |
| 5761 | out << " "; |
| 5762 | PrintStorageClass(Ops[1]); |
| 5763 | for (uint32_t i = 2; i < Ops.size(); i++) { |
| 5764 | out << " "; |
| 5765 | PrintOperand(Ops[i]); |
| 5766 | } |
| 5767 | out << "\n"; |
| 5768 | break; |
| 5769 | } |
| 5770 | case spv::OpExtInst: { |
| 5771 | // Ops[0] = Result Type ID |
| 5772 | // Ops[1] = Set ID (OpExtInstImport ID) |
| 5773 | // Ops[2] = Instruction Number (Literal Number) |
| 5774 | // Ops[3] ... Ops[n] = Operand 1, ... , Operand n |
| 5775 | PrintResID(Inst); |
| 5776 | out << " = "; |
| 5777 | PrintOpcode(Inst); |
| 5778 | out << " "; |
| 5779 | PrintOperand(Ops[0]); |
| 5780 | out << " "; |
| 5781 | PrintOperand(Ops[1]); |
| 5782 | out << " "; |
| 5783 | PrintExtInst(Ops[2]); |
| 5784 | for (uint32_t i = 3; i < Ops.size(); i++) { |
| 5785 | out << " "; |
| 5786 | PrintOperand(Ops[i]); |
| 5787 | } |
| 5788 | out << "\n"; |
| 5789 | break; |
| 5790 | } |
| 5791 | case spv::OpCopyMemory: { |
| 5792 | // Ops[0] = Addressing Model |
| 5793 | // Ops[1] = Memory Model |
| 5794 | PrintOpcode(Inst); |
| 5795 | out << " "; |
| 5796 | PrintOperand(Ops[0]); |
| 5797 | out << " "; |
| 5798 | PrintOperand(Ops[1]); |
| 5799 | out << " "; |
| 5800 | PrintMemoryAccess(Ops[2]); |
| 5801 | out << " "; |
| 5802 | PrintOperand(Ops[3]); |
| 5803 | out << "\n"; |
| 5804 | break; |
| 5805 | } |
| 5806 | case spv::OpExtension: |
| 5807 | case spv::OpControlBarrier: |
| 5808 | case spv::OpMemoryBarrier: |
| 5809 | case spv::OpBranch: |
| 5810 | case spv::OpBranchConditional: |
| 5811 | case spv::OpStore: |
| 5812 | case spv::OpImageWrite: |
| 5813 | case spv::OpReturnValue: |
| 5814 | case spv::OpReturn: |
| 5815 | case spv::OpFunctionEnd: { |
| 5816 | PrintOpcode(Inst); |
| 5817 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5818 | out << " "; |
| 5819 | PrintOperand(Ops[i]); |
| 5820 | } |
| 5821 | out << "\n"; |
| 5822 | break; |
| 5823 | } |
| 5824 | case spv::OpExtInstImport: |
| 5825 | case spv::OpTypeRuntimeArray: |
| 5826 | case spv::OpTypeStruct: |
| 5827 | case spv::OpTypeSampler: |
| 5828 | case spv::OpTypeSampledImage: |
| 5829 | case spv::OpTypeInt: |
| 5830 | case spv::OpTypeFloat: |
| 5831 | case spv::OpTypeArray: |
| 5832 | case spv::OpTypeVector: |
| 5833 | case spv::OpTypeBool: |
| 5834 | case spv::OpTypeVoid: |
| 5835 | case spv::OpTypeFunction: |
| 5836 | case spv::OpFunctionParameter: |
| 5837 | case spv::OpLabel: |
| 5838 | case spv::OpPhi: |
| 5839 | case spv::OpLoad: |
| 5840 | case spv::OpSelect: |
| 5841 | case spv::OpAccessChain: |
| 5842 | case spv::OpPtrAccessChain: |
| 5843 | case spv::OpInBoundsAccessChain: |
| 5844 | case spv::OpUConvert: |
| 5845 | case spv::OpSConvert: |
| 5846 | case spv::OpConvertFToU: |
| 5847 | case spv::OpConvertFToS: |
| 5848 | case spv::OpConvertUToF: |
| 5849 | case spv::OpConvertSToF: |
| 5850 | case spv::OpFConvert: |
| 5851 | case spv::OpConvertPtrToU: |
| 5852 | case spv::OpConvertUToPtr: |
| 5853 | case spv::OpBitcast: |
| 5854 | case spv::OpIAdd: |
| 5855 | case spv::OpFAdd: |
| 5856 | case spv::OpISub: |
| 5857 | case spv::OpFSub: |
| 5858 | case spv::OpIMul: |
| 5859 | case spv::OpFMul: |
| 5860 | case spv::OpUDiv: |
| 5861 | case spv::OpSDiv: |
| 5862 | case spv::OpFDiv: |
| 5863 | case spv::OpUMod: |
| 5864 | case spv::OpSRem: |
| 5865 | case spv::OpFRem: |
| 5866 | case spv::OpBitwiseOr: |
| 5867 | case spv::OpBitwiseXor: |
| 5868 | case spv::OpBitwiseAnd: |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 5869 | case spv::OpNot: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5870 | case spv::OpShiftLeftLogical: |
| 5871 | case spv::OpShiftRightLogical: |
| 5872 | case spv::OpShiftRightArithmetic: |
| 5873 | case spv::OpBitCount: |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5874 | case spv::OpCompositeConstruct: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5875 | case spv::OpCompositeExtract: |
| 5876 | case spv::OpVectorExtractDynamic: |
| 5877 | case spv::OpCompositeInsert: |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 5878 | case spv::OpCopyObject: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5879 | case spv::OpVectorInsertDynamic: |
| 5880 | case spv::OpVectorShuffle: |
| 5881 | case spv::OpIEqual: |
| 5882 | case spv::OpINotEqual: |
| 5883 | case spv::OpUGreaterThan: |
| 5884 | case spv::OpUGreaterThanEqual: |
| 5885 | case spv::OpULessThan: |
| 5886 | case spv::OpULessThanEqual: |
| 5887 | case spv::OpSGreaterThan: |
| 5888 | case spv::OpSGreaterThanEqual: |
| 5889 | case spv::OpSLessThan: |
| 5890 | case spv::OpSLessThanEqual: |
| 5891 | case spv::OpFOrdEqual: |
| 5892 | case spv::OpFOrdGreaterThan: |
| 5893 | case spv::OpFOrdGreaterThanEqual: |
| 5894 | case spv::OpFOrdLessThan: |
| 5895 | case spv::OpFOrdLessThanEqual: |
| 5896 | case spv::OpFOrdNotEqual: |
| 5897 | case spv::OpFUnordEqual: |
| 5898 | case spv::OpFUnordGreaterThan: |
| 5899 | case spv::OpFUnordGreaterThanEqual: |
| 5900 | case spv::OpFUnordLessThan: |
| 5901 | case spv::OpFUnordLessThanEqual: |
| 5902 | case spv::OpFUnordNotEqual: |
| 5903 | case spv::OpSampledImage: |
| 5904 | case spv::OpFunctionCall: |
| 5905 | case spv::OpConstantTrue: |
| 5906 | case spv::OpConstantFalse: |
| 5907 | case spv::OpConstant: |
| 5908 | case spv::OpSpecConstant: |
| 5909 | case spv::OpConstantComposite: |
| 5910 | case spv::OpSpecConstantComposite: |
| 5911 | case spv::OpConstantNull: |
| 5912 | case spv::OpLogicalOr: |
| 5913 | case spv::OpLogicalAnd: |
| 5914 | case spv::OpLogicalNot: |
| 5915 | case spv::OpLogicalNotEqual: |
| 5916 | case spv::OpUndef: |
| 5917 | case spv::OpIsInf: |
| 5918 | case spv::OpIsNan: |
| 5919 | case spv::OpAny: |
| 5920 | case spv::OpAll: |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5921 | case spv::OpImageQuerySize: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5922 | case spv::OpAtomicIAdd: |
| 5923 | case spv::OpAtomicISub: |
| 5924 | case spv::OpAtomicExchange: |
| 5925 | case spv::OpAtomicIIncrement: |
| 5926 | case spv::OpAtomicIDecrement: |
| 5927 | case spv::OpAtomicCompareExchange: |
| 5928 | case spv::OpAtomicUMin: |
| 5929 | case spv::OpAtomicSMin: |
| 5930 | case spv::OpAtomicUMax: |
| 5931 | case spv::OpAtomicSMax: |
| 5932 | case spv::OpAtomicAnd: |
| 5933 | case spv::OpAtomicOr: |
| 5934 | case spv::OpAtomicXor: |
| 5935 | case spv::OpDot: { |
| 5936 | PrintResID(Inst); |
| 5937 | out << " = "; |
| 5938 | PrintOpcode(Inst); |
| 5939 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5940 | out << " "; |
| 5941 | PrintOperand(Ops[i]); |
| 5942 | } |
| 5943 | out << "\n"; |
| 5944 | break; |
| 5945 | } |
| 5946 | } |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | void SPIRVProducerPass::WriteOneWord(uint32_t Word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 5951 | binaryOut->write(reinterpret_cast<const char *>(&Word), sizeof(uint32_t)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | void SPIRVProducerPass::WriteResultID(SPIRVInstruction *Inst) { |
| 5955 | WriteOneWord(Inst->getResultID()); |
| 5956 | } |
| 5957 | |
| 5958 | void SPIRVProducerPass::WriteWordCountAndOpcode(SPIRVInstruction *Inst) { |
| 5959 | // High 16 bit : Word Count |
| 5960 | // Low 16 bit : Opcode |
| 5961 | uint32_t Word = Inst->getOpcode(); |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 5962 | const uint32_t count = Inst->getWordCount(); |
| 5963 | if (count > 65535) { |
| 5964 | errs() << "Word count limit of 65535 exceeded: " << count << "\n"; |
| 5965 | llvm_unreachable("Word count too high"); |
| 5966 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5967 | Word |= Inst->getWordCount() << 16; |
| 5968 | WriteOneWord(Word); |
| 5969 | } |
| 5970 | |
| 5971 | void SPIRVProducerPass::WriteOperand(SPIRVOperand *Op) { |
| 5972 | SPIRVOperandType OpTy = Op->getType(); |
| 5973 | switch (OpTy) { |
| 5974 | default: { |
| 5975 | llvm_unreachable("Unsupported SPIRV Operand Type???"); |
| 5976 | break; |
| 5977 | } |
| 5978 | case SPIRVOperandType::NUMBERID: { |
| 5979 | WriteOneWord(Op->getNumID()); |
| 5980 | break; |
| 5981 | } |
| 5982 | case SPIRVOperandType::LITERAL_STRING: { |
| 5983 | std::string Str = Op->getLiteralStr(); |
| 5984 | const char *Data = Str.c_str(); |
| 5985 | size_t WordSize = Str.size() / 4; |
| 5986 | for (unsigned Idx = 0; Idx < WordSize; Idx++) { |
| 5987 | WriteOneWord(*reinterpret_cast<const uint32_t *>(&Data[4 * Idx])); |
| 5988 | } |
| 5989 | |
| 5990 | uint32_t Remainder = Str.size() % 4; |
| 5991 | uint32_t LastWord = 0; |
| 5992 | if (Remainder) { |
| 5993 | for (unsigned Idx = 0; Idx < Remainder; Idx++) { |
| 5994 | LastWord |= Data[4 * WordSize + Idx] << 8 * Idx; |
| 5995 | } |
| 5996 | } |
| 5997 | |
| 5998 | WriteOneWord(LastWord); |
| 5999 | break; |
| 6000 | } |
| 6001 | case SPIRVOperandType::LITERAL_INTEGER: |
| 6002 | case SPIRVOperandType::LITERAL_FLOAT: { |
| 6003 | auto LiteralNum = Op->getLiteralNum(); |
| 6004 | // TODO: Handle LiteranNum carefully. |
| 6005 | for (auto Word : LiteralNum) { |
| 6006 | WriteOneWord(Word); |
| 6007 | } |
| 6008 | break; |
| 6009 | } |
| 6010 | } |
| 6011 | } |
| 6012 | |
| 6013 | void SPIRVProducerPass::WriteSPIRVBinary() { |
| 6014 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(); |
| 6015 | |
| 6016 | for (auto Inst : SPIRVInstList) { |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 6017 | SPIRVOperandList Ops{Inst->getOperands()}; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6018 | spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode()); |
| 6019 | |
| 6020 | switch (Opcode) { |
| 6021 | default: { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 6022 | errs() << "Unsupported SPIR-V instruction opcode " << int(Opcode) << "\n"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6023 | llvm_unreachable("Unsupported SPIRV instruction"); |
| 6024 | break; |
| 6025 | } |
| 6026 | case spv::OpCapability: |
| 6027 | case spv::OpExtension: |
| 6028 | case spv::OpMemoryModel: |
| 6029 | case spv::OpEntryPoint: |
| 6030 | case spv::OpExecutionMode: |
| 6031 | case spv::OpSource: |
| 6032 | case spv::OpDecorate: |
| 6033 | case spv::OpMemberDecorate: |
| 6034 | case spv::OpBranch: |
| 6035 | case spv::OpBranchConditional: |
| 6036 | case spv::OpSelectionMerge: |
| 6037 | case spv::OpLoopMerge: |
| 6038 | case spv::OpStore: |
| 6039 | case spv::OpImageWrite: |
| 6040 | case spv::OpReturnValue: |
| 6041 | case spv::OpControlBarrier: |
| 6042 | case spv::OpMemoryBarrier: |
| 6043 | case spv::OpReturn: |
| 6044 | case spv::OpFunctionEnd: |
| 6045 | case spv::OpCopyMemory: { |
| 6046 | WriteWordCountAndOpcode(Inst); |
| 6047 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 6048 | WriteOperand(Ops[i]); |
| 6049 | } |
| 6050 | break; |
| 6051 | } |
| 6052 | case spv::OpTypeBool: |
| 6053 | case spv::OpTypeVoid: |
| 6054 | case spv::OpTypeSampler: |
| 6055 | case spv::OpLabel: |
| 6056 | case spv::OpExtInstImport: |
| 6057 | case spv::OpTypePointer: |
| 6058 | case spv::OpTypeRuntimeArray: |
| 6059 | case spv::OpTypeStruct: |
| 6060 | case spv::OpTypeImage: |
| 6061 | case spv::OpTypeSampledImage: |
| 6062 | case spv::OpTypeInt: |
| 6063 | case spv::OpTypeFloat: |
| 6064 | case spv::OpTypeArray: |
| 6065 | case spv::OpTypeVector: |
| 6066 | case spv::OpTypeFunction: { |
| 6067 | WriteWordCountAndOpcode(Inst); |
| 6068 | WriteResultID(Inst); |
| 6069 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 6070 | WriteOperand(Ops[i]); |
| 6071 | } |
| 6072 | break; |
| 6073 | } |
| 6074 | case spv::OpFunction: |
| 6075 | case spv::OpFunctionParameter: |
| 6076 | case spv::OpAccessChain: |
| 6077 | case spv::OpPtrAccessChain: |
| 6078 | case spv::OpInBoundsAccessChain: |
| 6079 | case spv::OpUConvert: |
| 6080 | case spv::OpSConvert: |
| 6081 | case spv::OpConvertFToU: |
| 6082 | case spv::OpConvertFToS: |
| 6083 | case spv::OpConvertUToF: |
| 6084 | case spv::OpConvertSToF: |
| 6085 | case spv::OpFConvert: |
| 6086 | case spv::OpConvertPtrToU: |
| 6087 | case spv::OpConvertUToPtr: |
| 6088 | case spv::OpBitcast: |
| 6089 | case spv::OpIAdd: |
| 6090 | case spv::OpFAdd: |
| 6091 | case spv::OpISub: |
| 6092 | case spv::OpFSub: |
| 6093 | case spv::OpIMul: |
| 6094 | case spv::OpFMul: |
| 6095 | case spv::OpUDiv: |
| 6096 | case spv::OpSDiv: |
| 6097 | case spv::OpFDiv: |
| 6098 | case spv::OpUMod: |
| 6099 | case spv::OpSRem: |
| 6100 | case spv::OpFRem: |
| 6101 | case spv::OpBitwiseOr: |
| 6102 | case spv::OpBitwiseXor: |
| 6103 | case spv::OpBitwiseAnd: |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 6104 | case spv::OpNot: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6105 | case spv::OpShiftLeftLogical: |
| 6106 | case spv::OpShiftRightLogical: |
| 6107 | case spv::OpShiftRightArithmetic: |
| 6108 | case spv::OpBitCount: |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 6109 | case spv::OpCompositeConstruct: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6110 | case spv::OpCompositeExtract: |
| 6111 | case spv::OpVectorExtractDynamic: |
| 6112 | case spv::OpCompositeInsert: |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 6113 | case spv::OpCopyObject: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6114 | case spv::OpVectorInsertDynamic: |
| 6115 | case spv::OpVectorShuffle: |
| 6116 | case spv::OpIEqual: |
| 6117 | case spv::OpINotEqual: |
| 6118 | case spv::OpUGreaterThan: |
| 6119 | case spv::OpUGreaterThanEqual: |
| 6120 | case spv::OpULessThan: |
| 6121 | case spv::OpULessThanEqual: |
| 6122 | case spv::OpSGreaterThan: |
| 6123 | case spv::OpSGreaterThanEqual: |
| 6124 | case spv::OpSLessThan: |
| 6125 | case spv::OpSLessThanEqual: |
| 6126 | case spv::OpFOrdEqual: |
| 6127 | case spv::OpFOrdGreaterThan: |
| 6128 | case spv::OpFOrdGreaterThanEqual: |
| 6129 | case spv::OpFOrdLessThan: |
| 6130 | case spv::OpFOrdLessThanEqual: |
| 6131 | case spv::OpFOrdNotEqual: |
| 6132 | case spv::OpFUnordEqual: |
| 6133 | case spv::OpFUnordGreaterThan: |
| 6134 | case spv::OpFUnordGreaterThanEqual: |
| 6135 | case spv::OpFUnordLessThan: |
| 6136 | case spv::OpFUnordLessThanEqual: |
| 6137 | case spv::OpFUnordNotEqual: |
| 6138 | case spv::OpExtInst: |
| 6139 | case spv::OpIsInf: |
| 6140 | case spv::OpIsNan: |
| 6141 | case spv::OpAny: |
| 6142 | case spv::OpAll: |
| 6143 | case spv::OpUndef: |
| 6144 | case spv::OpConstantNull: |
| 6145 | case spv::OpLogicalOr: |
| 6146 | case spv::OpLogicalAnd: |
| 6147 | case spv::OpLogicalNot: |
| 6148 | case spv::OpLogicalNotEqual: |
| 6149 | case spv::OpConstantComposite: |
| 6150 | case spv::OpSpecConstantComposite: |
| 6151 | case spv::OpConstantTrue: |
| 6152 | case spv::OpConstantFalse: |
| 6153 | case spv::OpConstant: |
| 6154 | case spv::OpSpecConstant: |
| 6155 | case spv::OpVariable: |
| 6156 | case spv::OpFunctionCall: |
| 6157 | case spv::OpSampledImage: |
| 6158 | case spv::OpImageSampleExplicitLod: |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 6159 | case spv::OpImageQuerySize: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6160 | case spv::OpSelect: |
| 6161 | case spv::OpPhi: |
| 6162 | case spv::OpLoad: |
| 6163 | case spv::OpAtomicIAdd: |
| 6164 | case spv::OpAtomicISub: |
| 6165 | case spv::OpAtomicExchange: |
| 6166 | case spv::OpAtomicIIncrement: |
| 6167 | case spv::OpAtomicIDecrement: |
| 6168 | case spv::OpAtomicCompareExchange: |
| 6169 | case spv::OpAtomicUMin: |
| 6170 | case spv::OpAtomicSMin: |
| 6171 | case spv::OpAtomicUMax: |
| 6172 | case spv::OpAtomicSMax: |
| 6173 | case spv::OpAtomicAnd: |
| 6174 | case spv::OpAtomicOr: |
| 6175 | case spv::OpAtomicXor: |
| 6176 | case spv::OpDot: { |
| 6177 | WriteWordCountAndOpcode(Inst); |
| 6178 | WriteOperand(Ops[0]); |
| 6179 | WriteResultID(Inst); |
| 6180 | for (uint32_t i = 1; i < Ops.size(); i++) { |
| 6181 | WriteOperand(Ops[i]); |
| 6182 | } |
| 6183 | break; |
| 6184 | } |
| 6185 | } |
| 6186 | } |
| 6187 | } |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 6188 | |
| 6189 | bool SPIRVProducerPass::IsTypeNullable(const Type* type) const { |
| 6190 | switch (type->getTypeID()) { |
| 6191 | case Type::HalfTyID: |
| 6192 | case Type::FloatTyID: |
| 6193 | case Type::DoubleTyID: |
| 6194 | case Type::IntegerTyID: |
| 6195 | case Type::VectorTyID: |
| 6196 | return true; |
| 6197 | case Type::PointerTyID: { |
| 6198 | const PointerType *pointer_type = cast<PointerType>(type); |
| 6199 | if (pointer_type->getPointerAddressSpace() != |
| 6200 | AddressSpace::UniformConstant) { |
| 6201 | auto pointee_type = pointer_type->getPointerElementType(); |
| 6202 | if (pointee_type->isStructTy() && |
| 6203 | cast<StructType>(pointee_type)->isOpaque()) { |
| 6204 | // Images and samplers are not nullable. |
| 6205 | return false; |
| 6206 | } |
| 6207 | } |
| 6208 | return true; |
| 6209 | } |
| 6210 | case Type::ArrayTyID: |
| 6211 | return IsTypeNullable(cast<CompositeType>(type)->getTypeAtIndex(0u)); |
| 6212 | case Type::StructTyID: { |
| 6213 | const StructType* struct_type = cast<StructType>(type); |
| 6214 | // Images and samplers are not nullable. |
| 6215 | if (struct_type->isOpaque()) return false; |
| 6216 | for (const auto element : struct_type->elements()) { |
| 6217 | if (!IsTypeNullable(element)) return false; |
| 6218 | } |
| 6219 | return true; |
| 6220 | } |
| 6221 | default: |
| 6222 | return false; |
| 6223 | } |
| 6224 | } |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 6225 | |
| 6226 | void SPIRVProducerPass::PopulateUBOTypeMaps(Module &module) { |
| 6227 | if (auto *offsets_md = |
| 6228 | module.getNamedMetadata(clspv::RemappedTypeOffsetMetadataName())) { |
| 6229 | // Metdata is stored as key-value pair operands. The first element of each |
| 6230 | // operand is the type and the second is a vector of offsets. |
| 6231 | for (const auto *operand : offsets_md->operands()) { |
| 6232 | const auto *pair = cast<MDTuple>(operand); |
| 6233 | auto *type = |
| 6234 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 6235 | const auto *offset_vector = cast<MDTuple>(pair->getOperand(1)); |
| 6236 | std::vector<uint32_t> offsets; |
| 6237 | for (const Metadata *offset_md : offset_vector->operands()) { |
| 6238 | const auto *constant_md = cast<ConstantAsMetadata>(offset_md); |
| 6239 | offsets.push_back( |
| 6240 | cast<ConstantInt>(constant_md->getValue())->getZExtValue()); |
| 6241 | } |
| 6242 | RemappedUBOTypeOffsets.insert(std::make_pair(type, offsets)); |
| 6243 | } |
| 6244 | } |
| 6245 | |
| 6246 | if (auto *sizes_md = |
| 6247 | module.getNamedMetadata(clspv::RemappedTypeSizesMetadataName())) { |
| 6248 | // Metadata is stored as key-value pair operands. The first element of each |
| 6249 | // operand is the type and the second is a triple of sizes: type size in |
| 6250 | // bits, store size and alloc size. |
| 6251 | for (const auto *operand : sizes_md->operands()) { |
| 6252 | const auto *pair = cast<MDTuple>(operand); |
| 6253 | auto *type = |
| 6254 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 6255 | const auto *size_triple = cast<MDTuple>(pair->getOperand(1)); |
| 6256 | uint64_t type_size_in_bits = |
| 6257 | cast<ConstantInt>( |
| 6258 | cast<ConstantAsMetadata>(size_triple->getOperand(0))->getValue()) |
| 6259 | ->getZExtValue(); |
| 6260 | uint64_t type_store_size = |
| 6261 | cast<ConstantInt>( |
| 6262 | cast<ConstantAsMetadata>(size_triple->getOperand(1))->getValue()) |
| 6263 | ->getZExtValue(); |
| 6264 | uint64_t type_alloc_size = |
| 6265 | cast<ConstantInt>( |
| 6266 | cast<ConstantAsMetadata>(size_triple->getOperand(2))->getValue()) |
| 6267 | ->getZExtValue(); |
| 6268 | RemappedUBOTypeSizes.insert(std::make_pair( |
| 6269 | type, std::make_tuple(type_size_in_bits, type_store_size, |
| 6270 | type_alloc_size))); |
| 6271 | } |
| 6272 | } |
| 6273 | } |
| 6274 | |
| 6275 | uint64_t SPIRVProducerPass::GetTypeSizeInBits(Type *type, |
| 6276 | const DataLayout &DL) { |
| 6277 | auto iter = RemappedUBOTypeSizes.find(type); |
| 6278 | if (iter != RemappedUBOTypeSizes.end()) { |
| 6279 | return std::get<0>(iter->second); |
| 6280 | } |
| 6281 | |
| 6282 | return DL.getTypeSizeInBits(type); |
| 6283 | } |
| 6284 | |
| 6285 | uint64_t SPIRVProducerPass::GetTypeStoreSize(Type *type, const DataLayout &DL) { |
| 6286 | auto iter = RemappedUBOTypeSizes.find(type); |
| 6287 | if (iter != RemappedUBOTypeSizes.end()) { |
| 6288 | return std::get<1>(iter->second); |
| 6289 | } |
| 6290 | |
| 6291 | return DL.getTypeStoreSize(type); |
| 6292 | } |
| 6293 | |
| 6294 | uint64_t SPIRVProducerPass::GetTypeAllocSize(Type *type, const DataLayout &DL) { |
| 6295 | auto iter = RemappedUBOTypeSizes.find(type); |
| 6296 | if (iter != RemappedUBOTypeSizes.end()) { |
| 6297 | return std::get<2>(iter->second); |
| 6298 | } |
| 6299 | |
| 6300 | return DL.getTypeAllocSize(type); |
| 6301 | } |