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 | |
alan-baker | e090260 | 2020-03-23 08:43:40 -0400 | [diff] [blame] | 46 | #include "spirv/unified1/spirv.hpp" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 47 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 48 | #include "clspv/AddressSpace.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 49 | #include "clspv/DescriptorMap.h" |
David Neto | 118188e | 2018-08-24 11:27:54 -0400 | [diff] [blame] | 50 | #include "clspv/Option.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 51 | #include "clspv/spirv_c_strings.hpp" |
| 52 | #include "clspv/spirv_glsl.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 53 | |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 54 | #include "ArgKind.h" |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 55 | #include "Builtins.h" |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 56 | #include "ComputeStructuredOrder.h" |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 57 | #include "ConstantEmitter.h" |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 58 | #include "Constants.h" |
David Neto | 7838344 | 2018-06-15 20:31:56 -0400 | [diff] [blame] | 59 | #include "DescriptorCounter.h" |
alan-baker | 56f7aff | 2019-05-22 08:06:42 -0400 | [diff] [blame] | 60 | #include "NormalizeGlobalVariable.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 61 | #include "Passes.h" |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 62 | #include "SpecConstant.h" |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 63 | #include "Types.h" |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 64 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 65 | #if defined(_MSC_VER) |
| 66 | #pragma warning(pop) |
| 67 | #endif |
| 68 | |
| 69 | using namespace llvm; |
| 70 | using namespace clspv; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 71 | using namespace clspv::Builtins; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 72 | using namespace mdconst; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 73 | |
| 74 | namespace { |
David Neto | cd8ca5f | 2017-10-02 23:34:11 -0400 | [diff] [blame] | 75 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 76 | cl::opt<bool> ShowResourceVars("show-rv", cl::init(false), cl::Hidden, |
| 77 | cl::desc("Show resource variable creation")); |
| 78 | |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 79 | cl::opt<bool> |
| 80 | ShowProducerIR("show-producer-ir", cl::init(false), cl::ReallyHidden, |
| 81 | cl::desc("Dump the IR at the start of SPIRVProducer")); |
| 82 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 83 | // These hacks exist to help transition code generation algorithms |
| 84 | // without making huge noise in detailed test output. |
| 85 | const bool Hack_generate_runtime_array_stride_early = true; |
| 86 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 87 | // The value of 1/pi. This value is from MSDN |
| 88 | // https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx |
| 89 | const double kOneOverPi = 0.318309886183790671538; |
| 90 | const glsl::ExtInst kGlslExtInstBad = static_cast<glsl::ExtInst>(0); |
| 91 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 92 | const char *kCompositeConstructFunctionPrefix = "clspv.composite_construct."; |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 93 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 94 | // SPIRV Module Sections (per 2.4 of the SPIRV spec) |
| 95 | // These are used to collect SPIRVInstructions by type on-the-fly. |
| 96 | enum SPIRVSection { |
| 97 | kCapabilities, |
| 98 | kExtensions, |
| 99 | kImports, |
| 100 | kMemoryModel, |
| 101 | kEntryPoints, |
| 102 | kExecutionModes, |
| 103 | |
| 104 | kDebug, |
| 105 | kAnnotations, |
| 106 | |
| 107 | kTypes, |
| 108 | kConstants = kTypes, |
| 109 | kGlobalVariables, |
| 110 | |
| 111 | kFunctions, |
| 112 | |
| 113 | kSectionCount |
| 114 | }; |
| 115 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 116 | enum SPIRVOperandType { |
| 117 | NUMBERID, |
| 118 | LITERAL_INTEGER, |
| 119 | LITERAL_STRING, |
| 120 | LITERAL_FLOAT |
| 121 | }; |
| 122 | |
| 123 | struct SPIRVOperand { |
| 124 | explicit SPIRVOperand(SPIRVOperandType Ty, uint32_t Num) |
| 125 | : Type(Ty), LiteralNum(1, Num) {} |
| 126 | explicit SPIRVOperand(SPIRVOperandType Ty, const char *Str) |
| 127 | : Type(Ty), LiteralStr(Str) {} |
| 128 | explicit SPIRVOperand(SPIRVOperandType Ty, StringRef Str) |
| 129 | : Type(Ty), LiteralStr(Str) {} |
| 130 | explicit SPIRVOperand(SPIRVOperandType Ty, ArrayRef<uint32_t> NumVec) |
| 131 | : Type(Ty), LiteralNum(NumVec.begin(), NumVec.end()) {} |
| 132 | |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 133 | SPIRVOperandType getType() const { return Type; }; |
| 134 | uint32_t getNumID() const { return LiteralNum[0]; }; |
| 135 | std::string getLiteralStr() const { return LiteralStr; }; |
| 136 | ArrayRef<uint32_t> getLiteralNum() const { return LiteralNum; }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 137 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 138 | uint32_t GetNumWords() const { |
| 139 | switch (Type) { |
| 140 | case NUMBERID: |
| 141 | return 1; |
| 142 | case LITERAL_INTEGER: |
| 143 | case LITERAL_FLOAT: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 144 | return uint32_t(LiteralNum.size()); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 145 | case LITERAL_STRING: |
| 146 | // Account for the terminating null character. |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 147 | return uint32_t((LiteralStr.size() + 4) / 4); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 148 | } |
| 149 | llvm_unreachable("Unhandled case in SPIRVOperand::GetNumWords()"); |
| 150 | } |
| 151 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 152 | private: |
| 153 | SPIRVOperandType Type; |
| 154 | std::string LiteralStr; |
| 155 | SmallVector<uint32_t, 4> LiteralNum; |
| 156 | }; |
| 157 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 158 | class SPIRVOperandList { |
| 159 | public: |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 160 | typedef std::unique_ptr<SPIRVOperand> element_type; |
| 161 | typedef SmallVector<element_type, 8> container_type; |
| 162 | typedef container_type::iterator iterator; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 163 | SPIRVOperandList() {} |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 164 | SPIRVOperandList(const SPIRVOperandList &other) = delete; |
| 165 | SPIRVOperandList(SPIRVOperandList &&other) { |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 166 | contents_ = std::move(other.contents_); |
| 167 | other.contents_.clear(); |
| 168 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 169 | iterator begin() { return contents_.begin(); } |
| 170 | iterator end() { return contents_.end(); } |
| 171 | operator ArrayRef<element_type>() { return contents_; } |
| 172 | void push_back(element_type op) { contents_.push_back(std::move(op)); } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 173 | void clear() { contents_.clear(); } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 174 | size_t size() const { return contents_.size(); } |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 175 | const SPIRVOperand *operator[](size_t i) { return contents_[i].get(); } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 176 | |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 177 | const container_type &getOperands() const { return contents_; } |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 178 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 179 | private: |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 180 | container_type contents_; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 181 | }; |
| 182 | |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 183 | SPIRVOperandList &operator<<(SPIRVOperandList &list, |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 184 | std::unique_ptr<SPIRVOperand> elem) { |
| 185 | list.push_back(std::move(elem)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 186 | return list; |
| 187 | } |
| 188 | |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 189 | std::unique_ptr<SPIRVOperand> MkNum(uint32_t num) { |
| 190 | return std::make_unique<SPIRVOperand>(LITERAL_INTEGER, num); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 191 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 192 | std::unique_ptr<SPIRVOperand> MkInteger(ArrayRef<uint32_t> num_vec) { |
| 193 | return std::make_unique<SPIRVOperand>(LITERAL_INTEGER, num_vec); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 194 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 195 | std::unique_ptr<SPIRVOperand> MkFloat(ArrayRef<uint32_t> num_vec) { |
| 196 | return std::make_unique<SPIRVOperand>(LITERAL_FLOAT, num_vec); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 197 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 198 | std::unique_ptr<SPIRVOperand> MkId(uint32_t id) { |
| 199 | return std::make_unique<SPIRVOperand>(NUMBERID, id); |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 200 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 201 | std::unique_ptr<SPIRVOperand> MkString(StringRef str) { |
| 202 | return std::make_unique<SPIRVOperand>(LITERAL_STRING, str); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 203 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 204 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 205 | struct SPIRVInstruction { |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 206 | // Creates an instruction with an opcode and no result ID, and with the given |
| 207 | // operands. This computes its own word count. Takes ownership of the |
| 208 | // operands and clears |Ops|. |
| 209 | SPIRVInstruction(spv::Op Opc, SPIRVOperandList &Ops) |
| 210 | : WordCount(1), Opcode(static_cast<uint16_t>(Opc)), ResultID(0) { |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 211 | for (auto &operand : Ops) { |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 212 | WordCount += uint16_t(operand->GetNumWords()); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 213 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 214 | Operands.reserve(Ops.size()); |
| 215 | for (auto &ptr : Ops) { |
| 216 | Operands.emplace_back(std::move(ptr)); |
| 217 | ptr.reset(nullptr); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 218 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 219 | Ops.clear(); |
| 220 | } |
| 221 | // Creates an instruction with an opcode and a no-zero result ID, and |
| 222 | // with the given operands. This computes its own word count. Takes ownership |
| 223 | // of the operands and clears |Ops|. |
| 224 | SPIRVInstruction(spv::Op Opc, uint32_t ResID, SPIRVOperandList &Ops) |
| 225 | : WordCount(2), Opcode(static_cast<uint16_t>(Opc)), ResultID(ResID) { |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 226 | for (auto &operand : Ops) { |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 227 | WordCount += operand->GetNumWords(); |
| 228 | } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 229 | Operands.reserve(Ops.size()); |
| 230 | for (auto &ptr : Ops) { |
| 231 | Operands.emplace_back(std::move(ptr)); |
| 232 | ptr.reset(nullptr); |
| 233 | } |
| 234 | if (ResID == 0) { |
| 235 | llvm_unreachable("Result ID of 0 was provided"); |
| 236 | } |
| 237 | Ops.clear(); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 238 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 239 | |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 240 | // Creates an instruction with an opcode and no result ID, and with the single |
| 241 | // operand. This computes its own word count. |
| 242 | SPIRVInstruction(spv::Op Opc, SPIRVOperandList::element_type operand) |
| 243 | : WordCount(1), Opcode(static_cast<uint16_t>(Opc)), ResultID(0) { |
| 244 | WordCount += operand->GetNumWords(); |
| 245 | Operands.emplace_back(std::move(operand)); |
| 246 | operand.reset(nullptr); |
| 247 | } |
| 248 | // Creates an instruction with an opcode and a non-zero result ID, and |
| 249 | // with the single operand. This computes its own word count. |
| 250 | SPIRVInstruction(spv::Op Opc, uint32_t ResID, |
| 251 | SPIRVOperandList::element_type operand) |
| 252 | : WordCount(2), Opcode(static_cast<uint16_t>(Opc)), ResultID(ResID) { |
| 253 | WordCount += operand->GetNumWords(); |
| 254 | if (ResID == 0) { |
| 255 | llvm_unreachable("Result ID of 0 was provided"); |
| 256 | } |
| 257 | Operands.emplace_back(std::move(operand)); |
| 258 | operand.reset(nullptr); |
| 259 | } |
| 260 | // Creates an instruction with an opcode and a no-zero result ID, and no |
| 261 | // operands. |
| 262 | SPIRVInstruction(spv::Op Opc, uint32_t ResID) |
| 263 | : WordCount(2), Opcode(static_cast<uint16_t>(Opc)), ResultID(ResID) { |
| 264 | if (ResID == 0) { |
| 265 | llvm_unreachable("Result ID of 0 was provided"); |
| 266 | } |
| 267 | } |
| 268 | // Creates an instruction with an opcode, no result ID, no type ID, and no |
| 269 | // operands. |
| 270 | SPIRVInstruction(spv::Op Opc) |
| 271 | : WordCount(1), Opcode(static_cast<uint16_t>(Opc)), ResultID(0) {} |
| 272 | |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 273 | uint32_t getWordCount() const { return WordCount; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 274 | uint16_t getOpcode() const { return Opcode; } |
| 275 | uint32_t getResultID() const { return ResultID; } |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 276 | ArrayRef<std::unique_ptr<SPIRVOperand>> getOperands() const { |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 277 | return Operands; |
| 278 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 279 | |
| 280 | private: |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 281 | uint32_t WordCount; // Check the 16-bit bound at code generation time. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 282 | uint16_t Opcode; |
| 283 | uint32_t ResultID; |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 284 | SmallVector<std::unique_ptr<SPIRVOperand>, 4> Operands; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | struct SPIRVProducerPass final : public ModulePass { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 288 | typedef DenseMap<Type *, uint32_t> TypeMapType; |
| 289 | typedef UniqueVector<Type *> TypeList; |
| 290 | typedef DenseMap<Value *, uint32_t> ValueMapType; |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 291 | typedef UniqueVector<Value *> ValueList; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 292 | typedef std::vector<std::pair<Value *, uint32_t>> EntryPointVecType; |
| 293 | typedef std::list<SPIRVInstruction *> SPIRVInstructionList; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 294 | // A vector of tuples, each of which is: |
| 295 | // - the LLVM instruction that we will later generate SPIR-V code for |
| 296 | // - where the SPIR-V instruction should be inserted |
| 297 | // - the result ID of the SPIR-V instruction |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 298 | typedef std::vector< |
| 299 | std::tuple<Value *, SPIRVInstructionList::iterator, uint32_t>> |
| 300 | DeferredInstVecType; |
| 301 | typedef DenseMap<FunctionType *, std::pair<FunctionType *, uint32_t>> |
| 302 | GlobalConstFuncMapType; |
| 303 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 304 | explicit SPIRVProducerPass( |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 305 | raw_pwrite_stream &out, |
| 306 | std::vector<clspv::version0::DescriptorMapEntry> *descriptor_map_entries, |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 307 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 308 | bool outputCInitList) |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 309 | : ModulePass(ID), samplerMap(samplerMap), out(out), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 310 | binaryTempOut(binaryTempUnderlyingVector), binaryOut(&out), |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 311 | descriptorMapEntries(descriptor_map_entries), |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 312 | outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1), |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 313 | OpExtInstImportID(0), HasVariablePointersStorageBuffer(false), |
| 314 | HasVariablePointers(false), SamplerTy(nullptr), WorkgroupSizeValueID(0), |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 315 | WorkgroupSizeVarID(0) {} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 316 | |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 317 | virtual ~SPIRVProducerPass() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 318 | for (int i = 0; i < kSectionCount; ++i) { |
| 319 | for (auto *Inst : SPIRVSections[i]) { |
| 320 | delete Inst; |
| 321 | } |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 325 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 326 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 327 | AU.addRequired<LoopInfoWrapperPass>(); |
| 328 | } |
| 329 | |
| 330 | virtual bool runOnModule(Module &module) override; |
| 331 | |
| 332 | // output the SPIR-V header block |
| 333 | void outputHeader(); |
| 334 | |
| 335 | // patch the SPIR-V header block |
| 336 | void patchHeader(); |
| 337 | |
| 338 | uint32_t lookupType(Type *Ty) { |
| 339 | if (Ty->isPointerTy() && |
| 340 | (Ty->getPointerAddressSpace() != AddressSpace::UniformConstant)) { |
| 341 | auto PointeeTy = Ty->getPointerElementType(); |
| 342 | if (PointeeTy->isStructTy() && |
| 343 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 344 | Ty = PointeeTy; |
| 345 | } |
| 346 | } |
| 347 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 348 | auto where = TypeMap.find(Ty); |
| 349 | if (where == TypeMap.end()) { |
| 350 | if (Ty) { |
| 351 | errs() << "Unhandled type " << *Ty << "\n"; |
| 352 | } else { |
| 353 | errs() << "Unhandled type (null)\n"; |
| 354 | } |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 355 | llvm_unreachable("\nUnhandled type!"); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 356 | } |
| 357 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 358 | return where->second; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 359 | } |
| 360 | TypeMapType &getImageTypeMap() { return ImageTypeMap; } |
alan-baker | abd8272 | 2019-12-03 17:14:51 -0500 | [diff] [blame] | 361 | TypeList &getImageTypeList() { return ImageTypeList; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 362 | TypeList &getTypeList() { return Types; }; |
| 363 | ValueList &getConstantList() { return Constants; }; |
| 364 | ValueMapType &getValueMap() { return ValueMap; } |
| 365 | ValueMapType &getAllocatedValueMap() { return AllocatedValueMap; } |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 366 | SPIRVInstructionList &getSPIRVInstList(SPIRVSection Section) { |
| 367 | return SPIRVSections[Section]; |
| 368 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 369 | EntryPointVecType &getEntryPointVec() { return EntryPointVec; }; |
| 370 | DeferredInstVecType &getDeferredInstVec() { return DeferredInstVec; }; |
| 371 | ValueList &getEntryPointInterfacesVec() { return EntryPointInterfacesVec; }; |
| 372 | uint32_t &getOpExtInstImportID() { return OpExtInstImportID; }; |
| 373 | std::vector<uint32_t> &getBuiltinDimVec() { return BuiltinDimensionVec; }; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 374 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 375 | bool hasVariablePointersStorageBuffer() { |
| 376 | return HasVariablePointersStorageBuffer; |
| 377 | } |
| 378 | void setVariablePointersStorageBuffer(bool Val) { |
| 379 | HasVariablePointersStorageBuffer = Val; |
| 380 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 381 | bool hasVariablePointers() { return HasVariablePointers; }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 382 | void setVariablePointers(bool Val) { HasVariablePointers = Val; }; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 383 | ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() { |
| 384 | return samplerMap; |
| 385 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 386 | GlobalConstFuncMapType &getGlobalConstFuncTypeMap() { |
| 387 | return GlobalConstFuncTypeMap; |
| 388 | } |
| 389 | SmallPtrSet<Value *, 16> &getGlobalConstArgSet() { |
| 390 | return GlobalConstArgumentSet; |
| 391 | } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 392 | TypeList &getTypesNeedingArrayStride() { return TypesNeedingArrayStride; } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 393 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 394 | void GenerateLLVMIRInfo(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 395 | // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will |
| 396 | // *not* be converted to a storage buffer, replace each such global variable |
| 397 | // with one in the storage class expecgted by SPIR-V. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 398 | void FindGlobalConstVars(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 399 | // Populate ResourceVarInfoList, FunctionToResourceVarsMap, and |
| 400 | // ModuleOrderedResourceVars. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 401 | void FindResourceVars(); |
| 402 | void FindWorkgroupVars(); |
| 403 | bool FindExtInst(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 404 | void FindTypePerGlobalVar(GlobalVariable &GV); |
| 405 | void FindTypePerFunc(Function &F); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 406 | void FindTypesForSamplerMap(); |
| 407 | void FindTypesForResourceVars(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 408 | // Inserts |Ty| and relevant sub-types into the |Types| member, indicating |
| 409 | // that |Ty| and its subtypes will need a corresponding SPIR-V type. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 410 | void FindType(Type *Ty); |
| 411 | void FindConstantPerGlobalVar(GlobalVariable &GV); |
| 412 | void FindConstantPerFunc(Function &F); |
| 413 | void FindConstant(Value *V); |
| 414 | void GenerateExtInstImport(); |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 415 | // Generates instructions for SPIR-V types corresponding to the LLVM types |
| 416 | // saved in the |Types| member. A type follows its subtypes. IDs are |
| 417 | // allocated sequentially starting with the current value of nextID, and |
| 418 | // with a type following its subtypes. Also updates nextID to just beyond |
| 419 | // the last generated ID. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 420 | void GenerateSPIRVTypes(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 421 | void GenerateSPIRVConstants(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 422 | void GenerateModuleInfo(); |
| 423 | void GeneratePushConstantDescriptorMapEntries(); |
| 424 | void GenerateSpecConstantDescriptorMapEntries(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 425 | void GenerateGlobalVar(GlobalVariable &GV); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 426 | void GenerateWorkgroupVars(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 427 | // Generate descriptor map entries for resource variables associated with |
| 428 | // arguments to F. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 429 | void GenerateDescriptorMapInfo(Function &F); |
| 430 | void GenerateSamplers(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 431 | // Generate OpVariables for %clspv.resource.var.* calls. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 432 | void GenerateResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 433 | void GenerateFuncPrologue(Function &F); |
| 434 | void GenerateFuncBody(Function &F); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 435 | void GenerateEntryPointInitialStores(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 436 | spv::Op GetSPIRVCmpOpcode(CmpInst *CmpI); |
| 437 | spv::Op GetSPIRVCastOpcode(Instruction &I); |
| 438 | spv::Op GetSPIRVBinaryOpcode(Instruction &I); |
| 439 | void GenerateInstruction(Instruction &I); |
| 440 | void GenerateFuncEpilogue(); |
| 441 | void HandleDeferredInstruction(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 442 | void HandleDeferredDecorations(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 443 | bool is4xi8vec(Type *Ty) const; |
| 444 | spv::StorageClass GetStorageClass(unsigned AddrSpace) const; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 445 | spv::StorageClass GetStorageClassForArgKind(clspv::ArgKind arg_kind) const; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 446 | spv::BuiltIn GetBuiltin(StringRef globalVarName) const; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 447 | // Returns the GLSL extended instruction enum that the given function |
| 448 | // call maps to. If none, then returns the 0 value, i.e. GLSLstd4580Bad. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 449 | glsl::ExtInst getExtInstEnum(StringRef Name); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 450 | // Returns the GLSL extended instruction enum indirectly used by the given |
| 451 | // function. That is, to implement the given function, we use an extended |
| 452 | // instruction plus one more instruction. If none, then returns the 0 value, |
| 453 | // i.e. GLSLstd4580Bad. |
| 454 | glsl::ExtInst getIndirectExtInstEnum(StringRef Name); |
| 455 | // Returns the single GLSL extended instruction used directly or |
| 456 | // indirectly by the given function call. |
| 457 | glsl::ExtInst getDirectOrIndirectExtInstEnum(StringRef Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 458 | void WriteOneWord(uint32_t Word); |
| 459 | void WriteResultID(SPIRVInstruction *Inst); |
| 460 | void WriteWordCountAndOpcode(SPIRVInstruction *Inst); |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 461 | void WriteOperand(const std::unique_ptr<SPIRVOperand> &Op); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 462 | void WriteSPIRVBinary(); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 463 | void WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 464 | |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 465 | // Returns true if |type| is compatible with OpConstantNull. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 466 | bool IsTypeNullable(const Type *type) const; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 467 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 468 | // Populate UBO remapped type maps. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 469 | void PopulateUBOTypeMaps(); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 470 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 471 | // Populate the merge and continue block maps. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 472 | void PopulateStructuredCFGMaps(); |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 473 | |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 474 | // Wrapped methods of DataLayout accessors. If |type| was remapped for UBOs, |
| 475 | // uses the internal map, otherwise it falls back on the data layout. |
| 476 | uint64_t GetTypeSizeInBits(Type *type, const DataLayout &DL); |
| 477 | uint64_t GetTypeStoreSize(Type *type, const DataLayout &DL); |
| 478 | uint64_t GetTypeAllocSize(Type *type, const DataLayout &DL); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 479 | uint32_t GetExplicitLayoutStructMemberOffset(StructType *type, |
| 480 | unsigned member, |
| 481 | const DataLayout &DL); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 482 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 483 | // Returns the base pointer of |v|. |
| 484 | Value *GetBasePointer(Value *v); |
| 485 | |
| 486 | // Sets |HasVariablePointersStorageBuffer| or |HasVariablePointers| base on |
| 487 | // |address_space|. |
| 488 | void setVariablePointersCapabilities(unsigned address_space); |
| 489 | |
| 490 | // Returns true if |lhs| and |rhs| represent the same resource or workgroup |
| 491 | // variable. |
| 492 | bool sameResource(Value *lhs, Value *rhs) const; |
| 493 | |
| 494 | // Returns true if |inst| is phi or select that selects from the same |
| 495 | // structure (or null). |
| 496 | bool selectFromSameObject(Instruction *inst); |
| 497 | |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 498 | // Returns true if |Arg| is called with a coherent resource. |
| 499 | bool CalledWithCoherentResource(Argument &Arg); |
| 500 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 501 | private: |
| 502 | static char ID; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 503 | |
| 504 | Module *module; |
| 505 | |
David Neto | 4479515 | 2017-07-13 15:45:28 -0400 | [diff] [blame] | 506 | ArrayRef<std::pair<unsigned, std::string>> samplerMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 507 | raw_pwrite_stream &out; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 508 | |
| 509 | // TODO(dneto): Wouldn't it be better to always just emit a binary, and then |
| 510 | // convert to other formats on demand? |
| 511 | |
| 512 | // When emitting a C initialization list, the WriteSPIRVBinary method |
| 513 | // will actually write its words to this vector via binaryTempOut. |
| 514 | SmallVector<char, 100> binaryTempUnderlyingVector; |
| 515 | raw_svector_ostream binaryTempOut; |
| 516 | |
| 517 | // Binary output writes to this stream, which might be |out| or |
| 518 | // |binaryTempOut|. It's the latter when we really want to write a C |
| 519 | // initializer list. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 520 | raw_pwrite_stream *binaryOut; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 521 | std::vector<version0::DescriptorMapEntry> *descriptorMapEntries; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 522 | const bool outputCInitList; // If true, output look like {0x7023, ... , 5} |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 523 | uint64_t patchBoundOffset; |
| 524 | uint32_t nextID; |
| 525 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 526 | // ID for OpTypeInt 32 1. |
| 527 | uint32_t int32ID = 0; |
| 528 | // ID for OpTypeVector %int 4. |
| 529 | uint32_t v4int32ID = 0; |
| 530 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 531 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 532 | TypeMapType TypeMap; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 533 | // Maps an LLVM image type to its SPIR-V ID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 534 | TypeMapType ImageTypeMap; |
alan-baker | abd8272 | 2019-12-03 17:14:51 -0500 | [diff] [blame] | 535 | // A unique-vector of LLVM image types. This list is used to provide |
| 536 | // deterministic traversal of image types. |
| 537 | TypeList ImageTypeList; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 538 | // 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] | 539 | TypeList Types; |
| 540 | ValueList Constants; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 541 | // Maps an LLVM Value pointer to the corresponding SPIR-V Id. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 542 | ValueMapType ValueMap; |
| 543 | ValueMapType AllocatedValueMap; |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 544 | SPIRVInstructionList SPIRVSections[kSectionCount]; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 545 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 546 | EntryPointVecType EntryPointVec; |
| 547 | DeferredInstVecType DeferredInstVec; |
| 548 | ValueList EntryPointInterfacesVec; |
| 549 | uint32_t OpExtInstImportID; |
| 550 | std::vector<uint32_t> BuiltinDimensionVec; |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 551 | bool HasVariablePointersStorageBuffer; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 552 | bool HasVariablePointers; |
| 553 | Type *SamplerTy; |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 554 | DenseMap<unsigned, unsigned> SamplerLiteralToIDMap; |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 555 | |
| 556 | // 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] | 557 | // will map F's type to (G, index of the parameter), where in a first phase |
| 558 | // G is F's type. During FindTypePerFunc, G will be changed to F's type |
| 559 | // but replacing the pointer-to-constant parameter with |
| 560 | // pointer-to-ModuleScopePrivate. |
David Neto | c77d9e2 | 2018-03-24 06:30:28 -0700 | [diff] [blame] | 561 | // TODO(dneto): This doesn't seem general enough? A function might have |
| 562 | // more than one such parameter. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 563 | GlobalConstFuncMapType GlobalConstFuncTypeMap; |
| 564 | SmallPtrSet<Value *, 16> GlobalConstArgumentSet; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 565 | // An ordered set of pointer types of Base arguments to OpPtrAccessChain, |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 566 | // or array types, and which point into transparent memory (StorageBuffer |
| 567 | // storage class). These will require an ArrayStride decoration. |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 568 | // See SPV_KHR_variable_pointers rev 13. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 569 | TypeList TypesNeedingArrayStride; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 570 | |
| 571 | // This is truly ugly, but works around what look like driver bugs. |
| 572 | // For get_local_size, an earlier part of the flow has created a module-scope |
| 573 | // variable in Private address space to hold the value for the workgroup |
| 574 | // size. Its intializer is a uint3 value marked as builtin WorkgroupSize. |
| 575 | // When this is present, save the IDs of the initializer value and variable |
| 576 | // in these two variables. We only ever do a vector load from it, and |
| 577 | // when we see one of those, substitute just the value of the intializer. |
| 578 | // 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] | 579 | // TODO(dneto): Remove this once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 580 | uint32_t WorkgroupSizeValueID; |
| 581 | uint32_t WorkgroupSizeVarID; |
David Neto | 26aaf62 | 2017-10-23 18:11:53 -0400 | [diff] [blame] | 582 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 583 | // Bookkeeping for mapping kernel arguments to resource variables. |
| 584 | struct ResourceVarInfo { |
| 585 | ResourceVarInfo(int index_arg, unsigned set_arg, unsigned binding_arg, |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 586 | Function *fn, clspv::ArgKind arg_kind_arg, int coherent_arg) |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 587 | : index(index_arg), descriptor_set(set_arg), binding(binding_arg), |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 588 | var_fn(fn), arg_kind(arg_kind_arg), coherent(coherent_arg), |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 589 | addr_space(fn->getReturnType()->getPointerAddressSpace()) {} |
| 590 | const int index; // Index into ResourceVarInfoList |
| 591 | const unsigned descriptor_set; |
| 592 | const unsigned binding; |
| 593 | Function *const var_fn; // The @clspv.resource.var.* function. |
| 594 | const clspv::ArgKind arg_kind; |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 595 | const int coherent; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 596 | const unsigned addr_space; // The LLVM address space |
| 597 | // The SPIR-V ID of the OpVariable. Not populated at construction time. |
| 598 | uint32_t var_id = 0; |
| 599 | }; |
| 600 | // A list of resource var info. Each one correponds to a module-scope |
| 601 | // resource variable we will have to create. Resource var indices are |
| 602 | // indices into this vector. |
| 603 | SmallVector<std::unique_ptr<ResourceVarInfo>, 8> ResourceVarInfoList; |
| 604 | // This is a vector of pointers of all the resource vars, but ordered by |
| 605 | // kernel function, and then by argument. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 606 | UniqueVector<ResourceVarInfo *> ModuleOrderedResourceVars; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 607 | // Map a function to the ordered list of resource variables it uses, one for |
| 608 | // each argument. If an argument does not use a resource variable, it |
| 609 | // will have a null pointer entry. |
| 610 | using FunctionToResourceVarsMapType = |
| 611 | DenseMap<Function *, SmallVector<ResourceVarInfo *, 8>>; |
| 612 | FunctionToResourceVarsMapType FunctionToResourceVarsMap; |
| 613 | |
| 614 | // What LLVM types map to SPIR-V types needing layout? These are the |
| 615 | // arrays and structures supporting storage buffers and uniform buffers. |
| 616 | TypeList TypesNeedingLayout; |
| 617 | // What LLVM struct types map to a SPIR-V struct type with Block decoration? |
| 618 | UniqueVector<StructType *> StructTypesNeedingBlock; |
| 619 | // For a call that represents a load from an opaque type (samplers, images), |
| 620 | // map it to the variable id it should load from. |
| 621 | DenseMap<CallInst *, uint32_t> ResourceVarDeferredLoadCalls; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 622 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 623 | // An ordered list of the kernel arguments of type pointer-to-local. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 624 | using LocalArgList = SmallVector<Argument *, 8>; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 625 | LocalArgList LocalArgs; |
| 626 | // Information about a pointer-to-local argument. |
| 627 | struct LocalArgInfo { |
| 628 | // The SPIR-V ID of the array variable. |
| 629 | uint32_t variable_id; |
| 630 | // The element type of the |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 631 | Type *elem_type; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 632 | // The ID of the array type. |
| 633 | uint32_t array_size_id; |
| 634 | // The ID of the array type. |
| 635 | uint32_t array_type_id; |
| 636 | // The ID of the pointer to the array type. |
| 637 | uint32_t ptr_array_type_id; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 638 | // The specialization constant ID of the array size. |
| 639 | int spec_id; |
| 640 | }; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 641 | // A mapping from Argument to its assigned SpecId. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 642 | DenseMap<const Argument *, int> LocalArgSpecIds; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 643 | // A mapping from SpecId to its LocalArgInfo. |
| 644 | DenseMap<int, LocalArgInfo> LocalSpecIdInfoMap; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 645 | // A mapping from a remapped type to its real offsets. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 646 | DenseMap<Type *, std::vector<uint32_t>> RemappedUBOTypeOffsets; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 647 | // A mapping from a remapped type to its real sizes. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 648 | DenseMap<Type *, std::tuple<uint64_t, uint64_t, uint64_t>> |
| 649 | RemappedUBOTypeSizes; |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 650 | |
| 651 | // Maps basic block to its merge block. |
| 652 | DenseMap<BasicBlock *, BasicBlock *> MergeBlocks; |
| 653 | // Maps basic block to its continue block. |
| 654 | DenseMap<BasicBlock *, BasicBlock *> ContinueBlocks; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 655 | }; |
| 656 | |
| 657 | char SPIRVProducerPass::ID; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 658 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 659 | } // namespace |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 660 | |
| 661 | namespace clspv { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 662 | ModulePass *createSPIRVProducerPass( |
| 663 | raw_pwrite_stream &out, |
| 664 | std::vector<version0::DescriptorMapEntry> *descriptor_map_entries, |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 665 | ArrayRef<std::pair<unsigned, std::string>> samplerMap, |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 666 | bool outputCInitList) { |
| 667 | return new SPIRVProducerPass(out, descriptor_map_entries, samplerMap, |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 668 | outputCInitList); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 669 | } |
David Neto | c2c368d | 2017-06-30 16:50:17 -0400 | [diff] [blame] | 670 | } // namespace clspv |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 671 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 672 | bool SPIRVProducerPass::runOnModule(Module &M) { |
| 673 | module = &M; |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 674 | if (ShowProducerIR) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 675 | llvm::outs() << *module << "\n"; |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 676 | } |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 677 | binaryOut = outputCInitList ? &binaryTempOut : &out; |
| 678 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 679 | PopulateUBOTypeMaps(); |
| 680 | PopulateStructuredCFGMaps(); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 681 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 682 | // SPIR-V always begins with its header information |
| 683 | outputHeader(); |
| 684 | |
| 685 | // Gather information from the LLVM IR that we require. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 686 | GenerateLLVMIRInfo(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 687 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 688 | // Collect information on global variables too. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 689 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 690 | // If the GV is one of our special __spirv_* variables, remove the |
| 691 | // initializer as it was only placed there to force LLVM to not throw the |
| 692 | // value away. |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 693 | if (GV.getName().startswith("__spirv_") || |
| 694 | GV.getAddressSpace() == clspv::AddressSpace::PushConstant) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 695 | GV.setInitializer(nullptr); |
| 696 | } |
| 697 | |
| 698 | // Collect types' information from global variable. |
| 699 | FindTypePerGlobalVar(GV); |
| 700 | |
| 701 | // Collect constant information from global variable. |
| 702 | FindConstantPerGlobalVar(GV); |
| 703 | |
| 704 | // If the variable is an input, entry points need to know about it. |
| 705 | if (AddressSpace::Input == GV.getType()->getPointerAddressSpace()) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 706 | getEntryPointInterfacesVec().insert(&GV); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | // If there are extended instructions, generate OpExtInstImport. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 711 | if (FindExtInst()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 712 | GenerateExtInstImport(); |
| 713 | } |
| 714 | |
| 715 | // Generate SPIRV instructions for types. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 716 | GenerateSPIRVTypes(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 717 | |
| 718 | // Generate SPIRV constants. |
| 719 | GenerateSPIRVConstants(); |
| 720 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 721 | // Generate literal samplers if necessary. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 722 | GenerateSamplers(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 723 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 724 | // Generate descriptor map entries for all push constants |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 725 | GeneratePushConstantDescriptorMapEntries(); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 726 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 727 | // Generate SPIRV variables. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 728 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 729 | GenerateGlobalVar(GV); |
| 730 | } |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 731 | GenerateResourceVars(); |
| 732 | GenerateWorkgroupVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 733 | |
| 734 | // Generate SPIRV instructions for each function. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 735 | for (Function &F : *module) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 736 | if (F.isDeclaration()) { |
| 737 | continue; |
| 738 | } |
| 739 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 740 | GenerateDescriptorMapInfo(F); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 741 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 742 | // Generate Function Prologue. |
| 743 | GenerateFuncPrologue(F); |
| 744 | |
| 745 | // Generate SPIRV instructions for function body. |
| 746 | GenerateFuncBody(F); |
| 747 | |
| 748 | // Generate Function Epilogue. |
| 749 | GenerateFuncEpilogue(); |
| 750 | } |
| 751 | |
| 752 | HandleDeferredInstruction(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 753 | HandleDeferredDecorations(); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 754 | |
| 755 | // Generate descriptor map entries for module scope specialization constants. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 756 | GenerateSpecConstantDescriptorMapEntries(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 757 | |
| 758 | // Generate SPIRV module information. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 759 | GenerateModuleInfo(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 760 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 761 | WriteSPIRVBinary(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 762 | |
| 763 | // We need to patch the SPIR-V header to set bound correctly. |
| 764 | patchHeader(); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 765 | |
| 766 | if (outputCInitList) { |
| 767 | bool first = true; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 768 | std::ostringstream os; |
| 769 | |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 770 | auto emit_word = [&os, &first](uint32_t word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 771 | if (!first) |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 772 | os << ",\n"; |
| 773 | os << word; |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 774 | first = false; |
| 775 | }; |
| 776 | |
| 777 | os << "{"; |
David Neto | 57fb0b9 | 2017-08-04 15:35:09 -0400 | [diff] [blame] | 778 | const std::string str(binaryTempOut.str()); |
| 779 | for (unsigned i = 0; i < str.size(); i += 4) { |
| 780 | const uint32_t a = static_cast<unsigned char>(str[i]); |
| 781 | const uint32_t b = static_cast<unsigned char>(str[i + 1]); |
| 782 | const uint32_t c = static_cast<unsigned char>(str[i + 2]); |
| 783 | const uint32_t d = static_cast<unsigned char>(str[i + 3]); |
| 784 | emit_word(a | (b << 8) | (c << 16) | (d << 24)); |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 785 | } |
| 786 | os << "}\n"; |
| 787 | out << os.str(); |
| 788 | } |
| 789 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 790 | return false; |
| 791 | } |
| 792 | |
| 793 | void SPIRVProducerPass::outputHeader() { |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 794 | binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber), |
| 795 | sizeof(spv::MagicNumber)); |
alan-baker | e090260 | 2020-03-23 08:43:40 -0400 | [diff] [blame] | 796 | const uint32_t spv_version = 0x10000; // SPIR-V 1.0 |
| 797 | binaryOut->write(reinterpret_cast<const char *>(&spv_version), |
| 798 | sizeof(spv_version)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 799 | |
alan-baker | 0c18ab0 | 2019-06-12 10:23:21 -0400 | [diff] [blame] | 800 | // use Google's vendor ID |
| 801 | const uint32_t vendor = 21 << 16; |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 802 | binaryOut->write(reinterpret_cast<const char *>(&vendor), sizeof(vendor)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 803 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 804 | // we record where we need to come back to and patch in the bound value |
| 805 | patchBoundOffset = binaryOut->tell(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 806 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 807 | // output a bad bound for now |
| 808 | binaryOut->write(reinterpret_cast<const char *>(&nextID), sizeof(nextID)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 809 | |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 810 | // output the schema (reserved for use and must be 0) |
| 811 | const uint32_t schema = 0; |
| 812 | binaryOut->write(reinterpret_cast<const char *>(&schema), sizeof(schema)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | void SPIRVProducerPass::patchHeader() { |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 816 | // for a binary we just write the value of nextID over bound |
| 817 | binaryOut->pwrite(reinterpret_cast<char *>(&nextID), sizeof(nextID), |
| 818 | patchBoundOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 819 | } |
| 820 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 821 | void SPIRVProducerPass::GenerateLLVMIRInfo() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 822 | // This function generates LLVM IR for function such as global variable for |
| 823 | // argument, constant and pointer type for argument access. These information |
| 824 | // is artificial one because we need Vulkan SPIR-V output. This function is |
| 825 | // executed ahead of FindType and FindConstant. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 826 | LLVMContext &Context = module->getContext(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 827 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 828 | FindGlobalConstVars(); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 829 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 830 | FindResourceVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 831 | |
| 832 | bool HasWorkGroupBuiltin = false; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 833 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 834 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 835 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 836 | HasWorkGroupBuiltin = true; |
| 837 | } |
| 838 | } |
| 839 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 840 | FindTypesForSamplerMap(); |
| 841 | FindTypesForResourceVars(); |
| 842 | FindWorkgroupVars(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 843 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 844 | for (Function &F : *module) { |
Kévin Petit | abef452 | 2019-03-27 13:08:01 +0000 | [diff] [blame] | 845 | if (F.isDeclaration()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 846 | continue; |
| 847 | } |
| 848 | |
| 849 | for (BasicBlock &BB : F) { |
| 850 | for (Instruction &I : BB) { |
| 851 | if (I.getOpcode() == Instruction::ZExt || |
| 852 | I.getOpcode() == Instruction::SExt || |
| 853 | I.getOpcode() == Instruction::UIToFP) { |
| 854 | // If there is zext with i1 type, it will be changed to OpSelect. The |
| 855 | // OpSelect needs constant 0 and 1 so the constants are added here. |
| 856 | |
| 857 | auto OpTy = I.getOperand(0)->getType(); |
| 858 | |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 859 | if (OpTy->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 860 | if (I.getOpcode() == Instruction::ZExt) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 861 | FindConstant(Constant::getNullValue(I.getType())); |
Kévin Petit | 7bfb899 | 2019-02-26 13:45:08 +0000 | [diff] [blame] | 862 | FindConstant(ConstantInt::get(I.getType(), 1)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 863 | } else if (I.getOpcode() == Instruction::SExt) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 864 | FindConstant(Constant::getNullValue(I.getType())); |
Kévin Petit | 7bfb899 | 2019-02-26 13:45:08 +0000 | [diff] [blame] | 865 | FindConstant(ConstantInt::getSigned(I.getType(), -1)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 866 | } else { |
| 867 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
| 868 | FindConstant(ConstantFP::get(Context, APFloat(1.0f))); |
| 869 | } |
| 870 | } |
| 871 | } else if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 872 | StringRef callee_name = Call->getCalledFunction()->getName(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 873 | |
| 874 | // Handle image type specially. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 875 | if (IsImageBuiltin(callee_name)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 876 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 877 | Type *ImageTy = |
| 878 | Call->getArgOperand(0)->getType()->getPointerElementType(); |
| 879 | OpImageTypeMap[ImageTy] = 0; |
alan-baker | abd8272 | 2019-12-03 17:14:51 -0500 | [diff] [blame] | 880 | getImageTypeList().insert(ImageTy); |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 881 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 882 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 883 | if (IsSampledImageRead(callee_name)) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 884 | // All sampled reads need a floating point 0 for the Lod operand. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 885 | FindConstant(ConstantFP::get(Context, APFloat(0.0f))); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 886 | } else if (IsUnsampledImageRead(callee_name)) { |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 887 | // All unsampled reads need an integer 0 for the Lod operand. |
| 888 | FindConstant(ConstantInt::get(Context, APInt(32, 0))); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 889 | } else if (IsImageQuery(callee_name)) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 890 | Type *ImageTy = Call->getOperand(0)->getType(); |
| 891 | const uint32_t dim = ImageDimensionality(ImageTy); |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 892 | uint32_t components = |
| 893 | dim + (clspv::IsArrayImageType(ImageTy) ? 1 : 0); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 894 | if (components > 1) { |
| 895 | // OpImageQuerySize* return |components| components. |
| 896 | FindType(VectorType::get(Type::getInt32Ty(Context), components)); |
| 897 | if (dim == 3 && IsGetImageDim(callee_name)) { |
| 898 | // get_image_dim for 3D images returns an int4. |
| 899 | FindType( |
| 900 | VectorType::get(Type::getInt32Ty(Context), components + 1)); |
| 901 | } |
| 902 | } |
| 903 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 904 | if (IsSampledImageType(ImageTy)) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 905 | // All sampled image queries need a integer 0 for the Lod |
| 906 | // operand. |
| 907 | FindConstant(ConstantInt::get(Context, APInt(32, 0))); |
| 908 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 909 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
Kévin Petit | abef452 | 2019-03-27 13:08:01 +0000 | [diff] [blame] | 914 | // More things to do on kernel functions |
| 915 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 916 | if (const MDNode *MD = |
| 917 | dyn_cast<Function>(&F)->getMetadata("reqd_work_group_size")) { |
| 918 | // We generate constants if the WorkgroupSize builtin is being used. |
| 919 | if (HasWorkGroupBuiltin) { |
| 920 | // Collect constant information for work group size. |
| 921 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(0))); |
| 922 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(1))); |
| 923 | FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(2))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 928 | // TODO(alan-baker): make this better. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 929 | if (module->getTypeByName("opencl.image1d_ro_t.float") || |
| 930 | module->getTypeByName("opencl.image1d_ro_t.float.sampled") || |
| 931 | module->getTypeByName("opencl.image1d_wo_t.float") || |
| 932 | module->getTypeByName("opencl.image2d_ro_t.float") || |
| 933 | module->getTypeByName("opencl.image2d_ro_t.float.sampled") || |
| 934 | module->getTypeByName("opencl.image2d_wo_t.float") || |
| 935 | module->getTypeByName("opencl.image3d_ro_t.float") || |
| 936 | module->getTypeByName("opencl.image3d_ro_t.float.sampled") || |
| 937 | module->getTypeByName("opencl.image3d_wo_t.float") || |
| 938 | module->getTypeByName("opencl.image1d_array_ro_t.float") || |
| 939 | module->getTypeByName("opencl.image1d_array_ro_t.float.sampled") || |
| 940 | module->getTypeByName("opencl.image1d_array_wo_t.float") || |
| 941 | module->getTypeByName("opencl.image2d_array_ro_t.float") || |
| 942 | module->getTypeByName("opencl.image2d_array_ro_t.float.sampled") || |
| 943 | module->getTypeByName("opencl.image2d_array_wo_t.float")) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 944 | FindType(Type::getFloatTy(Context)); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 945 | } else if (module->getTypeByName("opencl.image1d_ro_t.uint") || |
| 946 | module->getTypeByName("opencl.image1d_ro_t.uint.sampled") || |
| 947 | module->getTypeByName("opencl.image1d_wo_t.uint") || |
| 948 | module->getTypeByName("opencl.image2d_ro_t.uint") || |
| 949 | module->getTypeByName("opencl.image2d_ro_t.uint.sampled") || |
| 950 | module->getTypeByName("opencl.image2d_wo_t.uint") || |
| 951 | module->getTypeByName("opencl.image3d_ro_t.uint") || |
| 952 | module->getTypeByName("opencl.image3d_ro_t.uint.sampled") || |
| 953 | module->getTypeByName("opencl.image3d_wo_t.uint") || |
| 954 | module->getTypeByName("opencl.image1d_array_ro_t.uint") || |
| 955 | module->getTypeByName( |
| 956 | "opencl.image1d_array_ro_t.uint.sampled") || |
| 957 | module->getTypeByName("opencl.image1d_array_wo_t.uint") || |
| 958 | module->getTypeByName("opencl.image2d_array_ro_t.uint") || |
| 959 | module->getTypeByName( |
| 960 | "opencl.image2d_array_ro_t.uint.sampled") || |
| 961 | module->getTypeByName("opencl.image2d_array_wo_t.uint")) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 962 | FindType(Type::getInt32Ty(Context)); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 963 | } else if (module->getTypeByName("opencl.image1d_ro_t.int") || |
| 964 | module->getTypeByName("opencl.image1d_ro_t.int.sampled") || |
| 965 | module->getTypeByName("opencl.image1d_wo_t.int") || |
| 966 | module->getTypeByName("opencl.image2d_ro_t.int") || |
| 967 | module->getTypeByName("opencl.image2d_ro_t.int.sampled") || |
| 968 | module->getTypeByName("opencl.image2d_wo_t.int") || |
| 969 | module->getTypeByName("opencl.image3d_ro_t.int") || |
| 970 | module->getTypeByName("opencl.image3d_ro_t.int.sampled") || |
| 971 | module->getTypeByName("opencl.image3d_wo_t.int") || |
| 972 | module->getTypeByName("opencl.image1d_array_ro_t.int") || |
| 973 | module->getTypeByName("opencl.image1d_array_ro_t.int.sampled") || |
| 974 | module->getTypeByName("opencl.image1d_array_wo_t.int") || |
| 975 | module->getTypeByName("opencl.image2d_array_ro_t.int") || |
| 976 | module->getTypeByName("opencl.image2d_array_ro_t.int.sampled") || |
| 977 | module->getTypeByName("opencl.image2d_array_wo_t.int")) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 978 | // Nothing for now... |
| 979 | } else { |
| 980 | // This was likely an UndefValue. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 981 | FindType(Type::getFloatTy(Context)); |
| 982 | } |
| 983 | |
| 984 | // Collect types' information from function. |
| 985 | FindTypePerFunc(F); |
| 986 | |
| 987 | // Collect constant information from function. |
| 988 | FindConstantPerFunc(F); |
| 989 | } |
| 990 | } |
| 991 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 992 | void SPIRVProducerPass::FindGlobalConstVars() { |
| 993 | clspv::NormalizeGlobalVariables(*module); |
| 994 | const DataLayout &DL = module->getDataLayout(); |
alan-baker | 56f7aff | 2019-05-22 08:06:42 -0400 | [diff] [blame] | 995 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 996 | SmallVector<GlobalVariable *, 8> GVList; |
| 997 | SmallVector<GlobalVariable *, 8> DeadGVList; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 998 | for (GlobalVariable &GV : module->globals()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 999 | if (GV.getType()->getAddressSpace() == AddressSpace::Constant) { |
| 1000 | if (GV.use_empty()) { |
| 1001 | DeadGVList.push_back(&GV); |
| 1002 | } else { |
| 1003 | GVList.push_back(&GV); |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | // Remove dead global __constant variables. |
| 1009 | for (auto GV : DeadGVList) { |
| 1010 | GV->eraseFromParent(); |
| 1011 | } |
| 1012 | DeadGVList.clear(); |
| 1013 | |
| 1014 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 1015 | // For now, we only support a single storage buffer. |
| 1016 | if (GVList.size() > 0) { |
| 1017 | assert(GVList.size() == 1); |
| 1018 | const auto *GV = GVList[0]; |
| 1019 | const auto constants_byte_size = |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1020 | (GetTypeSizeInBits(GV->getInitializer()->getType(), DL)) / 8; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1021 | const size_t kConstantMaxSize = 65536; |
| 1022 | if (constants_byte_size > kConstantMaxSize) { |
| 1023 | outs() << "Max __constant capacity of " << kConstantMaxSize |
| 1024 | << " bytes exceeded: " << constants_byte_size << " bytes used\n"; |
| 1025 | llvm_unreachable("Max __constant capacity exceeded"); |
| 1026 | } |
| 1027 | } |
| 1028 | } else { |
| 1029 | // Change global constant variable's address space to ModuleScopePrivate. |
| 1030 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 1031 | for (auto GV : GVList) { |
| 1032 | // Create new gv with ModuleScopePrivate address space. |
| 1033 | Type *NewGVTy = GV->getType()->getPointerElementType(); |
| 1034 | GlobalVariable *NewGV = new GlobalVariable( |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1035 | *module, NewGVTy, false, GV->getLinkage(), GV->getInitializer(), "", |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1036 | nullptr, GV->getThreadLocalMode(), AddressSpace::ModuleScopePrivate); |
| 1037 | NewGV->takeName(GV); |
| 1038 | |
| 1039 | const SmallVector<User *, 8> GVUsers(GV->user_begin(), GV->user_end()); |
| 1040 | SmallVector<User *, 8> CandidateUsers; |
| 1041 | |
| 1042 | auto record_called_function_type_as_user = |
| 1043 | [&GlobalConstFuncTyMap](Value *gv, CallInst *call) { |
| 1044 | // Find argument index. |
| 1045 | unsigned index = 0; |
| 1046 | for (unsigned i = 0; i < call->getNumArgOperands(); i++) { |
| 1047 | if (gv == call->getOperand(i)) { |
| 1048 | // TODO(dneto): Should we break here? |
| 1049 | index = i; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | // Record function type with global constant. |
| 1054 | GlobalConstFuncTyMap[call->getFunctionType()] = |
| 1055 | std::make_pair(call->getFunctionType(), index); |
| 1056 | }; |
| 1057 | |
| 1058 | for (User *GVU : GVUsers) { |
| 1059 | if (CallInst *Call = dyn_cast<CallInst>(GVU)) { |
| 1060 | record_called_function_type_as_user(GV, Call); |
| 1061 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(GVU)) { |
| 1062 | // Check GEP users. |
| 1063 | for (User *GEPU : GEP->users()) { |
| 1064 | if (CallInst *GEPCall = dyn_cast<CallInst>(GEPU)) { |
| 1065 | record_called_function_type_as_user(GEP, GEPCall); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | CandidateUsers.push_back(GVU); |
| 1071 | } |
| 1072 | |
| 1073 | for (User *U : CandidateUsers) { |
| 1074 | // Update users of gv with new gv. |
alan-baker | ed80f57 | 2019-02-11 17:28:26 -0500 | [diff] [blame] | 1075 | if (!isa<Constant>(U)) { |
| 1076 | // #254: Can't change operands of a constant, but this shouldn't be |
| 1077 | // something that sticks around in the module. |
| 1078 | U->replaceUsesOfWith(GV, NewGV); |
| 1079 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | // Delete original gv. |
| 1083 | GV->eraseFromParent(); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1088 | void SPIRVProducerPass::FindResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1089 | ResourceVarInfoList.clear(); |
| 1090 | FunctionToResourceVarsMap.clear(); |
| 1091 | ModuleOrderedResourceVars.reset(); |
| 1092 | // Normally, there is one resource variable per clspv.resource.var.* |
| 1093 | // function, since that is unique'd by arg type and index. By design, |
| 1094 | // we can share these resource variables across kernels because all |
| 1095 | // kernels use the same descriptor set. |
| 1096 | // |
| 1097 | // But if the user requested distinct descriptor sets per kernel, then |
| 1098 | // the descriptor allocator has made different (set,binding) pairs for |
| 1099 | // the same (type,arg_index) pair. Since we can decorate a resource |
| 1100 | // variable with only exactly one DescriptorSet and Binding, we are |
| 1101 | // forced in this case to make distinct resource variables whenever |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 1102 | // the same clspv.resource.var.X function is seen with disintct |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1103 | // (set,binding) values. |
| 1104 | const bool always_distinct_sets = |
| 1105 | clspv::Option::DistinctKernelDescriptorSets(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1106 | for (Function &F : *module) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1107 | // Rely on the fact the resource var functions have a stable ordering |
| 1108 | // in the module. |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1109 | if (F.getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1110 | // Find all calls to this function with distinct set and binding pairs. |
| 1111 | // Save them in ResourceVarInfoList. |
| 1112 | |
| 1113 | // Determine uniqueness of the (set,binding) pairs only withing this |
| 1114 | // one resource-var builtin function. |
| 1115 | using SetAndBinding = std::pair<unsigned, unsigned>; |
| 1116 | // Maps set and binding to the resource var info. |
| 1117 | DenseMap<SetAndBinding, ResourceVarInfo *> set_and_binding_map; |
| 1118 | bool first_use = true; |
| 1119 | for (auto &U : F.uses()) { |
| 1120 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 1121 | const auto set = unsigned( |
| 1122 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 1123 | const auto binding = unsigned( |
| 1124 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 1125 | const auto arg_kind = clspv::ArgKind( |
| 1126 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 1127 | const auto arg_index = unsigned( |
| 1128 | dyn_cast<ConstantInt>(call->getArgOperand(3))->getZExtValue()); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 1129 | const auto coherent = unsigned( |
| 1130 | dyn_cast<ConstantInt>(call->getArgOperand(5))->getZExtValue()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1131 | |
| 1132 | // Find or make the resource var info for this combination. |
| 1133 | ResourceVarInfo *rv = nullptr; |
| 1134 | if (always_distinct_sets) { |
| 1135 | // Make a new resource var any time we see a different |
| 1136 | // (set,binding) pair. |
| 1137 | SetAndBinding key{set, binding}; |
| 1138 | auto where = set_and_binding_map.find(key); |
| 1139 | if (where == set_and_binding_map.end()) { |
| 1140 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 1141 | binding, &F, arg_kind, coherent); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1142 | ResourceVarInfoList.emplace_back(rv); |
| 1143 | set_and_binding_map[key] = rv; |
| 1144 | } else { |
| 1145 | rv = where->second; |
| 1146 | } |
| 1147 | } else { |
| 1148 | // The default is to make exactly one resource for each |
| 1149 | // clspv.resource.var.* function. |
| 1150 | if (first_use) { |
| 1151 | first_use = false; |
| 1152 | rv = new ResourceVarInfo(int(ResourceVarInfoList.size()), set, |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 1153 | binding, &F, arg_kind, coherent); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1154 | ResourceVarInfoList.emplace_back(rv); |
| 1155 | } else { |
| 1156 | rv = ResourceVarInfoList.back().get(); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | // Now populate FunctionToResourceVarsMap. |
| 1161 | auto &mapping = |
| 1162 | FunctionToResourceVarsMap[call->getParent()->getParent()]; |
| 1163 | while (mapping.size() <= arg_index) { |
| 1164 | mapping.push_back(nullptr); |
| 1165 | } |
| 1166 | mapping[arg_index] = rv; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | // Populate ModuleOrderedResourceVars. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1173 | for (Function &F : *module) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1174 | auto where = FunctionToResourceVarsMap.find(&F); |
| 1175 | if (where != FunctionToResourceVarsMap.end()) { |
| 1176 | for (auto &rv : where->second) { |
| 1177 | if (rv != nullptr) { |
| 1178 | ModuleOrderedResourceVars.insert(rv); |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | if (ShowResourceVars) { |
| 1184 | for (auto *info : ModuleOrderedResourceVars) { |
| 1185 | outs() << "MORV index " << info->index << " (" << info->descriptor_set |
| 1186 | << "," << info->binding << ") " << *(info->var_fn->getReturnType()) |
| 1187 | << "\n"; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1192 | bool SPIRVProducerPass::FindExtInst() { |
| 1193 | LLVMContext &Context = module->getContext(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1194 | bool HasExtInst = false; |
| 1195 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1196 | for (Function &F : *module) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1197 | for (BasicBlock &BB : F) { |
| 1198 | for (Instruction &I : BB) { |
| 1199 | if (CallInst *Call = dyn_cast<CallInst>(&I)) { |
| 1200 | Function *Callee = Call->getCalledFunction(); |
| 1201 | // Check whether this call is for extend instructions. |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1202 | auto callee_name = Callee->getName(); |
| 1203 | const glsl::ExtInst EInst = getExtInstEnum(callee_name); |
| 1204 | const glsl::ExtInst IndirectEInst = |
| 1205 | getIndirectExtInstEnum(callee_name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1206 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1207 | HasExtInst |= |
| 1208 | (EInst != kGlslExtInstBad) || (IndirectEInst != kGlslExtInstBad); |
| 1209 | |
| 1210 | if (IndirectEInst) { |
| 1211 | // Register extra constants if needed. |
| 1212 | |
| 1213 | // Registers a type and constant for computing the result of the |
| 1214 | // given instruction. If the result of the instruction is a vector, |
| 1215 | // then make a splat vector constant with the same number of |
| 1216 | // elements. |
| 1217 | auto register_constant = [this, &I](Constant *constant) { |
| 1218 | FindType(constant->getType()); |
| 1219 | FindConstant(constant); |
| 1220 | if (auto *vectorTy = dyn_cast<VectorType>(I.getType())) { |
| 1221 | // Register the splat vector of the value with the same |
| 1222 | // width as the result of the instruction. |
| 1223 | auto *vec_constant = ConstantVector::getSplat( |
alan-baker | 7261e06 | 2020-03-15 14:35:48 -0400 | [diff] [blame] | 1224 | {static_cast<unsigned>(vectorTy->getNumElements()), false}, |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1225 | constant); |
| 1226 | FindConstant(vec_constant); |
| 1227 | FindType(vec_constant->getType()); |
| 1228 | } |
| 1229 | }; |
| 1230 | switch (IndirectEInst) { |
| 1231 | case glsl::ExtInstFindUMsb: |
| 1232 | // clz needs OpExtInst and OpISub with constant 31, or splat |
| 1233 | // vector of 31. Add it to the constant list here. |
| 1234 | register_constant( |
| 1235 | ConstantInt::get(Type::getInt32Ty(Context), 31)); |
| 1236 | break; |
| 1237 | case glsl::ExtInstAcos: |
| 1238 | case glsl::ExtInstAsin: |
Kévin Petit | eb9f90a | 2018-09-29 12:29:34 +0100 | [diff] [blame] | 1239 | case glsl::ExtInstAtan: |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 1240 | case glsl::ExtInstAtan2: |
| 1241 | // We need 1/pi for acospi, asinpi, atan2pi. |
| 1242 | register_constant( |
| 1243 | ConstantFP::get(Type::getFloatTy(Context), kOneOverPi)); |
| 1244 | break; |
| 1245 | default: |
| 1246 | assert(false && "internally inconsistent"); |
| 1247 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return HasExtInst; |
| 1255 | } |
| 1256 | |
| 1257 | void SPIRVProducerPass::FindTypePerGlobalVar(GlobalVariable &GV) { |
| 1258 | // Investigate global variable's type. |
| 1259 | FindType(GV.getType()); |
| 1260 | } |
| 1261 | |
| 1262 | void SPIRVProducerPass::FindTypePerFunc(Function &F) { |
| 1263 | // Investigate function's type. |
| 1264 | FunctionType *FTy = F.getFunctionType(); |
| 1265 | |
| 1266 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 1267 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1268 | // Handle a regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1269 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 1270 | uint32_t GVCstArgIdx = GlobalConstFuncTypeMap[FTy].second; |
| 1271 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1272 | for (unsigned i = 0; i < FTy->getNumParams(); i++) { |
| 1273 | Type *ParamTy = FTy->getParamType(i); |
| 1274 | if (i == GVCstArgIdx) { |
| 1275 | Type *EleTy = ParamTy->getPointerElementType(); |
| 1276 | ParamTy = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1277 | } |
| 1278 | |
| 1279 | NewFuncParamTys.push_back(ParamTy); |
| 1280 | } |
| 1281 | |
| 1282 | FunctionType *NewFTy = |
| 1283 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1284 | GlobalConstFuncTyMap[FTy] = std::make_pair(NewFTy, GVCstArgIdx); |
| 1285 | FTy = NewFTy; |
| 1286 | } |
| 1287 | |
| 1288 | FindType(FTy); |
| 1289 | } else { |
| 1290 | // As kernel functions do not have parameters, create new function type and |
| 1291 | // add it to type map. |
| 1292 | SmallVector<Type *, 4> NewFuncParamTys; |
| 1293 | FunctionType *NewFTy = |
| 1294 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 1295 | FindType(NewFTy); |
| 1296 | } |
| 1297 | |
| 1298 | // Investigate instructions' type in function body. |
| 1299 | for (BasicBlock &BB : F) { |
| 1300 | for (Instruction &I : BB) { |
| 1301 | if (isa<ShuffleVectorInst>(I)) { |
| 1302 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1303 | // Ignore type for mask of shuffle vector instruction. |
| 1304 | if (i == 2) { |
| 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | Value *Op = I.getOperand(i); |
| 1309 | if (!isa<MetadataAsValue>(Op)) { |
| 1310 | FindType(Op->getType()); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | FindType(I.getType()); |
| 1315 | continue; |
| 1316 | } |
| 1317 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1318 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 1319 | |
| 1320 | if (Call && Call->getCalledFunction()->getName().startswith( |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1321 | clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1322 | // This is a fake call representing access to a resource variable. |
| 1323 | // We handle that elsewhere. |
| 1324 | continue; |
| 1325 | } |
| 1326 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1327 | if (Call && Call->getCalledFunction()->getName().startswith( |
| 1328 | clspv::WorkgroupAccessorFunction())) { |
| 1329 | // This is a fake call representing access to a workgroup variable. |
| 1330 | // We handle that elsewhere. |
| 1331 | continue; |
| 1332 | } |
| 1333 | |
alan-baker | f083bed | 2020-01-29 08:15:42 -0500 | [diff] [blame] | 1334 | // #497: InsertValue and ExtractValue map to OpCompositeInsert and |
| 1335 | // OpCompositeExtract which takes literal values for indices. As a result |
| 1336 | // don't map the type of indices. |
| 1337 | if (I.getOpcode() == Instruction::ExtractValue) { |
| 1338 | FindType(I.getOperand(0)->getType()); |
| 1339 | continue; |
| 1340 | } |
| 1341 | if (I.getOpcode() == Instruction::InsertValue) { |
| 1342 | FindType(I.getOperand(0)->getType()); |
| 1343 | FindType(I.getOperand(1)->getType()); |
| 1344 | continue; |
| 1345 | } |
| 1346 | |
| 1347 | // #497: InsertElement and ExtractElement map to OpCompositeExtract if |
| 1348 | // the index is a constant. In such a case don't map the index type. |
| 1349 | if (I.getOpcode() == Instruction::ExtractElement) { |
| 1350 | FindType(I.getOperand(0)->getType()); |
| 1351 | Value *op1 = I.getOperand(1); |
| 1352 | if (!isa<Constant>(op1) || isa<GlobalValue>(op1)) { |
| 1353 | FindType(op1->getType()); |
| 1354 | } |
| 1355 | continue; |
| 1356 | } |
| 1357 | if (I.getOpcode() == Instruction::InsertElement) { |
| 1358 | FindType(I.getOperand(0)->getType()); |
| 1359 | FindType(I.getOperand(1)->getType()); |
| 1360 | Value *op2 = I.getOperand(2); |
| 1361 | if (!isa<Constant>(op2) || isa<GlobalValue>(op2)) { |
| 1362 | FindType(op2->getType()); |
| 1363 | } |
| 1364 | continue; |
| 1365 | } |
| 1366 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1367 | // Work through the operands of the instruction. |
| 1368 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1369 | Value *const Op = I.getOperand(i); |
| 1370 | // If any of the operands is a constant, find the type! |
| 1371 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1372 | FindType(Op->getType()); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | for (Use &Op : I.operands()) { |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame] | 1377 | if (isa<CallInst>(&I)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1378 | // Avoid to check call instruction's type. |
| 1379 | break; |
| 1380 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1381 | if (CallInst *OpCall = dyn_cast<CallInst>(Op)) { |
| 1382 | if (OpCall && OpCall->getCalledFunction()->getName().startswith( |
| 1383 | clspv::WorkgroupAccessorFunction())) { |
| 1384 | // This is a fake call representing access to a workgroup variable. |
| 1385 | // We handle that elsewhere. |
| 1386 | continue; |
| 1387 | } |
| 1388 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1389 | if (!isa<MetadataAsValue>(&Op)) { |
| 1390 | FindType(Op->getType()); |
| 1391 | continue; |
| 1392 | } |
| 1393 | } |
| 1394 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1395 | // We don't want to track the type of this call as we are going to replace |
| 1396 | // it. |
Kévin Petit | df71de3 | 2019-04-09 14:09:50 +0100 | [diff] [blame] | 1397 | if (Call && (clspv::LiteralSamplerFunction() == |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1398 | Call->getCalledFunction()->getName())) { |
| 1399 | continue; |
| 1400 | } |
| 1401 | |
| 1402 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) { |
| 1403 | // If gep's base operand has ModuleScopePrivate address space, make gep |
| 1404 | // return ModuleScopePrivate address space. |
| 1405 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate) { |
| 1406 | // Add pointer type with private address space for global constant to |
| 1407 | // type list. |
| 1408 | Type *EleTy = I.getType()->getPointerElementType(); |
| 1409 | Type *NewPTy = |
| 1410 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 1411 | |
| 1412 | FindType(NewPTy); |
| 1413 | continue; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | FindType(I.getType()); |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1422 | void SPIRVProducerPass::FindTypesForSamplerMap() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1423 | // If we are using a sampler map, find the type of the sampler. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1424 | if (module->getFunction(clspv::LiteralSamplerFunction()) || |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1425 | 0 < getSamplerMap().size()) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1426 | auto SamplerStructTy = module->getTypeByName("opencl.sampler_t"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1427 | if (!SamplerStructTy) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1428 | SamplerStructTy = |
| 1429 | StructType::create(module->getContext(), "opencl.sampler_t"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | SamplerTy = SamplerStructTy->getPointerTo(AddressSpace::UniformConstant); |
| 1433 | |
| 1434 | FindType(SamplerTy); |
| 1435 | } |
| 1436 | } |
| 1437 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1438 | void SPIRVProducerPass::FindTypesForResourceVars() { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1439 | // Record types so they are generated. |
| 1440 | TypesNeedingLayout.reset(); |
| 1441 | StructTypesNeedingBlock.reset(); |
| 1442 | |
| 1443 | // To match older clspv codegen, generate the float type first if required |
| 1444 | // for images. |
| 1445 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1446 | if (info->arg_kind == clspv::ArgKind::ReadOnlyImage || |
| 1447 | info->arg_kind == clspv::ArgKind::WriteOnlyImage) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1448 | if (IsIntImageType(info->var_fn->getReturnType())) { |
| 1449 | // Nothing for now... |
| 1450 | } else if (IsUintImageType(info->var_fn->getReturnType())) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1451 | FindType(Type::getInt32Ty(module->getContext())); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // We need "float" either for the sampled type or for the Lod operand. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1455 | FindType(Type::getFloatTy(module->getContext())); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | for (const auto *info : ModuleOrderedResourceVars) { |
| 1460 | Type *type = info->var_fn->getReturnType(); |
| 1461 | |
| 1462 | switch (info->arg_kind) { |
| 1463 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1464 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1465 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1466 | StructTypesNeedingBlock.insert(sty); |
| 1467 | } else { |
| 1468 | errs() << *type << "\n"; |
| 1469 | llvm_unreachable("Buffer arguments must map to structures!"); |
| 1470 | } |
| 1471 | break; |
| 1472 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 1473 | case clspv::ArgKind::PodUBO: |
| 1474 | case clspv::ArgKind::PodPushConstant: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1475 | if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) { |
| 1476 | StructTypesNeedingBlock.insert(sty); |
| 1477 | } else { |
| 1478 | errs() << *type << "\n"; |
| 1479 | llvm_unreachable("POD arguments must map to structures!"); |
| 1480 | } |
| 1481 | break; |
| 1482 | case clspv::ArgKind::ReadOnlyImage: |
| 1483 | case clspv::ArgKind::WriteOnlyImage: |
| 1484 | case clspv::ArgKind::Sampler: |
| 1485 | // Sampler and image types map to the pointee type but |
| 1486 | // in the uniform constant address space. |
| 1487 | type = PointerType::get(type->getPointerElementType(), |
| 1488 | clspv::AddressSpace::UniformConstant); |
| 1489 | break; |
| 1490 | default: |
| 1491 | break; |
| 1492 | } |
| 1493 | |
| 1494 | // The converted type is the type of the OpVariable we will generate. |
| 1495 | // If the pointee type is an array of size zero, FindType will convert it |
| 1496 | // to a runtime array. |
| 1497 | FindType(type); |
| 1498 | } |
| 1499 | |
alan-baker | dcd9741 | 2019-09-16 15:32:30 -0400 | [diff] [blame] | 1500 | // If module constants are clustered in a storage buffer then that struct |
| 1501 | // needs layout decorations. |
| 1502 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1503 | for (GlobalVariable &GV : module->globals()) { |
alan-baker | dcd9741 | 2019-09-16 15:32:30 -0400 | [diff] [blame] | 1504 | PointerType *PTy = cast<PointerType>(GV.getType()); |
| 1505 | const auto AS = PTy->getAddressSpace(); |
| 1506 | const bool module_scope_constant_external_init = |
| 1507 | (AS == AddressSpace::Constant) && GV.hasInitializer(); |
| 1508 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 1509 | if (module_scope_constant_external_init && |
| 1510 | spv::BuiltInMax == BuiltinType) { |
| 1511 | StructTypesNeedingBlock.insert( |
| 1512 | cast<StructType>(PTy->getPointerElementType())); |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1517 | for (const GlobalVariable &GV : module->globals()) { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 1518 | if (GV.getAddressSpace() == clspv::AddressSpace::PushConstant) { |
| 1519 | auto Ty = cast<PointerType>(GV.getType())->getPointerElementType(); |
| 1520 | assert(Ty->isStructTy() && "Push constants have to be structures."); |
| 1521 | auto STy = cast<StructType>(Ty); |
| 1522 | StructTypesNeedingBlock.insert(STy); |
| 1523 | } |
| 1524 | } |
| 1525 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1526 | // Traverse the arrays and structures underneath each Block, and |
| 1527 | // mark them as needing layout. |
| 1528 | std::vector<Type *> work_list(StructTypesNeedingBlock.begin(), |
| 1529 | StructTypesNeedingBlock.end()); |
| 1530 | while (!work_list.empty()) { |
| 1531 | Type *type = work_list.back(); |
| 1532 | work_list.pop_back(); |
| 1533 | TypesNeedingLayout.insert(type); |
| 1534 | switch (type->getTypeID()) { |
| 1535 | case Type::ArrayTyID: |
| 1536 | work_list.push_back(type->getArrayElementType()); |
| 1537 | if (!Hack_generate_runtime_array_stride_early) { |
| 1538 | // Remember this array type for deferred decoration. |
| 1539 | TypesNeedingArrayStride.insert(type); |
| 1540 | } |
| 1541 | break; |
| 1542 | case Type::StructTyID: |
| 1543 | for (auto *elem_ty : cast<StructType>(type)->elements()) { |
| 1544 | work_list.push_back(elem_ty); |
| 1545 | } |
| 1546 | default: |
| 1547 | // This type and its contained types don't get layout. |
| 1548 | break; |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1553 | void SPIRVProducerPass::FindWorkgroupVars() { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1554 | // The SpecId assignment for pointer-to-local arguments is recorded in |
| 1555 | // module-level metadata. Translate that information into local argument |
| 1556 | // information. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1557 | NamedMDNode *nmd = module->getNamedMetadata(clspv::LocalSpecIdMetadataName()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1558 | if (!nmd) |
| 1559 | return; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1560 | for (auto operand : nmd->operands()) { |
| 1561 | MDTuple *tuple = cast<MDTuple>(operand); |
| 1562 | ValueAsMetadata *fn_md = cast<ValueAsMetadata>(tuple->getOperand(0)); |
| 1563 | Function *func = cast<Function>(fn_md->getValue()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1564 | ConstantAsMetadata *arg_index_md = |
| 1565 | cast<ConstantAsMetadata>(tuple->getOperand(1)); |
| 1566 | int arg_index = static_cast<int>( |
| 1567 | cast<ConstantInt>(arg_index_md->getValue())->getSExtValue()); |
| 1568 | Argument *arg = &*(func->arg_begin() + arg_index); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1569 | |
| 1570 | ConstantAsMetadata *spec_id_md = |
| 1571 | cast<ConstantAsMetadata>(tuple->getOperand(2)); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1572 | int spec_id = static_cast<int>( |
| 1573 | cast<ConstantInt>(spec_id_md->getValue())->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1574 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1575 | LocalArgSpecIds[arg] = spec_id; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1576 | if (LocalSpecIdInfoMap.count(spec_id)) |
| 1577 | continue; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1578 | |
| 1579 | // We haven't seen this SpecId yet, so generate the LocalArgInfo for it. |
| 1580 | LocalArgInfo info{nextID, arg->getType()->getPointerElementType(), |
| 1581 | nextID + 1, nextID + 2, |
| 1582 | nextID + 3, spec_id}; |
| 1583 | LocalSpecIdInfoMap[spec_id] = info; |
| 1584 | nextID += 4; |
| 1585 | |
| 1586 | // Ensure the types necessary for this argument get generated. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1587 | Type *IdxTy = Type::getInt32Ty(module->getContext()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1588 | FindConstant(ConstantInt::get(IdxTy, 0)); |
| 1589 | FindType(IdxTy); |
| 1590 | FindType(arg->getType()); |
| 1591 | } |
| 1592 | } |
| 1593 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1594 | void SPIRVProducerPass::FindType(Type *Ty) { |
| 1595 | TypeList &TyList = getTypeList(); |
| 1596 | |
| 1597 | if (0 != TyList.idFor(Ty)) { |
| 1598 | return; |
| 1599 | } |
| 1600 | |
| 1601 | if (Ty->isPointerTy()) { |
| 1602 | auto AddrSpace = Ty->getPointerAddressSpace(); |
| 1603 | if ((AddressSpace::Constant == AddrSpace) || |
| 1604 | (AddressSpace::Global == AddrSpace)) { |
| 1605 | auto PointeeTy = Ty->getPointerElementType(); |
| 1606 | |
| 1607 | if (PointeeTy->isStructTy() && |
| 1608 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 1609 | FindType(PointeeTy); |
| 1610 | auto ActualPointerTy = |
| 1611 | PointeeTy->getPointerTo(AddressSpace::UniformConstant); |
| 1612 | FindType(ActualPointerTy); |
| 1613 | return; |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1618 | // By convention, LLVM array type with 0 elements will map to |
| 1619 | // OpTypeRuntimeArray. Otherwise, it will map to OpTypeArray, which |
| 1620 | // has a constant number of elements. We need to support type of the |
| 1621 | // constant. |
| 1622 | if (auto *arrayTy = dyn_cast<ArrayType>(Ty)) { |
| 1623 | if (arrayTy->getNumElements() > 0) { |
| 1624 | LLVMContext &Context = Ty->getContext(); |
| 1625 | FindType(Type::getInt32Ty(Context)); |
| 1626 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | for (Type *SubTy : Ty->subtypes()) { |
| 1630 | FindType(SubTy); |
| 1631 | } |
| 1632 | |
| 1633 | TyList.insert(Ty); |
| 1634 | } |
| 1635 | |
| 1636 | void SPIRVProducerPass::FindConstantPerGlobalVar(GlobalVariable &GV) { |
| 1637 | // If the global variable has a (non undef) initializer. |
| 1638 | if (GV.hasInitializer() && !isa<UndefValue>(GV.getInitializer())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1639 | // Generate the constant if it's not the initializer to a module scope |
| 1640 | // constant that we will expect in a storage buffer. |
| 1641 | const bool module_scope_constant_external_init = |
| 1642 | (GV.getType()->getPointerAddressSpace() == AddressSpace::Constant) && |
| 1643 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 1644 | if (!module_scope_constant_external_init) { |
| 1645 | FindConstant(GV.getInitializer()); |
| 1646 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | void SPIRVProducerPass::FindConstantPerFunc(Function &F) { |
| 1651 | // Investigate constants in function body. |
| 1652 | for (BasicBlock &BB : F) { |
| 1653 | for (Instruction &I : BB) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1654 | if (auto *call = dyn_cast<CallInst>(&I)) { |
| 1655 | auto name = call->getCalledFunction()->getName(); |
Kévin Petit | df71de3 | 2019-04-09 14:09:50 +0100 | [diff] [blame] | 1656 | if (name == clspv::LiteralSamplerFunction()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1657 | // We've handled these constants elsewhere, so skip it. |
| 1658 | continue; |
| 1659 | } |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 1660 | if (name.startswith(clspv::ResourceAccessorFunction())) { |
| 1661 | continue; |
| 1662 | } |
| 1663 | if (name.startswith(clspv::WorkgroupAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1664 | continue; |
| 1665 | } |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 1666 | if (name.startswith(clspv::SPIRVOpIntrinsicFunction())) { |
| 1667 | // Skip the first operand that has the SPIR-V Opcode |
| 1668 | for (unsigned i = 1; i < I.getNumOperands(); i++) { |
| 1669 | if (isa<Constant>(I.getOperand(i)) && |
| 1670 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1671 | FindConstant(I.getOperand(i)); |
| 1672 | } |
| 1673 | } |
| 1674 | continue; |
| 1675 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | if (isa<AllocaInst>(I)) { |
| 1679 | // Alloca instruction has constant for the number of element. Ignore it. |
| 1680 | continue; |
| 1681 | } else if (isa<ShuffleVectorInst>(I)) { |
| 1682 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1683 | // Ignore constant for mask of shuffle vector instruction. |
| 1684 | if (i == 2) { |
| 1685 | continue; |
| 1686 | } |
| 1687 | |
| 1688 | if (isa<Constant>(I.getOperand(i)) && |
| 1689 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1690 | FindConstant(I.getOperand(i)); |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | continue; |
| 1695 | } else if (isa<InsertElementInst>(I)) { |
| 1696 | // Handle InsertElement with <4 x i8> specially. |
| 1697 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 1698 | if (is4xi8vec(CompositeTy)) { |
| 1699 | LLVMContext &Context = CompositeTy->getContext(); |
| 1700 | if (isa<Constant>(I.getOperand(0))) { |
| 1701 | FindConstant(I.getOperand(0)); |
| 1702 | } |
| 1703 | |
| 1704 | if (isa<Constant>(I.getOperand(1))) { |
| 1705 | FindConstant(I.getOperand(1)); |
| 1706 | } |
| 1707 | |
| 1708 | // Add mask constant 0xFF. |
| 1709 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 1710 | FindConstant(CstFF); |
| 1711 | |
| 1712 | // Add shift amount constant. |
| 1713 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 1714 | uint64_t Idx = CI->getZExtValue(); |
| 1715 | Constant *CstShiftAmount = |
| 1716 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 1717 | FindConstant(CstShiftAmount); |
| 1718 | } |
| 1719 | |
| 1720 | continue; |
| 1721 | } |
| 1722 | |
| 1723 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1724 | // Ignore constant for index of InsertElement instruction. |
| 1725 | if (i == 2) { |
| 1726 | continue; |
| 1727 | } |
| 1728 | |
| 1729 | if (isa<Constant>(I.getOperand(i)) && |
| 1730 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1731 | FindConstant(I.getOperand(i)); |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | continue; |
| 1736 | } else if (isa<ExtractElementInst>(I)) { |
| 1737 | // Handle ExtractElement with <4 x i8> specially. |
| 1738 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 1739 | if (is4xi8vec(CompositeTy)) { |
| 1740 | LLVMContext &Context = CompositeTy->getContext(); |
| 1741 | if (isa<Constant>(I.getOperand(0))) { |
| 1742 | FindConstant(I.getOperand(0)); |
| 1743 | } |
| 1744 | |
| 1745 | // Add mask constant 0xFF. |
| 1746 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 1747 | FindConstant(CstFF); |
| 1748 | |
| 1749 | // Add shift amount constant. |
| 1750 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 1751 | uint64_t Idx = CI->getZExtValue(); |
| 1752 | Constant *CstShiftAmount = |
| 1753 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 1754 | FindConstant(CstShiftAmount); |
| 1755 | } else { |
| 1756 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
| 1757 | FindConstant(Cst8); |
| 1758 | } |
| 1759 | |
| 1760 | continue; |
| 1761 | } |
| 1762 | |
| 1763 | for (unsigned i = 0; i < I.getNumOperands(); i++) { |
| 1764 | // Ignore constant for index of ExtractElement instruction. |
| 1765 | if (i == 1) { |
| 1766 | continue; |
| 1767 | } |
| 1768 | |
| 1769 | if (isa<Constant>(I.getOperand(i)) && |
| 1770 | !isa<GlobalValue>(I.getOperand(i))) { |
| 1771 | FindConstant(I.getOperand(i)); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | continue; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1776 | } else if ((Instruction::Xor == I.getOpcode()) && |
| 1777 | I.getType()->isIntegerTy(1)) { |
| 1778 | // We special case for Xor where the type is i1 and one of the arguments |
| 1779 | // is a constant 1 (true), this is an OpLogicalNot in SPIR-V, and we |
| 1780 | // don't need the constant |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1781 | bool foundConstantTrue = false; |
| 1782 | for (Use &Op : I.operands()) { |
| 1783 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1784 | auto CI = cast<ConstantInt>(Op); |
| 1785 | |
| 1786 | if (CI->isZero() || foundConstantTrue) { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1787 | // If we already found the true constant, we might (probably only |
| 1788 | // on -O0) have an OpLogicalNot which is taking a constant |
| 1789 | // argument, so discover it anyway. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1790 | FindConstant(Op); |
| 1791 | } else { |
| 1792 | foundConstantTrue = true; |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | continue; |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 1798 | } else if (isa<TruncInst>(I)) { |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 1799 | // Special case if i8 is not generally handled. |
| 1800 | if (!clspv::Option::Int8Support()) { |
| 1801 | // For truncation to i8 we mask against 255. |
| 1802 | Type *ToTy = I.getType(); |
| 1803 | if (8u == ToTy->getPrimitiveSizeInBits()) { |
| 1804 | LLVMContext &Context = ToTy->getContext(); |
| 1805 | Constant *Cst255 = |
| 1806 | ConstantInt::get(Type::getInt32Ty(Context), 0xff); |
| 1807 | FindConstant(Cst255); |
| 1808 | } |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 1809 | } |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 1810 | } else if (isa<AtomicRMWInst>(I)) { |
| 1811 | LLVMContext &Context = I.getContext(); |
| 1812 | |
| 1813 | FindConstant( |
| 1814 | ConstantInt::get(Type::getInt32Ty(Context), spv::ScopeDevice)); |
| 1815 | FindConstant(ConstantInt::get( |
| 1816 | Type::getInt32Ty(Context), |
| 1817 | spv::MemorySemanticsUniformMemoryMask | |
| 1818 | spv::MemorySemanticsSequentiallyConsistentMask)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | for (Use &Op : I.operands()) { |
| 1822 | if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) { |
| 1823 | FindConstant(Op); |
| 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | void SPIRVProducerPass::FindConstant(Value *V) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1831 | ValueList &CstList = getConstantList(); |
| 1832 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1833 | // If V is already tracked, ignore it. |
| 1834 | if (0 != CstList.idFor(V)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1835 | return; |
| 1836 | } |
| 1837 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1838 | if (isa<GlobalValue>(V) && clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 1839 | return; |
| 1840 | } |
| 1841 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1842 | Constant *Cst = cast<Constant>(V); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1843 | Type *CstTy = Cst->getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1844 | |
| 1845 | // Handle constant with <4 x i8> type specially. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1846 | if (is4xi8vec(CstTy)) { |
| 1847 | if (!isa<GlobalValue>(V)) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1848 | CstList.insert(V); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | if (Cst->getNumOperands()) { |
| 1853 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); I != E; |
| 1854 | ++I) { |
| 1855 | FindConstant(*I); |
| 1856 | } |
| 1857 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1858 | CstList.insert(Cst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1859 | return; |
| 1860 | } else if (const ConstantDataSequential *CDS = |
| 1861 | dyn_cast<ConstantDataSequential>(Cst)) { |
| 1862 | // Add constants for each element to constant list. |
| 1863 | for (unsigned i = 0; i < CDS->getNumElements(); i++) { |
| 1864 | Constant *EleCst = CDS->getElementAsConstant(i); |
| 1865 | FindConstant(EleCst); |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | if (!isa<GlobalValue>(V)) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 1870 | CstList.insert(V); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | spv::StorageClass SPIRVProducerPass::GetStorageClass(unsigned AddrSpace) const { |
| 1875 | switch (AddrSpace) { |
| 1876 | default: |
| 1877 | llvm_unreachable("Unsupported OpenCL address space"); |
| 1878 | case AddressSpace::Private: |
| 1879 | return spv::StorageClassFunction; |
| 1880 | case AddressSpace::Global: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1881 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1882 | case AddressSpace::Constant: |
| 1883 | return clspv::Option::ConstantArgsInUniformBuffer() |
| 1884 | ? spv::StorageClassUniform |
| 1885 | : spv::StorageClassStorageBuffer; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1886 | case AddressSpace::Input: |
| 1887 | return spv::StorageClassInput; |
| 1888 | case AddressSpace::Local: |
| 1889 | return spv::StorageClassWorkgroup; |
| 1890 | case AddressSpace::UniformConstant: |
| 1891 | return spv::StorageClassUniformConstant; |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 1892 | case AddressSpace::Uniform: |
David Neto | e439d70 | 2018-03-23 13:14:08 -0700 | [diff] [blame] | 1893 | return spv::StorageClassUniform; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1894 | case AddressSpace::ModuleScopePrivate: |
| 1895 | return spv::StorageClassPrivate; |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 1896 | case AddressSpace::PushConstant: |
| 1897 | return spv::StorageClassPushConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1901 | spv::StorageClass |
| 1902 | SPIRVProducerPass::GetStorageClassForArgKind(clspv::ArgKind arg_kind) const { |
| 1903 | switch (arg_kind) { |
| 1904 | case clspv::ArgKind::Buffer: |
| 1905 | return spv::StorageClassStorageBuffer; |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1906 | case clspv::ArgKind::BufferUBO: |
| 1907 | return spv::StorageClassUniform; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1908 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 1909 | return spv::StorageClassStorageBuffer; |
| 1910 | case clspv::ArgKind::PodUBO: |
| 1911 | return spv::StorageClassUniform; |
| 1912 | case clspv::ArgKind::PodPushConstant: |
| 1913 | return spv::StorageClassPushConstant; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1914 | case clspv::ArgKind::Local: |
| 1915 | return spv::StorageClassWorkgroup; |
| 1916 | case clspv::ArgKind::ReadOnlyImage: |
| 1917 | case clspv::ArgKind::WriteOnlyImage: |
| 1918 | case clspv::ArgKind::Sampler: |
| 1919 | return spv::StorageClassUniformConstant; |
Radek Szymanski | be4b0c4 | 2018-10-04 22:20:53 +0100 | [diff] [blame] | 1920 | default: |
| 1921 | llvm_unreachable("Unsupported storage class for argument kind"); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1925 | spv::BuiltIn SPIRVProducerPass::GetBuiltin(StringRef Name) const { |
| 1926 | return StringSwitch<spv::BuiltIn>(Name) |
| 1927 | .Case("__spirv_GlobalInvocationId", spv::BuiltInGlobalInvocationId) |
| 1928 | .Case("__spirv_LocalInvocationId", spv::BuiltInLocalInvocationId) |
| 1929 | .Case("__spirv_WorkgroupSize", spv::BuiltInWorkgroupSize) |
| 1930 | .Case("__spirv_NumWorkgroups", spv::BuiltInNumWorkgroups) |
| 1931 | .Case("__spirv_WorkgroupId", spv::BuiltInWorkgroupId) |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 1932 | .Case("__spirv_WorkDim", spv::BuiltInWorkDim) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1933 | .Default(spv::BuiltInMax); |
| 1934 | } |
| 1935 | |
| 1936 | void SPIRVProducerPass::GenerateExtInstImport() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 1937 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kImports); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1938 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 1939 | |
| 1940 | // |
| 1941 | // Generate OpExtInstImport. |
| 1942 | // |
| 1943 | // Ops[0] ... Ops[n] = Name (Literal String) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1944 | ExtInstImportID = nextID; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 1945 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpExtInstImport, nextID++, |
| 1946 | MkString("GLSL.std.450"))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1949 | void SPIRVProducerPass::GenerateSPIRVTypes() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 1950 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kTypes); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1951 | ValueMapType &VMap = getValueMap(); |
| 1952 | ValueMapType &AllocatedVMap = getAllocatedValueMap(); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 1953 | const auto &DL = module->getDataLayout(); |
| 1954 | LLVMContext &Context = module->getContext(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1955 | |
| 1956 | // Map for OpTypeRuntimeArray. If argument has pointer type, 2 spirv type |
| 1957 | // instructions are generated. They are OpTypePointer and OpTypeRuntimeArray. |
| 1958 | DenseMap<Type *, uint32_t> OpRuntimeTyMap; |
| 1959 | |
| 1960 | for (Type *Ty : getTypeList()) { |
| 1961 | // Update TypeMap with nextID for reference later. |
| 1962 | TypeMap[Ty] = nextID; |
| 1963 | |
| 1964 | switch (Ty->getTypeID()) { |
| 1965 | default: { |
| 1966 | Ty->print(errs()); |
| 1967 | llvm_unreachable("Unsupported type???"); |
| 1968 | break; |
| 1969 | } |
| 1970 | case Type::MetadataTyID: |
| 1971 | case Type::LabelTyID: { |
| 1972 | // Ignore these types. |
| 1973 | break; |
| 1974 | } |
| 1975 | case Type::PointerTyID: { |
| 1976 | PointerType *PTy = cast<PointerType>(Ty); |
| 1977 | unsigned AddrSpace = PTy->getAddressSpace(); |
| 1978 | |
| 1979 | // For the purposes of our Vulkan SPIR-V type system, constant and global |
| 1980 | // are conflated. |
| 1981 | bool UseExistingOpTypePointer = false; |
| 1982 | if (AddressSpace::Constant == AddrSpace) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1983 | if (!clspv::Option::ConstantArgsInUniformBuffer()) { |
| 1984 | AddrSpace = AddressSpace::Global; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1985 | // Check to see if we already created this type (for instance, if we |
| 1986 | // had a constant <type>* and a global <type>*, the type would be |
| 1987 | // created by one of these types, and shared by both). |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1988 | auto GlobalTy = PTy->getPointerElementType()->getPointerTo(AddrSpace); |
| 1989 | if (0 < TypeMap.count(GlobalTy)) { |
| 1990 | TypeMap[PTy] = TypeMap[GlobalTy]; |
| 1991 | UseExistingOpTypePointer = true; |
| 1992 | break; |
| 1993 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1994 | } |
| 1995 | } else if (AddressSpace::Global == AddrSpace) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 1996 | if (!clspv::Option::ConstantArgsInUniformBuffer()) { |
| 1997 | AddrSpace = AddressSpace::Constant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 1998 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 1999 | // Check to see if we already created this type (for instance, if we |
| 2000 | // had a constant <type>* and a global <type>*, the type would be |
| 2001 | // created by one of these types, and shared by both). |
| 2002 | auto ConstantTy = |
| 2003 | PTy->getPointerElementType()->getPointerTo(AddrSpace); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2004 | if (0 < TypeMap.count(ConstantTy)) { |
| 2005 | TypeMap[PTy] = TypeMap[ConstantTy]; |
| 2006 | UseExistingOpTypePointer = true; |
| 2007 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2011 | const bool HasArgUser = true; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2012 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2013 | if (HasArgUser && !UseExistingOpTypePointer) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2014 | // |
| 2015 | // Generate OpTypePointer. |
| 2016 | // |
| 2017 | |
| 2018 | // OpTypePointer |
| 2019 | // Ops[0] = Storage Class |
| 2020 | // Ops[1] = Element Type ID |
| 2021 | SPIRVOperandList Ops; |
| 2022 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2023 | Ops << MkNum(GetStorageClass(AddrSpace)) |
| 2024 | << MkId(lookupType(PTy->getElementType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2025 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2026 | auto *Inst = new SPIRVInstruction(spv::OpTypePointer, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2027 | SPIRVInstList.push_back(Inst); |
| 2028 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2029 | break; |
| 2030 | } |
| 2031 | case Type::StructTyID: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2032 | StructType *STy = cast<StructType>(Ty); |
| 2033 | |
| 2034 | // Handle sampler type. |
| 2035 | if (STy->isOpaque()) { |
| 2036 | if (STy->getName().equals("opencl.sampler_t")) { |
| 2037 | // |
| 2038 | // Generate OpTypeSampler |
| 2039 | // |
| 2040 | // Empty Ops. |
| 2041 | SPIRVOperandList Ops; |
| 2042 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2043 | auto *Inst = new SPIRVInstruction(spv::OpTypeSampler, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2044 | SPIRVInstList.push_back(Inst); |
| 2045 | break; |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 2046 | } else if (STy->getName().startswith("opencl.image1d_ro_t") || |
| 2047 | STy->getName().startswith("opencl.image1d_wo_t") || |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 2048 | STy->getName().startswith("opencl.image1d_array_ro_t") || |
| 2049 | STy->getName().startswith("opencl.image1d_array_wo_t") || |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 2050 | STy->getName().startswith("opencl.image2d_ro_t") || |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 2051 | STy->getName().startswith("opencl.image2d_wo_t") || |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 2052 | STy->getName().startswith("opencl.image2d_array_ro_t") || |
| 2053 | STy->getName().startswith("opencl.image2d_array_wo_t") || |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 2054 | STy->getName().startswith("opencl.image3d_ro_t") || |
| 2055 | STy->getName().startswith("opencl.image3d_wo_t")) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2056 | // |
| 2057 | // Generate OpTypeImage |
| 2058 | // |
| 2059 | // Ops[0] = Sampled Type ID |
| 2060 | // Ops[1] = Dim ID |
| 2061 | // Ops[2] = Depth (Literal Number) |
| 2062 | // Ops[3] = Arrayed (Literal Number) |
| 2063 | // Ops[4] = MS (Literal Number) |
| 2064 | // Ops[5] = Sampled (Literal Number) |
| 2065 | // Ops[6] = Image Format ID |
| 2066 | // |
| 2067 | SPIRVOperandList Ops; |
| 2068 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 2069 | uint32_t ImageTyID = nextID++; |
| 2070 | uint32_t SampledTyID = 0; |
| 2071 | if (STy->getName().contains(".float")) { |
| 2072 | SampledTyID = lookupType(Type::getFloatTy(Context)); |
| 2073 | } else if (STy->getName().contains(".uint")) { |
| 2074 | SampledTyID = lookupType(Type::getInt32Ty(Context)); |
| 2075 | } else if (STy->getName().contains(".int")) { |
| 2076 | // Generate a signed 32-bit integer if necessary. |
| 2077 | if (int32ID == 0) { |
| 2078 | int32ID = nextID++; |
| 2079 | SPIRVOperandList intOps; |
| 2080 | intOps << MkNum(32); |
| 2081 | intOps << MkNum(1); |
| 2082 | auto signed_int = |
| 2083 | new SPIRVInstruction(spv::OpTypeInt, int32ID, intOps); |
| 2084 | SPIRVInstList.push_back(signed_int); |
| 2085 | } |
| 2086 | SampledTyID = int32ID; |
| 2087 | |
| 2088 | // Generate a vec4 of the signed int if necessary. |
| 2089 | if (v4int32ID == 0) { |
| 2090 | v4int32ID = nextID++; |
| 2091 | SPIRVOperandList vecOps; |
| 2092 | vecOps << MkId(int32ID); |
| 2093 | vecOps << MkNum(4); |
| 2094 | auto int_vec = |
| 2095 | new SPIRVInstruction(spv::OpTypeVector, v4int32ID, vecOps); |
| 2096 | SPIRVInstList.push_back(int_vec); |
| 2097 | } |
| 2098 | } else { |
| 2099 | // This was likely an UndefValue. |
| 2100 | SampledTyID = lookupType(Type::getFloatTy(Context)); |
| 2101 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2102 | Ops << MkId(SampledTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2103 | |
| 2104 | spv::Dim DimID = spv::Dim2D; |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 2105 | if (STy->getName().startswith("opencl.image1d_ro_t") || |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 2106 | STy->getName().startswith("opencl.image1d_wo_t") || |
| 2107 | STy->getName().startswith("opencl.image1d_array_ro_t") || |
| 2108 | STy->getName().startswith("opencl.image1d_array_wo_t")) { |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 2109 | DimID = spv::Dim1D; |
| 2110 | } else if (STy->getName().startswith("opencl.image3d_ro_t") || |
| 2111 | STy->getName().startswith("opencl.image3d_wo_t")) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2112 | DimID = spv::Dim3D; |
| 2113 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2114 | Ops << MkNum(DimID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2115 | |
| 2116 | // TODO: Set up Depth. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2117 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2118 | |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 2119 | uint32_t arrayed = STy->getName().contains("_array_") ? 1 : 0; |
| 2120 | Ops << MkNum(arrayed); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2121 | |
| 2122 | // TODO: Set up MS. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2123 | Ops << MkNum(0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2124 | |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 2125 | // Set up Sampled. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2126 | // |
| 2127 | // From Spec |
| 2128 | // |
| 2129 | // 0 indicates this is only known at run time, not at compile time |
| 2130 | // 1 indicates will be used with sampler |
| 2131 | // 2 indicates will be used without a sampler (a storage image) |
| 2132 | uint32_t Sampled = 1; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 2133 | if (!STy->getName().contains(".sampled")) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2134 | Sampled = 2; |
| 2135 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2136 | Ops << MkNum(Sampled); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2137 | |
| 2138 | // TODO: Set up Image Format. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2139 | Ops << MkNum(spv::ImageFormatUnknown); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2140 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 2141 | auto *Inst = new SPIRVInstruction(spv::OpTypeImage, ImageTyID, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2142 | SPIRVInstList.push_back(Inst); |
| 2143 | break; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | // |
| 2148 | // Generate OpTypeStruct |
| 2149 | // |
| 2150 | // Ops[0] ... Ops[n] = Member IDs |
| 2151 | SPIRVOperandList Ops; |
| 2152 | |
| 2153 | for (auto *EleTy : STy->elements()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2154 | Ops << MkId(lookupType(EleTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2155 | } |
| 2156 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2157 | uint32_t STyID = nextID; |
| 2158 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2159 | auto *Inst = new SPIRVInstruction(spv::OpTypeStruct, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2160 | SPIRVInstList.push_back(Inst); |
| 2161 | |
| 2162 | // Generate OpMemberDecorate. |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2163 | if (TypesNeedingLayout.idFor(STy)) { |
| 2164 | for (unsigned MemberIdx = 0; MemberIdx < STy->getNumElements(); |
| 2165 | MemberIdx++) { |
| 2166 | // Ops[0] = Structure Type ID |
| 2167 | // Ops[1] = Member Index(Literal Number) |
| 2168 | // Ops[2] = Decoration (Offset) |
| 2169 | // Ops[3] = Byte Offset (Literal Number) |
| 2170 | Ops.clear(); |
David Neto | c463b37 | 2017-08-10 15:32:21 -0400 | [diff] [blame] | 2171 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2172 | Ops << MkId(STyID) << MkNum(MemberIdx) |
| 2173 | << MkNum(spv::DecorationOffset); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2174 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2175 | const auto ByteOffset = |
| 2176 | GetExplicitLayoutStructMemberOffset(STy, MemberIdx, DL); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2177 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2178 | Ops << MkNum(ByteOffset); |
| 2179 | |
| 2180 | auto *DecoInst = new SPIRVInstruction(spv::OpMemberDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2181 | getSPIRVInstList(kAnnotations).push_back(DecoInst); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2182 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | // Generate OpDecorate. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2186 | if (StructTypesNeedingBlock.idFor(STy)) { |
| 2187 | Ops.clear(); |
| 2188 | // Use Block decorations with StorageBuffer storage class. |
| 2189 | Ops << MkId(STyID) << MkNum(spv::DecorationBlock); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2190 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2191 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2192 | getSPIRVInstList(kAnnotations).push_back(DecoInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2193 | } |
| 2194 | break; |
| 2195 | } |
| 2196 | case Type::IntegerTyID: { |
alan-baker | 0e64a59 | 2019-11-18 13:36:25 -0500 | [diff] [blame] | 2197 | uint32_t BitWidth = static_cast<uint32_t>(Ty->getPrimitiveSizeInBits()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2198 | |
| 2199 | if (BitWidth == 1) { |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 2200 | auto *Inst = new SPIRVInstruction(spv::OpTypeBool, nextID++); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2201 | SPIRVInstList.push_back(Inst); |
| 2202 | } else { |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 2203 | if (!clspv::Option::Int8Support()) { |
| 2204 | // i8 is added to TypeMap as i32. |
| 2205 | // No matter what LLVM type is requested first, always alias the |
| 2206 | // second one's SPIR-V type to be the same as the one we generated |
| 2207 | // first. |
| 2208 | unsigned aliasToWidth = 0; |
| 2209 | if (BitWidth == 8) { |
| 2210 | aliasToWidth = 32; |
| 2211 | BitWidth = 32; |
| 2212 | } else if (BitWidth == 32) { |
| 2213 | aliasToWidth = 8; |
| 2214 | } |
| 2215 | if (aliasToWidth) { |
| 2216 | Type *otherType = Type::getIntNTy(Ty->getContext(), aliasToWidth); |
| 2217 | auto where = TypeMap.find(otherType); |
| 2218 | if (where == TypeMap.end()) { |
| 2219 | // Go ahead and make it, but also map the other type to it. |
| 2220 | TypeMap[otherType] = nextID; |
| 2221 | } else { |
| 2222 | // Alias this SPIR-V type the existing type. |
| 2223 | TypeMap[Ty] = where->second; |
| 2224 | break; |
| 2225 | } |
David Neto | 391aeb1 | 2017-08-26 15:51:58 -0400 | [diff] [blame] | 2226 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2227 | } |
| 2228 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2229 | SPIRVOperandList Ops; |
| 2230 | Ops << MkNum(BitWidth) << MkNum(0 /* not signed */); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2231 | |
| 2232 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2233 | new SPIRVInstruction(spv::OpTypeInt, nextID++, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2234 | } |
| 2235 | break; |
| 2236 | } |
| 2237 | case Type::HalfTyID: |
| 2238 | case Type::FloatTyID: |
| 2239 | case Type::DoubleTyID: { |
alan-baker | 0e64a59 | 2019-11-18 13:36:25 -0500 | [diff] [blame] | 2240 | uint32_t BitWidth = static_cast<uint32_t>(Ty->getPrimitiveSizeInBits()); |
James Price | 11010dc | 2019-12-19 13:53:09 -0500 | [diff] [blame] | 2241 | auto WidthOp = MkNum(BitWidth); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2242 | |
| 2243 | SPIRVInstList.push_back( |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 2244 | new SPIRVInstruction(spv::OpTypeFloat, nextID++, std::move(WidthOp))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2245 | break; |
| 2246 | } |
| 2247 | case Type::ArrayTyID: { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2248 | ArrayType *ArrTy = cast<ArrayType>(Ty); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2249 | const uint64_t Length = ArrTy->getArrayNumElements(); |
| 2250 | if (Length == 0) { |
| 2251 | // By convention, map it to a RuntimeArray. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2252 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2253 | // Only generate the type once. |
| 2254 | // TODO(dneto): Can it ever be generated more than once? |
| 2255 | // Doesn't LLVM type uniqueness guarantee we'll only see this |
| 2256 | // once? |
| 2257 | Type *EleTy = ArrTy->getArrayElementType(); |
| 2258 | if (OpRuntimeTyMap.count(EleTy) == 0) { |
| 2259 | uint32_t OpTypeRuntimeArrayID = nextID; |
| 2260 | OpRuntimeTyMap[Ty] = nextID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2261 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2262 | // |
| 2263 | // Generate OpTypeRuntimeArray. |
| 2264 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2265 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2266 | // OpTypeRuntimeArray |
| 2267 | // Ops[0] = Element Type ID |
| 2268 | SPIRVOperandList Ops; |
| 2269 | Ops << MkId(lookupType(EleTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2270 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2271 | SPIRVInstList.push_back( |
| 2272 | new SPIRVInstruction(spv::OpTypeRuntimeArray, nextID++, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2273 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2274 | if (Hack_generate_runtime_array_stride_early) { |
| 2275 | // Generate OpDecorate. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2276 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2277 | // Ops[0] = Target ID |
| 2278 | // Ops[1] = Decoration (ArrayStride) |
| 2279 | // Ops[2] = Stride Number(Literal Number) |
| 2280 | Ops.clear(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 2281 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2282 | Ops << MkId(OpTypeRuntimeArrayID) |
| 2283 | << MkNum(spv::DecorationArrayStride) |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2284 | << MkNum(static_cast<uint32_t>(GetTypeAllocSize(EleTy, DL))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2285 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2286 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2287 | getSPIRVInstList(kAnnotations).push_back(DecoInst); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2288 | } |
| 2289 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2290 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2291 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2292 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2293 | // |
| 2294 | // Generate OpConstant and OpTypeArray. |
| 2295 | // |
| 2296 | |
| 2297 | // |
| 2298 | // Generate OpConstant for array length. |
| 2299 | // |
| 2300 | // Ops[0] = Result Type ID |
| 2301 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 2302 | SPIRVOperandList Ops; |
| 2303 | |
| 2304 | Type *LengthTy = Type::getInt32Ty(Context); |
| 2305 | uint32_t ResTyID = lookupType(LengthTy); |
| 2306 | Ops << MkId(ResTyID); |
| 2307 | |
| 2308 | assert(Length < UINT32_MAX); |
| 2309 | Ops << MkNum(static_cast<uint32_t>(Length)); |
| 2310 | |
| 2311 | // Add constant for length to constant list. |
| 2312 | Constant *CstLength = ConstantInt::get(LengthTy, Length); |
| 2313 | AllocatedVMap[CstLength] = nextID; |
| 2314 | VMap[CstLength] = nextID; |
| 2315 | uint32_t LengthID = nextID; |
| 2316 | |
| 2317 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
| 2318 | SPIRVInstList.push_back(CstInst); |
| 2319 | |
| 2320 | // Remember to generate ArrayStride later |
| 2321 | getTypesNeedingArrayStride().insert(Ty); |
| 2322 | |
| 2323 | // |
| 2324 | // Generate OpTypeArray. |
| 2325 | // |
| 2326 | // Ops[0] = Element Type ID |
| 2327 | // Ops[1] = Array Length Constant ID |
| 2328 | Ops.clear(); |
| 2329 | |
| 2330 | uint32_t EleTyID = lookupType(ArrTy->getElementType()); |
| 2331 | Ops << MkId(EleTyID) << MkId(LengthID); |
| 2332 | |
| 2333 | // Update TypeMap with nextID. |
| 2334 | TypeMap[Ty] = nextID; |
| 2335 | |
| 2336 | auto *ArrayInst = new SPIRVInstruction(spv::OpTypeArray, nextID++, Ops); |
| 2337 | SPIRVInstList.push_back(ArrayInst); |
| 2338 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2339 | break; |
| 2340 | } |
James Price | 59a1c75 | 2020-04-23 23:06:16 -0400 | [diff] [blame^] | 2341 | case Type::FixedVectorTyID: { |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 2342 | auto VecTy = cast<VectorType>(Ty); |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 2343 | // <4 x i8> is changed to i32 if i8 is not generally supported. |
| 2344 | if (!clspv::Option::Int8Support() && |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 2345 | VecTy->getElementType() == Type::getInt8Ty(Context)) { |
| 2346 | if (VecTy->getNumElements() == 4) { |
| 2347 | TypeMap[Ty] = lookupType(VecTy->getElementType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2348 | break; |
| 2349 | } else { |
| 2350 | Ty->print(errs()); |
| 2351 | llvm_unreachable("Support above i8 vector type"); |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | // Ops[0] = Component Type ID |
| 2356 | // Ops[1] = Component Count (Literal Number) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2357 | SPIRVOperandList Ops; |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 2358 | Ops << MkId(lookupType(VecTy->getElementType())) |
| 2359 | << MkNum(VecTy->getNumElements()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2360 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2361 | SPIRVInstruction *inst = |
| 2362 | new SPIRVInstruction(spv::OpTypeVector, nextID++, Ops); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2363 | SPIRVInstList.push_back(inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2364 | break; |
| 2365 | } |
| 2366 | case Type::VoidTyID: { |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 2367 | auto *Inst = new SPIRVInstruction(spv::OpTypeVoid, nextID++); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2368 | SPIRVInstList.push_back(Inst); |
| 2369 | break; |
| 2370 | } |
| 2371 | case Type::FunctionTyID: { |
| 2372 | // Generate SPIRV instruction for function type. |
| 2373 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 2374 | |
| 2375 | // Ops[0] = Return Type ID |
| 2376 | // Ops[1] ... Ops[n] = Parameter Type IDs |
| 2377 | SPIRVOperandList Ops; |
| 2378 | |
| 2379 | // Find SPIRV instruction for return type |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2380 | Ops << MkId(lookupType(FTy->getReturnType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2381 | |
| 2382 | // Find SPIRV instructions for parameter types |
| 2383 | for (unsigned k = 0; k < FTy->getNumParams(); k++) { |
| 2384 | // Find SPIRV instruction for parameter type. |
| 2385 | auto ParamTy = FTy->getParamType(k); |
| 2386 | if (ParamTy->isPointerTy()) { |
| 2387 | auto PointeeTy = ParamTy->getPointerElementType(); |
| 2388 | if (PointeeTy->isStructTy() && |
| 2389 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 2390 | ParamTy = PointeeTy; |
| 2391 | } |
| 2392 | } |
| 2393 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2394 | Ops << MkId(lookupType(ParamTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2395 | } |
| 2396 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2397 | auto *Inst = new SPIRVInstruction(spv::OpTypeFunction, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2398 | SPIRVInstList.push_back(Inst); |
| 2399 | break; |
| 2400 | } |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | // Generate OpTypeSampledImage. |
alan-baker | abd8272 | 2019-12-03 17:14:51 -0500 | [diff] [blame] | 2405 | for (auto &ImgTy : getImageTypeList()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2406 | // |
| 2407 | // Generate OpTypeSampledImage. |
| 2408 | // |
| 2409 | // Ops[0] = Image Type ID |
| 2410 | // |
| 2411 | SPIRVOperandList Ops; |
| 2412 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2413 | Ops << MkId(TypeMap[ImgTy]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2414 | |
alan-baker | abd8272 | 2019-12-03 17:14:51 -0500 | [diff] [blame] | 2415 | // Update the image type map. |
| 2416 | getImageTypeMap()[ImgTy] = nextID; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2417 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2418 | auto *Inst = new SPIRVInstruction(spv::OpTypeSampledImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2419 | SPIRVInstList.push_back(Inst); |
| 2420 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2421 | |
| 2422 | // Generate types for pointer-to-local arguments. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2423 | for (auto pair : clspv::GetSpecConstants(module)) { |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 2424 | auto kind = pair.first; |
| 2425 | auto spec_id = pair.second; |
| 2426 | |
| 2427 | if (kind != SpecConstant::kLocalMemorySize) |
| 2428 | continue; |
| 2429 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2430 | LocalArgInfo &arg_info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2431 | |
| 2432 | // Generate the spec constant. |
| 2433 | SPIRVOperandList Ops; |
| 2434 | Ops << MkId(lookupType(Type::getInt32Ty(Context))) << MkNum(1); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2435 | SPIRVInstList.push_back( |
| 2436 | new SPIRVInstruction(spv::OpSpecConstant, arg_info.array_size_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2437 | |
| 2438 | // Generate the array type. |
| 2439 | Ops.clear(); |
| 2440 | // The element type must have been created. |
| 2441 | uint32_t elem_ty_id = lookupType(arg_info.elem_type); |
| 2442 | assert(elem_ty_id); |
| 2443 | Ops << MkId(elem_ty_id) << MkId(arg_info.array_size_id); |
| 2444 | |
| 2445 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2446 | new SPIRVInstruction(spv::OpTypeArray, arg_info.array_type_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2447 | |
| 2448 | Ops.clear(); |
| 2449 | Ops << MkNum(spv::StorageClassWorkgroup) << MkId(arg_info.array_type_id); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2450 | SPIRVInstList.push_back(new SPIRVInstruction( |
| 2451 | spv::OpTypePointer, arg_info.ptr_array_type_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 2452 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | void SPIRVProducerPass::GenerateSPIRVConstants() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2456 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kConstants); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2457 | ValueMapType &VMap = getValueMap(); |
| 2458 | ValueMapType &AllocatedVMap = getAllocatedValueMap(); |
| 2459 | ValueList &CstList = getConstantList(); |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 2460 | const bool hack_undef = clspv::Option::HackUndef(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2461 | |
| 2462 | for (uint32_t i = 0; i < CstList.size(); i++) { |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2463 | // UniqueVector ids are 1-based. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2464 | Constant *Cst = cast<Constant>(CstList[i + 1]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2465 | |
| 2466 | // OpTypeArray's constant was already generated. |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2467 | if (AllocatedVMap.find_as(Cst) != AllocatedVMap.end()) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2468 | continue; |
| 2469 | } |
| 2470 | |
David Neto | fb9a797 | 2017-08-25 17:08:24 -0400 | [diff] [blame] | 2471 | // Set ValueMap with nextID for reference later. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2472 | VMap[Cst] = nextID; |
| 2473 | |
| 2474 | // |
| 2475 | // Generate OpConstant. |
| 2476 | // |
| 2477 | |
| 2478 | // Ops[0] = Result Type ID |
| 2479 | // Ops[1] .. Ops[n] = Values LiteralNumber |
| 2480 | SPIRVOperandList Ops; |
| 2481 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2482 | Ops << MkId(lookupType(Cst->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2483 | |
| 2484 | std::vector<uint32_t> LiteralNum; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2485 | spv::Op Opcode = spv::OpNop; |
| 2486 | |
| 2487 | if (isa<UndefValue>(Cst)) { |
| 2488 | // Ops[0] = Result Type ID |
David Neto | c66b335 | 2017-10-20 14:28:46 -0400 | [diff] [blame] | 2489 | Opcode = spv::OpUndef; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 2490 | if (hack_undef && IsTypeNullable(Cst->getType())) { |
| 2491 | Opcode = spv::OpConstantNull; |
David Neto | c66b335 | 2017-10-20 14:28:46 -0400 | [diff] [blame] | 2492 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2493 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Cst)) { |
| 2494 | unsigned BitWidth = CI->getBitWidth(); |
| 2495 | if (BitWidth == 1) { |
| 2496 | // If the bitwidth of constant is 1, generate OpConstantTrue or |
| 2497 | // OpConstantFalse. |
| 2498 | if (CI->getZExtValue()) { |
| 2499 | // Ops[0] = Result Type ID |
| 2500 | Opcode = spv::OpConstantTrue; |
| 2501 | } else { |
| 2502 | // Ops[0] = Result Type ID |
| 2503 | Opcode = spv::OpConstantFalse; |
| 2504 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2505 | } else { |
| 2506 | auto V = CI->getZExtValue(); |
| 2507 | LiteralNum.push_back(V & 0xFFFFFFFF); |
| 2508 | |
| 2509 | if (BitWidth > 32) { |
| 2510 | LiteralNum.push_back(V >> 32); |
| 2511 | } |
| 2512 | |
| 2513 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2514 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2515 | Ops << MkInteger(LiteralNum); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2516 | } |
| 2517 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Cst)) { |
| 2518 | uint64_t FPVal = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 2519 | Type *CFPTy = CFP->getType(); |
| 2520 | if (CFPTy->isFloatTy()) { |
| 2521 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
Kévin Petit | 02ee34e | 2019-04-04 19:03:22 +0100 | [diff] [blame] | 2522 | } else if (CFPTy->isDoubleTy()) { |
| 2523 | LiteralNum.push_back(FPVal & 0xFFFFFFFF); |
| 2524 | LiteralNum.push_back(FPVal >> 32); |
alan-baker | 089bf93 | 2020-01-07 16:35:45 -0500 | [diff] [blame] | 2525 | } else if (CFPTy->isHalfTy()) { |
| 2526 | LiteralNum.push_back(FPVal & 0xFFFF); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2527 | } else { |
| 2528 | CFPTy->print(errs()); |
| 2529 | llvm_unreachable("Implement this ConstantFP Type"); |
| 2530 | } |
| 2531 | |
| 2532 | Opcode = spv::OpConstant; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2533 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2534 | Ops << MkFloat(LiteralNum); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2535 | } else if (isa<ConstantDataSequential>(Cst) && |
| 2536 | cast<ConstantDataSequential>(Cst)->isString()) { |
| 2537 | Cst->print(errs()); |
| 2538 | llvm_unreachable("Implement this Constant"); |
| 2539 | |
| 2540 | } else if (const ConstantDataSequential *CDS = |
| 2541 | dyn_cast<ConstantDataSequential>(Cst)) { |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2542 | // Let's convert <4 x i8> constant to int constant specially. |
| 2543 | // This case occurs when all the values are specified as constant |
| 2544 | // ints. |
| 2545 | Type *CstTy = Cst->getType(); |
| 2546 | if (is4xi8vec(CstTy)) { |
| 2547 | LLVMContext &Context = CstTy->getContext(); |
| 2548 | |
| 2549 | // |
| 2550 | // Generate OpConstant with OpTypeInt 32 0. |
| 2551 | // |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2552 | uint32_t IntValue = 0; |
| 2553 | for (unsigned k = 0; k < 4; k++) { |
| 2554 | const uint64_t Val = CDS->getElementAsInteger(k); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2555 | IntValue = (IntValue << 8) | (Val & 0xffu); |
| 2556 | } |
| 2557 | |
| 2558 | Type *i32 = Type::getInt32Ty(Context); |
| 2559 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
| 2560 | // If this constant is already registered on VMap, use it. |
| 2561 | if (VMap.count(CstInt)) { |
| 2562 | uint32_t CstID = VMap[CstInt]; |
| 2563 | VMap[Cst] = CstID; |
| 2564 | continue; |
| 2565 | } |
| 2566 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2567 | Ops << MkNum(IntValue); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2568 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2569 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2570 | SPIRVInstList.push_back(CstInst); |
| 2571 | |
| 2572 | continue; |
| 2573 | } |
| 2574 | |
| 2575 | // A normal constant-data-sequential case. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2576 | for (unsigned k = 0; k < CDS->getNumElements(); k++) { |
| 2577 | Constant *EleCst = CDS->getElementAsConstant(k); |
| 2578 | uint32_t EleCstID = VMap[EleCst]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2579 | Ops << MkId(EleCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2583 | } else if (const ConstantAggregate *CA = dyn_cast<ConstantAggregate>(Cst)) { |
| 2584 | // Let's convert <4 x i8> constant to int constant specially. |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2585 | // This case occurs when at least one of the values is an undef. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2586 | Type *CstTy = Cst->getType(); |
| 2587 | if (is4xi8vec(CstTy)) { |
| 2588 | LLVMContext &Context = CstTy->getContext(); |
| 2589 | |
| 2590 | // |
| 2591 | // Generate OpConstant with OpTypeInt 32 0. |
| 2592 | // |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2593 | uint32_t IntValue = 0; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2594 | for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); |
| 2595 | I != E; ++I) { |
| 2596 | uint64_t Val = 0; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2597 | const Value *CV = *I; |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 2598 | if (auto *CI2 = dyn_cast<ConstantInt>(CV)) { |
| 2599 | Val = CI2->getZExtValue(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2600 | } |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2601 | IntValue = (IntValue << 8) | (Val & 0xffu); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2602 | } |
| 2603 | |
David Neto | 49351ac | 2017-08-26 17:32:20 -0400 | [diff] [blame] | 2604 | Type *i32 = Type::getInt32Ty(Context); |
| 2605 | Constant *CstInt = ConstantInt::get(i32, IntValue); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2606 | // If this constant is already registered on VMap, use it. |
| 2607 | if (VMap.count(CstInt)) { |
| 2608 | uint32_t CstID = VMap[CstInt]; |
| 2609 | VMap[Cst] = CstID; |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 2610 | continue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2611 | } |
| 2612 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2613 | Ops << MkNum(IntValue); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2614 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2615 | auto *CstInst = new SPIRVInstruction(spv::OpConstant, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2616 | SPIRVInstList.push_back(CstInst); |
| 2617 | |
David Neto | 19a1bad | 2017-08-25 15:01:41 -0400 | [diff] [blame] | 2618 | continue; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | // We use a constant composite in SPIR-V for our constant aggregate in |
| 2622 | // LLVM. |
| 2623 | Opcode = spv::OpConstantComposite; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2624 | |
| 2625 | for (unsigned k = 0; k < CA->getNumOperands(); k++) { |
| 2626 | // Look up the ID of the element of this aggregate (which we will |
| 2627 | // previously have created a constant for). |
| 2628 | uint32_t ElementConstantID = VMap[CA->getAggregateElement(k)]; |
| 2629 | |
| 2630 | // And add an operand to the composite we are constructing |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2631 | Ops << MkId(ElementConstantID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2632 | } |
| 2633 | } else if (Cst->isNullValue()) { |
| 2634 | Opcode = spv::OpConstantNull; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2635 | } else { |
| 2636 | Cst->print(errs()); |
| 2637 | llvm_unreachable("Unsupported Constant???"); |
| 2638 | } |
| 2639 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 2640 | if (Opcode == spv::OpConstantNull && Cst->getType()->isPointerTy()) { |
| 2641 | // Null pointer requires variable pointers. |
| 2642 | setVariablePointersCapabilities(Cst->getType()->getPointerAddressSpace()); |
| 2643 | } |
| 2644 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2645 | auto *CstInst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2646 | SPIRVInstList.push_back(CstInst); |
| 2647 | } |
| 2648 | } |
| 2649 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2650 | void SPIRVProducerPass::GenerateSamplers() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2651 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kGlobalVariables); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2652 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2653 | auto &sampler_map = getSamplerMap(); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2654 | SamplerLiteralToIDMap.clear(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2655 | DenseMap<unsigned, unsigned> SamplerLiteralToDescriptorSetMap; |
| 2656 | DenseMap<unsigned, unsigned> SamplerLiteralToBindingMap; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2657 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2658 | // We might have samplers in the sampler map that are not used |
| 2659 | // in the translation unit. We need to allocate variables |
| 2660 | // for them and bindings too. |
| 2661 | DenseSet<unsigned> used_bindings; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2662 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2663 | auto *var_fn = module->getFunction(clspv::LiteralSamplerFunction()); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2664 | // Return if there are no literal samplers. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2665 | if (!var_fn) |
| 2666 | return; |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2667 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2668 | for (auto user : var_fn->users()) { |
| 2669 | // Populate SamplerLiteralToDescriptorSetMap and |
| 2670 | // SamplerLiteralToBindingMap. |
| 2671 | // |
| 2672 | // Look for calls like |
| 2673 | // call %opencl.sampler_t addrspace(2)* |
| 2674 | // @clspv.sampler.var.literal( |
| 2675 | // i32 descriptor, |
| 2676 | // i32 binding, |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2677 | // i32 (index-into-sampler-map|sampler_mask)) |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2678 | if (auto *call = dyn_cast<CallInst>(user)) { |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2679 | const auto third_param = static_cast<unsigned>( |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2680 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2681 | auto sampler_value = third_param; |
| 2682 | if (clspv::Option::UseSamplerMap()) { |
| 2683 | if (third_param >= sampler_map.size()) { |
| 2684 | errs() << "Out of bounds index to sampler map: " << third_param; |
| 2685 | llvm_unreachable("bad sampler init: out of bounds"); |
| 2686 | } |
| 2687 | sampler_value = sampler_map[third_param].first; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2688 | } |
| 2689 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2690 | const auto descriptor_set = static_cast<unsigned>( |
| 2691 | dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue()); |
| 2692 | const auto binding = static_cast<unsigned>( |
| 2693 | dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue()); |
| 2694 | |
| 2695 | SamplerLiteralToDescriptorSetMap[sampler_value] = descriptor_set; |
| 2696 | SamplerLiteralToBindingMap[sampler_value] = binding; |
| 2697 | used_bindings.insert(binding); |
| 2698 | } |
| 2699 | } |
| 2700 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2701 | DenseSet<size_t> seen; |
| 2702 | for (auto user : var_fn->users()) { |
| 2703 | if (!isa<CallInst>(user)) |
| 2704 | continue; |
| 2705 | |
| 2706 | auto call = cast<CallInst>(user); |
| 2707 | const unsigned third_param = static_cast<unsigned>( |
| 2708 | dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue()); |
| 2709 | |
| 2710 | // Already allocated a variable for this value. |
| 2711 | if (!seen.insert(third_param).second) |
| 2712 | continue; |
| 2713 | |
| 2714 | auto sampler_value = third_param; |
| 2715 | if (clspv::Option::UseSamplerMap()) { |
| 2716 | sampler_value = sampler_map[third_param].first; |
| 2717 | } |
| 2718 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2719 | // Generate OpVariable. |
| 2720 | // |
| 2721 | // GIDOps[0] : Result Type ID |
| 2722 | // GIDOps[1] : Storage Class |
| 2723 | SPIRVOperandList Ops; |
| 2724 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 2725 | Ops << MkId(lookupType(SamplerTy)) |
| 2726 | << MkNum(spv::StorageClassUniformConstant); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2727 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2728 | auto sampler_var_id = nextID++; |
| 2729 | auto *Inst = new SPIRVInstruction(spv::OpVariable, sampler_var_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2730 | SPIRVInstList.push_back(Inst); |
| 2731 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2732 | SamplerLiteralToIDMap[sampler_value] = sampler_var_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2733 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2734 | unsigned descriptor_set; |
| 2735 | unsigned binding; |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2736 | if (SamplerLiteralToBindingMap.find(sampler_value) == |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 2737 | SamplerLiteralToBindingMap.end()) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2738 | // This sampler is not actually used. Find the next one. |
| 2739 | for (binding = 0; used_bindings.count(binding); binding++) |
| 2740 | ; |
| 2741 | descriptor_set = 0; // Literal samplers always use descriptor set 0. |
| 2742 | used_bindings.insert(binding); |
| 2743 | } else { |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2744 | descriptor_set = SamplerLiteralToDescriptorSetMap[sampler_value]; |
| 2745 | binding = SamplerLiteralToBindingMap[sampler_value]; |
alan-baker | cff8015 | 2019-06-15 00:38:00 -0400 | [diff] [blame] | 2746 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 2747 | version0::DescriptorMapEntry::SamplerData sampler_data = {sampler_value}; |
alan-baker | cff8015 | 2019-06-15 00:38:00 -0400 | [diff] [blame] | 2748 | descriptorMapEntries->emplace_back(std::move(sampler_data), |
| 2749 | descriptor_set, binding); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2750 | } |
| 2751 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2752 | // Ops[0] = Target ID |
| 2753 | // Ops[1] = Decoration (DescriptorSet) |
| 2754 | // Ops[2] = LiteralNumber according to Decoration |
| 2755 | Ops.clear(); |
| 2756 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2757 | Ops << MkId(sampler_var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2758 | << MkNum(descriptor_set); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2759 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2760 | auto *DescDecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2761 | getSPIRVInstList(kAnnotations).push_back(DescDecoInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2762 | |
| 2763 | // Ops[0] = Target ID |
| 2764 | // Ops[1] = Decoration (Binding) |
| 2765 | // Ops[2] = LiteralNumber according to Decoration |
| 2766 | Ops.clear(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2767 | Ops << MkId(sampler_var_id) << MkNum(spv::DecorationBinding) |
| 2768 | << MkNum(binding); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2769 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 2770 | auto *BindDecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2771 | getSPIRVInstList(kAnnotations).push_back(BindDecoInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2772 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2773 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2774 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 2775 | void SPIRVProducerPass::GenerateResourceVars() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2776 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kGlobalVariables); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2777 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2778 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2779 | // Generate variables. Make one for each of resource var info object. |
| 2780 | for (auto *info : ModuleOrderedResourceVars) { |
| 2781 | Type *type = info->var_fn->getReturnType(); |
| 2782 | // Remap the address space for opaque types. |
| 2783 | switch (info->arg_kind) { |
| 2784 | case clspv::ArgKind::Sampler: |
| 2785 | case clspv::ArgKind::ReadOnlyImage: |
| 2786 | case clspv::ArgKind::WriteOnlyImage: |
| 2787 | type = PointerType::get(type->getPointerElementType(), |
| 2788 | clspv::AddressSpace::UniformConstant); |
| 2789 | break; |
| 2790 | default: |
| 2791 | break; |
| 2792 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2793 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2794 | info->var_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2795 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2796 | const auto type_id = lookupType(type); |
| 2797 | const auto sc = GetStorageClassForArgKind(info->arg_kind); |
| 2798 | SPIRVOperandList Ops; |
| 2799 | Ops << MkId(type_id) << MkNum(sc); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2800 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2801 | auto *Inst = new SPIRVInstruction(spv::OpVariable, info->var_id, Ops); |
| 2802 | SPIRVInstList.push_back(Inst); |
| 2803 | |
| 2804 | // Map calls to the variable-builtin-function. |
| 2805 | for (auto &U : info->var_fn->uses()) { |
| 2806 | if (auto *call = dyn_cast<CallInst>(U.getUser())) { |
| 2807 | const auto set = unsigned( |
| 2808 | dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue()); |
| 2809 | const auto binding = unsigned( |
| 2810 | dyn_cast<ConstantInt>(call->getOperand(1))->getZExtValue()); |
| 2811 | if (set == info->descriptor_set && binding == info->binding) { |
| 2812 | switch (info->arg_kind) { |
| 2813 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2814 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2815 | case clspv::ArgKind::Pod: |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 2816 | case clspv::ArgKind::PodUBO: |
| 2817 | case clspv::ArgKind::PodPushConstant: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2818 | // The call maps to the variable directly. |
| 2819 | VMap[call] = info->var_id; |
| 2820 | break; |
| 2821 | case clspv::ArgKind::Sampler: |
| 2822 | case clspv::ArgKind::ReadOnlyImage: |
| 2823 | case clspv::ArgKind::WriteOnlyImage: |
| 2824 | // The call maps to a load we generate later. |
| 2825 | ResourceVarDeferredLoadCalls[call] = info->var_id; |
| 2826 | break; |
| 2827 | default: |
| 2828 | llvm_unreachable("Unhandled arg kind"); |
| 2829 | } |
| 2830 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2831 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2832 | } |
| 2833 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2834 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2835 | // Generate associated decorations. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2836 | SPIRVInstructionList &Annotations = getSPIRVInstList(kAnnotations); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2837 | |
| 2838 | SPIRVOperandList Ops; |
| 2839 | for (auto *info : ModuleOrderedResourceVars) { |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 2840 | // Push constants don't need descriptor set or binding decorations. |
| 2841 | if (info->arg_kind == clspv::ArgKind::PodPushConstant) |
| 2842 | continue; |
| 2843 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2844 | // Decorate with DescriptorSet and Binding. |
| 2845 | Ops.clear(); |
| 2846 | Ops << MkId(info->var_id) << MkNum(spv::DecorationDescriptorSet) |
| 2847 | << MkNum(info->descriptor_set); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2848 | Annotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2849 | |
| 2850 | Ops.clear(); |
| 2851 | Ops << MkId(info->var_id) << MkNum(spv::DecorationBinding) |
| 2852 | << MkNum(info->binding); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2853 | Annotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2854 | |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2855 | if (info->coherent) { |
| 2856 | // Decorate with Coherent if required for the variable. |
| 2857 | Ops.clear(); |
| 2858 | Ops << MkId(info->var_id) << MkNum(spv::DecorationCoherent); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2859 | Annotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 2860 | } |
| 2861 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2862 | // Generate NonWritable and NonReadable |
| 2863 | switch (info->arg_kind) { |
| 2864 | case clspv::ArgKind::Buffer: |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 2865 | case clspv::ArgKind::BufferUBO: |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2866 | if (info->var_fn->getReturnType()->getPointerAddressSpace() == |
| 2867 | clspv::AddressSpace::Constant) { |
| 2868 | Ops.clear(); |
| 2869 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonWritable); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2870 | Annotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2871 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2872 | break; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2873 | case clspv::ArgKind::WriteOnlyImage: |
| 2874 | Ops.clear(); |
| 2875 | Ops << MkId(info->var_id) << MkNum(spv::DecorationNonReadable); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 2876 | Annotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 2877 | break; |
| 2878 | default: |
| 2879 | break; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2884 | namespace { |
| 2885 | |
| 2886 | bool isScalarType(Type *type) { |
| 2887 | return type->isIntegerTy() || type->isFloatTy(); |
| 2888 | } |
| 2889 | |
| 2890 | uint64_t structAlignment(StructType *type, |
| 2891 | std::function<uint64_t(Type *)> alignFn) { |
| 2892 | uint64_t maxAlign = 1; |
| 2893 | for (unsigned i = 0; i < type->getStructNumElements(); i++) { |
| 2894 | uint64_t align = alignFn(type->getStructElementType(i)); |
| 2895 | maxAlign = std::max(align, maxAlign); |
| 2896 | } |
| 2897 | return maxAlign; |
| 2898 | } |
| 2899 | |
| 2900 | uint64_t scalarAlignment(Type *type) { |
| 2901 | // A scalar of size N has a scalar alignment of N. |
| 2902 | if (isScalarType(type)) { |
| 2903 | return type->getScalarSizeInBits() / 8; |
| 2904 | } |
| 2905 | |
| 2906 | // A vector or matrix type has a scalar alignment equal to that of its |
| 2907 | // component type. |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 2908 | if (auto vec_type = dyn_cast<VectorType>(type)) { |
| 2909 | return scalarAlignment(vec_type->getElementType()); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | // An array type has a scalar alignment equal to that of its element type. |
| 2913 | if (type->isArrayTy()) { |
| 2914 | return scalarAlignment(type->getArrayElementType()); |
| 2915 | } |
| 2916 | |
| 2917 | // A structure has a scalar alignment equal to the largest scalar alignment of |
| 2918 | // any of its members. |
| 2919 | if (type->isStructTy()) { |
| 2920 | return structAlignment(cast<StructType>(type), scalarAlignment); |
| 2921 | } |
| 2922 | |
| 2923 | llvm_unreachable("Unsupported type"); |
| 2924 | } |
| 2925 | |
| 2926 | uint64_t baseAlignment(Type *type) { |
| 2927 | // A scalar has a base alignment equal to its scalar alignment. |
| 2928 | if (isScalarType(type)) { |
| 2929 | return scalarAlignment(type); |
| 2930 | } |
| 2931 | |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 2932 | if (auto vec_type = dyn_cast<VectorType>(type)) { |
| 2933 | unsigned numElems = vec_type->getNumElements(); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 2934 | |
| 2935 | // A two-component vector has a base alignment equal to twice its scalar |
| 2936 | // alignment. |
| 2937 | if (numElems == 2) { |
| 2938 | return 2 * scalarAlignment(type); |
| 2939 | } |
| 2940 | // A three- or four-component vector has a base alignment equal to four |
| 2941 | // times its scalar alignment. |
| 2942 | if ((numElems == 3) || (numElems == 4)) { |
| 2943 | return 4 * scalarAlignment(type); |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | // An array has a base alignment equal to the base alignment of its element |
| 2948 | // type. |
| 2949 | if (type->isArrayTy()) { |
| 2950 | return baseAlignment(type->getArrayElementType()); |
| 2951 | } |
| 2952 | |
| 2953 | // A structure has a base alignment equal to the largest base alignment of any |
| 2954 | // of its members. |
| 2955 | if (type->isStructTy()) { |
| 2956 | return structAlignment(cast<StructType>(type), baseAlignment); |
| 2957 | } |
| 2958 | |
| 2959 | // TODO A row-major matrix of C columns has a base alignment equal to the base |
| 2960 | // alignment of a vector of C matrix components. |
| 2961 | // TODO A column-major matrix has a base alignment equal to the base alignment |
| 2962 | // of the matrix column type. |
| 2963 | |
| 2964 | llvm_unreachable("Unsupported type"); |
| 2965 | } |
| 2966 | |
| 2967 | uint64_t extendedAlignment(Type *type) { |
| 2968 | // A scalar, vector or matrix type has an extended alignment equal to its base |
| 2969 | // alignment. |
| 2970 | // TODO matrix type |
| 2971 | if (isScalarType(type) || type->isVectorTy()) { |
| 2972 | return baseAlignment(type); |
| 2973 | } |
| 2974 | |
| 2975 | // An array or structure type has an extended alignment equal to the largest |
| 2976 | // extended alignment of any of its members, rounded up to a multiple of 16 |
| 2977 | if (type->isStructTy()) { |
| 2978 | auto salign = structAlignment(cast<StructType>(type), extendedAlignment); |
| 2979 | return alignTo(salign, 16); |
| 2980 | } |
| 2981 | |
| 2982 | if (type->isArrayTy()) { |
| 2983 | auto salign = extendedAlignment(type->getArrayElementType()); |
| 2984 | return alignTo(salign, 16); |
| 2985 | } |
| 2986 | |
| 2987 | llvm_unreachable("Unsupported type"); |
| 2988 | } |
| 2989 | |
| 2990 | uint64_t standardAlignment(Type *type, spv::StorageClass sclass) { |
| 2991 | // If the scalarBlockLayout feature is enabled on the device then every member |
| 2992 | // must be aligned according to its scalar alignment |
| 2993 | if (clspv::Option::ScalarBlockLayout()) { |
| 2994 | return scalarAlignment(type); |
| 2995 | } |
| 2996 | |
| 2997 | // All vectors must be aligned according to their scalar alignment |
| 2998 | if (type->isVectorTy()) { |
| 2999 | return scalarAlignment(type); |
| 3000 | } |
| 3001 | |
| 3002 | // If the uniformBufferStandardLayout feature is not enabled on the device, |
| 3003 | // then any member of an OpTypeStruct with a storage class of Uniform and a |
| 3004 | // decoration of Block must be aligned according to its extended alignment. |
| 3005 | if (!clspv::Option::Std430UniformBufferLayout() && |
| 3006 | sclass == spv::StorageClassUniform) { |
| 3007 | return extendedAlignment(type); |
| 3008 | } |
| 3009 | |
| 3010 | // Every other member must be aligned according to its base alignment |
| 3011 | return baseAlignment(type); |
| 3012 | } |
| 3013 | |
| 3014 | bool improperlyStraddles(const DataLayout &DL, Type *type, unsigned offset) { |
| 3015 | assert(type->isVectorTy()); |
| 3016 | |
| 3017 | auto size = DL.getTypeStoreSize(type); |
| 3018 | |
| 3019 | // It is a vector with total size less than or equal to 16 bytes, and has |
| 3020 | // Offset decorations placing its first byte at F and its last byte at L, |
| 3021 | // where floor(F / 16) != floor(L / 16). |
| 3022 | if ((size <= 16) && (offset % 16 + size > 16)) { |
| 3023 | return true; |
| 3024 | } |
| 3025 | |
| 3026 | // It is a vector with total size greater than 16 bytes and has its Offset |
| 3027 | // decorations placing its first byte at a non-integer multiple of 16 |
| 3028 | if ((size > 16) && (offset % 16 != 0)) { |
| 3029 | return true; |
| 3030 | } |
| 3031 | |
| 3032 | return false; |
| 3033 | } |
| 3034 | |
| 3035 | // See 14.5 Shader Resource Interface in Vulkan spec |
| 3036 | bool isValidExplicitLayout(Module &M, StructType *STy, unsigned Member, |
| 3037 | spv::StorageClass SClass, unsigned Offset, |
| 3038 | unsigned PreviousMemberOffset) { |
| 3039 | |
| 3040 | auto MemberType = STy->getElementType(Member); |
| 3041 | auto Align = standardAlignment(MemberType, SClass); |
| 3042 | auto &DL = M.getDataLayout(); |
| 3043 | |
| 3044 | // The Offset decoration of any member must be a multiple of its alignment |
| 3045 | if (Offset % Align != 0) { |
| 3046 | return false; |
| 3047 | } |
| 3048 | |
| 3049 | // TODO Any ArrayStride or MatrixStride decoration must be a multiple of the |
| 3050 | // alignment of the array or matrix as defined above |
| 3051 | |
| 3052 | if (!clspv::Option::ScalarBlockLayout()) { |
| 3053 | // Vectors must not improperly straddle, as defined above |
| 3054 | if (MemberType->isVectorTy() && |
| 3055 | improperlyStraddles(DL, MemberType, Offset)) { |
| 3056 | return true; |
| 3057 | } |
| 3058 | |
| 3059 | // The Offset decoration of a member must not place it between the end |
| 3060 | // of a structure or an array and the next multiple of the alignment of that |
| 3061 | // structure or array |
| 3062 | if (Member > 0) { |
| 3063 | auto PType = STy->getElementType(Member - 1); |
| 3064 | if (PType->isStructTy() || PType->isArrayTy()) { |
| 3065 | auto PAlign = standardAlignment(PType, SClass); |
| 3066 | if (Offset - PreviousMemberOffset < PAlign) { |
| 3067 | return false; |
| 3068 | } |
| 3069 | } |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | return true; |
| 3074 | } |
| 3075 | |
| 3076 | } // namespace |
| 3077 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3078 | void SPIRVProducerPass::GeneratePushConstantDescriptorMapEntries() { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 3079 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3080 | if (auto GV = module->getGlobalVariable(clspv::PushConstantsVariableName())) { |
| 3081 | auto const &DL = module->getDataLayout(); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 3082 | auto MD = GV->getMetadata(clspv::PushConstantsMetadataName()); |
| 3083 | auto STy = cast<StructType>(GV->getValueType()); |
| 3084 | |
| 3085 | for (unsigned i = 0; i < STy->getNumElements(); i++) { |
| 3086 | auto pc = static_cast<clspv::PushConstant>( |
| 3087 | mdconst::extract<ConstantInt>(MD->getOperand(i))->getZExtValue()); |
| 3088 | auto memberType = STy->getElementType(i); |
| 3089 | auto offset = GetExplicitLayoutStructMemberOffset(STy, i, DL); |
| 3090 | unsigned previousOffset = 0; |
| 3091 | if (i > 0) { |
| 3092 | previousOffset = GetExplicitLayoutStructMemberOffset(STy, i - 1, DL); |
| 3093 | } |
| 3094 | auto size = static_cast<uint32_t>(GetTypeSizeInBits(memberType, DL)) / 8; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3095 | assert(isValidExplicitLayout(*module, STy, i, |
| 3096 | spv::StorageClassPushConstant, offset, |
| 3097 | previousOffset)); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 3098 | version0::DescriptorMapEntry::PushConstantData data = {pc, offset, size}; |
| 3099 | descriptorMapEntries->emplace_back(std::move(data)); |
| 3100 | } |
| 3101 | } |
| 3102 | } |
| 3103 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3104 | void SPIRVProducerPass::GenerateSpecConstantDescriptorMapEntries() { |
| 3105 | for (auto pair : clspv::GetSpecConstants(module)) { |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3106 | auto kind = pair.first; |
| 3107 | auto id = pair.second; |
| 3108 | |
| 3109 | // Local memory size is only used for kernel arguments. |
| 3110 | if (kind == SpecConstant::kLocalMemorySize) |
| 3111 | continue; |
| 3112 | |
| 3113 | version0::DescriptorMapEntry::SpecConstantData data = {kind, id}; |
| 3114 | descriptorMapEntries->emplace_back(std::move(data)); |
| 3115 | } |
| 3116 | } |
| 3117 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3118 | void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3119 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kGlobalVariables); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3120 | ValueMapType &VMap = getValueMap(); |
| 3121 | std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3122 | const DataLayout &DL = GV.getParent()->getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3123 | |
| 3124 | const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName()); |
| 3125 | Type *Ty = GV.getType(); |
| 3126 | PointerType *PTy = cast<PointerType>(Ty); |
| 3127 | |
| 3128 | uint32_t InitializerID = 0; |
| 3129 | |
| 3130 | // Workgroup size is handled differently (it goes into a constant) |
| 3131 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 3132 | std::vector<bool> HasMDVec; |
| 3133 | uint32_t PrevXDimCst = 0xFFFFFFFF; |
| 3134 | uint32_t PrevYDimCst = 0xFFFFFFFF; |
| 3135 | uint32_t PrevZDimCst = 0xFFFFFFFF; |
| 3136 | for (Function &Func : *GV.getParent()) { |
| 3137 | if (Func.isDeclaration()) { |
| 3138 | continue; |
| 3139 | } |
| 3140 | |
| 3141 | // We only need to check kernels. |
| 3142 | if (Func.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 3143 | continue; |
| 3144 | } |
| 3145 | |
| 3146 | if (const MDNode *MD = |
| 3147 | dyn_cast<Function>(&Func)->getMetadata("reqd_work_group_size")) { |
| 3148 | uint32_t CurXDimCst = static_cast<uint32_t>( |
| 3149 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 3150 | uint32_t CurYDimCst = static_cast<uint32_t>( |
| 3151 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 3152 | uint32_t CurZDimCst = static_cast<uint32_t>( |
| 3153 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 3154 | |
| 3155 | if (PrevXDimCst == 0xFFFFFFFF && PrevYDimCst == 0xFFFFFFFF && |
| 3156 | PrevZDimCst == 0xFFFFFFFF) { |
| 3157 | PrevXDimCst = CurXDimCst; |
| 3158 | PrevYDimCst = CurYDimCst; |
| 3159 | PrevZDimCst = CurZDimCst; |
| 3160 | } else if (CurXDimCst != PrevXDimCst || CurYDimCst != PrevYDimCst || |
| 3161 | CurZDimCst != PrevZDimCst) { |
| 3162 | llvm_unreachable( |
| 3163 | "reqd_work_group_size must be the same across all kernels"); |
| 3164 | } else { |
| 3165 | continue; |
| 3166 | } |
| 3167 | |
| 3168 | // |
| 3169 | // Generate OpConstantComposite. |
| 3170 | // |
| 3171 | // Ops[0] : Result Type ID |
| 3172 | // Ops[1] : Constant size for x dimension. |
| 3173 | // Ops[2] : Constant size for y dimension. |
| 3174 | // Ops[3] : Constant size for z dimension. |
| 3175 | SPIRVOperandList Ops; |
| 3176 | |
| 3177 | uint32_t XDimCstID = |
| 3178 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(0))]; |
| 3179 | uint32_t YDimCstID = |
| 3180 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(1))]; |
| 3181 | uint32_t ZDimCstID = |
| 3182 | VMap[mdconst::extract<ConstantInt>(MD->getOperand(2))]; |
| 3183 | |
| 3184 | InitializerID = nextID; |
| 3185 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3186 | Ops << MkId(lookupType(Ty->getPointerElementType())) << MkId(XDimCstID) |
| 3187 | << MkId(YDimCstID) << MkId(ZDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3188 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3189 | auto *Inst = |
| 3190 | new SPIRVInstruction(spv::OpConstantComposite, nextID++, Ops); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3191 | getSPIRVInstList(kConstants).push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3192 | |
| 3193 | HasMDVec.push_back(true); |
| 3194 | } else { |
| 3195 | HasMDVec.push_back(false); |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | // Check all kernels have same definitions for work_group_size. |
| 3200 | bool HasMD = false; |
| 3201 | if (!HasMDVec.empty()) { |
| 3202 | HasMD = HasMDVec[0]; |
| 3203 | for (uint32_t i = 1; i < HasMDVec.size(); i++) { |
| 3204 | if (HasMD != HasMDVec[i]) { |
| 3205 | llvm_unreachable( |
| 3206 | "Kernels should have consistent work group size definition"); |
| 3207 | } |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | // If all kernels do not have metadata for reqd_work_group_size, generate |
| 3212 | // OpSpecConstants for x/y/z dimension. |
| 3213 | if (!HasMD) { |
| 3214 | // |
| 3215 | // Generate OpSpecConstants for x/y/z dimension. |
| 3216 | // |
| 3217 | // Ops[0] : Result Type ID |
| 3218 | // Ops[1] : Constant size for x/y/z dimension (Literal Number). |
| 3219 | uint32_t XDimCstID = 0; |
| 3220 | uint32_t YDimCstID = 0; |
| 3221 | uint32_t ZDimCstID = 0; |
| 3222 | |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3223 | // Allocate spec constants for workgroup size. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3224 | clspv::AddWorkgroupSpecConstants(module); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3225 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3226 | SPIRVOperandList Ops; |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 3227 | uint32_t result_type_id = lookupType( |
| 3228 | cast<VectorType>(Ty->getPointerElementType())->getElementType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3229 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3230 | // X Dimension |
| 3231 | Ops << MkId(result_type_id) << MkNum(1); |
| 3232 | XDimCstID = nextID++; |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3233 | getSPIRVInstList(kConstants) |
| 3234 | .push_back(new SPIRVInstruction(spv::OpSpecConstant, XDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3235 | |
| 3236 | // Y Dimension |
| 3237 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3238 | Ops << MkId(result_type_id) << MkNum(1); |
| 3239 | YDimCstID = nextID++; |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3240 | getSPIRVInstList(kConstants) |
| 3241 | .push_back(new SPIRVInstruction(spv::OpSpecConstant, YDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3242 | |
| 3243 | // Z Dimension |
| 3244 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3245 | Ops << MkId(result_type_id) << MkNum(1); |
| 3246 | ZDimCstID = nextID++; |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3247 | getSPIRVInstList(kConstants) |
| 3248 | .push_back(new SPIRVInstruction(spv::OpSpecConstant, ZDimCstID, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3249 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3250 | BuiltinDimVec.push_back(XDimCstID); |
| 3251 | BuiltinDimVec.push_back(YDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3252 | BuiltinDimVec.push_back(ZDimCstID); |
| 3253 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3254 | // |
| 3255 | // Generate OpSpecConstantComposite. |
| 3256 | // |
| 3257 | // Ops[0] : Result Type ID |
| 3258 | // Ops[1] : Constant size for x dimension. |
| 3259 | // Ops[2] : Constant size for y dimension. |
| 3260 | // Ops[3] : Constant size for z dimension. |
| 3261 | InitializerID = nextID; |
| 3262 | |
| 3263 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3264 | Ops << MkId(lookupType(Ty->getPointerElementType())) << MkId(XDimCstID) |
| 3265 | << MkId(YDimCstID) << MkId(ZDimCstID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3266 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3267 | auto *Inst = |
| 3268 | new SPIRVInstruction(spv::OpSpecConstantComposite, nextID++, Ops); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3269 | getSPIRVInstList(kConstants).push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3270 | } |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 3271 | } else if (BuiltinType == spv::BuiltInWorkDim) { |
| 3272 | // 1. Generate a specialization constant with a default of 3. |
| 3273 | // 2. Allocate and annotate a SpecId for the constant. |
| 3274 | // 3. Use the spec constant as the initializer for the variable. |
| 3275 | SPIRVOperandList Ops; |
| 3276 | |
| 3277 | // |
| 3278 | // Generate OpSpecConstant. |
| 3279 | // |
| 3280 | // Ops[0] : Result Type ID |
| 3281 | // Ops[1] : Default literal value |
| 3282 | InitializerID = nextID++; |
| 3283 | |
| 3284 | Ops << MkId(lookupType(IntegerType::get(GV.getContext(), 32))) << MkNum(3); |
| 3285 | |
| 3286 | auto *Inst = new SPIRVInstruction(spv::OpSpecConstant, InitializerID, Ops); |
| 3287 | getSPIRVInstList(kConstants).push_back(Inst); |
| 3288 | |
| 3289 | // |
| 3290 | // Generate SpecId decoration. |
| 3291 | // |
| 3292 | // Ops[0] : target |
| 3293 | // Ops[1] : decoration |
| 3294 | // Ops[2] : SpecId |
Alan Baker | 75ccc25 | 2020-04-21 17:11:52 -0400 | [diff] [blame] | 3295 | auto spec_id = AllocateSpecConstant(module, SpecConstant::kWorkDim); |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 3296 | Ops.clear(); |
| 3297 | Ops << MkId(InitializerID) << MkNum(spv::DecorationSpecId) |
| 3298 | << MkNum(spec_id); |
| 3299 | |
| 3300 | Inst = new SPIRVInstruction(spv::OpDecorate, Ops); |
| 3301 | getSPIRVInstList(kAnnotations).push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3302 | } |
| 3303 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3304 | VMap[&GV] = nextID; |
| 3305 | |
| 3306 | // |
| 3307 | // Generate OpVariable. |
| 3308 | // |
| 3309 | // GIDOps[0] : Result Type ID |
| 3310 | // GIDOps[1] : Storage Class |
| 3311 | SPIRVOperandList Ops; |
| 3312 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3313 | const auto AS = PTy->getAddressSpace(); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 3314 | Ops << MkId(lookupType(Ty)) << MkNum(GetStorageClass(AS)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3315 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3316 | const bool module_scope_constant_external_init = |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3317 | (AS == AddressSpace::Constant) && GV.hasInitializer() && |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3318 | clspv::Option::ModuleConstantsInStorageBuffer(); |
| 3319 | |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 3320 | if (GV.hasInitializer()) { |
| 3321 | auto GVInit = GV.getInitializer(); |
| 3322 | if (!isa<UndefValue>(GVInit) && !module_scope_constant_external_init) { |
| 3323 | assert(VMap.count(GVInit) == 1); |
| 3324 | InitializerID = VMap[GVInit]; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3325 | } |
| 3326 | } |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 3327 | |
| 3328 | if (0 != InitializerID) { |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 3329 | // Emit the ID of the initializer as part of the variable definition. |
Kévin Petit | 23d5f18 | 2019-08-13 16:21:29 +0100 | [diff] [blame] | 3330 | Ops << MkId(InitializerID); |
| 3331 | } |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3332 | const uint32_t var_id = nextID++; |
| 3333 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3334 | auto *Inst = new SPIRVInstruction(spv::OpVariable, var_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3335 | SPIRVInstList.push_back(Inst); |
| 3336 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3337 | SPIRVInstructionList &Annotations = getSPIRVInstList(kAnnotations); |
alan-baker | bed3a88 | 2020-04-21 14:42:41 -0400 | [diff] [blame] | 3338 | // If we have a builtin (not WorkDim). |
| 3339 | if (spv::BuiltInMax != BuiltinType && BuiltinType != spv::BuiltInWorkDim) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3340 | // |
| 3341 | // Generate OpDecorate. |
| 3342 | // |
| 3343 | // DOps[0] = Target ID |
| 3344 | // DOps[1] = Decoration (Builtin) |
| 3345 | // DOps[2] = BuiltIn ID |
| 3346 | uint32_t ResultID; |
| 3347 | |
| 3348 | // WorkgroupSize is different, we decorate the constant composite that has |
| 3349 | // its value, rather than the variable that we use to access the value. |
| 3350 | if (spv::BuiltInWorkgroupSize == BuiltinType) { |
| 3351 | ResultID = InitializerID; |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 3352 | // Save both the value and variable IDs for later. |
| 3353 | WorkgroupSizeValueID = InitializerID; |
| 3354 | WorkgroupSizeVarID = VMap[&GV]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3355 | } else { |
| 3356 | ResultID = VMap[&GV]; |
| 3357 | } |
| 3358 | |
| 3359 | SPIRVOperandList DOps; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3360 | DOps << MkId(ResultID) << MkNum(spv::DecorationBuiltIn) |
| 3361 | << MkNum(BuiltinType); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3362 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3363 | auto *DescDecoInst = new SPIRVInstruction(spv::OpDecorate, DOps); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3364 | Annotations.push_back(DescDecoInst); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3365 | } else if (module_scope_constant_external_init) { |
| 3366 | // This module scope constant is initialized from a storage buffer with data |
| 3367 | // provided by the host at binding 0 of the next descriptor set. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3368 | const uint32_t descriptor_set = TakeDescriptorIndex(module); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3369 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3370 | // Emit the intializer to the descriptor map file. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3371 | // Use "kind,buffer" to indicate storage buffer. We might want to expand |
| 3372 | // that later to other types, like uniform buffer. |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3373 | std::string hexbytes; |
| 3374 | llvm::raw_string_ostream str(hexbytes); |
| 3375 | clspv::ConstantEmitter(DL, str).Emit(GV.getInitializer()); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3376 | version0::DescriptorMapEntry::ConstantData constant_data = {ArgKind::Buffer, |
| 3377 | str.str()}; |
| 3378 | descriptorMapEntries->emplace_back(std::move(constant_data), descriptor_set, |
| 3379 | 0); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3380 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3381 | SPIRVOperandList DOps; |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 3382 | |
| 3383 | // OpDecorate %var DescriptorSet <descriptor_set> |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3384 | DOps << MkId(var_id) << MkNum(spv::DecorationDescriptorSet) |
| 3385 | << MkNum(descriptor_set); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3386 | Annotations.push_back(new SPIRVInstruction(spv::OpDecorate, DOps)); |
| 3387 | |
| 3388 | // OpDecorate %var Binding <binding> |
| 3389 | DOps.clear(); |
| 3390 | DOps << MkId(var_id) << MkNum(spv::DecorationBinding) << MkNum(0); |
| 3391 | Annotations.push_back(new SPIRVInstruction(spv::OpDecorate, DOps)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3395 | void SPIRVProducerPass::GenerateWorkgroupVars() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3396 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kGlobalVariables); |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3397 | auto spec_constant_md = |
| 3398 | module->getNamedMetadata(clspv::SpecConstantMetadataName()); |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3399 | if (!spec_constant_md) |
| 3400 | return; |
| 3401 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3402 | for (auto pair : clspv::GetSpecConstants(module)) { |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 3403 | auto kind = pair.first; |
| 3404 | auto spec_id = pair.second; |
| 3405 | |
| 3406 | if (kind != SpecConstant::kLocalMemorySize) |
| 3407 | continue; |
| 3408 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3409 | LocalArgInfo &info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 3410 | |
| 3411 | // Generate OpVariable. |
| 3412 | // |
| 3413 | // GIDOps[0] : Result Type ID |
| 3414 | // GIDOps[1] : Storage Class |
| 3415 | SPIRVOperandList Ops; |
| 3416 | Ops << MkId(info.ptr_array_type_id) << MkNum(spv::StorageClassWorkgroup); |
| 3417 | |
| 3418 | SPIRVInstList.push_back( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3419 | new SPIRVInstruction(spv::OpVariable, info.variable_id, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 3420 | } |
| 3421 | } |
| 3422 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3423 | void SPIRVProducerPass::GenerateDescriptorMapInfo(Function &F) { |
| 3424 | const auto &DL = module->getDataLayout(); |
David Neto | c5fb524 | 2018-07-30 13:28:31 -0400 | [diff] [blame] | 3425 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 3426 | return; |
| 3427 | } |
Kévin Petit | 717f857 | 2020-04-06 17:31:53 +0100 | [diff] [blame] | 3428 | // Add entries for each kernel |
| 3429 | version0::DescriptorMapEntry::KernelDeclData kernel_decl_data = { |
| 3430 | F.getName().str()}; |
| 3431 | descriptorMapEntries->emplace_back(std::move(kernel_decl_data)); |
| 3432 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3433 | // Gather the list of resources that are used by this function's arguments. |
| 3434 | auto &resource_var_at_index = FunctionToResourceVarsMap[&F]; |
| 3435 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3436 | // TODO(alan-baker): This should become unnecessary by fixing the rest of the |
| 3437 | // flow to generate pod_ubo arguments earlier. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3438 | auto remap_arg_kind = [](StringRef argKind) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3439 | std::string kind = |
| 3440 | clspv::Option::PodArgsInUniformBuffer() && argKind.equals("pod") |
| 3441 | ? "pod_ubo" |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 3442 | : argKind.str(); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3443 | return GetArgKindFromName(kind); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3444 | }; |
| 3445 | |
| 3446 | auto *fty = F.getType()->getPointerElementType(); |
| 3447 | auto *func_ty = dyn_cast<FunctionType>(fty); |
| 3448 | |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 3449 | // If we've clustered POD arguments, then argument details are in metadata. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3450 | // If an argument maps to a resource variable, then get descriptor set and |
| 3451 | // binding from the resoure variable. Other info comes from the metadata. |
| 3452 | const auto *arg_map = F.getMetadata("kernel_arg_map"); |
| 3453 | if (arg_map) { |
| 3454 | for (const auto &arg : arg_map->operands()) { |
| 3455 | const MDNode *arg_node = dyn_cast<MDNode>(arg.get()); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3456 | assert(arg_node->getNumOperands() == 7); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3457 | const auto name = |
| 3458 | dyn_cast<MDString>(arg_node->getOperand(0))->getString(); |
| 3459 | const auto old_index = |
| 3460 | dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue(); |
| 3461 | // Remapped argument index |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3462 | const size_t new_index = static_cast<size_t>( |
| 3463 | dyn_extract<ConstantInt>(arg_node->getOperand(2))->getZExtValue()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3464 | const auto offset = |
| 3465 | dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue(); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3466 | const auto arg_size = |
| 3467 | dyn_extract<ConstantInt>(arg_node->getOperand(4))->getZExtValue(); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3468 | const auto argKind = remap_arg_kind( |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3469 | dyn_cast<MDString>(arg_node->getOperand(5))->getString()); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3470 | const auto spec_id = |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3471 | dyn_extract<ConstantInt>(arg_node->getOperand(6))->getSExtValue(); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3472 | |
| 3473 | uint32_t descriptor_set = 0; |
| 3474 | uint32_t binding = 0; |
| 3475 | version0::DescriptorMapEntry::KernelArgData kernel_data = { |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 3476 | F.getName().str(), name.str(), static_cast<uint32_t>(old_index), |
| 3477 | argKind, static_cast<uint32_t>(spec_id), |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3478 | // This will be set below for pointer-to-local args. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3479 | 0, static_cast<uint32_t>(offset), static_cast<uint32_t>(arg_size)}; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3480 | if (spec_id > 0) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3481 | kernel_data.local_element_size = static_cast<uint32_t>(GetTypeAllocSize( |
| 3482 | func_ty->getParamType(unsigned(new_index))->getPointerElementType(), |
| 3483 | DL)); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3484 | } else { |
| 3485 | auto *info = resource_var_at_index[new_index]; |
| 3486 | assert(info); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3487 | descriptor_set = info->descriptor_set; |
| 3488 | binding = info->binding; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3489 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3490 | descriptorMapEntries->emplace_back(std::move(kernel_data), descriptor_set, |
| 3491 | binding); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3492 | } |
| 3493 | } else { |
| 3494 | // There is no argument map. |
| 3495 | // Take descriptor info from the resource variable calls. |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3496 | // Take argument name and size from the arguments list. |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3497 | |
| 3498 | SmallVector<Argument *, 4> arguments; |
| 3499 | for (auto &arg : F.args()) { |
| 3500 | arguments.push_back(&arg); |
| 3501 | } |
| 3502 | |
| 3503 | unsigned arg_index = 0; |
| 3504 | for (auto *info : resource_var_at_index) { |
| 3505 | if (info) { |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3506 | auto arg = arguments[arg_index]; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3507 | unsigned arg_size = 0; |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 3508 | if (info->arg_kind == clspv::ArgKind::Pod || |
| 3509 | info->arg_kind == clspv::ArgKind::PodUBO || |
| 3510 | info->arg_kind == clspv::ArgKind::PodPushConstant) { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3511 | arg_size = static_cast<uint32_t>(DL.getTypeStoreSize(arg->getType())); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3514 | // Local pointer arguments are unused in this case. Offset is always |
| 3515 | // zero. |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3516 | version0::DescriptorMapEntry::KernelArgData kernel_data = { |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 3517 | F.getName().str(), |
| 3518 | arg->getName().str(), |
| 3519 | arg_index, |
| 3520 | remap_arg_kind(clspv::GetArgKindName(info->arg_kind)), |
| 3521 | 0, |
| 3522 | 0, |
| 3523 | 0, |
| 3524 | arg_size}; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3525 | descriptorMapEntries->emplace_back(std::move(kernel_data), |
| 3526 | info->descriptor_set, info->binding); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3527 | } |
| 3528 | arg_index++; |
| 3529 | } |
| 3530 | // Generate mappings for pointer-to-local arguments. |
| 3531 | for (arg_index = 0; arg_index < arguments.size(); ++arg_index) { |
| 3532 | Argument *arg = arguments[arg_index]; |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 3533 | auto where = LocalArgSpecIds.find(arg); |
| 3534 | if (where != LocalArgSpecIds.end()) { |
| 3535 | auto &local_arg_info = LocalSpecIdInfoMap[where->second]; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3536 | // Pod arguments members are unused in this case. |
| 3537 | version0::DescriptorMapEntry::KernelArgData kernel_data = { |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 3538 | F.getName().str(), |
| 3539 | arg->getName().str(), |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3540 | arg_index, |
| 3541 | ArgKind::Local, |
| 3542 | static_cast<uint32_t>(local_arg_info.spec_id), |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3543 | static_cast<uint32_t>( |
| 3544 | GetTypeAllocSize(local_arg_info.elem_type, DL)), |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 3545 | 0, |
| 3546 | 0}; |
| 3547 | // Pointer-to-local arguments do not utilize descriptor set and binding. |
| 3548 | descriptorMapEntries->emplace_back(std::move(kernel_data), 0, 0); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 3549 | } |
| 3550 | } |
| 3551 | } |
| 3552 | } |
| 3553 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3554 | void SPIRVProducerPass::GenerateFuncPrologue(Function &F) { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3555 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kFunctions); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3556 | ValueMapType &VMap = getValueMap(); |
| 3557 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3558 | auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap(); |
| 3559 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 3560 | |
| 3561 | FunctionType *FTy = F.getFunctionType(); |
| 3562 | |
| 3563 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3564 | // Generate OPFunction. |
| 3565 | // |
| 3566 | |
| 3567 | // FOps[0] : Result Type ID |
| 3568 | // FOps[1] : Function Control |
| 3569 | // FOps[2] : Function Type ID |
| 3570 | SPIRVOperandList FOps; |
| 3571 | |
| 3572 | // Find SPIRV instruction for return type. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3573 | FOps << MkId(lookupType(FTy->getReturnType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3574 | |
| 3575 | // Check function attributes for SPIRV Function Control. |
| 3576 | uint32_t FuncControl = spv::FunctionControlMaskNone; |
| 3577 | if (F.hasFnAttribute(Attribute::AlwaysInline)) { |
| 3578 | FuncControl |= spv::FunctionControlInlineMask; |
| 3579 | } |
| 3580 | if (F.hasFnAttribute(Attribute::NoInline)) { |
| 3581 | FuncControl |= spv::FunctionControlDontInlineMask; |
| 3582 | } |
| 3583 | // TODO: Check llvm attribute for Function Control Pure. |
| 3584 | if (F.hasFnAttribute(Attribute::ReadOnly)) { |
| 3585 | FuncControl |= spv::FunctionControlPureMask; |
| 3586 | } |
| 3587 | // TODO: Check llvm attribute for Function Control Const. |
| 3588 | if (F.hasFnAttribute(Attribute::ReadNone)) { |
| 3589 | FuncControl |= spv::FunctionControlConstMask; |
| 3590 | } |
| 3591 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3592 | FOps << MkNum(FuncControl); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3593 | |
| 3594 | uint32_t FTyID; |
| 3595 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 3596 | SmallVector<Type *, 4> NewFuncParamTys; |
| 3597 | FunctionType *NewFTy = |
| 3598 | FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false); |
| 3599 | FTyID = lookupType(NewFTy); |
| 3600 | } else { |
David Neto | 9ed8e2f | 2018-03-24 06:47:24 -0700 | [diff] [blame] | 3601 | // Handle regular function with global constant parameters. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3602 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 3603 | FTyID = lookupType(GlobalConstFuncTyMap[FTy].first); |
| 3604 | } else { |
| 3605 | FTyID = lookupType(FTy); |
| 3606 | } |
| 3607 | } |
| 3608 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3609 | FOps << MkId(FTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3610 | |
| 3611 | if (F.getCallingConv() == CallingConv::SPIR_KERNEL) { |
| 3612 | EntryPoints.push_back(std::make_pair(&F, nextID)); |
| 3613 | } |
| 3614 | |
| 3615 | VMap[&F] = nextID; |
| 3616 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 3617 | if (clspv::Option::ShowIDs()) { |
David Neto | b05675d | 2018-02-16 12:37:49 -0500 | [diff] [blame] | 3618 | errs() << "Function " << F.getName() << " is " << nextID << "\n"; |
| 3619 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3620 | // Generate SPIRV instruction for function. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3621 | auto *FuncInst = new SPIRVInstruction(spv::OpFunction, nextID++, FOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3622 | SPIRVInstList.push_back(FuncInst); |
| 3623 | |
| 3624 | // |
| 3625 | // Generate OpFunctionParameter for Normal function. |
| 3626 | // |
| 3627 | |
| 3628 | if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 3629 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3630 | // Iterate Argument for name instead of param type from function type. |
| 3631 | unsigned ArgIdx = 0; |
| 3632 | for (Argument &Arg : F.args()) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 3633 | uint32_t param_id = nextID++; |
| 3634 | VMap[&Arg] = param_id; |
| 3635 | |
| 3636 | if (CalledWithCoherentResource(Arg)) { |
| 3637 | // If the arg is passed a coherent resource ever, then decorate this |
| 3638 | // parameter with Coherent too. |
| 3639 | SPIRVOperandList decoration_ops; |
| 3640 | decoration_ops << MkId(param_id) << MkNum(spv::DecorationCoherent); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3641 | getSPIRVInstList(kAnnotations) |
| 3642 | .push_back(new SPIRVInstruction(spv::OpDecorate, decoration_ops)); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 3643 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3644 | |
| 3645 | // ParamOps[0] : Result Type ID |
| 3646 | SPIRVOperandList ParamOps; |
| 3647 | |
| 3648 | // Find SPIRV instruction for parameter type. |
| 3649 | uint32_t ParamTyID = lookupType(Arg.getType()); |
| 3650 | if (PointerType *PTy = dyn_cast<PointerType>(Arg.getType())) { |
| 3651 | if (GlobalConstFuncTyMap.count(FTy)) { |
| 3652 | if (ArgIdx == GlobalConstFuncTyMap[FTy].second) { |
| 3653 | Type *EleTy = PTy->getPointerElementType(); |
| 3654 | Type *ArgTy = |
| 3655 | PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
| 3656 | ParamTyID = lookupType(ArgTy); |
| 3657 | GlobalConstArgSet.insert(&Arg); |
| 3658 | } |
| 3659 | } |
| 3660 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3661 | ParamOps << MkId(ParamTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3662 | |
| 3663 | // Generate SPIRV instruction for parameter. |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3664 | auto *ParamInst = |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 3665 | new SPIRVInstruction(spv::OpFunctionParameter, param_id, ParamOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3666 | SPIRVInstList.push_back(ParamInst); |
| 3667 | |
| 3668 | ArgIdx++; |
| 3669 | } |
| 3670 | } |
| 3671 | } |
| 3672 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3673 | void SPIRVProducerPass::GenerateModuleInfo() { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3674 | EntryPointVecType &EntryPoints = getEntryPointVec(); |
| 3675 | ValueMapType &VMap = getValueMap(); |
| 3676 | ValueList &EntryPointInterfaces = getEntryPointInterfacesVec(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3677 | std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec(); |
| 3678 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3679 | SPIRVInstructionList &SPIRVCapabilities = getSPIRVInstList(kCapabilities); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3680 | // |
| 3681 | // Generate OpCapability |
| 3682 | // |
| 3683 | // TODO: Which llvm information is mapped to SPIRV Capapbility? |
| 3684 | |
| 3685 | // Ops[0] = Capability |
| 3686 | SPIRVOperandList Ops; |
| 3687 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3688 | auto *CapInst = |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 3689 | new SPIRVInstruction(spv::OpCapability, MkNum(spv::CapabilityShader)); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3690 | SPIRVCapabilities.push_back(CapInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3691 | |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3692 | bool write_without_format = false; |
| 3693 | bool sampled_1d = false; |
| 3694 | bool image_1d = false; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3695 | for (Type *Ty : getTypeList()) { |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 3696 | if (clspv::Option::Int8Support() && Ty->isIntegerTy(8)) { |
| 3697 | // Generate OpCapability for i8 type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3698 | SPIRVCapabilities.push_back( |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 3699 | new SPIRVInstruction(spv::OpCapability, MkNum(spv::CapabilityInt8))); |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 3700 | } else if (Ty->isIntegerTy(16)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3701 | // Generate OpCapability for i16 type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3702 | SPIRVCapabilities.push_back( |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 3703 | new SPIRVInstruction(spv::OpCapability, MkNum(spv::CapabilityInt16))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3704 | } else if (Ty->isIntegerTy(64)) { |
| 3705 | // Generate OpCapability for i64 type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3706 | SPIRVCapabilities.push_back( |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 3707 | new SPIRVInstruction(spv::OpCapability, MkNum(spv::CapabilityInt64))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3708 | } else if (Ty->isHalfTy()) { |
| 3709 | // Generate OpCapability for half type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3710 | SPIRVCapabilities.push_back(new SPIRVInstruction( |
| 3711 | spv::OpCapability, MkNum(spv::CapabilityFloat16))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3712 | } else if (Ty->isDoubleTy()) { |
| 3713 | // Generate OpCapability for double type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3714 | SPIRVCapabilities.push_back(new SPIRVInstruction( |
| 3715 | spv::OpCapability, MkNum(spv::CapabilityFloat64))); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3716 | } else if (auto *STy = dyn_cast<StructType>(Ty)) { |
| 3717 | if (STy->isOpaque()) { |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3718 | if (STy->getName().startswith("opencl.image1d_wo_t") || |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 3719 | STy->getName().startswith("opencl.image1d_array_wo_t") || |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3720 | STy->getName().startswith("opencl.image2d_wo_t") || |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 3721 | STy->getName().startswith("opencl.image2d_array_wo_t") || |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 3722 | STy->getName().startswith("opencl.image3d_wo_t")) { |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3723 | write_without_format = true; |
| 3724 | } |
| 3725 | if (STy->getName().startswith("opencl.image1d_ro_t") || |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 3726 | STy->getName().startswith("opencl.image1d_wo_t") || |
| 3727 | STy->getName().startswith("opencl.image1d_array_ro_t") || |
| 3728 | STy->getName().startswith("opencl.image1d_array_wo_t")) { |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3729 | if (STy->getName().contains(".sampled")) |
| 3730 | sampled_1d = true; |
| 3731 | else |
| 3732 | image_1d = true; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3733 | } |
| 3734 | } |
| 3735 | } |
| 3736 | } |
| 3737 | |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3738 | if (write_without_format) { |
| 3739 | // Generate OpCapability for write only image type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3740 | SPIRVCapabilities.push_back(new SPIRVInstruction( |
| 3741 | spv::OpCapability, |
| 3742 | {MkNum(spv::CapabilityStorageImageWriteWithoutFormat)})); |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3743 | } |
| 3744 | if (image_1d) { |
| 3745 | // Generate OpCapability for unsampled 1D image type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3746 | SPIRVCapabilities.push_back(new SPIRVInstruction( |
| 3747 | spv::OpCapability, {MkNum(spv::CapabilityImage1D)})); |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3748 | } else if (sampled_1d) { |
| 3749 | // Generate OpCapability for sampled 1D image type. |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3750 | SPIRVCapabilities.push_back(new SPIRVInstruction( |
| 3751 | spv::OpCapability, {MkNum(spv::CapabilitySampled1D)})); |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 3752 | } |
| 3753 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3754 | { // OpCapability ImageQuery |
| 3755 | bool hasImageQuery = false; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 3756 | for (const auto &SymVal : module->getValueSymbolTable()) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 3757 | if (auto F = dyn_cast<Function>(SymVal.getValue())) { |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 3758 | if (IsImageQuery(F)) { |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 3759 | hasImageQuery = true; |
| 3760 | break; |
| 3761 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3762 | } |
| 3763 | } |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 3764 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3765 | if (hasImageQuery) { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3766 | SPIRVCapabilities.push_back(new SPIRVInstruction( |
| 3767 | spv::OpCapability, {MkNum(spv::CapabilityImageQuery)})); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3771 | if (hasVariablePointers()) { |
| 3772 | // |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3773 | // Generate OpCapability. |
| 3774 | // |
| 3775 | // Ops[0] = Capability |
| 3776 | // |
| 3777 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3778 | Ops << MkNum(spv::CapabilityVariablePointers); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3779 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3780 | SPIRVCapabilities.push_back(new SPIRVInstruction(spv::OpCapability, Ops)); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3781 | } else if (hasVariablePointersStorageBuffer()) { |
| 3782 | // |
| 3783 | // Generate OpCapability. |
| 3784 | // |
| 3785 | // Ops[0] = Capability |
| 3786 | // |
| 3787 | Ops.clear(); |
| 3788 | Ops << MkNum(spv::CapabilityVariablePointersStorageBuffer); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3789 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3790 | SPIRVCapabilities.push_back(new SPIRVInstruction(spv::OpCapability, Ops)); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3791 | } |
| 3792 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3793 | SPIRVInstructionList &SPIRVExtensions = getSPIRVInstList(kExtensions); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3794 | // Always add the storage buffer extension |
| 3795 | { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3796 | // |
| 3797 | // Generate OpExtension. |
| 3798 | // |
| 3799 | // Ops[0] = Name (Literal String) |
| 3800 | // |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3801 | auto *ExtensionInst = new SPIRVInstruction( |
| 3802 | spv::OpExtension, {MkString("SPV_KHR_storage_buffer_storage_class")}); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3803 | SPIRVExtensions.push_back(ExtensionInst); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3804 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3805 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3806 | if (hasVariablePointers() || hasVariablePointersStorageBuffer()) { |
| 3807 | // |
| 3808 | // Generate OpExtension. |
| 3809 | // |
| 3810 | // Ops[0] = Name (Literal String) |
| 3811 | // |
| 3812 | auto *ExtensionInst = new SPIRVInstruction( |
| 3813 | spv::OpExtension, {MkString("SPV_KHR_variable_pointers")}); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3814 | SPIRVExtensions.push_back(ExtensionInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | // |
| 3818 | // Generate OpMemoryModel |
| 3819 | // |
| 3820 | // Memory model for Vulkan will always be GLSL450. |
| 3821 | |
| 3822 | // Ops[0] = Addressing Model |
| 3823 | // Ops[1] = Memory Model |
| 3824 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3825 | Ops << MkNum(spv::AddressingModelLogical) << MkNum(spv::MemoryModelGLSL450); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3826 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3827 | auto *MemModelInst = new SPIRVInstruction(spv::OpMemoryModel, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3828 | getSPIRVInstList(kMemoryModel).push_back(MemModelInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3829 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3830 | SPIRVInstructionList &SPIRVEntryPoints = getSPIRVInstList(kEntryPoints); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3831 | // |
| 3832 | // Generate OpEntryPoint |
| 3833 | // |
| 3834 | for (auto EntryPoint : EntryPoints) { |
| 3835 | // Ops[0] = Execution Model |
| 3836 | // Ops[1] = EntryPoint ID |
| 3837 | // Ops[2] = Name (Literal String) |
| 3838 | // ... |
| 3839 | // |
| 3840 | // TODO: Do we need to consider Interface ID for forward references??? |
| 3841 | Ops.clear(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3842 | const StringRef &name = EntryPoint.first->getName(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3843 | Ops << MkNum(spv::ExecutionModelGLCompute) << MkId(EntryPoint.second) |
| 3844 | << MkString(name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3845 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3846 | for (Value *Interface : EntryPointInterfaces) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3847 | Ops << MkId(VMap[Interface]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3848 | } |
| 3849 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3850 | auto *EntryPointInst = new SPIRVInstruction(spv::OpEntryPoint, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3851 | SPIRVEntryPoints.push_back(EntryPointInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3852 | } |
| 3853 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3854 | SPIRVInstructionList &SPIRVExecutionModes = getSPIRVInstList(kExecutionModes); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3855 | for (auto EntryPoint : EntryPoints) { |
| 3856 | if (const MDNode *MD = dyn_cast<Function>(EntryPoint.first) |
| 3857 | ->getMetadata("reqd_work_group_size")) { |
| 3858 | |
| 3859 | if (!BuiltinDimVec.empty()) { |
| 3860 | llvm_unreachable( |
| 3861 | "Kernels should have consistent work group size definition"); |
| 3862 | } |
| 3863 | |
| 3864 | // |
| 3865 | // Generate OpExecutionMode |
| 3866 | // |
| 3867 | |
| 3868 | // Ops[0] = Entry Point ID |
| 3869 | // Ops[1] = Execution Mode |
| 3870 | // Ops[2] ... Ops[n] = Optional literals according to Execution Mode |
| 3871 | Ops.clear(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3872 | Ops << MkId(EntryPoint.second) << MkNum(spv::ExecutionModeLocalSize); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3873 | |
| 3874 | uint32_t XDim = static_cast<uint32_t>( |
| 3875 | mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue()); |
| 3876 | uint32_t YDim = static_cast<uint32_t>( |
| 3877 | mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue()); |
| 3878 | uint32_t ZDim = static_cast<uint32_t>( |
| 3879 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue()); |
| 3880 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3881 | Ops << MkNum(XDim) << MkNum(YDim) << MkNum(ZDim); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3882 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3883 | auto *ExecModeInst = new SPIRVInstruction(spv::OpExecutionMode, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3884 | SPIRVExecutionModes.push_back(ExecModeInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | // |
| 3889 | // Generate OpSource. |
| 3890 | // |
| 3891 | // Ops[0] = SourceLanguage ID |
| 3892 | // Ops[1] = Version (LiteralNum) |
| 3893 | // |
| 3894 | Ops.clear(); |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3895 | switch (clspv::Option::Language()) { |
| 3896 | case clspv::Option::SourceLanguage::OpenCL_C_10: |
| 3897 | Ops << MkNum(spv::SourceLanguageOpenCL_C) << MkNum(100); |
| 3898 | break; |
| 3899 | case clspv::Option::SourceLanguage::OpenCL_C_11: |
| 3900 | Ops << MkNum(spv::SourceLanguageOpenCL_C) << MkNum(110); |
| 3901 | break; |
| 3902 | case clspv::Option::SourceLanguage::OpenCL_C_12: |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 3903 | Ops << MkNum(spv::SourceLanguageOpenCL_C) << MkNum(120); |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 3904 | break; |
| 3905 | case clspv::Option::SourceLanguage::OpenCL_C_20: |
| 3906 | Ops << MkNum(spv::SourceLanguageOpenCL_C) << MkNum(200); |
| 3907 | break; |
| 3908 | case clspv::Option::SourceLanguage::OpenCL_CPP: |
| 3909 | Ops << MkNum(spv::SourceLanguageOpenCL_CPP) << MkNum(100); |
| 3910 | break; |
| 3911 | default: |
| 3912 | Ops << MkNum(spv::SourceLanguageUnknown) << MkNum(0); |
| 3913 | break; |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 3914 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3915 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3916 | auto *OpenSourceInst = new SPIRVInstruction(spv::OpSource, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3917 | getSPIRVInstList(kDebug).push_back(OpenSourceInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3918 | |
| 3919 | if (!BuiltinDimVec.empty()) { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3920 | SPIRVInstructionList &SPIRVAnnotations = getSPIRVInstList(kAnnotations); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3921 | // |
| 3922 | // Generate OpDecorates for x/y/z dimension. |
| 3923 | // |
| 3924 | // Ops[0] = Target ID |
| 3925 | // Ops[1] = Decoration (SpecId) |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3926 | // Ops[2] = Specialization Constant ID (Literal Number) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3927 | |
| 3928 | // X Dimension |
| 3929 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3930 | Ops << MkId(BuiltinDimVec[0]) << MkNum(spv::DecorationSpecId) << MkNum(0); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3931 | SPIRVAnnotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3932 | |
| 3933 | // Y Dimension |
| 3934 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3935 | Ops << MkId(BuiltinDimVec[1]) << MkNum(spv::DecorationSpecId) << MkNum(1); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3936 | SPIRVAnnotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3937 | |
| 3938 | // Z Dimension |
| 3939 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 3940 | Ops << MkId(BuiltinDimVec[2]) << MkNum(spv::DecorationSpecId) << MkNum(2); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3941 | SPIRVAnnotations.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3942 | } |
| 3943 | } |
| 3944 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3945 | void SPIRVProducerPass::GenerateEntryPointInitialStores() { |
| 3946 | // Work around a driver bug. Initializers on Private variables might not |
| 3947 | // work. So the start of the kernel should store the initializer value to the |
| 3948 | // variables. Yes, *every* entry point pays this cost if *any* entry point |
| 3949 | // uses this builtin. At this point I judge this to be an acceptable tradeoff |
| 3950 | // of complexity vs. runtime, for a broken driver. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 3951 | // TODO(dneto): Remove this at some point once fixed drivers are widely |
| 3952 | // available. |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3953 | if (WorkgroupSizeVarID) { |
| 3954 | assert(WorkgroupSizeValueID); |
| 3955 | |
| 3956 | SPIRVOperandList Ops; |
| 3957 | Ops << MkId(WorkgroupSizeVarID) << MkId(WorkgroupSizeValueID); |
| 3958 | |
| 3959 | auto *Inst = new SPIRVInstruction(spv::OpStore, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3960 | getSPIRVInstList(kFunctions).push_back(Inst); |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3961 | } |
| 3962 | } |
| 3963 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3964 | void SPIRVProducerPass::GenerateFuncBody(Function &F) { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 3965 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kFunctions); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3966 | ValueMapType &VMap = getValueMap(); |
| 3967 | |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3968 | const bool IsKernel = F.getCallingConv() == CallingConv::SPIR_KERNEL; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3969 | |
| 3970 | for (BasicBlock &BB : F) { |
| 3971 | // Register BasicBlock to ValueMap. |
| 3972 | VMap[&BB] = nextID; |
| 3973 | |
| 3974 | // |
| 3975 | // Generate OpLabel for Basic Block. |
| 3976 | // |
| 3977 | SPIRVOperandList Ops; |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 3978 | auto *Inst = new SPIRVInstruction(spv::OpLabel, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3979 | SPIRVInstList.push_back(Inst); |
| 3980 | |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3981 | // OpVariable instructions must come first. |
| 3982 | for (Instruction &I : BB) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3983 | if (auto *alloca = dyn_cast<AllocaInst>(&I)) { |
| 3984 | // Allocating a pointer requires variable pointers. |
| 3985 | if (alloca->getAllocatedType()->isPointerTy()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 3986 | setVariablePointersCapabilities( |
| 3987 | alloca->getAllocatedType()->getPointerAddressSpace()); |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 3988 | } |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 3989 | GenerateInstruction(I); |
| 3990 | } |
| 3991 | } |
| 3992 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3993 | if (&BB == &F.getEntryBlock() && IsKernel) { |
David Neto | b6e2e06 | 2018-04-25 10:32:06 -0400 | [diff] [blame] | 3994 | if (clspv::Option::HackInitializers()) { |
| 3995 | GenerateEntryPointInitialStores(); |
| 3996 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | for (Instruction &I : BB) { |
David Neto | 6dcd471 | 2017-06-23 11:06:47 -0400 | [diff] [blame] | 4000 | if (!isa<AllocaInst>(I)) { |
| 4001 | GenerateInstruction(I); |
| 4002 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | spv::Op SPIRVProducerPass::GetSPIRVCmpOpcode(CmpInst *I) { |
| 4008 | const std::map<CmpInst::Predicate, spv::Op> Map = { |
| 4009 | {CmpInst::ICMP_EQ, spv::OpIEqual}, |
| 4010 | {CmpInst::ICMP_NE, spv::OpINotEqual}, |
| 4011 | {CmpInst::ICMP_UGT, spv::OpUGreaterThan}, |
| 4012 | {CmpInst::ICMP_UGE, spv::OpUGreaterThanEqual}, |
| 4013 | {CmpInst::ICMP_ULT, spv::OpULessThan}, |
| 4014 | {CmpInst::ICMP_ULE, spv::OpULessThanEqual}, |
| 4015 | {CmpInst::ICMP_SGT, spv::OpSGreaterThan}, |
| 4016 | {CmpInst::ICMP_SGE, spv::OpSGreaterThanEqual}, |
| 4017 | {CmpInst::ICMP_SLT, spv::OpSLessThan}, |
| 4018 | {CmpInst::ICMP_SLE, spv::OpSLessThanEqual}, |
| 4019 | {CmpInst::FCMP_OEQ, spv::OpFOrdEqual}, |
| 4020 | {CmpInst::FCMP_OGT, spv::OpFOrdGreaterThan}, |
| 4021 | {CmpInst::FCMP_OGE, spv::OpFOrdGreaterThanEqual}, |
| 4022 | {CmpInst::FCMP_OLT, spv::OpFOrdLessThan}, |
| 4023 | {CmpInst::FCMP_OLE, spv::OpFOrdLessThanEqual}, |
| 4024 | {CmpInst::FCMP_ONE, spv::OpFOrdNotEqual}, |
| 4025 | {CmpInst::FCMP_UEQ, spv::OpFUnordEqual}, |
| 4026 | {CmpInst::FCMP_UGT, spv::OpFUnordGreaterThan}, |
| 4027 | {CmpInst::FCMP_UGE, spv::OpFUnordGreaterThanEqual}, |
| 4028 | {CmpInst::FCMP_ULT, spv::OpFUnordLessThan}, |
| 4029 | {CmpInst::FCMP_ULE, spv::OpFUnordLessThanEqual}, |
| 4030 | {CmpInst::FCMP_UNE, spv::OpFUnordNotEqual}}; |
| 4031 | |
| 4032 | assert(0 != Map.count(I->getPredicate())); |
| 4033 | |
| 4034 | return Map.at(I->getPredicate()); |
| 4035 | } |
| 4036 | |
| 4037 | spv::Op SPIRVProducerPass::GetSPIRVCastOpcode(Instruction &I) { |
| 4038 | const std::map<unsigned, spv::Op> Map{ |
| 4039 | {Instruction::Trunc, spv::OpUConvert}, |
| 4040 | {Instruction::ZExt, spv::OpUConvert}, |
| 4041 | {Instruction::SExt, spv::OpSConvert}, |
| 4042 | {Instruction::FPToUI, spv::OpConvertFToU}, |
| 4043 | {Instruction::FPToSI, spv::OpConvertFToS}, |
| 4044 | {Instruction::UIToFP, spv::OpConvertUToF}, |
| 4045 | {Instruction::SIToFP, spv::OpConvertSToF}, |
| 4046 | {Instruction::FPTrunc, spv::OpFConvert}, |
| 4047 | {Instruction::FPExt, spv::OpFConvert}, |
| 4048 | {Instruction::BitCast, spv::OpBitcast}}; |
| 4049 | |
| 4050 | assert(0 != Map.count(I.getOpcode())); |
| 4051 | |
| 4052 | return Map.at(I.getOpcode()); |
| 4053 | } |
| 4054 | |
| 4055 | spv::Op SPIRVProducerPass::GetSPIRVBinaryOpcode(Instruction &I) { |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 4056 | if (I.getType()->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4057 | switch (I.getOpcode()) { |
| 4058 | default: |
| 4059 | break; |
| 4060 | case Instruction::Or: |
| 4061 | return spv::OpLogicalOr; |
| 4062 | case Instruction::And: |
| 4063 | return spv::OpLogicalAnd; |
| 4064 | case Instruction::Xor: |
| 4065 | return spv::OpLogicalNotEqual; |
| 4066 | } |
| 4067 | } |
| 4068 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4069 | const std::map<unsigned, spv::Op> Map{ |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4070 | {Instruction::Add, spv::OpIAdd}, |
| 4071 | {Instruction::FAdd, spv::OpFAdd}, |
| 4072 | {Instruction::Sub, spv::OpISub}, |
| 4073 | {Instruction::FSub, spv::OpFSub}, |
| 4074 | {Instruction::Mul, spv::OpIMul}, |
| 4075 | {Instruction::FMul, spv::OpFMul}, |
| 4076 | {Instruction::UDiv, spv::OpUDiv}, |
| 4077 | {Instruction::SDiv, spv::OpSDiv}, |
| 4078 | {Instruction::FDiv, spv::OpFDiv}, |
| 4079 | {Instruction::URem, spv::OpUMod}, |
| 4080 | {Instruction::SRem, spv::OpSRem}, |
| 4081 | {Instruction::FRem, spv::OpFRem}, |
| 4082 | {Instruction::Or, spv::OpBitwiseOr}, |
| 4083 | {Instruction::Xor, spv::OpBitwiseXor}, |
| 4084 | {Instruction::And, spv::OpBitwiseAnd}, |
| 4085 | {Instruction::Shl, spv::OpShiftLeftLogical}, |
| 4086 | {Instruction::LShr, spv::OpShiftRightLogical}, |
| 4087 | {Instruction::AShr, spv::OpShiftRightArithmetic}}; |
| 4088 | |
| 4089 | assert(0 != Map.count(I.getOpcode())); |
| 4090 | |
| 4091 | return Map.at(I.getOpcode()); |
| 4092 | } |
| 4093 | |
| 4094 | void SPIRVProducerPass::GenerateInstruction(Instruction &I) { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 4095 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kFunctions); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4096 | ValueMapType &VMap = getValueMap(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4097 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 4098 | LLVMContext &Context = I.getParent()->getParent()->getParent()->getContext(); |
| 4099 | |
| 4100 | // Register Instruction to ValueMap. |
| 4101 | if (0 == VMap[&I]) { |
| 4102 | VMap[&I] = nextID; |
| 4103 | } |
| 4104 | |
| 4105 | switch (I.getOpcode()) { |
| 4106 | default: { |
| 4107 | if (Instruction::isCast(I.getOpcode())) { |
| 4108 | // |
| 4109 | // Generate SPIRV instructions for cast operators. |
| 4110 | // |
| 4111 | |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 4112 | auto Ty = I.getType(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4113 | auto OpTy = I.getOperand(0)->getType(); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 4114 | auto toI8 = Ty == Type::getInt8Ty(Context); |
| 4115 | auto fromI32 = OpTy == Type::getInt32Ty(Context); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4116 | // Handle zext, sext and uitofp with i1 type specially. |
| 4117 | if ((I.getOpcode() == Instruction::ZExt || |
| 4118 | I.getOpcode() == Instruction::SExt || |
| 4119 | I.getOpcode() == Instruction::UIToFP) && |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4120 | OpTy->isIntOrIntVectorTy(1)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4121 | // |
| 4122 | // Generate OpSelect. |
| 4123 | // |
| 4124 | |
| 4125 | // Ops[0] = Result Type ID |
| 4126 | // Ops[1] = Condition ID |
| 4127 | // Ops[2] = True Constant ID |
| 4128 | // Ops[3] = False Constant ID |
| 4129 | SPIRVOperandList Ops; |
| 4130 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4131 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4132 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4133 | uint32_t CondID = VMap[I.getOperand(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4134 | Ops << MkId(CondID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4135 | |
| 4136 | uint32_t TrueID = 0; |
| 4137 | if (I.getOpcode() == Instruction::ZExt) { |
Kévin Petit | 7bfb899 | 2019-02-26 13:45:08 +0000 | [diff] [blame] | 4138 | TrueID = VMap[ConstantInt::get(I.getType(), 1)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4139 | } else if (I.getOpcode() == Instruction::SExt) { |
Kévin Petit | 7bfb899 | 2019-02-26 13:45:08 +0000 | [diff] [blame] | 4140 | TrueID = VMap[ConstantInt::getSigned(I.getType(), -1)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4141 | } else { |
| 4142 | TrueID = VMap[ConstantFP::get(Context, APFloat(1.0f))]; |
| 4143 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4144 | Ops << MkId(TrueID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4145 | |
| 4146 | uint32_t FalseID = 0; |
| 4147 | if (I.getOpcode() == Instruction::ZExt) { |
| 4148 | FalseID = VMap[Constant::getNullValue(I.getType())]; |
| 4149 | } else if (I.getOpcode() == Instruction::SExt) { |
| 4150 | FalseID = VMap[Constant::getNullValue(I.getType())]; |
| 4151 | } else { |
| 4152 | FalseID = VMap[ConstantFP::get(Context, APFloat(0.0f))]; |
| 4153 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4154 | Ops << MkId(FalseID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4155 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4156 | auto *Inst = new SPIRVInstruction(spv::OpSelect, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4157 | SPIRVInstList.push_back(Inst); |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 4158 | } else if (!clspv::Option::Int8Support() && |
| 4159 | I.getOpcode() == Instruction::Trunc && fromI32 && toI8) { |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 4160 | // The SPIR-V target type is a 32-bit int. Keep only the bottom |
| 4161 | // 8 bits. |
| 4162 | // Before: |
| 4163 | // %result = trunc i32 %a to i8 |
| 4164 | // After |
| 4165 | // %result = OpBitwiseAnd %uint %a %uint_255 |
| 4166 | |
| 4167 | SPIRVOperandList Ops; |
| 4168 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4169 | Ops << MkId(lookupType(OpTy)) << MkId(VMap[I.getOperand(0)]); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 4170 | |
| 4171 | Type *UintTy = Type::getInt32Ty(Context); |
| 4172 | uint32_t MaskID = VMap[ConstantInt::get(UintTy, 255)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4173 | Ops << MkId(MaskID); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 4174 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4175 | auto *Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | d2de94a | 2017-08-28 17:27:47 -0400 | [diff] [blame] | 4176 | SPIRVInstList.push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4177 | } else { |
| 4178 | // Ops[0] = Result Type ID |
| 4179 | // Ops[1] = Source Value ID |
| 4180 | SPIRVOperandList Ops; |
| 4181 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4182 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4183 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4184 | auto *Inst = new SPIRVInstruction(GetSPIRVCastOpcode(I), nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4185 | SPIRVInstList.push_back(Inst); |
| 4186 | } |
| 4187 | } else if (isa<BinaryOperator>(I)) { |
| 4188 | // |
| 4189 | // Generate SPIRV instructions for binary operators. |
| 4190 | // |
| 4191 | |
| 4192 | // Handle xor with i1 type specially. |
| 4193 | if (I.getOpcode() == Instruction::Xor && |
| 4194 | I.getType() == Type::getInt1Ty(Context) && |
Kévin Petit | 24272b6 | 2018-10-18 19:16:12 +0000 | [diff] [blame] | 4195 | ((isa<ConstantInt>(I.getOperand(0)) && |
| 4196 | !cast<ConstantInt>(I.getOperand(0))->isZero()) || |
| 4197 | (isa<ConstantInt>(I.getOperand(1)) && |
| 4198 | !cast<ConstantInt>(I.getOperand(1))->isZero()))) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4199 | // |
| 4200 | // Generate OpLogicalNot. |
| 4201 | // |
| 4202 | // Ops[0] = Result Type ID |
| 4203 | // Ops[1] = Operand |
| 4204 | SPIRVOperandList Ops; |
| 4205 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4206 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4207 | |
| 4208 | Value *CondV = I.getOperand(0); |
| 4209 | if (isa<Constant>(I.getOperand(0))) { |
| 4210 | CondV = I.getOperand(1); |
| 4211 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4212 | Ops << MkId(VMap[CondV]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4213 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4214 | auto *Inst = new SPIRVInstruction(spv::OpLogicalNot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4215 | SPIRVInstList.push_back(Inst); |
| 4216 | } else { |
| 4217 | // Ops[0] = Result Type ID |
| 4218 | // Ops[1] = Operand 0 |
| 4219 | // Ops[2] = Operand 1 |
| 4220 | SPIRVOperandList Ops; |
| 4221 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4222 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]) |
| 4223 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4224 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4225 | auto *Inst = |
| 4226 | new SPIRVInstruction(GetSPIRVBinaryOpcode(I), nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4227 | SPIRVInstList.push_back(Inst); |
| 4228 | } |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 4229 | } else if (I.getOpcode() == Instruction::FNeg) { |
| 4230 | // The only unary operator. |
| 4231 | // |
| 4232 | // Ops[0] = Result Type ID |
| 4233 | // Ops[1] = Operand 0 |
| 4234 | SPIRVOperandList ops; |
| 4235 | |
| 4236 | ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]); |
| 4237 | auto *Inst = new SPIRVInstruction(spv::OpFNegate, nextID++, ops); |
| 4238 | SPIRVInstList.push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4239 | } else { |
| 4240 | I.print(errs()); |
| 4241 | llvm_unreachable("Unsupported instruction???"); |
| 4242 | } |
| 4243 | break; |
| 4244 | } |
| 4245 | case Instruction::GetElementPtr: { |
| 4246 | auto &GlobalConstArgSet = getGlobalConstArgSet(); |
| 4247 | |
| 4248 | // |
| 4249 | // Generate OpAccessChain. |
| 4250 | // |
| 4251 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(&I); |
| 4252 | |
| 4253 | // |
| 4254 | // Generate OpAccessChain. |
| 4255 | // |
| 4256 | |
| 4257 | // Ops[0] = Result Type ID |
| 4258 | // Ops[1] = Base ID |
| 4259 | // Ops[2] ... Ops[n] = Indexes ID |
| 4260 | SPIRVOperandList Ops; |
| 4261 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4262 | PointerType *ResultType = cast<PointerType>(GEP->getType()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4263 | if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate || |
| 4264 | GlobalConstArgSet.count(GEP->getPointerOperand())) { |
| 4265 | // Use pointer type with private address space for global constant. |
| 4266 | Type *EleTy = I.getType()->getPointerElementType(); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4267 | ResultType = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4268 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4269 | |
| 4270 | Ops << MkId(lookupType(ResultType)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4271 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4272 | // Generate the base pointer. |
| 4273 | Ops << MkId(VMap[GEP->getPointerOperand()]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4274 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4275 | // TODO(dneto): Simplify the following? |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4276 | |
| 4277 | // |
| 4278 | // Follows below rules for gep. |
| 4279 | // |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4280 | // 1. If gep's first index is 0 generate OpAccessChain and ignore gep's |
| 4281 | // first index. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4282 | // 2. If gep's first index is not 0, generate OpPtrAccessChain and use gep's |
| 4283 | // first index. |
| 4284 | // 3. If gep's first index is not constant, generate OpPtrAccessChain and |
| 4285 | // use gep's first index. |
| 4286 | // 4. If it is not above case 1, 2 and 3, generate OpAccessChain and use |
| 4287 | // gep's first index. |
| 4288 | // |
| 4289 | spv::Op Opcode = spv::OpAccessChain; |
| 4290 | unsigned offset = 0; |
| 4291 | if (ConstantInt *CstInt = dyn_cast<ConstantInt>(GEP->getOperand(1))) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4292 | if (CstInt->getZExtValue() == 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4293 | offset = 1; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4294 | } else if (CstInt->getZExtValue() != 0) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4295 | Opcode = spv::OpPtrAccessChain; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4296 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4297 | } else { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4298 | Opcode = spv::OpPtrAccessChain; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | if (Opcode == spv::OpPtrAccessChain) { |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4302 | // Do we need to generate ArrayStride? Check against the GEP result type |
| 4303 | // rather than the pointer type of the base because when indexing into |
| 4304 | // an OpenCL program-scope constant, we'll swap out the LLVM base pointer |
| 4305 | // for something else in the SPIR-V. |
| 4306 | // E.g. see test/PointerAccessChain/pointer_index_is_constant_1.cl |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4307 | auto address_space = ResultType->getAddressSpace(); |
| 4308 | setVariablePointersCapabilities(address_space); |
| 4309 | switch (GetStorageClass(address_space)) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 4310 | case spv::StorageClassStorageBuffer: |
| 4311 | case spv::StorageClassUniform: |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4312 | // Save the need to generate an ArrayStride decoration. But defer |
| 4313 | // generation until later, so we only make one decoration. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 4314 | getTypesNeedingArrayStride().insert(ResultType); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 4315 | break; |
| 4316 | default: |
| 4317 | break; |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 4318 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | for (auto II = GEP->idx_begin() + offset; II != GEP->idx_end(); II++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4322 | Ops << MkId(VMap[*II]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4323 | } |
| 4324 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4325 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4326 | SPIRVInstList.push_back(Inst); |
| 4327 | break; |
| 4328 | } |
| 4329 | case Instruction::ExtractValue: { |
| 4330 | ExtractValueInst *EVI = cast<ExtractValueInst>(&I); |
| 4331 | // Ops[0] = Result Type ID |
| 4332 | // Ops[1] = Composite ID |
| 4333 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
| 4334 | SPIRVOperandList Ops; |
| 4335 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4336 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4337 | |
| 4338 | uint32_t CompositeID = VMap[EVI->getAggregateOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4339 | Ops << MkId(CompositeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4340 | |
| 4341 | for (auto &Index : EVI->indices()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4342 | Ops << MkNum(Index); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4343 | } |
| 4344 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4345 | auto *Inst = new SPIRVInstruction(spv::OpCompositeExtract, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4346 | SPIRVInstList.push_back(Inst); |
| 4347 | break; |
| 4348 | } |
| 4349 | case Instruction::InsertValue: { |
| 4350 | InsertValueInst *IVI = cast<InsertValueInst>(&I); |
| 4351 | // Ops[0] = Result Type ID |
| 4352 | // Ops[1] = Object ID |
| 4353 | // Ops[2] = Composite ID |
| 4354 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
| 4355 | SPIRVOperandList Ops; |
| 4356 | |
| 4357 | uint32_t ResTyID = lookupType(I.getType()); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4358 | Ops << MkId(ResTyID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4359 | |
| 4360 | uint32_t ObjectID = VMap[IVI->getInsertedValueOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4361 | Ops << MkId(ObjectID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4362 | |
| 4363 | uint32_t CompositeID = VMap[IVI->getAggregateOperand()]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4364 | Ops << MkId(CompositeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4365 | |
| 4366 | for (auto &Index : IVI->indices()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4367 | Ops << MkNum(Index); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4368 | } |
| 4369 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4370 | auto *Inst = new SPIRVInstruction(spv::OpCompositeInsert, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4371 | SPIRVInstList.push_back(Inst); |
| 4372 | break; |
| 4373 | } |
| 4374 | case Instruction::Select: { |
| 4375 | // |
| 4376 | // Generate OpSelect. |
| 4377 | // |
| 4378 | |
| 4379 | // Ops[0] = Result Type ID |
| 4380 | // Ops[1] = Condition ID |
| 4381 | // Ops[2] = True Constant ID |
| 4382 | // Ops[3] = False Constant ID |
| 4383 | SPIRVOperandList Ops; |
| 4384 | |
| 4385 | // Find SPIRV instruction for parameter type. |
| 4386 | auto Ty = I.getType(); |
| 4387 | if (Ty->isPointerTy()) { |
| 4388 | auto PointeeTy = Ty->getPointerElementType(); |
| 4389 | if (PointeeTy->isStructTy() && |
| 4390 | dyn_cast<StructType>(PointeeTy)->isOpaque()) { |
| 4391 | Ty = PointeeTy; |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4392 | } else { |
| 4393 | // Selecting between pointers requires variable pointers. |
| 4394 | setVariablePointersCapabilities(Ty->getPointerAddressSpace()); |
| 4395 | if (!hasVariablePointers() && !selectFromSameObject(&I)) { |
| 4396 | setVariablePointers(true); |
| 4397 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4401 | Ops << MkId(lookupType(Ty)) << MkId(VMap[I.getOperand(0)]) |
| 4402 | << MkId(VMap[I.getOperand(1)]) << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4403 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4404 | auto *Inst = new SPIRVInstruction(spv::OpSelect, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4405 | SPIRVInstList.push_back(Inst); |
| 4406 | break; |
| 4407 | } |
| 4408 | case Instruction::ExtractElement: { |
| 4409 | // Handle <4 x i8> type manually. |
| 4410 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 4411 | if (is4xi8vec(CompositeTy)) { |
| 4412 | // |
| 4413 | // Generate OpShiftRightLogical and OpBitwiseAnd for extractelement with |
| 4414 | // <4 x i8>. |
| 4415 | // |
| 4416 | |
| 4417 | // |
| 4418 | // Generate OpShiftRightLogical |
| 4419 | // |
| 4420 | // Ops[0] = Result Type ID |
| 4421 | // Ops[1] = Operand 0 |
| 4422 | // Ops[2] = Operand 1 |
| 4423 | // |
| 4424 | SPIRVOperandList Ops; |
| 4425 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4426 | Ops << MkId(lookupType(CompositeTy)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4427 | |
| 4428 | uint32_t Op0ID = VMap[I.getOperand(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4429 | Ops << MkId(Op0ID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4430 | |
| 4431 | uint32_t Op1ID = 0; |
| 4432 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
| 4433 | // Handle constant index. |
| 4434 | uint64_t Idx = CI->getZExtValue(); |
| 4435 | Value *ShiftAmount = |
| 4436 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 4437 | Op1ID = VMap[ShiftAmount]; |
| 4438 | } else { |
| 4439 | // Handle variable index. |
| 4440 | SPIRVOperandList TmpOps; |
| 4441 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4442 | TmpOps << MkId(lookupType(Type::getInt32Ty(Context))) |
| 4443 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4444 | |
| 4445 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4446 | TmpOps << MkId(VMap[Cst8]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4447 | |
| 4448 | Op1ID = nextID; |
| 4449 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4450 | auto *TmpInst = new SPIRVInstruction(spv::OpIMul, nextID++, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4451 | SPIRVInstList.push_back(TmpInst); |
| 4452 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4453 | Ops << MkId(Op1ID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4454 | |
| 4455 | uint32_t ShiftID = nextID; |
| 4456 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4457 | auto *Inst = |
| 4458 | new SPIRVInstruction(spv::OpShiftRightLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4459 | SPIRVInstList.push_back(Inst); |
| 4460 | |
| 4461 | // |
| 4462 | // Generate OpBitwiseAnd |
| 4463 | // |
| 4464 | // Ops[0] = Result Type ID |
| 4465 | // Ops[1] = Operand 0 |
| 4466 | // Ops[2] = Operand 1 |
| 4467 | // |
| 4468 | Ops.clear(); |
| 4469 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4470 | Ops << MkId(lookupType(CompositeTy)) << MkId(ShiftID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4471 | |
| 4472 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4473 | Ops << MkId(VMap[CstFF]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4474 | |
David Neto | 9b2d625 | 2017-09-06 15:47:37 -0400 | [diff] [blame] | 4475 | // Reset mapping for this value to the result of the bitwise and. |
| 4476 | VMap[&I] = nextID; |
| 4477 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4478 | Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4479 | SPIRVInstList.push_back(Inst); |
| 4480 | break; |
| 4481 | } |
| 4482 | |
| 4483 | // Ops[0] = Result Type ID |
| 4484 | // Ops[1] = Composite ID |
| 4485 | // Ops[2] ... Ops[n] = Indexes (Literal Number) |
| 4486 | SPIRVOperandList Ops; |
| 4487 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4488 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4489 | |
| 4490 | spv::Op Opcode = spv::OpCompositeExtract; |
| 4491 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4492 | Ops << MkNum(static_cast<uint32_t>(CI->getZExtValue())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4493 | } else { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4494 | Ops << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4495 | Opcode = spv::OpVectorExtractDynamic; |
| 4496 | } |
| 4497 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4498 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4499 | SPIRVInstList.push_back(Inst); |
| 4500 | break; |
| 4501 | } |
| 4502 | case Instruction::InsertElement: { |
| 4503 | // Handle <4 x i8> type manually. |
| 4504 | Type *CompositeTy = I.getOperand(0)->getType(); |
| 4505 | if (is4xi8vec(CompositeTy)) { |
| 4506 | Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF); |
| 4507 | uint32_t CstFFID = VMap[CstFF]; |
| 4508 | |
| 4509 | uint32_t ShiftAmountID = 0; |
| 4510 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
| 4511 | // Handle constant index. |
| 4512 | uint64_t Idx = CI->getZExtValue(); |
| 4513 | Value *ShiftAmount = |
| 4514 | ConstantInt::get(Type::getInt32Ty(Context), Idx * 8); |
| 4515 | ShiftAmountID = VMap[ShiftAmount]; |
| 4516 | } else { |
| 4517 | // Handle variable index. |
| 4518 | SPIRVOperandList TmpOps; |
| 4519 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4520 | TmpOps << MkId(lookupType(Type::getInt32Ty(Context))) |
| 4521 | << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4522 | |
| 4523 | ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4524 | TmpOps << MkId(VMap[Cst8]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4525 | |
| 4526 | ShiftAmountID = nextID; |
| 4527 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4528 | auto *TmpInst = new SPIRVInstruction(spv::OpIMul, nextID++, TmpOps); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4529 | SPIRVInstList.push_back(TmpInst); |
| 4530 | } |
| 4531 | |
| 4532 | // |
| 4533 | // Generate mask operations. |
| 4534 | // |
| 4535 | |
| 4536 | // ShiftLeft mask according to index of insertelement. |
| 4537 | SPIRVOperandList Ops; |
| 4538 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4539 | const uint32_t ResTyID = lookupType(CompositeTy); |
| 4540 | Ops << MkId(ResTyID) << MkId(CstFFID) << MkId(ShiftAmountID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4541 | |
| 4542 | uint32_t MaskID = nextID; |
| 4543 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4544 | auto *Inst = new SPIRVInstruction(spv::OpShiftLeftLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4545 | SPIRVInstList.push_back(Inst); |
| 4546 | |
| 4547 | // Inverse mask. |
| 4548 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4549 | Ops << MkId(ResTyID) << MkId(MaskID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4550 | |
| 4551 | uint32_t InvMaskID = nextID; |
| 4552 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4553 | Inst = new SPIRVInstruction(spv::OpNot, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4554 | SPIRVInstList.push_back(Inst); |
| 4555 | |
| 4556 | // Apply mask. |
| 4557 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4558 | Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(0)]) << MkId(InvMaskID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4559 | |
| 4560 | uint32_t OrgValID = nextID; |
| 4561 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4562 | Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4563 | SPIRVInstList.push_back(Inst); |
| 4564 | |
| 4565 | // Create correct value according to index of insertelement. |
| 4566 | Ops.clear(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4567 | Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(1)]) |
| 4568 | << MkId(ShiftAmountID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4569 | |
| 4570 | uint32_t InsertValID = nextID; |
| 4571 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4572 | Inst = new SPIRVInstruction(spv::OpShiftLeftLogical, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4573 | SPIRVInstList.push_back(Inst); |
| 4574 | |
| 4575 | // Insert value to original value. |
| 4576 | Ops.clear(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4577 | Ops << MkId(ResTyID) << MkId(OrgValID) << MkId(InsertValID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4578 | |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 4579 | VMap[&I] = nextID; |
| 4580 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4581 | Inst = new SPIRVInstruction(spv::OpBitwiseOr, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4582 | SPIRVInstList.push_back(Inst); |
| 4583 | |
| 4584 | break; |
| 4585 | } |
| 4586 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4587 | SPIRVOperandList Ops; |
| 4588 | |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4589 | // Ops[0] = Result Type ID |
| 4590 | Ops << MkId(lookupType(I.getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4591 | |
| 4592 | spv::Op Opcode = spv::OpCompositeInsert; |
| 4593 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4594 | const auto value = CI->getZExtValue(); |
| 4595 | assert(value <= UINT32_MAX); |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4596 | // Ops[1] = Object ID |
| 4597 | // Ops[2] = Composite ID |
| 4598 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4599 | Ops << MkId(VMap[I.getOperand(1)]) << MkId(VMap[I.getOperand(0)]) |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4600 | << MkNum(static_cast<uint32_t>(value)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4601 | } else { |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4602 | // Ops[1] = Composite ID |
| 4603 | // Ops[2] = Object ID |
| 4604 | // Ops[3] ... Ops[n] = Indexes (Literal Number) |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4605 | Ops << MkId(VMap[I.getOperand(0)]) << MkId(VMap[I.getOperand(1)]) |
James Price | d26efea | 2018-06-09 23:28:32 +0100 | [diff] [blame] | 4606 | << MkId(VMap[I.getOperand(2)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4607 | Opcode = spv::OpVectorInsertDynamic; |
| 4608 | } |
| 4609 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4610 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4611 | SPIRVInstList.push_back(Inst); |
| 4612 | break; |
| 4613 | } |
| 4614 | case Instruction::ShuffleVector: { |
| 4615 | // Ops[0] = Result Type ID |
| 4616 | // Ops[1] = Vector 1 ID |
| 4617 | // Ops[2] = Vector 2 ID |
| 4618 | // Ops[3] ... Ops[n] = Components (Literal Number) |
| 4619 | SPIRVOperandList Ops; |
| 4620 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4621 | Ops << MkId(lookupType(I.getType())) << MkId(VMap[I.getOperand(0)]) |
| 4622 | << MkId(VMap[I.getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4623 | |
alan-baker | c966671 | 2020-04-01 16:31:21 -0400 | [diff] [blame] | 4624 | auto shuffle = cast<ShuffleVectorInst>(&I); |
| 4625 | SmallVector<int, 4> mask; |
| 4626 | shuffle->getShuffleMask(mask); |
| 4627 | for (auto i : mask) { |
| 4628 | if (i == UndefMaskElem) { |
| 4629 | if (clspv::Option::HackUndef()) |
| 4630 | // Use 0 instead of undef. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4631 | Ops << MkNum(0); |
alan-baker | c966671 | 2020-04-01 16:31:21 -0400 | [diff] [blame] | 4632 | else |
| 4633 | // Undef for shuffle in SPIR-V. |
| 4634 | Ops << MkNum(0xffffffff); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4635 | } else { |
alan-baker | c966671 | 2020-04-01 16:31:21 -0400 | [diff] [blame] | 4636 | Ops << MkNum(i); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4637 | } |
| 4638 | } |
| 4639 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4640 | auto *Inst = new SPIRVInstruction(spv::OpVectorShuffle, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4641 | SPIRVInstList.push_back(Inst); |
| 4642 | break; |
| 4643 | } |
| 4644 | case Instruction::ICmp: |
| 4645 | case Instruction::FCmp: { |
| 4646 | CmpInst *CmpI = cast<CmpInst>(&I); |
| 4647 | |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4648 | // Pointer equality is invalid. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4649 | Type *ArgTy = CmpI->getOperand(0)->getType(); |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4650 | if (isa<PointerType>(ArgTy)) { |
| 4651 | CmpI->print(errs()); |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 4652 | std::string name = I.getParent()->getParent()->getName().str(); |
David Neto | d4ca2e6 | 2017-07-06 18:47:35 -0400 | [diff] [blame] | 4653 | errs() |
| 4654 | << "\nPointer equality test is not supported by SPIR-V for Vulkan, " |
| 4655 | << "in function " << name << "\n"; |
| 4656 | llvm_unreachable("Pointer equality check is invalid"); |
| 4657 | break; |
| 4658 | } |
| 4659 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4660 | // Ops[0] = Result Type ID |
| 4661 | // Ops[1] = Operand 1 ID |
| 4662 | // Ops[2] = Operand 2 ID |
| 4663 | SPIRVOperandList Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4664 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4665 | Ops << MkId(lookupType(CmpI->getType())) << MkId(VMap[CmpI->getOperand(0)]) |
| 4666 | << MkId(VMap[CmpI->getOperand(1)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4667 | |
| 4668 | spv::Op Opcode = GetSPIRVCmpOpcode(CmpI); |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4669 | auto *Inst = new SPIRVInstruction(Opcode, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4670 | SPIRVInstList.push_back(Inst); |
| 4671 | break; |
| 4672 | } |
| 4673 | case Instruction::Br: { |
| 4674 | // Branch instrucion is deferred because it needs label's ID. Record slot's |
| 4675 | // location on SPIRVInstructionList. |
| 4676 | DeferredInsts.push_back( |
| 4677 | std::make_tuple(&I, --SPIRVInstList.end(), 0 /* No id */)); |
| 4678 | break; |
| 4679 | } |
| 4680 | case Instruction::Switch: { |
| 4681 | I.print(errs()); |
| 4682 | llvm_unreachable("Unsupported instruction???"); |
| 4683 | break; |
| 4684 | } |
| 4685 | case Instruction::IndirectBr: { |
| 4686 | I.print(errs()); |
| 4687 | llvm_unreachable("Unsupported instruction???"); |
| 4688 | break; |
| 4689 | } |
| 4690 | case Instruction::PHI: { |
| 4691 | // Branch instrucion is deferred because it needs label's ID. Record slot's |
| 4692 | // location on SPIRVInstructionList. |
| 4693 | DeferredInsts.push_back( |
| 4694 | std::make_tuple(&I, --SPIRVInstList.end(), nextID++)); |
| 4695 | break; |
| 4696 | } |
| 4697 | case Instruction::Alloca: { |
| 4698 | // |
| 4699 | // Generate OpVariable. |
| 4700 | // |
| 4701 | // Ops[0] : Result Type ID |
| 4702 | // Ops[1] : Storage Class |
| 4703 | SPIRVOperandList Ops; |
| 4704 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4705 | Ops << MkId(lookupType(I.getType())) << MkNum(spv::StorageClassFunction); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4706 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4707 | auto *Inst = new SPIRVInstruction(spv::OpVariable, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4708 | SPIRVInstList.push_back(Inst); |
| 4709 | break; |
| 4710 | } |
| 4711 | case Instruction::Load: { |
| 4712 | LoadInst *LD = cast<LoadInst>(&I); |
| 4713 | // |
| 4714 | // Generate OpLoad. |
| 4715 | // |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 4716 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4717 | if (LD->getType()->isPointerTy()) { |
| 4718 | // Loading a pointer requires variable pointers. |
| 4719 | setVariablePointersCapabilities(LD->getType()->getPointerAddressSpace()); |
| 4720 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4721 | |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4722 | uint32_t ResTyID = lookupType(LD->getType()); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4723 | uint32_t PointerID = VMap[LD->getPointerOperand()]; |
| 4724 | |
| 4725 | // This is a hack to work around what looks like a driver bug. |
| 4726 | // When we're loading from the special variable holding the WorkgroupSize |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4727 | // builtin value, use an OpBitWiseAnd of the value's ID rather than |
| 4728 | // generating a load. |
David Neto | 66cfe64 | 2018-03-24 06:13:56 -0700 | [diff] [blame] | 4729 | // TODO(dneto): Remove this awful hack once drivers are fixed. |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4730 | if (PointerID == WorkgroupSizeVarID) { |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4731 | // Generate a bitwise-and of the original value with itself. |
| 4732 | // We should have been able to get away with just an OpCopyObject, |
| 4733 | // but we need something more complex to get past certain driver bugs. |
| 4734 | // This is ridiculous, but necessary. |
| 4735 | // TODO(dneto): Revisit this once drivers fix their bugs. |
| 4736 | |
| 4737 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4738 | Ops << MkId(ResTyID) << MkId(WorkgroupSizeValueID) |
| 4739 | << MkId(WorkgroupSizeValueID); |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4740 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4741 | auto *Inst = new SPIRVInstruction(spv::OpBitwiseAnd, nextID++, Ops); |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4742 | SPIRVInstList.push_back(Inst); |
David Neto | a60b00b | 2017-09-15 16:34:09 -0400 | [diff] [blame] | 4743 | break; |
| 4744 | } |
| 4745 | |
| 4746 | // This is the normal path. Generate a load. |
| 4747 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4748 | // Ops[0] = Result Type ID |
| 4749 | // Ops[1] = Pointer ID |
| 4750 | // Ops[2] ... Ops[n] = Optional Memory Access |
| 4751 | // |
| 4752 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 4753 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4754 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4755 | Ops << MkId(ResTyID) << MkId(PointerID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4756 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4757 | auto *Inst = new SPIRVInstruction(spv::OpLoad, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4758 | SPIRVInstList.push_back(Inst); |
| 4759 | break; |
| 4760 | } |
| 4761 | case Instruction::Store: { |
| 4762 | StoreInst *ST = cast<StoreInst>(&I); |
| 4763 | // |
| 4764 | // Generate OpStore. |
| 4765 | // |
| 4766 | |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 4767 | if (ST->getValueOperand()->getType()->isPointerTy()) { |
| 4768 | // Storing a pointer requires variable pointers. |
| 4769 | setVariablePointersCapabilities( |
| 4770 | ST->getValueOperand()->getType()->getPointerAddressSpace()); |
| 4771 | } |
| 4772 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4773 | // Ops[0] = Pointer ID |
| 4774 | // Ops[1] = Object ID |
| 4775 | // Ops[2] ... Ops[n] = Optional Memory Access (later???) |
| 4776 | // |
| 4777 | // TODO: Do we need to implement Optional Memory Access??? |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4778 | SPIRVOperandList Ops; |
| 4779 | Ops << MkId(VMap[ST->getPointerOperand()]) |
| 4780 | << MkId(VMap[ST->getValueOperand()]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4781 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4782 | auto *Inst = new SPIRVInstruction(spv::OpStore, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4783 | SPIRVInstList.push_back(Inst); |
| 4784 | break; |
| 4785 | } |
| 4786 | case Instruction::AtomicCmpXchg: { |
| 4787 | I.print(errs()); |
| 4788 | llvm_unreachable("Unsupported instruction???"); |
| 4789 | break; |
| 4790 | } |
| 4791 | case Instruction::AtomicRMW: { |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4792 | AtomicRMWInst *AtomicRMW = dyn_cast<AtomicRMWInst>(&I); |
| 4793 | |
| 4794 | spv::Op opcode; |
| 4795 | |
| 4796 | switch (AtomicRMW->getOperation()) { |
| 4797 | default: |
| 4798 | I.print(errs()); |
| 4799 | llvm_unreachable("Unsupported instruction???"); |
| 4800 | case llvm::AtomicRMWInst::Add: |
| 4801 | opcode = spv::OpAtomicIAdd; |
| 4802 | break; |
| 4803 | case llvm::AtomicRMWInst::Sub: |
| 4804 | opcode = spv::OpAtomicISub; |
| 4805 | break; |
| 4806 | case llvm::AtomicRMWInst::Xchg: |
| 4807 | opcode = spv::OpAtomicExchange; |
| 4808 | break; |
| 4809 | case llvm::AtomicRMWInst::Min: |
| 4810 | opcode = spv::OpAtomicSMin; |
| 4811 | break; |
| 4812 | case llvm::AtomicRMWInst::Max: |
| 4813 | opcode = spv::OpAtomicSMax; |
| 4814 | break; |
| 4815 | case llvm::AtomicRMWInst::UMin: |
| 4816 | opcode = spv::OpAtomicUMin; |
| 4817 | break; |
| 4818 | case llvm::AtomicRMWInst::UMax: |
| 4819 | opcode = spv::OpAtomicUMax; |
| 4820 | break; |
| 4821 | case llvm::AtomicRMWInst::And: |
| 4822 | opcode = spv::OpAtomicAnd; |
| 4823 | break; |
| 4824 | case llvm::AtomicRMWInst::Or: |
| 4825 | opcode = spv::OpAtomicOr; |
| 4826 | break; |
| 4827 | case llvm::AtomicRMWInst::Xor: |
| 4828 | opcode = spv::OpAtomicXor; |
| 4829 | break; |
| 4830 | } |
| 4831 | |
| 4832 | // |
| 4833 | // Generate OpAtomic*. |
| 4834 | // |
| 4835 | SPIRVOperandList Ops; |
| 4836 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4837 | Ops << MkId(lookupType(I.getType())) |
| 4838 | << MkId(VMap[AtomicRMW->getPointerOperand()]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4839 | |
| 4840 | auto IntTy = Type::getInt32Ty(I.getContext()); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4841 | const auto ConstantScopeDevice = ConstantInt::get(IntTy, spv::ScopeDevice); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4842 | Ops << MkId(VMap[ConstantScopeDevice]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4843 | |
| 4844 | const auto ConstantMemorySemantics = ConstantInt::get( |
| 4845 | IntTy, spv::MemorySemanticsUniformMemoryMask | |
| 4846 | spv::MemorySemanticsSequentiallyConsistentMask); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4847 | Ops << MkId(VMap[ConstantMemorySemantics]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4848 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4849 | Ops << MkId(VMap[AtomicRMW->getValOperand()]); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4850 | |
| 4851 | VMap[&I] = nextID; |
| 4852 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4853 | auto *Inst = new SPIRVInstruction(opcode, nextID++, Ops); |
Neil Henning | 3967210 | 2017-09-29 14:33:13 +0100 | [diff] [blame] | 4854 | SPIRVInstList.push_back(Inst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4855 | break; |
| 4856 | } |
| 4857 | case Instruction::Fence: { |
| 4858 | I.print(errs()); |
| 4859 | llvm_unreachable("Unsupported instruction???"); |
| 4860 | break; |
| 4861 | } |
| 4862 | case Instruction::Call: { |
| 4863 | CallInst *Call = dyn_cast<CallInst>(&I); |
| 4864 | Function *Callee = Call->getCalledFunction(); |
| 4865 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4866 | if (Callee->getName().startswith(clspv::ResourceAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4867 | if (ResourceVarDeferredLoadCalls.count(Call) && Call->hasNUsesOrMore(1)) { |
| 4868 | // Generate an OpLoad |
| 4869 | SPIRVOperandList Ops; |
| 4870 | const auto load_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4871 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4872 | Ops << MkId(lookupType(Call->getType()->getPointerElementType())) |
| 4873 | << MkId(ResourceVarDeferredLoadCalls[Call]); |
| 4874 | |
| 4875 | auto *Inst = new SPIRVInstruction(spv::OpLoad, load_id, Ops); |
| 4876 | SPIRVInstList.push_back(Inst); |
| 4877 | VMap[Call] = load_id; |
| 4878 | break; |
| 4879 | |
| 4880 | } else { |
| 4881 | // This maps to an OpVariable we've already generated. |
| 4882 | // No code is generated for the call. |
| 4883 | } |
| 4884 | break; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4885 | } else if (Callee->getName().startswith( |
| 4886 | clspv::WorkgroupAccessorFunction())) { |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4887 | // Don't codegen an instruction here, but instead map this call directly |
| 4888 | // to the workgroup variable id. |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 4889 | int spec_id = static_cast<int>( |
| 4890 | cast<ConstantInt>(Call->getOperand(0))->getSExtValue()); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 4891 | const auto &info = LocalSpecIdInfoMap[spec_id]; |
| 4892 | VMap[Call] = info.variable_id; |
| 4893 | break; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | // Sampler initializers become a load of the corresponding sampler. |
| 4897 | |
Kévin Petit | df71de3 | 2019-04-09 14:09:50 +0100 | [diff] [blame] | 4898 | if (Callee->getName().equals(clspv::LiteralSamplerFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4899 | // Map this to a load from the variable. |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 4900 | const auto third_param = static_cast<unsigned>( |
| 4901 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue()); |
| 4902 | auto sampler_value = third_param; |
| 4903 | if (clspv::Option::UseSamplerMap()) { |
| 4904 | sampler_value = getSamplerMap()[third_param].first; |
| 4905 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4906 | |
| 4907 | // Generate an OpLoad |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4908 | SPIRVOperandList Ops; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4909 | const auto load_id = nextID++; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4910 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4911 | Ops << MkId(lookupType(SamplerTy->getPointerElementType())) |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 4912 | << MkId(SamplerLiteralToIDMap[sampler_value]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4913 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4914 | auto *Inst = new SPIRVInstruction(spv::OpLoad, load_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4915 | SPIRVInstList.push_back(Inst); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 4916 | VMap[Call] = load_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4917 | break; |
| 4918 | } |
| 4919 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 4920 | // Handle SPIR-V intrinsics |
Kévin Petit | 9b34026 | 2019-06-19 18:31:11 +0100 | [diff] [blame] | 4921 | spv::Op opcode = StringSwitch<spv::Op>(Callee->getName()) |
| 4922 | .Case("spirv.atomic_xor", spv::OpAtomicXor) |
| 4923 | .Default(spv::OpNop); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4924 | |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 4925 | // If the switch above didn't have an entry maybe the intrinsic |
| 4926 | // is using the name mangling logic. |
| 4927 | bool usesMangler = false; |
| 4928 | if (opcode == spv::OpNop) { |
| 4929 | if (Callee->getName().startswith(clspv::SPIRVOpIntrinsicFunction())) { |
| 4930 | auto OpCst = cast<ConstantInt>(Call->getOperand(0)); |
| 4931 | opcode = static_cast<spv::Op>(OpCst->getZExtValue()); |
| 4932 | usesMangler = true; |
| 4933 | } |
| 4934 | } |
| 4935 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 4936 | if (opcode != spv::OpNop) { |
| 4937 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4938 | SPIRVOperandList Ops; |
| 4939 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 4940 | if (!I.getType()->isVoidTy()) { |
| 4941 | Ops << MkId(lookupType(I.getType())); |
| 4942 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4943 | |
Kévin Petit | 617a76d | 2019-04-04 13:54:16 +0100 | [diff] [blame] | 4944 | unsigned firstOperand = usesMangler ? 1 : 0; |
| 4945 | for (unsigned i = firstOperand; i < Call->getNumArgOperands(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4946 | Ops << MkId(VMap[Call->getArgOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4947 | } |
| 4948 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 4949 | if (!I.getType()->isVoidTy()) { |
| 4950 | VMap[&I] = nextID; |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Kévin Petit | 349c950 | 2019-03-28 17:24:14 +0000 | [diff] [blame] | 4953 | SPIRVInstruction *Inst; |
| 4954 | if (!I.getType()->isVoidTy()) { |
| 4955 | Inst = new SPIRVInstruction(opcode, nextID++, Ops); |
| 4956 | } else { |
| 4957 | Inst = new SPIRVInstruction(opcode, Ops); |
| 4958 | } |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 4959 | SPIRVInstList.push_back(Inst); |
| 4960 | break; |
| 4961 | } |
| 4962 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4963 | // spirv.copy_memory.* intrinsics become OpMemoryMemory's. |
| 4964 | if (Callee->getName().startswith("spirv.copy_memory")) { |
| 4965 | // |
| 4966 | // Generate OpCopyMemory. |
| 4967 | // |
| 4968 | |
| 4969 | // Ops[0] = Dst ID |
| 4970 | // Ops[1] = Src ID |
| 4971 | // Ops[2] = Memory Access |
| 4972 | // Ops[3] = Alignment |
| 4973 | |
| 4974 | auto IsVolatile = |
| 4975 | dyn_cast<ConstantInt>(Call->getArgOperand(3))->getZExtValue() != 0; |
| 4976 | |
| 4977 | auto VolatileMemoryAccess = (IsVolatile) ? spv::MemoryAccessVolatileMask |
| 4978 | : spv::MemoryAccessMaskNone; |
| 4979 | |
| 4980 | auto MemoryAccess = VolatileMemoryAccess | spv::MemoryAccessAlignedMask; |
| 4981 | |
| 4982 | auto Alignment = |
| 4983 | dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue(); |
| 4984 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 4985 | SPIRVOperandList Ops; |
| 4986 | Ops << MkId(VMap[Call->getArgOperand(0)]) |
| 4987 | << MkId(VMap[Call->getArgOperand(1)]) << MkNum(MemoryAccess) |
| 4988 | << MkNum(static_cast<uint32_t>(Alignment)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4989 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 4990 | auto *Inst = new SPIRVInstruction(spv::OpCopyMemory, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 4991 | |
| 4992 | SPIRVInstList.push_back(Inst); |
| 4993 | |
| 4994 | break; |
| 4995 | } |
| 4996 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 4997 | // read_image is converted to OpSampledImage and OpImageSampleExplicitLod. |
| 4998 | // Additionally, OpTypeSampledImage is generated. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 4999 | if (IsSampledImageRead(Callee)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5000 | // |
| 5001 | // Generate OpSampledImage. |
| 5002 | // |
| 5003 | // Ops[0] = Result Type ID |
| 5004 | // Ops[1] = Image ID |
| 5005 | // Ops[2] = Sampler ID |
| 5006 | // |
| 5007 | SPIRVOperandList Ops; |
| 5008 | |
| 5009 | Value *Image = Call->getArgOperand(0); |
| 5010 | Value *Sampler = Call->getArgOperand(1); |
| 5011 | Value *Coordinate = Call->getArgOperand(2); |
| 5012 | |
| 5013 | TypeMapType &OpImageTypeMap = getImageTypeMap(); |
| 5014 | Type *ImageTy = Image->getType()->getPointerElementType(); |
| 5015 | uint32_t ImageTyID = OpImageTypeMap[ImageTy]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5016 | uint32_t ImageID = VMap[Image]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5017 | uint32_t SamplerID = VMap[Sampler]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5018 | |
| 5019 | Ops << MkId(ImageTyID) << MkId(ImageID) << MkId(SamplerID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5020 | |
| 5021 | uint32_t SampledImageID = nextID; |
| 5022 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5023 | auto *Inst = new SPIRVInstruction(spv::OpSampledImage, nextID++, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5024 | SPIRVInstList.push_back(Inst); |
| 5025 | |
| 5026 | // |
| 5027 | // Generate OpImageSampleExplicitLod. |
| 5028 | // |
| 5029 | // Ops[0] = Result Type ID |
| 5030 | // Ops[1] = Sampled Image ID |
| 5031 | // Ops[2] = Coordinate ID |
| 5032 | // Ops[3] = Image Operands Type ID |
| 5033 | // Ops[4] ... Ops[n] = Operands ID |
| 5034 | // |
| 5035 | Ops.clear(); |
| 5036 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 5037 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 5038 | uint32_t result_type = 0; |
| 5039 | if (is_int_image) { |
| 5040 | result_type = v4int32ID; |
| 5041 | } else { |
| 5042 | result_type = lookupType(Call->getType()); |
| 5043 | } |
| 5044 | |
| 5045 | Ops << MkId(result_type) << MkId(SampledImageID) << MkId(VMap[Coordinate]) |
| 5046 | << MkNum(spv::ImageOperandsLodMask); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5047 | |
| 5048 | Constant *CstFP0 = ConstantFP::get(Context, APFloat(0.0f)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5049 | Ops << MkId(VMap[CstFP0]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5050 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 5051 | uint32_t final_id = nextID++; |
| 5052 | VMap[&I] = final_id; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5053 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 5054 | uint32_t image_id = final_id; |
| 5055 | if (is_int_image) { |
| 5056 | // Int image requires a bitcast from v4int to v4uint. |
| 5057 | image_id = nextID++; |
| 5058 | } |
| 5059 | |
| 5060 | Inst = new SPIRVInstruction(spv::OpImageSampleExplicitLod, image_id, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5061 | SPIRVInstList.push_back(Inst); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 5062 | |
| 5063 | if (is_int_image) { |
| 5064 | // Generate the bitcast. |
| 5065 | Ops.clear(); |
| 5066 | Ops << MkId(lookupType(Call->getType())) << MkId(image_id); |
| 5067 | Inst = new SPIRVInstruction(spv::OpBitcast, final_id, Ops); |
| 5068 | SPIRVInstList.push_back(Inst); |
| 5069 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5070 | break; |
| 5071 | } |
| 5072 | |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 5073 | // read_image (without a sampler) is mapped to OpImageFetch. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 5074 | if (IsUnsampledImageRead(Callee)) { |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 5075 | Value *Image = Call->getArgOperand(0); |
| 5076 | Value *Coordinate = Call->getArgOperand(1); |
| 5077 | |
| 5078 | // |
| 5079 | // Generate OpImageFetch |
| 5080 | // |
| 5081 | // Ops[0] = Result Type ID |
| 5082 | // Ops[1] = Image ID |
| 5083 | // Ops[2] = Coordinate ID |
| 5084 | // Ops[3] = Lod |
| 5085 | // Ops[4] = 0 |
| 5086 | // |
| 5087 | SPIRVOperandList Ops; |
| 5088 | |
| 5089 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 5090 | uint32_t result_type = 0; |
| 5091 | if (is_int_image) { |
| 5092 | result_type = v4int32ID; |
| 5093 | } else { |
| 5094 | result_type = lookupType(Call->getType()); |
| 5095 | } |
| 5096 | |
| 5097 | Ops << MkId(result_type) << MkId(VMap[Image]) << MkId(VMap[Coordinate]) |
| 5098 | << MkNum(spv::ImageOperandsLodMask); |
| 5099 | |
| 5100 | Constant *CstInt0 = ConstantInt::get(Context, APInt(32, 0)); |
| 5101 | Ops << MkId(VMap[CstInt0]); |
| 5102 | |
| 5103 | uint32_t final_id = nextID++; |
| 5104 | VMap[&I] = final_id; |
| 5105 | |
| 5106 | uint32_t image_id = final_id; |
| 5107 | if (is_int_image) { |
| 5108 | // Int image requires a bitcast from v4int to v4uint. |
| 5109 | image_id = nextID++; |
| 5110 | } |
| 5111 | |
| 5112 | auto *Inst = new SPIRVInstruction(spv::OpImageFetch, image_id, Ops); |
| 5113 | SPIRVInstList.push_back(Inst); |
| 5114 | |
| 5115 | if (is_int_image) { |
| 5116 | // Generate the bitcast. |
| 5117 | Ops.clear(); |
| 5118 | Ops << MkId(lookupType(Call->getType())) << MkId(image_id); |
| 5119 | Inst = new SPIRVInstruction(spv::OpBitcast, final_id, Ops); |
| 5120 | SPIRVInstList.push_back(Inst); |
| 5121 | } |
| 5122 | break; |
| 5123 | } |
| 5124 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 5125 | // write_image is mapped to OpImageWrite. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 5126 | if (IsImageWrite(Callee)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5127 | // |
| 5128 | // Generate OpImageWrite. |
| 5129 | // |
| 5130 | // Ops[0] = Image ID |
| 5131 | // Ops[1] = Coordinate ID |
| 5132 | // Ops[2] = Texel ID |
| 5133 | // Ops[3] = (Optional) Image Operands Type (Literal Number) |
| 5134 | // Ops[4] ... Ops[n] = (Optional) Operands ID |
| 5135 | // |
| 5136 | SPIRVOperandList Ops; |
| 5137 | |
| 5138 | Value *Image = Call->getArgOperand(0); |
| 5139 | Value *Coordinate = Call->getArgOperand(1); |
| 5140 | Value *Texel = Call->getArgOperand(2); |
| 5141 | |
| 5142 | uint32_t ImageID = VMap[Image]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5143 | uint32_t CoordinateID = VMap[Coordinate]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5144 | uint32_t TexelID = VMap[Texel]; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 5145 | |
| 5146 | const bool is_int_image = IsIntImageType(Image->getType()); |
| 5147 | if (is_int_image) { |
| 5148 | // Generate a bitcast to v4int and use it as the texel value. |
| 5149 | uint32_t castID = nextID++; |
| 5150 | Ops << MkId(v4int32ID) << MkId(TexelID); |
| 5151 | auto cast = new SPIRVInstruction(spv::OpBitcast, castID, Ops); |
| 5152 | SPIRVInstList.push_back(cast); |
| 5153 | Ops.clear(); |
| 5154 | TexelID = castID; |
| 5155 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5156 | Ops << MkId(ImageID) << MkId(CoordinateID) << MkId(TexelID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5157 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5158 | auto *Inst = new SPIRVInstruction(spv::OpImageWrite, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5159 | SPIRVInstList.push_back(Inst); |
| 5160 | break; |
| 5161 | } |
| 5162 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5163 | // get_image_* is mapped to OpImageQuerySize or OpImageQuerySizeLod |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 5164 | if (IsImageQuery(Callee)) { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5165 | // |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5166 | // Generate OpImageQuerySize[Lod] |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5167 | // |
| 5168 | // Ops[0] = Image ID |
| 5169 | // |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5170 | // Result type has components equal to the dimensionality of the image, |
| 5171 | // plus 1 if the image is arrayed. |
| 5172 | // |
alan-baker | f906d2b | 2019-12-10 11:26:23 -0500 | [diff] [blame] | 5173 | // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0] |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5174 | SPIRVOperandList Ops; |
| 5175 | |
| 5176 | // Implement: |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5177 | // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0] |
| 5178 | uint32_t SizesTypeID = 0; |
| 5179 | |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5180 | Value *Image = Call->getArgOperand(0); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5181 | const uint32_t dim = ImageDimensionality(Image->getType()); |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 5182 | const uint32_t components = |
| 5183 | dim + (IsArrayImageType(Image->getType()) ? 1 : 0); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5184 | if (components == 1) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5185 | SizesTypeID = TypeMap[Type::getInt32Ty(Context)]; |
| 5186 | } else { |
alan-baker | 7150a1d | 2020-02-25 08:31:06 -0500 | [diff] [blame] | 5187 | SizesTypeID = |
| 5188 | TypeMap[VectorType::get(Type::getInt32Ty(Context), components)]; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5189 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5190 | uint32_t ImageID = VMap[Image]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5191 | Ops << MkId(SizesTypeID) << MkId(ImageID); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5192 | spv::Op query_opcode = spv::OpImageQuerySize; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 5193 | if (IsSampledImageType(Image->getType())) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5194 | query_opcode = spv::OpImageQuerySizeLod; |
| 5195 | // Need explicit 0 for Lod operand. |
| 5196 | Constant *CstInt0 = ConstantInt::get(Context, APInt(32, 0)); |
| 5197 | Ops << MkId(VMap[CstInt0]); |
| 5198 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5199 | |
| 5200 | uint32_t SizesID = nextID++; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5201 | auto *QueryInst = new SPIRVInstruction(query_opcode, SizesID, Ops); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5202 | SPIRVInstList.push_back(QueryInst); |
| 5203 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5204 | // May require an extra instruction to create the appropriate result of |
| 5205 | // the builtin function. |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 5206 | if (IsGetImageDim(Callee)) { |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5207 | if (dim == 3) { |
| 5208 | // get_image_dim returns an int4 for 3D images. |
| 5209 | // |
| 5210 | // Reset value map entry since we generated an intermediate |
| 5211 | // instruction. |
| 5212 | VMap[&I] = nextID; |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5213 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5214 | // Implement: |
| 5215 | // %result = OpCompositeConstruct %uint4 %sizes %uint_0 |
| 5216 | Ops.clear(); |
| 5217 | Ops << MkId(lookupType(VectorType::get(Type::getInt32Ty(Context), 4))) |
| 5218 | << MkId(SizesID); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5219 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5220 | Constant *CstInt0 = ConstantInt::get(Context, APInt(32, 0)); |
| 5221 | Ops << MkId(VMap[CstInt0]); |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5222 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 5223 | auto *Inst = |
| 5224 | new SPIRVInstruction(spv::OpCompositeConstruct, nextID++, Ops); |
| 5225 | SPIRVInstList.push_back(Inst); |
| 5226 | } else if (dim != components) { |
| 5227 | // get_image_dim return an int2 regardless of the arrayedness of the |
| 5228 | // image. If the image is arrayed an element must be dropped from the |
| 5229 | // query result. |
| 5230 | // |
| 5231 | // Reset value map entry since we generated an intermediate |
| 5232 | // instruction. |
| 5233 | VMap[&I] = nextID; |
| 5234 | |
| 5235 | // Implement: |
| 5236 | // %result = OpVectorShuffle %uint2 %sizes %sizes 0 1 |
| 5237 | Ops.clear(); |
| 5238 | Ops << MkId(lookupType(VectorType::get(Type::getInt32Ty(Context), 2))) |
| 5239 | << MkId(SizesID) << MkId(SizesID) << MkNum(0) << MkNum(1); |
| 5240 | |
| 5241 | auto *Inst = |
| 5242 | new SPIRVInstruction(spv::OpVectorShuffle, nextID++, Ops); |
| 5243 | SPIRVInstList.push_back(Inst); |
| 5244 | } |
| 5245 | } else if (components > 1) { |
| 5246 | // Reset value map entry since we generated an intermediate instruction. |
| 5247 | VMap[&I] = nextID; |
| 5248 | |
| 5249 | // Implement: |
| 5250 | // %result = OpCompositeExtract %uint %sizes <component number> |
| 5251 | Ops.clear(); |
| 5252 | Ops << MkId(TypeMap[I.getType()]) << MkId(SizesID); |
| 5253 | |
| 5254 | uint32_t component = 0; |
| 5255 | if (IsGetImageHeight(Callee)) |
| 5256 | component = 1; |
| 5257 | else if (IsGetImageDepth(Callee)) |
| 5258 | component = 2; |
| 5259 | Ops << MkNum(component); |
| 5260 | |
| 5261 | auto *Inst = |
| 5262 | new SPIRVInstruction(spv::OpCompositeExtract, nextID++, Ops); |
| 5263 | SPIRVInstList.push_back(Inst); |
| 5264 | } |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5265 | break; |
| 5266 | } |
| 5267 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5268 | // Call instrucion is deferred because it needs function's ID. Record |
| 5269 | // slot's location on SPIRVInstructionList. |
| 5270 | DeferredInsts.push_back( |
| 5271 | std::make_tuple(&I, --SPIRVInstList.end(), nextID++)); |
| 5272 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5273 | // Check whether the implementation of this call uses an extended |
| 5274 | // instruction plus one more value-producing instruction. If so, then |
| 5275 | // reserve the id for the extra value-producing slot. |
| 5276 | glsl::ExtInst EInst = getIndirectExtInstEnum(Callee->getName()); |
| 5277 | if (EInst != kGlslExtInstBad) { |
| 5278 | // Reserve a spot for the extra value. |
David Neto | 4d02a53 | 2017-09-17 12:57:44 -0400 | [diff] [blame] | 5279 | // Increase nextID. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5280 | VMap[&I] = nextID; |
| 5281 | nextID++; |
| 5282 | } |
| 5283 | break; |
| 5284 | } |
| 5285 | case Instruction::Ret: { |
| 5286 | unsigned NumOps = I.getNumOperands(); |
| 5287 | if (NumOps == 0) { |
| 5288 | // |
| 5289 | // Generate OpReturn. |
| 5290 | // |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 5291 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpReturn)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5292 | } else { |
| 5293 | // |
| 5294 | // Generate OpReturnValue. |
| 5295 | // |
| 5296 | |
| 5297 | // Ops[0] = Return Value ID |
| 5298 | SPIRVOperandList Ops; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5299 | |
| 5300 | Ops << MkId(VMap[I.getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5301 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5302 | auto *Inst = new SPIRVInstruction(spv::OpReturnValue, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5303 | SPIRVInstList.push_back(Inst); |
| 5304 | break; |
| 5305 | } |
| 5306 | break; |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | |
| 5311 | void SPIRVProducerPass::GenerateFuncEpilogue() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 5312 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kFunctions); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5313 | |
| 5314 | // |
| 5315 | // Generate OpFunctionEnd |
| 5316 | // |
| 5317 | |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 5318 | auto *Inst = new SPIRVInstruction(spv::OpFunctionEnd); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5319 | SPIRVInstList.push_back(Inst); |
| 5320 | } |
| 5321 | |
| 5322 | bool SPIRVProducerPass::is4xi8vec(Type *Ty) const { |
alan-baker | b39c826 | 2019-03-08 14:03:37 -0500 | [diff] [blame] | 5323 | // Don't specialize <4 x i8> if i8 is generally supported. |
| 5324 | if (clspv::Option::Int8Support()) |
| 5325 | return false; |
| 5326 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5327 | LLVMContext &Context = Ty->getContext(); |
James Price | cf53df4 | 2020-04-20 14:41:24 -0400 | [diff] [blame] | 5328 | if (auto VecTy = dyn_cast<VectorType>(Ty)) { |
| 5329 | if (VecTy->getElementType() == Type::getInt8Ty(Context) && |
| 5330 | VecTy->getNumElements() == 4) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5331 | return true; |
| 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | return false; |
| 5336 | } |
| 5337 | |
| 5338 | void SPIRVProducerPass::HandleDeferredInstruction() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 5339 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kFunctions); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5340 | ValueMapType &VMap = getValueMap(); |
| 5341 | DeferredInstVecType &DeferredInsts = getDeferredInstVec(); |
| 5342 | |
| 5343 | for (auto DeferredInst = DeferredInsts.rbegin(); |
| 5344 | DeferredInst != DeferredInsts.rend(); ++DeferredInst) { |
| 5345 | Value *Inst = std::get<0>(*DeferredInst); |
| 5346 | SPIRVInstructionList::iterator InsertPoint = ++std::get<1>(*DeferredInst); |
| 5347 | if (InsertPoint != SPIRVInstList.end()) { |
| 5348 | while ((*InsertPoint)->getOpcode() == spv::OpPhi) { |
| 5349 | ++InsertPoint; |
| 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | if (BranchInst *Br = dyn_cast<BranchInst>(Inst)) { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5354 | // Check whether this branch needs to be preceeded by merge instruction. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5355 | BasicBlock *BrBB = Br->getParent(); |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5356 | if (ContinueBlocks.count(BrBB)) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5357 | // |
| 5358 | // Generate OpLoopMerge. |
| 5359 | // |
| 5360 | // Ops[0] = Merge Block ID |
| 5361 | // Ops[1] = Continue Target ID |
| 5362 | // Ops[2] = Selection Control |
| 5363 | SPIRVOperandList Ops; |
| 5364 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5365 | auto MergeBB = MergeBlocks[BrBB]; |
| 5366 | auto ContinueBB = ContinueBlocks[BrBB]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5367 | uint32_t MergeBBID = VMap[MergeBB]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5368 | uint32_t ContinueBBID = VMap[ContinueBB]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5369 | Ops << MkId(MergeBBID) << MkId(ContinueBBID) |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5370 | << MkNum(spv::LoopControlMaskNone); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5371 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5372 | auto *MergeInst = new SPIRVInstruction(spv::OpLoopMerge, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5373 | SPIRVInstList.insert(InsertPoint, MergeInst); |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5374 | } else if (MergeBlocks.count(BrBB)) { |
| 5375 | // |
| 5376 | // Generate OpSelectionMerge. |
| 5377 | // |
| 5378 | // Ops[0] = Merge Block ID |
| 5379 | // Ops[1] = Selection Control |
| 5380 | SPIRVOperandList Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5381 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5382 | auto MergeBB = MergeBlocks[BrBB]; |
| 5383 | uint32_t MergeBBID = VMap[MergeBB]; |
| 5384 | Ops << MkId(MergeBBID) << MkNum(spv::SelectionControlMaskNone); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5385 | |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 5386 | auto *MergeInst = new SPIRVInstruction(spv::OpSelectionMerge, Ops); |
| 5387 | SPIRVInstList.insert(InsertPoint, MergeInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | if (Br->isConditional()) { |
| 5391 | // |
| 5392 | // Generate OpBranchConditional. |
| 5393 | // |
| 5394 | // Ops[0] = Condition ID |
| 5395 | // Ops[1] = True Label ID |
| 5396 | // Ops[2] = False Label ID |
| 5397 | // Ops[3] ... Ops[n] = Branch weights (Literal Number) |
| 5398 | SPIRVOperandList Ops; |
| 5399 | |
| 5400 | uint32_t CondID = VMap[Br->getCondition()]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5401 | uint32_t TrueBBID = VMap[Br->getSuccessor(0)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5402 | uint32_t FalseBBID = VMap[Br->getSuccessor(1)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5403 | |
| 5404 | Ops << MkId(CondID) << MkId(TrueBBID) << MkId(FalseBBID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5405 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5406 | auto *BrInst = new SPIRVInstruction(spv::OpBranchConditional, Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5407 | SPIRVInstList.insert(InsertPoint, BrInst); |
| 5408 | } else { |
| 5409 | // |
| 5410 | // Generate OpBranch. |
| 5411 | // |
| 5412 | // Ops[0] = Target Label ID |
| 5413 | SPIRVOperandList Ops; |
| 5414 | |
| 5415 | uint32_t TargetID = VMap[Br->getSuccessor(0)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5416 | Ops << MkId(TargetID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5417 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5418 | SPIRVInstList.insert(InsertPoint, |
| 5419 | new SPIRVInstruction(spv::OpBranch, Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5420 | } |
| 5421 | } else if (PHINode *PHI = dyn_cast<PHINode>(Inst)) { |
alan-baker | 5ed8754 | 2020-03-23 11:05:22 -0400 | [diff] [blame] | 5422 | if (PHI->getType()->isPointerTy() && !IsSamplerType(PHI->getType()) && |
| 5423 | !IsImageType(PHI->getType())) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5424 | // OpPhi on pointers requires variable pointers. |
| 5425 | setVariablePointersCapabilities( |
| 5426 | PHI->getType()->getPointerAddressSpace()); |
| 5427 | if (!hasVariablePointers() && !selectFromSameObject(PHI)) { |
| 5428 | setVariablePointers(true); |
| 5429 | } |
| 5430 | } |
| 5431 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5432 | // |
| 5433 | // Generate OpPhi. |
| 5434 | // |
| 5435 | // Ops[0] = Result Type ID |
| 5436 | // Ops[1] ... Ops[n] = (Variable ID, Parent ID) pairs |
| 5437 | SPIRVOperandList Ops; |
| 5438 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5439 | Ops << MkId(lookupType(PHI->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5440 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5441 | for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) { |
| 5442 | uint32_t VarID = VMap[PHI->getIncomingValue(i)]; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5443 | uint32_t ParentID = VMap[PHI->getIncomingBlock(i)]; |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5444 | Ops << MkId(VarID) << MkId(ParentID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5448 | InsertPoint, |
| 5449 | new SPIRVInstruction(spv::OpPhi, std::get<2>(*DeferredInst), Ops)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5450 | } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { |
| 5451 | Function *Callee = Call->getCalledFunction(); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 5452 | LLVMContext &Context = Callee->getContext(); |
| 5453 | auto IntTy = Type::getInt32Ty(Context); |
| 5454 | auto callee_code = Builtins::Lookup(Callee); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5455 | auto callee_name = Callee->getName(); |
| 5456 | glsl::ExtInst EInst = getDirectOrIndirectExtInstEnum(callee_name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5457 | |
| 5458 | if (EInst) { |
| 5459 | uint32_t &ExtInstImportID = getOpExtInstImportID(); |
| 5460 | |
| 5461 | // |
| 5462 | // Generate OpExtInst. |
| 5463 | // |
| 5464 | |
| 5465 | // Ops[0] = Result Type ID |
| 5466 | // Ops[1] = Set ID (OpExtInstImport ID) |
| 5467 | // Ops[2] = Instruction Number (Literal Number) |
| 5468 | // Ops[3] ... Ops[n] = Operand 1, ... , Operand n |
| 5469 | SPIRVOperandList Ops; |
| 5470 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5471 | Ops << MkId(lookupType(Call->getType())) << MkId(ExtInstImportID) |
| 5472 | << MkNum(EInst); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5473 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5474 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
| 5475 | for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5476 | Ops << MkId(VMap[Call->getOperand(i)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5477 | } |
| 5478 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5479 | auto *ExtInst = new SPIRVInstruction(spv::OpExtInst, |
| 5480 | std::get<2>(*DeferredInst), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5481 | SPIRVInstList.insert(InsertPoint, ExtInst); |
| 5482 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5483 | const auto IndirectExtInst = getIndirectExtInstEnum(callee_name); |
| 5484 | if (IndirectExtInst != kGlslExtInstBad) { |
| 5485 | // Generate one more instruction that uses the result of the extended |
| 5486 | // instruction. Its result id is one more than the id of the |
| 5487 | // extended instruction. |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5488 | auto generate_extra_inst = [this, &Context, &Call, &DeferredInst, |
| 5489 | &VMap, &SPIRVInstList, &InsertPoint]( |
| 5490 | spv::Op opcode, Constant *constant) { |
| 5491 | // |
| 5492 | // Generate instruction like: |
| 5493 | // result = opcode constant <extinst-result> |
| 5494 | // |
| 5495 | // Ops[0] = Result Type ID |
| 5496 | // Ops[1] = Operand 0 ;; the constant, suitably splatted |
| 5497 | // Ops[2] = Operand 1 ;; the result of the extended instruction |
| 5498 | SPIRVOperandList Ops; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5499 | |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5500 | Type *resultTy = Call->getType(); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5501 | Ops << MkId(lookupType(resultTy)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5502 | |
| 5503 | if (auto *vectorTy = dyn_cast<VectorType>(resultTy)) { |
| 5504 | constant = ConstantVector::getSplat( |
alan-baker | 7261e06 | 2020-03-15 14:35:48 -0400 | [diff] [blame] | 5505 | {static_cast<unsigned>(vectorTy->getNumElements()), false}, |
| 5506 | constant); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5507 | } |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5508 | Ops << MkId(VMap[constant]) << MkId(std::get<2>(*DeferredInst)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5509 | |
| 5510 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5511 | InsertPoint, new SPIRVInstruction( |
| 5512 | opcode, std::get<2>(*DeferredInst) + 1, Ops)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5513 | }; |
| 5514 | |
| 5515 | switch (IndirectExtInst) { |
| 5516 | case glsl::ExtInstFindUMsb: // Implementing clz |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 5517 | generate_extra_inst(spv::OpISub, ConstantInt::get(IntTy, 31)); |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5518 | break; |
| 5519 | case glsl::ExtInstAcos: // Implementing acospi |
| 5520 | case glsl::ExtInstAsin: // Implementing asinpi |
Kévin Petit | eb9f90a | 2018-09-29 12:29:34 +0100 | [diff] [blame] | 5521 | case glsl::ExtInstAtan: // Implementing atanpi |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5522 | case glsl::ExtInstAtan2: // Implementing atan2pi |
| 5523 | generate_extra_inst( |
| 5524 | spv::OpFMul, |
| 5525 | ConstantFP::get(Type::getFloatTy(Context), kOneOverPi)); |
| 5526 | break; |
| 5527 | |
| 5528 | default: |
| 5529 | assert(false && "internally inconsistent"); |
David Neto | 4d02a53 | 2017-09-17 12:57:44 -0400 | [diff] [blame] | 5530 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5531 | } |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5532 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 5533 | } else if (callee_code == Builtins::kPopcount) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5534 | // |
| 5535 | // Generate OpBitCount |
| 5536 | // |
| 5537 | // Ops[0] = Result Type ID |
| 5538 | // Ops[1] = Base ID |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5539 | SPIRVOperandList Ops; |
| 5540 | Ops << MkId(lookupType(Call->getType())) |
| 5541 | << MkId(VMap[Call->getOperand(0)]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5542 | |
| 5543 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5544 | InsertPoint, new SPIRVInstruction(spv::OpBitCount, |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5545 | std::get<2>(*DeferredInst), Ops)); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5546 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5547 | } else if (callee_name.startswith(kCompositeConstructFunctionPrefix)) { |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5548 | |
| 5549 | // Generate an OpCompositeConstruct |
| 5550 | SPIRVOperandList Ops; |
| 5551 | |
| 5552 | // The result type. |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5553 | Ops << MkId(lookupType(Call->getType())); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5554 | |
| 5555 | for (Use &use : Call->arg_operands()) { |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5556 | Ops << MkId(VMap[use.get()]); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5557 | } |
| 5558 | |
| 5559 | SPIRVInstList.insert( |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5560 | InsertPoint, new SPIRVInstruction(spv::OpCompositeConstruct, |
| 5561 | std::get<2>(*DeferredInst), Ops)); |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 5562 | |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 5563 | } else if (callee_name.startswith(clspv::ResourceAccessorFunction())) { |
| 5564 | |
| 5565 | // We have already mapped the call's result value to an ID. |
| 5566 | // Don't generate any code now. |
| 5567 | |
| 5568 | } else if (callee_name.startswith(clspv::WorkgroupAccessorFunction())) { |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5569 | |
| 5570 | // We have already mapped the call's result value to an ID. |
| 5571 | // Don't generate any code now. |
| 5572 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5573 | } else { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5574 | if (Call->getType()->isPointerTy()) { |
| 5575 | // Functions returning pointers require variable pointers. |
| 5576 | setVariablePointersCapabilities( |
| 5577 | Call->getType()->getPointerAddressSpace()); |
| 5578 | } |
| 5579 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5580 | // |
| 5581 | // Generate OpFunctionCall. |
| 5582 | // |
| 5583 | |
| 5584 | // Ops[0] = Result Type ID |
| 5585 | // Ops[1] = Callee Function ID |
| 5586 | // Ops[2] ... Ops[n] = Argument 0, ... , Argument n |
| 5587 | SPIRVOperandList Ops; |
| 5588 | |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5589 | Ops << MkId(lookupType(Call->getType())); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5590 | |
| 5591 | uint32_t CalleeID = VMap[Callee]; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 5592 | if (CalleeID == 0) { |
| 5593 | errs() << "Can't translate function call. Missing builtin? " |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 5594 | << callee_name << " in: " << *Call << "\n"; |
David Neto | 43568eb | 2017-10-13 18:25:25 -0400 | [diff] [blame] | 5595 | // TODO(dneto): Can we error out? Enabling this llvm_unreachable |
| 5596 | // causes an infinite loop. Instead, go ahead and generate |
| 5597 | // the bad function call. A validator will catch the 0-Id. |
| 5598 | // llvm_unreachable("Can't translate function call"); |
| 5599 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5600 | |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5601 | Ops << MkId(CalleeID); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5602 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5603 | FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType()); |
| 5604 | for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5605 | auto *operand = Call->getOperand(i); |
alan-baker | d4d5065 | 2019-12-03 17:17:15 -0500 | [diff] [blame] | 5606 | auto *operand_type = operand->getType(); |
| 5607 | // Images and samplers can be passed as function parameters without |
| 5608 | // variable pointers. |
| 5609 | if (operand_type->isPointerTy() && !IsImageType(operand_type) && |
| 5610 | !IsSamplerType(operand_type)) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 5611 | auto sc = |
| 5612 | GetStorageClass(operand->getType()->getPointerAddressSpace()); |
| 5613 | if (sc == spv::StorageClassStorageBuffer) { |
| 5614 | // Passing SSBO by reference requires variable pointers storage |
| 5615 | // buffer. |
| 5616 | setVariablePointersStorageBuffer(true); |
| 5617 | } else if (sc == spv::StorageClassWorkgroup) { |
| 5618 | // Workgroup references require variable pointers if they are not |
| 5619 | // memory object declarations. |
| 5620 | if (auto *operand_call = dyn_cast<CallInst>(operand)) { |
| 5621 | // Workgroup accessor represents a variable reference. |
| 5622 | if (!operand_call->getCalledFunction()->getName().startswith( |
| 5623 | clspv::WorkgroupAccessorFunction())) |
| 5624 | setVariablePointers(true); |
| 5625 | } else { |
| 5626 | // Arguments are function parameters. |
| 5627 | if (!isa<Argument>(operand)) |
| 5628 | setVariablePointers(true); |
| 5629 | } |
| 5630 | } |
| 5631 | } |
| 5632 | Ops << MkId(VMap[operand]); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5633 | } |
| 5634 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5635 | auto *CallInst = new SPIRVInstruction(spv::OpFunctionCall, |
| 5636 | std::get<2>(*DeferredInst), Ops); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5637 | SPIRVInstList.insert(InsertPoint, CallInst); |
| 5638 | } |
| 5639 | } |
| 5640 | } |
| 5641 | } |
| 5642 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5643 | void SPIRVProducerPass::HandleDeferredDecorations() { |
| 5644 | const auto &DL = module->getDataLayout(); |
Alan Baker | 202c8c7 | 2018-08-13 13:47:44 -0400 | [diff] [blame] | 5645 | if (getTypesNeedingArrayStride().empty() && LocalArgSpecIds.empty()) { |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5646 | return; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5647 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5648 | |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 5649 | SPIRVInstructionList &SPIRVInstList = getSPIRVInstList(kAnnotations); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5650 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5651 | // Insert ArrayStride decorations on pointer types, due to OpPtrAccessChain |
| 5652 | // instructions we generated earlier. |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 5653 | for (auto *type : getTypesNeedingArrayStride()) { |
| 5654 | Type *elemTy = nullptr; |
| 5655 | if (auto *ptrTy = dyn_cast<PointerType>(type)) { |
| 5656 | elemTy = ptrTy->getElementType(); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5657 | } else if (auto *arrayTy = dyn_cast<ArrayType>(type)) { |
alan-baker | 8eb435a | 2020-04-08 00:42:06 -0400 | [diff] [blame] | 5658 | elemTy = arrayTy->getElementType(); |
| 5659 | } else if (auto *vecTy = dyn_cast<VectorType>(type)) { |
| 5660 | elemTy = vecTy->getElementType(); |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 5661 | } else { |
| 5662 | errs() << "Unhandled strided type " << *type << "\n"; |
| 5663 | llvm_unreachable("Unhandled strided type"); |
| 5664 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5665 | |
| 5666 | // Ops[0] = Target ID |
| 5667 | // Ops[1] = Decoration (ArrayStride) |
| 5668 | // Ops[2] = Stride number (Literal Number) |
| 5669 | SPIRVOperandList Ops; |
| 5670 | |
David Neto | 8508264 | 2018-03-24 06:55:20 -0700 | [diff] [blame] | 5671 | // Same as DL.getIndexedOffsetInType( elemTy, { 1 } ); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 5672 | const uint32_t stride = static_cast<uint32_t>(GetTypeAllocSize(elemTy, DL)); |
David Neto | 257c389 | 2018-04-11 13:19:45 -0400 | [diff] [blame] | 5673 | |
| 5674 | Ops << MkId(lookupType(type)) << MkNum(spv::DecorationArrayStride) |
| 5675 | << MkNum(stride); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5676 | |
David Neto | 8784674 | 2018-04-11 17:36:22 -0400 | [diff] [blame] | 5677 | auto *DecoInst = new SPIRVInstruction(spv::OpDecorate, Ops); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 5678 | SPIRVInstList.push_back(DecoInst); |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5679 | } |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5680 | |
| 5681 | // Emit SpecId decorations targeting the array size value. |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 5682 | for (auto pair : clspv::GetSpecConstants(module)) { |
alan-baker | a1be332 | 2020-04-20 12:48:18 -0400 | [diff] [blame] | 5683 | auto kind = pair.first; |
| 5684 | auto spec_id = pair.second; |
| 5685 | |
| 5686 | if (kind != SpecConstant::kLocalMemorySize) |
| 5687 | continue; |
| 5688 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5689 | LocalArgInfo &arg_info = LocalSpecIdInfoMap[spec_id]; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5690 | SPIRVOperandList Ops; |
| 5691 | Ops << MkId(arg_info.array_size_id) << MkNum(spv::DecorationSpecId) |
| 5692 | << MkNum(arg_info.spec_id); |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 5693 | SPIRVInstList.push_back(new SPIRVInstruction(spv::OpDecorate, Ops)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 5694 | } |
David Neto | 1a1a058 | 2017-07-07 12:01:44 -0400 | [diff] [blame] | 5695 | } |
| 5696 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5697 | glsl::ExtInst SPIRVProducerPass::getExtInstEnum(StringRef Name) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 5698 | |
| 5699 | const auto &fi = Builtins::Lookup(Name); |
| 5700 | switch (fi) { |
| 5701 | case Builtins::kClamp: { |
| 5702 | auto param_type = fi.getParameter(0); |
| 5703 | if (param_type.type_id == Type::FloatTyID) { |
| 5704 | return glsl::ExtInst::ExtInstFClamp; |
| 5705 | } |
| 5706 | return param_type.is_signed ? glsl::ExtInst::ExtInstSClamp |
| 5707 | : glsl::ExtInst::ExtInstUClamp; |
| 5708 | } |
| 5709 | case Builtins::kMax: { |
| 5710 | auto param_type = fi.getParameter(0); |
| 5711 | if (param_type.type_id == Type::FloatTyID) { |
| 5712 | return glsl::ExtInst::ExtInstFMax; |
| 5713 | } |
| 5714 | return param_type.is_signed ? glsl::ExtInst::ExtInstSMax |
| 5715 | : glsl::ExtInst::ExtInstUMax; |
| 5716 | } |
| 5717 | case Builtins::kMin: { |
| 5718 | auto param_type = fi.getParameter(0); |
| 5719 | if (param_type.type_id == Type::FloatTyID) { |
| 5720 | return glsl::ExtInst::ExtInstFMin; |
| 5721 | } |
| 5722 | return param_type.is_signed ? glsl::ExtInst::ExtInstSMin |
| 5723 | : glsl::ExtInst::ExtInstUMin; |
| 5724 | } |
| 5725 | case Builtins::kAbs: |
| 5726 | return glsl::ExtInst::ExtInstSAbs; |
| 5727 | case Builtins::kFmax: |
| 5728 | return glsl::ExtInst::ExtInstFMax; |
| 5729 | case Builtins::kFmin: |
| 5730 | return glsl::ExtInst::ExtInstFMin; |
| 5731 | case Builtins::kDegrees: |
| 5732 | return glsl::ExtInst::ExtInstDegrees; |
| 5733 | case Builtins::kRadians: |
| 5734 | return glsl::ExtInst::ExtInstRadians; |
| 5735 | case Builtins::kMix: |
| 5736 | return glsl::ExtInst::ExtInstFMix; |
| 5737 | case Builtins::kAcos: |
| 5738 | case Builtins::kAcospi: |
| 5739 | return glsl::ExtInst::ExtInstAcos; |
| 5740 | case Builtins::kAcosh: |
| 5741 | return glsl::ExtInst::ExtInstAcosh; |
| 5742 | case Builtins::kAsin: |
| 5743 | case Builtins::kAsinpi: |
| 5744 | return glsl::ExtInst::ExtInstAsin; |
| 5745 | case Builtins::kAsinh: |
| 5746 | return glsl::ExtInst::ExtInstAsinh; |
| 5747 | case Builtins::kAtan: |
| 5748 | case Builtins::kAtanpi: |
| 5749 | return glsl::ExtInst::ExtInstAtan; |
| 5750 | case Builtins::kAtanh: |
| 5751 | return glsl::ExtInst::ExtInstAtanh; |
| 5752 | case Builtins::kAtan2: |
| 5753 | case Builtins::kAtan2pi: |
| 5754 | return glsl::ExtInst::ExtInstAtan2; |
| 5755 | case Builtins::kCeil: |
| 5756 | return glsl::ExtInst::ExtInstCeil; |
| 5757 | case Builtins::kSin: |
| 5758 | case Builtins::kHalfSin: |
| 5759 | case Builtins::kNativeSin: |
| 5760 | return glsl::ExtInst::ExtInstSin; |
| 5761 | case Builtins::kSinh: |
| 5762 | return glsl::ExtInst::ExtInstSinh; |
| 5763 | case Builtins::kCos: |
| 5764 | case Builtins::kHalfCos: |
| 5765 | case Builtins::kNativeCos: |
| 5766 | return glsl::ExtInst::ExtInstCos; |
| 5767 | case Builtins::kCosh: |
| 5768 | return glsl::ExtInst::ExtInstCosh; |
| 5769 | case Builtins::kTan: |
| 5770 | case Builtins::kHalfTan: |
| 5771 | case Builtins::kNativeTan: |
| 5772 | return glsl::ExtInst::ExtInstTan; |
| 5773 | case Builtins::kTanh: |
| 5774 | return glsl::ExtInst::ExtInstTanh; |
| 5775 | case Builtins::kExp: |
| 5776 | case Builtins::kHalfExp: |
| 5777 | case Builtins::kNativeExp: |
| 5778 | return glsl::ExtInst::ExtInstExp; |
| 5779 | case Builtins::kExp2: |
| 5780 | case Builtins::kHalfExp2: |
| 5781 | case Builtins::kNativeExp2: |
| 5782 | return glsl::ExtInst::ExtInstExp2; |
| 5783 | case Builtins::kLog: |
| 5784 | case Builtins::kHalfLog: |
| 5785 | case Builtins::kNativeLog: |
| 5786 | return glsl::ExtInst::ExtInstLog; |
| 5787 | case Builtins::kLog2: |
| 5788 | case Builtins::kHalfLog2: |
| 5789 | case Builtins::kNativeLog2: |
| 5790 | return glsl::ExtInst::ExtInstLog2; |
| 5791 | case Builtins::kFabs: |
| 5792 | return glsl::ExtInst::ExtInstFAbs; |
| 5793 | case Builtins::kFma: |
| 5794 | return glsl::ExtInst::ExtInstFma; |
| 5795 | case Builtins::kFloor: |
| 5796 | return glsl::ExtInst::ExtInstFloor; |
| 5797 | case Builtins::kLdexp: |
| 5798 | return glsl::ExtInst::ExtInstLdexp; |
| 5799 | case Builtins::kPow: |
| 5800 | case Builtins::kPowr: |
| 5801 | case Builtins::kHalfPowr: |
| 5802 | case Builtins::kNativePowr: |
| 5803 | return glsl::ExtInst::ExtInstPow; |
| 5804 | case Builtins::kRound: |
| 5805 | return glsl::ExtInst::ExtInstRound; |
| 5806 | case Builtins::kSqrt: |
| 5807 | case Builtins::kHalfSqrt: |
| 5808 | case Builtins::kNativeSqrt: |
| 5809 | return glsl::ExtInst::ExtInstSqrt; |
| 5810 | case Builtins::kRsqrt: |
| 5811 | case Builtins::kHalfRsqrt: |
| 5812 | case Builtins::kNativeRsqrt: |
| 5813 | return glsl::ExtInst::ExtInstInverseSqrt; |
| 5814 | case Builtins::kTrunc: |
| 5815 | return glsl::ExtInst::ExtInstTrunc; |
| 5816 | case Builtins::kFrexp: |
| 5817 | return glsl::ExtInst::ExtInstFrexp; |
| 5818 | case Builtins::kFract: |
| 5819 | return glsl::ExtInst::ExtInstFract; |
| 5820 | case Builtins::kSign: |
| 5821 | return glsl::ExtInst::ExtInstFSign; |
| 5822 | case Builtins::kLength: |
| 5823 | case Builtins::kFastLength: |
| 5824 | return glsl::ExtInst::ExtInstLength; |
| 5825 | case Builtins::kDistance: |
| 5826 | case Builtins::kFastDistance: |
| 5827 | return glsl::ExtInst::ExtInstDistance; |
| 5828 | case Builtins::kStep: |
| 5829 | return glsl::ExtInst::ExtInstStep; |
| 5830 | case Builtins::kSmoothstep: |
| 5831 | return glsl::ExtInst::ExtInstSmoothStep; |
| 5832 | case Builtins::kCross: |
| 5833 | return glsl::ExtInst::ExtInstCross; |
| 5834 | case Builtins::kNormalize: |
| 5835 | case Builtins::kFastNormalize: |
| 5836 | return glsl::ExtInst::ExtInstNormalize; |
| 5837 | default: |
| 5838 | break; |
| 5839 | } |
| 5840 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5841 | return StringSwitch<glsl::ExtInst>(Name) |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5842 | .StartsWith("llvm.fmuladd.", glsl::ExtInst::ExtInstFma) |
| 5843 | .Case("spirv.unpack.v2f16", glsl::ExtInst::ExtInstUnpackHalf2x16) |
| 5844 | .Case("spirv.pack.v2f16", glsl::ExtInst::ExtInstPackHalf2x16) |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5845 | .Default(kGlslExtInstBad); |
| 5846 | } |
| 5847 | |
| 5848 | glsl::ExtInst SPIRVProducerPass::getIndirectExtInstEnum(StringRef Name) { |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 5849 | switch (Builtins::Lookup(Name)) { |
| 5850 | case Builtins::kClz: |
| 5851 | return glsl::ExtInst::ExtInstFindUMsb; |
| 5852 | case Builtins::kAcospi: |
| 5853 | return glsl::ExtInst::ExtInstAcos; |
| 5854 | case Builtins::kAsinpi: |
| 5855 | return glsl::ExtInst::ExtInstAsin; |
| 5856 | case Builtins::kAtanpi: |
| 5857 | return glsl::ExtInst::ExtInstAtan; |
| 5858 | case Builtins::kAtan2pi: |
| 5859 | return glsl::ExtInst::ExtInstAtan2; |
| 5860 | default: |
| 5861 | break; |
| 5862 | } |
| 5863 | return kGlslExtInstBad; |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5864 | } |
| 5865 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 5866 | glsl::ExtInst |
| 5867 | SPIRVProducerPass::getDirectOrIndirectExtInstEnum(StringRef Name) { |
David Neto | 3fbb407 | 2017-10-16 11:28:14 -0400 | [diff] [blame] | 5868 | auto direct = getExtInstEnum(Name); |
| 5869 | if (direct != kGlslExtInstBad) |
| 5870 | return direct; |
| 5871 | return getIndirectExtInstEnum(Name); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5872 | } |
| 5873 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5874 | void SPIRVProducerPass::WriteOneWord(uint32_t Word) { |
David Neto | 0676e6f | 2017-07-11 18:47:44 -0400 | [diff] [blame] | 5875 | binaryOut->write(reinterpret_cast<const char *>(&Word), sizeof(uint32_t)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | void SPIRVProducerPass::WriteResultID(SPIRVInstruction *Inst) { |
| 5879 | WriteOneWord(Inst->getResultID()); |
| 5880 | } |
| 5881 | |
| 5882 | void SPIRVProducerPass::WriteWordCountAndOpcode(SPIRVInstruction *Inst) { |
| 5883 | // High 16 bit : Word Count |
| 5884 | // Low 16 bit : Opcode |
| 5885 | uint32_t Word = Inst->getOpcode(); |
David Neto | ee2660d | 2018-06-28 16:31:29 -0400 | [diff] [blame] | 5886 | const uint32_t count = Inst->getWordCount(); |
| 5887 | if (count > 65535) { |
| 5888 | errs() << "Word count limit of 65535 exceeded: " << count << "\n"; |
| 5889 | llvm_unreachable("Word count too high"); |
| 5890 | } |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5891 | Word |= Inst->getWordCount() << 16; |
| 5892 | WriteOneWord(Word); |
| 5893 | } |
| 5894 | |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 5895 | void SPIRVProducerPass::WriteOperand(const std::unique_ptr<SPIRVOperand> &Op) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5896 | SPIRVOperandType OpTy = Op->getType(); |
| 5897 | switch (OpTy) { |
| 5898 | default: { |
| 5899 | llvm_unreachable("Unsupported SPIRV Operand Type???"); |
| 5900 | break; |
| 5901 | } |
| 5902 | case SPIRVOperandType::NUMBERID: { |
| 5903 | WriteOneWord(Op->getNumID()); |
| 5904 | break; |
| 5905 | } |
| 5906 | case SPIRVOperandType::LITERAL_STRING: { |
| 5907 | std::string Str = Op->getLiteralStr(); |
| 5908 | const char *Data = Str.c_str(); |
| 5909 | size_t WordSize = Str.size() / 4; |
| 5910 | for (unsigned Idx = 0; Idx < WordSize; Idx++) { |
| 5911 | WriteOneWord(*reinterpret_cast<const uint32_t *>(&Data[4 * Idx])); |
| 5912 | } |
| 5913 | |
| 5914 | uint32_t Remainder = Str.size() % 4; |
| 5915 | uint32_t LastWord = 0; |
| 5916 | if (Remainder) { |
| 5917 | for (unsigned Idx = 0; Idx < Remainder; Idx++) { |
| 5918 | LastWord |= Data[4 * WordSize + Idx] << 8 * Idx; |
| 5919 | } |
| 5920 | } |
| 5921 | |
| 5922 | WriteOneWord(LastWord); |
| 5923 | break; |
| 5924 | } |
| 5925 | case SPIRVOperandType::LITERAL_INTEGER: |
| 5926 | case SPIRVOperandType::LITERAL_FLOAT: { |
| 5927 | auto LiteralNum = Op->getLiteralNum(); |
| 5928 | // TODO: Handle LiteranNum carefully. |
| 5929 | for (auto Word : LiteralNum) { |
| 5930 | WriteOneWord(Word); |
| 5931 | } |
| 5932 | break; |
| 5933 | } |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | void SPIRVProducerPass::WriteSPIRVBinary() { |
SJW | 69939d5 | 2020-04-16 07:29:07 -0500 | [diff] [blame] | 5938 | for (int i = 0; i < kSectionCount; ++i) { |
| 5939 | WriteSPIRVBinary(SPIRVSections[i]); |
| 5940 | } |
| 5941 | } |
| 5942 | |
| 5943 | void SPIRVProducerPass::WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5944 | |
| 5945 | for (auto Inst : SPIRVInstList) { |
David Neto | ef5ba2b | 2019-12-20 08:35:54 -0500 | [diff] [blame] | 5946 | const auto &Ops = Inst->getOperands(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5947 | spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode()); |
| 5948 | |
| 5949 | switch (Opcode) { |
| 5950 | default: { |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 5951 | errs() << "Unsupported SPIR-V instruction opcode " << int(Opcode) << "\n"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 5952 | llvm_unreachable("Unsupported SPIRV instruction"); |
| 5953 | break; |
| 5954 | } |
| 5955 | case spv::OpCapability: |
| 5956 | case spv::OpExtension: |
| 5957 | case spv::OpMemoryModel: |
| 5958 | case spv::OpEntryPoint: |
| 5959 | case spv::OpExecutionMode: |
| 5960 | case spv::OpSource: |
| 5961 | case spv::OpDecorate: |
| 5962 | case spv::OpMemberDecorate: |
| 5963 | case spv::OpBranch: |
| 5964 | case spv::OpBranchConditional: |
| 5965 | case spv::OpSelectionMerge: |
| 5966 | case spv::OpLoopMerge: |
| 5967 | case spv::OpStore: |
| 5968 | case spv::OpImageWrite: |
| 5969 | case spv::OpReturnValue: |
| 5970 | case spv::OpControlBarrier: |
| 5971 | case spv::OpMemoryBarrier: |
| 5972 | case spv::OpReturn: |
| 5973 | case spv::OpFunctionEnd: |
| 5974 | case spv::OpCopyMemory: { |
| 5975 | WriteWordCountAndOpcode(Inst); |
| 5976 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5977 | WriteOperand(Ops[i]); |
| 5978 | } |
| 5979 | break; |
| 5980 | } |
| 5981 | case spv::OpTypeBool: |
| 5982 | case spv::OpTypeVoid: |
| 5983 | case spv::OpTypeSampler: |
| 5984 | case spv::OpLabel: |
| 5985 | case spv::OpExtInstImport: |
| 5986 | case spv::OpTypePointer: |
| 5987 | case spv::OpTypeRuntimeArray: |
| 5988 | case spv::OpTypeStruct: |
| 5989 | case spv::OpTypeImage: |
| 5990 | case spv::OpTypeSampledImage: |
| 5991 | case spv::OpTypeInt: |
| 5992 | case spv::OpTypeFloat: |
| 5993 | case spv::OpTypeArray: |
| 5994 | case spv::OpTypeVector: |
| 5995 | case spv::OpTypeFunction: { |
| 5996 | WriteWordCountAndOpcode(Inst); |
| 5997 | WriteResultID(Inst); |
| 5998 | for (uint32_t i = 0; i < Ops.size(); i++) { |
| 5999 | WriteOperand(Ops[i]); |
| 6000 | } |
| 6001 | break; |
| 6002 | } |
| 6003 | case spv::OpFunction: |
| 6004 | case spv::OpFunctionParameter: |
| 6005 | case spv::OpAccessChain: |
| 6006 | case spv::OpPtrAccessChain: |
| 6007 | case spv::OpInBoundsAccessChain: |
| 6008 | case spv::OpUConvert: |
| 6009 | case spv::OpSConvert: |
| 6010 | case spv::OpConvertFToU: |
| 6011 | case spv::OpConvertFToS: |
| 6012 | case spv::OpConvertUToF: |
| 6013 | case spv::OpConvertSToF: |
| 6014 | case spv::OpFConvert: |
| 6015 | case spv::OpConvertPtrToU: |
| 6016 | case spv::OpConvertUToPtr: |
| 6017 | case spv::OpBitcast: |
alan-baker | c9c55ae | 2019-12-02 16:01:27 -0500 | [diff] [blame] | 6018 | case spv::OpFNegate: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6019 | case spv::OpIAdd: |
| 6020 | case spv::OpFAdd: |
| 6021 | case spv::OpISub: |
| 6022 | case spv::OpFSub: |
| 6023 | case spv::OpIMul: |
| 6024 | case spv::OpFMul: |
| 6025 | case spv::OpUDiv: |
| 6026 | case spv::OpSDiv: |
| 6027 | case spv::OpFDiv: |
| 6028 | case spv::OpUMod: |
| 6029 | case spv::OpSRem: |
| 6030 | case spv::OpFRem: |
Kévin Petit | 8a56088 | 2019-03-21 15:24:34 +0000 | [diff] [blame] | 6031 | case spv::OpUMulExtended: |
| 6032 | case spv::OpSMulExtended: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6033 | case spv::OpBitwiseOr: |
| 6034 | case spv::OpBitwiseXor: |
| 6035 | case spv::OpBitwiseAnd: |
David Neto | a394f39 | 2017-08-26 20:45:29 -0400 | [diff] [blame] | 6036 | case spv::OpNot: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6037 | case spv::OpShiftLeftLogical: |
| 6038 | case spv::OpShiftRightLogical: |
| 6039 | case spv::OpShiftRightArithmetic: |
| 6040 | case spv::OpBitCount: |
David Neto | ab03f43 | 2017-11-03 17:00:44 -0400 | [diff] [blame] | 6041 | case spv::OpCompositeConstruct: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6042 | case spv::OpCompositeExtract: |
| 6043 | case spv::OpVectorExtractDynamic: |
| 6044 | case spv::OpCompositeInsert: |
David Neto | 0a2f98d | 2017-09-15 19:38:40 -0400 | [diff] [blame] | 6045 | case spv::OpCopyObject: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6046 | case spv::OpVectorInsertDynamic: |
| 6047 | case spv::OpVectorShuffle: |
| 6048 | case spv::OpIEqual: |
| 6049 | case spv::OpINotEqual: |
| 6050 | case spv::OpUGreaterThan: |
| 6051 | case spv::OpUGreaterThanEqual: |
| 6052 | case spv::OpULessThan: |
| 6053 | case spv::OpULessThanEqual: |
| 6054 | case spv::OpSGreaterThan: |
| 6055 | case spv::OpSGreaterThanEqual: |
| 6056 | case spv::OpSLessThan: |
| 6057 | case spv::OpSLessThanEqual: |
| 6058 | case spv::OpFOrdEqual: |
| 6059 | case spv::OpFOrdGreaterThan: |
| 6060 | case spv::OpFOrdGreaterThanEqual: |
| 6061 | case spv::OpFOrdLessThan: |
| 6062 | case spv::OpFOrdLessThanEqual: |
| 6063 | case spv::OpFOrdNotEqual: |
| 6064 | case spv::OpFUnordEqual: |
| 6065 | case spv::OpFUnordGreaterThan: |
| 6066 | case spv::OpFUnordGreaterThanEqual: |
| 6067 | case spv::OpFUnordLessThan: |
| 6068 | case spv::OpFUnordLessThanEqual: |
| 6069 | case spv::OpFUnordNotEqual: |
| 6070 | case spv::OpExtInst: |
| 6071 | case spv::OpIsInf: |
| 6072 | case spv::OpIsNan: |
| 6073 | case spv::OpAny: |
| 6074 | case spv::OpAll: |
| 6075 | case spv::OpUndef: |
| 6076 | case spv::OpConstantNull: |
| 6077 | case spv::OpLogicalOr: |
| 6078 | case spv::OpLogicalAnd: |
| 6079 | case spv::OpLogicalNot: |
| 6080 | case spv::OpLogicalNotEqual: |
| 6081 | case spv::OpConstantComposite: |
| 6082 | case spv::OpSpecConstantComposite: |
| 6083 | case spv::OpConstantTrue: |
| 6084 | case spv::OpConstantFalse: |
| 6085 | case spv::OpConstant: |
| 6086 | case spv::OpSpecConstant: |
| 6087 | case spv::OpVariable: |
| 6088 | case spv::OpFunctionCall: |
| 6089 | case spv::OpSampledImage: |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 6090 | case spv::OpImageFetch: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6091 | case spv::OpImageSampleExplicitLod: |
David Neto | 5c22a25 | 2018-03-15 16:07:41 -0400 | [diff] [blame] | 6092 | case spv::OpImageQuerySize: |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 6093 | case spv::OpImageQuerySizeLod: |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 6094 | case spv::OpSelect: |
| 6095 | case spv::OpPhi: |
| 6096 | case spv::OpLoad: |
| 6097 | case spv::OpAtomicIAdd: |
| 6098 | case spv::OpAtomicISub: |
| 6099 | case spv::OpAtomicExchange: |
| 6100 | case spv::OpAtomicIIncrement: |
| 6101 | case spv::OpAtomicIDecrement: |
| 6102 | case spv::OpAtomicCompareExchange: |
| 6103 | case spv::OpAtomicUMin: |
| 6104 | case spv::OpAtomicSMin: |
| 6105 | case spv::OpAtomicUMax: |
| 6106 | case spv::OpAtomicSMax: |
| 6107 | case spv::OpAtomicAnd: |
| 6108 | case spv::OpAtomicOr: |
| 6109 | case spv::OpAtomicXor: |
| 6110 | case spv::OpDot: { |
| 6111 | WriteWordCountAndOpcode(Inst); |
| 6112 | WriteOperand(Ops[0]); |
| 6113 | WriteResultID(Inst); |
| 6114 | for (uint32_t i = 1; i < Ops.size(); i++) { |
| 6115 | WriteOperand(Ops[i]); |
| 6116 | } |
| 6117 | break; |
| 6118 | } |
| 6119 | } |
| 6120 | } |
| 6121 | } |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 6122 | |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 6123 | bool SPIRVProducerPass::IsTypeNullable(const Type *type) const { |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 6124 | switch (type->getTypeID()) { |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 6125 | case Type::HalfTyID: |
| 6126 | case Type::FloatTyID: |
| 6127 | case Type::DoubleTyID: |
| 6128 | case Type::IntegerTyID: |
James Price | 59a1c75 | 2020-04-23 23:06:16 -0400 | [diff] [blame^] | 6129 | case Type::FixedVectorTyID: |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 6130 | return true; |
| 6131 | case Type::PointerTyID: { |
| 6132 | const PointerType *pointer_type = cast<PointerType>(type); |
| 6133 | if (pointer_type->getPointerAddressSpace() != |
| 6134 | AddressSpace::UniformConstant) { |
| 6135 | auto pointee_type = pointer_type->getPointerElementType(); |
| 6136 | if (pointee_type->isStructTy() && |
| 6137 | cast<StructType>(pointee_type)->isOpaque()) { |
| 6138 | // Images and samplers are not nullable. |
| 6139 | return false; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 6140 | } |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 6141 | } |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 6142 | return true; |
| 6143 | } |
| 6144 | case Type::ArrayTyID: |
alan-baker | 8eb435a | 2020-04-08 00:42:06 -0400 | [diff] [blame] | 6145 | return IsTypeNullable(type->getArrayElementType()); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 6146 | case Type::StructTyID: { |
| 6147 | const StructType *struct_type = cast<StructType>(type); |
| 6148 | // Images and samplers are not nullable. |
| 6149 | if (struct_type->isOpaque()) |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 6150 | return false; |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 6151 | for (const auto element : struct_type->elements()) { |
| 6152 | if (!IsTypeNullable(element)) |
| 6153 | return false; |
| 6154 | } |
| 6155 | return true; |
| 6156 | } |
| 6157 | default: |
| 6158 | return false; |
Alan Baker | 9bf93fb | 2018-08-28 16:59:26 -0400 | [diff] [blame] | 6159 | } |
| 6160 | } |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 6161 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 6162 | void SPIRVProducerPass::PopulateUBOTypeMaps() { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 6163 | if (auto *offsets_md = |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 6164 | module->getNamedMetadata(clspv::RemappedTypeOffsetMetadataName())) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 6165 | // Metdata is stored as key-value pair operands. The first element of each |
| 6166 | // operand is the type and the second is a vector of offsets. |
| 6167 | for (const auto *operand : offsets_md->operands()) { |
| 6168 | const auto *pair = cast<MDTuple>(operand); |
| 6169 | auto *type = |
| 6170 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 6171 | const auto *offset_vector = cast<MDTuple>(pair->getOperand(1)); |
| 6172 | std::vector<uint32_t> offsets; |
| 6173 | for (const Metadata *offset_md : offset_vector->operands()) { |
| 6174 | const auto *constant_md = cast<ConstantAsMetadata>(offset_md); |
alan-baker | b6b09dc | 2018-11-08 16:59:28 -0500 | [diff] [blame] | 6175 | offsets.push_back(static_cast<uint32_t>( |
| 6176 | cast<ConstantInt>(constant_md->getValue())->getZExtValue())); |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 6177 | } |
| 6178 | RemappedUBOTypeOffsets.insert(std::make_pair(type, offsets)); |
| 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | if (auto *sizes_md = |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 6183 | module->getNamedMetadata(clspv::RemappedTypeSizesMetadataName())) { |
Alan Baker | fcda948 | 2018-10-02 17:09:59 -0400 | [diff] [blame] | 6184 | // Metadata is stored as key-value pair operands. The first element of each |
| 6185 | // operand is the type and the second is a triple of sizes: type size in |
| 6186 | // bits, store size and alloc size. |
| 6187 | for (const auto *operand : sizes_md->operands()) { |
| 6188 | const auto *pair = cast<MDTuple>(operand); |
| 6189 | auto *type = |
| 6190 | cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType(); |
| 6191 | const auto *size_triple = cast<MDTuple>(pair->getOperand(1)); |
| 6192 | uint64_t type_size_in_bits = |
| 6193 | cast<ConstantInt>( |
| 6194 | cast<ConstantAsMetadata>(size_triple->getOperand(0))->getValue()) |
| 6195 | ->getZExtValue(); |
| 6196 | uint64_t type_store_size = |
| 6197 | cast<ConstantInt>( |
| 6198 | cast<ConstantAsMetadata>(size_triple->getOperand(1))->getValue()) |
| 6199 | ->getZExtValue(); |
| 6200 | uint64_t type_alloc_size = |
| 6201 | cast<ConstantInt>( |
| 6202 | cast<ConstantAsMetadata>(size_triple->getOperand(2))->getValue()) |
| 6203 | ->getZExtValue(); |
| 6204 | RemappedUBOTypeSizes.insert(std::make_pair( |
| 6205 | type, std::make_tuple(type_size_in_bits, type_store_size, |
| 6206 | type_alloc_size))); |
| 6207 | } |
| 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | uint64_t SPIRVProducerPass::GetTypeSizeInBits(Type *type, |
| 6212 | const DataLayout &DL) { |
| 6213 | auto iter = RemappedUBOTypeSizes.find(type); |
| 6214 | if (iter != RemappedUBOTypeSizes.end()) { |
| 6215 | return std::get<0>(iter->second); |
| 6216 | } |
| 6217 | |
| 6218 | return DL.getTypeSizeInBits(type); |
| 6219 | } |
| 6220 | |
| 6221 | uint64_t SPIRVProducerPass::GetTypeStoreSize(Type *type, const DataLayout &DL) { |
| 6222 | auto iter = RemappedUBOTypeSizes.find(type); |
| 6223 | if (iter != RemappedUBOTypeSizes.end()) { |
| 6224 | return std::get<1>(iter->second); |
| 6225 | } |
| 6226 | |
| 6227 | return DL.getTypeStoreSize(type); |
| 6228 | } |
| 6229 | |
| 6230 | uint64_t SPIRVProducerPass::GetTypeAllocSize(Type *type, const DataLayout &DL) { |
| 6231 | auto iter = RemappedUBOTypeSizes.find(type); |
| 6232 | if (iter != RemappedUBOTypeSizes.end()) { |
| 6233 | return std::get<2>(iter->second); |
| 6234 | } |
| 6235 | |
| 6236 | return DL.getTypeAllocSize(type); |
| 6237 | } |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 6238 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 6239 | uint32_t SPIRVProducerPass::GetExplicitLayoutStructMemberOffset( |
| 6240 | StructType *type, unsigned member, const DataLayout &DL) { |
| 6241 | const auto StructLayout = DL.getStructLayout(type); |
| 6242 | // Search for the correct offsets if this type was remapped. |
| 6243 | std::vector<uint32_t> *offsets = nullptr; |
| 6244 | auto iter = RemappedUBOTypeOffsets.find(type); |
| 6245 | if (iter != RemappedUBOTypeOffsets.end()) { |
| 6246 | offsets = &iter->second; |
| 6247 | } |
| 6248 | auto ByteOffset = |
| 6249 | static_cast<uint32_t>(StructLayout->getElementOffset(member)); |
| 6250 | if (offsets) { |
| 6251 | ByteOffset = (*offsets)[member]; |
| 6252 | } |
| 6253 | |
| 6254 | return ByteOffset; |
| 6255 | } |
| 6256 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 6257 | void SPIRVProducerPass::setVariablePointersCapabilities( |
| 6258 | unsigned address_space) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 6259 | if (GetStorageClass(address_space) == spv::StorageClassStorageBuffer) { |
| 6260 | setVariablePointersStorageBuffer(true); |
| 6261 | } else { |
| 6262 | setVariablePointers(true); |
| 6263 | } |
| 6264 | } |
| 6265 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 6266 | Value *SPIRVProducerPass::GetBasePointer(Value *v) { |
alan-baker | 5b86ed7 | 2019-02-15 08:26:50 -0500 | [diff] [blame] | 6267 | if (auto *gep = dyn_cast<GetElementPtrInst>(v)) { |
| 6268 | return GetBasePointer(gep->getPointerOperand()); |
| 6269 | } |
| 6270 | |
| 6271 | // Conservatively return |v|. |
| 6272 | return v; |
| 6273 | } |
| 6274 | |
| 6275 | bool SPIRVProducerPass::sameResource(Value *lhs, Value *rhs) const { |
| 6276 | if (auto *lhs_call = dyn_cast<CallInst>(lhs)) { |
| 6277 | if (auto *rhs_call = dyn_cast<CallInst>(rhs)) { |
| 6278 | if (lhs_call->getCalledFunction()->getName().startswith( |
| 6279 | clspv::ResourceAccessorFunction()) && |
| 6280 | rhs_call->getCalledFunction()->getName().startswith( |
| 6281 | clspv::ResourceAccessorFunction())) { |
| 6282 | // For resource accessors, match descriptor set and binding. |
| 6283 | if (lhs_call->getOperand(0) == rhs_call->getOperand(0) && |
| 6284 | lhs_call->getOperand(1) == rhs_call->getOperand(1)) |
| 6285 | return true; |
| 6286 | } else if (lhs_call->getCalledFunction()->getName().startswith( |
| 6287 | clspv::WorkgroupAccessorFunction()) && |
| 6288 | rhs_call->getCalledFunction()->getName().startswith( |
| 6289 | clspv::WorkgroupAccessorFunction())) { |
| 6290 | // For workgroup resources, match spec id. |
| 6291 | if (lhs_call->getOperand(0) == rhs_call->getOperand(0)) |
| 6292 | return true; |
| 6293 | } |
| 6294 | } |
| 6295 | } |
| 6296 | |
| 6297 | return false; |
| 6298 | } |
| 6299 | |
| 6300 | bool SPIRVProducerPass::selectFromSameObject(Instruction *inst) { |
| 6301 | assert(inst->getType()->isPointerTy()); |
| 6302 | assert(GetStorageClass(inst->getType()->getPointerAddressSpace()) == |
| 6303 | spv::StorageClassStorageBuffer); |
| 6304 | const bool hack_undef = clspv::Option::HackUndef(); |
| 6305 | if (auto *select = dyn_cast<SelectInst>(inst)) { |
| 6306 | auto *true_base = GetBasePointer(select->getTrueValue()); |
| 6307 | auto *false_base = GetBasePointer(select->getFalseValue()); |
| 6308 | |
| 6309 | if (true_base == false_base) |
| 6310 | return true; |
| 6311 | |
| 6312 | // If either the true or false operand is a null, then we satisfy the same |
| 6313 | // object constraint. |
| 6314 | if (auto *true_cst = dyn_cast<Constant>(true_base)) { |
| 6315 | if (true_cst->isNullValue() || (hack_undef && isa<UndefValue>(true_base))) |
| 6316 | return true; |
| 6317 | } |
| 6318 | |
| 6319 | if (auto *false_cst = dyn_cast<Constant>(false_base)) { |
| 6320 | if (false_cst->isNullValue() || |
| 6321 | (hack_undef && isa<UndefValue>(false_base))) |
| 6322 | return true; |
| 6323 | } |
| 6324 | |
| 6325 | if (sameResource(true_base, false_base)) |
| 6326 | return true; |
| 6327 | } else if (auto *phi = dyn_cast<PHINode>(inst)) { |
| 6328 | Value *value = nullptr; |
| 6329 | bool ok = true; |
| 6330 | for (unsigned i = 0; ok && i != phi->getNumIncomingValues(); ++i) { |
| 6331 | auto *base = GetBasePointer(phi->getIncomingValue(i)); |
| 6332 | // Null values satisfy the constraint of selecting of selecting from the |
| 6333 | // same object. |
| 6334 | if (!value) { |
| 6335 | if (auto *cst = dyn_cast<Constant>(base)) { |
| 6336 | if (!cst->isNullValue() && !(hack_undef && isa<UndefValue>(base))) |
| 6337 | value = base; |
| 6338 | } else { |
| 6339 | value = base; |
| 6340 | } |
| 6341 | } else if (base != value) { |
| 6342 | if (auto *base_cst = dyn_cast<Constant>(base)) { |
| 6343 | if (base_cst->isNullValue() || (hack_undef && isa<UndefValue>(base))) |
| 6344 | continue; |
| 6345 | } |
| 6346 | |
| 6347 | if (sameResource(value, base)) |
| 6348 | continue; |
| 6349 | |
| 6350 | // Values don't represent the same base. |
| 6351 | ok = false; |
| 6352 | } |
| 6353 | } |
| 6354 | |
| 6355 | return ok; |
| 6356 | } |
| 6357 | |
| 6358 | // Conservatively return false. |
| 6359 | return false; |
| 6360 | } |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 6361 | |
| 6362 | bool SPIRVProducerPass::CalledWithCoherentResource(Argument &Arg) { |
| 6363 | if (!Arg.getType()->isPointerTy() || |
| 6364 | Arg.getType()->getPointerAddressSpace() != clspv::AddressSpace::Global) { |
| 6365 | // Only SSBOs need to be annotated as coherent. |
| 6366 | return false; |
| 6367 | } |
| 6368 | |
| 6369 | DenseSet<Value *> visited; |
| 6370 | std::vector<Value *> stack; |
| 6371 | for (auto *U : Arg.getParent()->users()) { |
| 6372 | if (auto *call = dyn_cast<CallInst>(U)) { |
| 6373 | stack.push_back(call->getOperand(Arg.getArgNo())); |
| 6374 | } |
| 6375 | } |
| 6376 | |
| 6377 | while (!stack.empty()) { |
| 6378 | Value *v = stack.back(); |
| 6379 | stack.pop_back(); |
| 6380 | |
| 6381 | if (!visited.insert(v).second) |
| 6382 | continue; |
| 6383 | |
| 6384 | auto *resource_call = dyn_cast<CallInst>(v); |
| 6385 | if (resource_call && |
| 6386 | resource_call->getCalledFunction()->getName().startswith( |
| 6387 | clspv::ResourceAccessorFunction())) { |
| 6388 | // If this is a resource accessor function, check if the coherent operand |
| 6389 | // is set. |
| 6390 | const auto coherent = |
| 6391 | unsigned(dyn_cast<ConstantInt>(resource_call->getArgOperand(5)) |
| 6392 | ->getZExtValue()); |
| 6393 | if (coherent == 1) |
| 6394 | return true; |
| 6395 | } else if (auto *arg = dyn_cast<Argument>(v)) { |
| 6396 | // If this is a function argument, trace through its callers. |
alan-baker | e98f3f9 | 2019-04-08 15:06:36 -0400 | [diff] [blame] | 6397 | for (auto U : arg->getParent()->users()) { |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 6398 | if (auto *call = dyn_cast<CallInst>(U)) { |
| 6399 | stack.push_back(call->getOperand(arg->getArgNo())); |
| 6400 | } |
| 6401 | } |
| 6402 | } else if (auto *user = dyn_cast<User>(v)) { |
| 6403 | // If this is a user, traverse all operands that could lead to resource |
| 6404 | // variables. |
| 6405 | for (unsigned i = 0; i != user->getNumOperands(); ++i) { |
| 6406 | Value *operand = user->getOperand(i); |
| 6407 | if (operand->getType()->isPointerTy() && |
| 6408 | operand->getType()->getPointerAddressSpace() == |
| 6409 | clspv::AddressSpace::Global) { |
| 6410 | stack.push_back(operand); |
| 6411 | } |
| 6412 | } |
| 6413 | } |
| 6414 | } |
| 6415 | |
| 6416 | // No coherent resource variables encountered. |
| 6417 | return false; |
| 6418 | } |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 6419 | |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 6420 | void SPIRVProducerPass::PopulateStructuredCFGMaps() { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 6421 | // First, track loop merges and continues. |
| 6422 | DenseSet<BasicBlock *> LoopMergesAndContinues; |
SJW | 77b87ad | 2020-04-21 14:37:52 -0500 | [diff] [blame] | 6423 | for (auto &F : *module) { |
alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame] | 6424 | if (F.isDeclaration()) |
| 6425 | continue; |
| 6426 | |
| 6427 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); |
| 6428 | const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>(F).getLoopInfo(); |
| 6429 | std::deque<BasicBlock *> order; |
| 6430 | DenseSet<BasicBlock *> visited; |
| 6431 | clspv::ComputeStructuredOrder(&*F.begin(), &DT, LI, &order, &visited); |
| 6432 | |
| 6433 | for (auto BB : order) { |
| 6434 | auto terminator = BB->getTerminator(); |
| 6435 | auto branch = dyn_cast<BranchInst>(terminator); |
| 6436 | if (LI.isLoopHeader(BB)) { |
| 6437 | auto L = LI.getLoopFor(BB); |
| 6438 | BasicBlock *ContinueBB = nullptr; |
| 6439 | BasicBlock *MergeBB = nullptr; |
| 6440 | |
| 6441 | MergeBB = L->getExitBlock(); |
| 6442 | if (!MergeBB) { |
| 6443 | // StructurizeCFG pass converts CFG into triangle shape and the cfg |
| 6444 | // has regions with single entry/exit. As a result, loop should not |
| 6445 | // have multiple exits. |
| 6446 | llvm_unreachable("Loop has multiple exits???"); |
| 6447 | } |
| 6448 | |
| 6449 | if (L->isLoopLatch(BB)) { |
| 6450 | ContinueBB = BB; |
| 6451 | } else { |
| 6452 | // From SPIR-V spec 2.11, Continue Target must dominate that back-edge |
| 6453 | // block. |
| 6454 | BasicBlock *Header = L->getHeader(); |
| 6455 | BasicBlock *Latch = L->getLoopLatch(); |
| 6456 | for (auto *loop_block : L->blocks()) { |
| 6457 | if (loop_block == Header) { |
| 6458 | continue; |
| 6459 | } |
| 6460 | |
| 6461 | // Check whether block dominates block with back-edge. |
| 6462 | // The loop latch is the single block with a back-edge. If it was |
| 6463 | // possible, StructurizeCFG made the loop conform to this |
| 6464 | // requirement, otherwise |Latch| is a nullptr. |
| 6465 | if (DT.dominates(loop_block, Latch)) { |
| 6466 | ContinueBB = loop_block; |
| 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | if (!ContinueBB) { |
| 6471 | llvm_unreachable("Wrong continue block from loop"); |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | // Record the continue and merge blocks. |
| 6476 | MergeBlocks[BB] = MergeBB; |
| 6477 | ContinueBlocks[BB] = ContinueBB; |
| 6478 | LoopMergesAndContinues.insert(MergeBB); |
| 6479 | LoopMergesAndContinues.insert(ContinueBB); |
| 6480 | } else if (branch && branch->isConditional()) { |
| 6481 | auto L = LI.getLoopFor(BB); |
| 6482 | bool HasBackedge = false; |
| 6483 | while (L && !HasBackedge) { |
| 6484 | if (L->isLoopLatch(BB)) { |
| 6485 | HasBackedge = true; |
| 6486 | } |
| 6487 | L = L->getParentLoop(); |
| 6488 | } |
| 6489 | |
| 6490 | if (!HasBackedge) { |
| 6491 | // Only need a merge if the branch doesn't include a loop break or |
| 6492 | // continue. |
| 6493 | auto true_bb = branch->getSuccessor(0); |
| 6494 | auto false_bb = branch->getSuccessor(1); |
| 6495 | if (!LoopMergesAndContinues.count(true_bb) && |
| 6496 | !LoopMergesAndContinues.count(false_bb)) { |
| 6497 | // StructurizeCFG pass already manipulated CFG. Just use false block |
| 6498 | // of branch instruction as merge block. |
| 6499 | MergeBlocks[BB] = false_bb; |
| 6500 | } |
| 6501 | } |
| 6502 | } |
| 6503 | } |
| 6504 | } |
| 6505 | } |