Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader 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 | #ifndef sw_SpirvShader_hpp |
| 16 | #define sw_SpirvShader_hpp |
| 17 | |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 18 | #include "ShaderCore.hpp" |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 19 | #include "SpirvID.hpp" |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 20 | #include "System/Types.hpp" |
| 21 | #include "Vulkan/VkDebug.hpp" |
| 22 | #include "Vulkan/VkConfig.h" |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 23 | #include "Device/Config.hpp" |
| 24 | |
| 25 | #include <spirv/unified1/spirv.hpp> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 26 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 27 | #include <array> |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 28 | #include <cstring> |
Ben Clayton | 49d8158 | 2019-03-12 20:05:04 +0000 | [diff] [blame] | 29 | #include <functional> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 32 | #include <unordered_set> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 33 | #include <unordered_map> |
| 34 | #include <cstdint> |
Chris Forbes | ef4ab0f | 2019-01-18 08:11:03 -0800 | [diff] [blame] | 35 | #include <type_traits> |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 36 | #include <memory> |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 37 | #include <queue> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 38 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 39 | namespace vk |
| 40 | { |
| 41 | class PipelineLayout; |
| 42 | } // namespace vk |
| 43 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 44 | namespace sw |
| 45 | { |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 46 | // Forward declarations. |
| 47 | class SpirvRoutine; |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 48 | class GenericValue; |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 49 | |
| 50 | // SIMD contains types that represent multiple scalars packed into a single |
| 51 | // vector data type. Types in the SIMD namespace provide a semantic hint |
| 52 | // that the data should be treated as a per-execution-lane scalar instead of |
| 53 | // a typical euclidean-style vector type. |
| 54 | namespace SIMD |
| 55 | { |
| 56 | // Width is the number of per-lane scalars packed into each SIMD vector. |
| 57 | static constexpr int Width = 4; |
| 58 | |
| 59 | using Float = rr::Float4; |
| 60 | using Int = rr::Int4; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 61 | using UInt = rr::UInt4; |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 62 | |
| 63 | struct Pointer |
| 64 | { |
| 65 | Pointer(rr::Pointer<Byte> base) : base(base), offset(0), uniform(true) {} |
| 66 | Pointer(rr::Pointer<Byte> base, SIMD::Int offset) : base(base), offset(offset), uniform(false) {} |
| 67 | |
| 68 | // Base address for the pointer, common across all lanes. |
| 69 | rr::Pointer<rr::Float> base; |
| 70 | |
| 71 | // Per lane offsets from base. |
| 72 | // If uniform is false, all offsets are considered zero. |
| 73 | Int offset; |
| 74 | |
| 75 | // True if all offsets are zero. |
| 76 | bool uniform; |
| 77 | }; |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 80 | // Incrementally constructed complex bundle of rvalues |
| 81 | // Effectively a restricted vector, supporting only: |
| 82 | // - allocation to a (runtime-known) fixed size |
| 83 | // - in-place construction of elements |
| 84 | // - const operator[] |
| 85 | class Intermediate |
| 86 | { |
| 87 | public: |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 88 | Intermediate(uint32_t size) : scalar(new rr::Value*[size]), size(size) { |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 89 | memset(scalar, 0, sizeof(rr::Value*) * size); |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 90 | } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 91 | |
| 92 | ~Intermediate() |
| 93 | { |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 94 | delete[] scalar; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 97 | void move(uint32_t i, RValue<SIMD::Float> &&scalar) { emplace(i, scalar.value); } |
| 98 | void move(uint32_t i, RValue<SIMD::Int> &&scalar) { emplace(i, scalar.value); } |
| 99 | void move(uint32_t i, RValue<SIMD::UInt> &&scalar) { emplace(i, scalar.value); } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 100 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 101 | void move(uint32_t i, const RValue<SIMD::Float> &scalar) { emplace(i, scalar.value); } |
| 102 | void move(uint32_t i, const RValue<SIMD::Int> &scalar) { emplace(i, scalar.value); } |
| 103 | void move(uint32_t i, const RValue<SIMD::UInt> &scalar) { emplace(i, scalar.value); } |
Ben Clayton | 093be46 | 2019-03-08 08:37:24 +0000 | [diff] [blame] | 104 | |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 105 | // Value retrieval functions. |
| 106 | RValue<SIMD::Float> Float(uint32_t i) const |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 107 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 108 | ASSERT(i < size); |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 109 | ASSERT(scalar[i] != nullptr); |
| 110 | return As<SIMD::Float>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Float>(scalar) |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 111 | } |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 112 | |
| 113 | RValue<SIMD::Int> Int(uint32_t i) const |
| 114 | { |
| 115 | ASSERT(i < size); |
| 116 | ASSERT(scalar[i] != nullptr); |
| 117 | return As<SIMD::Int>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Int>(scalar) |
| 118 | } |
| 119 | |
| 120 | RValue<SIMD::UInt> UInt(uint32_t i) const |
| 121 | { |
| 122 | ASSERT(i < size); |
| 123 | ASSERT(scalar[i] != nullptr); |
| 124 | return As<SIMD::UInt>(scalar[i]); // TODO(b/128539387): RValue<SIMD::UInt>(scalar) |
| 125 | } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 126 | |
| 127 | // No copy/move construction or assignment |
| 128 | Intermediate(Intermediate const &) = delete; |
| 129 | Intermediate(Intermediate &&) = delete; |
| 130 | Intermediate & operator=(Intermediate const &) = delete; |
| 131 | Intermediate & operator=(Intermediate &&) = delete; |
| 132 | |
| 133 | private: |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 134 | void emplace(uint32_t i, rr::Value *value) |
| 135 | { |
| 136 | ASSERT(i < size); |
| 137 | ASSERT(scalar[i] == nullptr); |
| 138 | scalar[i] = value; |
| 139 | } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 140 | |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 141 | rr::Value **const scalar; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 142 | uint32_t size; |
| 143 | }; |
| 144 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 145 | class SpirvShader |
| 146 | { |
| 147 | public: |
| 148 | using InsnStore = std::vector<uint32_t>; |
| 149 | InsnStore insns; |
| 150 | |
| 151 | /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */ |
| 152 | class InsnIterator |
| 153 | { |
| 154 | InsnStore::const_iterator iter; |
| 155 | |
| 156 | public: |
| 157 | spv::Op opcode() const |
| 158 | { |
| 159 | return static_cast<spv::Op>(*iter & spv::OpCodeMask); |
| 160 | } |
| 161 | |
| 162 | uint32_t wordCount() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 163 | { |
| 164 | return *iter >> spv::WordCountShift; |
| 165 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 166 | |
| 167 | uint32_t word(uint32_t n) const |
| 168 | { |
| 169 | ASSERT(n < wordCount()); |
| 170 | return iter[n]; |
| 171 | } |
| 172 | |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 173 | uint32_t const * wordPointer(uint32_t n) const |
| 174 | { |
| 175 | ASSERT(n < wordCount()); |
| 176 | return &iter[n]; |
| 177 | } |
| 178 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 179 | bool operator==(InsnIterator const &other) const |
| 180 | { |
| 181 | return iter == other.iter; |
| 182 | } |
| 183 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 184 | bool operator!=(InsnIterator const &other) const |
| 185 | { |
| 186 | return iter != other.iter; |
| 187 | } |
| 188 | |
| 189 | InsnIterator operator*() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 190 | { |
| 191 | return *this; |
| 192 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 193 | |
| 194 | InsnIterator &operator++() |
| 195 | { |
| 196 | iter += wordCount(); |
| 197 | return *this; |
| 198 | } |
| 199 | |
| 200 | InsnIterator const operator++(int) |
| 201 | { |
| 202 | InsnIterator ret{*this}; |
| 203 | iter += wordCount(); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | InsnIterator(InsnIterator const &other) = default; |
| 208 | |
| 209 | InsnIterator() = default; |
| 210 | |
| 211 | explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter} |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 212 | { |
| 213 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | /* range-based-for interface */ |
| 217 | InsnIterator begin() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 218 | { |
| 219 | return InsnIterator{insns.cbegin() + 5}; |
| 220 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 221 | |
| 222 | InsnIterator end() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 223 | { |
| 224 | return InsnIterator{insns.cend()}; |
| 225 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 226 | |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 227 | class Type |
| 228 | { |
| 229 | public: |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 230 | using ID = SpirvID<Type>; |
| 231 | |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 232 | spv::Op opcode() const { return definition.opcode(); } |
| 233 | |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 234 | InsnIterator definition; |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 235 | spv::StorageClass storageClass = static_cast<spv::StorageClass>(-1); |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 236 | uint32_t sizeInComponents = 0; |
| 237 | bool isBuiltInBlock = false; |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 238 | |
| 239 | // Inner element type for pointers, arrays, vectors and matrices. |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 240 | ID element; |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 243 | class Object |
| 244 | { |
| 245 | public: |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 246 | using ID = SpirvID<Object>; |
| 247 | |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 248 | spv::Op opcode() const { return definition.opcode(); } |
| 249 | |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 250 | InsnIterator definition; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 251 | Type::ID type; |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 252 | std::unique_ptr<uint32_t[]> constantValue = nullptr; |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 253 | |
| 254 | enum class Kind |
| 255 | { |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 256 | // Invalid default kind. |
| 257 | // If we get left with an object in this state, the module was |
| 258 | // broken. |
| 259 | Unknown, |
| 260 | |
| 261 | // TODO: Better document this kind. |
| 262 | // A shader interface variable pointer. |
| 263 | // Pointer with uniform address across all lanes. |
| 264 | // Pointer held by SpirvRoutine::pointers |
| 265 | InterfaceVariable, |
| 266 | |
| 267 | // Constant value held by Object::constantValue. |
| 268 | Constant, |
| 269 | |
| 270 | // Value held by SpirvRoutine::intermediates. |
| 271 | Intermediate, |
| 272 | |
| 273 | // DivergentPointer formed from a base pointer and per-lane offset. |
| 274 | // Base pointer held by SpirvRoutine::pointers |
| 275 | // Per-lane offset held by SpirvRoutine::intermediates. |
| 276 | DivergentPointer, |
| 277 | |
| 278 | // Pointer with uniform address across all lanes. |
| 279 | // Pointer held by SpirvRoutine::pointers |
| 280 | NonDivergentPointer, |
| 281 | |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 282 | // A pointer to a vk::DescriptorSet*. |
| 283 | // Pointer held by SpirvRoutine::pointers. |
| 284 | DescriptorSet, |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 285 | }; |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 286 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 287 | Kind kind = Kind::Unknown; |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 288 | }; |
| 289 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 290 | // Block is an interval of SPIR-V instructions, starting with the |
| 291 | // opening OpLabel, and ending with a termination instruction. |
| 292 | class Block |
| 293 | { |
| 294 | public: |
| 295 | using ID = SpirvID<Block>; |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 296 | using Set = std::unordered_set<ID>; |
| 297 | |
| 298 | // Edge represents the graph edge between two blocks. |
| 299 | struct Edge |
| 300 | { |
| 301 | ID from; |
| 302 | ID to; |
| 303 | |
| 304 | bool operator == (const Edge& other) const { return from == other.from && to == other.to; } |
| 305 | |
| 306 | struct Hash |
| 307 | { |
| 308 | std::size_t operator()(const Edge& edge) const noexcept |
| 309 | { |
| 310 | return std::hash<uint32_t>()(edge.from.value() * 31 + edge.to.value()); |
| 311 | } |
| 312 | }; |
| 313 | }; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 314 | |
| 315 | Block() = default; |
| 316 | Block(const Block& other) = default; |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 317 | explicit Block(InsnIterator begin, InsnIterator end); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 318 | |
| 319 | /* range-based-for interface */ |
| 320 | inline InsnIterator begin() const { return begin_; } |
| 321 | inline InsnIterator end() const { return end_; } |
| 322 | |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 323 | enum Kind |
| 324 | { |
| 325 | Simple, // OpBranch or other simple terminator. |
| 326 | StructuredBranchConditional, // OpSelectionMerge + OpBranchConditional |
| 327 | UnstructuredBranchConditional, // OpBranchConditional |
| 328 | StructuredSwitch, // OpSelectionMerge + OpSwitch |
| 329 | UnstructuredSwitch, // OpSwitch |
| 330 | Loop, // OpLoopMerge + [OpBranchConditional | OpBranch] |
| 331 | }; |
| 332 | |
| 333 | Kind kind; |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 334 | InsnIterator mergeInstruction; // Structured control flow merge instruction. |
| 335 | InsnIterator branchInstruction; // Branch instruction. |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 336 | ID mergeBlock; // Structured flow merge block. |
| 337 | ID continueTarget; // Loop continue block. |
| 338 | Set ins; // Blocks that branch into this block. |
| 339 | Set outs; // Blocks that this block branches to. |
| 340 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 341 | private: |
| 342 | InsnIterator begin_; |
| 343 | InsnIterator end_; |
| 344 | }; |
| 345 | |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 346 | struct TypeOrObject {}; // Dummy struct to represent a Type or Object. |
| 347 | |
| 348 | // TypeOrObjectID is an identifier that represents a Type or an Object, |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 349 | // and supports implicit casting to and from Type::ID or Object::ID. |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 350 | class TypeOrObjectID : public SpirvID<TypeOrObject> |
| 351 | { |
| 352 | public: |
| 353 | using Hash = std::hash<SpirvID<TypeOrObject>>; |
| 354 | |
| 355 | inline TypeOrObjectID(uint32_t id) : SpirvID(id) {} |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 356 | inline TypeOrObjectID(Type::ID id) : SpirvID(id.value()) {} |
| 357 | inline TypeOrObjectID(Object::ID id) : SpirvID(id.value()) {} |
| 358 | inline operator Type::ID() const { return Type::ID(value()); } |
| 359 | inline operator Object::ID() const { return Object::ID(value()); } |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 360 | }; |
| 361 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 362 | int getSerialID() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 363 | { |
| 364 | return serialID; |
| 365 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 366 | |
| 367 | explicit SpirvShader(InsnStore const &insns); |
| 368 | |
| 369 | struct Modes |
| 370 | { |
| 371 | bool EarlyFragmentTests : 1; |
| 372 | bool DepthReplacing : 1; |
| 373 | bool DepthGreater : 1; |
| 374 | bool DepthLess : 1; |
| 375 | bool DepthUnchanged : 1; |
Chris Forbes | 8b0a281 | 2019-01-17 10:10:09 -0800 | [diff] [blame] | 376 | bool ContainsKill : 1; |
Chris Forbes | 93f70b3 | 2019-02-10 21:26:27 +0000 | [diff] [blame] | 377 | bool NeedsCentroid : 1; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 378 | |
| 379 | // Compute workgroup dimensions |
Ben Clayton | 62758f5 | 2019-03-13 14:18:58 +0000 | [diff] [blame] | 380 | int WorkgroupSizeX = 1, WorkgroupSizeY = 1, WorkgroupSizeZ = 1; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 381 | }; |
| 382 | |
| 383 | Modes const &getModes() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 384 | { |
| 385 | return modes; |
| 386 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 387 | |
Chris Forbes | 2e7f35b | 2019-01-17 09:51:39 -0800 | [diff] [blame] | 388 | enum AttribType : unsigned char |
| 389 | { |
| 390 | ATTRIBTYPE_FLOAT, |
| 391 | ATTRIBTYPE_INT, |
| 392 | ATTRIBTYPE_UINT, |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 393 | ATTRIBTYPE_UNUSED, |
Chris Forbes | 2e7f35b | 2019-01-17 09:51:39 -0800 | [diff] [blame] | 394 | |
| 395 | ATTRIBTYPE_LAST = ATTRIBTYPE_UINT |
| 396 | }; |
| 397 | |
Chris Forbes | 8b0a281 | 2019-01-17 10:10:09 -0800 | [diff] [blame] | 398 | bool hasBuiltinInput(spv::BuiltIn b) const |
| 399 | { |
| 400 | return inputBuiltins.find(b) != inputBuiltins.end(); |
| 401 | } |
| 402 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 403 | struct Decorations |
| 404 | { |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 405 | int32_t Location = -1; |
| 406 | int32_t Component = 0; |
| 407 | spv::BuiltIn BuiltIn = static_cast<spv::BuiltIn>(-1); |
| 408 | int32_t Offset = -1; |
| 409 | int32_t ArrayStride = -1; |
| 410 | int32_t MatrixStride = 1; |
| 411 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 412 | bool HasLocation : 1; |
| 413 | bool HasComponent : 1; |
| 414 | bool HasBuiltIn : 1; |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 415 | bool HasOffset : 1; |
| 416 | bool HasArrayStride : 1; |
| 417 | bool HasMatrixStride : 1; |
| 418 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 419 | bool Flat : 1; |
| 420 | bool Centroid : 1; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 421 | bool NoPerspective : 1; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 422 | bool Block : 1; |
| 423 | bool BufferBlock : 1; |
Ben Clayton | 8448cc5 | 2019-04-09 16:24:31 -0400 | [diff] [blame] | 424 | bool RelaxedPrecision : 1; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 425 | |
| 426 | Decorations() |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 427 | : Location{-1}, Component{0}, |
Ben Clayton | d073d8e | 2019-02-26 11:06:50 +0000 | [diff] [blame] | 428 | BuiltIn{static_cast<spv::BuiltIn>(-1)}, |
Chris Forbes | 6532107 | 2019-03-07 16:13:56 -0800 | [diff] [blame] | 429 | Offset{-1}, ArrayStride{-1}, MatrixStride{-1}, |
Ben Clayton | d073d8e | 2019-02-26 11:06:50 +0000 | [diff] [blame] | 430 | HasLocation{false}, HasComponent{false}, |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 431 | HasBuiltIn{false}, HasOffset{false}, |
| 432 | HasArrayStride{false}, HasMatrixStride{false}, |
| 433 | Flat{false}, Centroid{false}, NoPerspective{false}, |
| 434 | Block{false}, BufferBlock{false}, |
Ben Clayton | 8448cc5 | 2019-04-09 16:24:31 -0400 | [diff] [blame] | 435 | RelaxedPrecision{false} |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 436 | { |
| 437 | } |
| 438 | |
| 439 | Decorations(Decorations const &) = default; |
| 440 | |
| 441 | void Apply(Decorations const &src); |
| 442 | |
| 443 | void Apply(spv::Decoration decoration, uint32_t arg); |
| 444 | }; |
| 445 | |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 446 | std::unordered_map<TypeOrObjectID, Decorations, TypeOrObjectID::Hash> decorations; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 447 | std::unordered_map<Type::ID, std::vector<Decorations>> memberDecorations; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 448 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 449 | struct DescriptorDecorations |
| 450 | { |
| 451 | int32_t DescriptorSet = -1; |
| 452 | int32_t Binding = -1; |
| 453 | |
| 454 | void Apply(DescriptorDecorations const &src); |
| 455 | }; |
| 456 | |
| 457 | std::unordered_map<Object::ID, DescriptorDecorations> descriptorDecorations; |
| 458 | |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 459 | struct InterfaceComponent |
| 460 | { |
| 461 | AttribType Type; |
| 462 | bool Flat : 1; |
| 463 | bool Centroid : 1; |
| 464 | bool NoPerspective : 1; |
| 465 | |
| 466 | InterfaceComponent() |
| 467 | : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false} |
| 468 | { |
| 469 | } |
| 470 | }; |
| 471 | |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 472 | struct BuiltinMapping |
| 473 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 474 | Object::ID Id; |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 475 | uint32_t FirstComponent; |
| 476 | uint32_t SizeInComponents; |
| 477 | }; |
| 478 | |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 479 | std::vector<InterfaceComponent> inputs; |
| 480 | std::vector<InterfaceComponent> outputs; |
| 481 | |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 482 | void emitProlog(SpirvRoutine *routine) const; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 483 | void emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask) const; |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 484 | void emitEpilog(SpirvRoutine *routine) const; |
Chris Forbes | ef4ab0f | 2019-01-18 08:11:03 -0800 | [diff] [blame] | 485 | |
| 486 | using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>; |
| 487 | std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins; |
| 488 | std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 489 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 490 | Type const &getType(Type::ID id) const |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 491 | { |
| 492 | auto it = types.find(id); |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 493 | ASSERT_MSG(it != types.end(), "Unknown type %d", id.value()); |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 494 | return it->second; |
| 495 | } |
| 496 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 497 | Object const &getObject(Object::ID id) const |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 498 | { |
Chris Forbes | 1c65823 | 2019-02-01 17:12:25 -0800 | [diff] [blame] | 499 | auto it = defs.find(id); |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 500 | ASSERT_MSG(it != defs.end(), "Unknown object %d", id.value()); |
Chris Forbes | 1c65823 | 2019-02-01 17:12:25 -0800 | [diff] [blame] | 501 | return it->second; |
| 502 | } |
| 503 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 504 | Block const &getBlock(Block::ID id) const |
| 505 | { |
| 506 | auto it = blocks.find(id); |
Ben Clayton | af26cfe | 2019-03-21 17:32:44 +0000 | [diff] [blame] | 507 | ASSERT_MSG(it != blocks.end(), "Unknown block %d", id.value()); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 508 | return it->second; |
| 509 | } |
| 510 | |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 511 | private: |
| 512 | const int serialID; |
| 513 | static volatile int serialCounter; |
| 514 | Modes modes; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 515 | HandleMap<Type> types; |
| 516 | HandleMap<Object> defs; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 517 | HandleMap<Block> blocks; |
| 518 | Block::ID mainBlockId; // Block of the entry point function. |
| 519 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 520 | // Walks all reachable the blocks starting from id adding them to |
| 521 | // reachable. |
| 522 | void TraverseReachableBlocks(Block::ID id, Block::Set& reachable); |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 523 | |
| 524 | // Assigns Block::ins from Block::outs for every block. |
| 525 | void AssignBlockIns(); |
| 526 | |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 527 | // DeclareType creates a Type for the given OpTypeX instruction, storing |
| 528 | // it into the types map. It is called from the analysis pass (constructor). |
| 529 | void DeclareType(InsnIterator insn); |
| 530 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 531 | void ProcessExecutionMode(InsnIterator it); |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 532 | |
| 533 | uint32_t ComputeTypeSize(InsnIterator insn); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 534 | void ApplyDecorationsForId(Decorations *d, TypeOrObjectID id) const; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 535 | void ApplyDecorationsForIdMember(Decorations *d, Type::ID id, uint32_t member) const; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 536 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame^] | 537 | // Creates an Object for the instruction's result in 'defs'. |
| 538 | void DefineResult(const InsnIterator &insn); |
| 539 | |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 540 | // Returns true if data in the given storage class is word-interleaved |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 541 | // by each SIMD vector lane, otherwise data is stored linerally. |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 542 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 543 | // Each lane addresses a single word, picked by a base pointer and an |
| 544 | // integer offset. |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 545 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 546 | // A word is currently 32 bits (single float, int32_t, uint32_t). |
| 547 | // A lane is a single element of a SIMD vector register. |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 548 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 549 | // Storage interleaved by lane - (IsStorageInterleavedByLane() == true): |
| 550 | // --------------------------------------------------------------------- |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 551 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 552 | // Address = PtrBase + sizeof(Word) * (SIMD::Width * LaneOffset + LaneIndex) |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 553 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 554 | // Assuming SIMD::Width == 4: |
| 555 | // |
| 556 | // Lane[0] | Lane[1] | Lane[2] | Lane[3] |
| 557 | // ===========+===========+===========+========== |
| 558 | // LaneOffset=0: | Word[0] | Word[1] | Word[2] | Word[3] |
| 559 | // ---------------+-----------+-----------+-----------+---------- |
| 560 | // LaneOffset=1: | Word[4] | Word[5] | Word[6] | Word[7] |
| 561 | // ---------------+-----------+-----------+-----------+---------- |
| 562 | // LaneOffset=2: | Word[8] | Word[9] | Word[a] | Word[b] |
| 563 | // ---------------+-----------+-----------+-----------+---------- |
| 564 | // LaneOffset=3: | Word[c] | Word[d] | Word[e] | Word[f] |
| 565 | // |
| 566 | // |
| 567 | // Linear storage - (IsStorageInterleavedByLane() == false): |
| 568 | // --------------------------------------------------------- |
| 569 | // |
| 570 | // Address = PtrBase + sizeof(Word) * LaneOffset |
| 571 | // |
| 572 | // Lane[0] | Lane[1] | Lane[2] | Lane[3] |
| 573 | // ===========+===========+===========+========== |
| 574 | // LaneOffset=0: | Word[0] | Word[0] | Word[0] | Word[0] |
| 575 | // ---------------+-----------+-----------+-----------+---------- |
| 576 | // LaneOffset=1: | Word[1] | Word[1] | Word[1] | Word[1] |
| 577 | // ---------------+-----------+-----------+-----------+---------- |
| 578 | // LaneOffset=2: | Word[2] | Word[2] | Word[2] | Word[2] |
| 579 | // ---------------+-----------+-----------+-----------+---------- |
| 580 | // LaneOffset=3: | Word[3] | Word[3] | Word[3] | Word[3] |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 581 | // |
| 582 | static bool IsStorageInterleavedByLane(spv::StorageClass storageClass); |
| 583 | |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 584 | template<typename F> |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 585 | int VisitInterfaceInner(Type::ID id, Decorations d, F f) const; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 586 | |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 587 | template<typename F> |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 588 | void VisitInterface(Object::ID id, F f) const; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 589 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 590 | uint32_t GetConstantInt(Object::ID id) const; |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 591 | Object& CreateConstant(InsnIterator it); |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 592 | |
Chris Forbes | 049ff38 | 2019-02-02 15:16:43 -0800 | [diff] [blame] | 593 | void ProcessInterfaceVariable(Object &object); |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 594 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 595 | // Returns a SIMD::Pointer to the underlying data for the given pointer |
| 596 | // object. |
| 597 | // Handles objects of the following kinds: |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 598 | // • DescriptorSet |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 599 | // • DivergentPointer |
| 600 | // • InterfaceVariable |
| 601 | // • NonDivergentPointer |
| 602 | // Calling GetPointerToData with objects of any other kind will assert. |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 603 | SIMD::Pointer GetPointerToData(Object::ID id, int arrayIndex, SpirvRoutine *routine) const; |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 604 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 605 | SIMD::Pointer WalkExplicitLayoutAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 606 | SIMD::Int WalkAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const; |
| 607 | uint32_t WalkLiteralAccessChain(Type::ID id, uint32_t numIndexes, uint32_t const *indexes) const; |
Ben Clayton | d4e4c66 | 2019-02-26 11:54:34 +0000 | [diff] [blame] | 608 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 609 | // EmitState holds control-flow state for the emit() pass. |
| 610 | class EmitState |
| 611 | { |
| 612 | public: |
| 613 | RValue<SIMD::Int> activeLaneMask() const |
| 614 | { |
| 615 | ASSERT(activeLaneMaskValue != nullptr); |
| 616 | return RValue<SIMD::Int>(activeLaneMaskValue); |
| 617 | } |
| 618 | |
| 619 | void setActiveLaneMask(RValue<SIMD::Int> mask) |
| 620 | { |
| 621 | activeLaneMaskValue = mask.value; |
| 622 | } |
| 623 | |
| 624 | // Add a new active lane mask edge from the current block to out. |
| 625 | // The edge mask value will be (mask AND activeLaneMaskValue). |
| 626 | // If multiple active lane masks are added for the same edge, then |
| 627 | // they will be ORed together. |
| 628 | void addOutputActiveLaneMaskEdge(Block::ID out, RValue<SIMD::Int> mask); |
| 629 | |
| 630 | // Add a new active lane mask for the edge from -> to. |
| 631 | // If multiple active lane masks are added for the same edge, then |
| 632 | // they will be ORed together. |
| 633 | void addActiveLaneMaskEdge(Block::ID from, Block::ID to, RValue<SIMD::Int> mask); |
| 634 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 635 | SpirvRoutine *routine = nullptr; // The current routine being built. |
| 636 | rr::Value *activeLaneMaskValue = nullptr; // The current active lane mask. |
| 637 | Block::ID currentBlock; // The current block being built. |
| 638 | Block::Set visited; // Blocks already built. |
| 639 | std::unordered_map<Block::Edge, RValue<SIMD::Int>, Block::Edge::Hash> edgeActiveLaneMasks; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 640 | std::queue<Block::ID> *pending; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | // EmitResult is an enumerator of result values from the Emit functions. |
| 644 | enum class EmitResult |
| 645 | { |
| 646 | Continue, // No termination instructions. |
| 647 | Terminator, // Reached a termination instruction. |
| 648 | }; |
| 649 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 650 | // existsPath returns true if there's a direct or indirect flow from |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 651 | // the 'from' block to the 'to' block that does not pass through |
| 652 | // notPassingThrough. |
| 653 | bool existsPath(Block::ID from, Block::ID to, Block::ID notPassingThrough) const; |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 654 | |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 655 | // Lookup the active lane mask for the edge from -> to. |
| 656 | // If from is unreachable, then a mask of all zeros is returned. |
| 657 | // Asserts if from is reachable and the edge does not exist. |
| 658 | RValue<SIMD::Int> GetActiveLaneMaskEdge(EmitState *state, Block::ID from, Block::ID to) const; |
| 659 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 660 | // Emit all the unvisited blocks (except for ignore) in BFS order, |
| 661 | // starting with id. |
| 662 | void EmitBlocks(Block::ID id, EmitState *state, Block::ID ignore = 0) const; |
| 663 | void EmitNonLoop(EmitState *state) const; |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 664 | void EmitLoop(EmitState *state) const; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 665 | |
| 666 | void EmitInstructions(InsnIterator begin, InsnIterator end, EmitState *state) const; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 667 | EmitResult EmitInstruction(InsnIterator insn, EmitState *state) const; |
| 668 | |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 669 | // Emit pass instructions: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 670 | EmitResult EmitVariable(InsnIterator insn, EmitState *state) const; |
| 671 | EmitResult EmitLoad(InsnIterator insn, EmitState *state) const; |
| 672 | EmitResult EmitStore(InsnIterator insn, EmitState *state) const; |
| 673 | EmitResult EmitAccessChain(InsnIterator insn, EmitState *state) const; |
| 674 | EmitResult EmitCompositeConstruct(InsnIterator insn, EmitState *state) const; |
| 675 | EmitResult EmitCompositeInsert(InsnIterator insn, EmitState *state) const; |
| 676 | EmitResult EmitCompositeExtract(InsnIterator insn, EmitState *state) const; |
| 677 | EmitResult EmitVectorShuffle(InsnIterator insn, EmitState *state) const; |
| 678 | EmitResult EmitVectorTimesScalar(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 06f4ed7 | 2019-03-28 09:53:20 +1300 | [diff] [blame] | 679 | EmitResult EmitMatrixTimesVector(InsnIterator insn, EmitState *state) const; |
Chris Forbes | a563dd8 | 2019-03-28 10:32:55 +1300 | [diff] [blame] | 680 | EmitResult EmitVectorTimesMatrix(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 51562f1 | 2019-03-28 19:08:39 -0700 | [diff] [blame] | 681 | EmitResult EmitMatrixTimesMatrix(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 3ee5299 | 2019-04-08 11:01:23 -0400 | [diff] [blame] | 682 | EmitResult EmitOuterProduct(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 620f708 | 2019-04-08 11:12:08 -0400 | [diff] [blame] | 683 | EmitResult EmitTranspose(InsnIterator insn, EmitState *state) const; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 684 | EmitResult EmitVectorExtractDynamic(InsnIterator insn, EmitState *state) const; |
| 685 | EmitResult EmitVectorInsertDynamic(InsnIterator insn, EmitState *state) const; |
| 686 | EmitResult EmitUnaryOp(InsnIterator insn, EmitState *state) const; |
| 687 | EmitResult EmitBinaryOp(InsnIterator insn, EmitState *state) const; |
| 688 | EmitResult EmitDot(InsnIterator insn, EmitState *state) const; |
| 689 | EmitResult EmitSelect(InsnIterator insn, EmitState *state) const; |
| 690 | EmitResult EmitExtendedInstruction(InsnIterator insn, EmitState *state) const; |
| 691 | EmitResult EmitAny(InsnIterator insn, EmitState *state) const; |
| 692 | EmitResult EmitAll(InsnIterator insn, EmitState *state) const; |
| 693 | EmitResult EmitBranch(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 694 | EmitResult EmitBranchConditional(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 213a8ce | 2019-03-21 18:57:23 +0000 | [diff] [blame] | 695 | EmitResult EmitSwitch(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 696 | EmitResult EmitUnreachable(InsnIterator insn, EmitState *state) const; |
| 697 | EmitResult EmitReturn(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 97e9589 | 2019-04-02 13:37:37 +1300 | [diff] [blame] | 698 | EmitResult EmitKill(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 699 | EmitResult EmitPhi(InsnIterator insn, EmitState *state) const; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 700 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 701 | // OpcodeName() returns the name of the opcode op. |
| 702 | // If NDEBUG is defined, then OpcodeName() will only return the numerical code. |
Ben Clayton | d4e4c66 | 2019-02-26 11:54:34 +0000 | [diff] [blame] | 703 | static std::string OpcodeName(spv::Op op); |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 704 | static std::memory_order MemoryOrder(spv::MemorySemanticsMask memorySemantics); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 705 | |
| 706 | // Helper as we often need to take dot products as part of doing other things. |
| 707 | SIMD::Float Dot(unsigned numComponents, GenericValue const & x, GenericValue const & y) const; |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 708 | |
Chris Forbes | 50e6493 | 2019-04-08 17:49:27 -0700 | [diff] [blame] | 709 | SIMD::UInt FloatToHalfBits(SIMD::UInt floatBits, bool storeInUpperBits) const; |
| 710 | SIMD::UInt HalfToFloatBits(SIMD::UInt halfBits) const; |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 711 | |
| 712 | // Splits x into a floating-point significand in the range [0.5, 1.0) |
| 713 | // and an integral exponent of two, such that: |
| 714 | // x = significand * 2^exponent |
| 715 | // Returns the pair <significand, exponent> |
| 716 | std::pair<SIMD::Float, SIMD::Int> Frexp(RValue<SIMD::Float> val) const; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 717 | }; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 718 | |
| 719 | class SpirvRoutine |
| 720 | { |
| 721 | public: |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 722 | SpirvRoutine(vk::PipelineLayout const *pipelineLayout); |
| 723 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 724 | using Variable = Array<SIMD::Float>; |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 725 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 726 | vk::PipelineLayout const * const pipelineLayout; |
| 727 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 728 | std::unordered_map<SpirvShader::Object::ID, Variable> variables; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 729 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 730 | std::unordered_map<SpirvShader::Object::ID, Intermediate> intermediates; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 731 | |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 732 | std::unordered_map<SpirvShader::Object::ID, Pointer<Byte> > pointers; |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 733 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 734 | Variable inputs = Variable{MAX_INTERFACE_COMPONENTS}; |
| 735 | Variable outputs = Variable{MAX_INTERFACE_COMPONENTS}; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 736 | |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 737 | Pointer<Pointer<Byte>> descriptorSets; |
| 738 | Pointer<Int> descriptorDynamicOffsets; |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 739 | Pointer<Byte> pushConstants; |
Chris Forbes | 97e9589 | 2019-04-02 13:37:37 +1300 | [diff] [blame] | 740 | Int killMask = Int{0}; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 741 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 742 | void createVariable(SpirvShader::Object::ID id, uint32_t size) |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 743 | { |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 744 | bool added = variables.emplace(id, Variable(size)).second; |
| 745 | ASSERT_MSG(added, "Variable %d created twice", id.value()); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 748 | template <typename T> |
| 749 | void createPointer(SpirvShader::Object::ID id, Pointer<T> ptrBase) |
| 750 | { |
| 751 | bool added = pointers.emplace(id, ptrBase).second; |
| 752 | ASSERT_MSG(added, "Pointer %d created twice", id.value()); |
| 753 | } |
| 754 | |
| 755 | template <typename T> |
| 756 | void createPointer(SpirvShader::Object::ID id, RValue<Pointer<T>> ptrBase) |
| 757 | { |
| 758 | createPointer(id, Pointer<T>(ptrBase)); |
| 759 | } |
| 760 | |
| 761 | template <typename T> |
| 762 | void createPointer(SpirvShader::Object::ID id, Reference<Pointer<T>> ptrBase) |
| 763 | { |
| 764 | createPointer(id, Pointer<T>(ptrBase)); |
| 765 | } |
| 766 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 767 | Intermediate& createIntermediate(SpirvShader::Object::ID id, uint32_t size) |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 768 | { |
Chris Forbes | 928dfee | 2019-02-26 21:22:32 -0800 | [diff] [blame] | 769 | auto it = intermediates.emplace(std::piecewise_construct, |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 770 | std::forward_as_tuple(id), |
| 771 | std::forward_as_tuple(size)); |
Ben Clayton | 0e3d328 | 2019-04-04 15:53:04 +0100 | [diff] [blame] | 772 | ASSERT_MSG(it.second, "Intermediate %d created twice", id.value()); |
Chris Forbes | 928dfee | 2019-02-26 21:22:32 -0800 | [diff] [blame] | 773 | return it.first->second; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 776 | Variable& getVariable(SpirvShader::Object::ID id) |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 777 | { |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 778 | auto it = variables.find(id); |
| 779 | ASSERT_MSG(it != variables.end(), "Unknown variables %d", id.value()); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 780 | return it->second; |
| 781 | } |
| 782 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 783 | Intermediate const& getIntermediate(SpirvShader::Object::ID id) const |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 784 | { |
| 785 | auto it = intermediates.find(id); |
Ben Clayton | af26cfe | 2019-03-21 17:32:44 +0000 | [diff] [blame] | 786 | ASSERT_MSG(it != intermediates.end(), "Unknown intermediate %d", id.value()); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 787 | return it->second; |
| 788 | } |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 789 | |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 790 | Pointer<Byte>& getPointer(SpirvShader::Object::ID id) |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 791 | { |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 792 | auto it = pointers.find(id); |
| 793 | ASSERT_MSG(it != pointers.end(), "Unknown pointer %d", id.value()); |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 794 | return it->second; |
| 795 | } |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 798 | class GenericValue |
| 799 | { |
| 800 | // Generic wrapper over either per-lane intermediate value, or a constant. |
| 801 | // Constants are transparently widened to per-lane values in operator[]. |
| 802 | // This is appropriate in most cases -- if we're not going to do something |
| 803 | // significantly different based on whether the value is uniform across lanes. |
| 804 | |
| 805 | SpirvShader::Object const &obj; |
| 806 | Intermediate const *intermediate; |
| 807 | |
| 808 | public: |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 809 | GenericValue(SpirvShader const *shader, SpirvRoutine const *routine, SpirvShader::Object::ID objId) : |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 810 | obj(shader->getObject(objId)), |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 811 | intermediate(obj.kind == SpirvShader::Object::Kind::Intermediate ? &routine->getIntermediate(objId) : nullptr), |
| 812 | type(obj.type) {} |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 813 | |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 814 | RValue<SIMD::Float> Float(uint32_t i) const |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 815 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 816 | if (intermediate != nullptr) |
| 817 | { |
| 818 | return intermediate->Float(i); |
| 819 | } |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 820 | auto constantValue = reinterpret_cast<float *>(obj.constantValue.get()); |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 821 | return RValue<SIMD::Float>(constantValue[i]); |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 822 | } |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 823 | |
| 824 | RValue<SIMD::Int> Int(uint32_t i) const |
| 825 | { |
| 826 | return As<SIMD::Int>(Float(i)); |
| 827 | } |
| 828 | |
| 829 | RValue<SIMD::UInt> UInt(uint32_t i) const |
| 830 | { |
| 831 | return As<SIMD::UInt>(Float(i)); |
| 832 | } |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 833 | |
| 834 | SpirvShader::Type::ID const type; |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 835 | }; |
| 836 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 839 | #endif // sw_SpirvShader_hpp |