1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef sw_SpirvShader_hpp
16 #define sw_SpirvShader_hpp
18 #include "ShaderCore.hpp"
19 #include "SpirvID.hpp"
20 #include "System/Types.hpp"
21 #include "Vulkan/VkDebug.hpp"
22 #include "Vulkan/VkConfig.h"
23 #include "Device/Config.hpp"
25 #include <spirv/unified1/spirv.hpp>
32 #include <unordered_set>
33 #include <unordered_map>
35 #include <type_traits>
46 // Forward declarations.
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.
56 // Width is the number of per-lane scalars packed into each SIMD vector.
57 static constexpr int Width = 4;
59 using Float = rr::Float4;
61 using UInt = rr::UInt4;
64 // Incrementally constructed complex bundle of rvalues
65 // Effectively a restricted vector, supporting only:
66 // - allocation to a (runtime-known) fixed size
67 // - in-place construction of elements
72 Intermediate(uint32_t size) : scalar(new rr::Value*[size]), size(size) {
73 memset(scalar, 0, sizeof(rr::Value*) * size);
81 void move(uint32_t i, RValue<SIMD::Float> &&scalar) { emplace(i, scalar.value); }
82 void move(uint32_t i, RValue<SIMD::Int> &&scalar) { emplace(i, scalar.value); }
83 void move(uint32_t i, RValue<SIMD::UInt> &&scalar) { emplace(i, scalar.value); }
85 void move(uint32_t i, const RValue<SIMD::Float> &scalar) { emplace(i, scalar.value); }
86 void move(uint32_t i, const RValue<SIMD::Int> &scalar) { emplace(i, scalar.value); }
87 void move(uint32_t i, const RValue<SIMD::UInt> &scalar) { emplace(i, scalar.value); }
89 void replace(uint32_t i, RValue<SIMD::Float> &&scalar) { replace(i, scalar.value); }
90 void replace(uint32_t i, RValue<SIMD::Int> &&scalar) { replace(i, scalar.value); }
91 void replace(uint32_t i, RValue<SIMD::UInt> &&scalar) { replace(i, scalar.value); }
93 void replace(uint32_t i, const RValue<SIMD::Float> &scalar) { replace(i, scalar.value); }
94 void replace(uint32_t i, const RValue<SIMD::Int> &scalar) { replace(i, scalar.value); }
95 void replace(uint32_t i, const RValue<SIMD::UInt> &scalar) { replace(i, scalar.value); }
97 // Value retrieval functions.
98 RValue<SIMD::Float> Float(uint32_t i) const
101 ASSERT(scalar[i] != nullptr);
102 return As<SIMD::Float>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Float>(scalar)
105 RValue<SIMD::Int> Int(uint32_t i) const
108 ASSERT(scalar[i] != nullptr);
109 return As<SIMD::Int>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Int>(scalar)
112 RValue<SIMD::UInt> UInt(uint32_t i) const
115 ASSERT(scalar[i] != nullptr);
116 return As<SIMD::UInt>(scalar[i]); // TODO(b/128539387): RValue<SIMD::UInt>(scalar)
119 // No copy/move construction or assignment
120 Intermediate(Intermediate const &) = delete;
121 Intermediate(Intermediate &&) = delete;
122 Intermediate & operator=(Intermediate const &) = delete;
123 Intermediate & operator=(Intermediate &&) = delete;
126 void emplace(uint32_t i, rr::Value *value)
129 ASSERT(scalar[i] == nullptr);
133 void replace(uint32_t i, rr::Value *value)
139 rr::Value **const scalar;
146 using InsnStore = std::vector<uint32_t>;
149 /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */
152 InsnStore::const_iterator iter;
155 spv::Op opcode() const
157 return static_cast<spv::Op>(*iter & spv::OpCodeMask);
160 uint32_t wordCount() const
162 return *iter >> spv::WordCountShift;
165 uint32_t word(uint32_t n) const
167 ASSERT(n < wordCount());
171 uint32_t const * wordPointer(uint32_t n) const
173 ASSERT(n < wordCount());
177 bool operator==(InsnIterator const &other) const
179 return iter == other.iter;
182 bool operator!=(InsnIterator const &other) const
184 return iter != other.iter;
187 InsnIterator operator*() const
192 InsnIterator &operator++()
198 InsnIterator const operator++(int)
200 InsnIterator ret{*this};
205 InsnIterator(InsnIterator const &other) = default;
207 InsnIterator() = default;
209 explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter}
214 /* range-based-for interface */
215 InsnIterator begin() const
217 return InsnIterator{insns.cbegin() + 5};
220 InsnIterator end() const
222 return InsnIterator{insns.cend()};
228 using ID = SpirvID<Type>;
230 spv::Op opcode() const { return definition.opcode(); }
232 InsnIterator definition;
233 spv::StorageClass storageClass = static_cast<spv::StorageClass>(-1);
234 uint32_t sizeInComponents = 0;
235 bool isBuiltInBlock = false;
237 // Inner element type for pointers, arrays, vectors and matrices.
244 using ID = SpirvID<Object>;
246 spv::Op opcode() const { return definition.opcode(); }
248 InsnIterator definition;
251 std::unique_ptr<uint32_t[]> constantValue = nullptr;
255 Unknown, /* for paranoia -- if we get left with an object in this state, the module was broken */
256 Variable, // TODO: Document
257 InterfaceVariable, // TODO: Document
258 Constant, // Values held by Object::constantValue
259 Value, // Values held by SpirvRoutine::intermediates
260 PhysicalPointer, // Pointer held by SpirvRoutine::physicalPointers
261 } kind = Kind::Unknown;
264 // Block is an interval of SPIR-V instructions, starting with the
265 // opening OpLabel, and ending with a termination instruction.
269 using ID = SpirvID<Block>;
270 using Set = std::unordered_set<ID>;
272 // Edge represents the graph edge between two blocks.
278 bool operator == (const Edge& other) const { return from == other.from && to == other.to; }
282 std::size_t operator()(const Edge& edge) const noexcept
284 return std::hash<uint32_t>()(edge.from.value() * 31 + edge.to.value());
290 Block(const Block& other) = default;
291 explicit Block(InsnIterator begin, InsnIterator end);
293 /* range-based-for interface */
294 inline InsnIterator begin() const { return begin_; }
295 inline InsnIterator end() const { return end_; }
299 Simple, // OpBranch or other simple terminator.
300 StructuredBranchConditional, // OpSelectionMerge + OpBranchConditional
301 UnstructuredBranchConditional, // OpBranchConditional
302 StructuredSwitch, // OpSelectionMerge + OpSwitch
303 UnstructuredSwitch, // OpSwitch
304 Loop, // OpLoopMerge + [OpBranchConditional | OpBranch]
308 InsnIterator mergeInstruction; // Structured control flow merge instruction.
309 InsnIterator branchInstruction; // Branch instruction.
310 ID mergeBlock; // Structured flow merge block.
311 ID continueTarget; // Loop continue block.
312 Set ins; // Blocks that branch into this block.
313 Set outs; // Blocks that this block branches to.
320 struct TypeOrObject {}; // Dummy struct to represent a Type or Object.
322 // TypeOrObjectID is an identifier that represents a Type or an Object,
323 // and supports implicit casting to and from Type::ID or Object::ID.
324 class TypeOrObjectID : public SpirvID<TypeOrObject>
327 using Hash = std::hash<SpirvID<TypeOrObject>>;
329 inline TypeOrObjectID(uint32_t id) : SpirvID(id) {}
330 inline TypeOrObjectID(Type::ID id) : SpirvID(id.value()) {}
331 inline TypeOrObjectID(Object::ID id) : SpirvID(id.value()) {}
332 inline operator Type::ID() const { return Type::ID(value()); }
333 inline operator Object::ID() const { return Object::ID(value()); }
336 int getSerialID() const
341 explicit SpirvShader(InsnStore const &insns);
345 bool EarlyFragmentTests : 1;
346 bool DepthReplacing : 1;
347 bool DepthGreater : 1;
349 bool DepthUnchanged : 1;
350 bool ContainsKill : 1;
351 bool NeedsCentroid : 1;
353 // Compute workgroup dimensions
354 int WorkgroupSizeX = 1, WorkgroupSizeY = 1, WorkgroupSizeZ = 1;
357 Modes const &getModes() const
362 enum AttribType : unsigned char
369 ATTRIBTYPE_LAST = ATTRIBTYPE_UINT
372 bool hasBuiltinInput(spv::BuiltIn b) const
374 return inputBuiltins.find(b) != inputBuiltins.end();
381 int32_t DescriptorSet;
383 spv::BuiltIn BuiltIn;
386 int32_t MatrixStride;
387 bool HasLocation : 1;
388 bool HasComponent : 1;
389 bool HasDescriptorSet : 1;
394 bool NoPerspective : 1;
396 bool BufferBlock : 1;
398 bool HasArrayStride : 1;
399 bool HasMatrixStride : 1;
402 : Location{-1}, Component{0}, DescriptorSet{-1}, Binding{-1},
403 BuiltIn{static_cast<spv::BuiltIn>(-1)},
404 Offset{-1}, ArrayStride{-1}, MatrixStride{-1},
405 HasLocation{false}, HasComponent{false},
406 HasDescriptorSet{false}, HasBinding{false},
407 HasBuiltIn{false}, Flat{false}, Centroid{false},
408 NoPerspective{false}, Block{false}, BufferBlock{false},
409 HasOffset{false}, HasArrayStride{false}, HasMatrixStride{false}
413 Decorations(Decorations const &) = default;
415 void Apply(Decorations const &src);
417 void Apply(spv::Decoration decoration, uint32_t arg);
420 std::unordered_map<TypeOrObjectID, Decorations, TypeOrObjectID::Hash> decorations;
421 std::unordered_map<Type::ID, std::vector<Decorations>> memberDecorations;
423 struct InterfaceComponent
428 bool NoPerspective : 1;
431 : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false}
436 struct BuiltinMapping
439 uint32_t FirstComponent;
440 uint32_t SizeInComponents;
443 std::vector<InterfaceComponent> inputs;
444 std::vector<InterfaceComponent> outputs;
446 void emitProlog(SpirvRoutine *routine) const;
447 void emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask) const;
448 void emitEpilog(SpirvRoutine *routine) const;
450 using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>;
451 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins;
452 std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins;
454 Type const &getType(Type::ID id) const
456 auto it = types.find(id);
457 ASSERT_MSG(it != types.end(), "Unknown type %d", id.value());
461 Object const &getObject(Object::ID id) const
463 auto it = defs.find(id);
464 ASSERT_MSG(it != defs.end(), "Unknown object %d", id.value());
468 Block const &getBlock(Block::ID id) const
470 auto it = blocks.find(id);
471 ASSERT_MSG(it != blocks.end(), "Unknown block %d", id.value());
477 static volatile int serialCounter;
479 HandleMap<Type> types;
480 HandleMap<Object> defs;
481 HandleMap<Block> blocks;
482 Block::ID mainBlockId; // Block of the entry point function.
484 // Walks all reachable the blocks starting from id adding them to
486 void TraverseReachableBlocks(Block::ID id, Block::Set& reachable);
488 // Assigns Block::ins from Block::outs for every block.
489 void AssignBlockIns();
491 // DeclareType creates a Type for the given OpTypeX instruction, storing
492 // it into the types map. It is called from the analysis pass (constructor).
493 void DeclareType(InsnIterator insn);
495 void ProcessExecutionMode(InsnIterator it);
497 uint32_t ComputeTypeSize(InsnIterator insn);
498 void ApplyDecorationsForId(Decorations *d, TypeOrObjectID id) const;
499 void ApplyDecorationsForIdMember(Decorations *d, Type::ID id, uint32_t member) const;
501 // Returns true if data in the given storage class is word-interleaved
502 // by each SIMD vector lane, otherwise data is linerally stored.
504 // A 'lane' is a component of a SIMD vector register.
505 // Given 4 consecutive loads/stores of 4 SIMD vector registers:
507 // "StorageInterleavedByLane":
509 // Ptr+0:Reg0.x | Ptr+1:Reg0.y | Ptr+2:Reg0.z | Ptr+3:Reg0.w
510 // --------------+--------------+--------------+--------------
511 // Ptr+4:Reg1.x | Ptr+5:Reg1.y | Ptr+6:Reg1.z | Ptr+7:Reg1.w
512 // --------------+--------------+--------------+--------------
513 // Ptr+8:Reg2.x | Ptr+9:Reg2.y | Ptr+a:Reg2.z | Ptr+b:Reg2.w
514 // --------------+--------------+--------------+--------------
515 // Ptr+c:Reg3.x | Ptr+d:Reg3.y | Ptr+e:Reg3.z | Ptr+f:Reg3.w
517 // Not "StorageInterleavedByLane":
519 // Ptr+0:Reg0.x | Ptr+0:Reg0.y | Ptr+0:Reg0.z | Ptr+0:Reg0.w
520 // --------------+--------------+--------------+--------------
521 // Ptr+1:Reg1.x | Ptr+1:Reg1.y | Ptr+1:Reg1.z | Ptr+1:Reg1.w
522 // --------------+--------------+--------------+--------------
523 // Ptr+2:Reg2.x | Ptr+2:Reg2.y | Ptr+2:Reg2.z | Ptr+2:Reg2.w
524 // --------------+--------------+--------------+--------------
525 // Ptr+3:Reg3.x | Ptr+3:Reg3.y | Ptr+3:Reg3.z | Ptr+3:Reg3.w
527 static bool IsStorageInterleavedByLane(spv::StorageClass storageClass);
530 int VisitInterfaceInner(Type::ID id, Decorations d, F f) const;
533 void VisitInterface(Object::ID id, F f) const;
535 uint32_t GetConstantInt(Object::ID id) const;
536 Object& CreateConstant(InsnIterator it);
538 void ProcessInterfaceVariable(Object &object);
540 SIMD::Int WalkExplicitLayoutAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const;
541 SIMD::Int WalkAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const;
542 uint32_t WalkLiteralAccessChain(Type::ID id, uint32_t numIndexes, uint32_t const *indexes) const;
544 // EmitState holds control-flow state for the emit() pass.
548 RValue<SIMD::Int> activeLaneMask() const
550 ASSERT(activeLaneMaskValue != nullptr);
551 return RValue<SIMD::Int>(activeLaneMaskValue);
554 void setActiveLaneMask(RValue<SIMD::Int> mask)
556 activeLaneMaskValue = mask.value;
559 // Add a new active lane mask edge from the current block to out.
560 // The edge mask value will be (mask AND activeLaneMaskValue).
561 // If multiple active lane masks are added for the same edge, then
562 // they will be ORed together.
563 void addOutputActiveLaneMaskEdge(Block::ID out, RValue<SIMD::Int> mask);
565 // Add a new active lane mask for the edge from -> to.
566 // If multiple active lane masks are added for the same edge, then
567 // they will be ORed together.
568 void addActiveLaneMaskEdge(Block::ID from, Block::ID to, RValue<SIMD::Int> mask);
570 SpirvRoutine *routine = nullptr; // The current routine being built.
571 rr::Value *activeLaneMaskValue = nullptr; // The current active lane mask.
572 Block::ID currentBlock; // The current block being built.
573 Block::Set visited; // Blocks already built.
574 std::unordered_map<Block::Edge, RValue<SIMD::Int>, Block::Edge::Hash> edgeActiveLaneMasks;
575 std::queue<Block::ID> *pending;
578 // EmitResult is an enumerator of result values from the Emit functions.
579 enum class EmitResult
581 Continue, // No termination instructions.
582 Terminator, // Reached a termination instruction.
585 // existsPath returns true if there's a direct or indirect flow from
586 // the 'from' block to the 'to' block that does not pass through
587 // notPassingThrough.
588 bool existsPath(Block::ID from, Block::ID to, Block::ID notPassingThrough) const;
590 // Lookup the active lane mask for the edge from -> to.
591 // If from is unreachable, then a mask of all zeros is returned.
592 // Asserts if from is reachable and the edge does not exist.
593 RValue<SIMD::Int> GetActiveLaneMaskEdge(EmitState *state, Block::ID from, Block::ID to) const;
595 // Emit all the unvisited blocks (except for ignore) in BFS order,
597 void EmitBlocks(Block::ID id, EmitState *state, Block::ID ignore = 0) const;
598 void EmitNonLoop(EmitState *state) const;
599 void EmitLoop(EmitState *state) const;
601 void EmitInstructions(InsnIterator begin, InsnIterator end, EmitState *state) const;
602 EmitResult EmitInstruction(InsnIterator insn, EmitState *state) const;
604 // Emit pass instructions:
605 EmitResult EmitVariable(InsnIterator insn, EmitState *state) const;
606 EmitResult EmitLoad(InsnIterator insn, EmitState *state) const;
607 EmitResult EmitStore(InsnIterator insn, EmitState *state) const;
608 EmitResult EmitAccessChain(InsnIterator insn, EmitState *state) const;
609 EmitResult EmitCompositeConstruct(InsnIterator insn, EmitState *state) const;
610 EmitResult EmitCompositeInsert(InsnIterator insn, EmitState *state) const;
611 EmitResult EmitCompositeExtract(InsnIterator insn, EmitState *state) const;
612 EmitResult EmitVectorShuffle(InsnIterator insn, EmitState *state) const;
613 EmitResult EmitVectorTimesScalar(InsnIterator insn, EmitState *state) const;
614 EmitResult EmitMatrixTimesVector(InsnIterator insn, EmitState *state) const;
615 EmitResult EmitVectorTimesMatrix(InsnIterator insn, EmitState *state) const;
616 EmitResult EmitMatrixTimesMatrix(InsnIterator insn, EmitState *state) const;
617 EmitResult EmitVectorExtractDynamic(InsnIterator insn, EmitState *state) const;
618 EmitResult EmitVectorInsertDynamic(InsnIterator insn, EmitState *state) const;
619 EmitResult EmitUnaryOp(InsnIterator insn, EmitState *state) const;
620 EmitResult EmitBinaryOp(InsnIterator insn, EmitState *state) const;
621 EmitResult EmitDot(InsnIterator insn, EmitState *state) const;
622 EmitResult EmitSelect(InsnIterator insn, EmitState *state) const;
623 EmitResult EmitExtendedInstruction(InsnIterator insn, EmitState *state) const;
624 EmitResult EmitAny(InsnIterator insn, EmitState *state) const;
625 EmitResult EmitAll(InsnIterator insn, EmitState *state) const;
626 EmitResult EmitBranch(InsnIterator insn, EmitState *state) const;
627 EmitResult EmitBranchConditional(InsnIterator insn, EmitState *state) const;
628 EmitResult EmitSwitch(InsnIterator insn, EmitState *state) const;
629 EmitResult EmitUnreachable(InsnIterator insn, EmitState *state) const;
630 EmitResult EmitReturn(InsnIterator insn, EmitState *state) const;
631 EmitResult EmitPhi(InsnIterator insn, EmitState *state) const;
633 // OpcodeName() returns the name of the opcode op.
634 // If NDEBUG is defined, then OpcodeName() will only return the numerical code.
635 static std::string OpcodeName(spv::Op op);
636 static std::memory_order MemoryOrder(spv::MemorySemanticsMask memorySemantics);
638 // Helper as we often need to take dot products as part of doing other things.
639 SIMD::Float Dot(unsigned numComponents, GenericValue const & x, GenericValue const & y) const;
645 SpirvRoutine(vk::PipelineLayout const *pipelineLayout);
647 using Value = Array<SIMD::Float>;
649 vk::PipelineLayout const * const pipelineLayout;
651 std::unordered_map<SpirvShader::Object::ID, Value> lvalues;
653 std::unordered_map<SpirvShader::Object::ID, Intermediate> intermediates;
655 std::unordered_map<SpirvShader::Object::ID, Pointer<Byte> > physicalPointers;
657 Value inputs = Value{MAX_INTERFACE_COMPONENTS};
658 Value outputs = Value{MAX_INTERFACE_COMPONENTS};
660 std::array<Pointer<Byte>, vk::MAX_BOUND_DESCRIPTOR_SETS> descriptorSets;
661 Pointer<Byte> pushConstants;
663 void createLvalue(SpirvShader::Object::ID id, uint32_t size)
665 lvalues.emplace(id, Value(size));
668 Intermediate& createIntermediate(SpirvShader::Object::ID id, uint32_t size)
670 auto it = intermediates.emplace(std::piecewise_construct,
671 std::forward_as_tuple(id),
672 std::forward_as_tuple(size));
673 return it.first->second;
676 Value& getValue(SpirvShader::Object::ID id)
678 auto it = lvalues.find(id);
679 ASSERT_MSG(it != lvalues.end(), "Unknown value %d", id.value());
683 Intermediate const& getIntermediate(SpirvShader::Object::ID id) const
685 auto it = intermediates.find(id);
686 ASSERT_MSG(it != intermediates.end(), "Unknown intermediate %d", id.value());
690 Pointer<Byte>& getPhysicalPointer(SpirvShader::Object::ID id)
692 auto it = physicalPointers.find(id);
693 ASSERT_MSG(it != physicalPointers.end(), "Unknown physical pointer %d", id.value());
700 // Generic wrapper over either per-lane intermediate value, or a constant.
701 // Constants are transparently widened to per-lane values in operator[].
702 // This is appropriate in most cases -- if we're not going to do something
703 // significantly different based on whether the value is uniform across lanes.
705 SpirvShader::Object const &obj;
706 Intermediate const *intermediate;
709 GenericValue(SpirvShader const *shader, SpirvRoutine const *routine, SpirvShader::Object::ID objId) :
710 obj(shader->getObject(objId)),
711 intermediate(obj.kind == SpirvShader::Object::Kind::Value ? &routine->getIntermediate(objId) : nullptr) {}
713 RValue<SIMD::Float> Float(uint32_t i) const
715 if (intermediate != nullptr)
717 return intermediate->Float(i);
719 auto constantValue = reinterpret_cast<float *>(obj.constantValue.get());
720 return RValue<SIMD::Float>(constantValue[i]);
723 RValue<SIMD::Int> Int(uint32_t i) const
725 return As<SIMD::Int>(Float(i));
728 RValue<SIMD::UInt> UInt(uint32_t i) const
730 return As<SIMD::UInt>(Float(i));
736 #endif // sw_SpirvShader_hpp