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 | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 31 | #include "llvm/ADT/StringSwitch.h" |
| 32 | #include "llvm/ADT/UniqueVector.h" |
| 33 | #include "llvm/Analysis/LoopInfo.h" |
| 34 | #include "llvm/IR/Constants.h" |
| 35 | #include "llvm/IR/Dominators.h" |
| 36 | #include "llvm/IR/Instructions.h" |
| 37 | #include "llvm/IR/Metadata.h" |
| 38 | #include "llvm/IR/Module.h" |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 39 | #include "llvm/IR/ValueSymbolTable.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 40 | #include "llvm/Pass.h" |
| 41 | #include "llvm/Support/CommandLine.h" |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 42 | #include "llvm/Support/MathExtras.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
| 44 | #include "llvm/Transforms/Utils/Cloning.h" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 45 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 46 | // enable spv::HasResultAndType |
| 47 | #define SPV_ENABLE_UTILITY_CODE |
alan-baker | e090260 | 2020-03-23 08:43:40 -0400 | [diff] [blame] | 48 | #include "spirv/unified1/spirv.hpp" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 49 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 50 | #include "clspv/AddressSpace.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 51 | #include "clspv/DescriptorMap.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 52 | #include "clspv/Option.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 53 | #include "clspv/spirv_c_strings.hpp" |
| 54 | #include "clspv/spirv_glsl.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 55 | |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 56 | #include "ArgKind.h" |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 57 | #include "Builtins.h" |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 58 | #include "ComputeStructuredOrder.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 59 | #include "ConstantEmitter.h" |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 60 | #include "Constants.h" |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 61 | #include "DescriptorCounter.h" |
alan-baker | c4579bb | 2020-04-29 14:15:50 -0400 | [diff] [blame] | 62 | #include "Layout.h" |
alan-baker | 56f7aff | 2019-05-22 08:06:42 -0400 | [diff] [blame] | 63 | #include "NormalizeGlobalVariable.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 64 | #include "Passes.h" |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 65 | #include "SpecConstant.h" |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 66 | #include "Types.h" |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 67 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 68 | #if defined(_MSC_VER) |
| 69 | #pragma warning(pop) |
| 70 | #endif |
| 71 | |
| 72 | using namespace llvm; |
| 73 | using namespace clspv; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 74 | using namespace clspv::Builtins; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 75 | using namespace mdconst; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 76 | |
| 77 | namespace { |
David Neto | cd8ca5f | 2017-10-02 23:34:11 -0400 | [diff] [blame] | 78 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 79 | cl::opt<bool> ShowResourceVars("show-rv", cl::init(false), cl::Hidden, |
| 80 | cl::desc("Show resource variable creation")); |
| 81 | |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 82 | cl::opt<bool> |
| 83 | ShowProducerIR("show-producer-ir", cl::init(false), cl::ReallyHidden, |
| 84 | cl::desc("Dump the IR at the start of SPIRVProducer")); |
| 85 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 86 | // These hacks exist to help transition code generation algorithms |
| 87 | // without making huge noise in detailed test output. |
| 88 | const bool Hack_generate_runtime_array_stride_early = true; |
| 89 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 90 | // The value of 1/pi. This value is from MSDN |
| 91 | // https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx |
| 92 | const double kOneOverPi = 0.318309886183790671538; |
| 93 | const glsl::ExtInst kGlslExtInstBad = static_cast<glsl::ExtInst>(0); |
| 94 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 95 | const char *kCompositeConstructFunctionPrefix = "clspv.composite_construct."; |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 96 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 97 | // SPIRV Module Sections (per 2.4 of the SPIRV spec) |
| 98 | // These are used to collect SPIRVInstructions by type on-the-fly. |
| 99 | enum SPIRVSection { |
| 100 | kCapabilities, |
| 101 | kExtensions, |
| 102 | kImports, |
| 103 | kMemoryModel, |
| 104 | kEntryPoints, |
| 105 | kExecutionModes, |
| 106 | |
| 107 | kDebug, |
| 108 | kAnnotations, |
| 109 | |
| 110 | kTypes, |
| 111 | kConstants = kTypes, |
| 112 | kGlobalVariables, |
| 113 | |
| 114 | kFunctions, |
| 115 | |
| 116 | kSectionCount |
| 117 | }; |
| 118 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 119 | class SPIRVID { |
| 120 | uint32_t id; |
| 121 | |
| 122 | public: |
| 123 | SPIRVID(uint32_t _id = 0) : id(_id) {} |
| 124 | uint32_t get() const { return id; } |
| 125 | bool isValid() const { return id != 0; } |
| 126 | bool operator==(const SPIRVID &that) const { return id == that.id; } |
| 127 | }; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 128 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 129 | enum SPIRVOperandType { NUMBERID, LITERAL_WORD, LITERAL_DWORD, LITERAL_STRING }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 130 | |
| 131 | struct SPIRVOperand { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 132 | explicit SPIRVOperand(SPIRVOperandType Ty, uint32_t Num) : Type(Ty) { |
| 133 | LiteralNum[0] = Num; |
| 134 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 135 | explicit SPIRVOperand(SPIRVOperandType Ty, const char *Str) |
| 136 | : Type(Ty), LiteralStr(Str) {} |
| 137 | explicit SPIRVOperand(SPIRVOperandType Ty, StringRef Str) |
| 138 | : Type(Ty), LiteralStr(Str) {} |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 139 | explicit SPIRVOperand(ArrayRef<uint32_t> NumVec) { |
| 140 | auto sz = NumVec.size(); |
| 141 | assert(sz >= 1 && sz <= 2); |
| 142 | Type = sz == 1 ? LITERAL_WORD : LITERAL_DWORD; |
| 143 | LiteralNum[0] = NumVec[0]; |
| 144 | if (sz == 2) { |
| 145 | LiteralNum[1] = NumVec[1]; |
| 146 | } |
| 147 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 148 | |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 149 | SPIRVOperandType getType() const { return Type; }; |
| 150 | uint32_t getNumID() const { return LiteralNum[0]; }; |
| 151 | std::string getLiteralStr() const { return LiteralStr; }; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 152 | const uint32_t *getLiteralNum() const { return LiteralNum; }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 153 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 154 | uint32_t GetNumWords() const { |
| 155 | switch (Type) { |
| 156 | case NUMBERID: |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 157 | case LITERAL_WORD: |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 158 | return 1; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 159 | case LITERAL_DWORD: |
| 160 | return 2; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 161 | case LITERAL_STRING: |
| 162 | // Account for the terminating null character. |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 163 | return uint32_t((LiteralStr.size() + 4) / 4); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 164 | } |
| 165 | llvm_unreachable("Unhandled case in SPIRVOperand::GetNumWords()"); |
| 166 | } |
| 167 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 168 | private: |
| 169 | SPIRVOperandType Type; |
| 170 | std::string LiteralStr; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 171 | uint32_t LiteralNum[2]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 172 | }; |
| 173 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 174 | typedef SmallVector<SPIRVOperand, 4> SPIRVOperandVec; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 175 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 176 | struct SPIRVInstruction { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 177 | // Primary constructor must have Opcode, initializes WordCount based on ResID. |
| 178 | SPIRVInstruction(spv::Op Opc, SPIRVID ResID = 0) |
| 179 | : Opcode(static_cast<uint16_t>(Opc)) { |
| 180 | setResult(ResID); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 181 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 182 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 183 | // Creates an instruction with an opcode and no result ID, and with the given |
| 184 | // operands. This calls primary constructor to initialize Opcode, WordCount. |
| 185 | // Takes ownership of the operands and clears |Ops|. |
| 186 | SPIRVInstruction(spv::Op Opc, SPIRVOperandVec &Ops) : SPIRVInstruction(Opc) { |
| 187 | setOperands(Ops); |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 188 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 189 | // Creates an instruction with an opcode and no result ID, and with the given |
| 190 | // operands. This calls primary constructor to initialize Opcode, WordCount. |
| 191 | // Takes ownership of the operands and clears |Ops|. |
| 192 | SPIRVInstruction(spv::Op Opc, SPIRVID ResID, SPIRVOperandVec &Ops) |
| 193 | : SPIRVInstruction(Opc, ResID) { |
| 194 | setOperands(Ops); |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 195 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 196 | |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 197 | uint32_t getWordCount() const { return WordCount; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 198 | uint16_t getOpcode() const { return Opcode; } |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 199 | SPIRVID getResultID() const { return ResultID; } |
| 200 | const SPIRVOperandVec &getOperands() const { return Operands; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 201 | |
| 202 | private: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 203 | void setResult(SPIRVID ResID = 0) { |
| 204 | WordCount = 1 + (ResID.isValid() ? 1 : 0); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 205 | ResultID = ResID; |
| 206 | } |
| 207 | |
| 208 | void setOperands(SPIRVOperandVec &Ops) { |
| 209 | assert(Operands.empty()); |
| 210 | Operands = std::move(Ops); |
| 211 | for (auto &opd : Operands) { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 212 | WordCount += uint16_t(opd.GetNumWords()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | private: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 217 | uint32_t WordCount; // Check the 16-bit bound at code generation time. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 218 | uint16_t Opcode; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 219 | SPIRVID ResultID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 220 | SPIRVOperandVec Operands; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | struct SPIRVProducerPass final : public ModulePass { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 224 | typedef DenseMap<Type *, SPIRVID> TypeMapType; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 225 | typedef UniqueVector<Type *> TypeList; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 226 | typedef DenseMap<Value *, SPIRVID> ValueMapType; |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 227 | typedef UniqueVector<Value *> ValueList; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 228 | typedef std::vector<std::pair<Value *, SPIRVID>> EntryPointVecType; |
| 229 | typedef std::set<uint32_t> CapabilitySetType; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 230 | typedef std::list<SPIRVInstruction> SPIRVInstructionList; |
| 231 | // A vector of pairs, each of which is: |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 232 | // - the LLVM instruction that we will later generate SPIR-V code for |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 233 | // - the SPIR-V instruction placeholder that will be replaced |
| 234 | typedef std::vector<std::pair<Value *, SPIRVInstruction *>> |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 235 | DeferredInstVecType; |
| 236 | typedef DenseMap<FunctionType *, std::pair<FunctionType *, uint32_t>> |
| 237 | GlobalConstFuncMapType; |
| 238 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 239 | explicit SPIRVProducerPass( |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 240 | raw_pwrite_stream &out, |
| 241 | std::vector<clspv::version0::DescriptorMapEntry> *descriptor_map_entries, |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 242 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 243 | bool outputCInitList) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 244 | : ModulePass(ID), module(nullptr), samplerMap(samplerMap), out(out), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 245 | binaryTempOut(binaryTempUnderlyingVector), binaryOut(&out), |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 246 | descriptorMapEntries(descriptor_map_entries), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 247 | outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1), |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 248 | OpExtInstImportID(0), HasVariablePointersStorageBuffer(false), |
| 249 | HasVariablePointers(false), SamplerTy(nullptr), WorkgroupSizeValueID(0), |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 250 | WorkgroupSizeVarID(0) { |
| 251 | addCapability(spv::CapabilityShader); |
| 252 | Ptr = this; |
| 253 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 254 | |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 255 | virtual ~SPIRVProducerPass() { |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 256 | } |
| 257 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 258 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 259 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 260 | AU.addRequired<LoopInfoWrapperPass>(); |
| 261 | } |
| 262 | |
| 263 | virtual bool runOnModule(Module &module) override; |
| 264 | |
| 265 | // output the SPIR-V header block |
| 266 | void outputHeader(); |
| 267 | |
| 268 | // patch the SPIR-V header block |
| 269 | void patchHeader(); |
| 270 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 271 | CapabilitySetType &getCapabilitySet() { return CapabilitySet; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 272 | TypeMapType &getImageTypeMap() { return ImageTypeMap; } |
| 273 | TypeList &getTypeList() { return Types; }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 274 | ValueMapType &getValueMap() { return ValueMap; } |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 275 | SPIRVInstructionList &getSPIRVInstList(SPIRVSection Section) { |
| 276 | return SPIRVSections[Section]; |
| 277 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 278 | EntryPointVecType &getEntryPointVec() { return EntryPointVec; }; |
| 279 | DeferredInstVecType &getDeferredInstVec() { return DeferredInstVec; }; |
| 280 | ValueList &getEntryPointInterfacesVec() { return EntryPointInterfacesVec; }; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 281 | SPIRVID getOpExtInstImportID(); |
| 282 | std::vector<SPIRVID> &getBuiltinDimVec() { return BuiltinDimensionVec; }; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 283 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 284 | bool hasVariablePointersStorageBuffer() { |
| 285 | return HasVariablePointersStorageBuffer; |
| 286 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 287 | void setVariablePointersStorageBuffer() { |
| 288 | if (!HasVariablePointersStorageBuffer) { |
| 289 | addCapability(spv::CapabilityVariablePointersStorageBuffer); |
| 290 | HasVariablePointersStorageBuffer = true; |
| 291 | } |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 292 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 293 | bool hasVariablePointers() { return HasVariablePointers; }; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 294 | void setVariablePointers() { |
| 295 | if (!HasVariablePointers) { |
| 296 | addCapability(spv::CapabilityVariablePointers); |
| 297 | HasVariablePointers = true; |
| 298 | } |
| 299 | }; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 300 | ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() { |
| 301 | return samplerMap; |
| 302 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 303 | GlobalConstFuncMapType &getGlobalConstFuncTypeMap() { |
| 304 | return GlobalConstFuncTypeMap; |
| 305 | } |
| 306 | SmallPtrSet<Value *, 16> &getGlobalConstArgSet() { |
| 307 | return GlobalConstArgumentSet; |
| 308 | } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 309 | TypeList &getTypesNeedingArrayStride() { return TypesNeedingArrayStride; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 310 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 311 | void GenerateLLVMIRInfo(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 312 | // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will |
| 313 | // *not* be converted to a storage buffer, replace each such global variable |
| 314 | // with one in the storage class expecgted by SPIR-V. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 315 | void FindGlobalConstVars(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 316 | // Populate ResourceVarInfoList, FunctionToResourceVarsMap, and |
| 317 | // ModuleOrderedResourceVars. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 318 | void FindResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 319 | void FindTypePerGlobalVar(GlobalVariable &GV); |
| 320 | void FindTypePerFunc(Function &F); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 321 | void FindTypesForSamplerMap(); |
| 322 | void FindTypesForResourceVars(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 323 | // Inserts |Ty| and relevant sub-types into the |Types| member, indicating |
| 324 | // that |Ty| and its subtypes will need a corresponding SPIR-V type. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 325 | void FindType(Type *Ty); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 326 | |
| 327 | // Lookup or create Types, Constants. |
| 328 | // Returns SPIRVID once it has been created. |
| 329 | SPIRVID getSPIRVType(Type *Ty); |
| 330 | SPIRVID getSPIRVConstant(Constant *Cst); |
| 331 | // Lookup SPIRVID of llvm::Value, may create Constant. |
| 332 | SPIRVID getSPIRVValue(Value *V); |
| 333 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 334 | // Generates instructions for SPIR-V types corresponding to the LLVM types |
| 335 | // saved in the |Types| member. A type follows its subtypes. IDs are |
| 336 | // allocated sequentially starting with the current value of nextID, and |
| 337 | // with a type following its subtypes. Also updates nextID to just beyond |
| 338 | // the last generated ID. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 339 | void GenerateSPIRVTypes(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 340 | void GenerateModuleInfo(); |
| 341 | void GeneratePushConstantDescriptorMapEntries(); |
| 342 | void GenerateSpecConstantDescriptorMapEntries(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 343 | void GenerateGlobalVar(GlobalVariable &GV); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 344 | void GenerateWorkgroupVars(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 345 | // Generate descriptor map entries for resource variables associated with |
| 346 | // arguments to F. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 347 | void GenerateDescriptorMapInfo(Function &F); |
| 348 | void GenerateSamplers(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 349 | // Generate OpVariables for %clspv.resource.var.* calls. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 350 | void GenerateResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 351 | void GenerateFuncPrologue(Function &F); |
| 352 | void GenerateFuncBody(Function &F); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 353 | void GenerateEntryPointInitialStores(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 354 | spv::Op GetSPIRVCmpOpcode(CmpInst *CmpI); |
| 355 | spv::Op GetSPIRVCastOpcode(Instruction &I); |
| 356 | spv::Op GetSPIRVBinaryOpcode(Instruction &I); |
| 357 | void GenerateInstruction(Instruction &I); |
| 358 | void GenerateFuncEpilogue(); |
| 359 | void HandleDeferredInstruction(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 360 | void HandleDeferredDecorations(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 361 | bool is4xi8vec(Type *Ty) const; |
| 362 | spv::StorageClass GetStorageClass(unsigned AddrSpace) const; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 363 | spv::StorageClass GetStorageClassForArgKind(clspv::ArgKind arg_kind) const; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 364 | spv::BuiltIn GetBuiltin(StringRef globalVarName) const; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 365 | // Returns the GLSL extended instruction enum that the given function |
| 366 | // 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] | 367 | glsl::ExtInst getExtInstEnum(StringRef Name); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 368 | // Returns the GLSL extended instruction enum indirectly used by the given |
| 369 | // function. That is, to implement the given function, we use an extended |
| 370 | // instruction plus one more instruction. If none, then returns the 0 value, |
| 371 | // i.e. GLSLstd4580Bad. |
| 372 | glsl::ExtInst getIndirectExtInstEnum(StringRef Name); |
| 373 | // Returns the single GLSL extended instruction used directly or |
| 374 | // indirectly by the given function call. |
| 375 | glsl::ExtInst getDirectOrIndirectExtInstEnum(StringRef Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 376 | void WriteOneWord(uint32_t Word); |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 377 | void WriteResultID(const SPIRVInstruction &Inst); |
| 378 | void WriteWordCountAndOpcode(const SPIRVInstruction &Inst); |
| 379 | void WriteOperand(const SPIRVOperand &Op); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 380 | void WriteSPIRVBinary(); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 381 | void WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 382 | |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 383 | // Returns true if |type| is compatible with OpConstantNull. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 384 | bool IsTypeNullable(const Type *type) const; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 385 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 386 | // Populate UBO remapped type maps. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 387 | void PopulateUBOTypeMaps(); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 388 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 389 | // Populate the merge and continue block maps. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 390 | void PopulateStructuredCFGMaps(); |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 391 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 392 | // Wrapped methods of DataLayout accessors. If |type| was remapped for UBOs, |
| 393 | // uses the internal map, otherwise it falls back on the data layout. |
| 394 | uint64_t GetTypeSizeInBits(Type *type, const DataLayout &DL); |
| 395 | uint64_t GetTypeStoreSize(Type *type, const DataLayout &DL); |
| 396 | uint64_t GetTypeAllocSize(Type *type, const DataLayout &DL); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 397 | uint32_t GetExplicitLayoutStructMemberOffset(StructType *type, |
| 398 | unsigned member, |
| 399 | const DataLayout &DL); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 400 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 401 | // Returns the base pointer of |v|. |
| 402 | Value *GetBasePointer(Value *v); |
| 403 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 404 | // Add Capability if not already (e.g. CapabilityGroupNonUniformBroadcast) |
| 405 | void addCapability(uint32_t c) { CapabilitySet.emplace(c); } |
| 406 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 407 | // Sets |HasVariablePointersStorageBuffer| or |HasVariablePointers| base on |
| 408 | // |address_space|. |
| 409 | void setVariablePointersCapabilities(unsigned address_space); |
| 410 | |
| 411 | // Returns true if |lhs| and |rhs| represent the same resource or workgroup |
| 412 | // variable. |
| 413 | bool sameResource(Value *lhs, Value *rhs) const; |
| 414 | |
| 415 | // Returns true if |inst| is phi or select that selects from the same |
| 416 | // structure (or null). |
| 417 | bool selectFromSameObject(Instruction *inst); |
| 418 | |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 419 | // Returns true if |Arg| is called with a coherent resource. |
| 420 | bool CalledWithCoherentResource(Argument &Arg); |
| 421 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 422 | // |
| 423 | // Primary interface for adding SPIRVInstructions to a SPIRVSection. |
| 424 | template <enum SPIRVSection TSection = kFunctions> |
| 425 | SPIRVID addSPIRVInst(spv::Op Opcode, SPIRVOperandVec &Operands) { |
| 426 | bool has_result, has_result_type; |
| 427 | spv::HasResultAndType(Opcode, &has_result, &has_result_type); |
| 428 | SPIRVID RID = has_result ? incrNextID() : 0; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 429 | SPIRVSections[TSection].emplace_back(Opcode, RID, Operands); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 430 | return RID; |
| 431 | } |
| 432 | template <enum SPIRVSection TSection = kFunctions> |
| 433 | SPIRVID addSPIRVInst(spv::Op Op) { |
| 434 | SPIRVOperandVec Ops; |
| 435 | return addSPIRVInst<TSection>(Op, Ops); |
| 436 | } |
| 437 | template <enum SPIRVSection TSection = kFunctions> |
| 438 | SPIRVID addSPIRVInst(spv::Op Op, uint32_t V) { |
| 439 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 440 | Ops.emplace_back(LITERAL_WORD, V); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 441 | return addSPIRVInst<TSection>(Op, Ops); |
| 442 | } |
| 443 | template <enum SPIRVSection TSection = kFunctions> |
| 444 | SPIRVID addSPIRVInst(spv::Op Op, const char *V) { |
| 445 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 446 | Ops.emplace_back(LITERAL_STRING, V); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 447 | return addSPIRVInst<TSection>(Op, Ops); |
| 448 | } |
| 449 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 450 | // |
| 451 | // Add placeholder for llvm::Value that references future values. |
| 452 | // Must have result ID just in case final SPIRVInstruction requires. |
| 453 | SPIRVID addSPIRVPlaceholder(Value *I) { |
| 454 | SPIRVID RID = incrNextID(); |
| 455 | SPIRVOperandVec Ops; |
| 456 | SPIRVSections[kFunctions].emplace_back(spv::OpExtInst, RID, Ops); |
| 457 | DeferredInstVec.push_back({I, &SPIRVSections[kFunctions].back()}); |
| 458 | return RID; |
| 459 | } |
| 460 | // Replace placeholder with actual SPIRVInstruction on the final pass |
| 461 | // (HandleDeferredInstruction). |
| 462 | SPIRVID replaceSPIRVInst(SPIRVInstruction *I, spv::Op Opcode, |
| 463 | SPIRVOperandVec &Operands) { |
| 464 | bool has_result, has_result_type; |
| 465 | spv::HasResultAndType(Opcode, &has_result, &has_result_type); |
| 466 | SPIRVID RID = has_result ? I->getResultID() : 0; |
| 467 | *I = SPIRVInstruction(Opcode, RID, Operands); |
| 468 | return RID; |
| 469 | } |
| 470 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 471 | private: |
| 472 | static char ID; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 473 | |
| 474 | Module *module; |
| 475 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 476 | // Set of Capabilities required |
| 477 | CapabilitySetType CapabilitySet; |
| 478 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 479 | ArrayRef<std::pair<unsigned, std::string>> samplerMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 480 | raw_pwrite_stream &out; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 481 | |
| 482 | // TODO(dneto): Wouldn't it be better to always just emit a binary, and then |
| 483 | // convert to other formats on demand? |
| 484 | |
| 485 | // When emitting a C initialization list, the WriteSPIRVBinary method |
| 486 | // will actually write its words to this vector via binaryTempOut. |
| 487 | SmallVector<char, 100> binaryTempUnderlyingVector; |
| 488 | raw_svector_ostream binaryTempOut; |
| 489 | |
| 490 | // Binary output writes to this stream, which might be |out| or |
| 491 | // |binaryTempOut|. It's the latter when we really want to write a C |
| 492 | // initializer list. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 493 | raw_pwrite_stream *binaryOut; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 494 | std::vector<version0::DescriptorMapEntry> *descriptorMapEntries; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 495 | const bool outputCInitList; // If true, output look like {0x7023, ... , 5} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 496 | uint64_t patchBoundOffset; |
| 497 | uint32_t nextID; |
| 498 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 499 | SPIRVID incrNextID() { return nextID++; } |
| 500 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 501 | // ID for OpTypeInt 32 1. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 502 | SPIRVID int32ID; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 503 | // ID for OpTypeVector %int 4. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 504 | SPIRVID v4int32ID; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 505 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 506 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 507 | TypeMapType TypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 508 | // Maps an LLVM image type to its SPIR-V ID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 509 | TypeMapType ImageTypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 510 | // 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] | 511 | TypeList Types; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 512 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 513 | ValueMapType ValueMap; |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 514 | SPIRVInstructionList SPIRVSections[kSectionCount]; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 515 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 516 | EntryPointVecType EntryPointVec; |
| 517 | DeferredInstVecType DeferredInstVec; |
| 518 | ValueList EntryPointInterfacesVec; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 519 | SPIRVID OpExtInstImportID; |
| 520 | std::vector<SPIRVID> BuiltinDimensionVec; |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 521 | bool HasVariablePointersStorageBuffer; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 522 | bool HasVariablePointers; |
| 523 | Type *SamplerTy; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 524 | DenseMap<unsigned, SPIRVID> SamplerLiteralToIDMap; |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 525 | |
| 526 | // 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] | 527 | // will map F's type to (G, index of the parameter), where in a first phase |
| 528 | // G is F's type. During FindTypePerFunc, G will be changed to F's type |
| 529 | // but replacing the pointer-to-constant parameter with |
| 530 | // pointer-to-ModuleScopePrivate. |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 531 | // TODO(dneto): This doesn't seem general enough? A function might have |
| 532 | // more than one such parameter. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 533 | GlobalConstFuncMapType GlobalConstFuncTypeMap; |
| 534 | SmallPtrSet<Value *, 16> GlobalConstArgumentSet; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 535 | // An ordered set of pointer types of Base arguments to OpPtrAccessChain, |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 536 | // or array types, and which point into transparent memory (StorageBuffer |
| 537 | // storage class). These will require an ArrayStride decoration. |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 538 | // See SPV_KHR_variable_pointers rev 13. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 539 | TypeList TypesNeedingArrayStride; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 540 | |
| 541 | // This is truly ugly, but works around what look like driver bugs. |
| 542 | // For get_local_size, an earlier part of the flow has created a module-scope |
| 543 | // variable in Private address space to hold the value for the workgroup |
| 544 | // size. Its intializer is a uint3 value marked as builtin WorkgroupSize. |
| 545 | // When this is present, save the IDs of the initializer value and variable |
| 546 | // in these two variables. We only ever do a vector load from it, and |
| 547 | // when we see one of those, substitute just the value of the intializer. |
| 548 | // 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] | 549 | // TODO(dneto): Remove this once drivers are fixed. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 550 | SPIRVID WorkgroupSizeValueID; |
| 551 | SPIRVID WorkgroupSizeVarID; |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 552 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 553 | // Bookkeeping for mapping kernel arguments to resource variables. |
| 554 | struct ResourceVarInfo { |
| 555 | ResourceVarInfo(int index_arg, unsigned set_arg, unsigned binding_arg, |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 556 | Function *fn, clspv::ArgKind arg_kind_arg, int coherent_arg) |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 557 | : index(index_arg), descriptor_set(set_arg), binding(binding_arg), |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 558 | var_fn(fn), arg_kind(arg_kind_arg), coherent(coherent_arg), |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 559 | addr_space(fn->getReturnType()->getPointerAddressSpace()) {} |
| 560 | const int index; // Index into ResourceVarInfoList |
| 561 | const unsigned descriptor_set; |
| 562 | const unsigned binding; |
| 563 | Function *const var_fn; // The @clspv.resource.var.* function. |
| 564 | const clspv::ArgKind arg_kind; |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 565 | const int coherent; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 566 | const unsigned addr_space; // The LLVM address space |
| 567 | // The SPIR-V ID of the OpVariable. Not populated at construction time. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 568 | SPIRVID var_id; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 569 | }; |
| 570 | // A list of resource var info. Each one correponds to a module-scope |
| 571 | // resource variable we will have to create. Resource var indices are |
| 572 | // indices into this vector. |
| 573 | SmallVector<std::unique_ptr<ResourceVarInfo>, 8> ResourceVarInfoList; |
| 574 | // This is a vector of pointers of all the resource vars, but ordered by |
| 575 | // kernel function, and then by argument. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 576 | UniqueVector<ResourceVarInfo *> ModuleOrderedResourceVars; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 577 | // Map a function to the ordered list of resource variables it uses, one for |
| 578 | // each argument. If an argument does not use a resource variable, it |
| 579 | // will have a null pointer entry. |
| 580 | using FunctionToResourceVarsMapType = |
| 581 | DenseMap<Function *, SmallVector<ResourceVarInfo *, 8>>; |
| 582 | FunctionToResourceVarsMapType FunctionToResourceVarsMap; |
| 583 | |
| 584 | // What LLVM types map to SPIR-V types needing layout? These are the |
| 585 | // arrays and structures supporting storage buffers and uniform buffers. |
| 586 | TypeList TypesNeedingLayout; |
| 587 | // What LLVM struct types map to a SPIR-V struct type with Block decoration? |
| 588 | UniqueVector<StructType *> StructTypesNeedingBlock; |
| 589 | // For a call that represents a load from an opaque type (samplers, images), |
| 590 | // map it to the variable id it should load from. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 591 | DenseMap<CallInst *, SPIRVID> ResourceVarDeferredLoadCalls; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 592 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 593 | // An ordered list of the kernel arguments of type pointer-to-local. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 594 | using LocalArgList = SmallVector<Argument *, 8>; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 595 | LocalArgList LocalArgs; |
| 596 | // Information about a pointer-to-local argument. |
| 597 | struct LocalArgInfo { |
| 598 | // The SPIR-V ID of the array variable. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 599 | SPIRVID variable_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 600 | // The element type of the |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 601 | Type *elem_type; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 602 | // The ID of the array type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 603 | SPIRVID array_size_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 604 | // The ID of the array type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 605 | SPIRVID array_type_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 606 | // The ID of the pointer to the array type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 607 | SPIRVID ptr_array_type_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 608 | // The specialization constant ID of the array size. |
| 609 | int spec_id; |
| 610 | }; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 611 | // A mapping from Argument to its assigned SpecId. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 612 | DenseMap<const Argument *, int> LocalArgSpecIds; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 613 | // A mapping from SpecId to its LocalArgInfo. |
| 614 | DenseMap<int, LocalArgInfo> LocalSpecIdInfoMap; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 615 | // A mapping from a remapped type to its real offsets. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 616 | DenseMap<Type *, std::vector<uint32_t>> RemappedUBOTypeOffsets; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 617 | // A mapping from a remapped type to its real sizes. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 618 | DenseMap<Type *, std::tuple<uint64_t, uint64_t, uint64_t>> |
| 619 | RemappedUBOTypeSizes; |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 620 | |
| 621 | // Maps basic block to its merge block. |
| 622 | DenseMap<BasicBlock *, BasicBlock *> MergeBlocks; |
| 623 | // Maps basic block to its continue block. |
| 624 | DenseMap<BasicBlock *, BasicBlock *> ContinueBlocks; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 625 | |
| 626 | public: |
| 627 | static SPIRVProducerPass *Ptr; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | char SPIRVProducerPass::ID; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 631 | SPIRVProducerPass *SPIRVProducerPass::Ptr = nullptr; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 632 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 633 | } // namespace |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 634 | |
| 635 | namespace clspv { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 636 | ModulePass *createSPIRVProducerPass( |
| 637 | raw_pwrite_stream &out, |
| 638 | std::vector<version0::DescriptorMapEntry> *descriptor_map_entries, |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 639 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 640 | bool outputCInitList) { |
| 641 | return new SPIRVProducerPass(out, descriptor_map_entries, samplerMap, |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 642 | outputCInitList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 643 | } |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 644 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 645 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 646 | namespace { |
| 647 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, uint32_t num) { |
| 648 | list.emplace_back(LITERAL_WORD, num); |
| 649 | return list; |
| 650 | } |
| 651 | |
| 652 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, int32_t num) { |
| 653 | list.emplace_back(LITERAL_WORD, static_cast<uint32_t>(num)); |
| 654 | return list; |
| 655 | } |
| 656 | |
| 657 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, ArrayRef<uint32_t> num_vec) { |
| 658 | list.emplace_back(num_vec); |
| 659 | return list; |
| 660 | } |
| 661 | |
| 662 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, StringRef str) { |
| 663 | list.emplace_back(LITERAL_STRING, str); |
| 664 | return list; |
| 665 | } |
| 666 | |
| 667 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, Type *t) { |
| 668 | list.emplace_back(NUMBERID, SPIRVProducerPass::Ptr->getSPIRVType(t).get()); |
| 669 | return list; |
| 670 | } |
| 671 | |
| 672 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, Value *v) { |
| 673 | list.emplace_back(NUMBERID, SPIRVProducerPass::Ptr->getSPIRVValue(v).get()); |
| 674 | return list; |
| 675 | } |
| 676 | |
| 677 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, SPIRVID &v) { |
| 678 | list.emplace_back(NUMBERID, v.get()); |
| 679 | return list; |
| 680 | } |
| 681 | } // namespace |
| 682 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 683 | bool SPIRVProducerPass::runOnModule(Module &M) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 684 | // TODO(sjw): Need to reset all data members for each Module, or better |
| 685 | // yet create a new SPIRVProducer for every module.. For now only |
| 686 | // allow 1 call. |
| 687 | assert(module == nullptr); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 688 | module = &M; |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 689 | if (ShowProducerIR) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 690 | llvm::outs() << *module << "\n"; |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 691 | } |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 692 | binaryOut = outputCInitList ? &binaryTempOut : &out; |
| 693 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 694 | PopulateUBOTypeMaps(); |
| 695 | PopulateStructuredCFGMaps(); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 696 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 697 | // SPIR-V always begins with its header information |
| 698 | outputHeader(); |
| 699 | |
| 700 | // Gather information from the LLVM IR that we require. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 701 | GenerateLLVMIRInfo(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 702 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 703 | // Collect information on global variables too. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 704 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 705 | // If the GV is one of our special __spirv_* variables, remove the |
| 706 | // initializer as it was only placed there to force LLVM to not throw the |
| 707 | // value away. |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 708 | if (GV.getName().startswith("__spirv_") || |
| 709 | GV.getAddressSpace() == clspv::AddressSpace::PushConstant) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 710 | GV.setInitializer(nullptr); |
| 711 | } |
| 712 | |
| 713 | // Collect types' information from global variable. |
| 714 | FindTypePerGlobalVar(GV); |
| 715 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 716 | // If the variable is an input, entry points need to know about it. |
| 717 | if (AddressSpace::Input == GV.getType()->getPointerAddressSpace()) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 718 | getEntryPointInterfacesVec().insert(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 722 | // Generate SPIRV instructions for types. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 723 | GenerateSPIRVTypes(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 724 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 725 | // Generate literal samplers if necessary. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 726 | GenerateSamplers(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 727 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 728 | // Generate descriptor map entries for all push constants |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 729 | GeneratePushConstantDescriptorMapEntries(); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 730 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 731 | // Generate SPIRV variables. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 732 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 733 | GenerateGlobalVar(GV); |
| 734 | } |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 735 | GenerateResourceVars(); |
| 736 | GenerateWorkgroupVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 737 | |
| 738 | // Generate SPIRV instructions for each function. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 739 | for (Function &F : *module) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 740 | if (F.isDeclaration()) { |
| 741 | continue; |
| 742 | } |
| 743 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 744 | GenerateDescriptorMapInfo(F); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 745 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 746 | // Generate Function Prologue. |
| 747 | GenerateFuncPrologue(F); |
| 748 | |
| 749 | // Generate SPIRV instructions for function body. |
| 750 | GenerateFuncBody(F); |
| 751 | |
| 752 | // Generate Function Epilogue. |
| 753 | GenerateFuncEpilogue(); |
| 754 | } |
| 755 | |
| 756 | HandleDeferredInstruction(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 757 | HandleDeferredDecorations(); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 758 | |
| 759 | // Generate descriptor map entries for module scope specialization constants. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 760 | GenerateSpecConstantDescriptorMapEntries(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 761 | |
| 762 | // Generate SPIRV module information. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 763 | GenerateModuleInfo(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 764 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 765 | WriteSPIRVBinary(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 766 | |
| 767 | // We need to patch the SPIR-V header to set bound correctly. |
| 768 | patchHeader(); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 769 | |
| 770 | if (outputCInitList) { |
| 771 | bool first = true; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 772 | std::ostringstream os; |
| 773 | |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 774 | auto emit_word = [&os, &first](uint32_t word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 775 | if (!first) |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 776 | os << ",\n"; |
| 777 | os << word; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 778 | first = false; |
| 779 | }; |
| 780 | |
| 781 | os << "{"; |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 782 | const std::string str(binaryTempOut.str()); |
| 783 | for (unsigned i = 0; i < str.size(); i += 4) { |
| 784 | const uint32_t a = static_cast<unsigned char>(str[i]); |
| 785 | const uint32_t b = static_cast<unsigned char>(str[i + 1]); |
| 786 | const uint32_t c = static_cast<unsigned char>(str[i + 2]); |
| 787 | const uint32_t d = static_cast<unsigned char>(str[i + 3]); |
| 788 | emit_word(a | (b << 8) | (c << 16) | (d << 24)); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 789 | } |
| 790 | os << "}\n"; |
| 791 | out << os.str(); |
| 792 | } |
| 793 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 794 | return false; |
| 795 | } |
| 796 | |
| 797 | void SPIRVProducerPass::outputHeader() { |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 798 | binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber), |
| 799 | sizeof(spv::MagicNumber)); |
alan-baker | e090260 | 2020-03-23 08:43:40 -0400 | [diff] [blame] | 800 | const uint32_t spv_version = 0x10000; // SPIR-V 1.0 |
| 801 | binaryOut->write(reinterpret_cast<const char *>(&spv_version), |
| 802 | sizeof(spv_version)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 803 | |
alan-baker | 0c18ab0 | 2019-06-12 10:23:21 -0400 | [diff] [blame] | 804 | // use Google's vendor ID |
| 805 | const uint32_t vendor = 21 << 16; |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 806 | binaryOut->write(reinterpret_cast<const char *>(&vendor), sizeof(vendor)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 807 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 808 | // we record where we need to come back to and patch in the bound value |
| 809 | patchBoundOffset = binaryOut->tell(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 810 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 811 | // output a bad bound for now |
| 812 | binaryOut->write(reinterpret_cast<const char *>(&nextID), sizeof(nextID)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 813 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 814 | // output the schema (reserved for use and must be 0) |
| 815 | const uint32_t schema = 0; |
| 816 | binaryOut->write(reinterpret_cast<const char *>(&schema), sizeof(schema)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | void SPIRVProducerPass::patchHeader() { |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 820 | // for a binary we just write the value of nextID over bound |
| 821 | binaryOut->pwrite(reinterpret_cast<char *>(&nextID), sizeof(nextID), |
| 822 | patchBoundOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 823 | } |
| 824 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 825 | void SPIRVProducerPass::GenerateLLVMIRInfo() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 826 | // This function generates LLVM IR for function such as global variable for |
| 827 | // argument, constant and pointer type for argument access. These information |
| 828 | // is artificial one because we need Vulkan SPIR-V output. This function is |
| 829 | // executed ahead of FindType and FindConstant. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 830 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 831 | FindGlobalConstVars(); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 832 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 833 | FindResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 834 | |
| 835 | bool HasWorkGroupBuiltin = false; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 836 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 837 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 838 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 839 | HasWorkGroupBuiltin = true; |
| 840 | } |
| 841 | } |
| 842 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 843 | FindTypesForSamplerMap(); |
| 844 | FindTypesForResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 845 | } |
| 846 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 847 | void SPIRVProducerPass::FindGlobalConstVars() { |
| 848 | clspv::NormalizeGlobalVariables(*module); |
| 849 | const DataLayout &DL = module->getDataLayout(); |
alan-baker | 56f7aff | 2019-05-22 08:06:42 -0400 | [diff] [blame] | 850 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 851 | SmallVector<GlobalVariable *, 8> GVList; |
| 852 | SmallVector<GlobalVariable *, 8> DeadGVList; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 853 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 854 | if (GV.getType()->getAddressSpace() == AddressSpace::Constant) { |
| 855 | if (GV.use_empty()) { |
| 856 | DeadGVList.push_back(&GV); |
| 857 | } else { |
| 858 | GVList.push_back(&GV); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | // Remove dead global __constant variables. |
| 864 | for (auto GV : DeadGVList) { |
| 865 | GV->eraseFromParent(); |
| 866 | } |
| 867 | DeadGVList.clear(); |
| 868 | |
| 869 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 870 | // For now, we only support a single storage buffer. |
| 871 | if (GVList.size() > 0) { |
| 872 | assert(GVList.size() == 1); |
| 873 | const auto *GV = GVList[0]; |
| 874 | const auto constants_byte_size = |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 875 | (GetTypeSizeInBits(GV->getInitializer()->getType(), DL)) / 8; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 876 | const size_t kConstantMaxSize = 65536; |
| 877 | if (constants_byte_size > kConstantMaxSize) { |
| 878 | outs() << "Max __constant capacity of " << kConstantMaxSize |
| 879 | << " bytes exceeded: " << constants_byte_size << " bytes used\n"; |
| 880 | llvm_unreachable("Max __constant capacity exceeded"); |
| 881 | } |
| 882 | } |
| 883 | } else { |
| 884 | // Change global constant variable's address space to ModuleScopePrivate. |
| 885 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 886 | for (auto GV : GVList) { |
| 887 | // Create new gv with ModuleScopePrivate address space. |
| 888 | Type *NewGVTy = GV->getType()->getPointerElementType(); |
| 889 | GlobalVariable *NewGV = new GlobalVariable( |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 890 | *module, NewGVTy, false, GV->getLinkage(), GV->getInitializer(), "", |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 891 | nullptr, GV->getThreadLocalMode(), AddressSpace::ModuleScopePrivate); |
| 892 | NewGV->takeName(GV); |
| 893 | |
| 894 | const SmallVector<User *, 8> GVUsers(GV->user_begin(), GV->user_end()); |
| 895 | SmallVector<User *, 8> CandidateUsers; |
| 896 | |
| 897 | auto record_called_function_type_as_user = |
| 898 | [&GlobalConstFuncTyMap](Value *gv, CallInst *call) { |
| 899 | // Find argument index. |
| 900 | unsigned index = 0; |
| 901 | for (unsigned i = 0; i < call->getNumArgOperands(); i++) { |
| 902 | if (gv == call->getOperand(i)) { |
| 903 | // TODO(dneto): Should we break here? |
| 904 | index = i; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | // Record function type with global constant. |
| 909 | GlobalConstFuncTyMap[call->getFunctionType()] = |
| 910 | std::make_pair(call->getFunctionType(), index); |
| 911 | }; |
| 912 | |
| 913 | for (User *GVU : GVUsers) { |
| 914 | if (CallInst *Call = dyn_cast<CallInst>(GVU)) { |
| 915 | record_called_function_type_as_user(GV, Call); |
| 916 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(GVU)) { |
| 917 | // Check GEP users. |
| 918 | for (User *GEPU : GEP->users()) { |
| 919 | if (CallInst *GEPCall = dyn_cast<CallInst>(GEPU)) { |
| 920 | record_called_function_type_as_user(GEP, GEPCall); |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | CandidateUsers.push_back(GVU); |
| 926 | } |
| 927 | |
| 928 | for (User *U : CandidateUsers) { |
| 929 | // Update users of gv with new gv. |
alan-baker | ed80f57 | 2019-02-11 17:28:26 -0500 | [diff] [blame] | 930 | if (!isa<Constant>(U)) { |
| 931 | // #254: Can't change operands of a constant, but this shouldn't be |
| 932 | // something that sticks around in the module. |
| 933 | U->replaceUsesOfWith(GV, NewGV); |
| 934 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | // Delete original gv. |
| 938 | GV->eraseFromParent(); |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 943 | void SPIRVProducerPass::FindResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 944 | ResourceVarInfoList.clear(); |
| 945 | FunctionToResourceVarsMap.clear(); |
| 946 | ModuleOrderedResourceVars.reset(); |
| 947 | // Normally, there is one resource variable per clspv.resource.var.* |
| 948 | // function, since that is unique'd by arg type and index. By design, |
| 949 | // we can share these resource variables across kernels because all |
| 950 | // kernels use the same descriptor set. |
| 951 | // |
| 952 | // But if the user requested distinct descriptor sets per kernel, then |
| 953 | // the descriptor allocator has made different (set,binding) pairs for |
| 954 | // the same (type,arg_index) pair. Since we can decorate a resource |
| 955 | // variable with only exactly one DescriptorSet and Binding, we are |
| 956 | // forced in this case to make distinct resource variables whenever |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 957 | // the same clspv.resource.var.X function is seen with disintct |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 958 | // (set,binding) values. |
| 959 | const bool always_distinct_sets = |
| 960 | clspv::Option::DistinctKernelDescriptorSets(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 961 | for (Function &F : *module) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 962 | // Rely on the fact the resource var functions have a stable ordering |
| 963 | // in the module. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 964 | if (F.getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 965 | // Find all calls to this function with distinct set and binding pairs. |
| 966 | // Save them in ResourceVarInfoList. |
| 967 | |
| 968 | // Determine uniqueness of the (set,binding) pairs only withing this |
| 969 | // one resource-var builtin function. |
| 970 | using SetAndBinding = std::pair<unsigned, unsigned>; |
| 971 | // Maps set and binding to the resource var info. |
| 972 | DenseMap<SetAndBinding, ResourceVarInfo *> set_and_binding_map; |
| 973 | bool first_use = true; |
| 974 | for (auto &U : F.uses()) { |
| 975 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 976 | const auto set = unsigned( |
| 977 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 978 | const auto binding = unsigned( |
| 979 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 980 | const auto arg_kind = clspv::ArgKind( |
| 981 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 982 | const auto arg_index = unsigned( |
| 983 | dyn_cast<ConstantInt>(call->getArgOperand(3))->getZExtValue()); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 984 | const auto coherent = unsigned( |
| 985 | dyn_cast<ConstantInt>(call->getArgOperand(5))->getZExtValue()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 986 | |
| 987 | // Find or make the resource var info for this combination. |
| 988 | ResourceVarInfo *rv = nullptr; |
| 989 | if (always_distinct_sets) { |
| 990 | // Make a new resource var any time we see a different |
| 991 | // (set,binding) pair. |
| 992 | SetAndBinding key{set, binding}; |
| 993 | auto where = set_and_binding_map.find(key); |
| 994 | if (where == set_and_binding_map.end()) { |
| 995 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 996 | binding, &F, arg_kind, coherent); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 997 | ResourceVarInfoList.emplace_back(rv); |
| 998 | set_and_binding_map[key] = rv; |
| 999 | } else { |
| 1000 | rv = where->second; |
| 1001 | } |
| 1002 | } else { |
| 1003 | // The default is to make exactly one resource for each |
| 1004 | // clspv.resource.var.* function. |
| 1005 | if (first_use) { |
| 1006 | first_use = false; |
| 1007 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 1008 | binding, &F, arg_kind, coherent); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1009 | ResourceVarInfoList.emplace_back(rv); |
| 1010 | } else { |
| 1011 | rv = ResourceVarInfoList.back().get(); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | // Now populate FunctionToResourceVarsMap. |
| 1016 | auto &mapping = |
| 1017 | FunctionToResourceVarsMap[call->getParent()->getParent()]; |
| 1018 | while (mapping.size() <= arg_index) { |
| 1019 | mapping.push_back(nullptr); |
| 1020 | } |
| 1021 | mapping[arg_index] = rv; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | // Populate ModuleOrderedResourceVars. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1028 | for (Function &F : *module) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1029 | auto where = FunctionToResourceVarsMap.find(&F); |
| 1030 | if (where != FunctionToResourceVarsMap.end()) { |
| 1031 | for (auto &rv : where->second) { |
| 1032 | if (rv != nullptr) { |
| 1033 | ModuleOrderedResourceVars.insert(rv); |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | if (ShowResourceVars) { |
| 1039 | for (auto *info : ModuleOrderedResourceVars) { |
| 1040 | outs() << "MORV index " << info->index << " (" << info->descriptor_set |
| 1041 | << "," << info->binding << ") " << *(info->var_fn->getReturnType()) |
| 1042 | << "\n"; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1047 | void SPIRVProducerPass::FindTypePerGlobalVar(GlobalVariable &GV) { |
| 1048 | // Investigate global variable's type. |
| 1049 | FindType(GV.getType()); |
| 1050 | } |
| 1051 | |
| 1052 | void SPIRVProducerPass::FindTypePerFunc(Function &F) { |
| 1053 | // Investigate function's type. |
| 1054 | FunctionType *FTy = F.getFunctionType(); |
| 1055 | |
| 1056 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 1057 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1058 | // Handle a regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1059 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 1060 | uint32_t GVCstArgIdx = GlobalConstFuncTypeMap[FTy].second; |
| 1061 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1062 | for (unsigned i = 0; i < FTy->getNumParams(); i++) { |
| 1063 | Type *ParamTy = FTy->getParamType(i); |
| 1064 | if (i == GVCstArgIdx) { |
| 1065 | Type *EleTy = ParamTy->getPointerElementType(); |
| 1066 | ParamTy = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1067 | } |
| 1068 | |
| 1069 | NewFuncParamTys.push_back(ParamTy); |
| 1070 | } |
| 1071 | |
| 1072 | FunctionType *NewFTy = |
| 1073 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1074 | GlobalConstFuncTyMap[FTy] = std::make_pair(NewFTy, GVCstArgIdx); |
| 1075 | FTy = NewFTy; |
| 1076 | } |
| 1077 | |
| 1078 | FindType(FTy); |
| 1079 | } else { |
| 1080 | // As kernel functions do not have parameters, create new function type and |
| 1081 | // add it to type map. |
| 1082 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1083 | FunctionType *NewFTy = |
| 1084 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1085 | FindType(NewFTy); |
| 1086 | } |
| 1087 | |
| 1088 | // Investigate instructions' type in function body. |
| 1089 | for (BasicBlock &BB : F) { |
| 1090 | for (Instruction &I : BB) { |
| 1091 | if (isa<ShuffleVectorInst>(I)) { |
| 1092 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1093 | // Ignore type for mask of shuffle vector instruction. |
| 1094 | if (i == 2) { |
| 1095 | continue; |
| 1096 | } |
| 1097 | |
| 1098 | Value *Op = I.getOperand(i); |
| 1099 | if (!isa<MetadataAsValue>(Op)) { |
| 1100 | FindType(Op->getType()); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | FindType(I.getType()); |
| 1105 | continue; |
| 1106 | } |
| 1107 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1108 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 1109 | |
| 1110 | if (Call && Call->getCalledFunction()->getName().startswith( |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1111 | clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1112 | // This is a fake call representing access to a resource variable. |
| 1113 | // We handle that elsewhere. |
| 1114 | continue; |
| 1115 | } |
| 1116 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1117 | if (Call && Call->getCalledFunction()->getName().startswith( |
| 1118 | clspv::WorkgroupAccessorFunction())) { |
| 1119 | // This is a fake call representing access to a workgroup variable. |
| 1120 | // We handle that elsewhere. |
| 1121 | continue; |
| 1122 | } |
| 1123 | |
alan-baker | f083bed | 2020-01-29 08:15:42 -0500 | [diff] [blame] | 1124 | // #497: InsertValue and ExtractValue map to OpCompositeInsert and |
| 1125 | // OpCompositeExtract which takes literal values for indices. As a result |
| 1126 | // don't map the type of indices. |
| 1127 | if (I.getOpcode() == Instruction::ExtractValue) { |
| 1128 | FindType(I.getOperand(0)->getType()); |
| 1129 | continue; |
| 1130 | } |
| 1131 | if (I.getOpcode() == Instruction::InsertValue) { |
| 1132 | FindType(I.getOperand(0)->getType()); |
| 1133 | FindType(I.getOperand(1)->getType()); |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
| 1137 | // #497: InsertElement and ExtractElement map to OpCompositeExtract if |
| 1138 | // the index is a constant. In such a case don't map the index type. |
| 1139 | if (I.getOpcode() == Instruction::ExtractElement) { |
| 1140 | FindType(I.getOperand(0)->getType()); |
| 1141 | Value *op1 = I.getOperand(1); |
| 1142 | if (!isa<Constant>(op1) || isa<GlobalValue>(op1)) { |
| 1143 | FindType(op1->getType()); |
| 1144 | } |
| 1145 | continue; |
| 1146 | } |
| 1147 | if (I.getOpcode() == Instruction::InsertElement) { |
| 1148 | FindType(I.getOperand(0)->getType()); |
| 1149 | FindType(I.getOperand(1)->getType()); |
| 1150 | Value *op2 = I.getOperand(2); |
| 1151 | if (!isa<Constant>(op2) || isa<GlobalValue>(op2)) { |
| 1152 | FindType(op2->getType()); |
| 1153 | } |
| 1154 | continue; |
| 1155 | } |
| 1156 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1157 | // Work through the operands of the instruction. |
| 1158 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1159 | Value *const Op = I.getOperand(i); |
| 1160 | // If any of the operands is a constant, find the type! |
| 1161 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1162 | FindType(Op->getType()); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | for (Use &Op : I.operands()) { |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame] | 1167 | if (isa<CallInst>(&I)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1168 | // Avoid to check call instruction's type. |
| 1169 | break; |
| 1170 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1171 | if (CallInst *OpCall = dyn_cast<CallInst>(Op)) { |
| 1172 | if (OpCall && OpCall->getCalledFunction()->getName().startswith( |
| 1173 | clspv::WorkgroupAccessorFunction())) { |
| 1174 | // This is a fake call representing access to a workgroup variable. |
| 1175 | // We handle that elsewhere. |
| 1176 | continue; |
| 1177 | } |
| 1178 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1179 | if (!isa<MetadataAsValue>(&Op)) { |
| 1180 | FindType(Op->getType()); |
| 1181 | continue; |
| 1182 | } |
| 1183 | } |
| 1184 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1185 | // We don't want to track the type of this call as we are going to replace |
| 1186 | // it. |
Kévin Petit | df71de3 | 2019-04-09 14:09:50 +0100 | [diff] [blame] | 1187 | if (Call && (clspv::LiteralSamplerFunction() == |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1188 | Call->getCalledFunction()->getName())) { |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 1193 | // If gep's base operand has ModuleScopePrivate address space, make gep |
| 1194 | // return ModuleScopePrivate address space. |
| 1195 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate) { |
| 1196 | // Add pointer type with private address space for global constant to |
| 1197 | // type list. |
| 1198 | Type *EleTy = I.getType()->getPointerElementType(); |
| 1199 | Type *NewPTy = |
| 1200 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1201 | |
| 1202 | FindType(NewPTy); |
| 1203 | continue; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | FindType(I.getType()); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1212 | void SPIRVProducerPass::FindTypesForSamplerMap() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1213 | // If we are using a sampler map, find the type of the sampler. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1214 | if (module->getFunction(clspv::LiteralSamplerFunction()) || |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1215 | 0 < getSamplerMap().size()) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1216 | auto SamplerStructTy = module->getTypeByName("opencl.sampler_t"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1217 | if (!SamplerStructTy) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1218 | SamplerStructTy = |
| 1219 | StructType::create(module->getContext(), "opencl.sampler_t"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | SamplerTy = SamplerStructTy->getPointerTo(AddressSpace::UniformConstant); |
| 1223 | |
| 1224 | FindType(SamplerTy); |
| 1225 | } |
| 1226 | } |
| 1227 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1228 | void SPIRVProducerPass::FindTypesForResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1229 | // Record types so they are generated. |
| 1230 | TypesNeedingLayout.reset(); |
| 1231 | StructTypesNeedingBlock.reset(); |
| 1232 | |
| 1233 | // To match older clspv codegen, generate the float type first if required |
| 1234 | // for images. |
| 1235 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1236 | if (info->arg_kind == clspv::ArgKind::ReadOnlyImage || |
| 1237 | info->arg_kind == clspv::ArgKind::WriteOnlyImage) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1238 | if (IsIntImageType(info->var_fn->getReturnType())) { |
| 1239 | // Nothing for now... |
| 1240 | } else if (IsUintImageType(info->var_fn->getReturnType())) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1241 | FindType(Type::getInt32Ty(module->getContext())); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | // We need "float" either for the sampled type or for the Lod operand. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1245 | FindType(Type::getFloatTy(module->getContext())); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1250 | Type *type = info->var_fn->getReturnType(); |
| 1251 | |
| 1252 | switch (info->arg_kind) { |
| 1253 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1254 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1255 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1256 | StructTypesNeedingBlock.insert(sty); |
| 1257 | } else { |
| 1258 | errs() << *type << "\n"; |
| 1259 | llvm_unreachable("Buffer arguments must map to structures!"); |
| 1260 | } |
| 1261 | break; |
| 1262 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 1263 | case clspv::ArgKind::PodUBO: |
| 1264 | case clspv::ArgKind::PodPushConstant: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1265 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1266 | StructTypesNeedingBlock.insert(sty); |
| 1267 | } else { |
| 1268 | errs() << *type << "\n"; |
| 1269 | llvm_unreachable("POD arguments must map to structures!"); |
| 1270 | } |
| 1271 | break; |
| 1272 | case clspv::ArgKind::ReadOnlyImage: |
| 1273 | case clspv::ArgKind::WriteOnlyImage: |
| 1274 | case clspv::ArgKind::Sampler: |
| 1275 | // Sampler and image types map to the pointee type but |
| 1276 | // in the uniform constant address space. |
| 1277 | type = PointerType::get(type->getPointerElementType(), |
| 1278 | clspv::AddressSpace::UniformConstant); |
| 1279 | break; |
| 1280 | default: |
| 1281 | break; |
| 1282 | } |
| 1283 | |
| 1284 | // The converted type is the type of the OpVariable we will generate. |
| 1285 | // If the pointee type is an array of size zero, FindType will convert it |
| 1286 | // to a runtime array. |
| 1287 | FindType(type); |
| 1288 | } |
| 1289 | |
alan-baker | dcd9741 | 2019-09-16 15:32:30 -0400 | [diff] [blame] | 1290 | // If module constants are clustered in a storage buffer then that struct |
| 1291 | // needs layout decorations. |
| 1292 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1293 | for (GlobalVariable &GV : module->globals()) { |
alan-baker | dcd9741 | 2019-09-16 15:32:30 -0400 | [diff] [blame] | 1294 | PointerType *PTy = cast<PointerType>(GV.getType()); |
| 1295 | const auto AS = PTy->getAddressSpace(); |
| 1296 | const bool module_scope_constant_external_init = |
| 1297 | (AS == AddressSpace::Constant) && GV.hasInitializer(); |
| 1298 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 1299 | if (module_scope_constant_external_init && |
| 1300 | spv::BuiltInMax == BuiltinType) { |
| 1301 | StructTypesNeedingBlock.insert( |
| 1302 | cast<StructType>(PTy->getPointerElementType())); |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1307 | for (const GlobalVariable &GV : module->globals()) { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 1308 | if (GV.getAddressSpace() == clspv::AddressSpace::PushConstant) { |
| 1309 | auto Ty = cast<PointerType>(GV.getType())->getPointerElementType(); |
| 1310 | assert(Ty->isStructTy() && "Push constants have to be structures."); |
| 1311 | auto STy = cast<StructType>(Ty); |
| 1312 | StructTypesNeedingBlock.insert(STy); |
| 1313 | } |
| 1314 | } |
| 1315 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1316 | // Traverse the arrays and structures underneath each Block, and |
| 1317 | // mark them as needing layout. |
| 1318 | std::vector<Type *> work_list(StructTypesNeedingBlock.begin(), |
| 1319 | StructTypesNeedingBlock.end()); |
| 1320 | while (!work_list.empty()) { |
| 1321 | Type *type = work_list.back(); |
| 1322 | work_list.pop_back(); |
| 1323 | TypesNeedingLayout.insert(type); |
| 1324 | switch (type->getTypeID()) { |
| 1325 | case Type::ArrayTyID: |
| 1326 | work_list.push_back(type->getArrayElementType()); |
| 1327 | if (!Hack_generate_runtime_array_stride_early) { |
| 1328 | // Remember this array type for deferred decoration. |
| 1329 | TypesNeedingArrayStride.insert(type); |
| 1330 | } |
| 1331 | break; |
| 1332 | case Type::StructTyID: |
| 1333 | for (auto *elem_ty : cast<StructType>(type)->elements()) { |
| 1334 | work_list.push_back(elem_ty); |
| 1335 | } |
| 1336 | default: |
| 1337 | // This type and its contained types don't get layout. |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1343 | void SPIRVProducerPass::GenerateWorkgroupVars() { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1344 | // The SpecId assignment for pointer-to-local arguments is recorded in |
| 1345 | // module-level metadata. Translate that information into local argument |
| 1346 | // information. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1347 | LLVMContext &Context = module->getContext(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1348 | NamedMDNode *nmd = module->getNamedMetadata(clspv::LocalSpecIdMetadataName()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1349 | if (!nmd) |
| 1350 | return; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1351 | for (auto operand : nmd->operands()) { |
| 1352 | MDTuple *tuple = cast<MDTuple>(operand); |
| 1353 | ValueAsMetadata *fn_md = cast<ValueAsMetadata>(tuple->getOperand(0)); |
| 1354 | Function *func = cast<Function>(fn_md->getValue()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1355 | ConstantAsMetadata *arg_index_md = |
| 1356 | cast<ConstantAsMetadata>(tuple->getOperand(1)); |
| 1357 | int arg_index = static_cast<int>( |
| 1358 | cast<ConstantInt>(arg_index_md->getValue())->getSExtValue()); |
| 1359 | Argument *arg = &*(func->arg_begin() + arg_index); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1360 | |
| 1361 | ConstantAsMetadata *spec_id_md = |
| 1362 | cast<ConstantAsMetadata>(tuple->getOperand(2)); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1363 | int spec_id = static_cast<int>( |
| 1364 | cast<ConstantInt>(spec_id_md->getValue())->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1365 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1366 | LocalArgSpecIds[arg] = spec_id; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1367 | if (LocalSpecIdInfoMap.count(spec_id)) |
| 1368 | continue; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1369 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1370 | // Generate the spec constant. |
| 1371 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1372 | Ops << Type::getInt32Ty(Context) << 1; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1373 | SPIRVID ArraySizeID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1374 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1375 | // Generate the array type. |
| 1376 | Type *ElemTy = arg->getType()->getPointerElementType(); |
| 1377 | Ops.clear(); |
| 1378 | // The element type must have been created. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1379 | Ops << ElemTy << ArraySizeID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1380 | |
| 1381 | SPIRVID ArrayTypeID = addSPIRVInst<kTypes>(spv::OpTypeArray, Ops); |
| 1382 | |
| 1383 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1384 | Ops << spv::StorageClassWorkgroup << ArrayTypeID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1385 | SPIRVID PtrArrayTypeID = addSPIRVInst<kTypes>(spv::OpTypePointer, Ops); |
| 1386 | |
| 1387 | // Generate OpVariable. |
| 1388 | // |
| 1389 | // Ops[0] : Result Type ID |
| 1390 | // Ops[1] : Storage Class |
| 1391 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1392 | Ops << PtrArrayTypeID << spv::StorageClassWorkgroup; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1393 | |
| 1394 | SPIRVID VariableID = addSPIRVInst<kGlobalVariables>(spv::OpVariable, Ops); |
| 1395 | |
| 1396 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1397 | Ops << ArraySizeID << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1398 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 1399 | |
| 1400 | LocalArgInfo info{VariableID, ElemTy, ArraySizeID, |
| 1401 | ArrayTypeID, PtrArrayTypeID, spec_id}; |
| 1402 | LocalSpecIdInfoMap[spec_id] = info; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1406 | void SPIRVProducerPass::FindType(Type *Ty) { |
| 1407 | TypeList &TyList = getTypeList(); |
| 1408 | |
| 1409 | if (0 != TyList.idFor(Ty)) { |
| 1410 | return; |
| 1411 | } |
| 1412 | |
| 1413 | if (Ty->isPointerTy()) { |
| 1414 | auto AddrSpace = Ty->getPointerAddressSpace(); |
| 1415 | if ((AddressSpace::Constant == AddrSpace) || |
| 1416 | (AddressSpace::Global == AddrSpace)) { |
| 1417 | auto PointeeTy = Ty->getPointerElementType(); |
| 1418 | |
| 1419 | if (PointeeTy->isStructTy() && |
| 1420 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1421 | FindType(PointeeTy); |
| 1422 | auto ActualPointerTy = |
| 1423 | PointeeTy->getPointerTo(AddressSpace::UniformConstant); |
| 1424 | FindType(ActualPointerTy); |
| 1425 | return; |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1430 | // By convention, LLVM array type with 0 elements will map to |
| 1431 | // OpTypeRuntimeArray. Otherwise, it will map to OpTypeArray, which |
| 1432 | // has a constant number of elements. We need to support type of the |
| 1433 | // constant. |
| 1434 | if (auto *arrayTy = dyn_cast<ArrayType>(Ty)) { |
| 1435 | if (arrayTy->getNumElements() > 0) { |
| 1436 | LLVMContext &Context = Ty->getContext(); |
| 1437 | FindType(Type::getInt32Ty(Context)); |
| 1438 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | for (Type *SubTy : Ty->subtypes()) { |
| 1442 | FindType(SubTy); |
| 1443 | } |
| 1444 | |
| 1445 | TyList.insert(Ty); |
| 1446 | } |
| 1447 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1448 | spv::StorageClass SPIRVProducerPass::GetStorageClass(unsigned AddrSpace) const { |
| 1449 | switch (AddrSpace) { |
| 1450 | default: |
| 1451 | llvm_unreachable("Unsupported OpenCL address space"); |
| 1452 | case AddressSpace::Private: |
| 1453 | return spv::StorageClassFunction; |
| 1454 | case AddressSpace::Global: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1455 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1456 | case AddressSpace::Constant: |
| 1457 | return clspv::Option::ConstantArgsInUniformBuffer() |
| 1458 | ? spv::StorageClassUniform |
| 1459 | : spv::StorageClassStorageBuffer; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1460 | case AddressSpace::Input: |
| 1461 | return spv::StorageClassInput; |
| 1462 | case AddressSpace::Local: |
| 1463 | return spv::StorageClassWorkgroup; |
| 1464 | case AddressSpace::UniformConstant: |
| 1465 | return spv::StorageClassUniformConstant; |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1466 | case AddressSpace::Uniform: |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 1467 | return spv::StorageClassUniform; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1468 | case AddressSpace::ModuleScopePrivate: |
| 1469 | return spv::StorageClassPrivate; |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 1470 | case AddressSpace::PushConstant: |
| 1471 | return spv::StorageClassPushConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1475 | spv::StorageClass |
| 1476 | SPIRVProducerPass::GetStorageClassForArgKind(clspv::ArgKind arg_kind) const { |
| 1477 | switch (arg_kind) { |
| 1478 | case clspv::ArgKind::Buffer: |
| 1479 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1480 | case clspv::ArgKind::BufferUBO: |
| 1481 | return spv::StorageClassUniform; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1482 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 1483 | return spv::StorageClassStorageBuffer; |
| 1484 | case clspv::ArgKind::PodUBO: |
| 1485 | return spv::StorageClassUniform; |
| 1486 | case clspv::ArgKind::PodPushConstant: |
| 1487 | return spv::StorageClassPushConstant; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1488 | case clspv::ArgKind::Local: |
| 1489 | return spv::StorageClassWorkgroup; |
| 1490 | case clspv::ArgKind::ReadOnlyImage: |
| 1491 | case clspv::ArgKind::WriteOnlyImage: |
| 1492 | case clspv::ArgKind::Sampler: |
| 1493 | return spv::StorageClassUniformConstant; |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame] | 1494 | default: |
| 1495 | llvm_unreachable("Unsupported storage class for argument kind"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1499 | spv::BuiltIn SPIRVProducerPass::GetBuiltin(StringRef Name) const { |
| 1500 | return StringSwitch<spv::BuiltIn>(Name) |
| 1501 | .Case("__spirv_GlobalInvocationId", spv::BuiltInGlobalInvocationId) |
| 1502 | .Case("__spirv_LocalInvocationId", spv::BuiltInLocalInvocationId) |
| 1503 | .Case("__spirv_WorkgroupSize", spv::BuiltInWorkgroupSize) |
| 1504 | .Case("__spirv_NumWorkgroups", spv::BuiltInNumWorkgroups) |
| 1505 | .Case("__spirv_WorkgroupId", spv::BuiltInWorkgroupId) |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 1506 | .Case("__spirv_WorkDim", spv::BuiltInWorkDim) |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 1507 | .Case("__spirv_GlobalOffset", spv::BuiltInGlobalOffset) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1508 | .Default(spv::BuiltInMax); |
| 1509 | } |
| 1510 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1511 | SPIRVID SPIRVProducerPass::getOpExtInstImportID() { |
| 1512 | if (OpExtInstImportID == 0) { |
| 1513 | // |
| 1514 | // Generate OpExtInstImport. |
| 1515 | // |
| 1516 | // Ops[0] ... Ops[n] = Name (Literal String) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1517 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1518 | OpExtInstImportID = |
| 1519 | addSPIRVInst<kImports>(spv::OpExtInstImport, "GLSL.std.450"); |
| 1520 | } |
| 1521 | return OpExtInstImportID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1522 | } |
| 1523 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1524 | SPIRVID SPIRVProducerPass::getSPIRVType(Type *Ty) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1525 | auto TI = TypeMap.find(Ty); |
| 1526 | if (TI != TypeMap.end()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1527 | assert(TI->second.isValid()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1528 | return TI->second; |
| 1529 | } |
| 1530 | |
| 1531 | const auto &DL = module->getDataLayout(); |
| 1532 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1533 | SPIRVID RID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1534 | |
| 1535 | switch (Ty->getTypeID()) { |
| 1536 | default: { |
| 1537 | Ty->print(errs()); |
| 1538 | llvm_unreachable("Unsupported type???"); |
| 1539 | break; |
| 1540 | } |
| 1541 | case Type::MetadataTyID: |
| 1542 | case Type::LabelTyID: { |
| 1543 | // Ignore these types. |
| 1544 | break; |
| 1545 | } |
| 1546 | case Type::PointerTyID: { |
| 1547 | PointerType *PTy = cast<PointerType>(Ty); |
| 1548 | unsigned AddrSpace = PTy->getAddressSpace(); |
| 1549 | |
| 1550 | if (AddrSpace != AddressSpace::UniformConstant) { |
| 1551 | auto PointeeTy = PTy->getElementType(); |
| 1552 | if (PointeeTy->isStructTy() && |
| 1553 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1554 | // TODO(sjw): assert always an image? |
| 1555 | RID = getSPIRVType(PointeeTy); |
| 1556 | break; |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | // For the purposes of our Vulkan SPIR-V type system, constant and global |
| 1561 | // are conflated. |
| 1562 | if (AddressSpace::Constant == AddrSpace) { |
| 1563 | if (!clspv::Option::ConstantArgsInUniformBuffer()) { |
| 1564 | AddrSpace = AddressSpace::Global; |
| 1565 | // Check to see if we already created this type (for instance, if we |
| 1566 | // had a constant <type>* and a global <type>*, the type would be |
| 1567 | // created by one of these types, and shared by both). |
| 1568 | auto GlobalTy = PTy->getPointerElementType()->getPointerTo(AddrSpace); |
| 1569 | if (0 < TypeMap.count(GlobalTy)) { |
| 1570 | RID = TypeMap[GlobalTy]; |
| 1571 | break; |
| 1572 | } |
| 1573 | } |
| 1574 | } else if (AddressSpace::Global == AddrSpace) { |
| 1575 | if (!clspv::Option::ConstantArgsInUniformBuffer()) { |
| 1576 | AddrSpace = AddressSpace::Constant; |
| 1577 | |
| 1578 | // Check to see if we already created this type (for instance, if we |
| 1579 | // had a constant <type>* and a global <type>*, the type would be |
| 1580 | // created by one of these types, and shared by both). |
| 1581 | auto ConstantTy = PTy->getPointerElementType()->getPointerTo(AddrSpace); |
| 1582 | if (0 < TypeMap.count(ConstantTy)) { |
| 1583 | RID = TypeMap[ConstantTy]; |
| 1584 | break; |
| 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | // |
| 1590 | // Generate OpTypePointer. |
| 1591 | // |
| 1592 | |
| 1593 | // OpTypePointer |
| 1594 | // Ops[0] = Storage Class |
| 1595 | // Ops[1] = Element Type ID |
| 1596 | SPIRVOperandVec Ops; |
| 1597 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1598 | Ops << GetStorageClass(AddrSpace) << PTy->getElementType(); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1599 | |
| 1600 | RID = addSPIRVInst<kTypes>(spv::OpTypePointer, Ops); |
| 1601 | break; |
| 1602 | } |
| 1603 | case Type::StructTyID: { |
| 1604 | StructType *STy = cast<StructType>(Ty); |
| 1605 | |
| 1606 | // Handle sampler type. |
| 1607 | if (STy->isOpaque()) { |
| 1608 | if (STy->getName().equals("opencl.sampler_t")) { |
| 1609 | // |
| 1610 | // Generate OpTypeSampler |
| 1611 | // |
| 1612 | // Empty Ops. |
| 1613 | |
| 1614 | RID = addSPIRVInst<kTypes>(spv::OpTypeSampler); |
| 1615 | break; |
| 1616 | } else if (STy->getName().startswith("opencl.image1d_ro_t") || |
| 1617 | STy->getName().startswith("opencl.image1d_wo_t") || |
| 1618 | STy->getName().startswith("opencl.image1d_array_ro_t") || |
| 1619 | STy->getName().startswith("opencl.image1d_array_wo_t") || |
| 1620 | STy->getName().startswith("opencl.image2d_ro_t") || |
| 1621 | STy->getName().startswith("opencl.image2d_wo_t") || |
| 1622 | STy->getName().startswith("opencl.image2d_array_ro_t") || |
| 1623 | STy->getName().startswith("opencl.image2d_array_wo_t") || |
| 1624 | STy->getName().startswith("opencl.image3d_ro_t") || |
| 1625 | STy->getName().startswith("opencl.image3d_wo_t")) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1626 | |
| 1627 | if (STy->getName().contains("_wo_t")) { |
| 1628 | addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
| 1629 | } |
| 1630 | if (STy->getName().startswith("opencl.image1d_")) { |
| 1631 | if (STy->getName().contains(".sampled")) |
| 1632 | addCapability(spv::CapabilitySampled1D); |
| 1633 | else |
| 1634 | addCapability(spv::CapabilityImage1D); |
| 1635 | } |
| 1636 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1637 | // |
| 1638 | // Generate OpTypeImage |
| 1639 | // |
| 1640 | // Ops[0] = Sampled Type ID |
| 1641 | // Ops[1] = Dim ID |
| 1642 | // Ops[2] = Depth (Literal Number) |
| 1643 | // Ops[3] = Arrayed (Literal Number) |
| 1644 | // Ops[4] = MS (Literal Number) |
| 1645 | // Ops[5] = Sampled (Literal Number) |
| 1646 | // Ops[6] = Image Format ID |
| 1647 | // |
| 1648 | SPIRVOperandVec Ops; |
| 1649 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1650 | SPIRVID SampledTyID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1651 | if (STy->getName().contains(".float")) { |
| 1652 | SampledTyID = getSPIRVType(Type::getFloatTy(Ty->getContext())); |
| 1653 | } else if (STy->getName().contains(".uint")) { |
| 1654 | SampledTyID = getSPIRVType(Type::getInt32Ty(Ty->getContext())); |
| 1655 | } else if (STy->getName().contains(".int")) { |
| 1656 | // Generate a signed 32-bit integer if necessary. |
| 1657 | if (int32ID == 0) { |
| 1658 | SPIRVOperandVec intOps; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1659 | intOps << 32 << 1; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1660 | int32ID = addSPIRVInst<kTypes>(spv::OpTypeInt, intOps); |
| 1661 | } |
| 1662 | SampledTyID = int32ID; |
| 1663 | |
| 1664 | // Generate a vec4 of the signed int if necessary. |
| 1665 | if (v4int32ID == 0) { |
| 1666 | SPIRVOperandVec vecOps; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1667 | vecOps << int32ID << 4; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1668 | v4int32ID = addSPIRVInst<kTypes>(spv::OpTypeVector, vecOps); |
| 1669 | } |
| 1670 | } else { |
| 1671 | // This was likely an UndefValue. |
| 1672 | SampledTyID = getSPIRVType(Type::getFloatTy(Ty->getContext())); |
| 1673 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1674 | Ops << SampledTyID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1675 | |
| 1676 | spv::Dim DimID = spv::Dim2D; |
| 1677 | if (STy->getName().startswith("opencl.image1d_ro_t") || |
| 1678 | STy->getName().startswith("opencl.image1d_wo_t") || |
| 1679 | STy->getName().startswith("opencl.image1d_array_ro_t") || |
| 1680 | STy->getName().startswith("opencl.image1d_array_wo_t")) { |
| 1681 | DimID = spv::Dim1D; |
| 1682 | } else if (STy->getName().startswith("opencl.image3d_ro_t") || |
| 1683 | STy->getName().startswith("opencl.image3d_wo_t")) { |
| 1684 | DimID = spv::Dim3D; |
| 1685 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1686 | Ops << DimID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1687 | |
| 1688 | // TODO: Set up Depth. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1689 | Ops << 0; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1690 | |
| 1691 | uint32_t arrayed = STy->getName().contains("_array_") ? 1 : 0; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1692 | Ops << arrayed; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1693 | |
| 1694 | // TODO: Set up MS. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1695 | Ops << 0; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1696 | |
| 1697 | // Set up Sampled. |
| 1698 | // |
| 1699 | // From Spec |
| 1700 | // |
| 1701 | // 0 indicates this is only known at run time, not at compile time |
| 1702 | // 1 indicates will be used with sampler |
| 1703 | // 2 indicates will be used without a sampler (a storage image) |
| 1704 | uint32_t Sampled = 1; |
| 1705 | if (!STy->getName().contains(".sampled")) { |
| 1706 | Sampled = 2; |
| 1707 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1708 | Ops << Sampled; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1709 | |
| 1710 | // TODO: Set up Image Format. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1711 | Ops << spv::ImageFormatUnknown; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1712 | |
| 1713 | RID = addSPIRVInst<kTypes>(spv::OpTypeImage, Ops); |
| 1714 | |
| 1715 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1716 | Ops << RID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1717 | |
| 1718 | getImageTypeMap()[Ty] = |
| 1719 | addSPIRVInst<kTypes>(spv::OpTypeSampledImage, Ops); |
| 1720 | break; |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | // |
| 1725 | // Generate OpTypeStruct |
| 1726 | // |
| 1727 | // Ops[0] ... Ops[n] = Member IDs |
| 1728 | SPIRVOperandVec Ops; |
| 1729 | |
| 1730 | for (auto *EleTy : STy->elements()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1731 | Ops << EleTy; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | RID = addSPIRVInst<kTypes>(spv::OpTypeStruct, Ops); |
| 1735 | |
| 1736 | // Generate OpMemberDecorate. |
| 1737 | if (TypesNeedingLayout.idFor(STy)) { |
| 1738 | for (unsigned MemberIdx = 0; MemberIdx < STy->getNumElements(); |
| 1739 | MemberIdx++) { |
| 1740 | // Ops[0] = Structure Type ID |
| 1741 | // Ops[1] = Member Index(Literal Number) |
| 1742 | // Ops[2] = Decoration (Offset) |
| 1743 | // Ops[3] = Byte Offset (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1744 | const auto ByteOffset = |
| 1745 | GetExplicitLayoutStructMemberOffset(STy, MemberIdx, DL); |
| 1746 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1747 | Ops.clear(); |
| 1748 | Ops << RID << MemberIdx << spv::DecorationOffset << ByteOffset; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1749 | |
| 1750 | addSPIRVInst<kAnnotations>(spv::OpMemberDecorate, Ops); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | // Generate OpDecorate. |
| 1755 | if (StructTypesNeedingBlock.idFor(STy)) { |
| 1756 | Ops.clear(); |
| 1757 | // Use Block decorations with StorageBuffer storage class. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1758 | Ops << RID << spv::DecorationBlock; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1759 | |
| 1760 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 1761 | } |
| 1762 | break; |
| 1763 | } |
| 1764 | case Type::IntegerTyID: { |
| 1765 | uint32_t BitWidth = static_cast<uint32_t>(Ty->getPrimitiveSizeInBits()); |
| 1766 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1767 | if (clspv::Option::Int8Support() && BitWidth == 8) { |
| 1768 | addCapability(spv::CapabilityInt8); |
| 1769 | } else if (BitWidth == 16) { |
| 1770 | addCapability(spv::CapabilityInt16); |
| 1771 | } else if (BitWidth == 64) { |
| 1772 | addCapability(spv::CapabilityInt64); |
| 1773 | } |
| 1774 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1775 | if (BitWidth == 1) { |
| 1776 | RID = addSPIRVInst<kTypes>(spv::OpTypeBool); |
| 1777 | } else { |
| 1778 | if (!clspv::Option::Int8Support() && BitWidth == 8) { |
| 1779 | // i8 is added to TypeMap as i32. |
| 1780 | RID = getSPIRVType(Type::getIntNTy(Ty->getContext(), 32)); |
| 1781 | } else { |
| 1782 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1783 | Ops << BitWidth << 0 /* not signed */; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1784 | RID = addSPIRVInst<kTypes>(spv::OpTypeInt, Ops); |
| 1785 | } |
| 1786 | } |
| 1787 | break; |
| 1788 | } |
| 1789 | case Type::HalfTyID: |
| 1790 | case Type::FloatTyID: |
| 1791 | case Type::DoubleTyID: { |
| 1792 | uint32_t BitWidth = static_cast<uint32_t>(Ty->getPrimitiveSizeInBits()); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1793 | if (BitWidth == 16) { |
| 1794 | addCapability(spv::CapabilityFloat16); |
| 1795 | } else if (BitWidth == 64) { |
| 1796 | addCapability(spv::CapabilityFloat64); |
| 1797 | } |
| 1798 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1799 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1800 | Ops << BitWidth; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1801 | |
| 1802 | RID = addSPIRVInst<kTypes>(spv::OpTypeFloat, Ops); |
| 1803 | break; |
| 1804 | } |
| 1805 | case Type::ArrayTyID: { |
| 1806 | ArrayType *ArrTy = cast<ArrayType>(Ty); |
| 1807 | const uint64_t Length = ArrTy->getArrayNumElements(); |
| 1808 | if (Length == 0) { |
| 1809 | // By convention, map it to a RuntimeArray. |
| 1810 | |
| 1811 | Type *EleTy = ArrTy->getArrayElementType(); |
| 1812 | |
| 1813 | // |
| 1814 | // Generate OpTypeRuntimeArray. |
| 1815 | // |
| 1816 | // OpTypeRuntimeArray |
| 1817 | // Ops[0] = Element Type ID |
| 1818 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1819 | Ops << EleTy; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1820 | |
| 1821 | RID = addSPIRVInst<kTypes>(spv::OpTypeRuntimeArray, Ops); |
| 1822 | |
| 1823 | if (Hack_generate_runtime_array_stride_early) { |
| 1824 | // Generate OpDecorate. |
| 1825 | |
| 1826 | // Ops[0] = Target ID |
| 1827 | // Ops[1] = Decoration (ArrayStride) |
| 1828 | // Ops[2] = Stride Number(Literal Number) |
| 1829 | Ops.clear(); |
| 1830 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1831 | Ops << RID << spv::DecorationArrayStride |
| 1832 | << static_cast<uint32_t>(GetTypeAllocSize(EleTy, DL)); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1833 | |
| 1834 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 1835 | } |
| 1836 | |
| 1837 | } else { |
| 1838 | |
| 1839 | // |
| 1840 | // Generate OpConstant and OpTypeArray. |
| 1841 | // |
| 1842 | |
| 1843 | // |
| 1844 | // Generate OpConstant for array length. |
| 1845 | // |
| 1846 | // Add constant for length to constant list. |
| 1847 | Constant *CstLength = |
| 1848 | ConstantInt::get(Type::getInt32Ty(module->getContext()), Length); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1849 | |
| 1850 | // Remember to generate ArrayStride later |
| 1851 | getTypesNeedingArrayStride().insert(Ty); |
| 1852 | |
| 1853 | // |
| 1854 | // Generate OpTypeArray. |
| 1855 | // |
| 1856 | // Ops[0] = Element Type ID |
| 1857 | // Ops[1] = Array Length Constant ID |
| 1858 | SPIRVOperandVec Ops; |
| 1859 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1860 | Ops << ArrTy->getElementType() << CstLength; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1861 | |
| 1862 | RID = addSPIRVInst<kTypes>(spv::OpTypeArray, Ops); |
| 1863 | } |
| 1864 | break; |
| 1865 | } |
| 1866 | case Type::FixedVectorTyID: { |
| 1867 | auto VecTy = cast<VectorType>(Ty); |
| 1868 | // <4 x i8> is changed to i32 if i8 is not generally supported. |
| 1869 | if (!clspv::Option::Int8Support() && |
| 1870 | VecTy->getElementType() == Type::getInt8Ty(module->getContext())) { |
| 1871 | if (VecTy->getNumElements() == 4) { |
| 1872 | RID = getSPIRVType(VecTy->getElementType()); |
| 1873 | break; |
| 1874 | } else { |
| 1875 | Ty->print(errs()); |
| 1876 | llvm_unreachable("Support above i8 vector type"); |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | // Ops[0] = Component Type ID |
| 1881 | // Ops[1] = Component Count (Literal Number) |
| 1882 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1883 | Ops << VecTy->getElementType() << VecTy->getNumElements(); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1884 | |
| 1885 | RID = addSPIRVInst<kTypes>(spv::OpTypeVector, Ops); |
| 1886 | break; |
| 1887 | } |
| 1888 | case Type::VoidTyID: { |
| 1889 | RID = addSPIRVInst<kTypes>(spv::OpTypeVoid); |
| 1890 | break; |
| 1891 | } |
| 1892 | case Type::FunctionTyID: { |
| 1893 | // Generate SPIRV instruction for function type. |
| 1894 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 1895 | |
| 1896 | // Ops[0] = Return Type ID |
| 1897 | // Ops[1] ... Ops[n] = Parameter Type IDs |
| 1898 | SPIRVOperandVec Ops; |
| 1899 | |
| 1900 | // Find SPIRV instruction for return type |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1901 | Ops << FTy->getReturnType(); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1902 | |
| 1903 | // Find SPIRV instructions for parameter types |
| 1904 | for (unsigned k = 0; k < FTy->getNumParams(); k++) { |
| 1905 | // Find SPIRV instruction for parameter type. |
| 1906 | auto ParamTy = FTy->getParamType(k); |
| 1907 | if (ParamTy->isPointerTy()) { |
| 1908 | auto PointeeTy = ParamTy->getPointerElementType(); |
| 1909 | if (PointeeTy->isStructTy() && |
| 1910 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1911 | ParamTy = PointeeTy; |
| 1912 | } |
| 1913 | } |
| 1914 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1915 | Ops << ParamTy; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | RID = addSPIRVInst<kTypes>(spv::OpTypeFunction, Ops); |
| 1919 | break; |
| 1920 | } |
| 1921 | } |
| 1922 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1923 | if (RID.isValid()) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1924 | TypeMap[Ty] = RID; |
| 1925 | } |
| 1926 | return RID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1927 | } |
| 1928 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1929 | void SPIRVProducerPass::GenerateSPIRVTypes() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1930 | for (Type *Ty : getTypeList()) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1931 | getSPIRVType(Ty); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 1932 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1933 | } |
| 1934 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1935 | SPIRVID SPIRVProducerPass::getSPIRVConstant(Constant *Cst) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1936 | ValueMapType &VMap = getValueMap(); |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 1937 | const bool hack_undef = clspv::Option::HackUndef(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1938 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1939 | SPIRVID RID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1940 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1941 | // |
| 1942 | // Generate OpConstant. |
| 1943 | // |
| 1944 | // Ops[0] = Result Type ID |
| 1945 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 1946 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1947 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1948 | Ops << Cst->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1949 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1950 | std::vector<uint32_t> LiteralNum; |
| 1951 | spv::Op Opcode = spv::OpNop; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1952 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1953 | if (isa<UndefValue>(Cst)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1954 | // Ops[0] = Result Type ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1955 | Opcode = spv::OpUndef; |
| 1956 | if (hack_undef && IsTypeNullable(Cst->getType())) { |
| 1957 | Opcode = spv::OpConstantNull; |
| 1958 | } |
| 1959 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Cst)) { |
| 1960 | unsigned BitWidth = CI->getBitWidth(); |
| 1961 | if (BitWidth == 1) { |
| 1962 | // If the bitwidth of constant is 1, generate OpConstantTrue or |
| 1963 | // OpConstantFalse. |
| 1964 | if (CI->getZExtValue()) { |
| 1965 | // Ops[0] = Result Type ID |
| 1966 | Opcode = spv::OpConstantTrue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1967 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1968 | // Ops[0] = Result Type ID |
| 1969 | Opcode = spv::OpConstantFalse; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1970 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1971 | } else { |
| 1972 | auto V = CI->getZExtValue(); |
| 1973 | LiteralNum.push_back(V & 0xFFFFFFFF); |
| 1974 | |
| 1975 | if (BitWidth > 32) { |
| 1976 | LiteralNum.push_back(V >> 32); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1980 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 1981 | Ops << LiteralNum; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1982 | } |
| 1983 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Cst)) { |
| 1984 | uint64_t FPVal = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 1985 | Type *CFPTy = CFP->getType(); |
| 1986 | if (CFPTy->isFloatTy()) { |
| 1987 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
| 1988 | } else if (CFPTy->isDoubleTy()) { |
| 1989 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
| 1990 | LiteralNum.push_back(FPVal >> 32); |
| 1991 | } else if (CFPTy->isHalfTy()) { |
| 1992 | LiteralNum.push_back(FPVal & 0xFFFF); |
| 1993 | } else { |
| 1994 | CFPTy->print(errs()); |
| 1995 | llvm_unreachable("Implement this ConstantFP Type"); |
| 1996 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1997 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1998 | Opcode = spv::OpConstant; |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 1999 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2000 | Ops << LiteralNum; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2001 | } else if (isa<ConstantDataSequential>(Cst) && |
| 2002 | cast<ConstantDataSequential>(Cst)->isString()) { |
| 2003 | Cst->print(errs()); |
| 2004 | llvm_unreachable("Implement this Constant"); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2005 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2006 | } else if (const ConstantDataSequential *CDS = |
| 2007 | dyn_cast<ConstantDataSequential>(Cst)) { |
| 2008 | // Let's convert <4 x i8> constant to int constant specially. |
| 2009 | // This case occurs when all the values are specified as constant |
| 2010 | // ints. |
| 2011 | Type *CstTy = Cst->getType(); |
| 2012 | if (is4xi8vec(CstTy)) { |
| 2013 | LLVMContext &Context = CstTy->getContext(); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2014 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2015 | // |
| 2016 | // Generate OpConstant with OpTypeInt 32 0. |
| 2017 | // |
| 2018 | uint32_t IntValue = 0; |
| 2019 | for (unsigned k = 0; k < 4; k++) { |
| 2020 | const uint64_t Val = CDS->getElementAsInteger(k); |
| 2021 | IntValue = (IntValue << 8) | (Val & 0xffu); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2022 | } |
| 2023 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2024 | Type *i32 = Type::getInt32Ty(Context); |
| 2025 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
| 2026 | RID = getSPIRVValue(CstInt); |
| 2027 | } else { |
| 2028 | |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2029 | // A normal constant-data-sequential case. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2030 | for (unsigned k = 0; k < CDS->getNumElements(); k++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2031 | Ops << CDS->getElementAsConstant(k); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | Opcode = spv::OpConstantComposite; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2035 | } |
| 2036 | } else if (const ConstantAggregate *CA = dyn_cast<ConstantAggregate>(Cst)) { |
| 2037 | // Let's convert <4 x i8> constant to int constant specially. |
| 2038 | // This case occurs when at least one of the values is an undef. |
| 2039 | Type *CstTy = Cst->getType(); |
| 2040 | if (is4xi8vec(CstTy)) { |
| 2041 | LLVMContext &Context = CstTy->getContext(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2042 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2043 | // |
| 2044 | // Generate OpConstant with OpTypeInt 32 0. |
| 2045 | // |
| 2046 | uint32_t IntValue = 0; |
| 2047 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); |
| 2048 | I != E; ++I) { |
| 2049 | uint64_t Val = 0; |
| 2050 | const Value *CV = *I; |
| 2051 | if (auto *CI2 = dyn_cast<ConstantInt>(CV)) { |
| 2052 | Val = CI2->getZExtValue(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2053 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2054 | IntValue = (IntValue << 8) | (Val & 0xffu); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2055 | } |
| 2056 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2057 | Type *i32 = Type::getInt32Ty(Context); |
| 2058 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
| 2059 | RID = getSPIRVValue(CstInt); |
| 2060 | } else { |
| 2061 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2062 | // We use a constant composite in SPIR-V for our constant aggregate in |
| 2063 | // LLVM. |
| 2064 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2065 | |
| 2066 | for (unsigned k = 0; k < CA->getNumOperands(); k++) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2067 | // And add an operand to the composite we are constructing |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2068 | Ops << CA->getAggregateElement(k); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2069 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2070 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2071 | } else if (Cst->isNullValue()) { |
| 2072 | Opcode = spv::OpConstantNull; |
| 2073 | } else { |
| 2074 | Cst->print(errs()); |
| 2075 | llvm_unreachable("Unsupported Constant???"); |
| 2076 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2077 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2078 | if (Opcode == spv::OpConstantNull && Cst->getType()->isPointerTy()) { |
| 2079 | // Null pointer requires variable pointers. |
| 2080 | setVariablePointersCapabilities(Cst->getType()->getPointerAddressSpace()); |
| 2081 | } |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2082 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2083 | if (RID == 0) { |
| 2084 | RID = addSPIRVInst<kConstants>(Opcode, Ops); |
| 2085 | } |
| 2086 | |
| 2087 | VMap[Cst] = RID; |
| 2088 | |
| 2089 | return RID; |
| 2090 | } |
| 2091 | |
| 2092 | SPIRVID SPIRVProducerPass::getSPIRVValue(Value *V) { |
| 2093 | auto II = ValueMap.find(V); |
| 2094 | if (II != ValueMap.end()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2095 | assert(II->second.isValid()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2096 | return II->second; |
| 2097 | } |
| 2098 | if (Constant *Cst = dyn_cast<Constant>(V)) { |
| 2099 | return getSPIRVConstant(Cst); |
| 2100 | } else { |
| 2101 | llvm_unreachable("Variable not found"); |
| 2102 | } |
| 2103 | } |
| 2104 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2105 | void SPIRVProducerPass::GenerateSamplers() { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2106 | auto &sampler_map = getSamplerMap(); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2107 | SamplerLiteralToIDMap.clear(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2108 | DenseMap<unsigned, unsigned> SamplerLiteralToDescriptorSetMap; |
| 2109 | DenseMap<unsigned, unsigned> SamplerLiteralToBindingMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2110 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2111 | // We might have samplers in the sampler map that are not used |
| 2112 | // in the translation unit. We need to allocate variables |
| 2113 | // for them and bindings too. |
| 2114 | DenseSet<unsigned> used_bindings; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2115 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2116 | auto *var_fn = module->getFunction(clspv::LiteralSamplerFunction()); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2117 | // Return if there are no literal samplers. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2118 | if (!var_fn) |
| 2119 | return; |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2120 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2121 | for (auto user : var_fn->users()) { |
| 2122 | // Populate SamplerLiteralToDescriptorSetMap and |
| 2123 | // SamplerLiteralToBindingMap. |
| 2124 | // |
| 2125 | // Look for calls like |
| 2126 | // call %opencl.sampler_t addrspace(2)* |
| 2127 | // @clspv.sampler.var.literal( |
| 2128 | // i32 descriptor, |
| 2129 | // i32 binding, |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2130 | // i32 (index-into-sampler-map|sampler_mask)) |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2131 | if (auto *call = dyn_cast<CallInst>(user)) { |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2132 | const auto third_param = static_cast<unsigned>( |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2133 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2134 | auto sampler_value = third_param; |
| 2135 | if (clspv::Option::UseSamplerMap()) { |
| 2136 | if (third_param >= sampler_map.size()) { |
| 2137 | errs() << "Out of bounds index to sampler map: " << third_param; |
| 2138 | llvm_unreachable("bad sampler init: out of bounds"); |
| 2139 | } |
| 2140 | sampler_value = sampler_map[third_param].first; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2141 | } |
| 2142 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2143 | const auto descriptor_set = static_cast<unsigned>( |
| 2144 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 2145 | const auto binding = static_cast<unsigned>( |
| 2146 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 2147 | |
| 2148 | SamplerLiteralToDescriptorSetMap[sampler_value] = descriptor_set; |
| 2149 | SamplerLiteralToBindingMap[sampler_value] = binding; |
| 2150 | used_bindings.insert(binding); |
| 2151 | } |
| 2152 | } |
| 2153 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2154 | DenseSet<size_t> seen; |
| 2155 | for (auto user : var_fn->users()) { |
| 2156 | if (!isa<CallInst>(user)) |
| 2157 | continue; |
| 2158 | |
| 2159 | auto call = cast<CallInst>(user); |
| 2160 | const unsigned third_param = static_cast<unsigned>( |
| 2161 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 2162 | |
| 2163 | // Already allocated a variable for this value. |
| 2164 | if (!seen.insert(third_param).second) |
| 2165 | continue; |
| 2166 | |
| 2167 | auto sampler_value = third_param; |
| 2168 | if (clspv::Option::UseSamplerMap()) { |
| 2169 | sampler_value = sampler_map[third_param].first; |
| 2170 | } |
| 2171 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2172 | // Generate OpVariable. |
| 2173 | // |
| 2174 | // GIDOps[0] : Result Type ID |
| 2175 | // GIDOps[1] : Storage Class |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2176 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2177 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2178 | Ops << SamplerTy << spv::StorageClassUniformConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2179 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2180 | auto sampler_var_id = addSPIRVInst<kGlobalVariables>(spv::OpVariable, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2181 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2182 | SamplerLiteralToIDMap[sampler_value] = sampler_var_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2183 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2184 | unsigned descriptor_set; |
| 2185 | unsigned binding; |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2186 | if (SamplerLiteralToBindingMap.find(sampler_value) == |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2187 | SamplerLiteralToBindingMap.end()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2188 | // This sampler is not actually used. Find the next one. |
| 2189 | for (binding = 0; used_bindings.count(binding); binding++) |
| 2190 | ; |
| 2191 | descriptor_set = 0; // Literal samplers always use descriptor set 0. |
| 2192 | used_bindings.insert(binding); |
| 2193 | } else { |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2194 | descriptor_set = SamplerLiteralToDescriptorSetMap[sampler_value]; |
| 2195 | binding = SamplerLiteralToBindingMap[sampler_value]; |
alan-baker | cff8015 | 2019-06-15 00:38:00 -0400 | [diff] [blame] | 2196 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2197 | version0::DescriptorMapEntry::SamplerData sampler_data = {sampler_value}; |
alan-baker | cff8015 | 2019-06-15 00:38:00 -0400 | [diff] [blame] | 2198 | descriptorMapEntries->emplace_back(std::move(sampler_data), |
| 2199 | descriptor_set, binding); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2200 | } |
| 2201 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2202 | // Ops[0] = Target ID |
| 2203 | // Ops[1] = Decoration (DescriptorSet) |
| 2204 | // Ops[2] = LiteralNumber according to Decoration |
| 2205 | Ops.clear(); |
| 2206 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2207 | Ops << sampler_var_id << spv::DecorationDescriptorSet << descriptor_set; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2208 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2209 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2210 | |
| 2211 | // Ops[0] = Target ID |
| 2212 | // Ops[1] = Decoration (Binding) |
| 2213 | // Ops[2] = LiteralNumber according to Decoration |
| 2214 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2215 | Ops << sampler_var_id << spv::DecorationBinding << binding; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2216 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2217 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2218 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2219 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2220 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2221 | void SPIRVProducerPass::GenerateResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2222 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2223 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2224 | // Generate variables. Make one for each of resource var info object. |
| 2225 | for (auto *info : ModuleOrderedResourceVars) { |
| 2226 | Type *type = info->var_fn->getReturnType(); |
| 2227 | // Remap the address space for opaque types. |
| 2228 | switch (info->arg_kind) { |
| 2229 | case clspv::ArgKind::Sampler: |
| 2230 | case clspv::ArgKind::ReadOnlyImage: |
| 2231 | case clspv::ArgKind::WriteOnlyImage: |
| 2232 | type = PointerType::get(type->getPointerElementType(), |
| 2233 | clspv::AddressSpace::UniformConstant); |
| 2234 | break; |
| 2235 | default: |
| 2236 | break; |
| 2237 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2238 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2239 | const auto sc = GetStorageClassForArgKind(info->arg_kind); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2240 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2241 | Ops << type << sc; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2242 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2243 | info->var_id = addSPIRVInst<kGlobalVariables>(spv::OpVariable, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2244 | |
| 2245 | // Map calls to the variable-builtin-function. |
| 2246 | for (auto &U : info->var_fn->uses()) { |
| 2247 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 2248 | const auto set = unsigned( |
| 2249 | dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue()); |
| 2250 | const auto binding = unsigned( |
| 2251 | dyn_cast<ConstantInt>(call->getOperand(1))->getZExtValue()); |
| 2252 | if (set == info->descriptor_set && binding == info->binding) { |
| 2253 | switch (info->arg_kind) { |
| 2254 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2255 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2256 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 2257 | case clspv::ArgKind::PodUBO: |
| 2258 | case clspv::ArgKind::PodPushConstant: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2259 | // The call maps to the variable directly. |
| 2260 | VMap[call] = info->var_id; |
| 2261 | break; |
| 2262 | case clspv::ArgKind::Sampler: |
| 2263 | case clspv::ArgKind::ReadOnlyImage: |
| 2264 | case clspv::ArgKind::WriteOnlyImage: |
| 2265 | // The call maps to a load we generate later. |
| 2266 | ResourceVarDeferredLoadCalls[call] = info->var_id; |
| 2267 | break; |
| 2268 | default: |
| 2269 | llvm_unreachable("Unhandled arg kind"); |
| 2270 | } |
| 2271 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2272 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2273 | } |
| 2274 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2275 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2276 | // Generate associated decorations. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2277 | SPIRVOperandVec Ops; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2278 | for (auto *info : ModuleOrderedResourceVars) { |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 2279 | // Push constants don't need descriptor set or binding decorations. |
| 2280 | if (info->arg_kind == clspv::ArgKind::PodPushConstant) |
| 2281 | continue; |
| 2282 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2283 | // Decorate with DescriptorSet and Binding. |
| 2284 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2285 | Ops << info->var_id << spv::DecorationDescriptorSet << info->descriptor_set; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2286 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2287 | |
| 2288 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2289 | Ops << info->var_id << spv::DecorationBinding << info->binding; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2290 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2291 | |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2292 | if (info->coherent) { |
| 2293 | // Decorate with Coherent if required for the variable. |
| 2294 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2295 | Ops << info->var_id << spv::DecorationCoherent; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2296 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2297 | } |
| 2298 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2299 | // Generate NonWritable and NonReadable |
| 2300 | switch (info->arg_kind) { |
| 2301 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2302 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2303 | if (info->var_fn->getReturnType()->getPointerAddressSpace() == |
| 2304 | clspv::AddressSpace::Constant) { |
| 2305 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2306 | Ops << info->var_id << spv::DecorationNonWritable; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2307 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2308 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2309 | break; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2310 | case clspv::ArgKind::WriteOnlyImage: |
| 2311 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2312 | Ops << info->var_id << spv::DecorationNonReadable; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2313 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2314 | break; |
| 2315 | default: |
| 2316 | break; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2321 | void SPIRVProducerPass::GeneratePushConstantDescriptorMapEntries() { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2322 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2323 | if (auto GV = module->getGlobalVariable(clspv::PushConstantsVariableName())) { |
| 2324 | auto const &DL = module->getDataLayout(); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2325 | auto MD = GV->getMetadata(clspv::PushConstantsMetadataName()); |
| 2326 | auto STy = cast<StructType>(GV->getValueType()); |
| 2327 | |
| 2328 | for (unsigned i = 0; i < STy->getNumElements(); i++) { |
| 2329 | auto pc = static_cast<clspv::PushConstant>( |
| 2330 | mdconst::extract<ConstantInt>(MD->getOperand(i))->getZExtValue()); |
| 2331 | auto memberType = STy->getElementType(i); |
| 2332 | auto offset = GetExplicitLayoutStructMemberOffset(STy, i, DL); |
| 2333 | unsigned previousOffset = 0; |
| 2334 | if (i > 0) { |
| 2335 | previousOffset = GetExplicitLayoutStructMemberOffset(STy, i - 1, DL); |
| 2336 | } |
| 2337 | auto size = static_cast<uint32_t>(GetTypeSizeInBits(memberType, DL)) / 8; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2338 | assert(isValidExplicitLayout(*module, STy, i, |
| 2339 | spv::StorageClassPushConstant, offset, |
| 2340 | previousOffset)); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2341 | version0::DescriptorMapEntry::PushConstantData data = {pc, offset, size}; |
| 2342 | descriptorMapEntries->emplace_back(std::move(data)); |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2347 | void SPIRVProducerPass::GenerateSpecConstantDescriptorMapEntries() { |
| 2348 | for (auto pair : clspv::GetSpecConstants(module)) { |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 2349 | auto kind = pair.first; |
| 2350 | auto id = pair.second; |
| 2351 | |
| 2352 | // Local memory size is only used for kernel arguments. |
| 2353 | if (kind == SpecConstant::kLocalMemorySize) |
| 2354 | continue; |
| 2355 | |
| 2356 | version0::DescriptorMapEntry::SpecConstantData data = {kind, id}; |
| 2357 | descriptorMapEntries->emplace_back(std::move(data)); |
| 2358 | } |
| 2359 | } |
| 2360 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2361 | void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2362 | ValueMapType &VMap = getValueMap(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2363 | std::vector<SPIRVID> &BuiltinDimVec = getBuiltinDimVec(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2364 | const DataLayout &DL = GV.getParent()->getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2365 | |
| 2366 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 2367 | Type *Ty = GV.getType(); |
| 2368 | PointerType *PTy = cast<PointerType>(Ty); |
| 2369 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2370 | SPIRVID InitializerID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2371 | |
| 2372 | // Workgroup size is handled differently (it goes into a constant) |
| 2373 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 2374 | std::vector<bool> HasMDVec; |
| 2375 | uint32_t PrevXDimCst = 0xFFFFFFFF; |
| 2376 | uint32_t PrevYDimCst = 0xFFFFFFFF; |
| 2377 | uint32_t PrevZDimCst = 0xFFFFFFFF; |
| 2378 | for (Function &Func : *GV.getParent()) { |
| 2379 | if (Func.isDeclaration()) { |
| 2380 | continue; |
| 2381 | } |
| 2382 | |
| 2383 | // We only need to check kernels. |
| 2384 | if (Func.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 2385 | continue; |
| 2386 | } |
| 2387 | |
| 2388 | if (const MDNode *MD = |
| 2389 | dyn_cast<Function>(&Func)->getMetadata("reqd_work_group_size")) { |
| 2390 | uint32_t CurXDimCst = static_cast<uint32_t>( |
| 2391 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 2392 | uint32_t CurYDimCst = static_cast<uint32_t>( |
| 2393 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 2394 | uint32_t CurZDimCst = static_cast<uint32_t>( |
| 2395 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 2396 | |
| 2397 | if (PrevXDimCst == 0xFFFFFFFF && PrevYDimCst == 0xFFFFFFFF && |
| 2398 | PrevZDimCst == 0xFFFFFFFF) { |
| 2399 | PrevXDimCst = CurXDimCst; |
| 2400 | PrevYDimCst = CurYDimCst; |
| 2401 | PrevZDimCst = CurZDimCst; |
| 2402 | } else if (CurXDimCst != PrevXDimCst || CurYDimCst != PrevYDimCst || |
| 2403 | CurZDimCst != PrevZDimCst) { |
| 2404 | llvm_unreachable( |
| 2405 | "reqd_work_group_size must be the same across all kernels"); |
| 2406 | } else { |
| 2407 | continue; |
| 2408 | } |
| 2409 | |
| 2410 | // |
| 2411 | // Generate OpConstantComposite. |
| 2412 | // |
| 2413 | // Ops[0] : Result Type ID |
| 2414 | // Ops[1] : Constant size for x dimension. |
| 2415 | // Ops[2] : Constant size for y dimension. |
| 2416 | // Ops[3] : Constant size for z dimension. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2417 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2418 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2419 | SPIRVID XDimCstID = |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2420 | getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(0))); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2421 | SPIRVID YDimCstID = |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2422 | getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(1))); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2423 | SPIRVID ZDimCstID = |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2424 | getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(2))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2425 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2426 | Ops << Ty->getPointerElementType() << XDimCstID << YDimCstID |
| 2427 | << ZDimCstID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2428 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2429 | InitializerID = |
| 2430 | addSPIRVInst<kGlobalVariables>(spv::OpConstantComposite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2431 | |
| 2432 | HasMDVec.push_back(true); |
| 2433 | } else { |
| 2434 | HasMDVec.push_back(false); |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | // Check all kernels have same definitions for work_group_size. |
| 2439 | bool HasMD = false; |
| 2440 | if (!HasMDVec.empty()) { |
| 2441 | HasMD = HasMDVec[0]; |
| 2442 | for (uint32_t i = 1; i < HasMDVec.size(); i++) { |
| 2443 | if (HasMD != HasMDVec[i]) { |
| 2444 | llvm_unreachable( |
| 2445 | "Kernels should have consistent work group size definition"); |
| 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | // If all kernels do not have metadata for reqd_work_group_size, generate |
| 2451 | // OpSpecConstants for x/y/z dimension. |
Kévin Petit | 21c23c6 | 2020-04-29 01:38:28 +0100 | [diff] [blame] | 2452 | if (!HasMD || clspv::Option::NonUniformNDRangeSupported()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2453 | // |
| 2454 | // Generate OpSpecConstants for x/y/z dimension. |
| 2455 | // |
| 2456 | // Ops[0] : Result Type ID |
| 2457 | // Ops[1] : Constant size for x/y/z dimension (Literal Number). |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2458 | |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 2459 | // Allocate spec constants for workgroup size. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2460 | clspv::AddWorkgroupSpecConstants(module); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 2461 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2462 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2463 | SPIRVID result_type_id = getSPIRVType( |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2464 | dyn_cast<VectorType>(Ty->getPointerElementType())->getElementType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2465 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2466 | // X Dimension |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2467 | Ops << result_type_id << 1; |
| 2468 | SPIRVID XDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2469 | |
| 2470 | // Y Dimension |
| 2471 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2472 | Ops << result_type_id << 1; |
| 2473 | SPIRVID YDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2474 | |
| 2475 | // Z Dimension |
| 2476 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2477 | Ops << result_type_id << 1; |
| 2478 | SPIRVID ZDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2479 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2480 | BuiltinDimVec.push_back(XDimCstID); |
| 2481 | BuiltinDimVec.push_back(YDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2482 | BuiltinDimVec.push_back(ZDimCstID); |
| 2483 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2484 | // |
| 2485 | // Generate OpSpecConstantComposite. |
| 2486 | // |
| 2487 | // Ops[0] : Result Type ID |
| 2488 | // Ops[1] : Constant size for x dimension. |
| 2489 | // Ops[2] : Constant size for y dimension. |
| 2490 | // Ops[3] : Constant size for z dimension. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2491 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2492 | Ops << Ty->getPointerElementType() << XDimCstID << YDimCstID << ZDimCstID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2493 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2494 | InitializerID = |
| 2495 | addSPIRVInst<kConstants>(spv::OpSpecConstantComposite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2496 | } |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2497 | } else if (BuiltinType == spv::BuiltInWorkDim) { |
| 2498 | // 1. Generate a specialization constant with a default of 3. |
| 2499 | // 2. Allocate and annotate a SpecId for the constant. |
| 2500 | // 3. Use the spec constant as the initializer for the variable. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2501 | SPIRVOperandVec Ops; |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2502 | |
| 2503 | // |
| 2504 | // Generate OpSpecConstant. |
| 2505 | // |
| 2506 | // Ops[0] : Result Type ID |
| 2507 | // Ops[1] : Default literal value |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2508 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2509 | Ops << IntegerType::get(GV.getContext(), 32) << 3; |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2510 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2511 | InitializerID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2512 | |
| 2513 | // |
| 2514 | // Generate SpecId decoration. |
| 2515 | // |
| 2516 | // Ops[0] : target |
| 2517 | // Ops[1] : decoration |
| 2518 | // Ops[2] : SpecId |
Alan Baker | 75ccc25 | 2020-04-21 17:11:52 -0400 | [diff] [blame] | 2519 | auto spec_id = AllocateSpecConstant(module, SpecConstant::kWorkDim); |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2520 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2521 | Ops << InitializerID << spv::DecorationSpecId << spec_id; |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2522 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2523 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2524 | } else if (BuiltinType == spv::BuiltInGlobalOffset) { |
| 2525 | // 1. Generate a spec constant with a default of {0, 0, 0}. |
| 2526 | // 2. Allocate and annotate SpecIds for the constants. |
| 2527 | // 3. Use the spec constant as the initializer for the variable. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2528 | SPIRVOperandVec Ops; |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2529 | |
| 2530 | // |
| 2531 | // Generate OpSpecConstant for each dimension. |
| 2532 | // |
| 2533 | // Ops[0] : Result Type ID |
| 2534 | // Ops[1] : Default literal value |
| 2535 | // |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2536 | Ops << IntegerType::get(GV.getContext(), 32) << 0; |
| 2537 | SPIRVID x_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2538 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2539 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2540 | Ops << IntegerType::get(GV.getContext(), 32) << 0; |
| 2541 | SPIRVID y_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2542 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2543 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2544 | Ops << IntegerType::get(GV.getContext(), 32) << 0; |
| 2545 | SPIRVID z_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2546 | |
| 2547 | // |
| 2548 | // Generate SpecId decoration for each dimension. |
| 2549 | // |
| 2550 | // Ops[0] : target |
| 2551 | // Ops[1] : decoration |
| 2552 | // Ops[2] : SpecId |
| 2553 | // |
| 2554 | auto spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetX); |
| 2555 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2556 | Ops << x_id << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2557 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2558 | |
| 2559 | spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetY); |
| 2560 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2561 | Ops << y_id << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2562 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2563 | |
| 2564 | spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetZ); |
| 2565 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2566 | Ops << z_id << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2567 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2568 | |
| 2569 | // |
| 2570 | // Generate OpSpecConstantComposite. |
| 2571 | // |
| 2572 | // Ops[0] : type id |
| 2573 | // Ops[1..n-1] : elements |
| 2574 | // |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2575 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2576 | Ops << GV.getType()->getPointerElementType() << x_id << y_id << z_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2577 | InitializerID = addSPIRVInst<kConstants>(spv::OpSpecConstantComposite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2578 | } |
| 2579 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2580 | // |
| 2581 | // Generate OpVariable. |
| 2582 | // |
| 2583 | // GIDOps[0] : Result Type ID |
| 2584 | // GIDOps[1] : Storage Class |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2585 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2586 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2587 | const auto AS = PTy->getAddressSpace(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2588 | Ops << Ty << GetStorageClass(AS); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2589 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2590 | const bool module_scope_constant_external_init = |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2591 | (AS == AddressSpace::Constant) && GV.hasInitializer() && |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2592 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 2593 | |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 2594 | if (GV.hasInitializer()) { |
| 2595 | auto GVInit = GV.getInitializer(); |
| 2596 | if (!isa<UndefValue>(GVInit) && !module_scope_constant_external_init) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2597 | InitializerID = getSPIRVValue(GVInit); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2598 | } |
| 2599 | } |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 2600 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2601 | if (InitializerID.isValid()) { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2602 | // Emit the ID of the initializer as part of the variable definition. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2603 | Ops << InitializerID; |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 2604 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2605 | SPIRVID var_id = addSPIRVInst<kGlobalVariables>(spv::OpVariable, Ops); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2606 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2607 | VMap[&GV] = var_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2608 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2609 | auto IsOpenCLBuiltin = [](spv::BuiltIn builtin) { |
| 2610 | return builtin == spv::BuiltInWorkDim || |
| 2611 | builtin == spv::BuiltInGlobalOffset; |
| 2612 | }; |
| 2613 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2614 | // If we have a builtin (not an OpenCL builtin). |
| 2615 | if (spv::BuiltInMax != BuiltinType && !IsOpenCLBuiltin(BuiltinType)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2616 | // |
| 2617 | // Generate OpDecorate. |
| 2618 | // |
| 2619 | // DOps[0] = Target ID |
| 2620 | // DOps[1] = Decoration (Builtin) |
| 2621 | // DOps[2] = BuiltIn ID |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2622 | SPIRVID ResultID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2623 | |
| 2624 | // WorkgroupSize is different, we decorate the constant composite that has |
| 2625 | // its value, rather than the variable that we use to access the value. |
| 2626 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 2627 | ResultID = InitializerID; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 2628 | // Save both the value and variable IDs for later. |
| 2629 | WorkgroupSizeValueID = InitializerID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2630 | WorkgroupSizeVarID = getSPIRVValue(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2631 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2632 | ResultID = getSPIRVValue(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2633 | } |
| 2634 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2635 | Ops.clear(); |
| 2636 | Ops << ResultID << spv::DecorationBuiltIn << BuiltinType; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2637 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2638 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2639 | } else if (module_scope_constant_external_init) { |
| 2640 | // This module scope constant is initialized from a storage buffer with data |
| 2641 | // provided by the host at binding 0 of the next descriptor set. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2642 | const uint32_t descriptor_set = TakeDescriptorIndex(module); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2643 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2644 | // Emit the intializer to the descriptor map file. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2645 | // Use "kind,buffer" to indicate storage buffer. We might want to expand |
| 2646 | // that later to other types, like uniform buffer. |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2647 | std::string hexbytes; |
| 2648 | llvm::raw_string_ostream str(hexbytes); |
| 2649 | clspv::ConstantEmitter(DL, str).Emit(GV.getInitializer()); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2650 | version0::DescriptorMapEntry::ConstantData constant_data = {ArgKind::Buffer, |
| 2651 | str.str()}; |
| 2652 | descriptorMapEntries->emplace_back(std::move(constant_data), descriptor_set, |
| 2653 | 0); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2654 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2655 | // OpDecorate %var DescriptorSet <descriptor_set> |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2656 | Ops.clear(); |
| 2657 | Ops << var_id << spv::DecorationDescriptorSet << descriptor_set; |
| 2658 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2659 | |
| 2660 | // OpDecorate %var Binding <binding> |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2661 | Ops.clear(); |
| 2662 | Ops << var_id << spv::DecorationBinding << 0; |
| 2663 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2664 | } |
| 2665 | } |
| 2666 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2667 | void SPIRVProducerPass::GenerateDescriptorMapInfo(Function &F) { |
| 2668 | const auto &DL = module->getDataLayout(); |
David Neto | c5fb524 | 2018-07-30 13:28:31 -0400 | [diff] [blame] | 2669 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 2670 | return; |
| 2671 | } |
Kévin Petit | 717f857 | 2020-04-06 17:31:53 +0100 | [diff] [blame] | 2672 | // Add entries for each kernel |
| 2673 | version0::DescriptorMapEntry::KernelDeclData kernel_decl_data = { |
| 2674 | F.getName().str()}; |
| 2675 | descriptorMapEntries->emplace_back(std::move(kernel_decl_data)); |
| 2676 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2677 | // Gather the list of resources that are used by this function's arguments. |
| 2678 | auto &resource_var_at_index = FunctionToResourceVarsMap[&F]; |
| 2679 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2680 | auto *fty = F.getType()->getPointerElementType(); |
| 2681 | auto *func_ty = dyn_cast<FunctionType>(fty); |
| 2682 | |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 2683 | // If we've clustered POD arguments, then argument details are in metadata. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2684 | // If an argument maps to a resource variable, then get descriptor set and |
| 2685 | // binding from the resoure variable. Other info comes from the metadata. |
alan-baker | ff6c929 | 2020-05-04 08:32:09 -0400 | [diff] [blame] | 2686 | const auto *arg_map = F.getMetadata(clspv::KernelArgMapMetadataName()); |
| 2687 | auto local_spec_id_md = |
| 2688 | module->getNamedMetadata(clspv::LocalSpecIdMetadataName()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2689 | if (arg_map) { |
| 2690 | for (const auto &arg : arg_map->operands()) { |
| 2691 | const MDNode *arg_node = dyn_cast<MDNode>(arg.get()); |
alan-baker | ff6c929 | 2020-05-04 08:32:09 -0400 | [diff] [blame] | 2692 | assert(arg_node->getNumOperands() == 6); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2693 | const auto name = |
| 2694 | dyn_cast<MDString>(arg_node->getOperand(0))->getString(); |
| 2695 | const auto old_index = |
| 2696 | dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue(); |
| 2697 | // Remapped argument index |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2698 | const size_t new_index = static_cast<size_t>( |
| 2699 | dyn_extract<ConstantInt>(arg_node->getOperand(2))->getZExtValue()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2700 | const auto offset = |
| 2701 | dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue(); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2702 | const auto arg_size = |
| 2703 | dyn_extract<ConstantInt>(arg_node->getOperand(4))->getZExtValue(); |
alan-baker | c4579bb | 2020-04-29 14:15:50 -0400 | [diff] [blame] | 2704 | const auto argKind = clspv::GetArgKindFromName( |
| 2705 | dyn_cast<MDString>(arg_node->getOperand(5))->getString().str()); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2706 | |
alan-baker | ff6c929 | 2020-05-04 08:32:09 -0400 | [diff] [blame] | 2707 | // If this is a local memory argument, find the right spec id for this |
| 2708 | // argument. |
| 2709 | int64_t spec_id = -1; |
| 2710 | if (argKind == clspv::ArgKind::Local) { |
| 2711 | for (auto spec_id_arg : local_spec_id_md->operands()) { |
| 2712 | if ((&F == dyn_cast<Function>( |
| 2713 | dyn_cast<ValueAsMetadata>(spec_id_arg->getOperand(0)) |
| 2714 | ->getValue())) && |
| 2715 | (new_index == |
| 2716 | mdconst::extract<ConstantInt>(spec_id_arg->getOperand(1)) |
| 2717 | ->getZExtValue())) { |
| 2718 | spec_id = mdconst::extract<ConstantInt>(spec_id_arg->getOperand(2)) |
| 2719 | ->getSExtValue(); |
| 2720 | break; |
| 2721 | } |
| 2722 | } |
| 2723 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2724 | uint32_t descriptor_set = 0; |
| 2725 | uint32_t binding = 0; |
| 2726 | version0::DescriptorMapEntry::KernelArgData kernel_data = { |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 2727 | F.getName().str(), name.str(), static_cast<uint32_t>(old_index), |
| 2728 | argKind, static_cast<uint32_t>(spec_id), |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2729 | // This will be set below for pointer-to-local args. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2730 | 0, static_cast<uint32_t>(offset), static_cast<uint32_t>(arg_size)}; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2731 | if (spec_id > 0) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2732 | kernel_data.local_element_size = static_cast<uint32_t>(GetTypeAllocSize( |
| 2733 | func_ty->getParamType(unsigned(new_index))->getPointerElementType(), |
| 2734 | DL)); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2735 | } else { |
| 2736 | auto *info = resource_var_at_index[new_index]; |
| 2737 | assert(info); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2738 | descriptor_set = info->descriptor_set; |
| 2739 | binding = info->binding; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2740 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2741 | descriptorMapEntries->emplace_back(std::move(kernel_data), descriptor_set, |
| 2742 | binding); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2743 | } |
| 2744 | } else { |
| 2745 | // There is no argument map. |
| 2746 | // Take descriptor info from the resource variable calls. |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2747 | // Take argument name and size from the arguments list. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2748 | |
| 2749 | SmallVector<Argument *, 4> arguments; |
| 2750 | for (auto &arg : F.args()) { |
| 2751 | arguments.push_back(&arg); |
| 2752 | } |
| 2753 | |
| 2754 | unsigned arg_index = 0; |
| 2755 | for (auto *info : resource_var_at_index) { |
| 2756 | if (info) { |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2757 | auto arg = arguments[arg_index]; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2758 | unsigned arg_size = 0; |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 2759 | if (info->arg_kind == clspv::ArgKind::Pod || |
| 2760 | info->arg_kind == clspv::ArgKind::PodUBO || |
| 2761 | info->arg_kind == clspv::ArgKind::PodPushConstant) { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2762 | arg_size = static_cast<uint32_t>(DL.getTypeStoreSize(arg->getType())); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2765 | // Local pointer arguments are unused in this case. Offset is always |
| 2766 | // zero. |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2767 | version0::DescriptorMapEntry::KernelArgData kernel_data = { |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 2768 | F.getName().str(), |
| 2769 | arg->getName().str(), |
| 2770 | arg_index, |
alan-baker | c4579bb | 2020-04-29 14:15:50 -0400 | [diff] [blame] | 2771 | info->arg_kind, |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 2772 | 0, |
| 2773 | 0, |
| 2774 | 0, |
| 2775 | arg_size}; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2776 | descriptorMapEntries->emplace_back(std::move(kernel_data), |
| 2777 | info->descriptor_set, info->binding); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2778 | } |
| 2779 | arg_index++; |
| 2780 | } |
| 2781 | // Generate mappings for pointer-to-local arguments. |
| 2782 | for (arg_index = 0; arg_index < arguments.size(); ++arg_index) { |
| 2783 | Argument *arg = arguments[arg_index]; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 2784 | auto where = LocalArgSpecIds.find(arg); |
| 2785 | if (where != LocalArgSpecIds.end()) { |
| 2786 | auto &local_arg_info = LocalSpecIdInfoMap[where->second]; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2787 | // Pod arguments members are unused in this case. |
| 2788 | version0::DescriptorMapEntry::KernelArgData kernel_data = { |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 2789 | F.getName().str(), |
| 2790 | arg->getName().str(), |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2791 | arg_index, |
| 2792 | ArgKind::Local, |
| 2793 | static_cast<uint32_t>(local_arg_info.spec_id), |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 2794 | static_cast<uint32_t>( |
| 2795 | GetTypeAllocSize(local_arg_info.elem_type, DL)), |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2796 | 0, |
| 2797 | 0}; |
| 2798 | // Pointer-to-local arguments do not utilize descriptor set and binding. |
| 2799 | descriptorMapEntries->emplace_back(std::move(kernel_data), 0, 0); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2805 | void SPIRVProducerPass::GenerateFuncPrologue(Function &F) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2806 | ValueMapType &VMap = getValueMap(); |
| 2807 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2808 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 2809 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 2810 | |
| 2811 | FunctionType *FTy = F.getFunctionType(); |
| 2812 | |
| 2813 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2814 | // Generate OPFunction. |
| 2815 | // |
| 2816 | |
| 2817 | // FOps[0] : Result Type ID |
| 2818 | // FOps[1] : Function Control |
| 2819 | // FOps[2] : Function Type ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2820 | SPIRVOperandVec FOps; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2821 | |
| 2822 | // Find SPIRV instruction for return type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2823 | FOps << FTy->getReturnType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2824 | |
| 2825 | // Check function attributes for SPIRV Function Control. |
| 2826 | uint32_t FuncControl = spv::FunctionControlMaskNone; |
| 2827 | if (F.hasFnAttribute(Attribute::AlwaysInline)) { |
| 2828 | FuncControl |= spv::FunctionControlInlineMask; |
| 2829 | } |
| 2830 | if (F.hasFnAttribute(Attribute::NoInline)) { |
| 2831 | FuncControl |= spv::FunctionControlDontInlineMask; |
| 2832 | } |
| 2833 | // TODO: Check llvm attribute for Function Control Pure. |
| 2834 | if (F.hasFnAttribute(Attribute::ReadOnly)) { |
| 2835 | FuncControl |= spv::FunctionControlPureMask; |
| 2836 | } |
| 2837 | // TODO: Check llvm attribute for Function Control Const. |
| 2838 | if (F.hasFnAttribute(Attribute::ReadNone)) { |
| 2839 | FuncControl |= spv::FunctionControlConstMask; |
| 2840 | } |
| 2841 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2842 | FOps << FuncControl; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2843 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2844 | SPIRVID FTyID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2845 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 2846 | SmallVector<Type *, 4> NewFuncParamTys; |
| 2847 | FunctionType *NewFTy = |
| 2848 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2849 | FTyID = getSPIRVType(NewFTy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2850 | } else { |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 2851 | // Handle regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2852 | if (GlobalConstFuncTyMap.count(FTy)) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2853 | FTyID = getSPIRVType(GlobalConstFuncTyMap[FTy].first); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2854 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2855 | FTyID = getSPIRVType(FTy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2856 | } |
| 2857 | } |
| 2858 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2859 | FOps << FTyID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2860 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2861 | // Generate SPIRV instruction for function. |
| 2862 | SPIRVID FID = addSPIRVInst(spv::OpFunction, FOps); |
| 2863 | VMap[&F] = FID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2864 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2865 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 2866 | EntryPoints.push_back(std::make_pair(&F, FID)); |
| 2867 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2868 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 2869 | if (clspv::Option::ShowIDs()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2870 | errs() << "Function " << F.getName() << " is " << FID.get() << "\n"; |
David Neto | b05675d | 2018-02-16 12:37:49 -0500 | [diff] [blame] | 2871 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2872 | |
| 2873 | // |
| 2874 | // Generate OpFunctionParameter for Normal function. |
| 2875 | // |
| 2876 | |
| 2877 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2878 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2879 | // Iterate Argument for name instead of param type from function type. |
| 2880 | unsigned ArgIdx = 0; |
| 2881 | for (Argument &Arg : F.args()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2882 | // ParamOps[0] : Result Type ID |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2883 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2884 | |
| 2885 | // Find SPIRV instruction for parameter type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2886 | SPIRVID ParamTyID = getSPIRVType(Arg.getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2887 | if (PointerType *PTy = dyn_cast<PointerType>(Arg.getType())) { |
| 2888 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 2889 | if (ArgIdx == GlobalConstFuncTyMap[FTy].second) { |
| 2890 | Type *EleTy = PTy->getPointerElementType(); |
| 2891 | Type *ArgTy = |
| 2892 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2893 | ParamTyID = getSPIRVType(ArgTy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2894 | GlobalConstArgSet.insert(&Arg); |
| 2895 | } |
| 2896 | } |
| 2897 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2898 | Ops << ParamTyID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2899 | |
| 2900 | // Generate SPIRV instruction for parameter. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2901 | SPIRVID param_id = addSPIRVInst(spv::OpFunctionParameter, Ops); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2902 | VMap[&Arg] = param_id; |
| 2903 | |
| 2904 | if (CalledWithCoherentResource(Arg)) { |
| 2905 | // If the arg is passed a coherent resource ever, then decorate this |
| 2906 | // parameter with Coherent too. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2907 | Ops.clear(); |
| 2908 | Ops << param_id << spv::DecorationCoherent; |
| 2909 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2910 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2911 | |
| 2912 | ArgIdx++; |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2917 | void SPIRVProducerPass::GenerateModuleInfo() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2918 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2919 | ValueList &EntryPointInterfaces = getEntryPointInterfacesVec(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2920 | std::vector<SPIRVID> &BuiltinDimVec = getBuiltinDimVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2921 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2922 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2923 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2924 | for (auto Capability : CapabilitySet) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2925 | // |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2926 | // Generate OpCapability |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2927 | // |
| 2928 | // Ops[0] = Capability |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2929 | addSPIRVInst<kCapabilities>(spv::OpCapability, Capability); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | // Always add the storage buffer extension |
| 2933 | { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2934 | // |
| 2935 | // Generate OpExtension. |
| 2936 | // |
| 2937 | // Ops[0] = Name (Literal String) |
| 2938 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2939 | addSPIRVInst<kExtensions>(spv::OpExtension, |
| 2940 | "SPV_KHR_storage_buffer_storage_class"); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2941 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2942 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2943 | if (hasVariablePointers() || hasVariablePointersStorageBuffer()) { |
| 2944 | // |
| 2945 | // Generate OpExtension. |
| 2946 | // |
| 2947 | // Ops[0] = Name (Literal String) |
| 2948 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2949 | addSPIRVInst<kExtensions>(spv::OpExtension, "SPV_KHR_variable_pointers"); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | // |
| 2953 | // Generate OpMemoryModel |
| 2954 | // |
| 2955 | // Memory model for Vulkan will always be GLSL450. |
| 2956 | |
| 2957 | // Ops[0] = Addressing Model |
| 2958 | // Ops[1] = Memory Model |
| 2959 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2960 | Ops << spv::AddressingModelLogical << spv::MemoryModelGLSL450; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2961 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2962 | addSPIRVInst<kMemoryModel>(spv::OpMemoryModel, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2963 | |
| 2964 | // |
| 2965 | // Generate OpEntryPoint |
| 2966 | // |
| 2967 | for (auto EntryPoint : EntryPoints) { |
| 2968 | // Ops[0] = Execution Model |
| 2969 | // Ops[1] = EntryPoint ID |
| 2970 | // Ops[2] = Name (Literal String) |
| 2971 | // ... |
| 2972 | // |
| 2973 | // TODO: Do we need to consider Interface ID for forward references??? |
| 2974 | Ops.clear(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2975 | const StringRef &name = EntryPoint.first->getName(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2976 | Ops << spv::ExecutionModelGLCompute << EntryPoint.second << name; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2977 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2978 | for (Value *Interface : EntryPointInterfaces) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 2979 | Ops << Interface; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2980 | } |
| 2981 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2982 | addSPIRVInst<kEntryPoints>(spv::OpEntryPoint, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | for (auto EntryPoint : EntryPoints) { |
Kévin Petit | 21c23c6 | 2020-04-29 01:38:28 +0100 | [diff] [blame] | 2986 | const MDNode *MD = dyn_cast<Function>(EntryPoint.first) |
| 2987 | ->getMetadata("reqd_work_group_size"); |
| 2988 | if ((MD != nullptr) && !clspv::Option::NonUniformNDRangeSupported()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2989 | |
| 2990 | if (!BuiltinDimVec.empty()) { |
| 2991 | llvm_unreachable( |
| 2992 | "Kernels should have consistent work group size definition"); |
| 2993 | } |
| 2994 | |
| 2995 | // |
| 2996 | // Generate OpExecutionMode |
| 2997 | // |
| 2998 | |
| 2999 | // Ops[0] = Entry Point ID |
| 3000 | // Ops[1] = Execution Mode |
| 3001 | // Ops[2] ... Ops[n] = Optional literals according to Execution Mode |
| 3002 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3003 | Ops << EntryPoint.second << spv::ExecutionModeLocalSize; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3004 | |
| 3005 | uint32_t XDim = static_cast<uint32_t>( |
| 3006 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 3007 | uint32_t YDim = static_cast<uint32_t>( |
| 3008 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 3009 | uint32_t ZDim = static_cast<uint32_t>( |
| 3010 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 3011 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3012 | Ops << XDim << YDim << ZDim; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3013 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3014 | addSPIRVInst<kExecutionModes>(spv::OpExecutionMode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | // |
| 3019 | // Generate OpSource. |
| 3020 | // |
| 3021 | // Ops[0] = SourceLanguage ID |
| 3022 | // Ops[1] = Version (LiteralNum) |
| 3023 | // |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3024 | uint32_t LangID = spv::SourceLanguageUnknown; |
| 3025 | uint32_t LangVer = 0; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3026 | switch (clspv::Option::Language()) { |
| 3027 | case clspv::Option::SourceLanguage::OpenCL_C_10: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3028 | LangID = spv::SourceLanguageOpenCL_C; |
| 3029 | LangVer = 100; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3030 | break; |
| 3031 | case clspv::Option::SourceLanguage::OpenCL_C_11: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3032 | LangID = spv::SourceLanguageOpenCL_C; |
| 3033 | LangVer = 110; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3034 | break; |
| 3035 | case clspv::Option::SourceLanguage::OpenCL_C_12: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3036 | LangID = spv::SourceLanguageOpenCL_C; |
| 3037 | LangVer = 120; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3038 | break; |
| 3039 | case clspv::Option::SourceLanguage::OpenCL_C_20: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3040 | LangID = spv::SourceLanguageOpenCL_C; |
| 3041 | LangVer = 200; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3042 | break; |
| 3043 | case clspv::Option::SourceLanguage::OpenCL_CPP: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3044 | LangID = spv::SourceLanguageOpenCL_CPP; |
| 3045 | LangVer = 100; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3046 | break; |
| 3047 | default: |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3048 | break; |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 3049 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3050 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3051 | Ops.clear(); |
| 3052 | Ops << LangID << LangVer; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3053 | addSPIRVInst<kDebug>(spv::OpSource, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3054 | |
| 3055 | if (!BuiltinDimVec.empty()) { |
| 3056 | // |
| 3057 | // Generate OpDecorates for x/y/z dimension. |
| 3058 | // |
| 3059 | // Ops[0] = Target ID |
| 3060 | // Ops[1] = Decoration (SpecId) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3061 | // Ops[2] = Specialization Constant ID (Literal Number) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3062 | |
| 3063 | // X Dimension |
| 3064 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3065 | Ops << BuiltinDimVec[0] << spv::DecorationSpecId << 0; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3066 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3067 | |
| 3068 | // Y Dimension |
| 3069 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3070 | Ops << BuiltinDimVec[1] << spv::DecorationSpecId << 1; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3071 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3072 | |
| 3073 | // Z Dimension |
| 3074 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3075 | Ops << BuiltinDimVec[2] << spv::DecorationSpecId << 2; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3076 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3080 | void SPIRVProducerPass::GenerateEntryPointInitialStores() { |
| 3081 | // Work around a driver bug. Initializers on Private variables might not |
| 3082 | // work. So the start of the kernel should store the initializer value to the |
| 3083 | // variables. Yes, *every* entry point pays this cost if *any* entry point |
| 3084 | // uses this builtin. At this point I judge this to be an acceptable tradeoff |
| 3085 | // of complexity vs. runtime, for a broken driver. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3086 | // TODO(dneto): Remove this at some point once fixed drivers are widely |
| 3087 | // available. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3088 | if (WorkgroupSizeVarID.isValid()) { |
| 3089 | assert(WorkgroupSizeValueID.isValid()); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3090 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3091 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3092 | Ops << WorkgroupSizeVarID << WorkgroupSizeValueID; |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3093 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3094 | addSPIRVInst(spv::OpStore, Ops); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3098 | void SPIRVProducerPass::GenerateFuncBody(Function &F) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3099 | ValueMapType &VMap = getValueMap(); |
| 3100 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3101 | const bool IsKernel = F.getCallingConv() == CallingConv::SPIR_KERNEL; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3102 | |
| 3103 | for (BasicBlock &BB : F) { |
| 3104 | // Register BasicBlock to ValueMap. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3105 | |
| 3106 | // |
| 3107 | // Generate OpLabel for Basic Block. |
| 3108 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3109 | VMap[&BB] = addSPIRVInst(spv::OpLabel); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3110 | |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3111 | // OpVariable instructions must come first. |
| 3112 | for (Instruction &I : BB) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3113 | if (auto *alloca = dyn_cast<AllocaInst>(&I)) { |
| 3114 | // Allocating a pointer requires variable pointers. |
| 3115 | if (alloca->getAllocatedType()->isPointerTy()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3116 | setVariablePointersCapabilities( |
| 3117 | alloca->getAllocatedType()->getPointerAddressSpace()); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3118 | } |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3119 | GenerateInstruction(I); |
| 3120 | } |
| 3121 | } |
| 3122 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3123 | if (&BB == &F.getEntryBlock() && IsKernel) { |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3124 | if (clspv::Option::HackInitializers()) { |
| 3125 | GenerateEntryPointInitialStores(); |
| 3126 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | for (Instruction &I : BB) { |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3130 | if (!isa<AllocaInst>(I)) { |
| 3131 | GenerateInstruction(I); |
| 3132 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3133 | } |
| 3134 | } |
| 3135 | } |
| 3136 | |
| 3137 | spv::Op SPIRVProducerPass::GetSPIRVCmpOpcode(CmpInst *I) { |
| 3138 | const std::map<CmpInst::Predicate, spv::Op> Map = { |
| 3139 | {CmpInst::ICMP_EQ, spv::OpIEqual}, |
| 3140 | {CmpInst::ICMP_NE, spv::OpINotEqual}, |
| 3141 | {CmpInst::ICMP_UGT, spv::OpUGreaterThan}, |
| 3142 | {CmpInst::ICMP_UGE, spv::OpUGreaterThanEqual}, |
| 3143 | {CmpInst::ICMP_ULT, spv::OpULessThan}, |
| 3144 | {CmpInst::ICMP_ULE, spv::OpULessThanEqual}, |
| 3145 | {CmpInst::ICMP_SGT, spv::OpSGreaterThan}, |
| 3146 | {CmpInst::ICMP_SGE, spv::OpSGreaterThanEqual}, |
| 3147 | {CmpInst::ICMP_SLT, spv::OpSLessThan}, |
| 3148 | {CmpInst::ICMP_SLE, spv::OpSLessThanEqual}, |
| 3149 | {CmpInst::FCMP_OEQ, spv::OpFOrdEqual}, |
| 3150 | {CmpInst::FCMP_OGT, spv::OpFOrdGreaterThan}, |
| 3151 | {CmpInst::FCMP_OGE, spv::OpFOrdGreaterThanEqual}, |
| 3152 | {CmpInst::FCMP_OLT, spv::OpFOrdLessThan}, |
| 3153 | {CmpInst::FCMP_OLE, spv::OpFOrdLessThanEqual}, |
| 3154 | {CmpInst::FCMP_ONE, spv::OpFOrdNotEqual}, |
| 3155 | {CmpInst::FCMP_UEQ, spv::OpFUnordEqual}, |
| 3156 | {CmpInst::FCMP_UGT, spv::OpFUnordGreaterThan}, |
| 3157 | {CmpInst::FCMP_UGE, spv::OpFUnordGreaterThanEqual}, |
| 3158 | {CmpInst::FCMP_ULT, spv::OpFUnordLessThan}, |
| 3159 | {CmpInst::FCMP_ULE, spv::OpFUnordLessThanEqual}, |
| 3160 | {CmpInst::FCMP_UNE, spv::OpFUnordNotEqual}}; |
| 3161 | |
| 3162 | assert(0 != Map.count(I->getPredicate())); |
| 3163 | |
| 3164 | return Map.at(I->getPredicate()); |
| 3165 | } |
| 3166 | |
| 3167 | spv::Op SPIRVProducerPass::GetSPIRVCastOpcode(Instruction &I) { |
| 3168 | const std::map<unsigned, spv::Op> Map{ |
| 3169 | {Instruction::Trunc, spv::OpUConvert}, |
| 3170 | {Instruction::ZExt, spv::OpUConvert}, |
| 3171 | {Instruction::SExt, spv::OpSConvert}, |
| 3172 | {Instruction::FPToUI, spv::OpConvertFToU}, |
| 3173 | {Instruction::FPToSI, spv::OpConvertFToS}, |
| 3174 | {Instruction::UIToFP, spv::OpConvertUToF}, |
| 3175 | {Instruction::SIToFP, spv::OpConvertSToF}, |
| 3176 | {Instruction::FPTrunc, spv::OpFConvert}, |
| 3177 | {Instruction::FPExt, spv::OpFConvert}, |
| 3178 | {Instruction::BitCast, spv::OpBitcast}}; |
| 3179 | |
| 3180 | assert(0 != Map.count(I.getOpcode())); |
| 3181 | |
| 3182 | return Map.at(I.getOpcode()); |
| 3183 | } |
| 3184 | |
| 3185 | spv::Op SPIRVProducerPass::GetSPIRVBinaryOpcode(Instruction &I) { |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 3186 | if (I.getType()->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3187 | switch (I.getOpcode()) { |
| 3188 | default: |
| 3189 | break; |
| 3190 | case Instruction::Or: |
| 3191 | return spv::OpLogicalOr; |
| 3192 | case Instruction::And: |
| 3193 | return spv::OpLogicalAnd; |
| 3194 | case Instruction::Xor: |
| 3195 | return spv::OpLogicalNotEqual; |
| 3196 | } |
| 3197 | } |
| 3198 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3199 | const std::map<unsigned, spv::Op> Map{ |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3200 | {Instruction::Add, spv::OpIAdd}, |
| 3201 | {Instruction::FAdd, spv::OpFAdd}, |
| 3202 | {Instruction::Sub, spv::OpISub}, |
| 3203 | {Instruction::FSub, spv::OpFSub}, |
| 3204 | {Instruction::Mul, spv::OpIMul}, |
| 3205 | {Instruction::FMul, spv::OpFMul}, |
| 3206 | {Instruction::UDiv, spv::OpUDiv}, |
| 3207 | {Instruction::SDiv, spv::OpSDiv}, |
| 3208 | {Instruction::FDiv, spv::OpFDiv}, |
| 3209 | {Instruction::URem, spv::OpUMod}, |
| 3210 | {Instruction::SRem, spv::OpSRem}, |
| 3211 | {Instruction::FRem, spv::OpFRem}, |
| 3212 | {Instruction::Or, spv::OpBitwiseOr}, |
| 3213 | {Instruction::Xor, spv::OpBitwiseXor}, |
| 3214 | {Instruction::And, spv::OpBitwiseAnd}, |
| 3215 | {Instruction::Shl, spv::OpShiftLeftLogical}, |
| 3216 | {Instruction::LShr, spv::OpShiftRightLogical}, |
| 3217 | {Instruction::AShr, spv::OpShiftRightArithmetic}}; |
| 3218 | |
| 3219 | assert(0 != Map.count(I.getOpcode())); |
| 3220 | |
| 3221 | return Map.at(I.getOpcode()); |
| 3222 | } |
| 3223 | |
| 3224 | void SPIRVProducerPass::GenerateInstruction(Instruction &I) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3225 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3226 | LLVMContext &Context = I.getParent()->getParent()->getParent()->getContext(); |
| 3227 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3228 | SPIRVID RID = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3229 | |
| 3230 | switch (I.getOpcode()) { |
| 3231 | default: { |
| 3232 | if (Instruction::isCast(I.getOpcode())) { |
| 3233 | // |
| 3234 | // Generate SPIRV instructions for cast operators. |
| 3235 | // |
| 3236 | |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3237 | auto Ty = I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3238 | auto OpTy = I.getOperand(0)->getType(); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3239 | auto toI8 = Ty == Type::getInt8Ty(Context); |
| 3240 | auto fromI32 = OpTy == Type::getInt32Ty(Context); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3241 | // Handle zext, sext and uitofp with i1 type specially. |
| 3242 | if ((I.getOpcode() == Instruction::ZExt || |
| 3243 | I.getOpcode() == Instruction::SExt || |
| 3244 | I.getOpcode() == Instruction::UIToFP) && |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3245 | OpTy->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3246 | // |
| 3247 | // Generate OpSelect. |
| 3248 | // |
| 3249 | |
| 3250 | // Ops[0] = Result Type ID |
| 3251 | // Ops[1] = Condition ID |
| 3252 | // Ops[2] = True Constant ID |
| 3253 | // Ops[3] = False Constant ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3254 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3255 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3256 | Ops << I.getType() << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3257 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3258 | if (I.getOpcode() == Instruction::ZExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3259 | Ops << ConstantInt::get(I.getType(), 1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3260 | } else if (I.getOpcode() == Instruction::SExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3261 | Ops << ConstantInt::getSigned(I.getType(), -1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3262 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3263 | Ops << ConstantFP::get(Context, APFloat(1.0f)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3264 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3265 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3266 | if (I.getOpcode() == Instruction::ZExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3267 | Ops << Constant::getNullValue(I.getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3268 | } else if (I.getOpcode() == Instruction::SExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3269 | Ops << Constant::getNullValue(I.getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3270 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3271 | Ops << ConstantFP::get(Context, APFloat(0.0f)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3272 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3273 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3274 | RID = addSPIRVInst(spv::OpSelect, Ops); |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 3275 | } else if (!clspv::Option::Int8Support() && |
| 3276 | I.getOpcode() == Instruction::Trunc && fromI32 && toI8) { |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3277 | // The SPIR-V target type is a 32-bit int. Keep only the bottom |
| 3278 | // 8 bits. |
| 3279 | // Before: |
| 3280 | // %result = trunc i32 %a to i8 |
| 3281 | // After |
| 3282 | // %result = OpBitwiseAnd %uint %a %uint_255 |
| 3283 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3284 | SPIRVOperandVec Ops; |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3285 | |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3286 | Type *UintTy = Type::getInt32Ty(Context); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3287 | Ops << OpTy << I.getOperand(0) << ConstantInt::get(UintTy, 255); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3288 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3289 | RID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3290 | } else { |
| 3291 | // Ops[0] = Result Type ID |
| 3292 | // Ops[1] = Source Value ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3293 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3294 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3295 | Ops << I.getType() << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3296 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3297 | RID = addSPIRVInst(GetSPIRVCastOpcode(I), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3298 | } |
| 3299 | } else if (isa<BinaryOperator>(I)) { |
| 3300 | // |
| 3301 | // Generate SPIRV instructions for binary operators. |
| 3302 | // |
| 3303 | |
| 3304 | // Handle xor with i1 type specially. |
| 3305 | if (I.getOpcode() == Instruction::Xor && |
| 3306 | I.getType() == Type::getInt1Ty(Context) && |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 3307 | ((isa<ConstantInt>(I.getOperand(0)) && |
| 3308 | !cast<ConstantInt>(I.getOperand(0))->isZero()) || |
| 3309 | (isa<ConstantInt>(I.getOperand(1)) && |
| 3310 | !cast<ConstantInt>(I.getOperand(1))->isZero()))) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3311 | // |
| 3312 | // Generate OpLogicalNot. |
| 3313 | // |
| 3314 | // Ops[0] = Result Type ID |
| 3315 | // Ops[1] = Operand |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3316 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3317 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3318 | Ops << I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3319 | |
| 3320 | Value *CondV = I.getOperand(0); |
| 3321 | if (isa<Constant>(I.getOperand(0))) { |
| 3322 | CondV = I.getOperand(1); |
| 3323 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3324 | Ops << CondV; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3325 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3326 | RID = addSPIRVInst(spv::OpLogicalNot, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3327 | } else { |
| 3328 | // Ops[0] = Result Type ID |
| 3329 | // Ops[1] = Operand 0 |
| 3330 | // Ops[2] = Operand 1 |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3331 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3332 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3333 | Ops << I.getType() << I.getOperand(0) << I.getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3334 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3335 | RID = addSPIRVInst(GetSPIRVBinaryOpcode(I), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3336 | } |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 3337 | } else if (I.getOpcode() == Instruction::FNeg) { |
| 3338 | // The only unary operator. |
| 3339 | // |
| 3340 | // Ops[0] = Result Type ID |
| 3341 | // Ops[1] = Operand 0 |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3342 | SPIRVOperandVec Ops; |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 3343 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3344 | Ops << I.getType() << I.getOperand(0); |
| 3345 | RID = addSPIRVInst(spv::OpFNegate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3346 | } else { |
| 3347 | I.print(errs()); |
| 3348 | llvm_unreachable("Unsupported instruction???"); |
| 3349 | } |
| 3350 | break; |
| 3351 | } |
| 3352 | case Instruction::GetElementPtr: { |
| 3353 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 3354 | |
| 3355 | // |
| 3356 | // Generate OpAccessChain. |
| 3357 | // |
| 3358 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(&I); |
| 3359 | |
| 3360 | // |
| 3361 | // Generate OpAccessChain. |
| 3362 | // |
| 3363 | |
| 3364 | // Ops[0] = Result Type ID |
| 3365 | // Ops[1] = Base ID |
| 3366 | // Ops[2] ... Ops[n] = Indexes ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3367 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3368 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3369 | PointerType *ResultType = cast<PointerType>(GEP->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3370 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate || |
| 3371 | GlobalConstArgSet.count(GEP->getPointerOperand())) { |
| 3372 | // Use pointer type with private address space for global constant. |
| 3373 | Type *EleTy = I.getType()->getPointerElementType(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3374 | ResultType = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3375 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3376 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3377 | Ops << ResultType; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3378 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3379 | // Generate the base pointer. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3380 | Ops << GEP->getPointerOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3381 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3382 | // TODO(dneto): Simplify the following? |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3383 | |
| 3384 | // |
| 3385 | // Follows below rules for gep. |
| 3386 | // |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3387 | // 1. If gep's first index is 0 generate OpAccessChain and ignore gep's |
| 3388 | // first index. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3389 | // 2. If gep's first index is not 0, generate OpPtrAccessChain and use gep's |
| 3390 | // first index. |
| 3391 | // 3. If gep's first index is not constant, generate OpPtrAccessChain and |
| 3392 | // use gep's first index. |
| 3393 | // 4. If it is not above case 1, 2 and 3, generate OpAccessChain and use |
| 3394 | // gep's first index. |
| 3395 | // |
| 3396 | spv::Op Opcode = spv::OpAccessChain; |
| 3397 | unsigned offset = 0; |
| 3398 | if (ConstantInt *CstInt = dyn_cast<ConstantInt>(GEP->getOperand(1))) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3399 | if (CstInt->getZExtValue() == 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3400 | offset = 1; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3401 | } else if (CstInt->getZExtValue() != 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3402 | Opcode = spv::OpPtrAccessChain; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3403 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3404 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3405 | Opcode = spv::OpPtrAccessChain; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | if (Opcode == spv::OpPtrAccessChain) { |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3409 | // Do we need to generate ArrayStride? Check against the GEP result type |
| 3410 | // rather than the pointer type of the base because when indexing into |
| 3411 | // an OpenCL program-scope constant, we'll swap out the LLVM base pointer |
| 3412 | // for something else in the SPIR-V. |
| 3413 | // E.g. see test/PointerAccessChain/pointer_index_is_constant_1.cl |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3414 | auto address_space = ResultType->getAddressSpace(); |
| 3415 | setVariablePointersCapabilities(address_space); |
| 3416 | switch (GetStorageClass(address_space)) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 3417 | case spv::StorageClassStorageBuffer: |
| 3418 | case spv::StorageClassUniform: |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3419 | // Save the need to generate an ArrayStride decoration. But defer |
| 3420 | // generation until later, so we only make one decoration. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3421 | getTypesNeedingArrayStride().insert(ResultType); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 3422 | break; |
| 3423 | default: |
| 3424 | break; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 3425 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | for (auto II = GEP->idx_begin() + offset; II != GEP->idx_end(); II++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3429 | Ops << *II; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3430 | } |
| 3431 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3432 | RID = addSPIRVInst(Opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3433 | break; |
| 3434 | } |
| 3435 | case Instruction::ExtractValue: { |
| 3436 | ExtractValueInst *EVI = cast<ExtractValueInst>(&I); |
| 3437 | // Ops[0] = Result Type ID |
| 3438 | // Ops[1] = Composite ID |
| 3439 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3440 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3441 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3442 | Ops << I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3443 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3444 | Ops << EVI->getAggregateOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3445 | |
| 3446 | for (auto &Index : EVI->indices()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3447 | Ops << Index; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3448 | } |
| 3449 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3450 | RID = addSPIRVInst(spv::OpCompositeExtract, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3451 | break; |
| 3452 | } |
| 3453 | case Instruction::InsertValue: { |
| 3454 | InsertValueInst *IVI = cast<InsertValueInst>(&I); |
| 3455 | // Ops[0] = Result Type ID |
| 3456 | // Ops[1] = Object ID |
| 3457 | // Ops[2] = Composite ID |
| 3458 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3459 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3460 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3461 | Ops << I.getType() << IVI->getInsertedValueOperand() |
| 3462 | << IVI->getAggregateOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3463 | |
| 3464 | for (auto &Index : IVI->indices()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3465 | Ops << Index; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3466 | } |
| 3467 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3468 | RID = addSPIRVInst(spv::OpCompositeInsert, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3469 | break; |
| 3470 | } |
| 3471 | case Instruction::Select: { |
| 3472 | // |
| 3473 | // Generate OpSelect. |
| 3474 | // |
| 3475 | |
| 3476 | // Ops[0] = Result Type ID |
| 3477 | // Ops[1] = Condition ID |
| 3478 | // Ops[2] = True Constant ID |
| 3479 | // Ops[3] = False Constant ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3480 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3481 | |
| 3482 | // Find SPIRV instruction for parameter type. |
| 3483 | auto Ty = I.getType(); |
| 3484 | if (Ty->isPointerTy()) { |
| 3485 | auto PointeeTy = Ty->getPointerElementType(); |
| 3486 | if (PointeeTy->isStructTy() && |
| 3487 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 3488 | Ty = PointeeTy; |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3489 | } else { |
| 3490 | // Selecting between pointers requires variable pointers. |
| 3491 | setVariablePointersCapabilities(Ty->getPointerAddressSpace()); |
| 3492 | if (!hasVariablePointers() && !selectFromSameObject(&I)) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3493 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3494 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3495 | } |
| 3496 | } |
| 3497 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3498 | Ops << Ty << I.getOperand(0) << I.getOperand(1) << I.getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3499 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3500 | RID = addSPIRVInst(spv::OpSelect, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3501 | break; |
| 3502 | } |
| 3503 | case Instruction::ExtractElement: { |
| 3504 | // Handle <4 x i8> type manually. |
| 3505 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 3506 | if (is4xi8vec(CompositeTy)) { |
| 3507 | // |
| 3508 | // Generate OpShiftRightLogical and OpBitwiseAnd for extractelement with |
| 3509 | // <4 x i8>. |
| 3510 | // |
| 3511 | |
| 3512 | // |
| 3513 | // Generate OpShiftRightLogical |
| 3514 | // |
| 3515 | // Ops[0] = Result Type ID |
| 3516 | // Ops[1] = Operand 0 |
| 3517 | // Ops[2] = Operand 1 |
| 3518 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3519 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3520 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3521 | Ops << CompositeTy << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3522 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3523 | SPIRVID Op1ID = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3524 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 3525 | // Handle constant index. |
| 3526 | uint64_t Idx = CI->getZExtValue(); |
| 3527 | Value *ShiftAmount = |
| 3528 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3529 | Op1ID = getSPIRVValue(ShiftAmount); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3530 | } else { |
| 3531 | // Handle variable index. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3532 | SPIRVOperandVec TmpOps; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3533 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3534 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3535 | TmpOps << Type::getInt32Ty(Context) << I.getOperand(1) << Cst8; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3536 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3537 | Op1ID = addSPIRVInst(spv::OpIMul, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3538 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3539 | Ops << Op1ID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3540 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3541 | SPIRVID ShiftID = addSPIRVInst(spv::OpShiftRightLogical, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3542 | |
| 3543 | // |
| 3544 | // Generate OpBitwiseAnd |
| 3545 | // |
| 3546 | // Ops[0] = Result Type ID |
| 3547 | // Ops[1] = Operand 0 |
| 3548 | // Ops[2] = Operand 1 |
| 3549 | // |
| 3550 | Ops.clear(); |
| 3551 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3552 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3553 | Ops << CompositeTy << ShiftID << CstFF; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3554 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3555 | RID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3556 | break; |
| 3557 | } |
| 3558 | |
| 3559 | // Ops[0] = Result Type ID |
| 3560 | // Ops[1] = Composite ID |
| 3561 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3562 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3563 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3564 | Ops << I.getType() << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3565 | |
| 3566 | spv::Op Opcode = spv::OpCompositeExtract; |
| 3567 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3568 | Ops << static_cast<uint32_t>(CI->getZExtValue()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3569 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3570 | Ops << I.getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3571 | Opcode = spv::OpVectorExtractDynamic; |
| 3572 | } |
| 3573 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3574 | RID = addSPIRVInst(Opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3575 | break; |
| 3576 | } |
| 3577 | case Instruction::InsertElement: { |
| 3578 | // Handle <4 x i8> type manually. |
| 3579 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 3580 | if (is4xi8vec(CompositeTy)) { |
| 3581 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3582 | SPIRVID CstFFID = getSPIRVValue(CstFF); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3583 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3584 | SPIRVID ShiftAmountID = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3585 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 3586 | // Handle constant index. |
| 3587 | uint64_t Idx = CI->getZExtValue(); |
| 3588 | Value *ShiftAmount = |
| 3589 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3590 | ShiftAmountID = getSPIRVValue(ShiftAmount); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3591 | } else { |
| 3592 | // Handle variable index. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3593 | SPIRVOperandVec TmpOps; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3594 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3595 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3596 | TmpOps << Type::getInt32Ty(Context) << I.getOperand(2) << Cst8; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3597 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3598 | ShiftAmountID = addSPIRVInst(spv::OpIMul, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | // |
| 3602 | // Generate mask operations. |
| 3603 | // |
| 3604 | |
| 3605 | // ShiftLeft mask according to index of insertelement. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3606 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3607 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3608 | Ops << CompositeTy << CstFFID << ShiftAmountID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3609 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3610 | SPIRVID MaskID = addSPIRVInst(spv::OpShiftLeftLogical, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3611 | |
| 3612 | // Inverse mask. |
| 3613 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3614 | Ops << CompositeTy << MaskID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3615 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3616 | SPIRVID InvMaskID = addSPIRVInst(spv::OpNot, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3617 | |
| 3618 | // Apply mask. |
| 3619 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3620 | Ops << CompositeTy << I.getOperand(0) << InvMaskID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3621 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3622 | SPIRVID OrgValID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3623 | |
| 3624 | // Create correct value according to index of insertelement. |
| 3625 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3626 | Ops << CompositeTy << I.getOperand(1) << ShiftAmountID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3627 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3628 | SPIRVID InsertValID = addSPIRVInst(spv::OpShiftLeftLogical, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3629 | |
| 3630 | // Insert value to original value. |
| 3631 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3632 | Ops << CompositeTy << OrgValID << InsertValID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3633 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3634 | RID = addSPIRVInst(spv::OpBitwiseOr, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3635 | break; |
| 3636 | } |
| 3637 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3638 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3639 | |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 3640 | // Ops[0] = Result Type ID |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3641 | Ops << I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3642 | |
| 3643 | spv::Op Opcode = spv::OpCompositeInsert; |
| 3644 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3645 | const auto value = CI->getZExtValue(); |
| 3646 | assert(value <= UINT32_MAX); |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 3647 | // Ops[1] = Object ID |
| 3648 | // Ops[2] = Composite ID |
| 3649 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3650 | Ops << I.getOperand(1) << I.getOperand(0) << static_cast<uint32_t>(value); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3651 | } else { |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 3652 | // Ops[1] = Composite ID |
| 3653 | // Ops[2] = Object ID |
| 3654 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3655 | Ops << I.getOperand(0) << I.getOperand(1) << I.getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3656 | Opcode = spv::OpVectorInsertDynamic; |
| 3657 | } |
| 3658 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3659 | RID = addSPIRVInst(Opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3660 | break; |
| 3661 | } |
| 3662 | case Instruction::ShuffleVector: { |
| 3663 | // Ops[0] = Result Type ID |
| 3664 | // Ops[1] = Vector 1 ID |
| 3665 | // Ops[2] = Vector 2 ID |
| 3666 | // Ops[3] ... Ops[n] = Components (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3667 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3668 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3669 | Ops << I.getType() << I.getOperand(0) << I.getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3670 | |
alan-baker | c966671 | 2020-04-01 16:31:21 -0400 | [diff] [blame] | 3671 | auto shuffle = cast<ShuffleVectorInst>(&I); |
| 3672 | SmallVector<int, 4> mask; |
| 3673 | shuffle->getShuffleMask(mask); |
| 3674 | for (auto i : mask) { |
| 3675 | if (i == UndefMaskElem) { |
| 3676 | if (clspv::Option::HackUndef()) |
| 3677 | // Use 0 instead of undef. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3678 | Ops << 0; |
alan-baker | c966671 | 2020-04-01 16:31:21 -0400 | [diff] [blame] | 3679 | else |
| 3680 | // Undef for shuffle in SPIR-V. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3681 | Ops << 0xffffffff; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3682 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3683 | Ops << i; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3687 | RID = addSPIRVInst(spv::OpVectorShuffle, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3688 | break; |
| 3689 | } |
| 3690 | case Instruction::ICmp: |
| 3691 | case Instruction::FCmp: { |
| 3692 | CmpInst *CmpI = cast<CmpInst>(&I); |
| 3693 | |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 3694 | // Pointer equality is invalid. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3695 | Type *ArgTy = CmpI->getOperand(0)->getType(); |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 3696 | if (isa<PointerType>(ArgTy)) { |
| 3697 | CmpI->print(errs()); |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 3698 | std::string name = I.getParent()->getParent()->getName().str(); |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 3699 | errs() |
| 3700 | << "\nPointer equality test is not supported by SPIR-V for Vulkan, " |
| 3701 | << "in function " << name << "\n"; |
| 3702 | llvm_unreachable("Pointer equality check is invalid"); |
| 3703 | break; |
| 3704 | } |
| 3705 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3706 | // Ops[0] = Result Type ID |
| 3707 | // Ops[1] = Operand 1 ID |
| 3708 | // Ops[2] = Operand 2 ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3709 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3710 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3711 | Ops << CmpI->getType() << CmpI->getOperand(0) << CmpI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3712 | |
| 3713 | spv::Op Opcode = GetSPIRVCmpOpcode(CmpI); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3714 | RID = addSPIRVInst(Opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3715 | break; |
| 3716 | } |
| 3717 | case Instruction::Br: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 3718 | // Branch instruction is deferred because it needs label's ID. |
| 3719 | BasicBlock *BrBB = I.getParent(); |
| 3720 | if (ContinueBlocks.count(BrBB) || MergeBlocks.count(BrBB)) { |
| 3721 | // Placeholder for Merge operation |
| 3722 | RID = addSPIRVPlaceholder(&I); |
| 3723 | } |
| 3724 | RID = addSPIRVPlaceholder(&I); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3725 | break; |
| 3726 | } |
| 3727 | case Instruction::Switch: { |
| 3728 | I.print(errs()); |
| 3729 | llvm_unreachable("Unsupported instruction???"); |
| 3730 | break; |
| 3731 | } |
| 3732 | case Instruction::IndirectBr: { |
| 3733 | I.print(errs()); |
| 3734 | llvm_unreachable("Unsupported instruction???"); |
| 3735 | break; |
| 3736 | } |
| 3737 | case Instruction::PHI: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 3738 | // PHI instruction is deferred because it needs label's ID. |
| 3739 | RID = addSPIRVPlaceholder(&I); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3740 | break; |
| 3741 | } |
| 3742 | case Instruction::Alloca: { |
| 3743 | // |
| 3744 | // Generate OpVariable. |
| 3745 | // |
| 3746 | // Ops[0] : Result Type ID |
| 3747 | // Ops[1] : Storage Class |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3748 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3749 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3750 | Ops << I.getType() << spv::StorageClassFunction; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3751 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3752 | RID = addSPIRVInst(spv::OpVariable, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3753 | break; |
| 3754 | } |
| 3755 | case Instruction::Load: { |
| 3756 | LoadInst *LD = cast<LoadInst>(&I); |
| 3757 | // |
| 3758 | // Generate OpLoad. |
| 3759 | // |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3760 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3761 | if (LD->getType()->isPointerTy()) { |
| 3762 | // Loading a pointer requires variable pointers. |
| 3763 | setVariablePointersCapabilities(LD->getType()->getPointerAddressSpace()); |
| 3764 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3765 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3766 | SPIRVID PointerID = getSPIRVValue(LD->getPointerOperand()); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 3767 | // This is a hack to work around what looks like a driver bug. |
| 3768 | // When we're loading from the special variable holding the WorkgroupSize |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 3769 | // builtin value, use an OpBitWiseAnd of the value's ID rather than |
| 3770 | // generating a load. |
David Neto | 66cfe64 | 2018-03-24 06:13:56 -0700 | [diff] [blame] | 3771 | // TODO(dneto): Remove this awful hack once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 3772 | if (PointerID == WorkgroupSizeVarID) { |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 3773 | // Generate a bitwise-and of the original value with itself. |
| 3774 | // We should have been able to get away with just an OpCopyObject, |
| 3775 | // but we need something more complex to get past certain driver bugs. |
| 3776 | // This is ridiculous, but necessary. |
| 3777 | // TODO(dneto): Revisit this once drivers fix their bugs. |
| 3778 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3779 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3780 | Ops << LD->getType() << WorkgroupSizeValueID << WorkgroupSizeValueID; |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 3781 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3782 | RID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 3783 | break; |
| 3784 | } |
| 3785 | |
| 3786 | // This is the normal path. Generate a load. |
| 3787 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3788 | // Ops[0] = Result Type ID |
| 3789 | // Ops[1] = Pointer ID |
| 3790 | // Ops[2] ... Ops[n] = Optional Memory Access |
| 3791 | // |
| 3792 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 3793 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3794 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3795 | Ops << LD->getType() << LD->getPointerOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3796 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3797 | RID = addSPIRVInst(spv::OpLoad, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3798 | break; |
| 3799 | } |
| 3800 | case Instruction::Store: { |
| 3801 | StoreInst *ST = cast<StoreInst>(&I); |
| 3802 | // |
| 3803 | // Generate OpStore. |
| 3804 | // |
| 3805 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3806 | if (ST->getValueOperand()->getType()->isPointerTy()) { |
| 3807 | // Storing a pointer requires variable pointers. |
| 3808 | setVariablePointersCapabilities( |
| 3809 | ST->getValueOperand()->getType()->getPointerAddressSpace()); |
| 3810 | } |
| 3811 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3812 | // Ops[0] = Pointer ID |
| 3813 | // Ops[1] = Object ID |
| 3814 | // Ops[2] ... Ops[n] = Optional Memory Access (later???) |
| 3815 | // |
| 3816 | // TODO: Do we need to implement Optional Memory Access??? |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3817 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3818 | Ops << ST->getPointerOperand() << ST->getValueOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3819 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3820 | RID = addSPIRVInst(spv::OpStore, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3821 | break; |
| 3822 | } |
| 3823 | case Instruction::AtomicCmpXchg: { |
| 3824 | I.print(errs()); |
| 3825 | llvm_unreachable("Unsupported instruction???"); |
| 3826 | break; |
| 3827 | } |
| 3828 | case Instruction::AtomicRMW: { |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 3829 | AtomicRMWInst *AtomicRMW = dyn_cast<AtomicRMWInst>(&I); |
| 3830 | |
| 3831 | spv::Op opcode; |
| 3832 | |
| 3833 | switch (AtomicRMW->getOperation()) { |
| 3834 | default: |
| 3835 | I.print(errs()); |
| 3836 | llvm_unreachable("Unsupported instruction???"); |
| 3837 | case llvm::AtomicRMWInst::Add: |
| 3838 | opcode = spv::OpAtomicIAdd; |
| 3839 | break; |
| 3840 | case llvm::AtomicRMWInst::Sub: |
| 3841 | opcode = spv::OpAtomicISub; |
| 3842 | break; |
| 3843 | case llvm::AtomicRMWInst::Xchg: |
| 3844 | opcode = spv::OpAtomicExchange; |
| 3845 | break; |
| 3846 | case llvm::AtomicRMWInst::Min: |
| 3847 | opcode = spv::OpAtomicSMin; |
| 3848 | break; |
| 3849 | case llvm::AtomicRMWInst::Max: |
| 3850 | opcode = spv::OpAtomicSMax; |
| 3851 | break; |
| 3852 | case llvm::AtomicRMWInst::UMin: |
| 3853 | opcode = spv::OpAtomicUMin; |
| 3854 | break; |
| 3855 | case llvm::AtomicRMWInst::UMax: |
| 3856 | opcode = spv::OpAtomicUMax; |
| 3857 | break; |
| 3858 | case llvm::AtomicRMWInst::And: |
| 3859 | opcode = spv::OpAtomicAnd; |
| 3860 | break; |
| 3861 | case llvm::AtomicRMWInst::Or: |
| 3862 | opcode = spv::OpAtomicOr; |
| 3863 | break; |
| 3864 | case llvm::AtomicRMWInst::Xor: |
| 3865 | opcode = spv::OpAtomicXor; |
| 3866 | break; |
| 3867 | } |
| 3868 | |
| 3869 | // |
| 3870 | // Generate OpAtomic*. |
| 3871 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3872 | SPIRVOperandVec Ops; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 3873 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3874 | Ops << I.getType() << AtomicRMW->getPointerOperand(); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 3875 | |
| 3876 | auto IntTy = Type::getInt32Ty(I.getContext()); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 3877 | const auto ConstantScopeDevice = ConstantInt::get(IntTy, spv::ScopeDevice); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3878 | Ops << ConstantScopeDevice; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 3879 | |
| 3880 | const auto ConstantMemorySemantics = ConstantInt::get( |
| 3881 | IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 3882 | spv::MemorySemanticsSequentiallyConsistentMask); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3883 | Ops << ConstantMemorySemantics << AtomicRMW->getValOperand(); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 3884 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3885 | RID = addSPIRVInst(opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3886 | break; |
| 3887 | } |
| 3888 | case Instruction::Fence: { |
| 3889 | I.print(errs()); |
| 3890 | llvm_unreachable("Unsupported instruction???"); |
| 3891 | break; |
| 3892 | } |
| 3893 | case Instruction::Call: { |
| 3894 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 3895 | Function *Callee = Call->getCalledFunction(); |
| 3896 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 3897 | if (Callee->getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3898 | if (ResourceVarDeferredLoadCalls.count(Call) && Call->hasNUsesOrMore(1)) { |
| 3899 | // Generate an OpLoad |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3900 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3901 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3902 | Ops << Call->getType()->getPointerElementType() |
| 3903 | << ResourceVarDeferredLoadCalls[Call]; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3904 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3905 | RID = addSPIRVInst(spv::OpLoad, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3906 | break; |
| 3907 | |
| 3908 | } else { |
| 3909 | // This maps to an OpVariable we've already generated. |
| 3910 | // No code is generated for the call. |
| 3911 | } |
| 3912 | break; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3913 | } else if (Callee->getName().startswith( |
| 3914 | clspv::WorkgroupAccessorFunction())) { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 3915 | // Don't codegen an instruction here, but instead map this call directly |
| 3916 | // to the workgroup variable id. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3917 | int spec_id = static_cast<int>( |
| 3918 | cast<ConstantInt>(Call->getOperand(0))->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 3919 | const auto &info = LocalSpecIdInfoMap[spec_id]; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3920 | RID = info.variable_id; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 3921 | break; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | // Sampler initializers become a load of the corresponding sampler. |
| 3925 | |
Kévin Petit | df71de3 | 2019-04-09 14:09:50 +0100 | [diff] [blame] | 3926 | if (Callee->getName().equals(clspv::LiteralSamplerFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3927 | // Map this to a load from the variable. |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 3928 | const auto third_param = static_cast<unsigned>( |
| 3929 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue()); |
| 3930 | auto sampler_value = third_param; |
| 3931 | if (clspv::Option::UseSamplerMap()) { |
| 3932 | sampler_value = getSamplerMap()[third_param].first; |
| 3933 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3934 | |
| 3935 | // Generate an OpLoad |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3936 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3937 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3938 | Ops << SamplerTy->getPointerElementType() |
| 3939 | << SamplerLiteralToIDMap[sampler_value]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3940 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3941 | RID = addSPIRVInst(spv::OpLoad, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3942 | break; |
| 3943 | } |
| 3944 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 3945 | // Handle SPIR-V intrinsics |
Kévin Petit | 9b34026 | 2019-06-19 18:31:11 +0100 | [diff] [blame] | 3946 | spv::Op opcode = StringSwitch<spv::Op>(Callee->getName()) |
| 3947 | .Case("spirv.atomic_xor", spv::OpAtomicXor) |
| 3948 | .Default(spv::OpNop); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3949 | |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 3950 | // If the switch above didn't have an entry maybe the intrinsic |
| 3951 | // is using the name mangling logic. |
| 3952 | bool usesMangler = false; |
| 3953 | if (opcode == spv::OpNop) { |
| 3954 | if (Callee->getName().startswith(clspv::SPIRVOpIntrinsicFunction())) { |
| 3955 | auto OpCst = cast<ConstantInt>(Call->getOperand(0)); |
| 3956 | opcode = static_cast<spv::Op>(OpCst->getZExtValue()); |
| 3957 | usesMangler = true; |
| 3958 | } |
| 3959 | } |
| 3960 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 3961 | if (opcode != spv::OpNop) { |
| 3962 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3963 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3964 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 3965 | if (!I.getType()->isVoidTy()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3966 | Ops << I.getType(); |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 3967 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3968 | |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 3969 | unsigned firstOperand = usesMangler ? 1 : 0; |
| 3970 | for (unsigned i = firstOperand; i < Call->getNumArgOperands(); i++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 3971 | Ops << Call->getArgOperand(i); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3972 | } |
| 3973 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3974 | RID = addSPIRVInst(opcode, Ops); |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 3975 | break; |
| 3976 | } |
| 3977 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3978 | // spirv.copy_memory.* intrinsics become OpMemoryMemory's. |
| 3979 | if (Callee->getName().startswith("spirv.copy_memory")) { |
| 3980 | // |
| 3981 | // Generate OpCopyMemory. |
| 3982 | // |
| 3983 | |
| 3984 | // Ops[0] = Dst ID |
| 3985 | // Ops[1] = Src ID |
| 3986 | // Ops[2] = Memory Access |
| 3987 | // Ops[3] = Alignment |
| 3988 | |
| 3989 | auto IsVolatile = |
| 3990 | dyn_cast<ConstantInt>(Call->getArgOperand(3))->getZExtValue() != 0; |
| 3991 | |
| 3992 | auto VolatileMemoryAccess = (IsVolatile) ? spv::MemoryAccessVolatileMask |
| 3993 | : spv::MemoryAccessMaskNone; |
| 3994 | |
| 3995 | auto MemoryAccess = VolatileMemoryAccess | spv::MemoryAccessAlignedMask; |
| 3996 | |
| 3997 | auto Alignment = |
| 3998 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue(); |
| 3999 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4000 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4001 | Ops << Call->getArgOperand(0) << Call->getArgOperand(1) << MemoryAccess |
| 4002 | << static_cast<uint32_t>(Alignment); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4003 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4004 | RID = addSPIRVInst(spv::OpCopyMemory, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4005 | break; |
| 4006 | } |
| 4007 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4008 | // read_image is converted to OpSampledImage and OpImageSampleExplicitLod. |
| 4009 | // Additionally, OpTypeSampledImage is generated. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 4010 | if (IsSampledImageRead(Callee)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4011 | // |
| 4012 | // Generate OpSampledImage. |
| 4013 | // |
| 4014 | // Ops[0] = Result Type ID |
| 4015 | // Ops[1] = Image ID |
| 4016 | // Ops[2] = Sampler ID |
| 4017 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4018 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4019 | |
| 4020 | Value *Image = Call->getArgOperand(0); |
| 4021 | Value *Sampler = Call->getArgOperand(1); |
| 4022 | Value *Coordinate = Call->getArgOperand(2); |
| 4023 | |
| 4024 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 4025 | Type *ImageTy = Image->getType()->getPointerElementType(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4026 | SPIRVID ImageTyID = OpImageTypeMap[ImageTy]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4027 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4028 | Ops << ImageTyID << Image << Sampler; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4029 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4030 | SPIRVID SampledImageID = addSPIRVInst(spv::OpSampledImage, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4031 | |
| 4032 | // |
| 4033 | // Generate OpImageSampleExplicitLod. |
| 4034 | // |
| 4035 | // Ops[0] = Result Type ID |
| 4036 | // Ops[1] = Sampled Image ID |
| 4037 | // Ops[2] = Coordinate ID |
| 4038 | // Ops[3] = Image Operands Type ID |
| 4039 | // Ops[4] ... Ops[n] = Operands ID |
| 4040 | // |
| 4041 | Ops.clear(); |
| 4042 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4043 | const bool is_int_image = IsIntImageType(Image->getType()); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4044 | SPIRVID result_type; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4045 | if (is_int_image) { |
| 4046 | result_type = v4int32ID; |
| 4047 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4048 | result_type = getSPIRVType(Call->getType()); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4049 | } |
| 4050 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4051 | Constant *CstFP0 = ConstantFP::get(Context, APFloat(0.0f)); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4052 | Ops << result_type << SampledImageID << Coordinate |
| 4053 | << spv::ImageOperandsLodMask << CstFP0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4054 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4055 | RID = addSPIRVInst(spv::OpImageSampleExplicitLod, Ops); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4056 | |
| 4057 | if (is_int_image) { |
| 4058 | // Generate the bitcast. |
| 4059 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4060 | Ops << Call->getType() << RID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4061 | RID = addSPIRVInst(spv::OpBitcast, Ops); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4062 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4063 | break; |
| 4064 | } |
| 4065 | |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4066 | // read_image (without a sampler) is mapped to OpImageFetch. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 4067 | if (IsUnsampledImageRead(Callee)) { |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4068 | Value *Image = Call->getArgOperand(0); |
| 4069 | Value *Coordinate = Call->getArgOperand(1); |
| 4070 | |
| 4071 | // |
| 4072 | // Generate OpImageFetch |
| 4073 | // |
| 4074 | // Ops[0] = Result Type ID |
| 4075 | // Ops[1] = Image ID |
| 4076 | // Ops[2] = Coordinate ID |
| 4077 | // Ops[3] = Lod |
| 4078 | // Ops[4] = 0 |
| 4079 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4080 | SPIRVOperandVec Ops; |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4081 | |
| 4082 | const bool is_int_image = IsIntImageType(Image->getType()); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4083 | SPIRVID result_type; |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4084 | if (is_int_image) { |
| 4085 | result_type = v4int32ID; |
| 4086 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4087 | result_type = getSPIRVType(Call->getType()); |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4088 | } |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4089 | Constant *CstInt0 = ConstantInt::get(Context, APInt(32, 0)); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4090 | |
| 4091 | Ops << result_type << Image << Coordinate << spv::ImageOperandsLodMask |
| 4092 | << CstInt0; |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4093 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4094 | RID = addSPIRVInst(spv::OpImageFetch, Ops); |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4095 | |
| 4096 | if (is_int_image) { |
| 4097 | // Generate the bitcast. |
| 4098 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4099 | Ops << Call->getType() << RID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4100 | RID = addSPIRVInst(spv::OpBitcast, Ops); |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 4101 | } |
| 4102 | break; |
| 4103 | } |
| 4104 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4105 | // write_image is mapped to OpImageWrite. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 4106 | if (IsImageWrite(Callee)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4107 | // |
| 4108 | // Generate OpImageWrite. |
| 4109 | // |
| 4110 | // Ops[0] = Image ID |
| 4111 | // Ops[1] = Coordinate ID |
| 4112 | // Ops[2] = Texel ID |
| 4113 | // Ops[3] = (Optional) Image Operands Type (Literal Number) |
| 4114 | // Ops[4] ... Ops[n] = (Optional) Operands ID |
| 4115 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4116 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4117 | |
| 4118 | Value *Image = Call->getArgOperand(0); |
| 4119 | Value *Coordinate = Call->getArgOperand(1); |
| 4120 | Value *Texel = Call->getArgOperand(2); |
| 4121 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4122 | SPIRVID TexelID = getSPIRVValue(Texel); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4123 | |
| 4124 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 4125 | if (is_int_image) { |
| 4126 | // Generate a bitcast to v4int and use it as the texel value. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4127 | Ops << v4int32ID << TexelID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4128 | TexelID = addSPIRVInst(spv::OpBitcast, Ops); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4129 | Ops.clear(); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 4130 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4131 | Ops << Image << Coordinate << TexelID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4132 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4133 | RID = addSPIRVInst(spv::OpImageWrite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4134 | break; |
| 4135 | } |
| 4136 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4137 | // get_image_* is mapped to OpImageQuerySize or OpImageQuerySizeLod |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 4138 | if (IsImageQuery(Callee)) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4139 | |
| 4140 | addCapability(spv::CapabilityImageQuery); |
| 4141 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4142 | // |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4143 | // Generate OpImageQuerySize[Lod] |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4144 | // |
| 4145 | // Ops[0] = Image ID |
| 4146 | // |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4147 | // Result type has components equal to the dimensionality of the image, |
| 4148 | // plus 1 if the image is arrayed. |
| 4149 | // |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 4150 | // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0] |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4151 | SPIRVOperandVec Ops; |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4152 | |
| 4153 | // Implement: |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4154 | // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0] |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4155 | SPIRVID SizesTypeID; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4156 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4157 | Value *Image = Call->getArgOperand(0); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4158 | const uint32_t dim = ImageDimensionality(Image->getType()); |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 4159 | const uint32_t components = |
| 4160 | dim + (IsArrayImageType(Image->getType()) ? 1 : 0); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4161 | if (components == 1) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4162 | SizesTypeID = getSPIRVType(Type::getInt32Ty(Context)); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4163 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4164 | SizesTypeID = getSPIRVType( |
| 4165 | VectorType::get(Type::getInt32Ty(Context), components)); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4166 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4167 | Ops << SizesTypeID << Image; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4168 | spv::Op query_opcode = spv::OpImageQuerySize; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 4169 | if (IsSampledImageType(Image->getType())) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4170 | query_opcode = spv::OpImageQuerySizeLod; |
| 4171 | // Need explicit 0 for Lod operand. |
| 4172 | Constant *CstInt0 = ConstantInt::get(Context, APInt(32, 0)); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4173 | Ops << CstInt0; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4174 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4175 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4176 | RID = addSPIRVInst(query_opcode, Ops); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4177 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4178 | // May require an extra instruction to create the appropriate result of |
| 4179 | // the builtin function. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 4180 | if (IsGetImageDim(Callee)) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4181 | if (dim == 3) { |
| 4182 | // get_image_dim returns an int4 for 3D images. |
| 4183 | // |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4184 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4185 | // Implement: |
| 4186 | // %result = OpCompositeConstruct %uint4 %sizes %uint_0 |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4187 | Constant *CstInt0 = ConstantInt::get(Context, APInt(32, 0)); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4188 | |
| 4189 | Ops.clear(); |
| 4190 | Ops << VectorType::get(Type::getInt32Ty(Context), 4) << RID |
| 4191 | << CstInt0; |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4192 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4193 | RID = addSPIRVInst(spv::OpCompositeConstruct, Ops); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4194 | } else if (dim != components) { |
| 4195 | // get_image_dim return an int2 regardless of the arrayedness of the |
| 4196 | // image. If the image is arrayed an element must be dropped from the |
| 4197 | // query result. |
| 4198 | // |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4199 | |
| 4200 | // Implement: |
| 4201 | // %result = OpVectorShuffle %uint2 %sizes %sizes 0 1 |
| 4202 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4203 | Ops << VectorType::get(Type::getInt32Ty(Context), 2) << RID << RID |
| 4204 | << 0 << 1; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4205 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4206 | RID = addSPIRVInst(spv::OpVectorShuffle, Ops); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4207 | } |
| 4208 | } else if (components > 1) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4209 | // Implement: |
| 4210 | // %result = OpCompositeExtract %uint %sizes <component number> |
| 4211 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4212 | Ops << I.getType() << RID; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4213 | |
| 4214 | uint32_t component = 0; |
| 4215 | if (IsGetImageHeight(Callee)) |
| 4216 | component = 1; |
| 4217 | else if (IsGetImageDepth(Callee)) |
| 4218 | component = 2; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4219 | Ops << component; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4220 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4221 | RID = addSPIRVInst(spv::OpCompositeExtract, Ops); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 4222 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4223 | break; |
| 4224 | } |
| 4225 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4226 | // Call instruction is deferred because it needs function's ID. |
| 4227 | RID = addSPIRVPlaceholder(&I); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4228 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4229 | // Check whether the implementation of this call uses an extended |
| 4230 | // instruction plus one more value-producing instruction. If so, then |
| 4231 | // reserve the id for the extra value-producing slot. |
| 4232 | glsl::ExtInst EInst = getIndirectExtInstEnum(Callee->getName()); |
| 4233 | if (EInst != kGlslExtInstBad) { |
| 4234 | // Reserve a spot for the extra value. |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4235 | RID = addSPIRVPlaceholder(&I); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4236 | } |
| 4237 | break; |
| 4238 | } |
| 4239 | case Instruction::Ret: { |
| 4240 | unsigned NumOps = I.getNumOperands(); |
| 4241 | if (NumOps == 0) { |
| 4242 | // |
| 4243 | // Generate OpReturn. |
| 4244 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4245 | RID = addSPIRVInst(spv::OpReturn); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4246 | } else { |
| 4247 | // |
| 4248 | // Generate OpReturnValue. |
| 4249 | // |
| 4250 | |
| 4251 | // Ops[0] = Return Value ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4252 | SPIRVOperandVec Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4253 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4254 | Ops << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4255 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4256 | RID = addSPIRVInst(spv::OpReturnValue, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4257 | break; |
| 4258 | } |
| 4259 | break; |
| 4260 | } |
| 4261 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4262 | |
| 4263 | // Register Instruction to ValueMap. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4264 | if (RID.isValid()) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4265 | VMap[&I] = RID; |
| 4266 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | void SPIRVProducerPass::GenerateFuncEpilogue() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4270 | |
| 4271 | // |
| 4272 | // Generate OpFunctionEnd |
| 4273 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4274 | addSPIRVInst(spv::OpFunctionEnd); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | bool SPIRVProducerPass::is4xi8vec(Type *Ty) const { |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 4278 | // Don't specialize <4 x i8> if i8 is generally supported. |
| 4279 | if (clspv::Option::Int8Support()) |
| 4280 | return false; |
| 4281 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4282 | LLVMContext &Context = Ty->getContext(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 4283 | if (auto VecTy = dyn_cast<VectorType>(Ty)) { |
| 4284 | if (VecTy->getElementType() == Type::getInt8Ty(Context) && |
| 4285 | VecTy->getNumElements() == 4) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4286 | return true; |
| 4287 | } |
| 4288 | } |
| 4289 | |
| 4290 | return false; |
| 4291 | } |
| 4292 | |
| 4293 | void SPIRVProducerPass::HandleDeferredInstruction() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4294 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 4295 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4296 | for (size_t i = 0; i < DeferredInsts.size(); ++i) { |
| 4297 | Value *Inst = DeferredInsts[i].first; |
| 4298 | SPIRVInstruction *Placeholder = DeferredInsts[i].second; |
| 4299 | SPIRVOperandVec Operands; |
| 4300 | |
| 4301 | auto nextDeferred = [&i, &Inst, &DeferredInsts, &Placeholder]() { |
| 4302 | ++i; |
| 4303 | assert(DeferredInsts.size() > i); |
| 4304 | assert(Inst == DeferredInsts[i].first); |
| 4305 | Placeholder = DeferredInsts[i].second; |
| 4306 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4307 | |
| 4308 | if (BranchInst *Br = dyn_cast<BranchInst>(Inst)) { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4309 | // Check whether this branch needs to be preceeded by merge instruction. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4310 | BasicBlock *BrBB = Br->getParent(); |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4311 | if (ContinueBlocks.count(BrBB)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4312 | // |
| 4313 | // Generate OpLoopMerge. |
| 4314 | // |
| 4315 | // Ops[0] = Merge Block ID |
| 4316 | // Ops[1] = Continue Target ID |
| 4317 | // Ops[2] = Selection Control |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4318 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4319 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4320 | Ops << MergeBlocks[BrBB] << ContinueBlocks[BrBB] |
| 4321 | << spv::LoopControlMaskNone; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4322 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4323 | replaceSPIRVInst(Placeholder, spv::OpLoopMerge, Ops); |
| 4324 | |
| 4325 | nextDeferred(); |
| 4326 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4327 | } else if (MergeBlocks.count(BrBB)) { |
| 4328 | // |
| 4329 | // Generate OpSelectionMerge. |
| 4330 | // |
| 4331 | // Ops[0] = Merge Block ID |
| 4332 | // Ops[1] = Selection Control |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4333 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4334 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4335 | auto MergeBB = MergeBlocks[BrBB]; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4336 | Ops << MergeBB << spv::SelectionControlMaskNone; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4337 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4338 | replaceSPIRVInst(Placeholder, spv::OpSelectionMerge, Ops); |
| 4339 | |
| 4340 | nextDeferred(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | if (Br->isConditional()) { |
| 4344 | // |
| 4345 | // Generate OpBranchConditional. |
| 4346 | // |
| 4347 | // Ops[0] = Condition ID |
| 4348 | // Ops[1] = True Label ID |
| 4349 | // Ops[2] = False Label ID |
| 4350 | // Ops[3] ... Ops[n] = Branch weights (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4351 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4352 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4353 | Ops << Br->getCondition() << Br->getSuccessor(0) << Br->getSuccessor(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4354 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4355 | replaceSPIRVInst(Placeholder, spv::OpBranchConditional, Ops); |
| 4356 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4357 | } else { |
| 4358 | // |
| 4359 | // Generate OpBranch. |
| 4360 | // |
| 4361 | // Ops[0] = Target Label ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4362 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4363 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4364 | Ops << Br->getSuccessor(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4365 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4366 | replaceSPIRVInst(Placeholder, spv::OpBranch, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4367 | } |
| 4368 | } else if (PHINode *PHI = dyn_cast<PHINode>(Inst)) { |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 4369 | if (PHI->getType()->isPointerTy() && !IsSamplerType(PHI->getType()) && |
| 4370 | !IsImageType(PHI->getType())) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4371 | // OpPhi on pointers requires variable pointers. |
| 4372 | setVariablePointersCapabilities( |
| 4373 | PHI->getType()->getPointerAddressSpace()); |
| 4374 | if (!hasVariablePointers() && !selectFromSameObject(PHI)) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4375 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4379 | // |
| 4380 | // Generate OpPhi. |
| 4381 | // |
| 4382 | // Ops[0] = Result Type ID |
| 4383 | // Ops[1] ... Ops[n] = (Variable ID, Parent ID) pairs |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4384 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4385 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4386 | Ops << PHI->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4387 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4388 | for (unsigned j = 0; j < PHI->getNumIncomingValues(); j++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4389 | Ops << PHI->getIncomingValue(j) << PHI->getIncomingBlock(j); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4390 | } |
| 4391 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4392 | replaceSPIRVInst(Placeholder, spv::OpPhi, Ops); |
| 4393 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4394 | } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { |
| 4395 | Function *Callee = Call->getCalledFunction(); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4396 | LLVMContext &Context = Callee->getContext(); |
| 4397 | auto IntTy = Type::getInt32Ty(Context); |
| 4398 | auto callee_code = Builtins::Lookup(Callee); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4399 | auto callee_name = Callee->getName(); |
| 4400 | glsl::ExtInst EInst = getDirectOrIndirectExtInstEnum(callee_name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4401 | |
| 4402 | if (EInst) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4403 | SPIRVID ExtInstImportID = getOpExtInstImportID(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4404 | |
| 4405 | // |
| 4406 | // Generate OpExtInst. |
| 4407 | // |
| 4408 | |
| 4409 | // Ops[0] = Result Type ID |
| 4410 | // Ops[1] = Set ID (OpExtInstImport ID) |
| 4411 | // Ops[2] = Instruction Number (Literal Number) |
| 4412 | // Ops[3] ... Ops[n] = Operand 1, ... , Operand n |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4413 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4414 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4415 | Ops << Call->getType() << ExtInstImportID << EInst; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4416 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4417 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4418 | for (unsigned j = 0; j < CalleeFTy->getNumParams(); j++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4419 | Ops << Call->getOperand(j); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4420 | } |
| 4421 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4422 | SPIRVID RID = replaceSPIRVInst(Placeholder, spv::OpExtInst, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4423 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4424 | const auto IndirectExtInst = getIndirectExtInstEnum(callee_name); |
| 4425 | if (IndirectExtInst != kGlslExtInstBad) { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4426 | |
| 4427 | nextDeferred(); |
| 4428 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4429 | // Generate one more instruction that uses the result of the extended |
| 4430 | // instruction. Its result id is one more than the id of the |
| 4431 | // extended instruction. |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4432 | auto generate_extra_inst = [this, &Context, &Call, &Placeholder, |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4433 | &RID](spv::Op opcode, |
| 4434 | Constant *constant) { |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4435 | // |
| 4436 | // Generate instruction like: |
| 4437 | // result = opcode constant <extinst-result> |
| 4438 | // |
| 4439 | // Ops[0] = Result Type ID |
| 4440 | // Ops[1] = Operand 0 ;; the constant, suitably splatted |
| 4441 | // Ops[2] = Operand 1 ;; the result of the extended instruction |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4442 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4443 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4444 | Type *resultTy = Call->getType(); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4445 | |
| 4446 | if (auto *vectorTy = dyn_cast<VectorType>(resultTy)) { |
| 4447 | constant = ConstantVector::getSplat( |
alan-baker | 7261e06 | 2020-03-15 14:35:48 -0400 | [diff] [blame] | 4448 | {static_cast<unsigned>(vectorTy->getNumElements()), false}, |
| 4449 | constant); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4450 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4451 | Ops << resultTy << constant << RID; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4452 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4453 | replaceSPIRVInst(Placeholder, opcode, Ops); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4454 | }; |
| 4455 | |
| 4456 | switch (IndirectExtInst) { |
| 4457 | case glsl::ExtInstFindUMsb: // Implementing clz |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4458 | generate_extra_inst(spv::OpISub, ConstantInt::get(IntTy, 31)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4459 | break; |
| 4460 | case glsl::ExtInstAcos: // Implementing acospi |
| 4461 | case glsl::ExtInstAsin: // Implementing asinpi |
Kévin Petit | eb9f90a | 2018-09-29 12:29:34 +0100 | [diff] [blame] | 4462 | case glsl::ExtInstAtan: // Implementing atanpi |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4463 | case glsl::ExtInstAtan2: // Implementing atan2pi |
| 4464 | generate_extra_inst( |
| 4465 | spv::OpFMul, |
| 4466 | ConstantFP::get(Type::getFloatTy(Context), kOneOverPi)); |
| 4467 | break; |
| 4468 | |
| 4469 | default: |
| 4470 | assert(false && "internally inconsistent"); |
David Neto | 4d02a53 | 2017-09-17 12:57:44 -0400 | [diff] [blame] | 4471 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4472 | } |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4473 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4474 | } else if (callee_code == Builtins::kPopcount) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4475 | // |
| 4476 | // Generate OpBitCount |
| 4477 | // |
| 4478 | // Ops[0] = Result Type ID |
| 4479 | // Ops[1] = Base ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4480 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4481 | Ops << Call->getType() << Call->getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4482 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4483 | replaceSPIRVInst(Placeholder, spv::OpBitCount, Ops); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4484 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4485 | } else if (callee_name.startswith(kCompositeConstructFunctionPrefix)) { |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4486 | |
| 4487 | // Generate an OpCompositeConstruct |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4488 | SPIRVOperandVec Ops; |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4489 | |
| 4490 | // The result type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4491 | Ops << Call->getType(); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4492 | |
| 4493 | for (Use &use : Call->arg_operands()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4494 | Ops << use.get(); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4495 | } |
| 4496 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4497 | replaceSPIRVInst(Placeholder, spv::OpCompositeConstruct, Ops); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4498 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4499 | } else if (callee_name.startswith(clspv::ResourceAccessorFunction())) { |
| 4500 | |
| 4501 | // We have already mapped the call's result value to an ID. |
| 4502 | // Don't generate any code now. |
| 4503 | |
| 4504 | } else if (callee_name.startswith(clspv::WorkgroupAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4505 | |
| 4506 | // We have already mapped the call's result value to an ID. |
| 4507 | // Don't generate any code now. |
| 4508 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4509 | } else { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4510 | if (Call->getType()->isPointerTy()) { |
| 4511 | // Functions returning pointers require variable pointers. |
| 4512 | setVariablePointersCapabilities( |
| 4513 | Call->getType()->getPointerAddressSpace()); |
| 4514 | } |
| 4515 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4516 | // |
| 4517 | // Generate OpFunctionCall. |
| 4518 | // |
| 4519 | |
| 4520 | // Ops[0] = Result Type ID |
| 4521 | // Ops[1] = Callee Function ID |
| 4522 | // Ops[2] ... Ops[n] = Argument 0, ... , Argument n |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4523 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4524 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4525 | Ops << Call->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4526 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4527 | SPIRVID CalleeID = getSPIRVValue(Callee); |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 4528 | if (CalleeID == 0) { |
| 4529 | errs() << "Can't translate function call. Missing builtin? " |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4530 | << callee_name << " in: " << *Call << "\n"; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 4531 | // TODO(dneto): Can we error out? Enabling this llvm_unreachable |
| 4532 | // causes an infinite loop. Instead, go ahead and generate |
| 4533 | // the bad function call. A validator will catch the 0-Id. |
| 4534 | // llvm_unreachable("Can't translate function call"); |
| 4535 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4536 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4537 | Ops << CalleeID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4538 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4539 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4540 | for (unsigned j = 0; j < CalleeFTy->getNumParams(); j++) { |
| 4541 | auto *operand = Call->getOperand(j); |
alan-baker | d4d5065 | 2019-12-03 17:17:15 -0500 | [diff] [blame] | 4542 | auto *operand_type = operand->getType(); |
| 4543 | // Images and samplers can be passed as function parameters without |
| 4544 | // variable pointers. |
| 4545 | if (operand_type->isPointerTy() && !IsImageType(operand_type) && |
| 4546 | !IsSamplerType(operand_type)) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4547 | auto sc = |
| 4548 | GetStorageClass(operand->getType()->getPointerAddressSpace()); |
| 4549 | if (sc == spv::StorageClassStorageBuffer) { |
| 4550 | // Passing SSBO by reference requires variable pointers storage |
| 4551 | // buffer. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4552 | setVariablePointersStorageBuffer(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4553 | } else if (sc == spv::StorageClassWorkgroup) { |
| 4554 | // Workgroup references require variable pointers if they are not |
| 4555 | // memory object declarations. |
| 4556 | if (auto *operand_call = dyn_cast<CallInst>(operand)) { |
| 4557 | // Workgroup accessor represents a variable reference. |
| 4558 | if (!operand_call->getCalledFunction()->getName().startswith( |
| 4559 | clspv::WorkgroupAccessorFunction())) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4560 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4561 | } else { |
| 4562 | // Arguments are function parameters. |
| 4563 | if (!isa<Argument>(operand)) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4564 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4565 | } |
| 4566 | } |
| 4567 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4568 | Ops << operand; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4569 | } |
| 4570 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4571 | replaceSPIRVInst(Placeholder, spv::OpFunctionCall, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4572 | } |
| 4573 | } |
| 4574 | } |
| 4575 | } |
| 4576 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 4577 | void SPIRVProducerPass::HandleDeferredDecorations() { |
| 4578 | const auto &DL = module->getDataLayout(); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4579 | if (getTypesNeedingArrayStride().empty() && LocalArgSpecIds.empty()) { |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4580 | return; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 4581 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4582 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 4583 | // Insert ArrayStride decorations on pointer types, due to OpPtrAccessChain |
| 4584 | // instructions we generated earlier. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4585 | for (auto *type : getTypesNeedingArrayStride()) { |
| 4586 | Type *elemTy = nullptr; |
| 4587 | if (auto *ptrTy = dyn_cast<PointerType>(type)) { |
| 4588 | elemTy = ptrTy->getElementType(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4589 | } else if (auto *arrayTy = dyn_cast<ArrayType>(type)) { |
alan-baker | 8eb435a | 2020-04-08 00:42:06 -0400 | [diff] [blame] | 4590 | elemTy = arrayTy->getElementType(); |
| 4591 | } else if (auto *vecTy = dyn_cast<VectorType>(type)) { |
| 4592 | elemTy = vecTy->getElementType(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4593 | } else { |
| 4594 | errs() << "Unhandled strided type " << *type << "\n"; |
| 4595 | llvm_unreachable("Unhandled strided type"); |
| 4596 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4597 | |
| 4598 | // Ops[0] = Target ID |
| 4599 | // Ops[1] = Decoration (ArrayStride) |
| 4600 | // Ops[2] = Stride number (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4601 | SPIRVOperandVec Ops; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4602 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4603 | // Same as DL.getIndexedOffsetInType( elemTy, { 1 } ); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 4604 | const uint32_t stride = static_cast<uint32_t>(GetTypeAllocSize(elemTy, DL)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4605 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4606 | Ops << type << spv::DecorationArrayStride << stride; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4607 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4608 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 4609 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4610 | } |
| 4611 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4612 | glsl::ExtInst SPIRVProducerPass::getExtInstEnum(StringRef Name) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4613 | |
| 4614 | const auto &fi = Builtins::Lookup(Name); |
| 4615 | switch (fi) { |
| 4616 | case Builtins::kClamp: { |
| 4617 | auto param_type = fi.getParameter(0); |
| 4618 | if (param_type.type_id == Type::FloatTyID) { |
| 4619 | return glsl::ExtInst::ExtInstFClamp; |
| 4620 | } |
| 4621 | return param_type.is_signed ? glsl::ExtInst::ExtInstSClamp |
| 4622 | : glsl::ExtInst::ExtInstUClamp; |
| 4623 | } |
| 4624 | case Builtins::kMax: { |
| 4625 | auto param_type = fi.getParameter(0); |
| 4626 | if (param_type.type_id == Type::FloatTyID) { |
| 4627 | return glsl::ExtInst::ExtInstFMax; |
| 4628 | } |
| 4629 | return param_type.is_signed ? glsl::ExtInst::ExtInstSMax |
| 4630 | : glsl::ExtInst::ExtInstUMax; |
| 4631 | } |
| 4632 | case Builtins::kMin: { |
| 4633 | auto param_type = fi.getParameter(0); |
| 4634 | if (param_type.type_id == Type::FloatTyID) { |
| 4635 | return glsl::ExtInst::ExtInstFMin; |
| 4636 | } |
| 4637 | return param_type.is_signed ? glsl::ExtInst::ExtInstSMin |
| 4638 | : glsl::ExtInst::ExtInstUMin; |
| 4639 | } |
| 4640 | case Builtins::kAbs: |
| 4641 | return glsl::ExtInst::ExtInstSAbs; |
| 4642 | case Builtins::kFmax: |
| 4643 | return glsl::ExtInst::ExtInstFMax; |
| 4644 | case Builtins::kFmin: |
| 4645 | return glsl::ExtInst::ExtInstFMin; |
| 4646 | case Builtins::kDegrees: |
| 4647 | return glsl::ExtInst::ExtInstDegrees; |
| 4648 | case Builtins::kRadians: |
| 4649 | return glsl::ExtInst::ExtInstRadians; |
| 4650 | case Builtins::kMix: |
| 4651 | return glsl::ExtInst::ExtInstFMix; |
| 4652 | case Builtins::kAcos: |
| 4653 | case Builtins::kAcospi: |
| 4654 | return glsl::ExtInst::ExtInstAcos; |
| 4655 | case Builtins::kAcosh: |
| 4656 | return glsl::ExtInst::ExtInstAcosh; |
| 4657 | case Builtins::kAsin: |
| 4658 | case Builtins::kAsinpi: |
| 4659 | return glsl::ExtInst::ExtInstAsin; |
| 4660 | case Builtins::kAsinh: |
| 4661 | return glsl::ExtInst::ExtInstAsinh; |
| 4662 | case Builtins::kAtan: |
| 4663 | case Builtins::kAtanpi: |
| 4664 | return glsl::ExtInst::ExtInstAtan; |
| 4665 | case Builtins::kAtanh: |
| 4666 | return glsl::ExtInst::ExtInstAtanh; |
| 4667 | case Builtins::kAtan2: |
| 4668 | case Builtins::kAtan2pi: |
| 4669 | return glsl::ExtInst::ExtInstAtan2; |
| 4670 | case Builtins::kCeil: |
| 4671 | return glsl::ExtInst::ExtInstCeil; |
| 4672 | case Builtins::kSin: |
| 4673 | case Builtins::kHalfSin: |
| 4674 | case Builtins::kNativeSin: |
| 4675 | return glsl::ExtInst::ExtInstSin; |
| 4676 | case Builtins::kSinh: |
| 4677 | return glsl::ExtInst::ExtInstSinh; |
| 4678 | case Builtins::kCos: |
| 4679 | case Builtins::kHalfCos: |
| 4680 | case Builtins::kNativeCos: |
| 4681 | return glsl::ExtInst::ExtInstCos; |
| 4682 | case Builtins::kCosh: |
| 4683 | return glsl::ExtInst::ExtInstCosh; |
| 4684 | case Builtins::kTan: |
| 4685 | case Builtins::kHalfTan: |
| 4686 | case Builtins::kNativeTan: |
| 4687 | return glsl::ExtInst::ExtInstTan; |
| 4688 | case Builtins::kTanh: |
| 4689 | return glsl::ExtInst::ExtInstTanh; |
| 4690 | case Builtins::kExp: |
| 4691 | case Builtins::kHalfExp: |
| 4692 | case Builtins::kNativeExp: |
| 4693 | return glsl::ExtInst::ExtInstExp; |
| 4694 | case Builtins::kExp2: |
| 4695 | case Builtins::kHalfExp2: |
| 4696 | case Builtins::kNativeExp2: |
| 4697 | return glsl::ExtInst::ExtInstExp2; |
| 4698 | case Builtins::kLog: |
| 4699 | case Builtins::kHalfLog: |
| 4700 | case Builtins::kNativeLog: |
| 4701 | return glsl::ExtInst::ExtInstLog; |
| 4702 | case Builtins::kLog2: |
| 4703 | case Builtins::kHalfLog2: |
| 4704 | case Builtins::kNativeLog2: |
| 4705 | return glsl::ExtInst::ExtInstLog2; |
| 4706 | case Builtins::kFabs: |
| 4707 | return glsl::ExtInst::ExtInstFAbs; |
| 4708 | case Builtins::kFma: |
| 4709 | return glsl::ExtInst::ExtInstFma; |
| 4710 | case Builtins::kFloor: |
| 4711 | return glsl::ExtInst::ExtInstFloor; |
| 4712 | case Builtins::kLdexp: |
| 4713 | return glsl::ExtInst::ExtInstLdexp; |
| 4714 | case Builtins::kPow: |
| 4715 | case Builtins::kPowr: |
| 4716 | case Builtins::kHalfPowr: |
| 4717 | case Builtins::kNativePowr: |
| 4718 | return glsl::ExtInst::ExtInstPow; |
| 4719 | case Builtins::kRound: |
| 4720 | return glsl::ExtInst::ExtInstRound; |
| 4721 | case Builtins::kSqrt: |
| 4722 | case Builtins::kHalfSqrt: |
| 4723 | case Builtins::kNativeSqrt: |
| 4724 | return glsl::ExtInst::ExtInstSqrt; |
| 4725 | case Builtins::kRsqrt: |
| 4726 | case Builtins::kHalfRsqrt: |
| 4727 | case Builtins::kNativeRsqrt: |
| 4728 | return glsl::ExtInst::ExtInstInverseSqrt; |
| 4729 | case Builtins::kTrunc: |
| 4730 | return glsl::ExtInst::ExtInstTrunc; |
| 4731 | case Builtins::kFrexp: |
| 4732 | return glsl::ExtInst::ExtInstFrexp; |
| 4733 | case Builtins::kFract: |
| 4734 | return glsl::ExtInst::ExtInstFract; |
| 4735 | case Builtins::kSign: |
| 4736 | return glsl::ExtInst::ExtInstFSign; |
| 4737 | case Builtins::kLength: |
| 4738 | case Builtins::kFastLength: |
| 4739 | return glsl::ExtInst::ExtInstLength; |
| 4740 | case Builtins::kDistance: |
| 4741 | case Builtins::kFastDistance: |
| 4742 | return glsl::ExtInst::ExtInstDistance; |
| 4743 | case Builtins::kStep: |
| 4744 | return glsl::ExtInst::ExtInstStep; |
| 4745 | case Builtins::kSmoothstep: |
| 4746 | return glsl::ExtInst::ExtInstSmoothStep; |
| 4747 | case Builtins::kCross: |
| 4748 | return glsl::ExtInst::ExtInstCross; |
| 4749 | case Builtins::kNormalize: |
| 4750 | case Builtins::kFastNormalize: |
| 4751 | return glsl::ExtInst::ExtInstNormalize; |
| 4752 | default: |
| 4753 | break; |
| 4754 | } |
| 4755 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4756 | return StringSwitch<glsl::ExtInst>(Name) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4757 | .StartsWith("llvm.fmuladd.", glsl::ExtInst::ExtInstFma) |
| 4758 | .Case("spirv.unpack.v2f16", glsl::ExtInst::ExtInstUnpackHalf2x16) |
| 4759 | .Case("spirv.pack.v2f16", glsl::ExtInst::ExtInstPackHalf2x16) |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4760 | .Default(kGlslExtInstBad); |
| 4761 | } |
| 4762 | |
| 4763 | glsl::ExtInst SPIRVProducerPass::getIndirectExtInstEnum(StringRef Name) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4764 | switch (Builtins::Lookup(Name)) { |
| 4765 | case Builtins::kClz: |
| 4766 | return glsl::ExtInst::ExtInstFindUMsb; |
| 4767 | case Builtins::kAcospi: |
| 4768 | return glsl::ExtInst::ExtInstAcos; |
| 4769 | case Builtins::kAsinpi: |
| 4770 | return glsl::ExtInst::ExtInstAsin; |
| 4771 | case Builtins::kAtanpi: |
| 4772 | return glsl::ExtInst::ExtInstAtan; |
| 4773 | case Builtins::kAtan2pi: |
| 4774 | return glsl::ExtInst::ExtInstAtan2; |
| 4775 | default: |
| 4776 | break; |
| 4777 | } |
| 4778 | return kGlslExtInstBad; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4779 | } |
| 4780 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4781 | glsl::ExtInst |
| 4782 | SPIRVProducerPass::getDirectOrIndirectExtInstEnum(StringRef Name) { |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4783 | auto direct = getExtInstEnum(Name); |
| 4784 | if (direct != kGlslExtInstBad) |
| 4785 | return direct; |
| 4786 | return getIndirectExtInstEnum(Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4787 | } |
| 4788 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4789 | void SPIRVProducerPass::WriteOneWord(uint32_t Word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 4790 | binaryOut->write(reinterpret_cast<const char *>(&Word), sizeof(uint32_t)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4791 | } |
| 4792 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4793 | void SPIRVProducerPass::WriteResultID(const SPIRVInstruction &Inst) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 4794 | WriteOneWord(Inst.getResultID().get()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4795 | } |
| 4796 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4797 | void SPIRVProducerPass::WriteWordCountAndOpcode(const SPIRVInstruction &Inst) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4798 | // High 16 bit : Word Count |
| 4799 | // Low 16 bit : Opcode |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4800 | uint32_t Word = Inst.getOpcode(); |
| 4801 | const uint32_t count = Inst.getWordCount(); |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 4802 | if (count > 65535) { |
| 4803 | errs() << "Word count limit of 65535 exceeded: " << count << "\n"; |
| 4804 | llvm_unreachable("Word count too high"); |
| 4805 | } |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4806 | Word |= Inst.getWordCount() << 16; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4807 | WriteOneWord(Word); |
| 4808 | } |
| 4809 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4810 | void SPIRVProducerPass::WriteOperand(const SPIRVOperand &Op) { |
| 4811 | SPIRVOperandType OpTy = Op.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4812 | switch (OpTy) { |
| 4813 | default: { |
| 4814 | llvm_unreachable("Unsupported SPIRV Operand Type???"); |
| 4815 | break; |
| 4816 | } |
| 4817 | case SPIRVOperandType::NUMBERID: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4818 | WriteOneWord(Op.getNumID()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4819 | break; |
| 4820 | } |
| 4821 | case SPIRVOperandType::LITERAL_STRING: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4822 | std::string Str = Op.getLiteralStr(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4823 | const char *Data = Str.c_str(); |
| 4824 | size_t WordSize = Str.size() / 4; |
| 4825 | for (unsigned Idx = 0; Idx < WordSize; Idx++) { |
| 4826 | WriteOneWord(*reinterpret_cast<const uint32_t *>(&Data[4 * Idx])); |
| 4827 | } |
| 4828 | |
| 4829 | uint32_t Remainder = Str.size() % 4; |
| 4830 | uint32_t LastWord = 0; |
| 4831 | if (Remainder) { |
| 4832 | for (unsigned Idx = 0; Idx < Remainder; Idx++) { |
| 4833 | LastWord |= Data[4 * WordSize + Idx] << 8 * Idx; |
| 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | WriteOneWord(LastWord); |
| 4838 | break; |
| 4839 | } |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4840 | case SPIRVOperandType::LITERAL_WORD: { |
| 4841 | WriteOneWord(Op.getLiteralNum()[0]); |
| 4842 | break; |
| 4843 | } |
| 4844 | case SPIRVOperandType::LITERAL_DWORD: { |
| 4845 | WriteOneWord(Op.getLiteralNum()[0]); |
| 4846 | WriteOneWord(Op.getLiteralNum()[1]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4847 | break; |
| 4848 | } |
| 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | void SPIRVProducerPass::WriteSPIRVBinary() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 4853 | for (int i = 0; i < kSectionCount; ++i) { |
| 4854 | WriteSPIRVBinary(SPIRVSections[i]); |
| 4855 | } |
| 4856 | } |
| 4857 | |
| 4858 | void SPIRVProducerPass::WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4859 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4860 | for (const auto &Inst : SPIRVInstList) { |
| 4861 | const auto &Ops = Inst.getOperands(); |
| 4862 | spv::Op Opcode = static_cast<spv::Op>(Inst.getOpcode()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4863 | |
| 4864 | switch (Opcode) { |
| 4865 | default: { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 4866 | errs() << "Unsupported SPIR-V instruction opcode " << int(Opcode) << "\n"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4867 | llvm_unreachable("Unsupported SPIRV instruction"); |
| 4868 | break; |
| 4869 | } |
| 4870 | case spv::OpCapability: |
| 4871 | case spv::OpExtension: |
| 4872 | case spv::OpMemoryModel: |
| 4873 | case spv::OpEntryPoint: |
| 4874 | case spv::OpExecutionMode: |
| 4875 | case spv::OpSource: |
| 4876 | case spv::OpDecorate: |
| 4877 | case spv::OpMemberDecorate: |
| 4878 | case spv::OpBranch: |
| 4879 | case spv::OpBranchConditional: |
| 4880 | case spv::OpSelectionMerge: |
| 4881 | case spv::OpLoopMerge: |
| 4882 | case spv::OpStore: |
| 4883 | case spv::OpImageWrite: |
| 4884 | case spv::OpReturnValue: |
| 4885 | case spv::OpControlBarrier: |
| 4886 | case spv::OpMemoryBarrier: |
| 4887 | case spv::OpReturn: |
| 4888 | case spv::OpFunctionEnd: |
| 4889 | case spv::OpCopyMemory: { |
| 4890 | WriteWordCountAndOpcode(Inst); |
| 4891 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 4892 | WriteOperand(Ops[i]); |
| 4893 | } |
| 4894 | break; |
| 4895 | } |
| 4896 | case spv::OpTypeBool: |
| 4897 | case spv::OpTypeVoid: |
| 4898 | case spv::OpTypeSampler: |
| 4899 | case spv::OpLabel: |
| 4900 | case spv::OpExtInstImport: |
| 4901 | case spv::OpTypePointer: |
| 4902 | case spv::OpTypeRuntimeArray: |
| 4903 | case spv::OpTypeStruct: |
| 4904 | case spv::OpTypeImage: |
| 4905 | case spv::OpTypeSampledImage: |
| 4906 | case spv::OpTypeInt: |
| 4907 | case spv::OpTypeFloat: |
| 4908 | case spv::OpTypeArray: |
| 4909 | case spv::OpTypeVector: |
| 4910 | case spv::OpTypeFunction: { |
| 4911 | WriteWordCountAndOpcode(Inst); |
| 4912 | WriteResultID(Inst); |
| 4913 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 4914 | WriteOperand(Ops[i]); |
| 4915 | } |
| 4916 | break; |
| 4917 | } |
| 4918 | case spv::OpFunction: |
| 4919 | case spv::OpFunctionParameter: |
| 4920 | case spv::OpAccessChain: |
| 4921 | case spv::OpPtrAccessChain: |
| 4922 | case spv::OpInBoundsAccessChain: |
| 4923 | case spv::OpUConvert: |
| 4924 | case spv::OpSConvert: |
| 4925 | case spv::OpConvertFToU: |
| 4926 | case spv::OpConvertFToS: |
| 4927 | case spv::OpConvertUToF: |
| 4928 | case spv::OpConvertSToF: |
| 4929 | case spv::OpFConvert: |
| 4930 | case spv::OpConvertPtrToU: |
| 4931 | case spv::OpConvertUToPtr: |
| 4932 | case spv::OpBitcast: |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 4933 | case spv::OpFNegate: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4934 | case spv::OpIAdd: |
| 4935 | case spv::OpFAdd: |
| 4936 | case spv::OpISub: |
| 4937 | case spv::OpFSub: |
| 4938 | case spv::OpIMul: |
| 4939 | case spv::OpFMul: |
| 4940 | case spv::OpUDiv: |
| 4941 | case spv::OpSDiv: |
| 4942 | case spv::OpFDiv: |
| 4943 | case spv::OpUMod: |
| 4944 | case spv::OpSRem: |
| 4945 | case spv::OpFRem: |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 4946 | case spv::OpUMulExtended: |
| 4947 | case spv::OpSMulExtended: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4948 | case spv::OpBitwiseOr: |
| 4949 | case spv::OpBitwiseXor: |
| 4950 | case spv::OpBitwiseAnd: |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 4951 | case spv::OpNot: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4952 | case spv::OpShiftLeftLogical: |
| 4953 | case spv::OpShiftRightLogical: |
| 4954 | case spv::OpShiftRightArithmetic: |
| 4955 | case spv::OpBitCount: |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4956 | case spv::OpCompositeConstruct: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4957 | case spv::OpCompositeExtract: |
| 4958 | case spv::OpVectorExtractDynamic: |
| 4959 | case spv::OpCompositeInsert: |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4960 | case spv::OpCopyObject: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4961 | case spv::OpVectorInsertDynamic: |
| 4962 | case spv::OpVectorShuffle: |
| 4963 | case spv::OpIEqual: |
| 4964 | case spv::OpINotEqual: |
| 4965 | case spv::OpUGreaterThan: |
| 4966 | case spv::OpUGreaterThanEqual: |
| 4967 | case spv::OpULessThan: |
| 4968 | case spv::OpULessThanEqual: |
| 4969 | case spv::OpSGreaterThan: |
| 4970 | case spv::OpSGreaterThanEqual: |
| 4971 | case spv::OpSLessThan: |
| 4972 | case spv::OpSLessThanEqual: |
| 4973 | case spv::OpFOrdEqual: |
| 4974 | case spv::OpFOrdGreaterThan: |
| 4975 | case spv::OpFOrdGreaterThanEqual: |
| 4976 | case spv::OpFOrdLessThan: |
| 4977 | case spv::OpFOrdLessThanEqual: |
| 4978 | case spv::OpFOrdNotEqual: |
| 4979 | case spv::OpFUnordEqual: |
| 4980 | case spv::OpFUnordGreaterThan: |
| 4981 | case spv::OpFUnordGreaterThanEqual: |
| 4982 | case spv::OpFUnordLessThan: |
| 4983 | case spv::OpFUnordLessThanEqual: |
| 4984 | case spv::OpFUnordNotEqual: |
| 4985 | case spv::OpExtInst: |
| 4986 | case spv::OpIsInf: |
| 4987 | case spv::OpIsNan: |
| 4988 | case spv::OpAny: |
| 4989 | case spv::OpAll: |
| 4990 | case spv::OpUndef: |
| 4991 | case spv::OpConstantNull: |
| 4992 | case spv::OpLogicalOr: |
| 4993 | case spv::OpLogicalAnd: |
| 4994 | case spv::OpLogicalNot: |
| 4995 | case spv::OpLogicalNotEqual: |
| 4996 | case spv::OpConstantComposite: |
| 4997 | case spv::OpSpecConstantComposite: |
| 4998 | case spv::OpConstantTrue: |
| 4999 | case spv::OpConstantFalse: |
| 5000 | case spv::OpConstant: |
| 5001 | case spv::OpSpecConstant: |
| 5002 | case spv::OpVariable: |
| 5003 | case spv::OpFunctionCall: |
| 5004 | case spv::OpSampledImage: |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 5005 | case spv::OpImageFetch: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5006 | case spv::OpImageSampleExplicitLod: |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5007 | case spv::OpImageQuerySize: |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5008 | case spv::OpImageQuerySizeLod: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5009 | case spv::OpSelect: |
| 5010 | case spv::OpPhi: |
| 5011 | case spv::OpLoad: |
| 5012 | case spv::OpAtomicIAdd: |
| 5013 | case spv::OpAtomicISub: |
| 5014 | case spv::OpAtomicExchange: |
| 5015 | case spv::OpAtomicIIncrement: |
| 5016 | case spv::OpAtomicIDecrement: |
| 5017 | case spv::OpAtomicCompareExchange: |
| 5018 | case spv::OpAtomicUMin: |
| 5019 | case spv::OpAtomicSMin: |
| 5020 | case spv::OpAtomicUMax: |
| 5021 | case spv::OpAtomicSMax: |
| 5022 | case spv::OpAtomicAnd: |
| 5023 | case spv::OpAtomicOr: |
| 5024 | case spv::OpAtomicXor: |
| 5025 | case spv::OpDot: { |
| 5026 | WriteWordCountAndOpcode(Inst); |
| 5027 | WriteOperand(Ops[0]); |
| 5028 | WriteResultID(Inst); |
| 5029 | for (uint32_t i = 1; i < Ops.size(); i++) { |
| 5030 | WriteOperand(Ops[i]); |
| 5031 | } |
| 5032 | break; |
| 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | } |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5037 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5038 | bool SPIRVProducerPass::IsTypeNullable(const Type *type) const { |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5039 | switch (type->getTypeID()) { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5040 | case Type::HalfTyID: |
| 5041 | case Type::FloatTyID: |
| 5042 | case Type::DoubleTyID: |
| 5043 | case Type::IntegerTyID: |
James Price | 59a1c75 | 2020-04-23 23:06:16 -0400 | [diff] [blame] | 5044 | case Type::FixedVectorTyID: |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5045 | return true; |
| 5046 | case Type::PointerTyID: { |
| 5047 | const PointerType *pointer_type = cast<PointerType>(type); |
| 5048 | if (pointer_type->getPointerAddressSpace() != |
| 5049 | AddressSpace::UniformConstant) { |
| 5050 | auto pointee_type = pointer_type->getPointerElementType(); |
| 5051 | if (pointee_type->isStructTy() && |
| 5052 | cast<StructType>(pointee_type)->isOpaque()) { |
| 5053 | // Images and samplers are not nullable. |
| 5054 | return false; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5055 | } |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5056 | } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5057 | return true; |
| 5058 | } |
| 5059 | case Type::ArrayTyID: |
alan-baker | 8eb435a | 2020-04-08 00:42:06 -0400 | [diff] [blame] | 5060 | return IsTypeNullable(type->getArrayElementType()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5061 | case Type::StructTyID: { |
| 5062 | const StructType *struct_type = cast<StructType>(type); |
| 5063 | // Images and samplers are not nullable. |
| 5064 | if (struct_type->isOpaque()) |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5065 | return false; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5066 | for (const auto element : struct_type->elements()) { |
| 5067 | if (!IsTypeNullable(element)) |
| 5068 | return false; |
| 5069 | } |
| 5070 | return true; |
| 5071 | } |
| 5072 | default: |
| 5073 | return false; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5074 | } |
| 5075 | } |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5076 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5077 | void SPIRVProducerPass::PopulateUBOTypeMaps() { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5078 | if (auto *offsets_md = |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5079 | module->getNamedMetadata(clspv::RemappedTypeOffsetMetadataName())) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5080 | // Metdata is stored as key-value pair operands. The first element of each |
| 5081 | // operand is the type and the second is a vector of offsets. |
| 5082 | for (const auto *operand : offsets_md->operands()) { |
| 5083 | const auto *pair = cast<MDTuple>(operand); |
| 5084 | auto *type = |
| 5085 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 5086 | const auto *offset_vector = cast<MDTuple>(pair->getOperand(1)); |
| 5087 | std::vector<uint32_t> offsets; |
| 5088 | for (const Metadata *offset_md : offset_vector->operands()) { |
| 5089 | const auto *constant_md = cast<ConstantAsMetadata>(offset_md); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5090 | offsets.push_back(static_cast<uint32_t>( |
| 5091 | cast<ConstantInt>(constant_md->getValue())->getZExtValue())); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5092 | } |
| 5093 | RemappedUBOTypeOffsets.insert(std::make_pair(type, offsets)); |
| 5094 | } |
| 5095 | } |
| 5096 | |
| 5097 | if (auto *sizes_md = |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5098 | module->getNamedMetadata(clspv::RemappedTypeSizesMetadataName())) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5099 | // Metadata is stored as key-value pair operands. The first element of each |
| 5100 | // operand is the type and the second is a triple of sizes: type size in |
| 5101 | // bits, store size and alloc size. |
| 5102 | for (const auto *operand : sizes_md->operands()) { |
| 5103 | const auto *pair = cast<MDTuple>(operand); |
| 5104 | auto *type = |
| 5105 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 5106 | const auto *size_triple = cast<MDTuple>(pair->getOperand(1)); |
| 5107 | uint64_t type_size_in_bits = |
| 5108 | cast<ConstantInt>( |
| 5109 | cast<ConstantAsMetadata>(size_triple->getOperand(0))->getValue()) |
| 5110 | ->getZExtValue(); |
| 5111 | uint64_t type_store_size = |
| 5112 | cast<ConstantInt>( |
| 5113 | cast<ConstantAsMetadata>(size_triple->getOperand(1))->getValue()) |
| 5114 | ->getZExtValue(); |
| 5115 | uint64_t type_alloc_size = |
| 5116 | cast<ConstantInt>( |
| 5117 | cast<ConstantAsMetadata>(size_triple->getOperand(2))->getValue()) |
| 5118 | ->getZExtValue(); |
| 5119 | RemappedUBOTypeSizes.insert(std::make_pair( |
| 5120 | type, std::make_tuple(type_size_in_bits, type_store_size, |
| 5121 | type_alloc_size))); |
| 5122 | } |
| 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | uint64_t SPIRVProducerPass::GetTypeSizeInBits(Type *type, |
| 5127 | const DataLayout &DL) { |
| 5128 | auto iter = RemappedUBOTypeSizes.find(type); |
| 5129 | if (iter != RemappedUBOTypeSizes.end()) { |
| 5130 | return std::get<0>(iter->second); |
| 5131 | } |
| 5132 | |
| 5133 | return DL.getTypeSizeInBits(type); |
| 5134 | } |
| 5135 | |
| 5136 | uint64_t SPIRVProducerPass::GetTypeStoreSize(Type *type, const DataLayout &DL) { |
| 5137 | auto iter = RemappedUBOTypeSizes.find(type); |
| 5138 | if (iter != RemappedUBOTypeSizes.end()) { |
| 5139 | return std::get<1>(iter->second); |
| 5140 | } |
| 5141 | |
| 5142 | return DL.getTypeStoreSize(type); |
| 5143 | } |
| 5144 | |
| 5145 | uint64_t SPIRVProducerPass::GetTypeAllocSize(Type *type, const DataLayout &DL) { |
| 5146 | auto iter = RemappedUBOTypeSizes.find(type); |
| 5147 | if (iter != RemappedUBOTypeSizes.end()) { |
| 5148 | return std::get<2>(iter->second); |
| 5149 | } |
| 5150 | |
| 5151 | return DL.getTypeAllocSize(type); |
| 5152 | } |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5153 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 5154 | uint32_t SPIRVProducerPass::GetExplicitLayoutStructMemberOffset( |
| 5155 | StructType *type, unsigned member, const DataLayout &DL) { |
| 5156 | const auto StructLayout = DL.getStructLayout(type); |
| 5157 | // Search for the correct offsets if this type was remapped. |
| 5158 | std::vector<uint32_t> *offsets = nullptr; |
| 5159 | auto iter = RemappedUBOTypeOffsets.find(type); |
| 5160 | if (iter != RemappedUBOTypeOffsets.end()) { |
| 5161 | offsets = &iter->second; |
| 5162 | } |
| 5163 | auto ByteOffset = |
| 5164 | static_cast<uint32_t>(StructLayout->getElementOffset(member)); |
| 5165 | if (offsets) { |
| 5166 | ByteOffset = (*offsets)[member]; |
| 5167 | } |
| 5168 | |
| 5169 | return ByteOffset; |
| 5170 | } |
| 5171 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 5172 | void SPIRVProducerPass::setVariablePointersCapabilities( |
| 5173 | unsigned address_space) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5174 | if (GetStorageClass(address_space) == spv::StorageClassStorageBuffer) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 5175 | setVariablePointersStorageBuffer(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5176 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame^] | 5177 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5178 | } |
| 5179 | } |
| 5180 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 5181 | Value *SPIRVProducerPass::GetBasePointer(Value *v) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5182 | if (auto *gep = dyn_cast<GetElementPtrInst>(v)) { |
| 5183 | return GetBasePointer(gep->getPointerOperand()); |
| 5184 | } |
| 5185 | |
| 5186 | // Conservatively return |v|. |
| 5187 | return v; |
| 5188 | } |
| 5189 | |
| 5190 | bool SPIRVProducerPass::sameResource(Value *lhs, Value *rhs) const { |
| 5191 | if (auto *lhs_call = dyn_cast<CallInst>(lhs)) { |
| 5192 | if (auto *rhs_call = dyn_cast<CallInst>(rhs)) { |
| 5193 | if (lhs_call->getCalledFunction()->getName().startswith( |
| 5194 | clspv::ResourceAccessorFunction()) && |
| 5195 | rhs_call->getCalledFunction()->getName().startswith( |
| 5196 | clspv::ResourceAccessorFunction())) { |
| 5197 | // For resource accessors, match descriptor set and binding. |
| 5198 | if (lhs_call->getOperand(0) == rhs_call->getOperand(0) && |
| 5199 | lhs_call->getOperand(1) == rhs_call->getOperand(1)) |
| 5200 | return true; |
| 5201 | } else if (lhs_call->getCalledFunction()->getName().startswith( |
| 5202 | clspv::WorkgroupAccessorFunction()) && |
| 5203 | rhs_call->getCalledFunction()->getName().startswith( |
| 5204 | clspv::WorkgroupAccessorFunction())) { |
| 5205 | // For workgroup resources, match spec id. |
| 5206 | if (lhs_call->getOperand(0) == rhs_call->getOperand(0)) |
| 5207 | return true; |
| 5208 | } |
| 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | return false; |
| 5213 | } |
| 5214 | |
| 5215 | bool SPIRVProducerPass::selectFromSameObject(Instruction *inst) { |
| 5216 | assert(inst->getType()->isPointerTy()); |
| 5217 | assert(GetStorageClass(inst->getType()->getPointerAddressSpace()) == |
| 5218 | spv::StorageClassStorageBuffer); |
| 5219 | const bool hack_undef = clspv::Option::HackUndef(); |
| 5220 | if (auto *select = dyn_cast<SelectInst>(inst)) { |
| 5221 | auto *true_base = GetBasePointer(select->getTrueValue()); |
| 5222 | auto *false_base = GetBasePointer(select->getFalseValue()); |
| 5223 | |
| 5224 | if (true_base == false_base) |
| 5225 | return true; |
| 5226 | |
| 5227 | // If either the true or false operand is a null, then we satisfy the same |
| 5228 | // object constraint. |
| 5229 | if (auto *true_cst = dyn_cast<Constant>(true_base)) { |
| 5230 | if (true_cst->isNullValue() || (hack_undef && isa<UndefValue>(true_base))) |
| 5231 | return true; |
| 5232 | } |
| 5233 | |
| 5234 | if (auto *false_cst = dyn_cast<Constant>(false_base)) { |
| 5235 | if (false_cst->isNullValue() || |
| 5236 | (hack_undef && isa<UndefValue>(false_base))) |
| 5237 | return true; |
| 5238 | } |
| 5239 | |
| 5240 | if (sameResource(true_base, false_base)) |
| 5241 | return true; |
| 5242 | } else if (auto *phi = dyn_cast<PHINode>(inst)) { |
| 5243 | Value *value = nullptr; |
| 5244 | bool ok = true; |
| 5245 | for (unsigned i = 0; ok && i != phi->getNumIncomingValues(); ++i) { |
| 5246 | auto *base = GetBasePointer(phi->getIncomingValue(i)); |
| 5247 | // Null values satisfy the constraint of selecting of selecting from the |
| 5248 | // same object. |
| 5249 | if (!value) { |
| 5250 | if (auto *cst = dyn_cast<Constant>(base)) { |
| 5251 | if (!cst->isNullValue() && !(hack_undef && isa<UndefValue>(base))) |
| 5252 | value = base; |
| 5253 | } else { |
| 5254 | value = base; |
| 5255 | } |
| 5256 | } else if (base != value) { |
| 5257 | if (auto *base_cst = dyn_cast<Constant>(base)) { |
| 5258 | if (base_cst->isNullValue() || (hack_undef && isa<UndefValue>(base))) |
| 5259 | continue; |
| 5260 | } |
| 5261 | |
| 5262 | if (sameResource(value, base)) |
| 5263 | continue; |
| 5264 | |
| 5265 | // Values don't represent the same base. |
| 5266 | ok = false; |
| 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | return ok; |
| 5271 | } |
| 5272 | |
| 5273 | // Conservatively return false. |
| 5274 | return false; |
| 5275 | } |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 5276 | |
| 5277 | bool SPIRVProducerPass::CalledWithCoherentResource(Argument &Arg) { |
| 5278 | if (!Arg.getType()->isPointerTy() || |
| 5279 | Arg.getType()->getPointerAddressSpace() != clspv::AddressSpace::Global) { |
| 5280 | // Only SSBOs need to be annotated as coherent. |
| 5281 | return false; |
| 5282 | } |
| 5283 | |
| 5284 | DenseSet<Value *> visited; |
| 5285 | std::vector<Value *> stack; |
| 5286 | for (auto *U : Arg.getParent()->users()) { |
| 5287 | if (auto *call = dyn_cast<CallInst>(U)) { |
| 5288 | stack.push_back(call->getOperand(Arg.getArgNo())); |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | while (!stack.empty()) { |
| 5293 | Value *v = stack.back(); |
| 5294 | stack.pop_back(); |
| 5295 | |
| 5296 | if (!visited.insert(v).second) |
| 5297 | continue; |
| 5298 | |
| 5299 | auto *resource_call = dyn_cast<CallInst>(v); |
| 5300 | if (resource_call && |
| 5301 | resource_call->getCalledFunction()->getName().startswith( |
| 5302 | clspv::ResourceAccessorFunction())) { |
| 5303 | // If this is a resource accessor function, check if the coherent operand |
| 5304 | // is set. |
| 5305 | const auto coherent = |
| 5306 | unsigned(dyn_cast<ConstantInt>(resource_call->getArgOperand(5)) |
| 5307 | ->getZExtValue()); |
| 5308 | if (coherent == 1) |
| 5309 | return true; |
| 5310 | } else if (auto *arg = dyn_cast<Argument>(v)) { |
| 5311 | // If this is a function argument, trace through its callers. |
alan-baker | e98f3f9 | 2019-04-08 15:06:36 -0400 | [diff] [blame] | 5312 | for (auto U : arg->getParent()->users()) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 5313 | if (auto *call = dyn_cast<CallInst>(U)) { |
| 5314 | stack.push_back(call->getOperand(arg->getArgNo())); |
| 5315 | } |
| 5316 | } |
| 5317 | } else if (auto *user = dyn_cast<User>(v)) { |
| 5318 | // If this is a user, traverse all operands that could lead to resource |
| 5319 | // variables. |
| 5320 | for (unsigned i = 0; i != user->getNumOperands(); ++i) { |
| 5321 | Value *operand = user->getOperand(i); |
| 5322 | if (operand->getType()->isPointerTy() && |
| 5323 | operand->getType()->getPointerAddressSpace() == |
| 5324 | clspv::AddressSpace::Global) { |
| 5325 | stack.push_back(operand); |
| 5326 | } |
| 5327 | } |
| 5328 | } |
| 5329 | } |
| 5330 | |
| 5331 | // No coherent resource variables encountered. |
| 5332 | return false; |
| 5333 | } |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5334 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5335 | void SPIRVProducerPass::PopulateStructuredCFGMaps() { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5336 | // First, track loop merges and continues. |
| 5337 | DenseSet<BasicBlock *> LoopMergesAndContinues; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5338 | for (auto &F : *module) { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5339 | if (F.isDeclaration()) |
| 5340 | continue; |
| 5341 | |
| 5342 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); |
| 5343 | const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>(F).getLoopInfo(); |
| 5344 | std::deque<BasicBlock *> order; |
| 5345 | DenseSet<BasicBlock *> visited; |
| 5346 | clspv::ComputeStructuredOrder(&*F.begin(), &DT, LI, &order, &visited); |
| 5347 | |
| 5348 | for (auto BB : order) { |
| 5349 | auto terminator = BB->getTerminator(); |
| 5350 | auto branch = dyn_cast<BranchInst>(terminator); |
| 5351 | if (LI.isLoopHeader(BB)) { |
| 5352 | auto L = LI.getLoopFor(BB); |
| 5353 | BasicBlock *ContinueBB = nullptr; |
| 5354 | BasicBlock *MergeBB = nullptr; |
| 5355 | |
| 5356 | MergeBB = L->getExitBlock(); |
| 5357 | if (!MergeBB) { |
| 5358 | // StructurizeCFG pass converts CFG into triangle shape and the cfg |
| 5359 | // has regions with single entry/exit. As a result, loop should not |
| 5360 | // have multiple exits. |
| 5361 | llvm_unreachable("Loop has multiple exits???"); |
| 5362 | } |
| 5363 | |
| 5364 | if (L->isLoopLatch(BB)) { |
| 5365 | ContinueBB = BB; |
| 5366 | } else { |
| 5367 | // From SPIR-V spec 2.11, Continue Target must dominate that back-edge |
| 5368 | // block. |
| 5369 | BasicBlock *Header = L->getHeader(); |
| 5370 | BasicBlock *Latch = L->getLoopLatch(); |
| 5371 | for (auto *loop_block : L->blocks()) { |
| 5372 | if (loop_block == Header) { |
| 5373 | continue; |
| 5374 | } |
| 5375 | |
| 5376 | // Check whether block dominates block with back-edge. |
| 5377 | // The loop latch is the single block with a back-edge. If it was |
| 5378 | // possible, StructurizeCFG made the loop conform to this |
| 5379 | // requirement, otherwise |Latch| is a nullptr. |
| 5380 | if (DT.dominates(loop_block, Latch)) { |
| 5381 | ContinueBB = loop_block; |
| 5382 | } |
| 5383 | } |
| 5384 | |
| 5385 | if (!ContinueBB) { |
| 5386 | llvm_unreachable("Wrong continue block from loop"); |
| 5387 | } |
| 5388 | } |
| 5389 | |
| 5390 | // Record the continue and merge blocks. |
| 5391 | MergeBlocks[BB] = MergeBB; |
| 5392 | ContinueBlocks[BB] = ContinueBB; |
| 5393 | LoopMergesAndContinues.insert(MergeBB); |
| 5394 | LoopMergesAndContinues.insert(ContinueBB); |
| 5395 | } else if (branch && branch->isConditional()) { |
| 5396 | auto L = LI.getLoopFor(BB); |
| 5397 | bool HasBackedge = false; |
| 5398 | while (L && !HasBackedge) { |
| 5399 | if (L->isLoopLatch(BB)) { |
| 5400 | HasBackedge = true; |
| 5401 | } |
| 5402 | L = L->getParentLoop(); |
| 5403 | } |
| 5404 | |
| 5405 | if (!HasBackedge) { |
| 5406 | // Only need a merge if the branch doesn't include a loop break or |
| 5407 | // continue. |
| 5408 | auto true_bb = branch->getSuccessor(0); |
| 5409 | auto false_bb = branch->getSuccessor(1); |
| 5410 | if (!LoopMergesAndContinues.count(true_bb) && |
| 5411 | !LoopMergesAndContinues.count(false_bb)) { |
| 5412 | // StructurizeCFG pass already manipulated CFG. Just use false block |
| 5413 | // of branch instruction as merge block. |
| 5414 | MergeBlocks[BB] = false_bb; |
| 5415 | } |
| 5416 | } |
| 5417 | } |
| 5418 | } |
| 5419 | } |
| 5420 | } |