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" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 51 | #include "clspv/Option.h" |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 52 | #include "clspv/PushConstant.h" |
| 53 | #include "clspv/SpecConstant.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 54 | #include "clspv/spirv_c_strings.hpp" |
| 55 | #include "clspv/spirv_glsl.hpp" |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 56 | #include "clspv/spirv_reflection.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 57 | |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 58 | #include "ArgKind.h" |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 59 | #include "Builtins.h" |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 60 | #include "ComputeStructuredOrder.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 61 | #include "ConstantEmitter.h" |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 62 | #include "Constants.h" |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 63 | #include "DescriptorCounter.h" |
alan-baker | c4579bb | 2020-04-29 14:15:50 -0400 | [diff] [blame] | 64 | #include "Layout.h" |
alan-baker | 56f7aff | 2019-05-22 08:06:42 -0400 | [diff] [blame] | 65 | #include "NormalizeGlobalVariable.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 66 | #include "Passes.h" |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 67 | #include "SpecConstant.h" |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 68 | #include "Types.h" |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 69 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 70 | #if defined(_MSC_VER) |
| 71 | #pragma warning(pop) |
| 72 | #endif |
| 73 | |
| 74 | using namespace llvm; |
| 75 | using namespace clspv; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 76 | using namespace clspv::Builtins; |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 77 | using namespace clspv::Option; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 78 | using namespace mdconst; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 79 | |
| 80 | namespace { |
David Neto | cd8ca5f | 2017-10-02 23:34:11 -0400 | [diff] [blame] | 81 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 82 | cl::opt<bool> ShowResourceVars("show-rv", cl::init(false), cl::Hidden, |
| 83 | cl::desc("Show resource variable creation")); |
| 84 | |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 85 | cl::opt<bool> |
| 86 | ShowProducerIR("show-producer-ir", cl::init(false), cl::ReallyHidden, |
| 87 | cl::desc("Dump the IR at the start of SPIRVProducer")); |
| 88 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 89 | // These hacks exist to help transition code generation algorithms |
| 90 | // without making huge noise in detailed test output. |
| 91 | const bool Hack_generate_runtime_array_stride_early = true; |
| 92 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 93 | // The value of 1/pi. This value is from MSDN |
| 94 | // https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx |
| 95 | const double kOneOverPi = 0.318309886183790671538; |
| 96 | const glsl::ExtInst kGlslExtInstBad = static_cast<glsl::ExtInst>(0); |
| 97 | |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 98 | // SPIRV Module Sections (per 2.4 of the SPIR-V spec) |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 99 | // These are used to collect SPIRVInstructions by type on-the-fly. |
| 100 | enum SPIRVSection { |
| 101 | kCapabilities, |
| 102 | kExtensions, |
| 103 | kImports, |
| 104 | kMemoryModel, |
| 105 | kEntryPoints, |
| 106 | kExecutionModes, |
| 107 | |
| 108 | kDebug, |
| 109 | kAnnotations, |
| 110 | |
| 111 | kTypes, |
| 112 | kConstants = kTypes, |
| 113 | kGlobalVariables, |
| 114 | |
| 115 | kFunctions, |
| 116 | |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 117 | // This is not a section of the SPIR-V spec and should always immediately |
| 118 | // precede kSectionCount. It is a convenient place for the embedded |
| 119 | // reflection data. |
| 120 | kReflection, |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 121 | kSectionCount |
| 122 | }; |
| 123 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 124 | class SPIRVID { |
| 125 | uint32_t id; |
| 126 | |
| 127 | public: |
| 128 | SPIRVID(uint32_t _id = 0) : id(_id) {} |
| 129 | uint32_t get() const { return id; } |
| 130 | bool isValid() const { return id != 0; } |
| 131 | bool operator==(const SPIRVID &that) const { return id == that.id; } |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 132 | bool operator<(const SPIRVID &that) const { return id < that.id; } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 133 | }; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 134 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 135 | enum SPIRVOperandType { NUMBERID, LITERAL_WORD, LITERAL_DWORD, LITERAL_STRING }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 136 | |
| 137 | struct SPIRVOperand { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 138 | explicit SPIRVOperand(SPIRVOperandType Ty, uint32_t Num) : Type(Ty) { |
| 139 | LiteralNum[0] = Num; |
| 140 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 141 | explicit SPIRVOperand(SPIRVOperandType Ty, const char *Str) |
| 142 | : Type(Ty), LiteralStr(Str) {} |
| 143 | explicit SPIRVOperand(SPIRVOperandType Ty, StringRef Str) |
| 144 | : Type(Ty), LiteralStr(Str) {} |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 145 | explicit SPIRVOperand(ArrayRef<uint32_t> NumVec) { |
| 146 | auto sz = NumVec.size(); |
| 147 | assert(sz >= 1 && sz <= 2); |
| 148 | Type = sz == 1 ? LITERAL_WORD : LITERAL_DWORD; |
| 149 | LiteralNum[0] = NumVec[0]; |
| 150 | if (sz == 2) { |
| 151 | LiteralNum[1] = NumVec[1]; |
| 152 | } |
| 153 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 154 | |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 155 | SPIRVOperandType getType() const { return Type; } |
| 156 | uint32_t getNumID() const { return LiteralNum[0]; } |
| 157 | std::string getLiteralStr() const { return LiteralStr; } |
| 158 | const uint32_t *getLiteralNum() const { return LiteralNum; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 159 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 160 | uint32_t GetNumWords() const { |
| 161 | switch (Type) { |
| 162 | case NUMBERID: |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 163 | case LITERAL_WORD: |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 164 | return 1; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 165 | case LITERAL_DWORD: |
| 166 | return 2; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 167 | case LITERAL_STRING: |
| 168 | // Account for the terminating null character. |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 169 | return uint32_t((LiteralStr.size() + 4) / 4); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 170 | } |
| 171 | llvm_unreachable("Unhandled case in SPIRVOperand::GetNumWords()"); |
| 172 | } |
| 173 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 174 | private: |
| 175 | SPIRVOperandType Type; |
| 176 | std::string LiteralStr; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 177 | uint32_t LiteralNum[2]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 178 | }; |
| 179 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 180 | typedef SmallVector<SPIRVOperand, 4> SPIRVOperandVec; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 181 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 182 | struct SPIRVInstruction { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 183 | // Primary constructor must have Opcode, initializes WordCount based on ResID. |
| 184 | SPIRVInstruction(spv::Op Opc, SPIRVID ResID = 0) |
| 185 | : Opcode(static_cast<uint16_t>(Opc)) { |
| 186 | setResult(ResID); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 187 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [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, SPIRVOperandVec &Ops) : SPIRVInstruction(Opc) { |
| 193 | setOperands(Ops); |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 194 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 195 | // Creates an instruction with an opcode and no result ID, and with the given |
| 196 | // operands. This calls primary constructor to initialize Opcode, WordCount. |
| 197 | // Takes ownership of the operands and clears |Ops|. |
| 198 | SPIRVInstruction(spv::Op Opc, SPIRVID ResID, SPIRVOperandVec &Ops) |
| 199 | : SPIRVInstruction(Opc, ResID) { |
| 200 | setOperands(Ops); |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 201 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 202 | |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 203 | uint32_t getWordCount() const { return WordCount; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 204 | uint16_t getOpcode() const { return Opcode; } |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 205 | SPIRVID getResultID() const { return ResultID; } |
| 206 | const SPIRVOperandVec &getOperands() const { return Operands; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 207 | |
| 208 | private: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 209 | void setResult(SPIRVID ResID = 0) { |
| 210 | WordCount = 1 + (ResID.isValid() ? 1 : 0); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 211 | ResultID = ResID; |
| 212 | } |
| 213 | |
| 214 | void setOperands(SPIRVOperandVec &Ops) { |
| 215 | assert(Operands.empty()); |
| 216 | Operands = std::move(Ops); |
| 217 | for (auto &opd : Operands) { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 218 | WordCount += uint16_t(opd.GetNumWords()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | private: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 223 | uint32_t WordCount; // Check the 16-bit bound at code generation time. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 224 | uint16_t Opcode; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 225 | SPIRVID ResultID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 226 | SPIRVOperandVec Operands; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | struct SPIRVProducerPass final : public ModulePass { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 230 | typedef DenseMap<Type *, SPIRVID> TypeMapType; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 231 | typedef UniqueVector<Type *> TypeList; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 232 | typedef DenseMap<Value *, SPIRVID> ValueMapType; |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 233 | typedef std::list<SPIRVID> SPIRVIDListType; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 234 | typedef std::vector<std::pair<Value *, SPIRVID>> EntryPointVecType; |
| 235 | typedef std::set<uint32_t> CapabilitySetType; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 236 | typedef std::list<SPIRVInstruction> SPIRVInstructionList; |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 237 | typedef std::map<spv::BuiltIn, SPIRVID> BuiltinConstantMapType; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 238 | // A vector of pairs, each of which is: |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 239 | // - the LLVM instruction that we will later generate SPIR-V code for |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 240 | // - the SPIR-V instruction placeholder that will be replaced |
| 241 | typedef std::vector<std::pair<Value *, SPIRVInstruction *>> |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 242 | DeferredInstVecType; |
| 243 | typedef DenseMap<FunctionType *, std::pair<FunctionType *, uint32_t>> |
| 244 | GlobalConstFuncMapType; |
| 245 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 246 | explicit SPIRVProducerPass( |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 247 | raw_pwrite_stream &out, |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 248 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 249 | bool outputCInitList) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 250 | : ModulePass(ID), module(nullptr), samplerMap(samplerMap), out(out), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 251 | binaryTempOut(binaryTempUnderlyingVector), binaryOut(&out), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 252 | outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1), |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 253 | OpExtInstImportID(0), HasVariablePointersStorageBuffer(false), |
| 254 | HasVariablePointers(false), SamplerTy(nullptr), WorkgroupSizeValueID(0), |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 255 | WorkgroupSizeVarID(0) { |
| 256 | addCapability(spv::CapabilityShader); |
| 257 | Ptr = this; |
| 258 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 259 | |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 260 | virtual ~SPIRVProducerPass() { |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 261 | } |
| 262 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 263 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 264 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 265 | AU.addRequired<LoopInfoWrapperPass>(); |
| 266 | } |
| 267 | |
| 268 | virtual bool runOnModule(Module &module) override; |
| 269 | |
| 270 | // output the SPIR-V header block |
| 271 | void outputHeader(); |
| 272 | |
| 273 | // patch the SPIR-V header block |
| 274 | void patchHeader(); |
| 275 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 276 | CapabilitySetType &getCapabilitySet() { return CapabilitySet; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 277 | TypeMapType &getImageTypeMap() { return ImageTypeMap; } |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 278 | TypeList &getTypeList() { return Types; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 279 | ValueMapType &getValueMap() { return ValueMap; } |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 280 | SPIRVInstructionList &getSPIRVInstList(SPIRVSection Section) { |
| 281 | return SPIRVSections[Section]; |
| 282 | }; |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 283 | EntryPointVecType &getEntryPointVec() { return EntryPointVec; } |
| 284 | DeferredInstVecType &getDeferredInstVec() { return DeferredInstVec; } |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 285 | SPIRVIDListType &getEntryPointInterfacesList() { |
| 286 | return EntryPointInterfacesList; |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 287 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 288 | SPIRVID getOpExtInstImportID(); |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 289 | std::vector<SPIRVID> &getBuiltinDimVec() { return BuiltinDimensionVec; } |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 290 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 291 | bool hasVariablePointersStorageBuffer() { |
| 292 | return HasVariablePointersStorageBuffer; |
| 293 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 294 | void setVariablePointersStorageBuffer() { |
| 295 | if (!HasVariablePointersStorageBuffer) { |
| 296 | addCapability(spv::CapabilityVariablePointersStorageBuffer); |
| 297 | HasVariablePointersStorageBuffer = true; |
| 298 | } |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 299 | } |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 300 | bool hasVariablePointers() { return HasVariablePointers; } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 301 | void setVariablePointers() { |
| 302 | if (!HasVariablePointers) { |
| 303 | addCapability(spv::CapabilityVariablePointers); |
| 304 | HasVariablePointers = true; |
| 305 | } |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 306 | } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 307 | ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() { |
| 308 | return samplerMap; |
| 309 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 310 | GlobalConstFuncMapType &getGlobalConstFuncTypeMap() { |
| 311 | return GlobalConstFuncTypeMap; |
| 312 | } |
| 313 | SmallPtrSet<Value *, 16> &getGlobalConstArgSet() { |
| 314 | return GlobalConstArgumentSet; |
| 315 | } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 316 | TypeList &getTypesNeedingArrayStride() { return TypesNeedingArrayStride; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 317 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 318 | void GenerateLLVMIRInfo(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 319 | // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will |
| 320 | // *not* be converted to a storage buffer, replace each such global variable |
| 321 | // with one in the storage class expecgted by SPIR-V. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 322 | void FindGlobalConstVars(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 323 | // Populate ResourceVarInfoList, FunctionToResourceVarsMap, and |
| 324 | // ModuleOrderedResourceVars. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 325 | void FindResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 326 | void FindTypePerGlobalVar(GlobalVariable &GV); |
| 327 | void FindTypePerFunc(Function &F); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 328 | void FindTypesForSamplerMap(); |
| 329 | void FindTypesForResourceVars(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 330 | // Inserts |Ty| and relevant sub-types into the |Types| member, indicating |
| 331 | // that |Ty| and its subtypes will need a corresponding SPIR-V type. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 332 | void FindType(Type *Ty); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 333 | |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 334 | // Returns the canonical type of |type|. |
| 335 | // |
| 336 | // By default, clspv maps both __constant and __global address space pointers |
| 337 | // to StorageBuffer storage class. In order to prevent duplicate types from |
| 338 | // being generated, clspv uses the canonical type as a representative. |
| 339 | Type *CanonicalType(Type *type); |
| 340 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 341 | // Lookup or create Types, Constants. |
| 342 | // Returns SPIRVID once it has been created. |
| 343 | SPIRVID getSPIRVType(Type *Ty); |
| 344 | SPIRVID getSPIRVConstant(Constant *Cst); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 345 | SPIRVID getSPIRVInt32Constant(uint32_t CstVal); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 346 | // Lookup SPIRVID of llvm::Value, may create Constant. |
| 347 | SPIRVID getSPIRVValue(Value *V); |
| 348 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 349 | SPIRVID getSPIRVBuiltin(spv::BuiltIn BID, spv::Capability Cap); |
| 350 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 351 | // Generates instructions for SPIR-V types corresponding to the LLVM types |
| 352 | // saved in the |Types| member. A type follows its subtypes. IDs are |
| 353 | // allocated sequentially starting with the current value of nextID, and |
| 354 | // with a type following its subtypes. Also updates nextID to just beyond |
| 355 | // the last generated ID. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 356 | void GenerateSPIRVTypes(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 357 | void GenerateModuleInfo(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 358 | void GenerateGlobalVar(GlobalVariable &GV); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 359 | void GenerateWorkgroupVars(); |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 360 | // Generate reflection instructions for resource variables associated with |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 361 | // arguments to F. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 362 | void GenerateSamplers(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 363 | // Generate OpVariables for %clspv.resource.var.* calls. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 364 | void GenerateResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 365 | void GenerateFuncPrologue(Function &F); |
| 366 | void GenerateFuncBody(Function &F); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 367 | void GenerateEntryPointInitialStores(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 368 | spv::Op GetSPIRVCmpOpcode(CmpInst *CmpI); |
| 369 | spv::Op GetSPIRVCastOpcode(Instruction &I); |
| 370 | spv::Op GetSPIRVBinaryOpcode(Instruction &I); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 371 | SPIRVID GenerateClspvInstruction(CallInst *Call, |
| 372 | const FunctionInfo &FuncInfo); |
| 373 | SPIRVID GenerateImageInstruction(CallInst *Call, |
| 374 | const FunctionInfo &FuncInfo); |
| 375 | SPIRVID GenerateSubgroupInstruction(CallInst *Call, |
| 376 | const FunctionInfo &FuncInfo); |
| 377 | SPIRVID GenerateInstructionFromCall(CallInst *Call); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 378 | void GenerateInstruction(Instruction &I); |
| 379 | void GenerateFuncEpilogue(); |
| 380 | void HandleDeferredInstruction(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 381 | void HandleDeferredDecorations(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 382 | bool is4xi8vec(Type *Ty) const; |
| 383 | spv::StorageClass GetStorageClass(unsigned AddrSpace) const; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 384 | spv::StorageClass GetStorageClassForArgKind(clspv::ArgKind arg_kind) const; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 385 | spv::BuiltIn GetBuiltin(StringRef globalVarName) const; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 386 | // Returns the GLSL extended instruction enum that the given function |
| 387 | // call maps to. If none, then returns the 0 value, i.e. GLSLstd4580Bad. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 388 | glsl::ExtInst getExtInstEnum(const Builtins::FunctionInfo &func_info); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 389 | // Returns the GLSL extended instruction enum indirectly used by the given |
| 390 | // function. That is, to implement the given function, we use an extended |
| 391 | // instruction plus one more instruction. If none, then returns the 0 value, |
| 392 | // i.e. GLSLstd4580Bad. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 393 | glsl::ExtInst getIndirectExtInstEnum(const Builtins::FunctionInfo &func_info); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 394 | // Returns the single GLSL extended instruction used directly or |
| 395 | // indirectly by the given function call. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 396 | glsl::ExtInst |
| 397 | getDirectOrIndirectExtInstEnum(const Builtins::FunctionInfo &func_info); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 398 | void WriteOneWord(uint32_t Word); |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 399 | void WriteResultID(const SPIRVInstruction &Inst); |
| 400 | void WriteWordCountAndOpcode(const SPIRVInstruction &Inst); |
| 401 | void WriteOperand(const SPIRVOperand &Op); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 402 | void WriteSPIRVBinary(); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 403 | void WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 404 | |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 405 | // Returns true if |type| is compatible with OpConstantNull. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 406 | bool IsTypeNullable(const Type *type) const; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 407 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 408 | // Populate UBO remapped type maps. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 409 | void PopulateUBOTypeMaps(); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 410 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 411 | // Populate the merge and continue block maps. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 412 | void PopulateStructuredCFGMaps(); |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 413 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 414 | // Wrapped methods of DataLayout accessors. If |type| was remapped for UBOs, |
| 415 | // uses the internal map, otherwise it falls back on the data layout. |
| 416 | uint64_t GetTypeSizeInBits(Type *type, const DataLayout &DL); |
| 417 | uint64_t GetTypeStoreSize(Type *type, const DataLayout &DL); |
| 418 | uint64_t GetTypeAllocSize(Type *type, const DataLayout &DL); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 419 | uint32_t GetExplicitLayoutStructMemberOffset(StructType *type, |
| 420 | unsigned member, |
| 421 | const DataLayout &DL); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 422 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 423 | // Returns the base pointer of |v|. |
| 424 | Value *GetBasePointer(Value *v); |
| 425 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 426 | // Add Capability if not already (e.g. CapabilityGroupNonUniformBroadcast) |
| 427 | void addCapability(uint32_t c) { CapabilitySet.emplace(c); } |
| 428 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 429 | // Sets |HasVariablePointersStorageBuffer| or |HasVariablePointers| base on |
| 430 | // |address_space|. |
| 431 | void setVariablePointersCapabilities(unsigned address_space); |
| 432 | |
| 433 | // Returns true if |lhs| and |rhs| represent the same resource or workgroup |
| 434 | // variable. |
| 435 | bool sameResource(Value *lhs, Value *rhs) const; |
| 436 | |
| 437 | // Returns true if |inst| is phi or select that selects from the same |
| 438 | // structure (or null). |
| 439 | bool selectFromSameObject(Instruction *inst); |
| 440 | |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 441 | // Returns true if |Arg| is called with a coherent resource. |
| 442 | bool CalledWithCoherentResource(Argument &Arg); |
| 443 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 444 | // |
| 445 | // Primary interface for adding SPIRVInstructions to a SPIRVSection. |
| 446 | template <enum SPIRVSection TSection = kFunctions> |
| 447 | SPIRVID addSPIRVInst(spv::Op Opcode, SPIRVOperandVec &Operands) { |
| 448 | bool has_result, has_result_type; |
| 449 | spv::HasResultAndType(Opcode, &has_result, &has_result_type); |
| 450 | SPIRVID RID = has_result ? incrNextID() : 0; |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 451 | SPIRVSections[TSection].emplace_back(Opcode, RID, Operands); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 452 | return RID; |
| 453 | } |
| 454 | template <enum SPIRVSection TSection = kFunctions> |
| 455 | SPIRVID addSPIRVInst(spv::Op Op) { |
| 456 | SPIRVOperandVec Ops; |
| 457 | return addSPIRVInst<TSection>(Op, Ops); |
| 458 | } |
| 459 | template <enum SPIRVSection TSection = kFunctions> |
| 460 | SPIRVID addSPIRVInst(spv::Op Op, uint32_t V) { |
| 461 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 462 | Ops.emplace_back(LITERAL_WORD, V); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 463 | return addSPIRVInst<TSection>(Op, Ops); |
| 464 | } |
| 465 | template <enum SPIRVSection TSection = kFunctions> |
| 466 | SPIRVID addSPIRVInst(spv::Op Op, const char *V) { |
| 467 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 468 | Ops.emplace_back(LITERAL_STRING, V); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 469 | return addSPIRVInst<TSection>(Op, Ops); |
| 470 | } |
| 471 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 472 | // |
| 473 | // Add placeholder for llvm::Value that references future values. |
| 474 | // Must have result ID just in case final SPIRVInstruction requires. |
| 475 | SPIRVID addSPIRVPlaceholder(Value *I) { |
| 476 | SPIRVID RID = incrNextID(); |
| 477 | SPIRVOperandVec Ops; |
| 478 | SPIRVSections[kFunctions].emplace_back(spv::OpExtInst, RID, Ops); |
| 479 | DeferredInstVec.push_back({I, &SPIRVSections[kFunctions].back()}); |
| 480 | return RID; |
| 481 | } |
| 482 | // Replace placeholder with actual SPIRVInstruction on the final pass |
| 483 | // (HandleDeferredInstruction). |
| 484 | SPIRVID replaceSPIRVInst(SPIRVInstruction *I, spv::Op Opcode, |
| 485 | SPIRVOperandVec &Operands) { |
| 486 | bool has_result, has_result_type; |
| 487 | spv::HasResultAndType(Opcode, &has_result, &has_result_type); |
| 488 | SPIRVID RID = has_result ? I->getResultID() : 0; |
| 489 | *I = SPIRVInstruction(Opcode, RID, Operands); |
| 490 | return RID; |
| 491 | } |
| 492 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 493 | // |
| 494 | // Add global variable and capture entry point interface |
| 495 | SPIRVID addSPIRVGlobalVariable(const SPIRVID &TypeID, spv::StorageClass SC, |
| 496 | const SPIRVID &InitID = SPIRVID()); |
| 497 | |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 498 | SPIRVID getReflectionImport(); |
| 499 | void GenerateReflection(); |
| 500 | void GenerateKernelReflection(); |
| 501 | void GeneratePushConstantReflection(); |
| 502 | void GenerateSpecConstantReflection(); |
| 503 | void AddArgumentReflection(SPIRVID kernel_decl, const std::string &name, |
| 504 | clspv::ArgKind arg_kind, uint32_t ordinal, |
| 505 | uint32_t descriptor_set, uint32_t binding, |
| 506 | uint32_t offset, uint32_t size, uint32_t spec_id, |
| 507 | uint32_t elem_size); |
| 508 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 509 | private: |
| 510 | static char ID; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 511 | |
| 512 | Module *module; |
| 513 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 514 | // Set of Capabilities required |
| 515 | CapabilitySetType CapabilitySet; |
| 516 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 517 | // Map from clspv::BuiltinType to SPIRV Global Variable |
| 518 | BuiltinConstantMapType BuiltinConstantMap; |
| 519 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 520 | ArrayRef<std::pair<unsigned, std::string>> samplerMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 521 | raw_pwrite_stream &out; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 522 | |
| 523 | // TODO(dneto): Wouldn't it be better to always just emit a binary, and then |
| 524 | // convert to other formats on demand? |
| 525 | |
| 526 | // When emitting a C initialization list, the WriteSPIRVBinary method |
| 527 | // will actually write its words to this vector via binaryTempOut. |
| 528 | SmallVector<char, 100> binaryTempUnderlyingVector; |
| 529 | raw_svector_ostream binaryTempOut; |
| 530 | |
| 531 | // Binary output writes to this stream, which might be |out| or |
| 532 | // |binaryTempOut|. It's the latter when we really want to write a C |
| 533 | // initializer list. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 534 | raw_pwrite_stream *binaryOut; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 535 | const bool outputCInitList; // If true, output look like {0x7023, ... , 5} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 536 | uint64_t patchBoundOffset; |
| 537 | uint32_t nextID; |
| 538 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 539 | SPIRVID incrNextID() { return nextID++; } |
| 540 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 541 | // ID for OpTypeInt 32 1. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 542 | SPIRVID int32ID; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 543 | // ID for OpTypeVector %int 4. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 544 | SPIRVID v4int32ID; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 545 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 546 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 547 | TypeMapType TypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 548 | // Maps an LLVM image type to its SPIR-V ID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 549 | TypeMapType ImageTypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 550 | // 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] | 551 | TypeList Types; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 552 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 553 | ValueMapType ValueMap; |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 554 | SPIRVInstructionList SPIRVSections[kSectionCount]; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 555 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 556 | EntryPointVecType EntryPointVec; |
| 557 | DeferredInstVecType DeferredInstVec; |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 558 | SPIRVIDListType EntryPointInterfacesList; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 559 | SPIRVID OpExtInstImportID; |
| 560 | std::vector<SPIRVID> BuiltinDimensionVec; |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 561 | bool HasVariablePointersStorageBuffer; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 562 | bool HasVariablePointers; |
| 563 | Type *SamplerTy; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 564 | DenseMap<unsigned, SPIRVID> SamplerLiteralToIDMap; |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 565 | |
| 566 | // 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] | 567 | // will map F's type to (G, index of the parameter), where in a first phase |
| 568 | // G is F's type. During FindTypePerFunc, G will be changed to F's type |
| 569 | // but replacing the pointer-to-constant parameter with |
| 570 | // pointer-to-ModuleScopePrivate. |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 571 | // TODO(dneto): This doesn't seem general enough? A function might have |
| 572 | // more than one such parameter. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 573 | GlobalConstFuncMapType GlobalConstFuncTypeMap; |
| 574 | SmallPtrSet<Value *, 16> GlobalConstArgumentSet; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 575 | // An ordered set of pointer types of Base arguments to OpPtrAccessChain, |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 576 | // or array types, and which point into transparent memory (StorageBuffer |
| 577 | // storage class). These will require an ArrayStride decoration. |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 578 | // See SPV_KHR_variable_pointers rev 13. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 579 | TypeList TypesNeedingArrayStride; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 580 | |
| 581 | // This is truly ugly, but works around what look like driver bugs. |
| 582 | // For get_local_size, an earlier part of the flow has created a module-scope |
| 583 | // variable in Private address space to hold the value for the workgroup |
| 584 | // size. Its intializer is a uint3 value marked as builtin WorkgroupSize. |
| 585 | // When this is present, save the IDs of the initializer value and variable |
| 586 | // in these two variables. We only ever do a vector load from it, and |
| 587 | // when we see one of those, substitute just the value of the intializer. |
| 588 | // 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] | 589 | // TODO(dneto): Remove this once drivers are fixed. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 590 | SPIRVID WorkgroupSizeValueID; |
| 591 | SPIRVID WorkgroupSizeVarID; |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 592 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 593 | // Bookkeeping for mapping kernel arguments to resource variables. |
| 594 | struct ResourceVarInfo { |
| 595 | ResourceVarInfo(int index_arg, unsigned set_arg, unsigned binding_arg, |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 596 | Function *fn, clspv::ArgKind arg_kind_arg, int coherent_arg) |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 597 | : index(index_arg), descriptor_set(set_arg), binding(binding_arg), |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 598 | var_fn(fn), arg_kind(arg_kind_arg), coherent(coherent_arg), |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 599 | addr_space(fn->getReturnType()->getPointerAddressSpace()) {} |
| 600 | const int index; // Index into ResourceVarInfoList |
| 601 | const unsigned descriptor_set; |
| 602 | const unsigned binding; |
| 603 | Function *const var_fn; // The @clspv.resource.var.* function. |
| 604 | const clspv::ArgKind arg_kind; |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 605 | const int coherent; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 606 | const unsigned addr_space; // The LLVM address space |
| 607 | // The SPIR-V ID of the OpVariable. Not populated at construction time. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 608 | SPIRVID var_id; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 609 | }; |
| 610 | // A list of resource var info. Each one correponds to a module-scope |
| 611 | // resource variable we will have to create. Resource var indices are |
| 612 | // indices into this vector. |
| 613 | SmallVector<std::unique_ptr<ResourceVarInfo>, 8> ResourceVarInfoList; |
| 614 | // This is a vector of pointers of all the resource vars, but ordered by |
| 615 | // kernel function, and then by argument. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 616 | UniqueVector<ResourceVarInfo *> ModuleOrderedResourceVars; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 617 | // Map a function to the ordered list of resource variables it uses, one for |
| 618 | // each argument. If an argument does not use a resource variable, it |
| 619 | // will have a null pointer entry. |
| 620 | using FunctionToResourceVarsMapType = |
| 621 | DenseMap<Function *, SmallVector<ResourceVarInfo *, 8>>; |
| 622 | FunctionToResourceVarsMapType FunctionToResourceVarsMap; |
| 623 | |
| 624 | // What LLVM types map to SPIR-V types needing layout? These are the |
| 625 | // arrays and structures supporting storage buffers and uniform buffers. |
| 626 | TypeList TypesNeedingLayout; |
| 627 | // What LLVM struct types map to a SPIR-V struct type with Block decoration? |
| 628 | UniqueVector<StructType *> StructTypesNeedingBlock; |
| 629 | // For a call that represents a load from an opaque type (samplers, images), |
| 630 | // map it to the variable id it should load from. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 631 | DenseMap<CallInst *, SPIRVID> ResourceVarDeferredLoadCalls; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 632 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 633 | // An ordered list of the kernel arguments of type pointer-to-local. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 634 | using LocalArgList = SmallVector<Argument *, 8>; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 635 | LocalArgList LocalArgs; |
| 636 | // Information about a pointer-to-local argument. |
| 637 | struct LocalArgInfo { |
| 638 | // The SPIR-V ID of the array variable. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 639 | SPIRVID variable_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 640 | // The element type of the |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 641 | Type *elem_type; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 642 | // The ID of the array type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 643 | SPIRVID array_size_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 644 | // The ID of the array type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 645 | SPIRVID array_type_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 646 | // The ID of the pointer to the array type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 647 | SPIRVID ptr_array_type_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 648 | // The specialization constant ID of the array size. |
| 649 | int spec_id; |
| 650 | }; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 651 | // A mapping from Argument to its assigned SpecId. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 652 | DenseMap<const Argument *, int> LocalArgSpecIds; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 653 | // A mapping from SpecId to its LocalArgInfo. |
| 654 | DenseMap<int, LocalArgInfo> LocalSpecIdInfoMap; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 655 | // A mapping from a remapped type to its real offsets. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 656 | DenseMap<Type *, std::vector<uint32_t>> RemappedUBOTypeOffsets; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 657 | // A mapping from a remapped type to its real sizes. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 658 | DenseMap<Type *, std::tuple<uint64_t, uint64_t, uint64_t>> |
| 659 | RemappedUBOTypeSizes; |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 660 | |
| 661 | // Maps basic block to its merge block. |
| 662 | DenseMap<BasicBlock *, BasicBlock *> MergeBlocks; |
| 663 | // Maps basic block to its continue block. |
| 664 | DenseMap<BasicBlock *, BasicBlock *> ContinueBlocks; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 665 | |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 666 | SPIRVID ReflectionID; |
| 667 | DenseMap<Function *, SPIRVID> KernelDeclarations; |
| 668 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 669 | public: |
| 670 | static SPIRVProducerPass *Ptr; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | char SPIRVProducerPass::ID; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 674 | SPIRVProducerPass *SPIRVProducerPass::Ptr = nullptr; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 675 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 676 | } // namespace |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 677 | |
| 678 | namespace clspv { |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 679 | ModulePass * |
| 680 | createSPIRVProducerPass(raw_pwrite_stream &out, |
| 681 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
| 682 | bool outputCInitList) { |
| 683 | return new SPIRVProducerPass(out, samplerMap, outputCInitList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 684 | } |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 685 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 686 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 687 | namespace { |
| 688 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, uint32_t num) { |
| 689 | list.emplace_back(LITERAL_WORD, num); |
| 690 | return list; |
| 691 | } |
| 692 | |
| 693 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, int32_t num) { |
| 694 | list.emplace_back(LITERAL_WORD, static_cast<uint32_t>(num)); |
| 695 | return list; |
| 696 | } |
| 697 | |
| 698 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, ArrayRef<uint32_t> num_vec) { |
| 699 | list.emplace_back(num_vec); |
| 700 | return list; |
| 701 | } |
| 702 | |
| 703 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, StringRef str) { |
| 704 | list.emplace_back(LITERAL_STRING, str); |
| 705 | return list; |
| 706 | } |
| 707 | |
| 708 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, Type *t) { |
| 709 | list.emplace_back(NUMBERID, SPIRVProducerPass::Ptr->getSPIRVType(t).get()); |
| 710 | return list; |
| 711 | } |
| 712 | |
| 713 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, Value *v) { |
| 714 | list.emplace_back(NUMBERID, SPIRVProducerPass::Ptr->getSPIRVValue(v).get()); |
| 715 | return list; |
| 716 | } |
| 717 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 718 | SPIRVOperandVec &operator<<(SPIRVOperandVec &list, const SPIRVID &v) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 719 | list.emplace_back(NUMBERID, v.get()); |
| 720 | return list; |
| 721 | } |
| 722 | } // namespace |
| 723 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 724 | bool SPIRVProducerPass::runOnModule(Module &M) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 725 | // TODO(sjw): Need to reset all data members for each Module, or better |
| 726 | // yet create a new SPIRVProducer for every module.. For now only |
| 727 | // allow 1 call. |
| 728 | assert(module == nullptr); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 729 | module = &M; |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 730 | if (ShowProducerIR) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 731 | llvm::outs() << *module << "\n"; |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 732 | } |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 733 | binaryOut = outputCInitList ? &binaryTempOut : &out; |
| 734 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 735 | PopulateUBOTypeMaps(); |
| 736 | PopulateStructuredCFGMaps(); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 737 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 738 | // SPIR-V always begins with its header information |
| 739 | outputHeader(); |
| 740 | |
| 741 | // Gather information from the LLVM IR that we require. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 742 | GenerateLLVMIRInfo(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 743 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 744 | // Collect information on global variables too. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 745 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 746 | // If the GV is one of our special __spirv_* variables, remove the |
| 747 | // initializer as it was only placed there to force LLVM to not throw the |
| 748 | // value away. |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 749 | if (GV.getName().startswith("__spirv_") || |
| 750 | GV.getAddressSpace() == clspv::AddressSpace::PushConstant) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 751 | GV.setInitializer(nullptr); |
| 752 | } |
| 753 | |
| 754 | // Collect types' information from global variable. |
| 755 | FindTypePerGlobalVar(GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 756 | } |
| 757 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 758 | // Generate SPIRV instructions for types. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 759 | GenerateSPIRVTypes(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 760 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 761 | // Generate literal samplers if necessary. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 762 | GenerateSamplers(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 763 | |
| 764 | // Generate SPIRV variables. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 765 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 766 | GenerateGlobalVar(GV); |
| 767 | } |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 768 | GenerateResourceVars(); |
| 769 | GenerateWorkgroupVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 770 | |
| 771 | // Generate SPIRV instructions for each function. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 772 | for (Function &F : *module) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 773 | if (F.isDeclaration()) { |
| 774 | continue; |
| 775 | } |
| 776 | |
| 777 | // Generate Function Prologue. |
| 778 | GenerateFuncPrologue(F); |
| 779 | |
| 780 | // Generate SPIRV instructions for function body. |
| 781 | GenerateFuncBody(F); |
| 782 | |
| 783 | // Generate Function Epilogue. |
| 784 | GenerateFuncEpilogue(); |
| 785 | } |
| 786 | |
| 787 | HandleDeferredInstruction(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 788 | HandleDeferredDecorations(); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 789 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 790 | // Generate SPIRV module information. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 791 | GenerateModuleInfo(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 792 | |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 793 | // Generate embedded reflection information. |
| 794 | GenerateReflection(); |
| 795 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 796 | WriteSPIRVBinary(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 797 | |
| 798 | // We need to patch the SPIR-V header to set bound correctly. |
| 799 | patchHeader(); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 800 | |
| 801 | if (outputCInitList) { |
| 802 | bool first = true; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 803 | std::ostringstream os; |
| 804 | |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 805 | auto emit_word = [&os, &first](uint32_t word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 806 | if (!first) |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 807 | os << ",\n"; |
| 808 | os << word; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 809 | first = false; |
| 810 | }; |
| 811 | |
| 812 | os << "{"; |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 813 | const std::string str(binaryTempOut.str()); |
| 814 | for (unsigned i = 0; i < str.size(); i += 4) { |
| 815 | const uint32_t a = static_cast<unsigned char>(str[i]); |
| 816 | const uint32_t b = static_cast<unsigned char>(str[i + 1]); |
| 817 | const uint32_t c = static_cast<unsigned char>(str[i + 2]); |
| 818 | const uint32_t d = static_cast<unsigned char>(str[i + 3]); |
| 819 | emit_word(a | (b << 8) | (c << 16) | (d << 24)); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 820 | } |
| 821 | os << "}\n"; |
| 822 | out << os.str(); |
| 823 | } |
| 824 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 825 | return false; |
| 826 | } |
| 827 | |
| 828 | void SPIRVProducerPass::outputHeader() { |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 829 | binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber), |
| 830 | sizeof(spv::MagicNumber)); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 831 | uint32_t minor = 0; |
| 832 | if (SpvVersion() == SPIRVVersion::SPIRV_1_3) { |
| 833 | minor = 3; |
| 834 | } |
| 835 | uint32_t version = (1 << 16) | (minor << 8); |
| 836 | binaryOut->write(reinterpret_cast<const char *>(&version), sizeof(version)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 837 | |
alan-baker | 0c18ab0 | 2019-06-12 10:23:21 -0400 | [diff] [blame] | 838 | // use Google's vendor ID |
| 839 | const uint32_t vendor = 21 << 16; |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 840 | binaryOut->write(reinterpret_cast<const char *>(&vendor), sizeof(vendor)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 841 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 842 | // we record where we need to come back to and patch in the bound value |
| 843 | patchBoundOffset = binaryOut->tell(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 844 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 845 | // output a bad bound for now |
| 846 | binaryOut->write(reinterpret_cast<const char *>(&nextID), sizeof(nextID)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 847 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 848 | // output the schema (reserved for use and must be 0) |
| 849 | const uint32_t schema = 0; |
| 850 | binaryOut->write(reinterpret_cast<const char *>(&schema), sizeof(schema)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | void SPIRVProducerPass::patchHeader() { |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 854 | // for a binary we just write the value of nextID over bound |
| 855 | binaryOut->pwrite(reinterpret_cast<char *>(&nextID), sizeof(nextID), |
| 856 | patchBoundOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 857 | } |
| 858 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 859 | void SPIRVProducerPass::GenerateLLVMIRInfo() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 860 | // This function generates LLVM IR for function such as global variable for |
| 861 | // argument, constant and pointer type for argument access. These information |
| 862 | // is artificial one because we need Vulkan SPIR-V output. This function is |
| 863 | // executed ahead of FindType and FindConstant. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 864 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 865 | FindGlobalConstVars(); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 866 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 867 | FindResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 868 | |
| 869 | bool HasWorkGroupBuiltin = false; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 870 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 871 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 872 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 873 | HasWorkGroupBuiltin = true; |
| 874 | } |
| 875 | } |
| 876 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 877 | FindTypesForSamplerMap(); |
| 878 | FindTypesForResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 879 | } |
| 880 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 881 | void SPIRVProducerPass::FindGlobalConstVars() { |
| 882 | clspv::NormalizeGlobalVariables(*module); |
| 883 | const DataLayout &DL = module->getDataLayout(); |
alan-baker | 56f7aff | 2019-05-22 08:06:42 -0400 | [diff] [blame] | 884 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 885 | SmallVector<GlobalVariable *, 8> GVList; |
| 886 | SmallVector<GlobalVariable *, 8> DeadGVList; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 887 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 888 | if (GV.getType()->getAddressSpace() == AddressSpace::Constant) { |
| 889 | if (GV.use_empty()) { |
| 890 | DeadGVList.push_back(&GV); |
| 891 | } else { |
| 892 | GVList.push_back(&GV); |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | // Remove dead global __constant variables. |
| 898 | for (auto GV : DeadGVList) { |
| 899 | GV->eraseFromParent(); |
| 900 | } |
| 901 | DeadGVList.clear(); |
| 902 | |
| 903 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 904 | // For now, we only support a single storage buffer. |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 905 | if (!GVList.empty()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 906 | assert(GVList.size() == 1); |
| 907 | const auto *GV = GVList[0]; |
| 908 | const auto constants_byte_size = |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 909 | (GetTypeSizeInBits(GV->getInitializer()->getType(), DL)) / 8; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 910 | const size_t kConstantMaxSize = 65536; |
| 911 | if (constants_byte_size > kConstantMaxSize) { |
| 912 | outs() << "Max __constant capacity of " << kConstantMaxSize |
| 913 | << " bytes exceeded: " << constants_byte_size << " bytes used\n"; |
| 914 | llvm_unreachable("Max __constant capacity exceeded"); |
| 915 | } |
| 916 | } |
| 917 | } else { |
| 918 | // Change global constant variable's address space to ModuleScopePrivate. |
| 919 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 920 | for (auto GV : GVList) { |
| 921 | // Create new gv with ModuleScopePrivate address space. |
| 922 | Type *NewGVTy = GV->getType()->getPointerElementType(); |
| 923 | GlobalVariable *NewGV = new GlobalVariable( |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 924 | *module, NewGVTy, false, GV->getLinkage(), GV->getInitializer(), "", |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 925 | nullptr, GV->getThreadLocalMode(), AddressSpace::ModuleScopePrivate); |
| 926 | NewGV->takeName(GV); |
| 927 | |
| 928 | const SmallVector<User *, 8> GVUsers(GV->user_begin(), GV->user_end()); |
| 929 | SmallVector<User *, 8> CandidateUsers; |
| 930 | |
| 931 | auto record_called_function_type_as_user = |
| 932 | [&GlobalConstFuncTyMap](Value *gv, CallInst *call) { |
| 933 | // Find argument index. |
| 934 | unsigned index = 0; |
| 935 | for (unsigned i = 0; i < call->getNumArgOperands(); i++) { |
| 936 | if (gv == call->getOperand(i)) { |
| 937 | // TODO(dneto): Should we break here? |
| 938 | index = i; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | // Record function type with global constant. |
| 943 | GlobalConstFuncTyMap[call->getFunctionType()] = |
| 944 | std::make_pair(call->getFunctionType(), index); |
| 945 | }; |
| 946 | |
| 947 | for (User *GVU : GVUsers) { |
| 948 | if (CallInst *Call = dyn_cast<CallInst>(GVU)) { |
| 949 | record_called_function_type_as_user(GV, Call); |
| 950 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(GVU)) { |
| 951 | // Check GEP users. |
| 952 | for (User *GEPU : GEP->users()) { |
| 953 | if (CallInst *GEPCall = dyn_cast<CallInst>(GEPU)) { |
| 954 | record_called_function_type_as_user(GEP, GEPCall); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | CandidateUsers.push_back(GVU); |
| 960 | } |
| 961 | |
| 962 | for (User *U : CandidateUsers) { |
| 963 | // Update users of gv with new gv. |
alan-baker | ed80f57 | 2019-02-11 17:28:26 -0500 | [diff] [blame] | 964 | if (!isa<Constant>(U)) { |
| 965 | // #254: Can't change operands of a constant, but this shouldn't be |
| 966 | // something that sticks around in the module. |
| 967 | U->replaceUsesOfWith(GV, NewGV); |
| 968 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | // Delete original gv. |
| 972 | GV->eraseFromParent(); |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 977 | void SPIRVProducerPass::FindResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 978 | ResourceVarInfoList.clear(); |
| 979 | FunctionToResourceVarsMap.clear(); |
| 980 | ModuleOrderedResourceVars.reset(); |
| 981 | // Normally, there is one resource variable per clspv.resource.var.* |
| 982 | // function, since that is unique'd by arg type and index. By design, |
| 983 | // we can share these resource variables across kernels because all |
| 984 | // kernels use the same descriptor set. |
| 985 | // |
| 986 | // But if the user requested distinct descriptor sets per kernel, then |
| 987 | // the descriptor allocator has made different (set,binding) pairs for |
| 988 | // the same (type,arg_index) pair. Since we can decorate a resource |
| 989 | // variable with only exactly one DescriptorSet and Binding, we are |
| 990 | // forced in this case to make distinct resource variables whenever |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 991 | // the same clspv.resource.var.X function is seen with disintct |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 992 | // (set,binding) values. |
| 993 | const bool always_distinct_sets = |
| 994 | clspv::Option::DistinctKernelDescriptorSets(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 995 | for (Function &F : *module) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 996 | // Rely on the fact the resource var functions have a stable ordering |
| 997 | // in the module. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 998 | if (Builtins::Lookup(&F) == Builtins::kClspvResource) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 999 | // Find all calls to this function with distinct set and binding pairs. |
| 1000 | // Save them in ResourceVarInfoList. |
| 1001 | |
| 1002 | // Determine uniqueness of the (set,binding) pairs only withing this |
| 1003 | // one resource-var builtin function. |
| 1004 | using SetAndBinding = std::pair<unsigned, unsigned>; |
| 1005 | // Maps set and binding to the resource var info. |
| 1006 | DenseMap<SetAndBinding, ResourceVarInfo *> set_and_binding_map; |
| 1007 | bool first_use = true; |
| 1008 | for (auto &U : F.uses()) { |
| 1009 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 1010 | const auto set = unsigned( |
| 1011 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 1012 | const auto binding = unsigned( |
| 1013 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 1014 | const auto arg_kind = clspv::ArgKind( |
| 1015 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 1016 | const auto arg_index = unsigned( |
| 1017 | dyn_cast<ConstantInt>(call->getArgOperand(3))->getZExtValue()); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 1018 | const auto coherent = unsigned( |
| 1019 | dyn_cast<ConstantInt>(call->getArgOperand(5))->getZExtValue()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1020 | |
| 1021 | // Find or make the resource var info for this combination. |
| 1022 | ResourceVarInfo *rv = nullptr; |
| 1023 | if (always_distinct_sets) { |
| 1024 | // Make a new resource var any time we see a different |
| 1025 | // (set,binding) pair. |
| 1026 | SetAndBinding key{set, binding}; |
| 1027 | auto where = set_and_binding_map.find(key); |
| 1028 | if (where == set_and_binding_map.end()) { |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 1029 | rv = new ResourceVarInfo( |
| 1030 | static_cast<int>(ResourceVarInfoList.size()), set, binding, |
| 1031 | &F, arg_kind, coherent); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1032 | ResourceVarInfoList.emplace_back(rv); |
| 1033 | set_and_binding_map[key] = rv; |
| 1034 | } else { |
| 1035 | rv = where->second; |
| 1036 | } |
| 1037 | } else { |
| 1038 | // The default is to make exactly one resource for each |
| 1039 | // clspv.resource.var.* function. |
| 1040 | if (first_use) { |
| 1041 | first_use = false; |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 1042 | rv = new ResourceVarInfo( |
| 1043 | static_cast<int>(ResourceVarInfoList.size()), set, binding, |
| 1044 | &F, arg_kind, coherent); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1045 | ResourceVarInfoList.emplace_back(rv); |
| 1046 | } else { |
| 1047 | rv = ResourceVarInfoList.back().get(); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // Now populate FunctionToResourceVarsMap. |
| 1052 | auto &mapping = |
| 1053 | FunctionToResourceVarsMap[call->getParent()->getParent()]; |
| 1054 | while (mapping.size() <= arg_index) { |
| 1055 | mapping.push_back(nullptr); |
| 1056 | } |
| 1057 | mapping[arg_index] = rv; |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | // Populate ModuleOrderedResourceVars. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1064 | for (Function &F : *module) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1065 | auto where = FunctionToResourceVarsMap.find(&F); |
| 1066 | if (where != FunctionToResourceVarsMap.end()) { |
| 1067 | for (auto &rv : where->second) { |
| 1068 | if (rv != nullptr) { |
| 1069 | ModuleOrderedResourceVars.insert(rv); |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | if (ShowResourceVars) { |
| 1075 | for (auto *info : ModuleOrderedResourceVars) { |
| 1076 | outs() << "MORV index " << info->index << " (" << info->descriptor_set |
| 1077 | << "," << info->binding << ") " << *(info->var_fn->getReturnType()) |
| 1078 | << "\n"; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1083 | void SPIRVProducerPass::FindTypePerGlobalVar(GlobalVariable &GV) { |
| 1084 | // Investigate global variable's type. |
| 1085 | FindType(GV.getType()); |
| 1086 | } |
| 1087 | |
| 1088 | void SPIRVProducerPass::FindTypePerFunc(Function &F) { |
| 1089 | // Investigate function's type. |
| 1090 | FunctionType *FTy = F.getFunctionType(); |
| 1091 | |
| 1092 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 1093 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1094 | // Handle a regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1095 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 1096 | uint32_t GVCstArgIdx = GlobalConstFuncTypeMap[FTy].second; |
| 1097 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1098 | for (unsigned i = 0; i < FTy->getNumParams(); i++) { |
| 1099 | Type *ParamTy = FTy->getParamType(i); |
| 1100 | if (i == GVCstArgIdx) { |
| 1101 | Type *EleTy = ParamTy->getPointerElementType(); |
| 1102 | ParamTy = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1103 | } |
| 1104 | |
| 1105 | NewFuncParamTys.push_back(ParamTy); |
| 1106 | } |
| 1107 | |
| 1108 | FunctionType *NewFTy = |
| 1109 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1110 | GlobalConstFuncTyMap[FTy] = std::make_pair(NewFTy, GVCstArgIdx); |
| 1111 | FTy = NewFTy; |
| 1112 | } |
| 1113 | |
| 1114 | FindType(FTy); |
| 1115 | } else { |
| 1116 | // As kernel functions do not have parameters, create new function type and |
| 1117 | // add it to type map. |
| 1118 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1119 | FunctionType *NewFTy = |
| 1120 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1121 | FindType(NewFTy); |
| 1122 | } |
| 1123 | |
| 1124 | // Investigate instructions' type in function body. |
| 1125 | for (BasicBlock &BB : F) { |
| 1126 | for (Instruction &I : BB) { |
| 1127 | if (isa<ShuffleVectorInst>(I)) { |
| 1128 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1129 | // Ignore type for mask of shuffle vector instruction. |
| 1130 | if (i == 2) { |
| 1131 | continue; |
| 1132 | } |
| 1133 | |
| 1134 | Value *Op = I.getOperand(i); |
| 1135 | if (!isa<MetadataAsValue>(Op)) { |
| 1136 | FindType(Op->getType()); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | FindType(I.getType()); |
| 1141 | continue; |
| 1142 | } |
| 1143 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1144 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 1145 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1146 | if (Call) { |
| 1147 | auto &func_info = Builtins::Lookup(Call->getCalledFunction()); |
| 1148 | if (func_info.getType() == Builtins::kClspvResource || |
| 1149 | func_info.getType() == Builtins::kClspvLocal) { |
| 1150 | // This is a fake call representing access to a resource/workgroup |
| 1151 | // variable. We handle that elsewhere. |
| 1152 | continue; |
| 1153 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1154 | } |
| 1155 | |
alan-baker | f083bed | 2020-01-29 08:15:42 -0500 | [diff] [blame] | 1156 | // #497: InsertValue and ExtractValue map to OpCompositeInsert and |
| 1157 | // OpCompositeExtract which takes literal values for indices. As a result |
| 1158 | // don't map the type of indices. |
| 1159 | if (I.getOpcode() == Instruction::ExtractValue) { |
| 1160 | FindType(I.getOperand(0)->getType()); |
| 1161 | continue; |
| 1162 | } |
| 1163 | if (I.getOpcode() == Instruction::InsertValue) { |
| 1164 | FindType(I.getOperand(0)->getType()); |
| 1165 | FindType(I.getOperand(1)->getType()); |
| 1166 | continue; |
| 1167 | } |
| 1168 | |
| 1169 | // #497: InsertElement and ExtractElement map to OpCompositeExtract if |
| 1170 | // the index is a constant. In such a case don't map the index type. |
| 1171 | if (I.getOpcode() == Instruction::ExtractElement) { |
| 1172 | FindType(I.getOperand(0)->getType()); |
| 1173 | Value *op1 = I.getOperand(1); |
| 1174 | if (!isa<Constant>(op1) || isa<GlobalValue>(op1)) { |
| 1175 | FindType(op1->getType()); |
| 1176 | } |
| 1177 | continue; |
| 1178 | } |
| 1179 | if (I.getOpcode() == Instruction::InsertElement) { |
| 1180 | FindType(I.getOperand(0)->getType()); |
| 1181 | FindType(I.getOperand(1)->getType()); |
| 1182 | Value *op2 = I.getOperand(2); |
| 1183 | if (!isa<Constant>(op2) || isa<GlobalValue>(op2)) { |
| 1184 | FindType(op2->getType()); |
| 1185 | } |
| 1186 | continue; |
| 1187 | } |
| 1188 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1189 | // Work through the operands of the instruction. |
| 1190 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1191 | Value *const Op = I.getOperand(i); |
| 1192 | // If any of the operands is a constant, find the type! |
| 1193 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1194 | FindType(Op->getType()); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | for (Use &Op : I.operands()) { |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame] | 1199 | if (isa<CallInst>(&I)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1200 | // Avoid to check call instruction's type. |
| 1201 | break; |
| 1202 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1203 | if (CallInst *OpCall = dyn_cast<CallInst>(Op)) { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1204 | if (Builtins::Lookup(OpCall->getCalledFunction()) == |
| 1205 | Builtins::kClspvLocal) { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1206 | // This is a fake call representing access to a workgroup variable. |
| 1207 | // We handle that elsewhere. |
| 1208 | continue; |
| 1209 | } |
| 1210 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1211 | if (!isa<MetadataAsValue>(&Op)) { |
| 1212 | FindType(Op->getType()); |
| 1213 | continue; |
| 1214 | } |
| 1215 | } |
| 1216 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1217 | // We don't want to track the type of this call as we are going to replace |
| 1218 | // it. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 1219 | if (Call && Builtins::Lookup(Call->getCalledFunction()) == |
| 1220 | Builtins::kClspvSamplerVarLiteral) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1221 | continue; |
| 1222 | } |
| 1223 | |
| 1224 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 1225 | // If gep's base operand has ModuleScopePrivate address space, make gep |
| 1226 | // return ModuleScopePrivate address space. |
| 1227 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate) { |
| 1228 | // Add pointer type with private address space for global constant to |
| 1229 | // type list. |
| 1230 | Type *EleTy = I.getType()->getPointerElementType(); |
| 1231 | Type *NewPTy = |
| 1232 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1233 | |
| 1234 | FindType(NewPTy); |
| 1235 | continue; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | FindType(I.getType()); |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1244 | void SPIRVProducerPass::FindTypesForSamplerMap() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1245 | // If we are using a sampler map, find the type of the sampler. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1246 | if (module->getFunction(clspv::LiteralSamplerFunction()) || |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 1247 | !getSamplerMap().empty()) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1248 | auto SamplerStructTy = module->getTypeByName("opencl.sampler_t"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1249 | if (!SamplerStructTy) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1250 | SamplerStructTy = |
| 1251 | StructType::create(module->getContext(), "opencl.sampler_t"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | SamplerTy = SamplerStructTy->getPointerTo(AddressSpace::UniformConstant); |
| 1255 | |
| 1256 | FindType(SamplerTy); |
| 1257 | } |
| 1258 | } |
| 1259 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1260 | void SPIRVProducerPass::FindTypesForResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1261 | // Record types so they are generated. |
| 1262 | TypesNeedingLayout.reset(); |
| 1263 | StructTypesNeedingBlock.reset(); |
| 1264 | |
| 1265 | // To match older clspv codegen, generate the float type first if required |
| 1266 | // for images. |
| 1267 | for (const auto *info : ModuleOrderedResourceVars) { |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1268 | if (info->arg_kind == clspv::ArgKind::SampledImage || |
| 1269 | info->arg_kind == clspv::ArgKind::StorageImage) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1270 | if (IsIntImageType(info->var_fn->getReturnType())) { |
| 1271 | // Nothing for now... |
| 1272 | } else if (IsUintImageType(info->var_fn->getReturnType())) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1273 | FindType(Type::getInt32Ty(module->getContext())); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | // We need "float" either for the sampled type or for the Lod operand. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1277 | FindType(Type::getFloatTy(module->getContext())); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1282 | Type *type = info->var_fn->getReturnType(); |
| 1283 | |
| 1284 | switch (info->arg_kind) { |
| 1285 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1286 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1287 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1288 | StructTypesNeedingBlock.insert(sty); |
| 1289 | } else { |
| 1290 | errs() << *type << "\n"; |
| 1291 | llvm_unreachable("Buffer arguments must map to structures!"); |
| 1292 | } |
| 1293 | break; |
| 1294 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 1295 | case clspv::ArgKind::PodUBO: |
| 1296 | case clspv::ArgKind::PodPushConstant: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1297 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1298 | StructTypesNeedingBlock.insert(sty); |
| 1299 | } else { |
| 1300 | errs() << *type << "\n"; |
| 1301 | llvm_unreachable("POD arguments must map to structures!"); |
| 1302 | } |
| 1303 | break; |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1304 | case clspv::ArgKind::SampledImage: |
| 1305 | case clspv::ArgKind::StorageImage: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1306 | case clspv::ArgKind::Sampler: |
| 1307 | // Sampler and image types map to the pointee type but |
| 1308 | // in the uniform constant address space. |
| 1309 | type = PointerType::get(type->getPointerElementType(), |
| 1310 | clspv::AddressSpace::UniformConstant); |
| 1311 | break; |
| 1312 | default: |
| 1313 | break; |
| 1314 | } |
| 1315 | |
| 1316 | // The converted type is the type of the OpVariable we will generate. |
| 1317 | // If the pointee type is an array of size zero, FindType will convert it |
| 1318 | // to a runtime array. |
| 1319 | FindType(type); |
| 1320 | } |
| 1321 | |
alan-baker | dcd9741 | 2019-09-16 15:32:30 -0400 | [diff] [blame] | 1322 | // If module constants are clustered in a storage buffer then that struct |
| 1323 | // needs layout decorations. |
| 1324 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1325 | for (GlobalVariable &GV : module->globals()) { |
alan-baker | dcd9741 | 2019-09-16 15:32:30 -0400 | [diff] [blame] | 1326 | PointerType *PTy = cast<PointerType>(GV.getType()); |
| 1327 | const auto AS = PTy->getAddressSpace(); |
| 1328 | const bool module_scope_constant_external_init = |
| 1329 | (AS == AddressSpace::Constant) && GV.hasInitializer(); |
| 1330 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 1331 | if (module_scope_constant_external_init && |
| 1332 | spv::BuiltInMax == BuiltinType) { |
| 1333 | StructTypesNeedingBlock.insert( |
| 1334 | cast<StructType>(PTy->getPointerElementType())); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1339 | for (const GlobalVariable &GV : module->globals()) { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 1340 | if (GV.getAddressSpace() == clspv::AddressSpace::PushConstant) { |
| 1341 | auto Ty = cast<PointerType>(GV.getType())->getPointerElementType(); |
| 1342 | assert(Ty->isStructTy() && "Push constants have to be structures."); |
| 1343 | auto STy = cast<StructType>(Ty); |
| 1344 | StructTypesNeedingBlock.insert(STy); |
| 1345 | } |
| 1346 | } |
| 1347 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1348 | // Traverse the arrays and structures underneath each Block, and |
| 1349 | // mark them as needing layout. |
| 1350 | std::vector<Type *> work_list(StructTypesNeedingBlock.begin(), |
| 1351 | StructTypesNeedingBlock.end()); |
| 1352 | while (!work_list.empty()) { |
| 1353 | Type *type = work_list.back(); |
| 1354 | work_list.pop_back(); |
| 1355 | TypesNeedingLayout.insert(type); |
| 1356 | switch (type->getTypeID()) { |
| 1357 | case Type::ArrayTyID: |
| 1358 | work_list.push_back(type->getArrayElementType()); |
| 1359 | if (!Hack_generate_runtime_array_stride_early) { |
| 1360 | // Remember this array type for deferred decoration. |
| 1361 | TypesNeedingArrayStride.insert(type); |
| 1362 | } |
| 1363 | break; |
| 1364 | case Type::StructTyID: |
| 1365 | for (auto *elem_ty : cast<StructType>(type)->elements()) { |
| 1366 | work_list.push_back(elem_ty); |
| 1367 | } |
| 1368 | default: |
| 1369 | // This type and its contained types don't get layout. |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1375 | void SPIRVProducerPass::GenerateWorkgroupVars() { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1376 | // The SpecId assignment for pointer-to-local arguments is recorded in |
| 1377 | // module-level metadata. Translate that information into local argument |
| 1378 | // information. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1379 | LLVMContext &Context = module->getContext(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1380 | NamedMDNode *nmd = module->getNamedMetadata(clspv::LocalSpecIdMetadataName()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1381 | if (!nmd) |
| 1382 | return; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1383 | for (auto operand : nmd->operands()) { |
| 1384 | MDTuple *tuple = cast<MDTuple>(operand); |
| 1385 | ValueAsMetadata *fn_md = cast<ValueAsMetadata>(tuple->getOperand(0)); |
| 1386 | Function *func = cast<Function>(fn_md->getValue()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1387 | ConstantAsMetadata *arg_index_md = |
| 1388 | cast<ConstantAsMetadata>(tuple->getOperand(1)); |
| 1389 | int arg_index = static_cast<int>( |
| 1390 | cast<ConstantInt>(arg_index_md->getValue())->getSExtValue()); |
| 1391 | Argument *arg = &*(func->arg_begin() + arg_index); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1392 | |
| 1393 | ConstantAsMetadata *spec_id_md = |
| 1394 | cast<ConstantAsMetadata>(tuple->getOperand(2)); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1395 | int spec_id = static_cast<int>( |
| 1396 | cast<ConstantInt>(spec_id_md->getValue())->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1397 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1398 | LocalArgSpecIds[arg] = spec_id; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1399 | if (LocalSpecIdInfoMap.count(spec_id)) |
| 1400 | continue; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1401 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1402 | // Generate the spec constant. |
| 1403 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1404 | Ops << Type::getInt32Ty(Context) << 1; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1405 | SPIRVID ArraySizeID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1406 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1407 | // Generate the array type. |
| 1408 | Type *ElemTy = arg->getType()->getPointerElementType(); |
| 1409 | Ops.clear(); |
| 1410 | // The element type must have been created. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1411 | Ops << ElemTy << ArraySizeID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1412 | |
| 1413 | SPIRVID ArrayTypeID = addSPIRVInst<kTypes>(spv::OpTypeArray, Ops); |
| 1414 | |
| 1415 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1416 | Ops << spv::StorageClassWorkgroup << ArrayTypeID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1417 | SPIRVID PtrArrayTypeID = addSPIRVInst<kTypes>(spv::OpTypePointer, Ops); |
| 1418 | |
| 1419 | // Generate OpVariable. |
| 1420 | // |
| 1421 | // Ops[0] : Result Type ID |
| 1422 | // Ops[1] : Storage Class |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 1423 | SPIRVID VariableID = |
| 1424 | addSPIRVGlobalVariable(PtrArrayTypeID, spv::StorageClassWorkgroup); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1425 | |
| 1426 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1427 | Ops << ArraySizeID << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1428 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 1429 | |
| 1430 | LocalArgInfo info{VariableID, ElemTy, ArraySizeID, |
| 1431 | ArrayTypeID, PtrArrayTypeID, spec_id}; |
| 1432 | LocalSpecIdInfoMap[spec_id] = info; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1436 | void SPIRVProducerPass::FindType(Type *Ty) { |
| 1437 | TypeList &TyList = getTypeList(); |
| 1438 | |
| 1439 | if (0 != TyList.idFor(Ty)) { |
| 1440 | return; |
| 1441 | } |
| 1442 | |
| 1443 | if (Ty->isPointerTy()) { |
| 1444 | auto AddrSpace = Ty->getPointerAddressSpace(); |
| 1445 | if ((AddressSpace::Constant == AddrSpace) || |
| 1446 | (AddressSpace::Global == AddrSpace)) { |
| 1447 | auto PointeeTy = Ty->getPointerElementType(); |
| 1448 | |
| 1449 | if (PointeeTy->isStructTy() && |
| 1450 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1451 | FindType(PointeeTy); |
| 1452 | auto ActualPointerTy = |
| 1453 | PointeeTy->getPointerTo(AddressSpace::UniformConstant); |
| 1454 | FindType(ActualPointerTy); |
| 1455 | return; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1460 | // By convention, LLVM array type with 0 elements will map to |
| 1461 | // OpTypeRuntimeArray. Otherwise, it will map to OpTypeArray, which |
| 1462 | // has a constant number of elements. We need to support type of the |
| 1463 | // constant. |
| 1464 | if (auto *arrayTy = dyn_cast<ArrayType>(Ty)) { |
| 1465 | if (arrayTy->getNumElements() > 0) { |
| 1466 | LLVMContext &Context = Ty->getContext(); |
| 1467 | FindType(Type::getInt32Ty(Context)); |
| 1468 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | for (Type *SubTy : Ty->subtypes()) { |
| 1472 | FindType(SubTy); |
| 1473 | } |
| 1474 | |
| 1475 | TyList.insert(Ty); |
| 1476 | } |
| 1477 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1478 | spv::StorageClass SPIRVProducerPass::GetStorageClass(unsigned AddrSpace) const { |
| 1479 | switch (AddrSpace) { |
| 1480 | default: |
| 1481 | llvm_unreachable("Unsupported OpenCL address space"); |
| 1482 | case AddressSpace::Private: |
| 1483 | return spv::StorageClassFunction; |
| 1484 | case AddressSpace::Global: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1485 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1486 | case AddressSpace::Constant: |
| 1487 | return clspv::Option::ConstantArgsInUniformBuffer() |
| 1488 | ? spv::StorageClassUniform |
| 1489 | : spv::StorageClassStorageBuffer; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1490 | case AddressSpace::Input: |
| 1491 | return spv::StorageClassInput; |
| 1492 | case AddressSpace::Local: |
| 1493 | return spv::StorageClassWorkgroup; |
| 1494 | case AddressSpace::UniformConstant: |
| 1495 | return spv::StorageClassUniformConstant; |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1496 | case AddressSpace::Uniform: |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 1497 | return spv::StorageClassUniform; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1498 | case AddressSpace::ModuleScopePrivate: |
| 1499 | return spv::StorageClassPrivate; |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 1500 | case AddressSpace::PushConstant: |
| 1501 | return spv::StorageClassPushConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1505 | spv::StorageClass |
| 1506 | SPIRVProducerPass::GetStorageClassForArgKind(clspv::ArgKind arg_kind) const { |
| 1507 | switch (arg_kind) { |
| 1508 | case clspv::ArgKind::Buffer: |
| 1509 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1510 | case clspv::ArgKind::BufferUBO: |
| 1511 | return spv::StorageClassUniform; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1512 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 1513 | return spv::StorageClassStorageBuffer; |
| 1514 | case clspv::ArgKind::PodUBO: |
| 1515 | return spv::StorageClassUniform; |
| 1516 | case clspv::ArgKind::PodPushConstant: |
| 1517 | return spv::StorageClassPushConstant; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1518 | case clspv::ArgKind::Local: |
| 1519 | return spv::StorageClassWorkgroup; |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1520 | case clspv::ArgKind::SampledImage: |
| 1521 | case clspv::ArgKind::StorageImage: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1522 | case clspv::ArgKind::Sampler: |
| 1523 | return spv::StorageClassUniformConstant; |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame] | 1524 | default: |
| 1525 | llvm_unreachable("Unsupported storage class for argument kind"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1526 | } |
| 1527 | } |
| 1528 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1529 | spv::BuiltIn SPIRVProducerPass::GetBuiltin(StringRef Name) const { |
| 1530 | return StringSwitch<spv::BuiltIn>(Name) |
| 1531 | .Case("__spirv_GlobalInvocationId", spv::BuiltInGlobalInvocationId) |
| 1532 | .Case("__spirv_LocalInvocationId", spv::BuiltInLocalInvocationId) |
| 1533 | .Case("__spirv_WorkgroupSize", spv::BuiltInWorkgroupSize) |
| 1534 | .Case("__spirv_NumWorkgroups", spv::BuiltInNumWorkgroups) |
| 1535 | .Case("__spirv_WorkgroupId", spv::BuiltInWorkgroupId) |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 1536 | .Case("__spirv_WorkDim", spv::BuiltInWorkDim) |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 1537 | .Case("__spirv_GlobalOffset", spv::BuiltInGlobalOffset) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1538 | .Default(spv::BuiltInMax); |
| 1539 | } |
| 1540 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1541 | SPIRVID SPIRVProducerPass::getOpExtInstImportID() { |
| 1542 | if (OpExtInstImportID == 0) { |
| 1543 | // |
| 1544 | // Generate OpExtInstImport. |
| 1545 | // |
| 1546 | // Ops[0] ... Ops[n] = Name (Literal String) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1547 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1548 | OpExtInstImportID = |
| 1549 | addSPIRVInst<kImports>(spv::OpExtInstImport, "GLSL.std.450"); |
| 1550 | } |
| 1551 | return OpExtInstImportID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1552 | } |
| 1553 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 1554 | SPIRVID SPIRVProducerPass::addSPIRVGlobalVariable(const SPIRVID &TypeID, |
| 1555 | spv::StorageClass SC, |
| 1556 | const SPIRVID &InitID) { |
| 1557 | // Generate OpVariable. |
| 1558 | // |
| 1559 | // Ops[0] : Result Type ID |
| 1560 | // Ops[1] : Storage Class |
| 1561 | // Ops[2] : Initialization Value ID (optional) |
| 1562 | |
| 1563 | SPIRVOperandVec Ops; |
| 1564 | Ops << TypeID << SC; |
| 1565 | if (InitID.isValid()) { |
| 1566 | Ops << InitID; |
| 1567 | } |
| 1568 | |
| 1569 | SPIRVID VID = addSPIRVInst<kGlobalVariables>(spv::OpVariable, Ops); |
| 1570 | |
| 1571 | if (SC == spv::StorageClassInput) { |
| 1572 | getEntryPointInterfacesList().push_back(VID); |
| 1573 | } |
| 1574 | |
| 1575 | return VID; |
| 1576 | } |
| 1577 | |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1578 | Type *SPIRVProducerPass::CanonicalType(Type *type) { |
| 1579 | if (type->getNumContainedTypes() != 0) { |
| 1580 | switch (type->getTypeID()) { |
| 1581 | case Type::PointerTyID: { |
| 1582 | // For the purposes of our Vulkan SPIR-V type system, constant and global |
| 1583 | // are conflated. |
| 1584 | auto *ptr_ty = cast<PointerType>(type); |
| 1585 | unsigned AddrSpace = ptr_ty->getAddressSpace(); |
| 1586 | if (AddressSpace::Constant == AddrSpace) { |
| 1587 | if (!clspv::Option::ConstantArgsInUniformBuffer()) { |
| 1588 | AddrSpace = AddressSpace::Global; |
| 1589 | // The canonical type of __constant is __global unless constants are |
| 1590 | // passed in uniform buffers. |
| 1591 | auto *GlobalTy = |
| 1592 | ptr_ty->getPointerElementType()->getPointerTo(AddrSpace); |
| 1593 | return GlobalTy; |
| 1594 | } |
| 1595 | } |
| 1596 | break; |
| 1597 | } |
| 1598 | case Type::StructTyID: { |
| 1599 | SmallVector<Type *, 8> subtypes; |
| 1600 | bool changed = false; |
| 1601 | for (auto *subtype : type->subtypes()) { |
| 1602 | auto canonical = CanonicalType(subtype); |
| 1603 | subtypes.push_back(canonical); |
| 1604 | if (canonical != subtype) { |
| 1605 | changed = true; |
| 1606 | } |
| 1607 | } |
| 1608 | if (changed) { |
| 1609 | return StructType::get(type->getContext(), subtypes, |
| 1610 | cast<StructType>(type)->isPacked()); |
| 1611 | } |
| 1612 | break; |
| 1613 | } |
| 1614 | case Type::ArrayTyID: { |
| 1615 | auto *elem_ty = type->getArrayElementType(); |
| 1616 | auto *equiv_elem_ty = CanonicalType(elem_ty); |
| 1617 | if (equiv_elem_ty != elem_ty) { |
| 1618 | return ArrayType::get(equiv_elem_ty, |
| 1619 | cast<ArrayType>(type)->getNumElements()); |
| 1620 | } |
| 1621 | break; |
| 1622 | } |
| 1623 | case Type::FunctionTyID: { |
| 1624 | auto *func_ty = cast<FunctionType>(type); |
| 1625 | auto *return_ty = CanonicalType(func_ty->getReturnType()); |
| 1626 | SmallVector<Type *, 8> params; |
| 1627 | for (unsigned i = 0; i < func_ty->getNumParams(); ++i) { |
| 1628 | params.push_back(CanonicalType(func_ty->getParamType(i))); |
| 1629 | } |
| 1630 | return FunctionType::get(return_ty, params, func_ty->isVarArg()); |
| 1631 | } |
| 1632 | default: |
| 1633 | break; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | return type; |
| 1638 | } |
| 1639 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1640 | SPIRVID SPIRVProducerPass::getSPIRVType(Type *Ty) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1641 | auto TI = TypeMap.find(Ty); |
| 1642 | if (TI != TypeMap.end()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1643 | assert(TI->second.isValid()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1644 | return TI->second; |
| 1645 | } |
| 1646 | |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1647 | auto Canonical = CanonicalType(Ty); |
| 1648 | if (Canonical != Ty) { |
| 1649 | auto CanonicalTI = TypeMap.find(Canonical); |
| 1650 | if (CanonicalTI != TypeMap.end()) { |
| 1651 | assert(CanonicalTI->second.isValid()); |
| 1652 | return CanonicalTI->second; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | // Perform the mapping with the canonical type. |
| 1657 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1658 | const auto &DL = module->getDataLayout(); |
| 1659 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1660 | SPIRVID RID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1661 | |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1662 | switch (Canonical->getTypeID()) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1663 | default: { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1664 | Canonical->print(errs()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1665 | llvm_unreachable("Unsupported type???"); |
| 1666 | break; |
| 1667 | } |
| 1668 | case Type::MetadataTyID: |
| 1669 | case Type::LabelTyID: { |
| 1670 | // Ignore these types. |
| 1671 | break; |
| 1672 | } |
| 1673 | case Type::PointerTyID: { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1674 | PointerType *PTy = cast<PointerType>(Canonical); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1675 | unsigned AddrSpace = PTy->getAddressSpace(); |
| 1676 | |
| 1677 | if (AddrSpace != AddressSpace::UniformConstant) { |
| 1678 | auto PointeeTy = PTy->getElementType(); |
| 1679 | if (PointeeTy->isStructTy() && |
| 1680 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1681 | // TODO(sjw): assert always an image? |
| 1682 | RID = getSPIRVType(PointeeTy); |
| 1683 | break; |
| 1684 | } |
| 1685 | } |
| 1686 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1687 | // |
| 1688 | // Generate OpTypePointer. |
| 1689 | // |
| 1690 | |
| 1691 | // OpTypePointer |
| 1692 | // Ops[0] = Storage Class |
| 1693 | // Ops[1] = Element Type ID |
| 1694 | SPIRVOperandVec Ops; |
| 1695 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1696 | Ops << GetStorageClass(AddrSpace) << PTy->getElementType(); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1697 | |
| 1698 | RID = addSPIRVInst<kTypes>(spv::OpTypePointer, Ops); |
| 1699 | break; |
| 1700 | } |
| 1701 | case Type::StructTyID: { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1702 | StructType *STy = cast<StructType>(Canonical); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1703 | |
| 1704 | // Handle sampler type. |
| 1705 | if (STy->isOpaque()) { |
| 1706 | if (STy->getName().equals("opencl.sampler_t")) { |
| 1707 | // |
| 1708 | // Generate OpTypeSampler |
| 1709 | // |
| 1710 | // Empty Ops. |
| 1711 | |
| 1712 | RID = addSPIRVInst<kTypes>(spv::OpTypeSampler); |
| 1713 | break; |
| 1714 | } else if (STy->getName().startswith("opencl.image1d_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1715 | STy->getName().startswith("opencl.image1d_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1716 | STy->getName().startswith("opencl.image1d_wo_t") || |
| 1717 | STy->getName().startswith("opencl.image1d_array_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1718 | STy->getName().startswith("opencl.image1d_array_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1719 | STy->getName().startswith("opencl.image1d_array_wo_t") || |
| 1720 | STy->getName().startswith("opencl.image2d_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1721 | STy->getName().startswith("opencl.image2d_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1722 | STy->getName().startswith("opencl.image2d_wo_t") || |
| 1723 | STy->getName().startswith("opencl.image2d_array_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1724 | STy->getName().startswith("opencl.image2d_array_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1725 | STy->getName().startswith("opencl.image2d_array_wo_t") || |
| 1726 | STy->getName().startswith("opencl.image3d_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1727 | STy->getName().startswith("opencl.image3d_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1728 | STy->getName().startswith("opencl.image3d_wo_t")) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1729 | if (STy->getName().startswith("opencl.image1d_")) { |
| 1730 | if (STy->getName().contains(".sampled")) |
| 1731 | addCapability(spv::CapabilitySampled1D); |
| 1732 | else |
| 1733 | addCapability(spv::CapabilityImage1D); |
| 1734 | } |
| 1735 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1736 | // |
| 1737 | // Generate OpTypeImage |
| 1738 | // |
| 1739 | // Ops[0] = Sampled Type ID |
| 1740 | // Ops[1] = Dim ID |
| 1741 | // Ops[2] = Depth (Literal Number) |
| 1742 | // Ops[3] = Arrayed (Literal Number) |
| 1743 | // Ops[4] = MS (Literal Number) |
| 1744 | // Ops[5] = Sampled (Literal Number) |
| 1745 | // Ops[6] = Image Format ID |
| 1746 | // |
| 1747 | SPIRVOperandVec Ops; |
| 1748 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1749 | SPIRVID SampledTyID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1750 | if (STy->getName().contains(".float")) { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1751 | SampledTyID = getSPIRVType(Type::getFloatTy(Canonical->getContext())); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1752 | } else if (STy->getName().contains(".uint")) { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1753 | SampledTyID = getSPIRVType(Type::getInt32Ty(Canonical->getContext())); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1754 | } else if (STy->getName().contains(".int")) { |
| 1755 | // Generate a signed 32-bit integer if necessary. |
| 1756 | if (int32ID == 0) { |
| 1757 | SPIRVOperandVec intOps; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1758 | intOps << 32 << 1; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1759 | int32ID = addSPIRVInst<kTypes>(spv::OpTypeInt, intOps); |
| 1760 | } |
| 1761 | SampledTyID = int32ID; |
| 1762 | |
| 1763 | // Generate a vec4 of the signed int if necessary. |
| 1764 | if (v4int32ID == 0) { |
| 1765 | SPIRVOperandVec vecOps; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1766 | vecOps << int32ID << 4; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1767 | v4int32ID = addSPIRVInst<kTypes>(spv::OpTypeVector, vecOps); |
| 1768 | } |
| 1769 | } else { |
| 1770 | // This was likely an UndefValue. |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1771 | SampledTyID = getSPIRVType(Type::getFloatTy(Canonical->getContext())); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1772 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1773 | Ops << SampledTyID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1774 | |
| 1775 | spv::Dim DimID = spv::Dim2D; |
| 1776 | if (STy->getName().startswith("opencl.image1d_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1777 | STy->getName().startswith("opencl.image1d_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1778 | STy->getName().startswith("opencl.image1d_wo_t") || |
| 1779 | STy->getName().startswith("opencl.image1d_array_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1780 | STy->getName().startswith("opencl.image1d_array_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1781 | STy->getName().startswith("opencl.image1d_array_wo_t")) { |
| 1782 | DimID = spv::Dim1D; |
| 1783 | } else if (STy->getName().startswith("opencl.image3d_ro_t") || |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1784 | STy->getName().startswith("opencl.image3d_rw_t") || |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1785 | STy->getName().startswith("opencl.image3d_wo_t")) { |
| 1786 | DimID = spv::Dim3D; |
| 1787 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1788 | Ops << DimID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1789 | |
| 1790 | // TODO: Set up Depth. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1791 | Ops << 0; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1792 | |
| 1793 | uint32_t arrayed = STy->getName().contains("_array_") ? 1 : 0; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1794 | Ops << arrayed; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1795 | |
| 1796 | // TODO: Set up MS. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1797 | Ops << 0; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1798 | |
| 1799 | // Set up Sampled. |
| 1800 | // |
| 1801 | // From Spec |
| 1802 | // |
| 1803 | // 0 indicates this is only known at run time, not at compile time |
| 1804 | // 1 indicates will be used with sampler |
| 1805 | // 2 indicates will be used without a sampler (a storage image) |
| 1806 | uint32_t Sampled = 1; |
| 1807 | if (!STy->getName().contains(".sampled")) { |
| 1808 | Sampled = 2; |
| 1809 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1810 | Ops << Sampled; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1811 | |
| 1812 | // TODO: Set up Image Format. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1813 | Ops << spv::ImageFormatUnknown; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1814 | RID = addSPIRVInst<kTypes>(spv::OpTypeImage, Ops); |
| 1815 | |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1816 | // Only need a sampled version of the type if it is used with a sampler. |
| 1817 | if (Sampled == 1) { |
| 1818 | Ops.clear(); |
| 1819 | Ops << RID; |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1820 | getImageTypeMap()[Canonical] = |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 1821 | addSPIRVInst<kTypes>(spv::OpTypeSampledImage, Ops); |
| 1822 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1823 | break; |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | // |
| 1828 | // Generate OpTypeStruct |
| 1829 | // |
| 1830 | // Ops[0] ... Ops[n] = Member IDs |
| 1831 | SPIRVOperandVec Ops; |
| 1832 | |
| 1833 | for (auto *EleTy : STy->elements()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1834 | Ops << EleTy; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | RID = addSPIRVInst<kTypes>(spv::OpTypeStruct, Ops); |
| 1838 | |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1839 | // Generate OpMemberDecorate unless we are generating it for the canonical |
| 1840 | // type. |
| 1841 | StructType *canonical = cast<StructType>(CanonicalType(STy)); |
| 1842 | if (TypesNeedingLayout.idFor(STy) && |
| 1843 | (canonical == STy || !TypesNeedingLayout.idFor(canonical))) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1844 | for (unsigned MemberIdx = 0; MemberIdx < STy->getNumElements(); |
| 1845 | MemberIdx++) { |
| 1846 | // Ops[0] = Structure Type ID |
| 1847 | // Ops[1] = Member Index(Literal Number) |
| 1848 | // Ops[2] = Decoration (Offset) |
| 1849 | // Ops[3] = Byte Offset (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1850 | const auto ByteOffset = |
| 1851 | GetExplicitLayoutStructMemberOffset(STy, MemberIdx, DL); |
| 1852 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1853 | Ops.clear(); |
| 1854 | Ops << RID << MemberIdx << spv::DecorationOffset << ByteOffset; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1855 | |
| 1856 | addSPIRVInst<kAnnotations>(spv::OpMemberDecorate, Ops); |
| 1857 | } |
| 1858 | } |
| 1859 | |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1860 | // Generate OpDecorate unless we are generating it for the canonical type. |
| 1861 | if (StructTypesNeedingBlock.idFor(STy) && |
| 1862 | (canonical == STy || !StructTypesNeedingBlock.idFor(canonical))) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1863 | Ops.clear(); |
| 1864 | // Use Block decorations with StorageBuffer storage class. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1865 | Ops << RID << spv::DecorationBlock; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1866 | |
| 1867 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 1868 | } |
| 1869 | break; |
| 1870 | } |
| 1871 | case Type::IntegerTyID: { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1872 | uint32_t bit_width = |
| 1873 | static_cast<uint32_t>(Canonical->getPrimitiveSizeInBits()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1874 | |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1875 | if (clspv::Option::Int8Support() && bit_width == 8) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1876 | addCapability(spv::CapabilityInt8); |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1877 | } else if (bit_width == 16) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1878 | addCapability(spv::CapabilityInt16); |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1879 | } else if (bit_width == 64) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1880 | addCapability(spv::CapabilityInt64); |
| 1881 | } |
| 1882 | |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1883 | if (bit_width == 1) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1884 | RID = addSPIRVInst<kTypes>(spv::OpTypeBool); |
| 1885 | } else { |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1886 | if (!clspv::Option::Int8Support() && bit_width == 8) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1887 | // i8 is added to TypeMap as i32. |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1888 | RID = getSPIRVType(Type::getIntNTy(Canonical->getContext(), 32)); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1889 | } else { |
| 1890 | SPIRVOperandVec Ops; |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1891 | Ops << bit_width << 0 /* not signed */; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1892 | RID = addSPIRVInst<kTypes>(spv::OpTypeInt, Ops); |
| 1893 | } |
| 1894 | } |
| 1895 | break; |
| 1896 | } |
| 1897 | case Type::HalfTyID: |
| 1898 | case Type::FloatTyID: |
| 1899 | case Type::DoubleTyID: { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1900 | uint32_t bit_width = |
| 1901 | static_cast<uint32_t>(Canonical->getPrimitiveSizeInBits()); |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1902 | if (bit_width == 16) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1903 | addCapability(spv::CapabilityFloat16); |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1904 | } else if (bit_width == 64) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1905 | addCapability(spv::CapabilityFloat64); |
| 1906 | } |
| 1907 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1908 | SPIRVOperandVec Ops; |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 1909 | Ops << bit_width; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1910 | |
| 1911 | RID = addSPIRVInst<kTypes>(spv::OpTypeFloat, Ops); |
| 1912 | break; |
| 1913 | } |
| 1914 | case Type::ArrayTyID: { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1915 | ArrayType *ArrTy = cast<ArrayType>(Canonical); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1916 | const uint64_t Length = ArrTy->getArrayNumElements(); |
| 1917 | if (Length == 0) { |
| 1918 | // By convention, map it to a RuntimeArray. |
| 1919 | |
| 1920 | Type *EleTy = ArrTy->getArrayElementType(); |
| 1921 | |
| 1922 | // |
| 1923 | // Generate OpTypeRuntimeArray. |
| 1924 | // |
| 1925 | // OpTypeRuntimeArray |
| 1926 | // Ops[0] = Element Type ID |
| 1927 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1928 | Ops << EleTy; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1929 | |
| 1930 | RID = addSPIRVInst<kTypes>(spv::OpTypeRuntimeArray, Ops); |
| 1931 | |
| 1932 | if (Hack_generate_runtime_array_stride_early) { |
| 1933 | // Generate OpDecorate. |
| 1934 | |
| 1935 | // Ops[0] = Target ID |
| 1936 | // Ops[1] = Decoration (ArrayStride) |
| 1937 | // Ops[2] = Stride Number(Literal Number) |
| 1938 | Ops.clear(); |
| 1939 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1940 | Ops << RID << spv::DecorationArrayStride |
| 1941 | << static_cast<uint32_t>(GetTypeAllocSize(EleTy, DL)); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1942 | |
| 1943 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 1944 | } |
| 1945 | |
| 1946 | } else { |
| 1947 | |
| 1948 | // |
| 1949 | // Generate OpConstant and OpTypeArray. |
| 1950 | // |
| 1951 | |
| 1952 | // |
| 1953 | // Generate OpConstant for array length. |
| 1954 | // |
| 1955 | // Add constant for length to constant list. |
| 1956 | Constant *CstLength = |
| 1957 | ConstantInt::get(Type::getInt32Ty(module->getContext()), Length); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1958 | |
| 1959 | // Remember to generate ArrayStride later |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1960 | getTypesNeedingArrayStride().insert(Canonical); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1961 | |
| 1962 | // |
| 1963 | // Generate OpTypeArray. |
| 1964 | // |
| 1965 | // Ops[0] = Element Type ID |
| 1966 | // Ops[1] = Array Length Constant ID |
| 1967 | SPIRVOperandVec Ops; |
| 1968 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 1969 | Ops << ArrTy->getElementType() << CstLength; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1970 | |
| 1971 | RID = addSPIRVInst<kTypes>(spv::OpTypeArray, Ops); |
| 1972 | } |
| 1973 | break; |
| 1974 | } |
| 1975 | case Type::FixedVectorTyID: { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1976 | auto VecTy = cast<VectorType>(Canonical); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1977 | // <4 x i8> is changed to i32 if i8 is not generally supported. |
| 1978 | if (!clspv::Option::Int8Support() && |
| 1979 | VecTy->getElementType() == Type::getInt8Ty(module->getContext())) { |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1980 | if (VecTy->getElementCount().getKnownMinValue() == 4) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1981 | RID = getSPIRVType(VecTy->getElementType()); |
| 1982 | break; |
| 1983 | } else { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 1984 | Canonical->print(errs()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1985 | llvm_unreachable("Support above i8 vector type"); |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | // Ops[0] = Component Type ID |
| 1990 | // Ops[1] = Component Count (Literal Number) |
| 1991 | SPIRVOperandVec Ops; |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 1992 | Ops << VecTy->getElementType() |
| 1993 | << VecTy->getElementCount().getKnownMinValue(); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 1994 | |
| 1995 | RID = addSPIRVInst<kTypes>(spv::OpTypeVector, Ops); |
| 1996 | break; |
| 1997 | } |
| 1998 | case Type::VoidTyID: { |
| 1999 | RID = addSPIRVInst<kTypes>(spv::OpTypeVoid); |
| 2000 | break; |
| 2001 | } |
| 2002 | case Type::FunctionTyID: { |
| 2003 | // Generate SPIRV instruction for function type. |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 2004 | FunctionType *FTy = cast<FunctionType>(Canonical); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2005 | |
| 2006 | // Ops[0] = Return Type ID |
| 2007 | // Ops[1] ... Ops[n] = Parameter Type IDs |
| 2008 | SPIRVOperandVec Ops; |
| 2009 | |
| 2010 | // Find SPIRV instruction for return type |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2011 | Ops << FTy->getReturnType(); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2012 | |
| 2013 | // Find SPIRV instructions for parameter types |
| 2014 | for (unsigned k = 0; k < FTy->getNumParams(); k++) { |
| 2015 | // Find SPIRV instruction for parameter type. |
| 2016 | auto ParamTy = FTy->getParamType(k); |
| 2017 | if (ParamTy->isPointerTy()) { |
| 2018 | auto PointeeTy = ParamTy->getPointerElementType(); |
| 2019 | if (PointeeTy->isStructTy() && |
| 2020 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 2021 | ParamTy = PointeeTy; |
| 2022 | } |
| 2023 | } |
| 2024 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2025 | Ops << ParamTy; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | RID = addSPIRVInst<kTypes>(spv::OpTypeFunction, Ops); |
| 2029 | break; |
| 2030 | } |
| 2031 | } |
| 2032 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2033 | if (RID.isValid()) { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 2034 | TypeMap[Canonical] = RID; |
| 2035 | if (Ty != Canonical) { |
| 2036 | // Speed up future lookups of this type by also caching the non-canonical |
| 2037 | // type. |
| 2038 | TypeMap[Ty] = RID; |
| 2039 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2040 | } |
| 2041 | return RID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2042 | } |
| 2043 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2044 | void SPIRVProducerPass::GenerateSPIRVTypes() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2045 | for (Type *Ty : getTypeList()) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2046 | getSPIRVType(Ty); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2047 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2048 | } |
| 2049 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2050 | SPIRVID SPIRVProducerPass::getSPIRVInt32Constant(uint32_t CstVal) { |
| 2051 | Type *i32 = Type::getInt32Ty(module->getContext()); |
| 2052 | Constant *Cst = ConstantInt::get(i32, CstVal); |
| 2053 | return getSPIRVValue(Cst); |
| 2054 | } |
| 2055 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2056 | SPIRVID SPIRVProducerPass::getSPIRVConstant(Constant *Cst) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2057 | ValueMapType &VMap = getValueMap(); |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 2058 | const bool hack_undef = clspv::Option::HackUndef(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2059 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2060 | SPIRVID RID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2061 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2062 | // |
| 2063 | // Generate OpConstant. |
| 2064 | // |
| 2065 | // Ops[0] = Result Type ID |
| 2066 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 2067 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2068 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2069 | Ops << Cst->getType(); |
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 | std::vector<uint32_t> LiteralNum; |
| 2072 | spv::Op Opcode = spv::OpNop; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2073 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2074 | if (isa<UndefValue>(Cst)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2075 | // Ops[0] = Result Type ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2076 | Opcode = spv::OpUndef; |
| 2077 | if (hack_undef && IsTypeNullable(Cst->getType())) { |
| 2078 | Opcode = spv::OpConstantNull; |
| 2079 | } |
| 2080 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Cst)) { |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 2081 | unsigned bit_width = CI->getBitWidth(); |
| 2082 | if (bit_width == 1) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2083 | // If the bitwidth of constant is 1, generate OpConstantTrue or |
| 2084 | // OpConstantFalse. |
| 2085 | if (CI->getZExtValue()) { |
| 2086 | // Ops[0] = Result Type ID |
| 2087 | Opcode = spv::OpConstantTrue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2088 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2089 | // Ops[0] = Result Type ID |
| 2090 | Opcode = spv::OpConstantFalse; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2091 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2092 | } else { |
| 2093 | auto V = CI->getZExtValue(); |
| 2094 | LiteralNum.push_back(V & 0xFFFFFFFF); |
| 2095 | |
alan-baker | e2a6275 | 2020-07-09 22:53:23 -0400 | [diff] [blame] | 2096 | if (bit_width > 32) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2097 | LiteralNum.push_back(V >> 32); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2101 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2102 | Ops << LiteralNum; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2103 | } |
| 2104 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Cst)) { |
| 2105 | uint64_t FPVal = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 2106 | Type *CFPTy = CFP->getType(); |
| 2107 | if (CFPTy->isFloatTy()) { |
| 2108 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
| 2109 | } else if (CFPTy->isDoubleTy()) { |
| 2110 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
| 2111 | LiteralNum.push_back(FPVal >> 32); |
| 2112 | } else if (CFPTy->isHalfTy()) { |
| 2113 | LiteralNum.push_back(FPVal & 0xFFFF); |
| 2114 | } else { |
| 2115 | CFPTy->print(errs()); |
| 2116 | llvm_unreachable("Implement this ConstantFP Type"); |
| 2117 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2118 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2119 | Opcode = spv::OpConstant; |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2120 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2121 | Ops << LiteralNum; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2122 | } else if (isa<ConstantDataSequential>(Cst) && |
| 2123 | cast<ConstantDataSequential>(Cst)->isString()) { |
| 2124 | Cst->print(errs()); |
| 2125 | llvm_unreachable("Implement this Constant"); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2126 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2127 | } else if (const ConstantDataSequential *CDS = |
| 2128 | dyn_cast<ConstantDataSequential>(Cst)) { |
| 2129 | // Let's convert <4 x i8> constant to int constant specially. |
| 2130 | // This case occurs when all the values are specified as constant |
| 2131 | // ints. |
| 2132 | Type *CstTy = Cst->getType(); |
| 2133 | if (is4xi8vec(CstTy)) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2134 | // |
| 2135 | // Generate OpConstant with OpTypeInt 32 0. |
| 2136 | // |
| 2137 | uint32_t IntValue = 0; |
| 2138 | for (unsigned k = 0; k < 4; k++) { |
| 2139 | const uint64_t Val = CDS->getElementAsInteger(k); |
| 2140 | IntValue = (IntValue << 8) | (Val & 0xffu); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2141 | } |
| 2142 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2143 | RID = getSPIRVInt32Constant(IntValue); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2144 | } else { |
| 2145 | |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2146 | // A normal constant-data-sequential case. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2147 | for (unsigned k = 0; k < CDS->getNumElements(); k++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2148 | Ops << CDS->getElementAsConstant(k); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | Opcode = spv::OpConstantComposite; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2152 | } |
| 2153 | } else if (const ConstantAggregate *CA = dyn_cast<ConstantAggregate>(Cst)) { |
| 2154 | // Let's convert <4 x i8> constant to int constant specially. |
| 2155 | // This case occurs when at least one of the values is an undef. |
| 2156 | Type *CstTy = Cst->getType(); |
| 2157 | if (is4xi8vec(CstTy)) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2158 | // |
| 2159 | // Generate OpConstant with OpTypeInt 32 0. |
| 2160 | // |
| 2161 | uint32_t IntValue = 0; |
| 2162 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); |
| 2163 | I != E; ++I) { |
| 2164 | uint64_t Val = 0; |
| 2165 | const Value *CV = *I; |
| 2166 | if (auto *CI2 = dyn_cast<ConstantInt>(CV)) { |
| 2167 | Val = CI2->getZExtValue(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2168 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2169 | IntValue = (IntValue << 8) | (Val & 0xffu); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2170 | } |
| 2171 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2172 | RID = getSPIRVInt32Constant(IntValue); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2173 | } else { |
| 2174 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2175 | // We use a constant composite in SPIR-V for our constant aggregate in |
| 2176 | // LLVM. |
| 2177 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2178 | |
| 2179 | for (unsigned k = 0; k < CA->getNumOperands(); k++) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2180 | // And add an operand to the composite we are constructing |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2181 | Ops << CA->getAggregateElement(k); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2182 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2183 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2184 | } else if (Cst->isNullValue()) { |
| 2185 | Opcode = spv::OpConstantNull; |
| 2186 | } else { |
| 2187 | Cst->print(errs()); |
| 2188 | llvm_unreachable("Unsupported Constant???"); |
| 2189 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2190 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2191 | if (Opcode == spv::OpConstantNull && Cst->getType()->isPointerTy()) { |
| 2192 | // Null pointer requires variable pointers. |
| 2193 | setVariablePointersCapabilities(Cst->getType()->getPointerAddressSpace()); |
| 2194 | } |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2195 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2196 | if (RID == 0) { |
| 2197 | RID = addSPIRVInst<kConstants>(Opcode, Ops); |
| 2198 | } |
| 2199 | |
| 2200 | VMap[Cst] = RID; |
| 2201 | |
| 2202 | return RID; |
| 2203 | } |
| 2204 | |
| 2205 | SPIRVID SPIRVProducerPass::getSPIRVValue(Value *V) { |
| 2206 | auto II = ValueMap.find(V); |
| 2207 | if (II != ValueMap.end()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2208 | assert(II->second.isValid()); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2209 | return II->second; |
| 2210 | } |
| 2211 | if (Constant *Cst = dyn_cast<Constant>(V)) { |
| 2212 | return getSPIRVConstant(Cst); |
| 2213 | } else { |
| 2214 | llvm_unreachable("Variable not found"); |
| 2215 | } |
| 2216 | } |
| 2217 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2218 | void SPIRVProducerPass::GenerateSamplers() { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2219 | auto &sampler_map = getSamplerMap(); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2220 | SamplerLiteralToIDMap.clear(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2221 | DenseMap<unsigned, unsigned> SamplerLiteralToDescriptorSetMap; |
| 2222 | DenseMap<unsigned, unsigned> SamplerLiteralToBindingMap; |
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 | // We might have samplers in the sampler map that are not used |
| 2225 | // in the translation unit. We need to allocate variables |
| 2226 | // for them and bindings too. |
| 2227 | DenseSet<unsigned> used_bindings; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2228 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2229 | auto *var_fn = module->getFunction(clspv::LiteralSamplerFunction()); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2230 | // Return if there are no literal samplers. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2231 | if (!var_fn) |
| 2232 | return; |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2233 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2234 | for (auto user : var_fn->users()) { |
| 2235 | // Populate SamplerLiteralToDescriptorSetMap and |
| 2236 | // SamplerLiteralToBindingMap. |
| 2237 | // |
| 2238 | // Look for calls like |
| 2239 | // call %opencl.sampler_t addrspace(2)* |
| 2240 | // @clspv.sampler.var.literal( |
| 2241 | // i32 descriptor, |
| 2242 | // i32 binding, |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2243 | // i32 (index-into-sampler-map|sampler_mask)) |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2244 | if (auto *call = dyn_cast<CallInst>(user)) { |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2245 | const auto third_param = static_cast<unsigned>( |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2246 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2247 | auto sampler_value = third_param; |
| 2248 | if (clspv::Option::UseSamplerMap()) { |
| 2249 | if (third_param >= sampler_map.size()) { |
| 2250 | errs() << "Out of bounds index to sampler map: " << third_param; |
| 2251 | llvm_unreachable("bad sampler init: out of bounds"); |
| 2252 | } |
| 2253 | sampler_value = sampler_map[third_param].first; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2254 | } |
| 2255 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2256 | const auto descriptor_set = static_cast<unsigned>( |
| 2257 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 2258 | const auto binding = static_cast<unsigned>( |
| 2259 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 2260 | |
| 2261 | SamplerLiteralToDescriptorSetMap[sampler_value] = descriptor_set; |
| 2262 | SamplerLiteralToBindingMap[sampler_value] = binding; |
| 2263 | used_bindings.insert(binding); |
| 2264 | } |
| 2265 | } |
| 2266 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2267 | DenseSet<size_t> seen; |
| 2268 | for (auto user : var_fn->users()) { |
| 2269 | if (!isa<CallInst>(user)) |
| 2270 | continue; |
| 2271 | |
| 2272 | auto call = cast<CallInst>(user); |
| 2273 | const unsigned third_param = static_cast<unsigned>( |
| 2274 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 2275 | |
| 2276 | // Already allocated a variable for this value. |
| 2277 | if (!seen.insert(third_param).second) |
| 2278 | continue; |
| 2279 | |
| 2280 | auto sampler_value = third_param; |
| 2281 | if (clspv::Option::UseSamplerMap()) { |
| 2282 | sampler_value = sampler_map[third_param].first; |
| 2283 | } |
| 2284 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2285 | auto sampler_var_id = addSPIRVGlobalVariable( |
| 2286 | getSPIRVType(SamplerTy), spv::StorageClassUniformConstant); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2287 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2288 | SamplerLiteralToIDMap[sampler_value] = sampler_var_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2289 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2290 | unsigned descriptor_set; |
| 2291 | unsigned binding; |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2292 | if (SamplerLiteralToBindingMap.find(sampler_value) == |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2293 | SamplerLiteralToBindingMap.end()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2294 | // This sampler is not actually used. Find the next one. |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 2295 | for (binding = 0; used_bindings.count(binding); binding++) { |
| 2296 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2297 | descriptor_set = 0; // Literal samplers always use descriptor set 0. |
| 2298 | used_bindings.insert(binding); |
| 2299 | } else { |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2300 | descriptor_set = SamplerLiteralToDescriptorSetMap[sampler_value]; |
| 2301 | binding = SamplerLiteralToBindingMap[sampler_value]; |
alan-baker | cff8015 | 2019-06-15 00:38:00 -0400 | [diff] [blame] | 2302 | |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 2303 | auto import_id = getReflectionImport(); |
| 2304 | SPIRVOperandVec Ops; |
| 2305 | Ops << getSPIRVType(Type::getVoidTy(module->getContext())) << import_id |
| 2306 | << reflection::ExtInstLiteralSampler |
| 2307 | << getSPIRVInt32Constant(descriptor_set) |
| 2308 | << getSPIRVInt32Constant(binding) |
| 2309 | << getSPIRVInt32Constant(sampler_value); |
| 2310 | addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2311 | } |
| 2312 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2313 | // Ops[0] = Target ID |
| 2314 | // Ops[1] = Decoration (DescriptorSet) |
| 2315 | // Ops[2] = LiteralNumber according to Decoration |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2316 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2317 | Ops << sampler_var_id << spv::DecorationDescriptorSet << descriptor_set; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2318 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2319 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2320 | |
| 2321 | // Ops[0] = Target ID |
| 2322 | // Ops[1] = Decoration (Binding) |
| 2323 | // Ops[2] = LiteralNumber according to Decoration |
| 2324 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2325 | Ops << sampler_var_id << spv::DecorationBinding << binding; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2326 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2327 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2328 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2329 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2330 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2331 | void SPIRVProducerPass::GenerateResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2332 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2333 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2334 | // Generate variables. Make one for each of resource var info object. |
| 2335 | for (auto *info : ModuleOrderedResourceVars) { |
| 2336 | Type *type = info->var_fn->getReturnType(); |
| 2337 | // Remap the address space for opaque types. |
| 2338 | switch (info->arg_kind) { |
| 2339 | case clspv::ArgKind::Sampler: |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 2340 | case clspv::ArgKind::SampledImage: |
| 2341 | case clspv::ArgKind::StorageImage: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2342 | type = PointerType::get(type->getPointerElementType(), |
| 2343 | clspv::AddressSpace::UniformConstant); |
| 2344 | break; |
| 2345 | default: |
| 2346 | break; |
| 2347 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2348 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2349 | const auto sc = GetStorageClassForArgKind(info->arg_kind); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2350 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2351 | info->var_id = addSPIRVGlobalVariable(getSPIRVType(type), sc); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2352 | |
| 2353 | // Map calls to the variable-builtin-function. |
| 2354 | for (auto &U : info->var_fn->uses()) { |
| 2355 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 2356 | const auto set = unsigned( |
| 2357 | dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue()); |
| 2358 | const auto binding = unsigned( |
| 2359 | dyn_cast<ConstantInt>(call->getOperand(1))->getZExtValue()); |
| 2360 | if (set == info->descriptor_set && binding == info->binding) { |
| 2361 | switch (info->arg_kind) { |
| 2362 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2363 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2364 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 2365 | case clspv::ArgKind::PodUBO: |
| 2366 | case clspv::ArgKind::PodPushConstant: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2367 | // The call maps to the variable directly. |
| 2368 | VMap[call] = info->var_id; |
| 2369 | break; |
| 2370 | case clspv::ArgKind::Sampler: |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 2371 | case clspv::ArgKind::SampledImage: |
| 2372 | case clspv::ArgKind::StorageImage: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2373 | // The call maps to a load we generate later. |
| 2374 | ResourceVarDeferredLoadCalls[call] = info->var_id; |
| 2375 | break; |
| 2376 | default: |
| 2377 | llvm_unreachable("Unhandled arg kind"); |
| 2378 | } |
| 2379 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2380 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2381 | } |
| 2382 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2383 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2384 | // Generate associated decorations. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2385 | SPIRVOperandVec Ops; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2386 | for (auto *info : ModuleOrderedResourceVars) { |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 2387 | // Push constants don't need descriptor set or binding decorations. |
| 2388 | if (info->arg_kind == clspv::ArgKind::PodPushConstant) |
| 2389 | continue; |
| 2390 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2391 | // Decorate with DescriptorSet and Binding. |
| 2392 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2393 | Ops << info->var_id << spv::DecorationDescriptorSet << info->descriptor_set; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2394 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2395 | |
| 2396 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2397 | Ops << info->var_id << spv::DecorationBinding << info->binding; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2398 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2399 | |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2400 | if (info->coherent) { |
| 2401 | // Decorate with Coherent if required for the variable. |
| 2402 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2403 | Ops << info->var_id << spv::DecorationCoherent; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2404 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2405 | } |
| 2406 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2407 | // Generate NonWritable and NonReadable |
| 2408 | switch (info->arg_kind) { |
| 2409 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2410 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2411 | if (info->var_fn->getReturnType()->getPointerAddressSpace() == |
| 2412 | clspv::AddressSpace::Constant) { |
| 2413 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2414 | Ops << info->var_id << spv::DecorationNonWritable; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2415 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2416 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2417 | break; |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 2418 | case clspv::ArgKind::StorageImage: { |
| 2419 | auto *type = info->var_fn->getReturnType(); |
| 2420 | auto *struct_ty = cast<StructType>(type->getPointerElementType()); |
| 2421 | // TODO(alan-baker): This is conservative. If compiling for OpenCL 2.0 or |
| 2422 | // above, the compiler treats all write_only images as read_write images. |
| 2423 | if (struct_ty->getName().contains("_wo_t")) { |
| 2424 | Ops.clear(); |
| 2425 | Ops << info->var_id << spv::DecorationNonReadable; |
| 2426 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 2427 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2428 | break; |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 2429 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2430 | default: |
| 2431 | break; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2437 | ValueMapType &VMap = getValueMap(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2438 | std::vector<SPIRVID> &BuiltinDimVec = getBuiltinDimVec(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2439 | const DataLayout &DL = GV.getParent()->getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2440 | |
| 2441 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 2442 | Type *Ty = GV.getType(); |
| 2443 | PointerType *PTy = cast<PointerType>(Ty); |
| 2444 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2445 | SPIRVID InitializerID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2446 | |
| 2447 | // Workgroup size is handled differently (it goes into a constant) |
| 2448 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2449 | uint32_t PrevXDimCst = 0xFFFFFFFF; |
| 2450 | uint32_t PrevYDimCst = 0xFFFFFFFF; |
| 2451 | uint32_t PrevZDimCst = 0xFFFFFFFF; |
alan-baker | 3b60977 | 2020-09-03 19:10:17 -0400 | [diff] [blame] | 2452 | bool HasMD = true; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2453 | for (Function &Func : *GV.getParent()) { |
| 2454 | if (Func.isDeclaration()) { |
| 2455 | continue; |
| 2456 | } |
| 2457 | |
| 2458 | // We only need to check kernels. |
| 2459 | if (Func.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 2460 | continue; |
| 2461 | } |
| 2462 | |
| 2463 | if (const MDNode *MD = |
| 2464 | dyn_cast<Function>(&Func)->getMetadata("reqd_work_group_size")) { |
| 2465 | uint32_t CurXDimCst = static_cast<uint32_t>( |
| 2466 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 2467 | uint32_t CurYDimCst = static_cast<uint32_t>( |
| 2468 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 2469 | uint32_t CurZDimCst = static_cast<uint32_t>( |
| 2470 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 2471 | |
| 2472 | if (PrevXDimCst == 0xFFFFFFFF && PrevYDimCst == 0xFFFFFFFF && |
| 2473 | PrevZDimCst == 0xFFFFFFFF) { |
| 2474 | PrevXDimCst = CurXDimCst; |
| 2475 | PrevYDimCst = CurYDimCst; |
| 2476 | PrevZDimCst = CurZDimCst; |
| 2477 | } else if (CurXDimCst != PrevXDimCst || CurYDimCst != PrevYDimCst || |
| 2478 | CurZDimCst != PrevZDimCst) { |
alan-baker | 3b60977 | 2020-09-03 19:10:17 -0400 | [diff] [blame] | 2479 | HasMD = false; |
| 2480 | continue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2481 | } else { |
| 2482 | continue; |
| 2483 | } |
| 2484 | |
| 2485 | // |
| 2486 | // Generate OpConstantComposite. |
| 2487 | // |
| 2488 | // Ops[0] : Result Type ID |
| 2489 | // Ops[1] : Constant size for x dimension. |
| 2490 | // Ops[2] : Constant size for y dimension. |
| 2491 | // Ops[3] : Constant size for z dimension. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2492 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2493 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2494 | SPIRVID XDimCstID = |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2495 | getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(0))); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2496 | SPIRVID YDimCstID = |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2497 | getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(1))); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2498 | SPIRVID ZDimCstID = |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2499 | getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(2))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2500 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2501 | Ops << Ty->getPointerElementType() << XDimCstID << YDimCstID |
| 2502 | << ZDimCstID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2503 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2504 | InitializerID = |
| 2505 | addSPIRVInst<kGlobalVariables>(spv::OpConstantComposite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2506 | } else { |
alan-baker | 3b60977 | 2020-09-03 19:10:17 -0400 | [diff] [blame] | 2507 | HasMD = false; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | // If all kernels do not have metadata for reqd_work_group_size, generate |
| 2512 | // OpSpecConstants for x/y/z dimension. |
Kévin Petit | 21c23c6 | 2020-04-29 01:38:28 +0100 | [diff] [blame] | 2513 | if (!HasMD || clspv::Option::NonUniformNDRangeSupported()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2514 | // |
| 2515 | // Generate OpSpecConstants for x/y/z dimension. |
| 2516 | // |
| 2517 | // Ops[0] : Result Type ID |
| 2518 | // Ops[1] : Constant size for x/y/z dimension (Literal Number). |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2519 | |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 2520 | // Allocate spec constants for workgroup size. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2521 | clspv::AddWorkgroupSpecConstants(module); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 2522 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2523 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2524 | SPIRVID result_type_id = getSPIRVType( |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2525 | dyn_cast<VectorType>(Ty->getPointerElementType())->getElementType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2526 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2527 | // X Dimension |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2528 | Ops << result_type_id << 1; |
| 2529 | SPIRVID XDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2530 | |
| 2531 | // Y Dimension |
| 2532 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2533 | Ops << result_type_id << 1; |
| 2534 | SPIRVID YDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2535 | |
| 2536 | // Z Dimension |
| 2537 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2538 | Ops << result_type_id << 1; |
| 2539 | SPIRVID ZDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2540 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2541 | BuiltinDimVec.push_back(XDimCstID); |
| 2542 | BuiltinDimVec.push_back(YDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2543 | BuiltinDimVec.push_back(ZDimCstID); |
| 2544 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2545 | // |
| 2546 | // Generate OpSpecConstantComposite. |
| 2547 | // |
| 2548 | // Ops[0] : Result Type ID |
| 2549 | // Ops[1] : Constant size for x dimension. |
| 2550 | // Ops[2] : Constant size for y dimension. |
| 2551 | // Ops[3] : Constant size for z dimension. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2552 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2553 | Ops << Ty->getPointerElementType() << XDimCstID << YDimCstID << ZDimCstID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2554 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2555 | InitializerID = |
| 2556 | addSPIRVInst<kConstants>(spv::OpSpecConstantComposite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2557 | } |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2558 | } else if (BuiltinType == spv::BuiltInWorkDim) { |
| 2559 | // 1. Generate a specialization constant with a default of 3. |
| 2560 | // 2. Allocate and annotate a SpecId for the constant. |
| 2561 | // 3. Use the spec constant as the initializer for the variable. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2562 | SPIRVOperandVec Ops; |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2563 | |
| 2564 | // |
| 2565 | // Generate OpSpecConstant. |
| 2566 | // |
| 2567 | // Ops[0] : Result Type ID |
| 2568 | // Ops[1] : Default literal value |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2569 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2570 | Ops << IntegerType::get(GV.getContext(), 32) << 3; |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2571 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2572 | InitializerID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2573 | |
| 2574 | // |
| 2575 | // Generate SpecId decoration. |
| 2576 | // |
| 2577 | // Ops[0] : target |
| 2578 | // Ops[1] : decoration |
| 2579 | // Ops[2] : SpecId |
Alan Baker | 75ccc25 | 2020-04-21 17:11:52 -0400 | [diff] [blame] | 2580 | auto spec_id = AllocateSpecConstant(module, SpecConstant::kWorkDim); |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2581 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2582 | Ops << InitializerID << spv::DecorationSpecId << spec_id; |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 2583 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2584 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2585 | } else if (BuiltinType == spv::BuiltInGlobalOffset) { |
| 2586 | // 1. Generate a spec constant with a default of {0, 0, 0}. |
| 2587 | // 2. Allocate and annotate SpecIds for the constants. |
| 2588 | // 3. Use the spec constant as the initializer for the variable. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2589 | SPIRVOperandVec Ops; |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2590 | |
| 2591 | // |
| 2592 | // Generate OpSpecConstant for each dimension. |
| 2593 | // |
| 2594 | // Ops[0] : Result Type ID |
| 2595 | // Ops[1] : Default literal value |
| 2596 | // |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2597 | Ops << IntegerType::get(GV.getContext(), 32) << 0; |
| 2598 | SPIRVID x_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2599 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2600 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2601 | Ops << IntegerType::get(GV.getContext(), 32) << 0; |
| 2602 | SPIRVID y_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2603 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2604 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2605 | Ops << IntegerType::get(GV.getContext(), 32) << 0; |
| 2606 | SPIRVID z_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2607 | |
| 2608 | // |
| 2609 | // Generate SpecId decoration for each dimension. |
| 2610 | // |
| 2611 | // Ops[0] : target |
| 2612 | // Ops[1] : decoration |
| 2613 | // Ops[2] : SpecId |
| 2614 | // |
| 2615 | auto spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetX); |
| 2616 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2617 | Ops << x_id << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2618 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2619 | |
| 2620 | spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetY); |
| 2621 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2622 | Ops << y_id << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2623 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2624 | |
| 2625 | spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetZ); |
| 2626 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2627 | Ops << z_id << spv::DecorationSpecId << spec_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2628 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2629 | |
| 2630 | // |
| 2631 | // Generate OpSpecConstantComposite. |
| 2632 | // |
| 2633 | // Ops[0] : type id |
| 2634 | // Ops[1..n-1] : elements |
| 2635 | // |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2636 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2637 | Ops << GV.getType()->getPointerElementType() << x_id << y_id << z_id; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2638 | InitializerID = addSPIRVInst<kConstants>(spv::OpSpecConstantComposite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2639 | } |
| 2640 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2641 | const auto AS = PTy->getAddressSpace(); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2642 | const auto spvSC = GetStorageClass(AS); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2643 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2644 | const bool module_scope_constant_external_init = |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2645 | (AS == AddressSpace::Constant) && GV.hasInitializer() && |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2646 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 2647 | |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 2648 | if (GV.hasInitializer()) { |
| 2649 | auto GVInit = GV.getInitializer(); |
| 2650 | if (!isa<UndefValue>(GVInit) && !module_scope_constant_external_init) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2651 | InitializerID = getSPIRVValue(GVInit); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2652 | } |
| 2653 | } |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 2654 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2655 | SPIRVID var_id = |
| 2656 | addSPIRVGlobalVariable(getSPIRVType(Ty), spvSC, InitializerID); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2657 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2658 | VMap[&GV] = var_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2659 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2660 | auto IsOpenCLBuiltin = [](spv::BuiltIn builtin) { |
| 2661 | return builtin == spv::BuiltInWorkDim || |
| 2662 | builtin == spv::BuiltInGlobalOffset; |
| 2663 | }; |
| 2664 | |
alan-baker | e199697 | 2020-05-04 08:38:12 -0400 | [diff] [blame] | 2665 | // If we have a builtin (not an OpenCL builtin). |
| 2666 | if (spv::BuiltInMax != BuiltinType && !IsOpenCLBuiltin(BuiltinType)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2667 | // |
| 2668 | // Generate OpDecorate. |
| 2669 | // |
| 2670 | // DOps[0] = Target ID |
| 2671 | // DOps[1] = Decoration (Builtin) |
| 2672 | // DOps[2] = BuiltIn ID |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2673 | SPIRVID ResultID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2674 | |
| 2675 | // WorkgroupSize is different, we decorate the constant composite that has |
| 2676 | // its value, rather than the variable that we use to access the value. |
| 2677 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 2678 | ResultID = InitializerID; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 2679 | // Save both the value and variable IDs for later. |
| 2680 | WorkgroupSizeValueID = InitializerID; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2681 | WorkgroupSizeVarID = getSPIRVValue(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2682 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2683 | ResultID = getSPIRVValue(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2684 | } |
| 2685 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2686 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2687 | Ops << ResultID << spv::DecorationBuiltIn << BuiltinType; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2688 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2689 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2690 | } else if (module_scope_constant_external_init) { |
| 2691 | // This module scope constant is initialized from a storage buffer with data |
| 2692 | // provided by the host at binding 0 of the next descriptor set. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2693 | const uint32_t descriptor_set = TakeDescriptorIndex(module); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2694 | |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 2695 | // Emit the intializer as a reflection instruction. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2696 | // Use "kind,buffer" to indicate storage buffer. We might want to expand |
| 2697 | // that later to other types, like uniform buffer. |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 2698 | std::string hexbytes; |
| 2699 | llvm::raw_string_ostream str(hexbytes); |
| 2700 | clspv::ConstantEmitter(DL, str).Emit(GV.getInitializer()); |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 2701 | |
| 2702 | // Reflection instruction for constant data. |
| 2703 | SPIRVOperandVec Ops; |
| 2704 | auto data_id = addSPIRVInst<kDebug>(spv::OpString, str.str().c_str()); |
| 2705 | Ops << getSPIRVType(Type::getVoidTy(module->getContext())) |
| 2706 | << getReflectionImport() << reflection::ExtInstConstantDataStorageBuffer |
| 2707 | << getSPIRVInt32Constant(descriptor_set) << getSPIRVInt32Constant(0) |
| 2708 | << data_id; |
| 2709 | addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2710 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2711 | // OpDecorate %var DescriptorSet <descriptor_set> |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 2712 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2713 | Ops << var_id << spv::DecorationDescriptorSet << descriptor_set; |
| 2714 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2715 | |
| 2716 | // OpDecorate %var Binding <binding> |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2717 | Ops.clear(); |
| 2718 | Ops << var_id << spv::DecorationBinding << 0; |
| 2719 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2720 | } |
| 2721 | } |
| 2722 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2723 | void SPIRVProducerPass::GenerateFuncPrologue(Function &F) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2724 | ValueMapType &VMap = getValueMap(); |
| 2725 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2726 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 2727 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 2728 | |
| 2729 | FunctionType *FTy = F.getFunctionType(); |
| 2730 | |
| 2731 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2732 | // Generate OPFunction. |
| 2733 | // |
| 2734 | |
| 2735 | // FOps[0] : Result Type ID |
| 2736 | // FOps[1] : Function Control |
| 2737 | // FOps[2] : Function Type ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2738 | SPIRVOperandVec FOps; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2739 | |
| 2740 | // Find SPIRV instruction for return type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2741 | FOps << FTy->getReturnType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2742 | |
| 2743 | // Check function attributes for SPIRV Function Control. |
| 2744 | uint32_t FuncControl = spv::FunctionControlMaskNone; |
| 2745 | if (F.hasFnAttribute(Attribute::AlwaysInline)) { |
| 2746 | FuncControl |= spv::FunctionControlInlineMask; |
| 2747 | } |
| 2748 | if (F.hasFnAttribute(Attribute::NoInline)) { |
| 2749 | FuncControl |= spv::FunctionControlDontInlineMask; |
| 2750 | } |
| 2751 | // TODO: Check llvm attribute for Function Control Pure. |
| 2752 | if (F.hasFnAttribute(Attribute::ReadOnly)) { |
| 2753 | FuncControl |= spv::FunctionControlPureMask; |
| 2754 | } |
| 2755 | // TODO: Check llvm attribute for Function Control Const. |
| 2756 | if (F.hasFnAttribute(Attribute::ReadNone)) { |
| 2757 | FuncControl |= spv::FunctionControlConstMask; |
| 2758 | } |
| 2759 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2760 | FOps << FuncControl; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2761 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2762 | SPIRVID FTyID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2763 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 2764 | SmallVector<Type *, 4> NewFuncParamTys; |
| 2765 | FunctionType *NewFTy = |
| 2766 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2767 | FTyID = getSPIRVType(NewFTy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2768 | } else { |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 2769 | // Handle regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2770 | if (GlobalConstFuncTyMap.count(FTy)) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2771 | FTyID = getSPIRVType(GlobalConstFuncTyMap[FTy].first); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2772 | } else { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2773 | FTyID = getSPIRVType(FTy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2777 | FOps << FTyID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2778 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2779 | // Generate SPIRV instruction for function. |
| 2780 | SPIRVID FID = addSPIRVInst(spv::OpFunction, FOps); |
| 2781 | VMap[&F] = FID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2782 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2783 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 2784 | EntryPoints.push_back(std::make_pair(&F, FID)); |
| 2785 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2786 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 2787 | if (clspv::Option::ShowIDs()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2788 | errs() << "Function " << F.getName() << " is " << FID.get() << "\n"; |
David Neto | b05675d | 2018-02-16 12:37:49 -0500 | [diff] [blame] | 2789 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2790 | |
| 2791 | // |
| 2792 | // Generate OpFunctionParameter for Normal function. |
| 2793 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2794 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2795 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2796 | // Iterate Argument for name instead of param type from function type. |
| 2797 | unsigned ArgIdx = 0; |
| 2798 | for (Argument &Arg : F.args()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2799 | // ParamOps[0] : Result Type ID |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2800 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2801 | |
| 2802 | // Find SPIRV instruction for parameter type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2803 | SPIRVID ParamTyID = getSPIRVType(Arg.getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2804 | if (PointerType *PTy = dyn_cast<PointerType>(Arg.getType())) { |
| 2805 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 2806 | if (ArgIdx == GlobalConstFuncTyMap[FTy].second) { |
| 2807 | Type *EleTy = PTy->getPointerElementType(); |
| 2808 | Type *ArgTy = |
| 2809 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2810 | ParamTyID = getSPIRVType(ArgTy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2811 | GlobalConstArgSet.insert(&Arg); |
| 2812 | } |
| 2813 | } |
| 2814 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2815 | Ops << ParamTyID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2816 | |
| 2817 | // Generate SPIRV instruction for parameter. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2818 | SPIRVID param_id = addSPIRVInst(spv::OpFunctionParameter, Ops); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2819 | VMap[&Arg] = param_id; |
| 2820 | |
| 2821 | if (CalledWithCoherentResource(Arg)) { |
| 2822 | // If the arg is passed a coherent resource ever, then decorate this |
| 2823 | // parameter with Coherent too. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2824 | Ops.clear(); |
| 2825 | Ops << param_id << spv::DecorationCoherent; |
| 2826 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2827 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2828 | |
| 2829 | ArgIdx++; |
| 2830 | } |
| 2831 | } |
| 2832 | } |
| 2833 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2834 | void SPIRVProducerPass::GenerateModuleInfo() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2835 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2836 | auto &EntryPointInterfaces = getEntryPointInterfacesList(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2837 | std::vector<SPIRVID> &BuiltinDimVec = getBuiltinDimVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2838 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2839 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2840 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2841 | for (auto Capability : CapabilitySet) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2842 | // |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2843 | // Generate OpCapability |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2844 | // |
| 2845 | // Ops[0] = Capability |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2846 | addSPIRVInst<kCapabilities>(spv::OpCapability, Capability); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | // Always add the storage buffer extension |
| 2850 | { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2851 | // |
| 2852 | // Generate OpExtension. |
| 2853 | // |
| 2854 | // Ops[0] = Name (Literal String) |
| 2855 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2856 | addSPIRVInst<kExtensions>(spv::OpExtension, |
| 2857 | "SPV_KHR_storage_buffer_storage_class"); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2858 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2859 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2860 | if (hasVariablePointers() || hasVariablePointersStorageBuffer()) { |
| 2861 | // |
| 2862 | // Generate OpExtension. |
| 2863 | // |
| 2864 | // Ops[0] = Name (Literal String) |
| 2865 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2866 | addSPIRVInst<kExtensions>(spv::OpExtension, "SPV_KHR_variable_pointers"); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | // |
| 2870 | // Generate OpMemoryModel |
| 2871 | // |
| 2872 | // Memory model for Vulkan will always be GLSL450. |
| 2873 | |
| 2874 | // Ops[0] = Addressing Model |
| 2875 | // Ops[1] = Memory Model |
| 2876 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2877 | Ops << spv::AddressingModelLogical << spv::MemoryModelGLSL450; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2878 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2879 | addSPIRVInst<kMemoryModel>(spv::OpMemoryModel, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2880 | |
| 2881 | // |
| 2882 | // Generate OpEntryPoint |
| 2883 | // |
| 2884 | for (auto EntryPoint : EntryPoints) { |
| 2885 | // Ops[0] = Execution Model |
| 2886 | // Ops[1] = EntryPoint ID |
| 2887 | // Ops[2] = Name (Literal String) |
| 2888 | // ... |
| 2889 | // |
| 2890 | // TODO: Do we need to consider Interface ID for forward references??? |
| 2891 | Ops.clear(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2892 | const StringRef &name = EntryPoint.first->getName(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2893 | Ops << spv::ExecutionModelGLCompute << EntryPoint.second << name; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2894 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 2895 | for (auto &Interface : EntryPointInterfaces) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2896 | Ops << Interface; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2897 | } |
| 2898 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2899 | addSPIRVInst<kEntryPoints>(spv::OpEntryPoint, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2900 | } |
| 2901 | |
alan-baker | 3b60977 | 2020-09-03 19:10:17 -0400 | [diff] [blame] | 2902 | if (BuiltinDimVec.empty()) { |
| 2903 | for (auto EntryPoint : EntryPoints) { |
| 2904 | const MDNode *MD = dyn_cast<Function>(EntryPoint.first) |
| 2905 | ->getMetadata("reqd_work_group_size"); |
| 2906 | if ((MD != nullptr) && !clspv::Option::NonUniformNDRangeSupported()) { |
| 2907 | // |
| 2908 | // Generate OpExecutionMode |
| 2909 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2910 | |
alan-baker | 3b60977 | 2020-09-03 19:10:17 -0400 | [diff] [blame] | 2911 | // Ops[0] = Entry Point ID |
| 2912 | // Ops[1] = Execution Mode |
| 2913 | // Ops[2] ... Ops[n] = Optional literals according to Execution Mode |
| 2914 | Ops.clear(); |
| 2915 | Ops << EntryPoint.second << spv::ExecutionModeLocalSize; |
| 2916 | |
| 2917 | uint32_t XDim = static_cast<uint32_t>( |
| 2918 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 2919 | uint32_t YDim = static_cast<uint32_t>( |
| 2920 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 2921 | uint32_t ZDim = static_cast<uint32_t>( |
| 2922 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 2923 | |
| 2924 | Ops << XDim << YDim << ZDim; |
| 2925 | |
| 2926 | addSPIRVInst<kExecutionModes>(spv::OpExecutionMode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2927 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | // |
| 2932 | // Generate OpSource. |
| 2933 | // |
| 2934 | // Ops[0] = SourceLanguage ID |
| 2935 | // Ops[1] = Version (LiteralNum) |
| 2936 | // |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2937 | uint32_t LangID = spv::SourceLanguageUnknown; |
| 2938 | uint32_t LangVer = 0; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2939 | switch (clspv::Option::Language()) { |
| 2940 | case clspv::Option::SourceLanguage::OpenCL_C_10: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2941 | LangID = spv::SourceLanguageOpenCL_C; |
| 2942 | LangVer = 100; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2943 | break; |
| 2944 | case clspv::Option::SourceLanguage::OpenCL_C_11: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2945 | LangID = spv::SourceLanguageOpenCL_C; |
| 2946 | LangVer = 110; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2947 | break; |
| 2948 | case clspv::Option::SourceLanguage::OpenCL_C_12: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2949 | LangID = spv::SourceLanguageOpenCL_C; |
| 2950 | LangVer = 120; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2951 | break; |
| 2952 | case clspv::Option::SourceLanguage::OpenCL_C_20: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2953 | LangID = spv::SourceLanguageOpenCL_C; |
| 2954 | LangVer = 200; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2955 | break; |
Kévin Petit | 77838ff | 2020-10-19 18:54:51 +0100 | [diff] [blame] | 2956 | case clspv::Option::SourceLanguage::OpenCL_C_30: |
| 2957 | LangID = spv::SourceLanguageOpenCL_C; |
| 2958 | LangVer = 300; |
| 2959 | break; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2960 | case clspv::Option::SourceLanguage::OpenCL_CPP: |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2961 | LangID = spv::SourceLanguageOpenCL_CPP; |
| 2962 | LangVer = 100; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2963 | break; |
| 2964 | default: |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 2965 | break; |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 2966 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2967 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2968 | Ops.clear(); |
| 2969 | Ops << LangID << LangVer; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2970 | addSPIRVInst<kDebug>(spv::OpSource, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2971 | |
| 2972 | if (!BuiltinDimVec.empty()) { |
| 2973 | // |
| 2974 | // Generate OpDecorates for x/y/z dimension. |
| 2975 | // |
| 2976 | // Ops[0] = Target ID |
| 2977 | // Ops[1] = Decoration (SpecId) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2978 | // Ops[2] = Specialization Constant ID (Literal Number) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2979 | |
| 2980 | // X Dimension |
| 2981 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2982 | Ops << BuiltinDimVec[0] << spv::DecorationSpecId << 0; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2983 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2984 | |
| 2985 | // Y Dimension |
| 2986 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2987 | Ops << BuiltinDimVec[1] << spv::DecorationSpecId << 1; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2988 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2989 | |
| 2990 | // Z Dimension |
| 2991 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 2992 | Ops << BuiltinDimVec[2] << spv::DecorationSpecId << 2; |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 2993 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 2997 | void SPIRVProducerPass::GenerateEntryPointInitialStores() { |
| 2998 | // Work around a driver bug. Initializers on Private variables might not |
| 2999 | // work. So the start of the kernel should store the initializer value to the |
| 3000 | // variables. Yes, *every* entry point pays this cost if *any* entry point |
| 3001 | // uses this builtin. At this point I judge this to be an acceptable tradeoff |
| 3002 | // of complexity vs. runtime, for a broken driver. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3003 | // TODO(dneto): Remove this at some point once fixed drivers are widely |
| 3004 | // available. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3005 | if (WorkgroupSizeVarID.isValid()) { |
| 3006 | assert(WorkgroupSizeValueID.isValid()); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3007 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3008 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3009 | Ops << WorkgroupSizeVarID << WorkgroupSizeValueID; |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3010 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3011 | addSPIRVInst(spv::OpStore, Ops); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3012 | } |
| 3013 | } |
| 3014 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3015 | void SPIRVProducerPass::GenerateFuncBody(Function &F) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3016 | ValueMapType &VMap = getValueMap(); |
| 3017 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3018 | const bool IsKernel = F.getCallingConv() == CallingConv::SPIR_KERNEL; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3019 | |
| 3020 | for (BasicBlock &BB : F) { |
| 3021 | // Register BasicBlock to ValueMap. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3022 | |
| 3023 | // |
| 3024 | // Generate OpLabel for Basic Block. |
| 3025 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3026 | VMap[&BB] = addSPIRVInst(spv::OpLabel); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3027 | |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3028 | // OpVariable instructions must come first. |
| 3029 | for (Instruction &I : BB) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3030 | if (auto *alloca = dyn_cast<AllocaInst>(&I)) { |
| 3031 | // Allocating a pointer requires variable pointers. |
| 3032 | if (alloca->getAllocatedType()->isPointerTy()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3033 | setVariablePointersCapabilities( |
| 3034 | alloca->getAllocatedType()->getPointerAddressSpace()); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3035 | } |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3036 | GenerateInstruction(I); |
| 3037 | } |
| 3038 | } |
| 3039 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3040 | if (&BB == &F.getEntryBlock() && IsKernel) { |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3041 | if (clspv::Option::HackInitializers()) { |
| 3042 | GenerateEntryPointInitialStores(); |
| 3043 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | for (Instruction &I : BB) { |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3047 | if (!isa<AllocaInst>(I)) { |
| 3048 | GenerateInstruction(I); |
| 3049 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3050 | } |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | spv::Op SPIRVProducerPass::GetSPIRVCmpOpcode(CmpInst *I) { |
| 3055 | const std::map<CmpInst::Predicate, spv::Op> Map = { |
| 3056 | {CmpInst::ICMP_EQ, spv::OpIEqual}, |
| 3057 | {CmpInst::ICMP_NE, spv::OpINotEqual}, |
| 3058 | {CmpInst::ICMP_UGT, spv::OpUGreaterThan}, |
| 3059 | {CmpInst::ICMP_UGE, spv::OpUGreaterThanEqual}, |
| 3060 | {CmpInst::ICMP_ULT, spv::OpULessThan}, |
| 3061 | {CmpInst::ICMP_ULE, spv::OpULessThanEqual}, |
| 3062 | {CmpInst::ICMP_SGT, spv::OpSGreaterThan}, |
| 3063 | {CmpInst::ICMP_SGE, spv::OpSGreaterThanEqual}, |
| 3064 | {CmpInst::ICMP_SLT, spv::OpSLessThan}, |
| 3065 | {CmpInst::ICMP_SLE, spv::OpSLessThanEqual}, |
| 3066 | {CmpInst::FCMP_OEQ, spv::OpFOrdEqual}, |
| 3067 | {CmpInst::FCMP_OGT, spv::OpFOrdGreaterThan}, |
| 3068 | {CmpInst::FCMP_OGE, spv::OpFOrdGreaterThanEqual}, |
| 3069 | {CmpInst::FCMP_OLT, spv::OpFOrdLessThan}, |
| 3070 | {CmpInst::FCMP_OLE, spv::OpFOrdLessThanEqual}, |
| 3071 | {CmpInst::FCMP_ONE, spv::OpFOrdNotEqual}, |
| 3072 | {CmpInst::FCMP_UEQ, spv::OpFUnordEqual}, |
| 3073 | {CmpInst::FCMP_UGT, spv::OpFUnordGreaterThan}, |
| 3074 | {CmpInst::FCMP_UGE, spv::OpFUnordGreaterThanEqual}, |
| 3075 | {CmpInst::FCMP_ULT, spv::OpFUnordLessThan}, |
| 3076 | {CmpInst::FCMP_ULE, spv::OpFUnordLessThanEqual}, |
| 3077 | {CmpInst::FCMP_UNE, spv::OpFUnordNotEqual}}; |
| 3078 | |
| 3079 | assert(0 != Map.count(I->getPredicate())); |
| 3080 | |
| 3081 | return Map.at(I->getPredicate()); |
| 3082 | } |
| 3083 | |
| 3084 | spv::Op SPIRVProducerPass::GetSPIRVCastOpcode(Instruction &I) { |
| 3085 | const std::map<unsigned, spv::Op> Map{ |
| 3086 | {Instruction::Trunc, spv::OpUConvert}, |
| 3087 | {Instruction::ZExt, spv::OpUConvert}, |
| 3088 | {Instruction::SExt, spv::OpSConvert}, |
| 3089 | {Instruction::FPToUI, spv::OpConvertFToU}, |
| 3090 | {Instruction::FPToSI, spv::OpConvertFToS}, |
| 3091 | {Instruction::UIToFP, spv::OpConvertUToF}, |
| 3092 | {Instruction::SIToFP, spv::OpConvertSToF}, |
| 3093 | {Instruction::FPTrunc, spv::OpFConvert}, |
| 3094 | {Instruction::FPExt, spv::OpFConvert}, |
| 3095 | {Instruction::BitCast, spv::OpBitcast}}; |
| 3096 | |
| 3097 | assert(0 != Map.count(I.getOpcode())); |
| 3098 | |
| 3099 | return Map.at(I.getOpcode()); |
| 3100 | } |
| 3101 | |
| 3102 | spv::Op SPIRVProducerPass::GetSPIRVBinaryOpcode(Instruction &I) { |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 3103 | if (I.getType()->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3104 | switch (I.getOpcode()) { |
| 3105 | default: |
| 3106 | break; |
| 3107 | case Instruction::Or: |
| 3108 | return spv::OpLogicalOr; |
| 3109 | case Instruction::And: |
| 3110 | return spv::OpLogicalAnd; |
| 3111 | case Instruction::Xor: |
| 3112 | return spv::OpLogicalNotEqual; |
| 3113 | } |
| 3114 | } |
| 3115 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3116 | const std::map<unsigned, spv::Op> Map{ |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3117 | {Instruction::Add, spv::OpIAdd}, |
| 3118 | {Instruction::FAdd, spv::OpFAdd}, |
| 3119 | {Instruction::Sub, spv::OpISub}, |
| 3120 | {Instruction::FSub, spv::OpFSub}, |
| 3121 | {Instruction::Mul, spv::OpIMul}, |
| 3122 | {Instruction::FMul, spv::OpFMul}, |
| 3123 | {Instruction::UDiv, spv::OpUDiv}, |
| 3124 | {Instruction::SDiv, spv::OpSDiv}, |
| 3125 | {Instruction::FDiv, spv::OpFDiv}, |
| 3126 | {Instruction::URem, spv::OpUMod}, |
| 3127 | {Instruction::SRem, spv::OpSRem}, |
| 3128 | {Instruction::FRem, spv::OpFRem}, |
| 3129 | {Instruction::Or, spv::OpBitwiseOr}, |
| 3130 | {Instruction::Xor, spv::OpBitwiseXor}, |
| 3131 | {Instruction::And, spv::OpBitwiseAnd}, |
| 3132 | {Instruction::Shl, spv::OpShiftLeftLogical}, |
| 3133 | {Instruction::LShr, spv::OpShiftRightLogical}, |
| 3134 | {Instruction::AShr, spv::OpShiftRightArithmetic}}; |
| 3135 | |
| 3136 | assert(0 != Map.count(I.getOpcode())); |
| 3137 | |
| 3138 | return Map.at(I.getOpcode()); |
| 3139 | } |
| 3140 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3141 | SPIRVID SPIRVProducerPass::getSPIRVBuiltin(spv::BuiltIn BID, |
| 3142 | spv::Capability Cap) { |
| 3143 | SPIRVID RID; |
| 3144 | |
| 3145 | auto ii = BuiltinConstantMap.find(BID); |
| 3146 | |
| 3147 | if (ii != BuiltinConstantMap.end()) { |
| 3148 | return ii->second; |
| 3149 | } else { |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3150 | addCapability(Cap); |
| 3151 | |
| 3152 | Type *type = PointerType::get(IntegerType::get(module->getContext(), 32), |
| 3153 | AddressSpace::Input); |
| 3154 | |
| 3155 | RID = addSPIRVGlobalVariable(getSPIRVType(type), spv::StorageClassInput); |
| 3156 | |
| 3157 | BuiltinConstantMap[BID] = RID; |
| 3158 | |
| 3159 | // |
| 3160 | // Generate OpDecorate. |
| 3161 | // |
| 3162 | // Ops[0] : target |
| 3163 | // Ops[1] : decoration |
| 3164 | // Ops[2] : SpecId |
| 3165 | SPIRVOperandVec Ops; |
| 3166 | Ops << RID << spv::DecorationBuiltIn << static_cast<int>(BID); |
| 3167 | |
| 3168 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
| 3169 | } |
| 3170 | |
| 3171 | return RID; |
| 3172 | } |
| 3173 | |
| 3174 | SPIRVID |
| 3175 | SPIRVProducerPass::GenerateClspvInstruction(CallInst *Call, |
| 3176 | const FunctionInfo &FuncInfo) { |
| 3177 | SPIRVID RID; |
| 3178 | |
| 3179 | switch (FuncInfo.getType()) { |
| 3180 | case Builtins::kClspvCompositeConstruct: |
| 3181 | RID = addSPIRVPlaceholder(Call); |
| 3182 | break; |
| 3183 | case Builtins::kClspvResource: { |
| 3184 | if (ResourceVarDeferredLoadCalls.count(Call) && Call->hasNUsesOrMore(1)) { |
| 3185 | // Generate an OpLoad |
| 3186 | SPIRVOperandVec Ops; |
| 3187 | |
| 3188 | Ops << Call->getType()->getPointerElementType() |
| 3189 | << ResourceVarDeferredLoadCalls[Call]; |
| 3190 | |
| 3191 | RID = addSPIRVInst(spv::OpLoad, Ops); |
| 3192 | |
| 3193 | } else { |
| 3194 | // This maps to an OpVariable we've already generated. |
| 3195 | // No code is generated for the call. |
| 3196 | } |
| 3197 | break; |
| 3198 | } |
| 3199 | case Builtins::kClspvLocal: { |
| 3200 | // Don't codegen an instruction here, but instead map this call directly |
| 3201 | // to the workgroup variable id. |
| 3202 | int spec_id = static_cast<int>( |
| 3203 | cast<ConstantInt>(Call->getOperand(0))->getSExtValue()); |
| 3204 | const auto &info = LocalSpecIdInfoMap[spec_id]; |
| 3205 | RID = info.variable_id; |
| 3206 | break; |
| 3207 | } |
| 3208 | case Builtins::kClspvSamplerVarLiteral: { |
| 3209 | // Sampler initializers become a load of the corresponding sampler. |
| 3210 | // Map this to a load from the variable. |
| 3211 | const auto third_param = static_cast<unsigned>( |
| 3212 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue()); |
| 3213 | auto sampler_value = third_param; |
| 3214 | if (clspv::Option::UseSamplerMap()) { |
| 3215 | sampler_value = getSamplerMap()[third_param].first; |
| 3216 | } |
| 3217 | |
| 3218 | // Generate an OpLoad |
| 3219 | SPIRVOperandVec Ops; |
| 3220 | |
| 3221 | Ops << SamplerTy->getPointerElementType() |
| 3222 | << SamplerLiteralToIDMap[sampler_value]; |
| 3223 | |
| 3224 | RID = addSPIRVInst(spv::OpLoad, Ops); |
| 3225 | break; |
| 3226 | } |
| 3227 | case Builtins::kSpirvAtomicXor: { |
| 3228 | // Handle SPIR-V intrinsics |
| 3229 | SPIRVOperandVec Ops; |
| 3230 | |
| 3231 | if (!Call->getType()->isVoidTy()) { |
| 3232 | Ops << Call->getType(); |
| 3233 | } |
| 3234 | |
| 3235 | for (unsigned i = 0; i < Call->getNumArgOperands(); i++) { |
| 3236 | Ops << Call->getArgOperand(i); |
| 3237 | } |
| 3238 | |
| 3239 | RID = addSPIRVInst(spv::OpAtomicXor, Ops); |
| 3240 | break; |
| 3241 | } |
| 3242 | case Builtins::kSpirvOp: { |
| 3243 | // Handle SPIR-V intrinsics |
| 3244 | auto *arg0 = dyn_cast<ConstantInt>(Call->getArgOperand(0)); |
| 3245 | spv::Op opcode = static_cast<spv::Op>(arg0->getZExtValue()); |
| 3246 | if (opcode != spv::OpNop) { |
| 3247 | SPIRVOperandVec Ops; |
| 3248 | |
| 3249 | if (!Call->getType()->isVoidTy()) { |
| 3250 | Ops << Call->getType(); |
| 3251 | } |
| 3252 | |
| 3253 | for (unsigned i = 1; i < Call->getNumArgOperands(); i++) { |
| 3254 | Ops << Call->getArgOperand(i); |
| 3255 | } |
| 3256 | |
| 3257 | RID = addSPIRVInst(opcode, Ops); |
| 3258 | } |
| 3259 | break; |
| 3260 | } |
| 3261 | case Builtins::kSpirvCopyMemory: { |
| 3262 | // |
| 3263 | // Generate OpCopyMemory. |
| 3264 | // |
| 3265 | |
| 3266 | // Ops[0] = Dst ID |
| 3267 | // Ops[1] = Src ID |
| 3268 | // Ops[2] = Memory Access |
| 3269 | // Ops[3] = Alignment |
| 3270 | |
| 3271 | auto IsVolatile = |
| 3272 | dyn_cast<ConstantInt>(Call->getArgOperand(3))->getZExtValue() != 0; |
| 3273 | |
| 3274 | auto VolatileMemoryAccess = (IsVolatile) ? spv::MemoryAccessVolatileMask |
| 3275 | : spv::MemoryAccessMaskNone; |
| 3276 | |
| 3277 | auto MemoryAccess = VolatileMemoryAccess | spv::MemoryAccessAlignedMask; |
| 3278 | |
| 3279 | auto Alignment = |
| 3280 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue(); |
| 3281 | |
| 3282 | SPIRVOperandVec Ops; |
| 3283 | Ops << Call->getArgOperand(0) << Call->getArgOperand(1) << MemoryAccess |
| 3284 | << static_cast<uint32_t>(Alignment); |
| 3285 | |
| 3286 | RID = addSPIRVInst(spv::OpCopyMemory, Ops); |
| 3287 | break; |
| 3288 | } |
| 3289 | default: |
| 3290 | llvm_unreachable("Unknown CLSPV Instruction"); |
| 3291 | break; |
| 3292 | } |
| 3293 | return RID; |
| 3294 | } |
| 3295 | |
| 3296 | SPIRVID |
| 3297 | SPIRVProducerPass::GenerateImageInstruction(CallInst *Call, |
| 3298 | const FunctionInfo &FuncInfo) { |
| 3299 | SPIRVID RID; |
| 3300 | |
| 3301 | LLVMContext &Context = module->getContext(); |
| 3302 | switch (FuncInfo.getType()) { |
| 3303 | case Builtins::kReadImagef: |
| 3304 | case Builtins::kReadImageh: |
| 3305 | case Builtins::kReadImagei: |
| 3306 | case Builtins::kReadImageui: { |
| 3307 | // read_image is converted to OpSampledImage and OpImageSampleExplicitLod. |
| 3308 | // Additionally, OpTypeSampledImage is generated. |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 3309 | const auto image_ty = Call->getArgOperand(0)->getType(); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3310 | const auto &pi = FuncInfo.getParameter(1); |
| 3311 | if (pi.isSampler()) { |
| 3312 | // |
| 3313 | // Generate OpSampledImage. |
| 3314 | // |
| 3315 | // Ops[0] = Result Type ID |
| 3316 | // Ops[1] = Image ID |
| 3317 | // Ops[2] = Sampler ID |
| 3318 | // |
| 3319 | SPIRVOperandVec Ops; |
| 3320 | |
| 3321 | Value *Image = Call->getArgOperand(0); |
| 3322 | Value *Sampler = Call->getArgOperand(1); |
| 3323 | Value *Coordinate = Call->getArgOperand(2); |
| 3324 | |
| 3325 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 3326 | Type *ImageTy = Image->getType()->getPointerElementType(); |
| 3327 | SPIRVID ImageTyID = OpImageTypeMap[ImageTy]; |
| 3328 | |
| 3329 | Ops << ImageTyID << Image << Sampler; |
| 3330 | |
| 3331 | SPIRVID SampledImageID = addSPIRVInst(spv::OpSampledImage, Ops); |
| 3332 | |
| 3333 | // |
| 3334 | // Generate OpImageSampleExplicitLod. |
| 3335 | // |
| 3336 | // Ops[0] = Result Type ID |
| 3337 | // Ops[1] = Sampled Image ID |
| 3338 | // Ops[2] = Coordinate ID |
| 3339 | // Ops[3] = Image Operands Type ID |
| 3340 | // Ops[4] ... Ops[n] = Operands ID |
| 3341 | // |
| 3342 | Ops.clear(); |
| 3343 | |
| 3344 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 3345 | SPIRVID result_type; |
| 3346 | if (is_int_image) { |
| 3347 | result_type = v4int32ID; |
| 3348 | } else { |
| 3349 | result_type = getSPIRVType(Call->getType()); |
| 3350 | } |
| 3351 | |
| 3352 | Constant *CstFP0 = ConstantFP::get(Context, APFloat(0.0f)); |
| 3353 | Ops << result_type << SampledImageID << Coordinate |
| 3354 | << spv::ImageOperandsLodMask << CstFP0; |
| 3355 | |
| 3356 | RID = addSPIRVInst(spv::OpImageSampleExplicitLod, Ops); |
| 3357 | |
| 3358 | if (is_int_image) { |
| 3359 | // Generate the bitcast. |
| 3360 | Ops.clear(); |
| 3361 | Ops << Call->getType() << RID; |
| 3362 | RID = addSPIRVInst(spv::OpBitcast, Ops); |
| 3363 | } |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 3364 | } else if (IsStorageImageType(image_ty)) { |
| 3365 | // read_image on a storage image is mapped to OpImageRead. |
| 3366 | Value *Image = Call->getArgOperand(0); |
| 3367 | Value *Coordinate = Call->getArgOperand(1); |
| 3368 | |
| 3369 | // |
| 3370 | // Generate OpImageRead |
| 3371 | // |
| 3372 | // Ops[0] = Result Type ID |
| 3373 | // Ops[1] = Image ID |
| 3374 | // Ops[2] = Coordinate |
| 3375 | // No optional image operands. |
| 3376 | // |
| 3377 | SPIRVOperandVec Ops; |
| 3378 | |
| 3379 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 3380 | SPIRVID result_type; |
| 3381 | if (is_int_image) { |
| 3382 | result_type = v4int32ID; |
| 3383 | } else { |
| 3384 | result_type = getSPIRVType(Call->getType()); |
| 3385 | } |
| 3386 | |
| 3387 | Ops << result_type << Image << Coordinate; |
| 3388 | RID = addSPIRVInst(spv::OpImageRead, Ops); |
| 3389 | |
| 3390 | if (is_int_image) { |
| 3391 | // Generate the bitcast. |
| 3392 | Ops.clear(); |
| 3393 | Ops << Call->getType() << RID; |
| 3394 | RID = addSPIRVInst(spv::OpBitcast, Ops); |
| 3395 | } |
| 3396 | |
| 3397 | // OpImageRead requires StorageImageReadWithoutFormat. |
| 3398 | addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3399 | } else { |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 3400 | // read_image on a sampled image (without a sampler) is mapped to |
| 3401 | // OpImageFetch. |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3402 | Value *Image = Call->getArgOperand(0); |
| 3403 | Value *Coordinate = Call->getArgOperand(1); |
| 3404 | |
| 3405 | // |
| 3406 | // Generate OpImageFetch |
| 3407 | // |
| 3408 | // Ops[0] = Result Type ID |
| 3409 | // Ops[1] = Image ID |
| 3410 | // Ops[2] = Coordinate ID |
| 3411 | // Ops[3] = Lod |
| 3412 | // Ops[4] = 0 |
| 3413 | // |
| 3414 | SPIRVOperandVec Ops; |
| 3415 | |
| 3416 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 3417 | SPIRVID result_type; |
| 3418 | if (is_int_image) { |
| 3419 | result_type = v4int32ID; |
| 3420 | } else { |
| 3421 | result_type = getSPIRVType(Call->getType()); |
| 3422 | } |
| 3423 | |
| 3424 | Ops << result_type << Image << Coordinate << spv::ImageOperandsLodMask |
| 3425 | << getSPIRVInt32Constant(0); |
| 3426 | |
| 3427 | RID = addSPIRVInst(spv::OpImageFetch, Ops); |
| 3428 | |
| 3429 | if (is_int_image) { |
| 3430 | // Generate the bitcast. |
| 3431 | Ops.clear(); |
| 3432 | Ops << Call->getType() << RID; |
| 3433 | RID = addSPIRVInst(spv::OpBitcast, Ops); |
| 3434 | } |
| 3435 | } |
| 3436 | break; |
| 3437 | } |
| 3438 | |
| 3439 | case Builtins::kWriteImagef: |
| 3440 | case Builtins::kWriteImageh: |
| 3441 | case Builtins::kWriteImagei: |
| 3442 | case Builtins::kWriteImageui: { |
| 3443 | // write_image is mapped to OpImageWrite. |
| 3444 | // |
| 3445 | // Generate OpImageWrite. |
| 3446 | // |
| 3447 | // Ops[0] = Image ID |
| 3448 | // Ops[1] = Coordinate ID |
| 3449 | // Ops[2] = Texel ID |
| 3450 | // Ops[3] = (Optional) Image Operands Type (Literal Number) |
| 3451 | // Ops[4] ... Ops[n] = (Optional) Operands ID |
| 3452 | // |
| 3453 | SPIRVOperandVec Ops; |
| 3454 | |
| 3455 | Value *Image = Call->getArgOperand(0); |
| 3456 | Value *Coordinate = Call->getArgOperand(1); |
| 3457 | Value *Texel = Call->getArgOperand(2); |
| 3458 | |
| 3459 | SPIRVID TexelID = getSPIRVValue(Texel); |
| 3460 | |
| 3461 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 3462 | if (is_int_image) { |
| 3463 | // Generate a bitcast to v4int and use it as the texel value. |
| 3464 | Ops << v4int32ID << TexelID; |
| 3465 | TexelID = addSPIRVInst(spv::OpBitcast, Ops); |
| 3466 | Ops.clear(); |
| 3467 | } |
| 3468 | Ops << Image << Coordinate << TexelID; |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3469 | RID = addSPIRVInst(spv::OpImageWrite, Ops); |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 3470 | |
| 3471 | // Image writes require StorageImageWriteWithoutFormat. |
| 3472 | addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3473 | break; |
| 3474 | } |
| 3475 | |
| 3476 | case Builtins::kGetImageHeight: |
| 3477 | case Builtins::kGetImageWidth: |
| 3478 | case Builtins::kGetImageDepth: |
| 3479 | case Builtins::kGetImageDim: { |
| 3480 | // get_image_* is mapped to OpImageQuerySize or OpImageQuerySizeLod |
| 3481 | addCapability(spv::CapabilityImageQuery); |
| 3482 | |
| 3483 | // |
| 3484 | // Generate OpImageQuerySize[Lod] |
| 3485 | // |
| 3486 | // Ops[0] = Image ID |
| 3487 | // |
| 3488 | // Result type has components equal to the dimensionality of the image, |
| 3489 | // plus 1 if the image is arrayed. |
| 3490 | // |
| 3491 | // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0] |
| 3492 | SPIRVOperandVec Ops; |
| 3493 | |
| 3494 | // Implement: |
| 3495 | // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0] |
| 3496 | SPIRVID SizesTypeID; |
| 3497 | |
| 3498 | Value *Image = Call->getArgOperand(0); |
| 3499 | const uint32_t dim = ImageDimensionality(Image->getType()); |
| 3500 | const uint32_t components = |
| 3501 | dim + (IsArrayImageType(Image->getType()) ? 1 : 0); |
| 3502 | if (components == 1) { |
| 3503 | SizesTypeID = getSPIRVType(Type::getInt32Ty(Context)); |
| 3504 | } else { |
| 3505 | SizesTypeID = getSPIRVType( |
| 3506 | FixedVectorType::get(Type::getInt32Ty(Context), components)); |
| 3507 | } |
| 3508 | Ops << SizesTypeID << Image; |
| 3509 | spv::Op query_opcode = spv::OpImageQuerySize; |
| 3510 | if (IsSampledImageType(Image->getType())) { |
| 3511 | query_opcode = spv::OpImageQuerySizeLod; |
| 3512 | // Need explicit 0 for Lod operand. |
| 3513 | Ops << getSPIRVInt32Constant(0); |
| 3514 | } |
| 3515 | |
| 3516 | RID = addSPIRVInst(query_opcode, Ops); |
| 3517 | |
| 3518 | // May require an extra instruction to create the appropriate result of |
| 3519 | // the builtin function. |
| 3520 | if (FuncInfo.getType() == Builtins::kGetImageDim) { |
| 3521 | if (dim == 3) { |
| 3522 | // get_image_dim returns an int4 for 3D images. |
| 3523 | // |
| 3524 | |
| 3525 | // Implement: |
| 3526 | // %result = OpCompositeConstruct %uint4 %sizes %uint_0 |
| 3527 | Ops.clear(); |
| 3528 | Ops << FixedVectorType::get(Type::getInt32Ty(Context), 4) << RID |
| 3529 | << getSPIRVInt32Constant(0); |
| 3530 | |
| 3531 | RID = addSPIRVInst(spv::OpCompositeConstruct, Ops); |
| 3532 | } else if (dim != components) { |
| 3533 | // get_image_dim return an int2 regardless of the arrayedness of the |
| 3534 | // image. If the image is arrayed an element must be dropped from the |
| 3535 | // query result. |
| 3536 | // |
| 3537 | |
| 3538 | // Implement: |
| 3539 | // %result = OpVectorShuffle %uint2 %sizes %sizes 0 1 |
| 3540 | Ops.clear(); |
| 3541 | Ops << FixedVectorType::get(Type::getInt32Ty(Context), 2) << RID << RID |
| 3542 | << 0 << 1; |
| 3543 | |
| 3544 | RID = addSPIRVInst(spv::OpVectorShuffle, Ops); |
| 3545 | } |
| 3546 | } else if (components > 1) { |
| 3547 | // Implement: |
| 3548 | // %result = OpCompositeExtract %uint %sizes <component number> |
| 3549 | Ops.clear(); |
| 3550 | Ops << Call->getType() << RID; |
| 3551 | |
| 3552 | uint32_t component = 0; |
| 3553 | if (FuncInfo.getType() == Builtins::kGetImageHeight) |
| 3554 | component = 1; |
| 3555 | else if (FuncInfo.getType() == Builtins::kGetImageDepth) |
| 3556 | component = 2; |
| 3557 | Ops << component; |
| 3558 | |
| 3559 | RID = addSPIRVInst(spv::OpCompositeExtract, Ops); |
| 3560 | } |
| 3561 | break; |
| 3562 | } |
| 3563 | default: |
| 3564 | llvm_unreachable("Unsupported Image builtin"); |
| 3565 | } |
| 3566 | |
| 3567 | return RID; |
| 3568 | } |
| 3569 | |
| 3570 | SPIRVID |
| 3571 | SPIRVProducerPass::GenerateSubgroupInstruction(CallInst *Call, |
| 3572 | const FunctionInfo &FuncInfo) { |
| 3573 | SPIRVID RID; |
| 3574 | |
| 3575 | // requires SPIRV version 1.3 or greater |
| 3576 | if (SpvVersion() != SPIRVVersion::SPIRV_1_3) { |
| 3577 | // llvm_unreachable("SubGroups extension requires SPIRV 1.3 or greater"); |
| 3578 | // TODO(sjw): error out gracefully |
| 3579 | } |
| 3580 | |
| 3581 | auto loadBuiltin = [this, Call](spv::BuiltIn spvBI, |
| 3582 | spv::Capability spvCap = |
| 3583 | spv::CapabilityGroupNonUniform) { |
| 3584 | SPIRVOperandVec Ops; |
| 3585 | Ops << Call->getType() << this->getSPIRVBuiltin(spvBI, spvCap); |
| 3586 | |
| 3587 | return addSPIRVInst(spv::OpLoad, Ops); |
| 3588 | }; |
| 3589 | |
| 3590 | spv::Op op = spv::OpNop; |
| 3591 | switch (FuncInfo.getType()) { |
| 3592 | case Builtins::kGetSubGroupSize: |
| 3593 | return loadBuiltin(spv::BuiltInSubgroupSize); |
| 3594 | case Builtins::kGetNumSubGroups: |
| 3595 | return loadBuiltin(spv::BuiltInNumSubgroups); |
| 3596 | case Builtins::kGetSubGroupId: |
| 3597 | return loadBuiltin(spv::BuiltInSubgroupId); |
| 3598 | case Builtins::kGetSubGroupLocalId: |
| 3599 | return loadBuiltin(spv::BuiltInSubgroupLocalInvocationId); |
| 3600 | |
| 3601 | case Builtins::kSubGroupBroadcast: |
| 3602 | if (SpvVersion() < SPIRVVersion::SPIRV_1_5 && |
| 3603 | !dyn_cast<ConstantInt>(Call->getOperand(1))) { |
| 3604 | llvm_unreachable("sub_group_broadcast requires constant lane Id for " |
| 3605 | "SPIRV version < 1.5"); |
| 3606 | } |
| 3607 | addCapability(spv::CapabilityGroupNonUniformBallot); |
| 3608 | op = spv::OpGroupNonUniformBroadcast; |
| 3609 | break; |
| 3610 | |
| 3611 | case Builtins::kSubGroupAll: |
| 3612 | addCapability(spv::CapabilityGroupNonUniformVote); |
| 3613 | op = spv::OpGroupNonUniformAll; |
| 3614 | break; |
| 3615 | case Builtins::kSubGroupAny: |
| 3616 | addCapability(spv::CapabilityGroupNonUniformVote); |
| 3617 | op = spv::OpGroupNonUniformAny; |
| 3618 | break; |
| 3619 | case Builtins::kSubGroupReduceAdd: |
| 3620 | case Builtins::kSubGroupScanExclusiveAdd: |
| 3621 | case Builtins::kSubGroupScanInclusiveAdd: { |
| 3622 | addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 3623 | if (FuncInfo.getParameter(0).type_id == Type::IntegerTyID) { |
| 3624 | op = spv::OpGroupNonUniformIAdd; |
| 3625 | } else { |
| 3626 | op = spv::OpGroupNonUniformFAdd; |
| 3627 | } |
| 3628 | break; |
| 3629 | } |
| 3630 | case Builtins::kSubGroupReduceMin: |
| 3631 | case Builtins::kSubGroupScanExclusiveMin: |
| 3632 | case Builtins::kSubGroupScanInclusiveMin: { |
| 3633 | addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 3634 | auto ¶m = FuncInfo.getParameter(0); |
| 3635 | if (param.type_id == Type::IntegerTyID) { |
| 3636 | op = param.is_signed ? spv::OpGroupNonUniformSMin |
| 3637 | : spv::OpGroupNonUniformUMin; |
| 3638 | } else { |
| 3639 | op = spv::OpGroupNonUniformFMin; |
| 3640 | } |
| 3641 | break; |
| 3642 | } |
| 3643 | case Builtins::kSubGroupReduceMax: |
| 3644 | case Builtins::kSubGroupScanExclusiveMax: |
| 3645 | case Builtins::kSubGroupScanInclusiveMax: { |
| 3646 | addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 3647 | auto ¶m = FuncInfo.getParameter(0); |
| 3648 | if (param.type_id == Type::IntegerTyID) { |
| 3649 | op = param.is_signed ? spv::OpGroupNonUniformSMax |
| 3650 | : spv::OpGroupNonUniformUMax; |
| 3651 | } else { |
| 3652 | op = spv::OpGroupNonUniformFMax; |
| 3653 | } |
| 3654 | break; |
| 3655 | } |
| 3656 | |
| 3657 | case Builtins::kGetEnqueuedNumSubGroups: |
| 3658 | // TODO(sjw): requires CapabilityKernel (incompatible with Shader) |
| 3659 | case Builtins::kGetMaxSubGroupSize: |
| 3660 | // TODO(sjw): use SpecConstant, capability Kernel (incompatible with Shader) |
| 3661 | case Builtins::kSubGroupBarrier: |
| 3662 | case Builtins::kSubGroupReserveReadPipe: |
| 3663 | case Builtins::kSubGroupReserveWritePipe: |
| 3664 | case Builtins::kSubGroupCommitReadPipe: |
| 3665 | case Builtins::kSubGroupCommitWritePipe: |
| 3666 | case Builtins::kGetKernelSubGroupCountForNdrange: |
| 3667 | case Builtins::kGetKernelMaxSubGroupSizeForNdrange: |
| 3668 | default: |
| 3669 | Call->print(errs()); |
| 3670 | llvm_unreachable("Unsupported sub_group operation"); |
| 3671 | break; |
| 3672 | } |
| 3673 | |
| 3674 | assert(op != spv::OpNop); |
| 3675 | |
| 3676 | SPIRVOperandVec Operands; |
| 3677 | |
| 3678 | // |
| 3679 | // Generate OpGroupNonUniform* |
| 3680 | // |
| 3681 | // Ops[0] = Result Type ID |
| 3682 | // Ops[1] = ScopeSubgroup |
| 3683 | // Ops[2] = Value ID |
| 3684 | // Ops[3] = Local ID |
| 3685 | |
| 3686 | // The result type. |
| 3687 | Operands << Call->getType(); |
| 3688 | |
| 3689 | // Subgroup Scope |
| 3690 | Operands << getSPIRVInt32Constant(spv::ScopeSubgroup); |
| 3691 | |
| 3692 | switch (FuncInfo.getType()) { |
| 3693 | case Builtins::kSubGroupReduceAdd: |
| 3694 | case Builtins::kSubGroupReduceMin: |
| 3695 | case Builtins::kSubGroupReduceMax: |
| 3696 | Operands << spv::GroupOperationReduce; |
| 3697 | break; |
| 3698 | case Builtins::kSubGroupScanExclusiveAdd: |
| 3699 | case Builtins::kSubGroupScanExclusiveMin: |
| 3700 | case Builtins::kSubGroupScanExclusiveMax: |
| 3701 | Operands << spv::GroupOperationExclusiveScan; |
| 3702 | break; |
| 3703 | case Builtins::kSubGroupScanInclusiveAdd: |
| 3704 | case Builtins::kSubGroupScanInclusiveMin: |
| 3705 | case Builtins::kSubGroupScanInclusiveMax: |
| 3706 | Operands << spv::GroupOperationInclusiveScan; |
| 3707 | break; |
| 3708 | default: |
| 3709 | break; |
| 3710 | } |
| 3711 | |
| 3712 | for (Use &use : Call->arg_operands()) { |
| 3713 | Operands << use.get(); |
| 3714 | } |
| 3715 | |
| 3716 | return addSPIRVInst(op, Operands); |
| 3717 | } |
| 3718 | |
| 3719 | SPIRVID SPIRVProducerPass::GenerateInstructionFromCall(CallInst *Call) { |
| 3720 | LLVMContext &Context = module->getContext(); |
| 3721 | |
| 3722 | auto &func_info = Builtins::Lookup(Call->getCalledFunction()); |
| 3723 | auto func_type = func_info.getType(); |
| 3724 | |
| 3725 | if (BUILTIN_IN_GROUP(func_type, Clspv)) { |
| 3726 | return GenerateClspvInstruction(Call, func_info); |
| 3727 | } else if (BUILTIN_IN_GROUP(func_type, Image)) { |
| 3728 | return GenerateImageInstruction(Call, func_info); |
| 3729 | } else if (BUILTIN_IN_GROUP(func_type, SubgroupsKHR)) { |
| 3730 | return GenerateSubgroupInstruction(Call, func_info); |
| 3731 | } |
| 3732 | |
| 3733 | SPIRVID RID; |
| 3734 | |
| 3735 | switch (func_type) { |
| 3736 | case Builtins::kPopcount: { |
| 3737 | // |
| 3738 | // Generate OpBitCount |
| 3739 | // |
| 3740 | // Ops[0] = Result Type ID |
| 3741 | // Ops[1] = Base ID |
| 3742 | SPIRVOperandVec Ops; |
| 3743 | Ops << Call->getType() << Call->getOperand(0); |
| 3744 | |
| 3745 | RID = addSPIRVInst(spv::OpBitCount, Ops); |
| 3746 | break; |
| 3747 | } |
| 3748 | default: { |
| 3749 | glsl::ExtInst EInst = getDirectOrIndirectExtInstEnum(func_info); |
| 3750 | |
| 3751 | if (EInst) { |
| 3752 | SPIRVID ExtInstImportID = getOpExtInstImportID(); |
| 3753 | |
| 3754 | // |
| 3755 | // Generate OpExtInst. |
| 3756 | // |
| 3757 | |
| 3758 | // Ops[0] = Result Type ID |
| 3759 | // Ops[1] = Set ID (OpExtInstImport ID) |
| 3760 | // Ops[2] = Instruction Number (Literal Number) |
| 3761 | // Ops[3] ... Ops[n] = Operand 1, ... , Operand n |
| 3762 | SPIRVOperandVec Ops; |
| 3763 | |
| 3764 | Ops << Call->getType() << ExtInstImportID << EInst; |
| 3765 | |
| 3766 | for (auto &use : Call->arg_operands()) { |
| 3767 | Ops << use.get(); |
| 3768 | } |
| 3769 | |
| 3770 | RID = addSPIRVInst(spv::OpExtInst, Ops); |
| 3771 | |
| 3772 | const auto IndirectExtInst = getIndirectExtInstEnum(func_info); |
| 3773 | if (IndirectExtInst != kGlslExtInstBad) { |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3774 | // Generate one more instruction that uses the result of the extended |
| 3775 | // instruction. Its result id is one more than the id of the |
| 3776 | // extended instruction. |
| 3777 | auto generate_extra_inst = [this, &Context, &Call, |
| 3778 | &RID](spv::Op opcode, Constant *constant) { |
| 3779 | // |
| 3780 | // Generate instruction like: |
| 3781 | // result = opcode constant <extinst-result> |
| 3782 | // |
| 3783 | // Ops[0] = Result Type ID |
| 3784 | // Ops[1] = Operand 0 ;; the constant, suitably splatted |
| 3785 | // Ops[2] = Operand 1 ;; the result of the extended instruction |
| 3786 | SPIRVOperandVec Ops; |
| 3787 | |
| 3788 | Type *resultTy = Call->getType(); |
| 3789 | |
| 3790 | if (auto *vectorTy = dyn_cast<VectorType>(resultTy)) { |
alan-baker | 931253b | 2020-08-20 17:15:38 -0400 | [diff] [blame] | 3791 | constant = |
| 3792 | ConstantVector::getSplat(vectorTy->getElementCount(), constant); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3793 | } |
| 3794 | Ops << resultTy << constant << RID; |
| 3795 | |
| 3796 | RID = addSPIRVInst(opcode, Ops); |
| 3797 | }; |
| 3798 | |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 3799 | auto bitwidth = Call->getType()->getScalarSizeInBits(); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3800 | switch (IndirectExtInst) { |
| 3801 | case glsl::ExtInstFindUMsb: // Implementing clz |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 3802 | generate_extra_inst( |
| 3803 | spv::OpISub, |
| 3804 | ConstantInt::get(Call->getType()->getScalarType(), bitwidth - 1)); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3805 | break; |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 3806 | case glsl::ExtInstFindILsb: { // Implementing ctz |
| 3807 | auto neg_one = Constant::getAllOnesValue(Call->getType()); |
| 3808 | Constant *int_32 = |
| 3809 | ConstantInt::get(Call->getType()->getScalarType(), 32); |
| 3810 | Type *i1_ty = Type::getInt1Ty(Call->getContext()); |
| 3811 | if (auto vec_ty = dyn_cast<VectorType>(Call->getType())) { |
| 3812 | i1_ty = VectorType::get(i1_ty, vec_ty->getElementCount()); |
| 3813 | int_32 = |
| 3814 | ConstantVector::getSplat(vec_ty->getElementCount(), int_32); |
| 3815 | } |
| 3816 | |
| 3817 | SPIRVOperandVec local_ops; |
| 3818 | local_ops << i1_ty << RID << neg_one; |
| 3819 | auto cmp = addSPIRVInst(spv::OpIEqual, local_ops); |
| 3820 | local_ops.clear(); |
| 3821 | local_ops << Call->getType() << cmp << int_32 << RID; |
| 3822 | RID = addSPIRVInst(spv::OpSelect, local_ops); |
| 3823 | break; |
| 3824 | } |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3825 | case glsl::ExtInstAcos: // Implementing acospi |
| 3826 | case glsl::ExtInstAsin: // Implementing asinpi |
| 3827 | case glsl::ExtInstAtan: // Implementing atanpi |
| 3828 | case glsl::ExtInstAtan2: // Implementing atan2pi |
| 3829 | generate_extra_inst( |
| 3830 | spv::OpFMul, |
alan-baker | cc2bafb | 2020-11-02 08:30:18 -0500 | [diff] [blame] | 3831 | ConstantFP::get(Call->getType()->getScalarType(), kOneOverPi)); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3832 | break; |
| 3833 | |
| 3834 | default: |
| 3835 | assert(false && "internally inconsistent"); |
| 3836 | } |
| 3837 | } |
| 3838 | } else { |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3839 | // A real function call (not builtin) |
| 3840 | // Call instruction is deferred because it needs function's ID. |
| 3841 | RID = addSPIRVPlaceholder(Call); |
| 3842 | } |
| 3843 | |
| 3844 | break; |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | return RID; |
| 3849 | } |
| 3850 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3851 | void SPIRVProducerPass::GenerateInstruction(Instruction &I) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3852 | ValueMapType &VMap = getValueMap(); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3853 | LLVMContext &Context = module->getContext(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3854 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3855 | SPIRVID RID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3856 | |
| 3857 | switch (I.getOpcode()) { |
| 3858 | default: { |
| 3859 | if (Instruction::isCast(I.getOpcode())) { |
| 3860 | // |
| 3861 | // Generate SPIRV instructions for cast operators. |
| 3862 | // |
| 3863 | |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3864 | auto Ty = I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3865 | auto OpTy = I.getOperand(0)->getType(); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3866 | auto toI8 = Ty == Type::getInt8Ty(Context); |
| 3867 | auto fromI32 = OpTy == Type::getInt32Ty(Context); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3868 | // Handle zext, sext and uitofp with i1 type specially. |
| 3869 | if ((I.getOpcode() == Instruction::ZExt || |
| 3870 | I.getOpcode() == Instruction::SExt || |
| 3871 | I.getOpcode() == Instruction::UIToFP) && |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3872 | OpTy->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3873 | // |
| 3874 | // Generate OpSelect. |
| 3875 | // |
| 3876 | |
| 3877 | // Ops[0] = Result Type ID |
| 3878 | // Ops[1] = Condition ID |
| 3879 | // Ops[2] = True Constant ID |
| 3880 | // Ops[3] = False Constant ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3881 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3882 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3883 | Ops << I.getType() << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3884 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3885 | if (I.getOpcode() == Instruction::ZExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3886 | Ops << ConstantInt::get(I.getType(), 1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3887 | } else if (I.getOpcode() == Instruction::SExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3888 | Ops << ConstantInt::getSigned(I.getType(), -1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3889 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3890 | Ops << ConstantFP::get(Context, APFloat(1.0f)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3891 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3892 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3893 | if (I.getOpcode() == Instruction::ZExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3894 | Ops << Constant::getNullValue(I.getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3895 | } else if (I.getOpcode() == Instruction::SExt) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3896 | Ops << Constant::getNullValue(I.getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3897 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3898 | Ops << ConstantFP::get(Context, APFloat(0.0f)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3899 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3900 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3901 | RID = addSPIRVInst(spv::OpSelect, Ops); |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 3902 | } else if (!clspv::Option::Int8Support() && |
| 3903 | I.getOpcode() == Instruction::Trunc && fromI32 && toI8) { |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3904 | // The SPIR-V target type is a 32-bit int. Keep only the bottom |
| 3905 | // 8 bits. |
| 3906 | // Before: |
| 3907 | // %result = trunc i32 %a to i8 |
| 3908 | // After |
| 3909 | // %result = OpBitwiseAnd %uint %a %uint_255 |
| 3910 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3911 | SPIRVOperandVec Ops; |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3912 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 3913 | Ops << OpTy << I.getOperand(0) << getSPIRVInt32Constant(255); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 3914 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3915 | RID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3916 | } else { |
| 3917 | // Ops[0] = Result Type ID |
| 3918 | // Ops[1] = Source Value ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3919 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3920 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3921 | Ops << I.getType() << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3922 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3923 | RID = addSPIRVInst(GetSPIRVCastOpcode(I), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3924 | } |
| 3925 | } else if (isa<BinaryOperator>(I)) { |
| 3926 | // |
| 3927 | // Generate SPIRV instructions for binary operators. |
| 3928 | // |
| 3929 | |
| 3930 | // Handle xor with i1 type specially. |
| 3931 | if (I.getOpcode() == Instruction::Xor && |
| 3932 | I.getType() == Type::getInt1Ty(Context) && |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 3933 | ((isa<ConstantInt>(I.getOperand(0)) && |
| 3934 | !cast<ConstantInt>(I.getOperand(0))->isZero()) || |
| 3935 | (isa<ConstantInt>(I.getOperand(1)) && |
| 3936 | !cast<ConstantInt>(I.getOperand(1))->isZero()))) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3937 | // |
| 3938 | // Generate OpLogicalNot. |
| 3939 | // |
| 3940 | // Ops[0] = Result Type ID |
| 3941 | // Ops[1] = Operand |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3942 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3943 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3944 | Ops << I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3945 | |
| 3946 | Value *CondV = I.getOperand(0); |
| 3947 | if (isa<Constant>(I.getOperand(0))) { |
| 3948 | CondV = I.getOperand(1); |
| 3949 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3950 | Ops << CondV; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3951 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3952 | RID = addSPIRVInst(spv::OpLogicalNot, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3953 | } else { |
| 3954 | // Ops[0] = Result Type ID |
| 3955 | // Ops[1] = Operand 0 |
| 3956 | // Ops[2] = Operand 1 |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3957 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3958 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3959 | Ops << I.getType() << I.getOperand(0) << I.getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3960 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3961 | RID = addSPIRVInst(GetSPIRVBinaryOpcode(I), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3962 | } |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 3963 | } else if (I.getOpcode() == Instruction::FNeg) { |
| 3964 | // The only unary operator. |
| 3965 | // |
| 3966 | // Ops[0] = Result Type ID |
| 3967 | // Ops[1] = Operand 0 |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3968 | SPIRVOperandVec Ops; |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 3969 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 3970 | Ops << I.getType() << I.getOperand(0); |
| 3971 | RID = addSPIRVInst(spv::OpFNegate, Ops); |
Marco Antognini | 68e5c51 | 2020-09-09 16:08:57 +0100 | [diff] [blame] | 3972 | } else if (I.getOpcode() == Instruction::Unreachable) { |
| 3973 | RID = addSPIRVInst(spv::OpUnreachable); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3974 | } else { |
| 3975 | I.print(errs()); |
| 3976 | llvm_unreachable("Unsupported instruction???"); |
| 3977 | } |
| 3978 | break; |
| 3979 | } |
| 3980 | case Instruction::GetElementPtr: { |
| 3981 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 3982 | |
| 3983 | // |
| 3984 | // Generate OpAccessChain. |
| 3985 | // |
| 3986 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(&I); |
| 3987 | |
| 3988 | // |
| 3989 | // Generate OpAccessChain. |
| 3990 | // |
| 3991 | |
| 3992 | // Ops[0] = Result Type ID |
| 3993 | // Ops[1] = Base ID |
| 3994 | // Ops[2] ... Ops[n] = Indexes ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 3995 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3996 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3997 | PointerType *ResultType = cast<PointerType>(GEP->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3998 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate || |
| 3999 | GlobalConstArgSet.count(GEP->getPointerOperand())) { |
| 4000 | // Use pointer type with private address space for global constant. |
| 4001 | Type *EleTy = I.getType()->getPointerElementType(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4002 | ResultType = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4003 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4004 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4005 | Ops << ResultType; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4006 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4007 | // Generate the base pointer. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4008 | Ops << GEP->getPointerOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4009 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4010 | // TODO(dneto): Simplify the following? |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4011 | |
| 4012 | // |
| 4013 | // Follows below rules for gep. |
| 4014 | // |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4015 | // 1. If gep's first index is 0 generate OpAccessChain and ignore gep's |
| 4016 | // first index. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4017 | // 2. If gep's first index is not 0, generate OpPtrAccessChain and use gep's |
| 4018 | // first index. |
| 4019 | // 3. If gep's first index is not constant, generate OpPtrAccessChain and |
| 4020 | // use gep's first index. |
| 4021 | // 4. If it is not above case 1, 2 and 3, generate OpAccessChain and use |
| 4022 | // gep's first index. |
| 4023 | // |
| 4024 | spv::Op Opcode = spv::OpAccessChain; |
| 4025 | unsigned offset = 0; |
| 4026 | if (ConstantInt *CstInt = dyn_cast<ConstantInt>(GEP->getOperand(1))) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4027 | if (CstInt->getZExtValue() == 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4028 | offset = 1; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4029 | } else if (CstInt->getZExtValue() != 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4030 | Opcode = spv::OpPtrAccessChain; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4031 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4032 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4033 | Opcode = spv::OpPtrAccessChain; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | if (Opcode == spv::OpPtrAccessChain) { |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 4037 | // Shader validation in the SPIR-V spec requires that the base pointer to |
| 4038 | // OpPtrAccessChain (in StorageBuffer storage class) be decorated with |
| 4039 | // ArrayStride. |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4040 | auto address_space = ResultType->getAddressSpace(); |
| 4041 | setVariablePointersCapabilities(address_space); |
| 4042 | switch (GetStorageClass(address_space)) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 4043 | case spv::StorageClassStorageBuffer: |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4044 | // Save the need to generate an ArrayStride decoration. But defer |
| 4045 | // generation until later, so we only make one decoration. |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 4046 | getTypesNeedingArrayStride().insert(GEP->getPointerOperandType()); |
| 4047 | break; |
| 4048 | case spv::StorageClassWorkgroup: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 4049 | break; |
| 4050 | default: |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 4051 | llvm_unreachable( |
| 4052 | "OpPtrAccessChain is not supported for this storage class"); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 4053 | break; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4054 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | for (auto II = GEP->idx_begin() + offset; II != GEP->idx_end(); II++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4058 | Ops << *II; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4059 | } |
| 4060 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4061 | RID = addSPIRVInst(Opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4062 | break; |
| 4063 | } |
| 4064 | case Instruction::ExtractValue: { |
| 4065 | ExtractValueInst *EVI = cast<ExtractValueInst>(&I); |
| 4066 | // Ops[0] = Result Type ID |
| 4067 | // Ops[1] = Composite ID |
| 4068 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4069 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4070 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4071 | Ops << I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4072 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4073 | Ops << EVI->getAggregateOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4074 | |
| 4075 | for (auto &Index : EVI->indices()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4076 | Ops << Index; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4077 | } |
| 4078 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4079 | RID = addSPIRVInst(spv::OpCompositeExtract, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4080 | break; |
| 4081 | } |
| 4082 | case Instruction::InsertValue: { |
| 4083 | InsertValueInst *IVI = cast<InsertValueInst>(&I); |
| 4084 | // Ops[0] = Result Type ID |
| 4085 | // Ops[1] = Object ID |
| 4086 | // Ops[2] = Composite ID |
| 4087 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4088 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4089 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4090 | Ops << I.getType() << IVI->getInsertedValueOperand() |
| 4091 | << IVI->getAggregateOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4092 | |
| 4093 | for (auto &Index : IVI->indices()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4094 | Ops << Index; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4095 | } |
| 4096 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4097 | RID = addSPIRVInst(spv::OpCompositeInsert, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4098 | break; |
| 4099 | } |
| 4100 | case Instruction::Select: { |
| 4101 | // |
| 4102 | // Generate OpSelect. |
| 4103 | // |
| 4104 | |
| 4105 | // Ops[0] = Result Type ID |
| 4106 | // Ops[1] = Condition ID |
| 4107 | // Ops[2] = True Constant ID |
| 4108 | // Ops[3] = False Constant ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4109 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4110 | |
| 4111 | // Find SPIRV instruction for parameter type. |
| 4112 | auto Ty = I.getType(); |
| 4113 | if (Ty->isPointerTy()) { |
| 4114 | auto PointeeTy = Ty->getPointerElementType(); |
| 4115 | if (PointeeTy->isStructTy() && |
| 4116 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 4117 | Ty = PointeeTy; |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4118 | } else { |
| 4119 | // Selecting between pointers requires variable pointers. |
| 4120 | setVariablePointersCapabilities(Ty->getPointerAddressSpace()); |
| 4121 | if (!hasVariablePointers() && !selectFromSameObject(&I)) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4122 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4123 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4127 | Ops << Ty << I.getOperand(0) << I.getOperand(1) << I.getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4128 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4129 | RID = addSPIRVInst(spv::OpSelect, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4130 | break; |
| 4131 | } |
| 4132 | case Instruction::ExtractElement: { |
| 4133 | // Handle <4 x i8> type manually. |
| 4134 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 4135 | if (is4xi8vec(CompositeTy)) { |
| 4136 | // |
| 4137 | // Generate OpShiftRightLogical and OpBitwiseAnd for extractelement with |
| 4138 | // <4 x i8>. |
| 4139 | // |
| 4140 | |
| 4141 | // |
| 4142 | // Generate OpShiftRightLogical |
| 4143 | // |
| 4144 | // Ops[0] = Result Type ID |
| 4145 | // Ops[1] = Operand 0 |
| 4146 | // Ops[2] = Operand 1 |
| 4147 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4148 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4149 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4150 | Ops << CompositeTy << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4151 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4152 | SPIRVID Op1ID = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4153 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 4154 | // Handle constant index. |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4155 | uint32_t Idx = static_cast<uint32_t>(CI->getZExtValue()); |
| 4156 | Op1ID = getSPIRVInt32Constant(Idx * 8); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4157 | } else { |
| 4158 | // Handle variable index. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4159 | SPIRVOperandVec TmpOps; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4160 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4161 | TmpOps << Type::getInt32Ty(Context) << I.getOperand(1) |
| 4162 | << getSPIRVInt32Constant(8); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4163 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4164 | Op1ID = addSPIRVInst(spv::OpIMul, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4165 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4166 | Ops << Op1ID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4167 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4168 | SPIRVID ShiftID = addSPIRVInst(spv::OpShiftRightLogical, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4169 | |
| 4170 | // |
| 4171 | // Generate OpBitwiseAnd |
| 4172 | // |
| 4173 | // Ops[0] = Result Type ID |
| 4174 | // Ops[1] = Operand 0 |
| 4175 | // Ops[2] = Operand 1 |
| 4176 | // |
| 4177 | Ops.clear(); |
| 4178 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4179 | Ops << CompositeTy << ShiftID << getSPIRVInt32Constant(0xFF); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4180 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4181 | RID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4182 | break; |
| 4183 | } |
| 4184 | |
| 4185 | // Ops[0] = Result Type ID |
| 4186 | // Ops[1] = Composite ID |
| 4187 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4188 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4189 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4190 | Ops << I.getType() << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4191 | |
| 4192 | spv::Op Opcode = spv::OpCompositeExtract; |
| 4193 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4194 | Ops << static_cast<uint32_t>(CI->getZExtValue()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4195 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4196 | Ops << I.getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4197 | Opcode = spv::OpVectorExtractDynamic; |
| 4198 | } |
| 4199 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4200 | RID = addSPIRVInst(Opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4201 | break; |
| 4202 | } |
| 4203 | case Instruction::InsertElement: { |
| 4204 | // Handle <4 x i8> type manually. |
| 4205 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 4206 | if (is4xi8vec(CompositeTy)) { |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4207 | SPIRVID CstFFID = getSPIRVInt32Constant(0xFF); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4208 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4209 | SPIRVID ShiftAmountID = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4210 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 4211 | // Handle constant index. |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4212 | uint32_t Idx = static_cast<uint32_t>(CI->getZExtValue()); |
| 4213 | ShiftAmountID = getSPIRVInt32Constant(Idx * 8); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4214 | } else { |
| 4215 | // Handle variable index. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4216 | SPIRVOperandVec TmpOps; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4217 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4218 | TmpOps << Type::getInt32Ty(Context) << I.getOperand(2) |
| 4219 | << getSPIRVInt32Constant(8); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4220 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4221 | ShiftAmountID = addSPIRVInst(spv::OpIMul, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | // |
| 4225 | // Generate mask operations. |
| 4226 | // |
| 4227 | |
| 4228 | // ShiftLeft mask according to index of insertelement. |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4229 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4230 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4231 | Ops << CompositeTy << CstFFID << ShiftAmountID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4232 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4233 | SPIRVID MaskID = addSPIRVInst(spv::OpShiftLeftLogical, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4234 | |
| 4235 | // Inverse mask. |
| 4236 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4237 | Ops << CompositeTy << MaskID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4238 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4239 | SPIRVID InvMaskID = addSPIRVInst(spv::OpNot, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4240 | |
| 4241 | // Apply mask. |
| 4242 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4243 | Ops << CompositeTy << I.getOperand(0) << InvMaskID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4244 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4245 | SPIRVID OrgValID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4246 | |
| 4247 | // Create correct value according to index of insertelement. |
| 4248 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4249 | Ops << CompositeTy << I.getOperand(1) << ShiftAmountID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4250 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4251 | SPIRVID InsertValID = addSPIRVInst(spv::OpShiftLeftLogical, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4252 | |
| 4253 | // Insert value to original value. |
| 4254 | Ops.clear(); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4255 | Ops << CompositeTy << OrgValID << InsertValID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4256 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4257 | RID = addSPIRVInst(spv::OpBitwiseOr, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4258 | break; |
| 4259 | } |
| 4260 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4261 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4262 | |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4263 | // Ops[0] = Result Type ID |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4264 | Ops << I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4265 | |
| 4266 | spv::Op Opcode = spv::OpCompositeInsert; |
| 4267 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4268 | const auto value = CI->getZExtValue(); |
| 4269 | assert(value <= UINT32_MAX); |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4270 | // Ops[1] = Object ID |
| 4271 | // Ops[2] = Composite ID |
| 4272 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4273 | Ops << I.getOperand(1) << I.getOperand(0) << static_cast<uint32_t>(value); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4274 | } else { |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4275 | // Ops[1] = Composite ID |
| 4276 | // Ops[2] = Object ID |
| 4277 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4278 | Ops << I.getOperand(0) << I.getOperand(1) << I.getOperand(2); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4279 | Opcode = spv::OpVectorInsertDynamic; |
| 4280 | } |
| 4281 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4282 | RID = addSPIRVInst(Opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4283 | break; |
| 4284 | } |
| 4285 | case Instruction::ShuffleVector: { |
| 4286 | // Ops[0] = Result Type ID |
| 4287 | // Ops[1] = Vector 1 ID |
| 4288 | // Ops[2] = Vector 2 ID |
| 4289 | // Ops[3] ... Ops[n] = Components (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4290 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4291 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4292 | Ops << I.getType() << I.getOperand(0) << I.getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4293 | |
alan-baker | c966671 | 2020-04-01 16:31:21 -0400 | [diff] [blame] | 4294 | auto shuffle = cast<ShuffleVectorInst>(&I); |
| 4295 | SmallVector<int, 4> mask; |
| 4296 | shuffle->getShuffleMask(mask); |
| 4297 | for (auto i : mask) { |
| 4298 | if (i == UndefMaskElem) { |
| 4299 | if (clspv::Option::HackUndef()) |
| 4300 | // Use 0 instead of undef. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4301 | Ops << 0; |
alan-baker | c966671 | 2020-04-01 16:31:21 -0400 | [diff] [blame] | 4302 | else |
| 4303 | // Undef for shuffle in SPIR-V. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4304 | Ops << 0xffffffff; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4305 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4306 | Ops << i; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4307 | } |
| 4308 | } |
| 4309 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4310 | RID = addSPIRVInst(spv::OpVectorShuffle, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4311 | break; |
| 4312 | } |
| 4313 | case Instruction::ICmp: |
| 4314 | case Instruction::FCmp: { |
| 4315 | CmpInst *CmpI = cast<CmpInst>(&I); |
| 4316 | |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4317 | // Pointer equality is invalid. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4318 | Type *ArgTy = CmpI->getOperand(0)->getType(); |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4319 | if (isa<PointerType>(ArgTy)) { |
| 4320 | CmpI->print(errs()); |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 4321 | std::string name = I.getParent()->getParent()->getName().str(); |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4322 | errs() |
| 4323 | << "\nPointer equality test is not supported by SPIR-V for Vulkan, " |
| 4324 | << "in function " << name << "\n"; |
| 4325 | llvm_unreachable("Pointer equality check is invalid"); |
| 4326 | break; |
| 4327 | } |
| 4328 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4329 | SPIRVOperandVec Ops; |
alan-baker | 1510657 | 2020-11-06 15:08:10 -0500 | [diff] [blame^] | 4330 | if (CmpI->getPredicate() == CmpInst::FCMP_ORD || |
| 4331 | CmpI->getPredicate() == CmpInst::FCMP_UNO) { |
| 4332 | // Implement ordered and unordered comparisons are OpIsNan instructions. |
| 4333 | // Optimize the constants to simplify the resulting code. |
| 4334 | auto lhs = CmpI->getOperand(0); |
| 4335 | auto rhs = CmpI->getOperand(1); |
| 4336 | auto const_lhs = dyn_cast_or_null<Constant>(lhs); |
| 4337 | auto const_rhs = dyn_cast_or_null<Constant>(rhs); |
| 4338 | if ((const_lhs && const_lhs->isNaN()) || |
| 4339 | (const_rhs && const_rhs->isNaN())) { |
| 4340 | // Result is a constant, false of ordered, true for unordered. |
| 4341 | if (CmpI->getPredicate() == CmpInst::FCMP_ORD) { |
| 4342 | RID = getSPIRVConstant(ConstantInt::getFalse(CmpI->getType())); |
| 4343 | } else { |
| 4344 | RID = getSPIRVConstant(ConstantInt::getTrue(CmpI->getType())); |
| 4345 | } |
| 4346 | break; |
| 4347 | } |
| 4348 | SPIRVID lhs_id; |
| 4349 | SPIRVID rhs_id; |
| 4350 | if (!const_lhs) { |
| 4351 | // Generate OpIsNan for the lhs. |
| 4352 | Ops.clear(); |
| 4353 | Ops << CmpI->getType() << lhs; |
| 4354 | lhs_id = addSPIRVInst(spv::OpIsNan, Ops); |
| 4355 | } |
| 4356 | if (!const_rhs) { |
| 4357 | // Generate OpIsNan for the rhs. |
| 4358 | Ops.clear(); |
| 4359 | Ops << CmpI->getType() << rhs; |
| 4360 | rhs_id = addSPIRVInst(spv::OpIsNan, Ops); |
| 4361 | } |
| 4362 | if (lhs_id.isValid() && rhs_id.isValid()) { |
| 4363 | // Or the results for the lhs and rhs. |
| 4364 | Ops.clear(); |
| 4365 | Ops << CmpI->getType() << lhs_id << rhs_id; |
| 4366 | RID = addSPIRVInst(spv::OpLogicalOr, Ops); |
| 4367 | } else { |
| 4368 | RID = lhs_id.isValid() ? lhs_id : rhs_id; |
| 4369 | } |
| 4370 | if (CmpI->getPredicate() == CmpInst::FCMP_ORD) { |
| 4371 | // For ordered comparisons, invert the intermediate result. |
| 4372 | Ops.clear(); |
| 4373 | Ops << CmpI->getType() << RID; |
| 4374 | RID = addSPIRVInst(spv::OpLogicalNot, Ops); |
| 4375 | } |
| 4376 | break; |
| 4377 | } else { |
| 4378 | // Remaining comparisons map directly to SPIR-V opcodes. |
| 4379 | // Ops[0] = Result Type ID |
| 4380 | // Ops[1] = Operand 1 ID |
| 4381 | // Ops[2] = Operand 2 ID |
| 4382 | Ops << CmpI->getType() << CmpI->getOperand(0) << CmpI->getOperand(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4383 | |
alan-baker | 1510657 | 2020-11-06 15:08:10 -0500 | [diff] [blame^] | 4384 | spv::Op Opcode = GetSPIRVCmpOpcode(CmpI); |
| 4385 | RID = addSPIRVInst(Opcode, Ops); |
| 4386 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4387 | break; |
| 4388 | } |
| 4389 | case Instruction::Br: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4390 | // Branch instruction is deferred because it needs label's ID. |
| 4391 | BasicBlock *BrBB = I.getParent(); |
| 4392 | if (ContinueBlocks.count(BrBB) || MergeBlocks.count(BrBB)) { |
| 4393 | // Placeholder for Merge operation |
| 4394 | RID = addSPIRVPlaceholder(&I); |
| 4395 | } |
| 4396 | RID = addSPIRVPlaceholder(&I); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4397 | break; |
| 4398 | } |
| 4399 | case Instruction::Switch: { |
| 4400 | I.print(errs()); |
| 4401 | llvm_unreachable("Unsupported instruction???"); |
| 4402 | break; |
| 4403 | } |
| 4404 | case Instruction::IndirectBr: { |
| 4405 | I.print(errs()); |
| 4406 | llvm_unreachable("Unsupported instruction???"); |
| 4407 | break; |
| 4408 | } |
| 4409 | case Instruction::PHI: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4410 | // PHI instruction is deferred because it needs label's ID. |
| 4411 | RID = addSPIRVPlaceholder(&I); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4412 | break; |
| 4413 | } |
| 4414 | case Instruction::Alloca: { |
| 4415 | // |
| 4416 | // Generate OpVariable. |
| 4417 | // |
| 4418 | // Ops[0] : Result Type ID |
| 4419 | // Ops[1] : Storage Class |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4420 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4421 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4422 | Ops << I.getType() << spv::StorageClassFunction; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4423 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4424 | RID = addSPIRVInst(spv::OpVariable, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4425 | break; |
| 4426 | } |
| 4427 | case Instruction::Load: { |
| 4428 | LoadInst *LD = cast<LoadInst>(&I); |
| 4429 | // |
| 4430 | // Generate OpLoad. |
| 4431 | // |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 4432 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4433 | if (LD->getType()->isPointerTy()) { |
| 4434 | // Loading a pointer requires variable pointers. |
| 4435 | setVariablePointersCapabilities(LD->getType()->getPointerAddressSpace()); |
| 4436 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4437 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4438 | SPIRVID PointerID = getSPIRVValue(LD->getPointerOperand()); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4439 | // This is a hack to work around what looks like a driver bug. |
| 4440 | // When we're loading from the special variable holding the WorkgroupSize |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4441 | // builtin value, use an OpBitWiseAnd of the value's ID rather than |
| 4442 | // generating a load. |
David Neto | 66cfe64 | 2018-03-24 06:13:56 -0700 | [diff] [blame] | 4443 | // TODO(dneto): Remove this awful hack once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4444 | if (PointerID == WorkgroupSizeVarID) { |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4445 | // Generate a bitwise-and of the original value with itself. |
| 4446 | // We should have been able to get away with just an OpCopyObject, |
| 4447 | // but we need something more complex to get past certain driver bugs. |
| 4448 | // This is ridiculous, but necessary. |
| 4449 | // TODO(dneto): Revisit this once drivers fix their bugs. |
| 4450 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4451 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4452 | Ops << LD->getType() << WorkgroupSizeValueID << WorkgroupSizeValueID; |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4453 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4454 | RID = addSPIRVInst(spv::OpBitwiseAnd, Ops); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4455 | break; |
| 4456 | } |
| 4457 | |
| 4458 | // This is the normal path. Generate a load. |
| 4459 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4460 | // Ops[0] = Result Type ID |
| 4461 | // Ops[1] = Pointer ID |
| 4462 | // Ops[2] ... Ops[n] = Optional Memory Access |
| 4463 | // |
| 4464 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4465 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4466 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4467 | Ops << LD->getType() << LD->getPointerOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4468 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4469 | RID = addSPIRVInst(spv::OpLoad, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4470 | break; |
| 4471 | } |
| 4472 | case Instruction::Store: { |
| 4473 | StoreInst *ST = cast<StoreInst>(&I); |
| 4474 | // |
| 4475 | // Generate OpStore. |
| 4476 | // |
| 4477 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4478 | if (ST->getValueOperand()->getType()->isPointerTy()) { |
| 4479 | // Storing a pointer requires variable pointers. |
| 4480 | setVariablePointersCapabilities( |
| 4481 | ST->getValueOperand()->getType()->getPointerAddressSpace()); |
| 4482 | } |
| 4483 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4484 | // Ops[0] = Pointer ID |
| 4485 | // Ops[1] = Object ID |
| 4486 | // Ops[2] ... Ops[n] = Optional Memory Access (later???) |
| 4487 | // |
| 4488 | // TODO: Do we need to implement Optional Memory Access??? |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4489 | SPIRVOperandVec Ops; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4490 | Ops << ST->getPointerOperand() << ST->getValueOperand(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4491 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4492 | RID = addSPIRVInst(spv::OpStore, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4493 | break; |
| 4494 | } |
| 4495 | case Instruction::AtomicCmpXchg: { |
| 4496 | I.print(errs()); |
| 4497 | llvm_unreachable("Unsupported instruction???"); |
| 4498 | break; |
| 4499 | } |
| 4500 | case Instruction::AtomicRMW: { |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4501 | AtomicRMWInst *AtomicRMW = dyn_cast<AtomicRMWInst>(&I); |
| 4502 | |
| 4503 | spv::Op opcode; |
| 4504 | |
| 4505 | switch (AtomicRMW->getOperation()) { |
| 4506 | default: |
| 4507 | I.print(errs()); |
| 4508 | llvm_unreachable("Unsupported instruction???"); |
| 4509 | case llvm::AtomicRMWInst::Add: |
| 4510 | opcode = spv::OpAtomicIAdd; |
| 4511 | break; |
| 4512 | case llvm::AtomicRMWInst::Sub: |
| 4513 | opcode = spv::OpAtomicISub; |
| 4514 | break; |
| 4515 | case llvm::AtomicRMWInst::Xchg: |
| 4516 | opcode = spv::OpAtomicExchange; |
| 4517 | break; |
| 4518 | case llvm::AtomicRMWInst::Min: |
| 4519 | opcode = spv::OpAtomicSMin; |
| 4520 | break; |
| 4521 | case llvm::AtomicRMWInst::Max: |
| 4522 | opcode = spv::OpAtomicSMax; |
| 4523 | break; |
| 4524 | case llvm::AtomicRMWInst::UMin: |
| 4525 | opcode = spv::OpAtomicUMin; |
| 4526 | break; |
| 4527 | case llvm::AtomicRMWInst::UMax: |
| 4528 | opcode = spv::OpAtomicUMax; |
| 4529 | break; |
| 4530 | case llvm::AtomicRMWInst::And: |
| 4531 | opcode = spv::OpAtomicAnd; |
| 4532 | break; |
| 4533 | case llvm::AtomicRMWInst::Or: |
| 4534 | opcode = spv::OpAtomicOr; |
| 4535 | break; |
| 4536 | case llvm::AtomicRMWInst::Xor: |
| 4537 | opcode = spv::OpAtomicXor; |
| 4538 | break; |
| 4539 | } |
| 4540 | |
| 4541 | // |
| 4542 | // Generate OpAtomic*. |
| 4543 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4544 | SPIRVOperandVec Ops; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4545 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4546 | Ops << I.getType() << AtomicRMW->getPointerOperand(); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4547 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4548 | const auto ConstantScopeDevice = getSPIRVInt32Constant(spv::ScopeDevice); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4549 | Ops << ConstantScopeDevice; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4550 | |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4551 | const auto ConstantMemorySemantics = |
| 4552 | getSPIRVInt32Constant(spv::MemorySemanticsUniformMemoryMask | |
| 4553 | spv::MemorySemanticsSequentiallyConsistentMask); |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4554 | Ops << ConstantMemorySemantics << AtomicRMW->getValOperand(); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4555 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4556 | RID = addSPIRVInst(opcode, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4557 | break; |
| 4558 | } |
| 4559 | case Instruction::Fence: { |
| 4560 | I.print(errs()); |
| 4561 | llvm_unreachable("Unsupported instruction???"); |
| 4562 | break; |
| 4563 | } |
| 4564 | case Instruction::Call: { |
| 4565 | CallInst *Call = dyn_cast<CallInst>(&I); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4566 | RID = GenerateInstructionFromCall(Call); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4567 | break; |
| 4568 | } |
| 4569 | case Instruction::Ret: { |
| 4570 | unsigned NumOps = I.getNumOperands(); |
| 4571 | if (NumOps == 0) { |
| 4572 | // |
| 4573 | // Generate OpReturn. |
| 4574 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4575 | RID = addSPIRVInst(spv::OpReturn); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4576 | } else { |
| 4577 | // |
| 4578 | // Generate OpReturnValue. |
| 4579 | // |
| 4580 | |
| 4581 | // Ops[0] = Return Value ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4582 | SPIRVOperandVec Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4583 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4584 | Ops << I.getOperand(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4585 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4586 | RID = addSPIRVInst(spv::OpReturnValue, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4587 | break; |
| 4588 | } |
| 4589 | break; |
| 4590 | } |
| 4591 | } |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4592 | |
| 4593 | // Register Instruction to ValueMap. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4594 | if (RID.isValid()) { |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4595 | VMap[&I] = RID; |
| 4596 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | void SPIRVProducerPass::GenerateFuncEpilogue() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4600 | // |
| 4601 | // Generate OpFunctionEnd |
| 4602 | // |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4603 | addSPIRVInst(spv::OpFunctionEnd); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | bool SPIRVProducerPass::is4xi8vec(Type *Ty) const { |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 4607 | // Don't specialize <4 x i8> if i8 is generally supported. |
| 4608 | if (clspv::Option::Int8Support()) |
| 4609 | return false; |
| 4610 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4611 | LLVMContext &Context = Ty->getContext(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 4612 | if (auto VecTy = dyn_cast<VectorType>(Ty)) { |
| 4613 | if (VecTy->getElementType() == Type::getInt8Ty(Context) && |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 4614 | VecTy->getElementCount().getKnownMinValue() == 4) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4615 | return true; |
| 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | return false; |
| 4620 | } |
| 4621 | |
| 4622 | void SPIRVProducerPass::HandleDeferredInstruction() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4623 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 4624 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4625 | for (size_t i = 0; i < DeferredInsts.size(); ++i) { |
| 4626 | Value *Inst = DeferredInsts[i].first; |
| 4627 | SPIRVInstruction *Placeholder = DeferredInsts[i].second; |
| 4628 | SPIRVOperandVec Operands; |
| 4629 | |
| 4630 | auto nextDeferred = [&i, &Inst, &DeferredInsts, &Placeholder]() { |
| 4631 | ++i; |
| 4632 | assert(DeferredInsts.size() > i); |
| 4633 | assert(Inst == DeferredInsts[i].first); |
| 4634 | Placeholder = DeferredInsts[i].second; |
| 4635 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4636 | |
| 4637 | if (BranchInst *Br = dyn_cast<BranchInst>(Inst)) { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4638 | // Check whether this branch needs to be preceeded by merge instruction. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4639 | BasicBlock *BrBB = Br->getParent(); |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4640 | if (ContinueBlocks.count(BrBB)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4641 | // |
| 4642 | // Generate OpLoopMerge. |
| 4643 | // |
| 4644 | // Ops[0] = Merge Block ID |
| 4645 | // Ops[1] = Continue Target ID |
| 4646 | // Ops[2] = Selection Control |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4647 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4648 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4649 | Ops << MergeBlocks[BrBB] << ContinueBlocks[BrBB] |
| 4650 | << spv::LoopControlMaskNone; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4651 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4652 | replaceSPIRVInst(Placeholder, spv::OpLoopMerge, Ops); |
| 4653 | |
| 4654 | nextDeferred(); |
| 4655 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4656 | } else if (MergeBlocks.count(BrBB)) { |
| 4657 | // |
| 4658 | // Generate OpSelectionMerge. |
| 4659 | // |
| 4660 | // Ops[0] = Merge Block ID |
| 4661 | // Ops[1] = Selection Control |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4662 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4663 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 4664 | auto MergeBB = MergeBlocks[BrBB]; |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4665 | Ops << MergeBB << spv::SelectionControlMaskNone; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4666 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4667 | replaceSPIRVInst(Placeholder, spv::OpSelectionMerge, Ops); |
| 4668 | |
| 4669 | nextDeferred(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4670 | } |
| 4671 | |
| 4672 | if (Br->isConditional()) { |
| 4673 | // |
| 4674 | // Generate OpBranchConditional. |
| 4675 | // |
| 4676 | // Ops[0] = Condition ID |
| 4677 | // Ops[1] = True Label ID |
| 4678 | // Ops[2] = False Label ID |
| 4679 | // Ops[3] ... Ops[n] = Branch weights (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4680 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4681 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4682 | Ops << Br->getCondition() << Br->getSuccessor(0) << Br->getSuccessor(1); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4683 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4684 | replaceSPIRVInst(Placeholder, spv::OpBranchConditional, Ops); |
| 4685 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4686 | } else { |
| 4687 | // |
| 4688 | // Generate OpBranch. |
| 4689 | // |
| 4690 | // Ops[0] = Target Label ID |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4691 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4692 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4693 | Ops << Br->getSuccessor(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4694 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4695 | replaceSPIRVInst(Placeholder, spv::OpBranch, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4696 | } |
| 4697 | } else if (PHINode *PHI = dyn_cast<PHINode>(Inst)) { |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 4698 | if (PHI->getType()->isPointerTy() && !IsSamplerType(PHI->getType()) && |
| 4699 | !IsImageType(PHI->getType())) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4700 | // OpPhi on pointers requires variable pointers. |
| 4701 | setVariablePointersCapabilities( |
| 4702 | PHI->getType()->getPointerAddressSpace()); |
| 4703 | if (!hasVariablePointers() && !selectFromSameObject(PHI)) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4704 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4705 | } |
| 4706 | } |
| 4707 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4708 | // |
| 4709 | // Generate OpPhi. |
| 4710 | // |
| 4711 | // Ops[0] = Result Type ID |
| 4712 | // Ops[1] ... Ops[n] = (Variable ID, Parent ID) pairs |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4713 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4714 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4715 | Ops << PHI->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4716 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4717 | for (unsigned j = 0; j < PHI->getNumIncomingValues(); j++) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4718 | Ops << PHI->getIncomingValue(j) << PHI->getIncomingBlock(j); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4719 | } |
| 4720 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4721 | replaceSPIRVInst(Placeholder, spv::OpPhi, Ops); |
| 4722 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4723 | } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { |
| 4724 | Function *Callee = Call->getCalledFunction(); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 4725 | auto callee_name = Callee->getName(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4726 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4727 | if (Builtins::Lookup(Callee) == Builtins::kClspvCompositeConstruct) { |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4728 | // Generate an OpCompositeConstruct |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4729 | SPIRVOperandVec Ops; |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4730 | |
| 4731 | // The result type. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4732 | Ops << Call->getType(); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4733 | |
| 4734 | for (Use &use : Call->arg_operands()) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4735 | Ops << use.get(); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4736 | } |
| 4737 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4738 | replaceSPIRVInst(Placeholder, spv::OpCompositeConstruct, Ops); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 4739 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4740 | } else { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4741 | if (Call->getType()->isPointerTy()) { |
| 4742 | // Functions returning pointers require variable pointers. |
| 4743 | setVariablePointersCapabilities( |
| 4744 | Call->getType()->getPointerAddressSpace()); |
| 4745 | } |
| 4746 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4747 | // |
| 4748 | // Generate OpFunctionCall. |
| 4749 | // |
| 4750 | |
| 4751 | // Ops[0] = Result Type ID |
| 4752 | // Ops[1] = Callee Function ID |
| 4753 | // Ops[2] ... Ops[n] = Argument 0, ... , Argument n |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4754 | SPIRVOperandVec Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4755 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4756 | Ops << Call->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4757 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4758 | SPIRVID CalleeID = getSPIRVValue(Callee); |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 4759 | if (!CalleeID.isValid()) { |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 4760 | errs() << "Can't translate function call. Missing builtin? " |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4761 | << callee_name << " in: " << *Call << "\n"; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 4762 | // TODO(dneto): Can we error out? Enabling this llvm_unreachable |
| 4763 | // causes an infinite loop. Instead, go ahead and generate |
| 4764 | // the bad function call. A validator will catch the 0-Id. |
| 4765 | // llvm_unreachable("Can't translate function call"); |
| 4766 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4767 | |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4768 | Ops << CalleeID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4769 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4770 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4771 | for (unsigned j = 0; j < CalleeFTy->getNumParams(); j++) { |
| 4772 | auto *operand = Call->getOperand(j); |
alan-baker | d4d5065 | 2019-12-03 17:17:15 -0500 | [diff] [blame] | 4773 | auto *operand_type = operand->getType(); |
| 4774 | // Images and samplers can be passed as function parameters without |
| 4775 | // variable pointers. |
| 4776 | if (operand_type->isPointerTy() && !IsImageType(operand_type) && |
| 4777 | !IsSamplerType(operand_type)) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4778 | auto sc = |
| 4779 | GetStorageClass(operand->getType()->getPointerAddressSpace()); |
| 4780 | if (sc == spv::StorageClassStorageBuffer) { |
| 4781 | // Passing SSBO by reference requires variable pointers storage |
| 4782 | // buffer. |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4783 | setVariablePointersStorageBuffer(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4784 | } else if (sc == spv::StorageClassWorkgroup) { |
| 4785 | // Workgroup references require variable pointers if they are not |
| 4786 | // memory object declarations. |
| 4787 | if (auto *operand_call = dyn_cast<CallInst>(operand)) { |
| 4788 | // Workgroup accessor represents a variable reference. |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4789 | if (Builtins::Lookup(operand_call->getCalledFunction()) != |
| 4790 | Builtins::kClspvLocal) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4791 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4792 | } else { |
| 4793 | // Arguments are function parameters. |
| 4794 | if (!isa<Argument>(operand)) |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4795 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4796 | } |
| 4797 | } |
| 4798 | } |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 4799 | Ops << operand; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4800 | } |
| 4801 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 4802 | replaceSPIRVInst(Placeholder, spv::OpFunctionCall, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4803 | } |
| 4804 | } |
| 4805 | } |
| 4806 | } |
| 4807 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 4808 | void SPIRVProducerPass::HandleDeferredDecorations() { |
| 4809 | const auto &DL = module->getDataLayout(); |
alan-baker | 5a8c3be | 2020-09-09 13:44:26 -0400 | [diff] [blame] | 4810 | if (getTypesNeedingArrayStride().empty()) { |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4811 | return; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 4812 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4813 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 4814 | // Insert ArrayStride decorations on pointer types, due to OpPtrAccessChain |
| 4815 | // instructions we generated earlier. |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 4816 | DenseSet<uint32_t> seen; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4817 | for (auto *type : getTypesNeedingArrayStride()) { |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 4818 | auto id = getSPIRVType(type); |
| 4819 | if (!seen.insert(id.get()).second) |
| 4820 | continue; |
| 4821 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4822 | Type *elemTy = nullptr; |
| 4823 | if (auto *ptrTy = dyn_cast<PointerType>(type)) { |
| 4824 | elemTy = ptrTy->getElementType(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4825 | } else if (auto *arrayTy = dyn_cast<ArrayType>(type)) { |
alan-baker | 8eb435a | 2020-04-08 00:42:06 -0400 | [diff] [blame] | 4826 | elemTy = arrayTy->getElementType(); |
| 4827 | } else if (auto *vecTy = dyn_cast<VectorType>(type)) { |
| 4828 | elemTy = vecTy->getElementType(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4829 | } else { |
| 4830 | errs() << "Unhandled strided type " << *type << "\n"; |
| 4831 | llvm_unreachable("Unhandled strided type"); |
| 4832 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4833 | |
| 4834 | // Ops[0] = Target ID |
| 4835 | // Ops[1] = Decoration (ArrayStride) |
| 4836 | // Ops[2] = Stride number (Literal Number) |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4837 | SPIRVOperandVec Ops; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4838 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4839 | // Same as DL.getIndexedOffsetInType( elemTy, { 1 } ); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 4840 | const uint32_t stride = static_cast<uint32_t>(GetTypeAllocSize(elemTy, DL)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4841 | |
alan-baker | c3fd07f | 2020-10-22 09:48:49 -0400 | [diff] [blame] | 4842 | Ops << id << spv::DecorationArrayStride << stride; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4843 | |
SJW | f93f5f3 | 2020-05-05 07:27:56 -0500 | [diff] [blame] | 4844 | addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 4845 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4846 | } |
| 4847 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4848 | glsl::ExtInst |
| 4849 | SPIRVProducerPass::getExtInstEnum(const Builtins::FunctionInfo &func_info) { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4850 | switch (func_info.getType()) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4851 | case Builtins::kClamp: { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4852 | auto param_type = func_info.getParameter(0); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4853 | if (param_type.type_id == Type::FloatTyID) { |
| 4854 | return glsl::ExtInst::ExtInstFClamp; |
| 4855 | } |
| 4856 | return param_type.is_signed ? glsl::ExtInst::ExtInstSClamp |
| 4857 | : glsl::ExtInst::ExtInstUClamp; |
| 4858 | } |
| 4859 | case Builtins::kMax: { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4860 | auto param_type = func_info.getParameter(0); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4861 | if (param_type.type_id == Type::FloatTyID) { |
| 4862 | return glsl::ExtInst::ExtInstFMax; |
| 4863 | } |
| 4864 | return param_type.is_signed ? glsl::ExtInst::ExtInstSMax |
| 4865 | : glsl::ExtInst::ExtInstUMax; |
| 4866 | } |
| 4867 | case Builtins::kMin: { |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4868 | auto param_type = func_info.getParameter(0); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4869 | if (param_type.type_id == Type::FloatTyID) { |
| 4870 | return glsl::ExtInst::ExtInstFMin; |
| 4871 | } |
| 4872 | return param_type.is_signed ? glsl::ExtInst::ExtInstSMin |
| 4873 | : glsl::ExtInst::ExtInstUMin; |
| 4874 | } |
| 4875 | case Builtins::kAbs: |
| 4876 | return glsl::ExtInst::ExtInstSAbs; |
| 4877 | case Builtins::kFmax: |
Marco Antognini | 55d5186 | 2020-07-21 17:50:07 +0100 | [diff] [blame] | 4878 | return glsl::ExtInst::ExtInstNMax; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4879 | case Builtins::kFmin: |
Marco Antognini | 55d5186 | 2020-07-21 17:50:07 +0100 | [diff] [blame] | 4880 | return glsl::ExtInst::ExtInstNMin; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4881 | case Builtins::kDegrees: |
| 4882 | return glsl::ExtInst::ExtInstDegrees; |
| 4883 | case Builtins::kRadians: |
| 4884 | return glsl::ExtInst::ExtInstRadians; |
| 4885 | case Builtins::kMix: |
| 4886 | return glsl::ExtInst::ExtInstFMix; |
| 4887 | case Builtins::kAcos: |
| 4888 | case Builtins::kAcospi: |
| 4889 | return glsl::ExtInst::ExtInstAcos; |
| 4890 | case Builtins::kAcosh: |
| 4891 | return glsl::ExtInst::ExtInstAcosh; |
| 4892 | case Builtins::kAsin: |
| 4893 | case Builtins::kAsinpi: |
| 4894 | return glsl::ExtInst::ExtInstAsin; |
| 4895 | case Builtins::kAsinh: |
| 4896 | return glsl::ExtInst::ExtInstAsinh; |
| 4897 | case Builtins::kAtan: |
| 4898 | case Builtins::kAtanpi: |
| 4899 | return glsl::ExtInst::ExtInstAtan; |
| 4900 | case Builtins::kAtanh: |
| 4901 | return glsl::ExtInst::ExtInstAtanh; |
| 4902 | case Builtins::kAtan2: |
| 4903 | case Builtins::kAtan2pi: |
| 4904 | return glsl::ExtInst::ExtInstAtan2; |
| 4905 | case Builtins::kCeil: |
| 4906 | return glsl::ExtInst::ExtInstCeil; |
| 4907 | case Builtins::kSin: |
| 4908 | case Builtins::kHalfSin: |
| 4909 | case Builtins::kNativeSin: |
| 4910 | return glsl::ExtInst::ExtInstSin; |
| 4911 | case Builtins::kSinh: |
| 4912 | return glsl::ExtInst::ExtInstSinh; |
| 4913 | case Builtins::kCos: |
| 4914 | case Builtins::kHalfCos: |
| 4915 | case Builtins::kNativeCos: |
| 4916 | return glsl::ExtInst::ExtInstCos; |
| 4917 | case Builtins::kCosh: |
| 4918 | return glsl::ExtInst::ExtInstCosh; |
| 4919 | case Builtins::kTan: |
| 4920 | case Builtins::kHalfTan: |
| 4921 | case Builtins::kNativeTan: |
| 4922 | return glsl::ExtInst::ExtInstTan; |
| 4923 | case Builtins::kTanh: |
| 4924 | return glsl::ExtInst::ExtInstTanh; |
| 4925 | case Builtins::kExp: |
| 4926 | case Builtins::kHalfExp: |
| 4927 | case Builtins::kNativeExp: |
| 4928 | return glsl::ExtInst::ExtInstExp; |
| 4929 | case Builtins::kExp2: |
| 4930 | case Builtins::kHalfExp2: |
| 4931 | case Builtins::kNativeExp2: |
| 4932 | return glsl::ExtInst::ExtInstExp2; |
| 4933 | case Builtins::kLog: |
| 4934 | case Builtins::kHalfLog: |
| 4935 | case Builtins::kNativeLog: |
| 4936 | return glsl::ExtInst::ExtInstLog; |
| 4937 | case Builtins::kLog2: |
| 4938 | case Builtins::kHalfLog2: |
| 4939 | case Builtins::kNativeLog2: |
| 4940 | return glsl::ExtInst::ExtInstLog2; |
| 4941 | case Builtins::kFabs: |
| 4942 | return glsl::ExtInst::ExtInstFAbs; |
| 4943 | case Builtins::kFma: |
| 4944 | return glsl::ExtInst::ExtInstFma; |
| 4945 | case Builtins::kFloor: |
| 4946 | return glsl::ExtInst::ExtInstFloor; |
| 4947 | case Builtins::kLdexp: |
| 4948 | return glsl::ExtInst::ExtInstLdexp; |
| 4949 | case Builtins::kPow: |
| 4950 | case Builtins::kPowr: |
| 4951 | case Builtins::kHalfPowr: |
| 4952 | case Builtins::kNativePowr: |
| 4953 | return glsl::ExtInst::ExtInstPow; |
James Price | 3855336 | 2020-09-03 18:30:40 -0400 | [diff] [blame] | 4954 | case Builtins::kRint: |
| 4955 | return glsl::ExtInst::ExtInstRoundEven; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4956 | case Builtins::kRound: |
| 4957 | return glsl::ExtInst::ExtInstRound; |
| 4958 | case Builtins::kSqrt: |
| 4959 | case Builtins::kHalfSqrt: |
| 4960 | case Builtins::kNativeSqrt: |
| 4961 | return glsl::ExtInst::ExtInstSqrt; |
| 4962 | case Builtins::kRsqrt: |
| 4963 | case Builtins::kHalfRsqrt: |
| 4964 | case Builtins::kNativeRsqrt: |
| 4965 | return glsl::ExtInst::ExtInstInverseSqrt; |
| 4966 | case Builtins::kTrunc: |
| 4967 | return glsl::ExtInst::ExtInstTrunc; |
| 4968 | case Builtins::kFrexp: |
| 4969 | return glsl::ExtInst::ExtInstFrexp; |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4970 | case Builtins::kClspvFract: |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4971 | case Builtins::kFract: |
| 4972 | return glsl::ExtInst::ExtInstFract; |
| 4973 | case Builtins::kSign: |
| 4974 | return glsl::ExtInst::ExtInstFSign; |
| 4975 | case Builtins::kLength: |
| 4976 | case Builtins::kFastLength: |
| 4977 | return glsl::ExtInst::ExtInstLength; |
| 4978 | case Builtins::kDistance: |
| 4979 | case Builtins::kFastDistance: |
| 4980 | return glsl::ExtInst::ExtInstDistance; |
| 4981 | case Builtins::kStep: |
| 4982 | return glsl::ExtInst::ExtInstStep; |
| 4983 | case Builtins::kSmoothstep: |
| 4984 | return glsl::ExtInst::ExtInstSmoothStep; |
| 4985 | case Builtins::kCross: |
| 4986 | return glsl::ExtInst::ExtInstCross; |
| 4987 | case Builtins::kNormalize: |
| 4988 | case Builtins::kFastNormalize: |
| 4989 | return glsl::ExtInst::ExtInstNormalize; |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4990 | case Builtins::kSpirvPack: |
| 4991 | return glsl::ExtInst::ExtInstPackHalf2x16; |
| 4992 | case Builtins::kSpirvUnpack: |
| 4993 | return glsl::ExtInst::ExtInstUnpackHalf2x16; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4994 | default: |
| 4995 | break; |
| 4996 | } |
| 4997 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 4998 | if (func_info.getName().find("llvm.fmuladd.") == 0) { |
| 4999 | return glsl::ExtInst::ExtInstFma; |
| 5000 | } |
| 5001 | return kGlslExtInstBad; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5002 | } |
| 5003 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 5004 | glsl::ExtInst SPIRVProducerPass::getIndirectExtInstEnum( |
| 5005 | const Builtins::FunctionInfo &func_info) { |
| 5006 | switch (func_info.getType()) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 5007 | case Builtins::kClz: |
| 5008 | return glsl::ExtInst::ExtInstFindUMsb; |
alan-baker | 2cecaa7 | 2020-11-05 14:05:20 -0500 | [diff] [blame] | 5009 | case Builtins::kCtz: |
| 5010 | return glsl::ExtInst::ExtInstFindILsb; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 5011 | case Builtins::kAcospi: |
| 5012 | return glsl::ExtInst::ExtInstAcos; |
| 5013 | case Builtins::kAsinpi: |
| 5014 | return glsl::ExtInst::ExtInstAsin; |
| 5015 | case Builtins::kAtanpi: |
| 5016 | return glsl::ExtInst::ExtInstAtan; |
| 5017 | case Builtins::kAtan2pi: |
| 5018 | return glsl::ExtInst::ExtInstAtan2; |
| 5019 | default: |
| 5020 | break; |
| 5021 | } |
| 5022 | return kGlslExtInstBad; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5023 | } |
| 5024 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 5025 | glsl::ExtInst SPIRVProducerPass::getDirectOrIndirectExtInstEnum( |
| 5026 | const Builtins::FunctionInfo &func_info) { |
| 5027 | auto direct = getExtInstEnum(func_info); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5028 | if (direct != kGlslExtInstBad) |
| 5029 | return direct; |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 5030 | return getIndirectExtInstEnum(func_info); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5031 | } |
| 5032 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5033 | void SPIRVProducerPass::WriteOneWord(uint32_t Word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 5034 | binaryOut->write(reinterpret_cast<const char *>(&Word), sizeof(uint32_t)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5035 | } |
| 5036 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5037 | void SPIRVProducerPass::WriteResultID(const SPIRVInstruction &Inst) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 5038 | WriteOneWord(Inst.getResultID().get()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5039 | } |
| 5040 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5041 | void SPIRVProducerPass::WriteWordCountAndOpcode(const SPIRVInstruction &Inst) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5042 | // High 16 bit : Word Count |
| 5043 | // Low 16 bit : Opcode |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5044 | uint32_t Word = Inst.getOpcode(); |
| 5045 | const uint32_t count = Inst.getWordCount(); |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 5046 | if (count > 65535) { |
| 5047 | errs() << "Word count limit of 65535 exceeded: " << count << "\n"; |
| 5048 | llvm_unreachable("Word count too high"); |
| 5049 | } |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5050 | Word |= Inst.getWordCount() << 16; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5051 | WriteOneWord(Word); |
| 5052 | } |
| 5053 | |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5054 | void SPIRVProducerPass::WriteOperand(const SPIRVOperand &Op) { |
| 5055 | SPIRVOperandType OpTy = Op.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5056 | switch (OpTy) { |
| 5057 | default: { |
| 5058 | llvm_unreachable("Unsupported SPIRV Operand Type???"); |
| 5059 | break; |
| 5060 | } |
| 5061 | case SPIRVOperandType::NUMBERID: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5062 | WriteOneWord(Op.getNumID()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5063 | break; |
| 5064 | } |
| 5065 | case SPIRVOperandType::LITERAL_STRING: { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5066 | std::string Str = Op.getLiteralStr(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5067 | const char *Data = Str.c_str(); |
| 5068 | size_t WordSize = Str.size() / 4; |
| 5069 | for (unsigned Idx = 0; Idx < WordSize; Idx++) { |
| 5070 | WriteOneWord(*reinterpret_cast<const uint32_t *>(&Data[4 * Idx])); |
| 5071 | } |
| 5072 | |
| 5073 | uint32_t Remainder = Str.size() % 4; |
| 5074 | uint32_t LastWord = 0; |
| 5075 | if (Remainder) { |
| 5076 | for (unsigned Idx = 0; Idx < Remainder; Idx++) { |
| 5077 | LastWord |= Data[4 * WordSize + Idx] << 8 * Idx; |
| 5078 | } |
| 5079 | } |
| 5080 | |
| 5081 | WriteOneWord(LastWord); |
| 5082 | break; |
| 5083 | } |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5084 | case SPIRVOperandType::LITERAL_WORD: { |
| 5085 | WriteOneWord(Op.getLiteralNum()[0]); |
| 5086 | break; |
| 5087 | } |
| 5088 | case SPIRVOperandType::LITERAL_DWORD: { |
| 5089 | WriteOneWord(Op.getLiteralNum()[0]); |
| 5090 | WriteOneWord(Op.getLiteralNum()[1]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5091 | break; |
| 5092 | } |
| 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | void SPIRVProducerPass::WriteSPIRVBinary() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 5097 | for (int i = 0; i < kSectionCount; ++i) { |
| 5098 | WriteSPIRVBinary(SPIRVSections[i]); |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | void SPIRVProducerPass::WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList) { |
SJW | 88ed5fe | 2020-05-11 12:40:57 -0500 | [diff] [blame] | 5103 | for (const auto &Inst : SPIRVInstList) { |
| 5104 | const auto &Ops = Inst.getOperands(); |
| 5105 | spv::Op Opcode = static_cast<spv::Op>(Inst.getOpcode()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5106 | |
| 5107 | switch (Opcode) { |
| 5108 | default: { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5109 | errs() << "Unsupported SPIR-V instruction opcode " << int(Opcode) << "\n"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5110 | llvm_unreachable("Unsupported SPIRV instruction"); |
| 5111 | break; |
| 5112 | } |
Marco Antognini | 68e5c51 | 2020-09-09 16:08:57 +0100 | [diff] [blame] | 5113 | case spv::OpUnreachable: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5114 | case spv::OpCapability: |
| 5115 | case spv::OpExtension: |
| 5116 | case spv::OpMemoryModel: |
| 5117 | case spv::OpEntryPoint: |
| 5118 | case spv::OpExecutionMode: |
| 5119 | case spv::OpSource: |
| 5120 | case spv::OpDecorate: |
| 5121 | case spv::OpMemberDecorate: |
| 5122 | case spv::OpBranch: |
| 5123 | case spv::OpBranchConditional: |
| 5124 | case spv::OpSelectionMerge: |
| 5125 | case spv::OpLoopMerge: |
| 5126 | case spv::OpStore: |
| 5127 | case spv::OpImageWrite: |
| 5128 | case spv::OpReturnValue: |
| 5129 | case spv::OpControlBarrier: |
| 5130 | case spv::OpMemoryBarrier: |
| 5131 | case spv::OpReturn: |
| 5132 | case spv::OpFunctionEnd: |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 5133 | case spv::OpCopyMemory: |
| 5134 | case spv::OpAtomicStore: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5135 | WriteWordCountAndOpcode(Inst); |
| 5136 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5137 | WriteOperand(Ops[i]); |
| 5138 | } |
| 5139 | break; |
| 5140 | } |
| 5141 | case spv::OpTypeBool: |
| 5142 | case spv::OpTypeVoid: |
| 5143 | case spv::OpTypeSampler: |
| 5144 | case spv::OpLabel: |
| 5145 | case spv::OpExtInstImport: |
| 5146 | case spv::OpTypePointer: |
| 5147 | case spv::OpTypeRuntimeArray: |
| 5148 | case spv::OpTypeStruct: |
| 5149 | case spv::OpTypeImage: |
| 5150 | case spv::OpTypeSampledImage: |
| 5151 | case spv::OpTypeInt: |
| 5152 | case spv::OpTypeFloat: |
| 5153 | case spv::OpTypeArray: |
| 5154 | case spv::OpTypeVector: |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 5155 | case spv::OpTypeFunction: |
| 5156 | case spv::OpString: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5157 | WriteWordCountAndOpcode(Inst); |
| 5158 | WriteResultID(Inst); |
| 5159 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5160 | WriteOperand(Ops[i]); |
| 5161 | } |
| 5162 | break; |
| 5163 | } |
| 5164 | case spv::OpFunction: |
| 5165 | case spv::OpFunctionParameter: |
| 5166 | case spv::OpAccessChain: |
| 5167 | case spv::OpPtrAccessChain: |
| 5168 | case spv::OpInBoundsAccessChain: |
| 5169 | case spv::OpUConvert: |
| 5170 | case spv::OpSConvert: |
| 5171 | case spv::OpConvertFToU: |
| 5172 | case spv::OpConvertFToS: |
| 5173 | case spv::OpConvertUToF: |
| 5174 | case spv::OpConvertSToF: |
| 5175 | case spv::OpFConvert: |
| 5176 | case spv::OpConvertPtrToU: |
| 5177 | case spv::OpConvertUToPtr: |
| 5178 | case spv::OpBitcast: |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 5179 | case spv::OpFNegate: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5180 | case spv::OpIAdd: |
alan-baker | a52b731 | 2020-10-26 08:58:51 -0400 | [diff] [blame] | 5181 | case spv::OpIAddCarry: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5182 | case spv::OpFAdd: |
| 5183 | case spv::OpISub: |
alan-baker | 3f1bf49 | 2020-11-05 09:07:36 -0500 | [diff] [blame] | 5184 | case spv::OpISubBorrow: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5185 | case spv::OpFSub: |
| 5186 | case spv::OpIMul: |
| 5187 | case spv::OpFMul: |
| 5188 | case spv::OpUDiv: |
| 5189 | case spv::OpSDiv: |
| 5190 | case spv::OpFDiv: |
| 5191 | case spv::OpUMod: |
| 5192 | case spv::OpSRem: |
| 5193 | case spv::OpFRem: |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 5194 | case spv::OpUMulExtended: |
| 5195 | case spv::OpSMulExtended: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5196 | case spv::OpBitwiseOr: |
| 5197 | case spv::OpBitwiseXor: |
| 5198 | case spv::OpBitwiseAnd: |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 5199 | case spv::OpNot: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5200 | case spv::OpShiftLeftLogical: |
| 5201 | case spv::OpShiftRightLogical: |
| 5202 | case spv::OpShiftRightArithmetic: |
| 5203 | case spv::OpBitCount: |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5204 | case spv::OpCompositeConstruct: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5205 | case spv::OpCompositeExtract: |
| 5206 | case spv::OpVectorExtractDynamic: |
| 5207 | case spv::OpCompositeInsert: |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 5208 | case spv::OpCopyObject: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5209 | case spv::OpVectorInsertDynamic: |
| 5210 | case spv::OpVectorShuffle: |
| 5211 | case spv::OpIEqual: |
| 5212 | case spv::OpINotEqual: |
| 5213 | case spv::OpUGreaterThan: |
| 5214 | case spv::OpUGreaterThanEqual: |
| 5215 | case spv::OpULessThan: |
| 5216 | case spv::OpULessThanEqual: |
| 5217 | case spv::OpSGreaterThan: |
| 5218 | case spv::OpSGreaterThanEqual: |
| 5219 | case spv::OpSLessThan: |
| 5220 | case spv::OpSLessThanEqual: |
| 5221 | case spv::OpFOrdEqual: |
| 5222 | case spv::OpFOrdGreaterThan: |
| 5223 | case spv::OpFOrdGreaterThanEqual: |
| 5224 | case spv::OpFOrdLessThan: |
| 5225 | case spv::OpFOrdLessThanEqual: |
| 5226 | case spv::OpFOrdNotEqual: |
| 5227 | case spv::OpFUnordEqual: |
| 5228 | case spv::OpFUnordGreaterThan: |
| 5229 | case spv::OpFUnordGreaterThanEqual: |
| 5230 | case spv::OpFUnordLessThan: |
| 5231 | case spv::OpFUnordLessThanEqual: |
| 5232 | case spv::OpFUnordNotEqual: |
| 5233 | case spv::OpExtInst: |
| 5234 | case spv::OpIsInf: |
| 5235 | case spv::OpIsNan: |
| 5236 | case spv::OpAny: |
| 5237 | case spv::OpAll: |
| 5238 | case spv::OpUndef: |
| 5239 | case spv::OpConstantNull: |
| 5240 | case spv::OpLogicalOr: |
| 5241 | case spv::OpLogicalAnd: |
| 5242 | case spv::OpLogicalNot: |
| 5243 | case spv::OpLogicalNotEqual: |
| 5244 | case spv::OpConstantComposite: |
| 5245 | case spv::OpSpecConstantComposite: |
| 5246 | case spv::OpConstantTrue: |
| 5247 | case spv::OpConstantFalse: |
| 5248 | case spv::OpConstant: |
| 5249 | case spv::OpSpecConstant: |
| 5250 | case spv::OpVariable: |
| 5251 | case spv::OpFunctionCall: |
| 5252 | case spv::OpSampledImage: |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 5253 | case spv::OpImageFetch: |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 5254 | case spv::OpImageRead: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5255 | case spv::OpImageSampleExplicitLod: |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5256 | case spv::OpImageQuerySize: |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5257 | case spv::OpImageQuerySizeLod: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5258 | case spv::OpSelect: |
| 5259 | case spv::OpPhi: |
| 5260 | case spv::OpLoad: |
alan-baker | 4986eff | 2020-10-29 13:38:00 -0400 | [diff] [blame] | 5261 | case spv::OpAtomicLoad: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5262 | case spv::OpAtomicIAdd: |
| 5263 | case spv::OpAtomicISub: |
| 5264 | case spv::OpAtomicExchange: |
| 5265 | case spv::OpAtomicIIncrement: |
| 5266 | case spv::OpAtomicIDecrement: |
| 5267 | case spv::OpAtomicCompareExchange: |
| 5268 | case spv::OpAtomicUMin: |
| 5269 | case spv::OpAtomicSMin: |
| 5270 | case spv::OpAtomicUMax: |
| 5271 | case spv::OpAtomicSMax: |
| 5272 | case spv::OpAtomicAnd: |
| 5273 | case spv::OpAtomicOr: |
| 5274 | case spv::OpAtomicXor: |
SJW | 806a5d8 | 2020-07-15 12:51:38 -0500 | [diff] [blame] | 5275 | case spv::OpDot: |
| 5276 | case spv::OpGroupNonUniformAll: |
| 5277 | case spv::OpGroupNonUniformAny: |
| 5278 | case spv::OpGroupNonUniformBroadcast: |
| 5279 | case spv::OpGroupNonUniformIAdd: |
| 5280 | case spv::OpGroupNonUniformFAdd: |
| 5281 | case spv::OpGroupNonUniformSMin: |
| 5282 | case spv::OpGroupNonUniformUMin: |
| 5283 | case spv::OpGroupNonUniformFMin: |
| 5284 | case spv::OpGroupNonUniformSMax: |
| 5285 | case spv::OpGroupNonUniformUMax: |
| 5286 | case spv::OpGroupNonUniformFMax: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5287 | WriteWordCountAndOpcode(Inst); |
| 5288 | WriteOperand(Ops[0]); |
| 5289 | WriteResultID(Inst); |
| 5290 | for (uint32_t i = 1; i < Ops.size(); i++) { |
| 5291 | WriteOperand(Ops[i]); |
| 5292 | } |
| 5293 | break; |
| 5294 | } |
| 5295 | } |
| 5296 | } |
| 5297 | } |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5298 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5299 | bool SPIRVProducerPass::IsTypeNullable(const Type *type) const { |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5300 | switch (type->getTypeID()) { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5301 | case Type::HalfTyID: |
| 5302 | case Type::FloatTyID: |
| 5303 | case Type::DoubleTyID: |
| 5304 | case Type::IntegerTyID: |
James Price | 59a1c75 | 2020-04-23 23:06:16 -0400 | [diff] [blame] | 5305 | case Type::FixedVectorTyID: |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5306 | return true; |
| 5307 | case Type::PointerTyID: { |
| 5308 | const PointerType *pointer_type = cast<PointerType>(type); |
| 5309 | if (pointer_type->getPointerAddressSpace() != |
| 5310 | AddressSpace::UniformConstant) { |
| 5311 | auto pointee_type = pointer_type->getPointerElementType(); |
| 5312 | if (pointee_type->isStructTy() && |
| 5313 | cast<StructType>(pointee_type)->isOpaque()) { |
| 5314 | // Images and samplers are not nullable. |
| 5315 | return false; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5316 | } |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5317 | } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5318 | return true; |
| 5319 | } |
| 5320 | case Type::ArrayTyID: |
alan-baker | 8eb435a | 2020-04-08 00:42:06 -0400 | [diff] [blame] | 5321 | return IsTypeNullable(type->getArrayElementType()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5322 | case Type::StructTyID: { |
| 5323 | const StructType *struct_type = cast<StructType>(type); |
| 5324 | // Images and samplers are not nullable. |
| 5325 | if (struct_type->isOpaque()) |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5326 | return false; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5327 | for (const auto element : struct_type->elements()) { |
| 5328 | if (!IsTypeNullable(element)) |
| 5329 | return false; |
| 5330 | } |
| 5331 | return true; |
| 5332 | } |
| 5333 | default: |
| 5334 | return false; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 5335 | } |
| 5336 | } |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5337 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5338 | void SPIRVProducerPass::PopulateUBOTypeMaps() { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5339 | if (auto *offsets_md = |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5340 | module->getNamedMetadata(clspv::RemappedTypeOffsetMetadataName())) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5341 | // Metdata is stored as key-value pair operands. The first element of each |
| 5342 | // operand is the type and the second is a vector of offsets. |
| 5343 | for (const auto *operand : offsets_md->operands()) { |
| 5344 | const auto *pair = cast<MDTuple>(operand); |
| 5345 | auto *type = |
| 5346 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 5347 | const auto *offset_vector = cast<MDTuple>(pair->getOperand(1)); |
| 5348 | std::vector<uint32_t> offsets; |
| 5349 | for (const Metadata *offset_md : offset_vector->operands()) { |
| 5350 | const auto *constant_md = cast<ConstantAsMetadata>(offset_md); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5351 | offsets.push_back(static_cast<uint32_t>( |
| 5352 | cast<ConstantInt>(constant_md->getValue())->getZExtValue())); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5353 | } |
| 5354 | RemappedUBOTypeOffsets.insert(std::make_pair(type, offsets)); |
| 5355 | } |
| 5356 | } |
| 5357 | |
| 5358 | if (auto *sizes_md = |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5359 | module->getNamedMetadata(clspv::RemappedTypeSizesMetadataName())) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5360 | // Metadata is stored as key-value pair operands. The first element of each |
| 5361 | // operand is the type and the second is a triple of sizes: type size in |
| 5362 | // bits, store size and alloc size. |
| 5363 | for (const auto *operand : sizes_md->operands()) { |
| 5364 | const auto *pair = cast<MDTuple>(operand); |
| 5365 | auto *type = |
| 5366 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 5367 | const auto *size_triple = cast<MDTuple>(pair->getOperand(1)); |
| 5368 | uint64_t type_size_in_bits = |
| 5369 | cast<ConstantInt>( |
| 5370 | cast<ConstantAsMetadata>(size_triple->getOperand(0))->getValue()) |
| 5371 | ->getZExtValue(); |
| 5372 | uint64_t type_store_size = |
| 5373 | cast<ConstantInt>( |
| 5374 | cast<ConstantAsMetadata>(size_triple->getOperand(1))->getValue()) |
| 5375 | ->getZExtValue(); |
| 5376 | uint64_t type_alloc_size = |
| 5377 | cast<ConstantInt>( |
| 5378 | cast<ConstantAsMetadata>(size_triple->getOperand(2))->getValue()) |
| 5379 | ->getZExtValue(); |
| 5380 | RemappedUBOTypeSizes.insert(std::make_pair( |
| 5381 | type, std::make_tuple(type_size_in_bits, type_store_size, |
| 5382 | type_alloc_size))); |
| 5383 | } |
| 5384 | } |
| 5385 | } |
| 5386 | |
| 5387 | uint64_t SPIRVProducerPass::GetTypeSizeInBits(Type *type, |
| 5388 | const DataLayout &DL) { |
| 5389 | auto iter = RemappedUBOTypeSizes.find(type); |
| 5390 | if (iter != RemappedUBOTypeSizes.end()) { |
| 5391 | return std::get<0>(iter->second); |
| 5392 | } |
| 5393 | |
| 5394 | return DL.getTypeSizeInBits(type); |
| 5395 | } |
| 5396 | |
| 5397 | uint64_t SPIRVProducerPass::GetTypeStoreSize(Type *type, const DataLayout &DL) { |
| 5398 | auto iter = RemappedUBOTypeSizes.find(type); |
| 5399 | if (iter != RemappedUBOTypeSizes.end()) { |
| 5400 | return std::get<1>(iter->second); |
| 5401 | } |
| 5402 | |
| 5403 | return DL.getTypeStoreSize(type); |
| 5404 | } |
| 5405 | |
| 5406 | uint64_t SPIRVProducerPass::GetTypeAllocSize(Type *type, const DataLayout &DL) { |
| 5407 | auto iter = RemappedUBOTypeSizes.find(type); |
| 5408 | if (iter != RemappedUBOTypeSizes.end()) { |
| 5409 | return std::get<2>(iter->second); |
| 5410 | } |
| 5411 | |
| 5412 | return DL.getTypeAllocSize(type); |
| 5413 | } |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5414 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 5415 | uint32_t SPIRVProducerPass::GetExplicitLayoutStructMemberOffset( |
| 5416 | StructType *type, unsigned member, const DataLayout &DL) { |
| 5417 | const auto StructLayout = DL.getStructLayout(type); |
| 5418 | // Search for the correct offsets if this type was remapped. |
| 5419 | std::vector<uint32_t> *offsets = nullptr; |
| 5420 | auto iter = RemappedUBOTypeOffsets.find(type); |
| 5421 | if (iter != RemappedUBOTypeOffsets.end()) { |
| 5422 | offsets = &iter->second; |
| 5423 | } |
| 5424 | auto ByteOffset = |
| 5425 | static_cast<uint32_t>(StructLayout->getElementOffset(member)); |
| 5426 | if (offsets) { |
| 5427 | ByteOffset = (*offsets)[member]; |
| 5428 | } |
| 5429 | |
| 5430 | return ByteOffset; |
| 5431 | } |
| 5432 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 5433 | void SPIRVProducerPass::setVariablePointersCapabilities( |
| 5434 | unsigned address_space) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5435 | if (GetStorageClass(address_space) == spv::StorageClassStorageBuffer) { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 5436 | setVariablePointersStorageBuffer(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5437 | } else { |
SJW | 01901d9 | 2020-05-21 08:58:31 -0500 | [diff] [blame] | 5438 | setVariablePointers(); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5439 | } |
| 5440 | } |
| 5441 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 5442 | Value *SPIRVProducerPass::GetBasePointer(Value *v) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5443 | if (auto *gep = dyn_cast<GetElementPtrInst>(v)) { |
| 5444 | return GetBasePointer(gep->getPointerOperand()); |
| 5445 | } |
| 5446 | |
| 5447 | // Conservatively return |v|. |
| 5448 | return v; |
| 5449 | } |
| 5450 | |
| 5451 | bool SPIRVProducerPass::sameResource(Value *lhs, Value *rhs) const { |
| 5452 | if (auto *lhs_call = dyn_cast<CallInst>(lhs)) { |
| 5453 | if (auto *rhs_call = dyn_cast<CallInst>(rhs)) { |
alan-baker | 7506abb | 2020-09-10 15:02:55 -0400 | [diff] [blame] | 5454 | const auto &lhs_func_info = |
| 5455 | Builtins::Lookup(lhs_call->getCalledFunction()); |
| 5456 | const auto &rhs_func_info = |
| 5457 | Builtins::Lookup(rhs_call->getCalledFunction()); |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 5458 | if (lhs_func_info.getType() == Builtins::kClspvResource && |
| 5459 | rhs_func_info.getType() == Builtins::kClspvResource) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5460 | // For resource accessors, match descriptor set and binding. |
| 5461 | if (lhs_call->getOperand(0) == rhs_call->getOperand(0) && |
| 5462 | lhs_call->getOperand(1) == rhs_call->getOperand(1)) |
| 5463 | return true; |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 5464 | } else if (lhs_func_info.getType() == Builtins::kClspvLocal && |
| 5465 | rhs_func_info.getType() == Builtins::kClspvLocal) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5466 | // For workgroup resources, match spec id. |
| 5467 | if (lhs_call->getOperand(0) == rhs_call->getOperand(0)) |
| 5468 | return true; |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | return false; |
| 5474 | } |
| 5475 | |
| 5476 | bool SPIRVProducerPass::selectFromSameObject(Instruction *inst) { |
| 5477 | assert(inst->getType()->isPointerTy()); |
| 5478 | assert(GetStorageClass(inst->getType()->getPointerAddressSpace()) == |
| 5479 | spv::StorageClassStorageBuffer); |
| 5480 | const bool hack_undef = clspv::Option::HackUndef(); |
| 5481 | if (auto *select = dyn_cast<SelectInst>(inst)) { |
| 5482 | auto *true_base = GetBasePointer(select->getTrueValue()); |
| 5483 | auto *false_base = GetBasePointer(select->getFalseValue()); |
| 5484 | |
| 5485 | if (true_base == false_base) |
| 5486 | return true; |
| 5487 | |
| 5488 | // If either the true or false operand is a null, then we satisfy the same |
| 5489 | // object constraint. |
| 5490 | if (auto *true_cst = dyn_cast<Constant>(true_base)) { |
| 5491 | if (true_cst->isNullValue() || (hack_undef && isa<UndefValue>(true_base))) |
| 5492 | return true; |
| 5493 | } |
| 5494 | |
| 5495 | if (auto *false_cst = dyn_cast<Constant>(false_base)) { |
| 5496 | if (false_cst->isNullValue() || |
| 5497 | (hack_undef && isa<UndefValue>(false_base))) |
| 5498 | return true; |
| 5499 | } |
| 5500 | |
| 5501 | if (sameResource(true_base, false_base)) |
| 5502 | return true; |
| 5503 | } else if (auto *phi = dyn_cast<PHINode>(inst)) { |
| 5504 | Value *value = nullptr; |
| 5505 | bool ok = true; |
| 5506 | for (unsigned i = 0; ok && i != phi->getNumIncomingValues(); ++i) { |
| 5507 | auto *base = GetBasePointer(phi->getIncomingValue(i)); |
| 5508 | // Null values satisfy the constraint of selecting of selecting from the |
| 5509 | // same object. |
| 5510 | if (!value) { |
| 5511 | if (auto *cst = dyn_cast<Constant>(base)) { |
| 5512 | if (!cst->isNullValue() && !(hack_undef && isa<UndefValue>(base))) |
| 5513 | value = base; |
| 5514 | } else { |
| 5515 | value = base; |
| 5516 | } |
| 5517 | } else if (base != value) { |
| 5518 | if (auto *base_cst = dyn_cast<Constant>(base)) { |
| 5519 | if (base_cst->isNullValue() || (hack_undef && isa<UndefValue>(base))) |
| 5520 | continue; |
| 5521 | } |
| 5522 | |
| 5523 | if (sameResource(value, base)) |
| 5524 | continue; |
| 5525 | |
| 5526 | // Values don't represent the same base. |
| 5527 | ok = false; |
| 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | return ok; |
| 5532 | } |
| 5533 | |
| 5534 | // Conservatively return false. |
| 5535 | return false; |
| 5536 | } |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 5537 | |
| 5538 | bool SPIRVProducerPass::CalledWithCoherentResource(Argument &Arg) { |
| 5539 | if (!Arg.getType()->isPointerTy() || |
| 5540 | Arg.getType()->getPointerAddressSpace() != clspv::AddressSpace::Global) { |
| 5541 | // Only SSBOs need to be annotated as coherent. |
| 5542 | return false; |
| 5543 | } |
| 5544 | |
| 5545 | DenseSet<Value *> visited; |
| 5546 | std::vector<Value *> stack; |
| 5547 | for (auto *U : Arg.getParent()->users()) { |
| 5548 | if (auto *call = dyn_cast<CallInst>(U)) { |
| 5549 | stack.push_back(call->getOperand(Arg.getArgNo())); |
| 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | while (!stack.empty()) { |
| 5554 | Value *v = stack.back(); |
| 5555 | stack.pop_back(); |
| 5556 | |
| 5557 | if (!visited.insert(v).second) |
| 5558 | continue; |
| 5559 | |
| 5560 | auto *resource_call = dyn_cast<CallInst>(v); |
| 5561 | if (resource_call && |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 5562 | Builtins::Lookup(resource_call->getCalledFunction()).getType() == |
| 5563 | Builtins::kClspvResource) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 5564 | // If this is a resource accessor function, check if the coherent operand |
| 5565 | // is set. |
| 5566 | const auto coherent = |
| 5567 | unsigned(dyn_cast<ConstantInt>(resource_call->getArgOperand(5)) |
| 5568 | ->getZExtValue()); |
| 5569 | if (coherent == 1) |
| 5570 | return true; |
| 5571 | } else if (auto *arg = dyn_cast<Argument>(v)) { |
| 5572 | // If this is a function argument, trace through its callers. |
alan-baker | e98f3f9 | 2019-04-08 15:06:36 -0400 | [diff] [blame] | 5573 | for (auto U : arg->getParent()->users()) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 5574 | if (auto *call = dyn_cast<CallInst>(U)) { |
| 5575 | stack.push_back(call->getOperand(arg->getArgNo())); |
| 5576 | } |
| 5577 | } |
| 5578 | } else if (auto *user = dyn_cast<User>(v)) { |
| 5579 | // If this is a user, traverse all operands that could lead to resource |
| 5580 | // variables. |
| 5581 | for (unsigned i = 0; i != user->getNumOperands(); ++i) { |
| 5582 | Value *operand = user->getOperand(i); |
| 5583 | if (operand->getType()->isPointerTy() && |
| 5584 | operand->getType()->getPointerAddressSpace() == |
| 5585 | clspv::AddressSpace::Global) { |
| 5586 | stack.push_back(operand); |
| 5587 | } |
| 5588 | } |
| 5589 | } |
| 5590 | } |
| 5591 | |
| 5592 | // No coherent resource variables encountered. |
| 5593 | return false; |
| 5594 | } |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5595 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5596 | void SPIRVProducerPass::PopulateStructuredCFGMaps() { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5597 | // First, track loop merges and continues. |
| 5598 | DenseSet<BasicBlock *> LoopMergesAndContinues; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5599 | for (auto &F : *module) { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5600 | if (F.isDeclaration()) |
| 5601 | continue; |
| 5602 | |
| 5603 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); |
| 5604 | const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>(F).getLoopInfo(); |
| 5605 | std::deque<BasicBlock *> order; |
| 5606 | DenseSet<BasicBlock *> visited; |
| 5607 | clspv::ComputeStructuredOrder(&*F.begin(), &DT, LI, &order, &visited); |
| 5608 | |
| 5609 | for (auto BB : order) { |
| 5610 | auto terminator = BB->getTerminator(); |
| 5611 | auto branch = dyn_cast<BranchInst>(terminator); |
| 5612 | if (LI.isLoopHeader(BB)) { |
| 5613 | auto L = LI.getLoopFor(BB); |
| 5614 | BasicBlock *ContinueBB = nullptr; |
| 5615 | BasicBlock *MergeBB = nullptr; |
| 5616 | |
| 5617 | MergeBB = L->getExitBlock(); |
| 5618 | if (!MergeBB) { |
| 5619 | // StructurizeCFG pass converts CFG into triangle shape and the cfg |
| 5620 | // has regions with single entry/exit. As a result, loop should not |
| 5621 | // have multiple exits. |
| 5622 | llvm_unreachable("Loop has multiple exits???"); |
| 5623 | } |
| 5624 | |
| 5625 | if (L->isLoopLatch(BB)) { |
| 5626 | ContinueBB = BB; |
| 5627 | } else { |
| 5628 | // From SPIR-V spec 2.11, Continue Target must dominate that back-edge |
| 5629 | // block. |
| 5630 | BasicBlock *Header = L->getHeader(); |
| 5631 | BasicBlock *Latch = L->getLoopLatch(); |
| 5632 | for (auto *loop_block : L->blocks()) { |
| 5633 | if (loop_block == Header) { |
| 5634 | continue; |
| 5635 | } |
| 5636 | |
| 5637 | // Check whether block dominates block with back-edge. |
| 5638 | // The loop latch is the single block with a back-edge. If it was |
| 5639 | // possible, StructurizeCFG made the loop conform to this |
| 5640 | // requirement, otherwise |Latch| is a nullptr. |
| 5641 | if (DT.dominates(loop_block, Latch)) { |
| 5642 | ContinueBB = loop_block; |
| 5643 | } |
| 5644 | } |
| 5645 | |
| 5646 | if (!ContinueBB) { |
| 5647 | llvm_unreachable("Wrong continue block from loop"); |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | // Record the continue and merge blocks. |
| 5652 | MergeBlocks[BB] = MergeBB; |
| 5653 | ContinueBlocks[BB] = ContinueBB; |
| 5654 | LoopMergesAndContinues.insert(MergeBB); |
| 5655 | LoopMergesAndContinues.insert(ContinueBB); |
| 5656 | } else if (branch && branch->isConditional()) { |
| 5657 | auto L = LI.getLoopFor(BB); |
| 5658 | bool HasBackedge = false; |
| 5659 | while (L && !HasBackedge) { |
| 5660 | if (L->isLoopLatch(BB)) { |
| 5661 | HasBackedge = true; |
| 5662 | } |
| 5663 | L = L->getParentLoop(); |
| 5664 | } |
| 5665 | |
| 5666 | if (!HasBackedge) { |
| 5667 | // Only need a merge if the branch doesn't include a loop break or |
| 5668 | // continue. |
| 5669 | auto true_bb = branch->getSuccessor(0); |
| 5670 | auto false_bb = branch->getSuccessor(1); |
| 5671 | if (!LoopMergesAndContinues.count(true_bb) && |
| 5672 | !LoopMergesAndContinues.count(false_bb)) { |
| 5673 | // StructurizeCFG pass already manipulated CFG. Just use false block |
| 5674 | // of branch instruction as merge block. |
| 5675 | MergeBlocks[BB] = false_bb; |
| 5676 | } |
| 5677 | } |
| 5678 | } |
| 5679 | } |
| 5680 | } |
| 5681 | } |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 5682 | |
| 5683 | SPIRVID SPIRVProducerPass::getReflectionImport() { |
| 5684 | if (!ReflectionID.isValid()) { |
| 5685 | addSPIRVInst<kExtensions>(spv::OpExtension, "SPV_KHR_non_semantic_info"); |
| 5686 | ReflectionID = addSPIRVInst<kImports>(spv::OpExtInstImport, |
| 5687 | "NonSemantic.ClspvReflection.1"); |
| 5688 | } |
| 5689 | return ReflectionID; |
| 5690 | } |
| 5691 | |
| 5692 | void SPIRVProducerPass::GenerateReflection() { |
| 5693 | GenerateKernelReflection(); |
| 5694 | GeneratePushConstantReflection(); |
| 5695 | GenerateSpecConstantReflection(); |
| 5696 | } |
| 5697 | |
| 5698 | void SPIRVProducerPass::GeneratePushConstantReflection() { |
| 5699 | if (auto GV = module->getGlobalVariable(clspv::PushConstantsVariableName())) { |
| 5700 | auto const &DL = module->getDataLayout(); |
| 5701 | auto MD = GV->getMetadata(clspv::PushConstantsMetadataName()); |
| 5702 | auto STy = cast<StructType>(GV->getValueType()); |
| 5703 | |
| 5704 | for (unsigned i = 0; i < STy->getNumElements(); i++) { |
| 5705 | auto pc = static_cast<clspv::PushConstant>( |
| 5706 | mdconst::extract<ConstantInt>(MD->getOperand(i))->getZExtValue()); |
| 5707 | if (pc == PushConstant::KernelArgument) |
| 5708 | continue; |
| 5709 | |
| 5710 | auto memberType = STy->getElementType(i); |
| 5711 | auto offset = GetExplicitLayoutStructMemberOffset(STy, i, DL); |
| 5712 | unsigned previousOffset = 0; |
| 5713 | if (i > 0) { |
| 5714 | previousOffset = GetExplicitLayoutStructMemberOffset(STy, i - 1, DL); |
| 5715 | } |
| 5716 | auto size = static_cast<uint32_t>(GetTypeSizeInBits(memberType, DL)) / 8; |
| 5717 | assert(isValidExplicitLayout(*module, STy, i, |
| 5718 | spv::StorageClassPushConstant, offset, |
| 5719 | previousOffset)); |
| 5720 | |
| 5721 | reflection::ExtInst pc_inst = reflection::ExtInstMax; |
| 5722 | switch (pc) { |
| 5723 | case PushConstant::GlobalOffset: |
| 5724 | pc_inst = reflection::ExtInstPushConstantGlobalOffset; |
| 5725 | break; |
| 5726 | case PushConstant::EnqueuedLocalSize: |
| 5727 | pc_inst = reflection::ExtInstPushConstantEnqueuedLocalSize; |
| 5728 | break; |
| 5729 | case PushConstant::GlobalSize: |
| 5730 | pc_inst = reflection::ExtInstPushConstantGlobalSize; |
| 5731 | break; |
| 5732 | case PushConstant::RegionOffset: |
| 5733 | pc_inst = reflection::ExtInstPushConstantRegionOffset; |
| 5734 | break; |
| 5735 | case PushConstant::NumWorkgroups: |
| 5736 | pc_inst = reflection::ExtInstPushConstantNumWorkgroups; |
| 5737 | break; |
| 5738 | case PushConstant::RegionGroupOffset: |
| 5739 | pc_inst = reflection::ExtInstPushConstantRegionGroupOffset; |
| 5740 | break; |
| 5741 | default: |
| 5742 | llvm_unreachable("Unhandled push constant"); |
| 5743 | break; |
| 5744 | } |
| 5745 | |
| 5746 | auto import_id = getReflectionImport(); |
| 5747 | SPIRVOperandVec Ops; |
| 5748 | Ops << getSPIRVType(Type::getVoidTy(module->getContext())) << import_id |
| 5749 | << pc_inst << getSPIRVInt32Constant(offset) |
| 5750 | << getSPIRVInt32Constant(size); |
| 5751 | addSPIRVInst(spv::OpExtInst, Ops); |
| 5752 | } |
| 5753 | } |
| 5754 | } |
| 5755 | |
| 5756 | void SPIRVProducerPass::GenerateSpecConstantReflection() { |
| 5757 | const uint32_t kMax = std::numeric_limits<uint32_t>::max(); |
| 5758 | uint32_t wgsize_id[3] = {kMax, kMax, kMax}; |
| 5759 | uint32_t global_offset_id[3] = {kMax, kMax, kMax}; |
| 5760 | uint32_t work_dim_id = kMax; |
| 5761 | for (auto pair : clspv::GetSpecConstants(module)) { |
| 5762 | auto kind = pair.first; |
| 5763 | auto id = pair.second; |
| 5764 | |
| 5765 | // Local memory size is only used for kernel arguments. |
| 5766 | if (kind == SpecConstant::kLocalMemorySize) |
| 5767 | continue; |
| 5768 | |
| 5769 | switch (kind) { |
| 5770 | case SpecConstant::kWorkgroupSizeX: |
| 5771 | wgsize_id[0] = id; |
| 5772 | break; |
| 5773 | case SpecConstant::kWorkgroupSizeY: |
| 5774 | wgsize_id[1] = id; |
| 5775 | break; |
| 5776 | case SpecConstant::kWorkgroupSizeZ: |
| 5777 | wgsize_id[2] = id; |
| 5778 | break; |
| 5779 | case SpecConstant::kGlobalOffsetX: |
| 5780 | global_offset_id[0] = id; |
| 5781 | break; |
| 5782 | case SpecConstant::kGlobalOffsetY: |
| 5783 | global_offset_id[1] = id; |
| 5784 | break; |
| 5785 | case SpecConstant::kGlobalOffsetZ: |
| 5786 | global_offset_id[2] = id; |
| 5787 | break; |
| 5788 | case SpecConstant::kWorkDim: |
| 5789 | work_dim_id = id; |
| 5790 | break; |
| 5791 | default: |
| 5792 | llvm_unreachable("Unhandled spec constant"); |
| 5793 | } |
| 5794 | } |
| 5795 | |
| 5796 | auto import_id = getReflectionImport(); |
| 5797 | auto void_id = getSPIRVType(Type::getVoidTy(module->getContext())); |
| 5798 | SPIRVOperandVec Ops; |
| 5799 | if (wgsize_id[0] != kMax) { |
| 5800 | assert(wgsize_id[1] != kMax); |
| 5801 | assert(wgsize_id[2] != kMax); |
| 5802 | Ops.clear(); |
| 5803 | Ops << void_id << import_id << reflection::ExtInstSpecConstantWorkgroupSize |
| 5804 | << getSPIRVInt32Constant(wgsize_id[0]) |
| 5805 | << getSPIRVInt32Constant(wgsize_id[1]) |
| 5806 | << getSPIRVInt32Constant(wgsize_id[2]); |
| 5807 | addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
| 5808 | } |
| 5809 | if (global_offset_id[0] != kMax) { |
| 5810 | assert(global_offset_id[1] != kMax); |
| 5811 | assert(global_offset_id[2] != kMax); |
| 5812 | Ops.clear(); |
| 5813 | Ops << void_id << import_id << reflection::ExtInstSpecConstantGlobalOffset |
| 5814 | << getSPIRVInt32Constant(global_offset_id[0]) |
| 5815 | << getSPIRVInt32Constant(global_offset_id[1]) |
| 5816 | << getSPIRVInt32Constant(global_offset_id[2]); |
| 5817 | addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
| 5818 | } |
| 5819 | if (work_dim_id != kMax) { |
| 5820 | Ops.clear(); |
| 5821 | Ops << void_id << import_id << reflection::ExtInstSpecConstantWorkDim |
| 5822 | << getSPIRVInt32Constant(work_dim_id); |
| 5823 | addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
| 5824 | } |
| 5825 | } |
| 5826 | |
| 5827 | void SPIRVProducerPass::GenerateKernelReflection() { |
| 5828 | const auto &DL = module->getDataLayout(); |
| 5829 | auto import_id = getReflectionImport(); |
| 5830 | auto void_id = getSPIRVType(Type::getVoidTy(module->getContext())); |
| 5831 | |
| 5832 | for (auto &F : *module) { |
| 5833 | if (F.isDeclaration() || F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 5834 | continue; |
| 5835 | } |
| 5836 | |
| 5837 | // OpString for the kernel name. |
| 5838 | auto kernel_name = |
| 5839 | addSPIRVInst<kDebug>(spv::OpString, F.getName().str().c_str()); |
| 5840 | |
| 5841 | // Kernel declaration |
| 5842 | // Ops[0] = void type |
| 5843 | // Ops[1] = reflection ext import |
| 5844 | // Ops[2] = function id |
| 5845 | // Ops[3] = kernel name |
| 5846 | SPIRVOperandVec Ops; |
| 5847 | Ops << void_id << import_id << reflection::ExtInstKernel << ValueMap[&F] |
| 5848 | << kernel_name; |
| 5849 | auto kernel_decl = addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
| 5850 | |
| 5851 | // Generate the required workgroup size property if it was specified. |
| 5852 | if (const MDNode *MD = F.getMetadata("reqd_work_group_size")) { |
| 5853 | uint32_t CurXDimCst = static_cast<uint32_t>( |
| 5854 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 5855 | uint32_t CurYDimCst = static_cast<uint32_t>( |
| 5856 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 5857 | uint32_t CurZDimCst = static_cast<uint32_t>( |
| 5858 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 5859 | |
| 5860 | Ops.clear(); |
| 5861 | Ops << void_id << import_id |
| 5862 | << reflection::ExtInstPropertyRequiredWorkgroupSize << kernel_decl |
| 5863 | << getSPIRVInt32Constant(CurXDimCst) |
| 5864 | << getSPIRVInt32Constant(CurYDimCst) |
| 5865 | << getSPIRVInt32Constant(CurZDimCst); |
| 5866 | addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
| 5867 | } |
| 5868 | |
| 5869 | auto &resource_var_at_index = FunctionToResourceVarsMap[&F]; |
| 5870 | auto *func_ty = F.getFunctionType(); |
| 5871 | |
| 5872 | // If we've clustered POD arguments, then argument details are in metadata. |
| 5873 | // If an argument maps to a resource variable, then get descriptor set and |
| 5874 | // binding from the resource variable. Other info comes from the metadata. |
| 5875 | const auto *arg_map = F.getMetadata(clspv::KernelArgMapMetadataName()); |
| 5876 | auto local_spec_id_md = |
| 5877 | module->getNamedMetadata(clspv::LocalSpecIdMetadataName()); |
| 5878 | if (arg_map) { |
| 5879 | for (const auto &arg : arg_map->operands()) { |
| 5880 | const MDNode *arg_node = dyn_cast<MDNode>(arg.get()); |
| 5881 | assert(arg_node->getNumOperands() == 6); |
| 5882 | const auto name = |
| 5883 | dyn_cast<MDString>(arg_node->getOperand(0))->getString(); |
| 5884 | const auto old_index = |
| 5885 | dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue(); |
| 5886 | // Remapped argument index |
| 5887 | const int new_index = static_cast<int>( |
| 5888 | dyn_extract<ConstantInt>(arg_node->getOperand(2))->getSExtValue()); |
| 5889 | const auto offset = |
| 5890 | dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue(); |
| 5891 | const auto size = |
| 5892 | dyn_extract<ConstantInt>(arg_node->getOperand(4))->getZExtValue(); |
| 5893 | const auto argKind = clspv::GetArgKindFromName( |
| 5894 | dyn_cast<MDString>(arg_node->getOperand(5))->getString().str()); |
| 5895 | |
| 5896 | // If this is a local memory argument, find the right spec id for this |
| 5897 | // argument. |
| 5898 | int64_t spec_id = -1; |
| 5899 | if (argKind == clspv::ArgKind::Local) { |
| 5900 | for (auto spec_id_arg : local_spec_id_md->operands()) { |
| 5901 | if ((&F == dyn_cast<Function>( |
| 5902 | dyn_cast<ValueAsMetadata>(spec_id_arg->getOperand(0)) |
| 5903 | ->getValue())) && |
| 5904 | (static_cast<uint64_t>(new_index) == |
| 5905 | mdconst::extract<ConstantInt>(spec_id_arg->getOperand(1)) |
| 5906 | ->getZExtValue())) { |
| 5907 | spec_id = |
| 5908 | mdconst::extract<ConstantInt>(spec_id_arg->getOperand(2)) |
| 5909 | ->getSExtValue(); |
| 5910 | break; |
| 5911 | } |
| 5912 | } |
| 5913 | } |
| 5914 | |
| 5915 | // Generate the specific argument instruction. |
| 5916 | const uint32_t ordinal = static_cast<uint32_t>(old_index); |
| 5917 | const uint32_t arg_offset = static_cast<uint32_t>(offset); |
| 5918 | const uint32_t arg_size = static_cast<uint32_t>(size); |
| 5919 | uint32_t elem_size = 0; |
| 5920 | uint32_t descriptor_set = 0; |
| 5921 | uint32_t binding = 0; |
| 5922 | if (spec_id > 0) { |
| 5923 | elem_size = static_cast<uint32_t>( |
| 5924 | GetTypeAllocSize(func_ty->getParamType(unsigned(new_index)) |
| 5925 | ->getPointerElementType(), |
| 5926 | DL)); |
| 5927 | } else if (new_index >= 0) { |
| 5928 | auto *info = resource_var_at_index[new_index]; |
| 5929 | assert(info); |
| 5930 | descriptor_set = info->descriptor_set; |
| 5931 | binding = info->binding; |
| 5932 | } |
| 5933 | AddArgumentReflection(kernel_decl, name.str(), argKind, ordinal, |
| 5934 | descriptor_set, binding, arg_offset, arg_size, |
| 5935 | static_cast<uint32_t>(spec_id), elem_size); |
| 5936 | } |
| 5937 | } else { |
| 5938 | // There is no argument map. |
| 5939 | // Take descriptor info from the resource variable calls. |
| 5940 | // Take argument name and size from the arguments list. |
| 5941 | |
| 5942 | SmallVector<Argument *, 4> arguments; |
| 5943 | for (auto &arg : F.args()) { |
| 5944 | arguments.push_back(&arg); |
| 5945 | } |
| 5946 | |
| 5947 | unsigned arg_index = 0; |
| 5948 | for (auto *info : resource_var_at_index) { |
| 5949 | if (info) { |
| 5950 | auto arg = arguments[arg_index]; |
| 5951 | unsigned arg_size = 0; |
| 5952 | if (info->arg_kind == clspv::ArgKind::Pod || |
| 5953 | info->arg_kind == clspv::ArgKind::PodUBO || |
| 5954 | info->arg_kind == clspv::ArgKind::PodPushConstant) { |
| 5955 | arg_size = |
| 5956 | static_cast<uint32_t>(DL.getTypeStoreSize(arg->getType())); |
| 5957 | } |
| 5958 | |
| 5959 | // Local pointer arguments are unused in this case. |
| 5960 | // offset, spec_id and elem_size always 0. |
| 5961 | AddArgumentReflection(kernel_decl, arg->getName().str(), |
| 5962 | info->arg_kind, arg_index, info->descriptor_set, |
| 5963 | info->binding, 0, arg_size, 0, 0); |
| 5964 | } |
| 5965 | arg_index++; |
| 5966 | } |
| 5967 | // Generate mappings for pointer-to-local arguments. |
| 5968 | for (arg_index = 0; arg_index < arguments.size(); ++arg_index) { |
| 5969 | Argument *arg = arguments[arg_index]; |
| 5970 | auto where = LocalArgSpecIds.find(arg); |
| 5971 | if (where != LocalArgSpecIds.end()) { |
| 5972 | auto &local_arg_info = LocalSpecIdInfoMap[where->second]; |
| 5973 | |
| 5974 | // descriptor_set, binding, offset and size are always 0. |
| 5975 | AddArgumentReflection(kernel_decl, arg->getName().str(), |
| 5976 | ArgKind::Local, arg_index, 0, 0, 0, 0, |
| 5977 | static_cast<uint32_t>(local_arg_info.spec_id), |
| 5978 | static_cast<uint32_t>(GetTypeAllocSize( |
| 5979 | local_arg_info.elem_type, DL))); |
| 5980 | } |
| 5981 | } |
| 5982 | } |
| 5983 | } |
| 5984 | } |
| 5985 | |
| 5986 | void SPIRVProducerPass::AddArgumentReflection( |
| 5987 | SPIRVID kernel_decl, const std::string &name, clspv::ArgKind arg_kind, |
| 5988 | uint32_t ordinal, uint32_t descriptor_set, uint32_t binding, |
| 5989 | uint32_t offset, uint32_t size, uint32_t spec_id, uint32_t elem_size) { |
| 5990 | // Generate ArgumentInfo for this argument. |
| 5991 | // TODO: generate remaining optional operands. |
| 5992 | auto import_id = getReflectionImport(); |
| 5993 | auto arg_name = addSPIRVInst<kDebug>(spv::OpString, name.c_str()); |
| 5994 | auto void_id = getSPIRVType(Type::getVoidTy(module->getContext())); |
| 5995 | SPIRVOperandVec Ops; |
| 5996 | Ops << void_id << import_id << reflection::ExtInstArgumentInfo << arg_name; |
| 5997 | auto arg_info = addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
| 5998 | |
| 5999 | Ops.clear(); |
| 6000 | Ops << void_id << import_id; |
| 6001 | reflection::ExtInst ext_inst = reflection::ExtInstMax; |
| 6002 | // Determine the extended instruction. |
| 6003 | switch (arg_kind) { |
| 6004 | case clspv::ArgKind::Buffer: |
| 6005 | ext_inst = reflection::ExtInstArgumentStorageBuffer; |
| 6006 | break; |
| 6007 | case clspv::ArgKind::BufferUBO: |
| 6008 | ext_inst = reflection::ExtInstArgumentUniform; |
| 6009 | break; |
| 6010 | case clspv::ArgKind::Local: |
| 6011 | ext_inst = reflection::ExtInstArgumentWorkgroup; |
| 6012 | break; |
| 6013 | case clspv::ArgKind::Pod: |
| 6014 | ext_inst = reflection::ExtInstArgumentPodStorageBuffer; |
| 6015 | break; |
| 6016 | case clspv::ArgKind::PodUBO: |
| 6017 | ext_inst = reflection::ExtInstArgumentPodUniform; |
| 6018 | break; |
| 6019 | case clspv::ArgKind::PodPushConstant: |
| 6020 | ext_inst = reflection::ExtInstArgumentPodPushConstant; |
| 6021 | break; |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 6022 | case clspv::ArgKind::SampledImage: |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 6023 | ext_inst = reflection::ExtInstArgumentSampledImage; |
| 6024 | break; |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 6025 | case clspv::ArgKind::StorageImage: |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 6026 | ext_inst = reflection::ExtInstArgumentStorageImage; |
| 6027 | break; |
| 6028 | case clspv::ArgKind::Sampler: |
| 6029 | ext_inst = reflection::ExtInstArgumentSampler; |
| 6030 | break; |
| 6031 | default: |
| 6032 | llvm_unreachable("Unhandled argument reflection"); |
| 6033 | break; |
| 6034 | } |
| 6035 | Ops << ext_inst << kernel_decl << getSPIRVInt32Constant(ordinal); |
| 6036 | |
| 6037 | // Add descriptor set and binding for applicable arguments. |
| 6038 | switch (arg_kind) { |
| 6039 | case clspv::ArgKind::Buffer: |
| 6040 | case clspv::ArgKind::BufferUBO: |
| 6041 | case clspv::ArgKind::Pod: |
| 6042 | case clspv::ArgKind::PodUBO: |
alan-baker | f6bc825 | 2020-09-23 14:58:55 -0400 | [diff] [blame] | 6043 | case clspv::ArgKind::SampledImage: |
| 6044 | case clspv::ArgKind::StorageImage: |
alan-baker | 86ce19c | 2020-08-05 13:09:19 -0400 | [diff] [blame] | 6045 | case clspv::ArgKind::Sampler: |
| 6046 | Ops << getSPIRVInt32Constant(descriptor_set) |
| 6047 | << getSPIRVInt32Constant(binding); |
| 6048 | break; |
| 6049 | default: |
| 6050 | break; |
| 6051 | } |
| 6052 | |
| 6053 | // Add remaining operands for arguments. |
| 6054 | switch (arg_kind) { |
| 6055 | case clspv::ArgKind::Local: |
| 6056 | Ops << getSPIRVInt32Constant(spec_id) << getSPIRVInt32Constant(elem_size); |
| 6057 | break; |
| 6058 | case clspv::ArgKind::Pod: |
| 6059 | case clspv::ArgKind::PodUBO: |
| 6060 | case clspv::ArgKind::PodPushConstant: |
| 6061 | Ops << getSPIRVInt32Constant(offset) << getSPIRVInt32Constant(size); |
| 6062 | break; |
| 6063 | default: |
| 6064 | break; |
| 6065 | } |
| 6066 | Ops << arg_info; |
| 6067 | addSPIRVInst<kReflection>(spv::OpExtInst, Ops); |
| 6068 | } |