2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SKSL_SPIRVCODEGENERATOR
9 #define SKSL_SPIRVCODEGENERATOR
11 #include "include/private/SkSLDefines.h"
12 #include "include/private/SkSLLayout.h"
13 #include "include/private/SkSLModifiers.h"
14 #include "include/private/SkSLProgramKind.h"
15 #include "include/private/SkTArray.h"
16 #include "include/private/SkTHash.h"
17 #include "src/sksl/SkSLMemoryLayout.h"
18 #include "src/sksl/SkSLStringStream.h"
19 #include "src/sksl/codegen/SkSLCodeGenerator.h"
20 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
21 #include "src/sksl/ir/SkSLFunctionDefinition.h"
22 #include "src/sksl/ir/SkSLInterfaceBlock.h"
23 #include "src/sksl/ir/SkSLSymbolTable.h"
24 #include "src/sksl/ir/SkSLType.h"
25 #include "src/sksl/ir/SkSLVariable.h"
26 #include "src/sksl/spirv.h"
32 #include <string_view>
35 template <typename T> class SkSpan;
40 class BinaryExpression;
42 class ConstructorCompound;
43 class ConstructorCompoundCast;
44 class ConstructorDiagonalMatrix;
45 class ConstructorMatrixResize;
46 class ConstructorScalarCast;
47 class ConstructorSplat;
59 class PostfixExpression;
60 class PrefixExpression;
62 class ReturnStatement;
64 class SwitchStatement;
65 class TernaryExpression;
67 class VariableReference;
68 enum IntrinsicKind : int8_t;
69 struct IndexExpression;
74 * Converts a Program into a SPIR-V binary.
76 class SPIRVCodeGenerator : public CodeGenerator {
78 // We reserve an impossible SpvId as a sentinel. (NA meaning none, n/a, etc.)
79 static constexpr SpvId NA = (SpvId)-1;
85 // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
86 // by a pointer (e.g. vector swizzles), returns NA.
87 virtual SpvId getPointer() { return NA; }
89 // Returns true if a valid pointer returned by getPointer represents a memory object
90 // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if
91 // getPointer() returns NA.
92 virtual bool isMemoryObjectPointer() const { return true; }
94 // Applies a swizzle to the components of the LValue, if possible. This is used to create
95 // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false.
96 virtual bool applySwizzle(const ComponentArray& components, const Type& newType) {
100 virtual SpvId load(OutputStream& out) = 0;
102 virtual void store(SpvId value, OutputStream& out) = 0;
105 SPIRVCodeGenerator(const Context* context,
106 const Program* program,
108 : INHERITED(context, program, out)
109 , fDefaultLayout(MemoryLayout::k140_Standard)
113 , fSynthetics(fContext, /*builtin=*/true) {}
115 bool generateCode() override;
118 enum IntrinsicOpcodeKind {
119 kGLSL_STD_450_IntrinsicOpcodeKind,
120 kSPIRV_IntrinsicOpcodeKind,
121 kSpecial_IntrinsicOpcodeKind,
122 kInvalid_IntrinsicOpcodeKind,
125 enum SpecialIntrinsic {
126 kAtan_SpecialIntrinsic,
127 kClamp_SpecialIntrinsic,
128 kMatrixCompMult_SpecialIntrinsic,
129 kMax_SpecialIntrinsic,
130 kMin_SpecialIntrinsic,
131 kMix_SpecialIntrinsic,
132 kMod_SpecialIntrinsic,
133 kDFdy_SpecialIntrinsic,
134 kSaturate_SpecialIntrinsic,
135 kSampledImage_SpecialIntrinsic,
136 kSmoothStep_SpecialIntrinsic,
137 kStep_SpecialIntrinsic,
138 kSubpassLoad_SpecialIntrinsic,
139 kTexture_SpecialIntrinsic,
142 enum class Precision {
150 std::unique_ptr<SPIRVCodeGenerator::LValue> lvalue;
154 * Pass in the type to automatically add a RelaxedPrecision decoration for the id when
155 * appropriate, or null to never add one.
157 SpvId nextId(const Type* type);
159 SpvId nextId(Precision precision);
161 SpvId getType(const Type& type);
163 SpvId getType(const Type& type, const MemoryLayout& layout);
165 SpvId getFunctionType(const FunctionDeclaration& function);
167 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
169 SpvId getPointerType(const Type& type, const MemoryLayout& layout,
170 SpvStorageClass_ storageClass);
172 std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
174 void writeLayout(const Layout& layout, SpvId target, Position pos);
176 void writeFieldLayout(const Layout& layout, SpvId target, int member);
178 SpvId writeStruct(const Type& type, const MemoryLayout& memoryLayout);
180 void writeProgramElement(const ProgramElement& pe, OutputStream& out);
182 SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTFlip = true);
184 SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
186 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
188 SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
190 void writeGlobalVar(ProgramKind kind, const VarDeclaration& v);
192 void writeVarDeclaration(const VarDeclaration& var, OutputStream& out);
194 SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
196 int findUniformFieldIndex(const Variable& var) const;
198 std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
200 SpvId writeExpression(const Expression& expr, OutputStream& out);
202 SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
204 SpvId writeFunctionCallArgument(const FunctionCall& call,
206 std::vector<TempVar>* tempVars,
209 void copyBackTempVars(const std::vector<TempVar>& tempVars, OutputStream& out);
211 SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
214 void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
215 SpvId signedInst, SpvId unsignedInst,
216 const std::vector<SpvId>& args, OutputStream& out);
219 * Promotes an expression to a vector. If the expression is already a vector with vectorSize
220 * columns, returns it unmodified. If the expression is a scalar, either promotes it to a
221 * vector (if vectorSize > 1) or returns it unmodified (if vectorSize == 1). Asserts if the
222 * expression is already a vector and it does not have vectorSize columns.
224 SpvId vectorize(const Expression& expr, int vectorSize, OutputStream& out);
227 * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
228 * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
229 * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
232 std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out);
234 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
236 SpvId writeScalarToMatrixSplat(const Type& matrixType, SpvId scalarId, OutputStream& out);
238 SpvId writeFloatConstructor(const AnyConstructor& c, OutputStream& out);
240 SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType,
243 SpvId writeIntConstructor(const AnyConstructor& c, OutputStream& out);
245 SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
248 SpvId writeUIntConstructor(const AnyConstructor& c, OutputStream& out);
250 SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType,
253 SpvId writeBooleanConstructor(const AnyConstructor& c, OutputStream& out);
255 SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType,
258 SpvId castScalarToType(SpvId inputExprId, const Type& inputType, const Type& outputType,
262 * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
263 * source matrix are filled with zero; entries which do not exist in the destination matrix are
266 SpvId writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType, OutputStream& out);
268 void addColumnEntry(const Type& columnType, SkTArray<SpvId>* currentColumn,
269 SkTArray<SpvId>* columnIds, int rows, SpvId entry, OutputStream& out);
271 SpvId writeConstructorCompound(const ConstructorCompound& c, OutputStream& out);
273 SpvId writeMatrixConstructor(const ConstructorCompound& c, OutputStream& out);
275 SpvId writeVectorConstructor(const ConstructorCompound& c, OutputStream& out);
277 SpvId writeCompositeConstructor(const AnyConstructor& c, OutputStream& out);
279 SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out);
281 SpvId writeConstructorMatrixResize(const ConstructorMatrixResize& c, OutputStream& out);
283 SpvId writeConstructorScalarCast(const ConstructorScalarCast& c, OutputStream& out);
285 SpvId writeConstructorSplat(const ConstructorSplat& c, OutputStream& out);
287 SpvId writeConstructorCompoundCast(const ConstructorCompoundCast& c, OutputStream& out);
289 SpvId writeComposite(const std::vector<SpvId>& arguments, const Type& type, OutputStream& out);
291 SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
293 SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
296 * Folds the potentially-vector result of a logical operation down to a single bool. If
297 * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
298 * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
299 * returns the original id value.
301 SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
303 SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
304 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
305 SpvOp_ mergeOperator, OutputStream& out);
307 SpvId writeStructComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs,
310 SpvId writeArrayComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs,
313 // Used by writeStructComparison and writeArrayComparison to logically combine field-by-field
314 // comparisons into an overall comparison result.
315 // - `a.x == b.x` merged with `a.y == b.y` generates `(a.x == b.x) && (a.y == b.y)`
316 // - `a.x != b.x` merged with `a.y != b.y` generates `(a.x != b.x) || (a.y != b.y)`
317 SpvId mergeComparisons(SpvId comparison, SpvId allComparisons, Operator op, OutputStream& out);
319 SpvId writeComponentwiseMatrixUnary(const Type& operandType,
324 SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
325 SpvOp_ op, OutputStream& out);
327 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
328 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
329 SpvOp_ ifBool, OutputStream& out);
331 SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out);
333 SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
334 const Type& rightType, SpvId rhs, const Type& resultType,
337 SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
339 SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
341 SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
343 SpvId writeLogicalAnd(const Expression& left, const Expression& right, OutputStream& out);
345 SpvId writeLogicalOr(const Expression& left, const Expression& right, OutputStream& out);
347 SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
349 SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
351 SpvId writeLiteral(const Literal& f);
353 SpvId writeLiteral(double value, const Type& type);
355 void writeStatement(const Statement& s, OutputStream& out);
357 void writeBlock(const Block& b, OutputStream& out);
359 void writeIfStatement(const IfStatement& stmt, OutputStream& out);
361 void writeForStatement(const ForStatement& f, OutputStream& out);
363 void writeDoStatement(const DoStatement& d, OutputStream& out);
365 void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
367 void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
369 void writeCapabilities(OutputStream& out);
371 void writeInstructions(const Program& program, OutputStream& out);
373 void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
375 void writeWord(int32_t word, OutputStream& out);
377 void writeString(std::string_view s, OutputStream& out);
379 void writeInstruction(SpvOp_ opCode, OutputStream& out);
381 void writeInstruction(SpvOp_ opCode, std::string_view string, OutputStream& out);
383 void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
385 void writeInstruction(SpvOp_ opCode, int32_t word1, std::string_view string,
388 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, std::string_view string,
391 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
393 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
396 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
399 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
400 int32_t word5, OutputStream& out);
402 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
403 int32_t word5, int32_t word6, OutputStream& out);
405 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
406 int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
408 void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
409 int32_t word5, int32_t word6, int32_t word7, int32_t word8,
412 // This form of writeInstruction can deduplicate redundant ops.
414 // 8 Words is enough for nearly all instructions (except variable-length instructions like
415 // OpAccessChain or OpConstantComposite).
416 using Words = SkSTArray<8, Word>;
417 SpvId writeInstruction(SpvOp_ opCode, const SkTArray<Word>& words, OutputStream& out);
422 SkSTArray<8, int32_t> fWords;
424 bool operator==(const Instruction& that) const;
428 static Instruction BuildInstructionKey(SpvOp_ opCode, const SkTArray<Word>& words);
430 // The writeOpXxxxx calls will simplify and deduplicate ops where possible.
431 SpvId writeOpConstantTrue(const Type& type);
432 SpvId writeOpConstantFalse(const Type& type);
433 SpvId writeOpConstant(const Type& type, int32_t valueBits);
434 SpvId writeOpConstantComposite(const Type& type, const SkTArray<SpvId>& values);
435 SpvId writeOpCompositeConstruct(const Type& type, const SkTArray<SpvId>&, OutputStream& out);
436 SpvId writeOpCompositeExtract(const Type& type, SpvId base, int component, OutputStream& out);
437 SpvId writeOpCompositeExtract(const Type& type, SpvId base, int componentA, int componentB,
439 SpvId writeOpLoad(SpvId type, Precision precision, SpvId pointer, OutputStream& out);
440 void writeOpStore(SpvStorageClass_ storageClass, SpvId pointer, SpvId value, OutputStream& out);
442 // Converts the provided SpvId(s) into an array of scalar OpConstants, if it can be done.
443 bool toConstants(SpvId value, SkTArray<SpvId>* constants);
444 bool toConstants(SkSpan<const SpvId> values, SkTArray<SpvId>* constants);
446 // Extracts the requested component SpvId from a composite instruction, if it can be done.
447 Instruction* resultTypeForInstruction(const Instruction& instr);
448 int numComponentsForVecInstruction(const Instruction& instr);
449 SpvId toComponent(SpvId id, int component);
451 struct ConditionalOpCounts {
452 size_t numReachableOps;
455 ConditionalOpCounts getConditionalOpCounts();
456 void pruneConditionalOps(ConditionalOpCounts ops);
458 enum StraightLineLabelType {
459 // Use "BranchlessBlock" for blocks which are never explicitly branched-to at all. This
460 // happens at the start of a function, or when we find unreachable code.
463 // Use "BranchIsOnPreviousLine" when writing a label that comes immediately after its
464 // associated branch. Example usage:
465 // - SPIR-V does not implicitly fall through from one block to the next, so you may need to
466 // use an OpBranch to explicitly jump to the next block, even when they are adjacent in
468 // - The block immediately following an OpBranchConditional or OpSwitch.
469 kBranchIsOnPreviousLine,
472 enum BranchingLabelType {
473 // Use "BranchIsAbove" for labels which are referenced by OpBranch or OpBranchConditional
474 // ops that are above the label in the code--i.e., the branch skips forward in the code.
477 // Use "BranchIsBelow" for labels which are referenced by OpBranch or OpBranchConditional
478 // ops below the label in the code--i.e., the branch jumps backward in the code.
481 // Use "BranchesOnBothSides" for labels which have branches coming from both directions.
482 kBranchesOnBothSides,
484 void writeLabel(SpvId label, StraightLineLabelType type, OutputStream& out);
485 void writeLabel(SpvId label, BranchingLabelType type, ConditionalOpCounts ops,
488 bool isDead(const Variable& var) const;
490 MemoryLayout memoryLayoutForVariable(const Variable&) const;
492 struct EntrypointAdapter {
493 std::unique_ptr<FunctionDefinition> entrypointDef;
494 std::unique_ptr<FunctionDeclaration> entrypointDecl;
496 Modifiers fModifiers;
499 EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main);
501 struct UniformBuffer {
502 std::unique_ptr<InterfaceBlock> fInterfaceBlock;
503 std::unique_ptr<Variable> fInnerVariable;
504 std::unique_ptr<Type> fStruct;
507 void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable);
509 void addRTFlipUniform(Position pos);
511 const MemoryLayout fDefaultLayout;
513 uint64_t fCapabilities;
515 SpvId fGLSLExtendedInstructions;
517 IntrinsicOpcodeKind opKind;
523 Intrinsic getIntrinsic(IntrinsicKind) const;
524 SkTHashMap<const FunctionDeclaration*, SpvId> fFunctionMap;
525 SkTHashMap<const Variable*, SpvId> fVariableMap;
526 SkTHashMap<const Type*, SpvId> fStructMap;
527 StringStream fGlobalInitializersBuffer;
528 StringStream fConstantBuffer;
529 StringStream fVariableBuffer;
530 StringStream fNameBuffer;
531 StringStream fDecorationBuffer;
533 // These caches map SpvIds to Instructions, and vice-versa. This enables us to deduplicate code
534 // (by detecting an Instruction we've already issued and reusing the SpvId), and to introspect
535 // and simplify code we've already emitted (by taking a SpvId from an Instruction and following
536 // it back to its source).
537 SkTHashMap<Instruction, SpvId, Instruction::Hash> fOpCache; // maps instruction -> SpvId
538 SkTHashMap<SpvId, Instruction> fSpvIdCache; // maps SpvId -> instruction
539 SkTHashMap<SpvId, SpvId> fStoreCache; // maps ptr SpvId -> value SpvId
541 // "Reachable" ops are instructions which can safely be accessed from the current block.
542 // For instance, if our SPIR-V contains `%3 = OpFAdd %1 %2`, we would be able to access and
543 // reuse that computation on following lines. However, if that Add operation occurred inside an
544 // `if` block, then its SpvId becomes inaccessible once we complete the if statement (since
545 // depending on the if condition, we may or may not have actually done that computation). The
546 // same logic applies to other control-flow blocks as well. Once an instruction becomes
547 // unreachable, we remove it from both op-caches.
548 std::vector<SpvId> fReachableOps;
550 // The "store-ops" list contains a running list of all the pointers in the store cache. If a
551 // store occurs inside of a conditional block, once that block exits, we no longer know what is
552 // stored in that particular SpvId. At that point, we must remove any associated entry from the
554 std::vector<SpvId> fStoreOps;
556 // label of the current block, or 0 if we are not in a block
558 std::stack<SpvId> fBreakTarget;
559 std::stack<SpvId> fContinueTarget;
560 bool fWroteRTFlip = false;
561 // holds variables synthesized during output, for lifetime purposes
562 SymbolTable fSynthetics;
563 // Holds a list of uniforms that were declared as globals at the top-level instead of in an
565 UniformBuffer fUniformBuffer;
566 std::vector<const VarDeclaration*> fTopLevelUniforms;
567 SkTHashMap<const Variable*, int> fTopLevelUniformMap; // <var, UniformBuffer field index>
568 SkTHashSet<const Variable*> fSPIRVBonusVariables;
569 SpvId fUniformBufferId = NA;
571 friend class PointerLValue;
572 friend class SwizzleLValue;
574 using INHERITED = CodeGenerator;