1/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "include/core/SkStream.h"
9#include "include/private/SkSLProgramElement.h"
10#include "include/private/SkSLStatement.h"
11#include "include/private/SkTArray.h"
12#include "include/private/SkTPin.h"
13#include "src/sksl/SkSLCompiler.h"
14#include "src/sksl/SkSLOperators.h"
15#include "src/sksl/codegen/SkSLCodeGenerator.h"
16#include "src/sksl/codegen/SkSLVMCodeGenerator.h"
17#include "src/sksl/ir/SkSLBinaryExpression.h"
18#include "src/sksl/ir/SkSLBlock.h"
19#include "src/sksl/ir/SkSLBreakStatement.h"
20#include "src/sksl/ir/SkSLChildCall.h"
21#include "src/sksl/ir/SkSLConstructor.h"
22#include "src/sksl/ir/SkSLConstructorArray.h"
23#include "src/sksl/ir/SkSLConstructorArrayCast.h"
24#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
25#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
26#include "src/sksl/ir/SkSLConstructorSplat.h"
27#include "src/sksl/ir/SkSLConstructorStruct.h"
28#include "src/sksl/ir/SkSLContinueStatement.h"
29#include "src/sksl/ir/SkSLDoStatement.h"
30#include "src/sksl/ir/SkSLExpressionStatement.h"
31#include "src/sksl/ir/SkSLExternalFunctionCall.h"
32#include "src/sksl/ir/SkSLExternalFunctionReference.h"
33#include "src/sksl/ir/SkSLFieldAccess.h"
34#include "src/sksl/ir/SkSLForStatement.h"
35#include "src/sksl/ir/SkSLFunctionCall.h"
36#include "src/sksl/ir/SkSLFunctionDeclaration.h"
37#include "src/sksl/ir/SkSLFunctionDefinition.h"
38#include "src/sksl/ir/SkSLIfStatement.h"
39#include "src/sksl/ir/SkSLIndexExpression.h"
40#include "src/sksl/ir/SkSLLiteral.h"
41#include "src/sksl/ir/SkSLPostfixExpression.h"
42#include "src/sksl/ir/SkSLPrefixExpression.h"
43#include "src/sksl/ir/SkSLReturnStatement.h"
44#include "src/sksl/ir/SkSLSwitchStatement.h"
45#include "src/sksl/ir/SkSLSwizzle.h"
46#include "src/sksl/ir/SkSLTernaryExpression.h"
47#include "src/sksl/ir/SkSLVarDeclarations.h"
48#include "src/sksl/ir/SkSLVariableReference.h"
49
50#include <algorithm>
51#include <unordered_map>
52
53namespace {
54    // sksl allows the optimizations of fast_mul(), so we want to use that most of the time.
55    // This little sneaky snippet of code lets us use ** as a fast multiply infix operator.
56    struct FastF32 { skvm::F32 val; };
57    static FastF32 operator*(skvm::F32 y) { return {y}; }
58    static skvm::F32 operator*(skvm::F32 x, FastF32 y) { return fast_mul(x, y.val); }
59    static skvm::F32 operator*(float     x, FastF32 y) { return fast_mul(x, y.val); }
60}
61
62namespace SkSL {
63
64namespace {
65
66// Holds scalars, vectors, or matrices
67struct Value {
68    Value() = default;
69    explicit Value(size_t slots) {
70        fVals.resize(slots);
71    }
72    Value(skvm::F32 x) : fVals({ x.id }) {}
73    Value(skvm::I32 x) : fVals({ x.id }) {}
74
75    explicit operator bool() const { return !fVals.empty(); }
76
77    size_t slots() const { return fVals.size(); }
78
79    struct ValRef {
80        ValRef(skvm::Val& val) : fVal(val) {}
81        // Required until C++17 copy elision
82        ValRef(const ValRef&) = default;
83
84        ValRef& operator=(ValRef    v) { fVal = v.fVal; return *this; }
85        ValRef& operator=(skvm::Val v) { fVal = v;      return *this; }
86        ValRef& operator=(skvm::F32 v) { fVal = v.id;   return *this; }
87        ValRef& operator=(skvm::I32 v) { fVal = v.id;   return *this; }
88
89        operator skvm::Val() { return fVal; }
90
91        skvm::Val& fVal;
92    };
93
94    ValRef    operator[](size_t i) {
95        // These redundant asserts work around what we think is a codegen bug in GCC 8.x for
96        // 32-bit x86 Debug builds.
97        SkASSERT(i < fVals.size());
98        return fVals[i];
99    }
100    skvm::Val operator[](size_t i) const {
101        // These redundant asserts work around what we think is a codegen bug in GCC 8.x for
102        // 32-bit x86 Debug builds.
103        SkASSERT(i < fVals.size());
104        return fVals[i];
105    }
106
107    SkSpan<skvm::Val> asSpan() { return SkMakeSpan(fVals); }
108
109private:
110    SkSTArray<4, skvm::Val, true> fVals;
111};
112
113}  // namespace
114
115class SkVMGenerator {
116public:
117    SkVMGenerator(const Program& program,
118                  skvm::Builder* builder,
119                  SkVMDebugInfo* debugInfo,
120                  SampleShaderFn sampleShader,
121                  SampleColorFilterFn sampleColorFilter,
122                  SampleBlenderFn sampleBlender);
123
124    void writeProgram(SkSpan<skvm::Val> uniforms,
125                      skvm::Coord device,
126                      const FunctionDefinition& function,
127                      SkSpan<skvm::Val> arguments,
128                      SkSpan<skvm::Val> outReturn);
129
130private:
131    /**
132     * In SkSL, a Variable represents a named, typed value (along with qualifiers, etc).
133     * Every Variable is mapped to one (or several, contiguous) indices into our vector of
134     * skvm::Val. Those skvm::Val entries hold the current actual value of that variable.
135     *
136     * NOTE: Conceptually, each Variable is just mapped to a Value. We could implement it that way,
137     * (and eliminate the indirection), but it would add overhead for each Variable,
138     * and add additional (different) bookkeeping for things like lvalue-swizzles.
139     *
140     * Any time a variable appears in an expression, that's a VariableReference, which is a kind of
141     * Expression. Evaluating that VariableReference (or any other Expression) produces a Value,
142     * which is a set of skvm::Val. (This allows an Expression to produce a vector or matrix, in
143     * addition to a scalar).
144     *
145     * For a VariableReference, producing a Value is straightforward - we get the slot of the
146     * Variable (from fVariableMap), use that to look up the current skvm::Vals holding the
147     * variable's contents, and construct a Value with those ids.
148     */
149
150    /** Appends this variable to the SkVMSlotInfo array inside of SkVMDebugInfo. */
151    void addDebugSlotInfo(String varName, const Type& type, int line);
152
153    /**
154     * Returns the slot holding v's Val(s). Allocates storage if this is first time 'v' is
155     * referenced. Compound variables (e.g. vectors) will consume more than one slot, with
156     * getSlot returning the start of the contiguous chunk of slots.
157     */
158    size_t getSlot(const Variable& v);
159
160    /**
161     * Writes a value to a slot previously created by getSlot.
162     */
163    void writeToSlot(int slot, skvm::Val value);
164
165    /**
166     * Emits an trace_line opcode. writeStatement already does this, but statements that alter
167     * control flow may need to explicitly add additional traces.
168     */
169    void emitTraceLine(int line);
170
171    /** Initializes uniforms and global variables at the start of main(). */
172    void setupGlobals(SkSpan<skvm::Val> uniforms, skvm::Coord device);
173
174    /** Emits an SkSL function. */
175    void writeFunction(const FunctionDefinition& function,
176                       SkSpan<skvm::Val> arguments,
177                       SkSpan<skvm::Val> outReturn);
178
179    skvm::F32 f32(skvm::Val id) { SkASSERT(id != skvm::NA); return {fBuilder, id}; }
180    skvm::I32 i32(skvm::Val id) { SkASSERT(id != skvm::NA); return {fBuilder, id}; }
181
182    // Shorthand for scalars
183    skvm::F32 f32(const Value& v) { SkASSERT(v.slots() == 1); return f32(v[0]); }
184    skvm::I32 i32(const Value& v) { SkASSERT(v.slots() == 1); return i32(v[0]); }
185
186    template <typename Fn>
187    Value unary(const Value& v, Fn&& fn) {
188        Value result(v.slots());
189        for (size_t i = 0; i < v.slots(); ++i) {
190            result[i] = fn({fBuilder, v[i]});
191        }
192        return result;
193    }
194
195    skvm::I32 mask() {
196        // Mask off execution if we have encountered `break` or `continue` on this path.
197        skvm::I32 result = fConditionMask & fLoopMask;
198        if (!fFunctionStack.empty()) {
199            // As we encounter (possibly conditional) return statements, fReturned is updated to
200            // store the lanes that have already returned. For the remainder of the current
201            // function, those lanes should be disabled.
202            result = result & ~currentFunction().fReturned;
203        }
204        return result;
205    }
206
207    size_t fieldSlotOffset(const FieldAccess& expr);
208    size_t indexSlotOffset(const IndexExpression& expr);
209
210    Value writeExpression(const Expression& expr);
211    Value writeBinaryExpression(const BinaryExpression& b);
212    Value writeAggregationConstructor(const AnyConstructor& c);
213    Value writeChildCall(const ChildCall& c);
214    Value writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c);
215    Value writeConstructorMatrixResize(const ConstructorMatrixResize& c);
216    Value writeConstructorCast(const AnyConstructor& c);
217    Value writeConstructorSplat(const ConstructorSplat& c);
218    Value writeFunctionCall(const FunctionCall& c);
219    Value writeExternalFunctionCall(const ExternalFunctionCall& c);
220    Value writeFieldAccess(const FieldAccess& expr);
221    Value writeLiteral(const Literal& l);
222    Value writeIndexExpression(const IndexExpression& expr);
223    Value writeIntrinsicCall(const FunctionCall& c);
224    Value writePostfixExpression(const PostfixExpression& p);
225    Value writePrefixExpression(const PrefixExpression& p);
226    Value writeSwizzle(const Swizzle& swizzle);
227    Value writeTernaryExpression(const TernaryExpression& t);
228    Value writeVariableExpression(const VariableReference& expr);
229
230    Value writeTypeConversion(const Value& src, Type::NumberKind srcKind, Type::NumberKind dstKind);
231
232    void writeStatement(const Statement& s);
233    void writeBlock(const Block& b);
234    void writeBreakStatement();
235    void writeContinueStatement();
236    void writeForStatement(const ForStatement& f);
237    void writeIfStatement(const IfStatement& stmt);
238    void writeReturnStatement(const ReturnStatement& r);
239    void writeSwitchStatement(const SwitchStatement& s);
240    void writeVarDeclaration(const VarDeclaration& decl);
241
242    Value writeStore(const Expression& lhs, const Value& rhs);
243    skvm::Val writeConditionalStore(skvm::Val lhs, skvm::Val rhs, skvm::I32 mask);
244
245    Value writeMatrixInverse2x2(const Value& m);
246    Value writeMatrixInverse3x3(const Value& m);
247    Value writeMatrixInverse4x4(const Value& m);
248
249    //
250    // Global state for the lifetime of the generator:
251    //
252    const Program& fProgram;
253    skvm::Builder* fBuilder;
254    SkVMDebugInfo* fDebugInfo;
255
256    const SampleShaderFn fSampleShader;
257    const SampleColorFilterFn fSampleColorFilter;
258    const SampleBlenderFn fSampleBlender;
259
260    struct Slot {
261        skvm::Val         val;
262    };
263    std::vector<Slot> fSlots;
264
265    std::unordered_map<const Variable*, size_t> fVariableMap;  // [Variable, first slot in fSlots]
266
267    // Conditional execution mask (managed by ScopedCondition, and tied to control-flow scopes)
268    skvm::I32 fConditionMask;
269
270    // Similar: loop execution masks. Each loop starts with all lanes active (fLoopMask).
271    // 'break' disables a lane in fLoopMask until the loop finishes
272    // 'continue' disables a lane in fLoopMask, and sets fContinueMask to be re-enabled on the next
273    //   iteration
274    skvm::I32 fLoopMask;
275    skvm::I32 fContinueMask;
276
277    //
278    // State that's local to the generation of a single function:
279    //
280    struct Function {
281        const SkSpan<skvm::Val> fReturnValue;
282        skvm::I32               fReturned;
283    };
284    std::vector<Function> fFunctionStack;
285    Function& currentFunction() { return fFunctionStack.back(); }
286
287    class ScopedCondition {
288    public:
289        ScopedCondition(SkVMGenerator* generator, skvm::I32 mask)
290                : fGenerator(generator), fOldConditionMask(fGenerator->fConditionMask) {
291            fGenerator->fConditionMask &= mask;
292        }
293
294        ~ScopedCondition() { fGenerator->fConditionMask = fOldConditionMask; }
295
296    private:
297        SkVMGenerator* fGenerator;
298        skvm::I32 fOldConditionMask;
299    };
300};
301
302void SkVMDebugInfo::dump(SkWStream* o) const {
303    for (size_t index = 0; index < fSlotInfo.size(); ++index) {
304        const SkVMSlotInfo& info = fSlotInfo[index];
305
306        o->writeText("$");
307        o->writeDecAsText(index);
308        o->writeText(" = ");
309        o->writeText(info.name.c_str());
310        o->writeText(" (");
311        switch (info.numberKind) {
312            case Type::NumberKind::kFloat:      o->writeText("float"); break;
313            case Type::NumberKind::kSigned:     o->writeText("int"); break;
314            case Type::NumberKind::kUnsigned:   o->writeText("uint"); break;
315            case Type::NumberKind::kBoolean:    o->writeText("bool"); break;
316            case Type::NumberKind::kNonnumeric: o->writeText("???"); break;
317        }
318        if (info.rows * info.columns > 1) {
319            o->writeDecAsText(info.columns);
320            if (info.rows != 1) {
321                o->writeText("x");
322                o->writeDecAsText(info.rows);
323            }
324            o->writeText(" : ");
325            o->writeText("slot ");
326            o->writeDecAsText(info.componentIndex + 1);
327            o->writeText("/");
328            o->writeDecAsText(info.rows * info.columns);
329        }
330        o->writeText(", L");
331        o->writeDecAsText(info.line);
332        o->writeText(")");
333        o->newline();
334    }
335    o->newline();
336}
337
338static Type::NumberKind base_number_kind(const Type& type) {
339    if (type.typeKind() == Type::TypeKind::kMatrix || type.typeKind() == Type::TypeKind::kVector) {
340        return base_number_kind(type.componentType());
341    }
342    return type.numberKind();
343}
344
345static inline bool is_uniform(const SkSL::Variable& var) {
346    return var.modifiers().fFlags & Modifiers::kUniform_Flag;
347}
348
349SkVMGenerator::SkVMGenerator(const Program& program,
350                             skvm::Builder* builder,
351                             SkVMDebugInfo* debugInfo,
352                             SampleShaderFn sampleShader,
353                             SampleColorFilterFn sampleColorFilter,
354                             SampleBlenderFn sampleBlender)
355        : fProgram(program)
356        , fBuilder(builder)
357        , fDebugInfo(debugInfo)
358        , fSampleShader(std::move(sampleShader))
359        , fSampleColorFilter(std::move(sampleColorFilter))
360        , fSampleBlender(std::move(sampleBlender)) {}
361
362void SkVMGenerator::writeProgram(SkSpan<skvm::Val> uniforms,
363                                 skvm::Coord device,
364                                 const FunctionDefinition& function,
365                                 SkSpan<skvm::Val> arguments,
366                                 SkSpan<skvm::Val> outReturn) {
367    fConditionMask = fLoopMask = fBuilder->splat(0xffff'ffff);
368
369    this->setupGlobals(uniforms, device);
370    this->writeFunction(function, arguments, outReturn);
371}
372
373void SkVMGenerator::setupGlobals(SkSpan<skvm::Val> uniforms, skvm::Coord device) {
374    // Add storage for each global variable (including uniforms) to fSlots, and entries in
375    // fVariableMap to remember where every variable is stored.
376    const skvm::Val* uniformIter = uniforms.begin();
377    size_t fpCount = 0;
378    for (const ProgramElement* e : fProgram.elements()) {
379        if (e->is<GlobalVarDeclaration>()) {
380            const GlobalVarDeclaration& gvd = e->as<GlobalVarDeclaration>();
381            const VarDeclaration& decl = gvd.declaration()->as<VarDeclaration>();
382            const Variable& var = decl.var();
383            SkASSERT(fVariableMap.find(&var) == fVariableMap.end());
384
385            // For most variables, fVariableMap stores an index into fSlots, but for children,
386            // fVariableMap stores the index to pass to fSample(Shader|ColorFilter|Blender)
387            if (var.type().isEffectChild()) {
388                fVariableMap[&var] = fpCount++;
389                continue;
390            }
391
392            // Opaque types include fragment processors, GL objects (samplers, textures, etc), and
393            // special types like 'void'. Of those, only fragment processors are legal variables.
394            SkASSERT(!var.type().isOpaque());
395
396            // getSlot() allocates space for the variable's value in fSlots, initializes it to zero,
397            // and populates fVariableMap.
398            size_t slot   = this->getSlot(var),
399                   nslots = var.type().slotCount();
400
401            // builtin variables are system-defined, with special semantics. The only builtin
402            // variable exposed to runtime effects is sk_FragCoord.
403            if (int builtin = var.modifiers().fLayout.fBuiltin; builtin >= 0) {
404                switch (builtin) {
405                    case SK_FRAGCOORD_BUILTIN:
406                        SkASSERT(nslots == 4);
407                        this->writeToSlot(slot + 0, device.x.id);
408                        this->writeToSlot(slot + 1, device.y.id);
409                        this->writeToSlot(slot + 2, fBuilder->splat(0.0f).id);
410                        this->writeToSlot(slot + 3, fBuilder->splat(1.0f).id);
411                        break;
412                    default:
413                        SkDEBUGFAILF("Unsupported builtin %d", builtin);
414                }
415                continue;
416            }
417
418            // For uniforms, copy the supplied IDs over
419            if (is_uniform(var)) {
420                SkASSERT(uniformIter + nslots <= uniforms.end());
421                for (size_t i = 0; i < nslots; ++i) {
422                    this->writeToSlot(slot + i, uniformIter[i]);
423                }
424                uniformIter += nslots;
425                continue;
426            }
427
428            // For other globals, populate with the initializer expression (if there is one)
429            if (decl.value()) {
430                Value val = this->writeExpression(*decl.value());
431                for (size_t i = 0; i < nslots; ++i) {
432                    this->writeToSlot(slot + i, val[i]);
433                }
434            }
435        }
436    }
437    SkASSERT(uniformIter == uniforms.end());
438}
439
440void SkVMGenerator::writeFunction(const FunctionDefinition& function,
441                                  SkSpan<skvm::Val> arguments,
442                                  SkSpan<skvm::Val> outReturn) {
443    const FunctionDeclaration& decl = function.declaration();
444    SkASSERT(decl.returnType().slotCount() == outReturn.size());
445
446    if (fDebugInfo) {
447        fBuilder->trace_call_enter(this->mask(), function.fLine);
448    }
449
450    fFunctionStack.push_back({outReturn, /*returned=*/fBuilder->splat(0)});
451
452    // For all parameters, copy incoming argument IDs to our vector of (all) variable IDs
453    size_t argIdx = 0;
454    for (const Variable* p : decl.parameters()) {
455        size_t paramSlot = this->getSlot(*p),
456               nslots    = p->type().slotCount();
457
458        for (size_t i = 0; i < nslots; ++i) {
459            this->writeToSlot(paramSlot + i, arguments[argIdx + i]);
460        }
461        argIdx += nslots;
462    }
463    SkASSERT(argIdx == arguments.size());
464
465    this->writeStatement(*function.body());
466
467    // Copy 'out' and 'inout' parameters back to their caller-supplied argument storage
468    argIdx = 0;
469    for (const Variable* p : decl.parameters()) {
470        size_t nslots = p->type().slotCount();
471
472        if (p->modifiers().fFlags & Modifiers::kOut_Flag) {
473            size_t paramSlot = this->getSlot(*p);
474            for (size_t i = 0; i < nslots; ++i) {
475                arguments[argIdx + i] = fSlots[paramSlot + i].val;
476            }
477        }
478        argIdx += nslots;
479    }
480    SkASSERT(argIdx == arguments.size());
481
482    fFunctionStack.pop_back();
483
484    if (fDebugInfo) {
485        fBuilder->trace_call_exit(this->mask(), function.fLine);
486    }
487}
488
489void SkVMGenerator::writeToSlot(int slot, skvm::Val value) {
490    if (fDebugInfo && fSlots[slot].val != value) {
491        if (fDebugInfo->fSlotInfo[slot].numberKind == Type::NumberKind::kFloat) {
492            fBuilder->trace_var(this->mask(), slot, f32(value));
493        } else if (fDebugInfo->fSlotInfo[slot].numberKind == Type::NumberKind::kBoolean) {
494            fBuilder->trace_var(this->mask(), slot, bool(value));
495        } else {
496            fBuilder->trace_var(this->mask(), slot, i32(value));
497        }
498    }
499
500    fSlots[slot].val = value;
501}
502
503void SkVMGenerator::addDebugSlotInfo(String varName, const Type& type, int line) {
504    SkASSERT(fDebugInfo);
505    switch (type.typeKind()) {
506        case Type::TypeKind::kArray: {
507            int nslots = type.columns();
508            const Type& elemType = type.componentType();
509            for (int slot = 0; slot < nslots; ++slot) {
510                this->addDebugSlotInfo(varName + "[" + to_string(slot) + "]",
511                                       elemType,
512                                       line);
513            }
514            break;
515        }
516        case Type::TypeKind::kStruct: {
517            for (const Type::Field& field : type.fields()) {
518                this->addDebugSlotInfo(varName + "." + field.fName,
519                                       *field.fType,
520                                       line);
521            }
522            break;
523        }
524        default:
525            SkASSERTF(0, "unsupported slot type %d", (int)type.typeKind());
526            [[fallthrough]];
527
528        case Type::TypeKind::kScalar:
529        case Type::TypeKind::kVector:
530        case Type::TypeKind::kMatrix: {
531            Type::NumberKind numberKind = type.componentType().numberKind();
532            int nslots = type.slotCount();
533
534            for (int slot = 0; slot < nslots; ++slot) {
535                SkVMSlotInfo slotInfo;
536                slotInfo.name = varName;
537                slotInfo.columns = type.columns();
538                slotInfo.rows = type.rows();
539                slotInfo.componentIndex = slot;
540                slotInfo.numberKind = numberKind;
541                slotInfo.line = line;
542                fDebugInfo->fSlotInfo.push_back(std::move(slotInfo));
543            }
544            break;
545        }
546    }
547}
548
549size_t SkVMGenerator::getSlot(const Variable& v) {
550    auto entry = fVariableMap.find(&v);
551    if (entry != fVariableMap.end()) {
552        return entry->second;
553    }
554
555    size_t slot   = fSlots.size(),
556           nslots = v.type().slotCount();
557
558    if (fDebugInfo) {
559        // Our debug slot-info table should always have the same length as the actual slot table.
560        SkASSERT(fDebugInfo->fSlotInfo.size() == slot);
561
562        // Append slots for this variable to our debug slot-info table.
563        fDebugInfo->fSlotInfo.reserve(slot + nslots);
564        this->addDebugSlotInfo(String(v.name()), v.type(), v.fLine);
565
566        // Confirm that we added the expected number of slots.
567        SkASSERT(fDebugInfo->fSlotInfo.size() == (slot + nslots));
568    }
569
570    // Create zeroed-out slots for this new variable.
571    skvm::Val initialValue = fBuilder->splat(0.0f).id;
572    fSlots.insert(fSlots.end(), nslots, Slot{initialValue});
573    fVariableMap[&v] = slot;
574    return slot;
575}
576
577Value SkVMGenerator::writeBinaryExpression(const BinaryExpression& b) {
578    const Expression& left = *b.left();
579    const Expression& right = *b.right();
580    Operator op = b.getOperator();
581    if (op.kind() == Token::Kind::TK_EQ) {
582        return this->writeStore(left, this->writeExpression(right));
583    }
584
585    const Type& lType = left.type();
586    const Type& rType = right.type();
587    bool lVecOrMtx = (lType.isVector() || lType.isMatrix());
588    bool rVecOrMtx = (rType.isVector() || rType.isMatrix());
589    bool isAssignment = op.isAssignment();
590    if (isAssignment) {
591        op = op.removeAssignment();
592    }
593    Type::NumberKind nk = base_number_kind(lType);
594
595    // A few ops require special treatment:
596    switch (op.kind()) {
597        case Token::Kind::TK_LOGICALAND: {
598            SkASSERT(!isAssignment);
599            SkASSERT(nk == Type::NumberKind::kBoolean);
600            skvm::I32 lVal = i32(this->writeExpression(left));
601            ScopedCondition shortCircuit(this, lVal);
602            skvm::I32 rVal = i32(this->writeExpression(right));
603            return lVal & rVal;
604        }
605        case Token::Kind::TK_LOGICALOR: {
606            SkASSERT(!isAssignment);
607            SkASSERT(nk == Type::NumberKind::kBoolean);
608            skvm::I32 lVal = i32(this->writeExpression(left));
609            ScopedCondition shortCircuit(this, ~lVal);
610            skvm::I32 rVal = i32(this->writeExpression(right));
611            return lVal | rVal;
612        }
613        case Token::Kind::TK_COMMA:
614            // We write the left side of the expression to preserve its side effects, even though we
615            // immediately discard the result.
616            this->writeExpression(left);
617            return this->writeExpression(right);
618        default:
619            break;
620    }
621
622    // All of the other ops always evaluate both sides of the expression
623    Value lVal = this->writeExpression(left),
624          rVal = this->writeExpression(right);
625
626    // Special case for M*V, V*M, M*M (but not V*V!)
627    if (op.kind() == Token::Kind::TK_STAR
628        && lVecOrMtx && rVecOrMtx && !(lType.isVector() && rType.isVector())) {
629        int rCols = rType.columns(),
630            rRows = rType.rows(),
631            lCols = lType.columns(),
632            lRows = lType.rows();
633        // M*V treats the vector as a column
634        if (rType.isVector()) {
635            std::swap(rCols, rRows);
636        }
637        SkASSERT(lCols == rRows);
638        SkASSERT(b.type().slotCount() == static_cast<size_t>(lRows * rCols));
639        Value result(lRows * rCols);
640        size_t resultIdx = 0;
641        const skvm::F32 zero = fBuilder->splat(0.0f);
642        for (int c = 0; c < rCols; ++c)
643        for (int r = 0; r < lRows; ++r) {
644            skvm::F32 sum = zero;
645            for (int j = 0; j < lCols; ++j) {
646                sum += f32(lVal[j*lRows + r]) * f32(rVal[c*rRows + j]);
647            }
648            result[resultIdx++] = sum;
649        }
650        SkASSERT(resultIdx == result.slots());
651        return isAssignment ? this->writeStore(left, result) : result;
652    }
653
654    size_t nslots = std::max(lVal.slots(), rVal.slots());
655
656    auto binary = [&](auto&& f_fn, auto&& i_fn) {
657        Value result(nslots);
658        for (size_t i = 0; i < nslots; ++i) {
659            // If one side is scalar, replicate it to all channels
660            skvm::Val L = lVal.slots() == 1 ? lVal[0] : lVal[i],
661                      R = rVal.slots() == 1 ? rVal[0] : rVal[i];
662            if (nk == Type::NumberKind::kFloat) {
663                result[i] = f_fn(f32(L), f32(R));
664            } else {
665                result[i] = i_fn(i32(L), i32(R));
666            }
667        }
668        return isAssignment ? this->writeStore(left, result) : result;
669    };
670
671    auto unsupported_f = [&](skvm::F32, skvm::F32) {
672        SkDEBUGFAIL("Unsupported operator");
673        return skvm::F32{};
674    };
675
676    switch (op.kind()) {
677        case Token::Kind::TK_EQEQ: {
678            SkASSERT(!isAssignment);
679            Value cmp = binary([](skvm::F32 x, skvm::F32 y) { return x == y; },
680                               [](skvm::I32 x, skvm::I32 y) { return x == y; });
681            skvm::I32 folded = i32(cmp[0]);
682            for (size_t i = 1; i < nslots; ++i) {
683                folded &= i32(cmp[i]);
684            }
685            return folded;
686        }
687        case Token::Kind::TK_NEQ: {
688            SkASSERT(!isAssignment);
689            Value cmp = binary([](skvm::F32 x, skvm::F32 y) { return x != y; },
690                               [](skvm::I32 x, skvm::I32 y) { return x != y; });
691            skvm::I32 folded = i32(cmp[0]);
692            for (size_t i = 1; i < nslots; ++i) {
693                folded |= i32(cmp[i]);
694            }
695            return folded;
696        }
697        case Token::Kind::TK_GT:
698            return binary([](skvm::F32 x, skvm::F32 y) { return x > y; },
699                          [](skvm::I32 x, skvm::I32 y) { return x > y; });
700        case Token::Kind::TK_GTEQ:
701            return binary([](skvm::F32 x, skvm::F32 y) { return x >= y; },
702                          [](skvm::I32 x, skvm::I32 y) { return x >= y; });
703        case Token::Kind::TK_LT:
704            return binary([](skvm::F32 x, skvm::F32 y) { return x < y; },
705                          [](skvm::I32 x, skvm::I32 y) { return x < y; });
706        case Token::Kind::TK_LTEQ:
707            return binary([](skvm::F32 x, skvm::F32 y) { return x <= y; },
708                          [](skvm::I32 x, skvm::I32 y) { return x <= y; });
709
710        case Token::Kind::TK_PLUS:
711            return binary([](skvm::F32 x, skvm::F32 y) { return x + y; },
712                          [](skvm::I32 x, skvm::I32 y) { return x + y; });
713        case Token::Kind::TK_MINUS:
714            return binary([](skvm::F32 x, skvm::F32 y) { return x - y; },
715                          [](skvm::I32 x, skvm::I32 y) { return x - y; });
716        case Token::Kind::TK_STAR:
717            return binary([](skvm::F32 x, skvm::F32 y) { return x ** y; },
718                          [](skvm::I32 x, skvm::I32 y) { return x * y; });
719        case Token::Kind::TK_SLASH:
720            // Minimum spec (GLSL ES 1.0) has very loose requirements for integer operations.
721            // (Low-end GPUs may not have integer ALUs). Given that, we are allowed to do floating
722            // point division plus rounding. Section 10.28 of the spec even clarifies that the
723            // rounding mode is undefined (but round-towards-zero is the obvious/common choice).
724            return binary([](skvm::F32 x, skvm::F32 y) { return x / y; },
725                          [](skvm::I32 x, skvm::I32 y) {
726                              return skvm::trunc(skvm::to_F32(x) / skvm::to_F32(y));
727                          });
728
729        case Token::Kind::TK_BITWISEXOR:
730        case Token::Kind::TK_LOGICALXOR:
731            return binary(unsupported_f, [](skvm::I32 x, skvm::I32 y) { return x ^ y; });
732        case Token::Kind::TK_BITWISEAND:
733            return binary(unsupported_f, [](skvm::I32 x, skvm::I32 y) { return x & y; });
734        case Token::Kind::TK_BITWISEOR:
735            return binary(unsupported_f, [](skvm::I32 x, skvm::I32 y) { return x | y; });
736
737        // These three operators are all 'reserved' (illegal) in our minimum spec, but will require
738        // implementation in the future.
739        case Token::Kind::TK_PERCENT:
740        case Token::Kind::TK_SHL:
741        case Token::Kind::TK_SHR:
742        default:
743            SkDEBUGFAIL("Unsupported operator");
744            return {};
745    }
746}
747
748Value SkVMGenerator::writeAggregationConstructor(const AnyConstructor& c) {
749    Value result(c.type().slotCount());
750    size_t resultIdx = 0;
751    for (const auto &arg : c.argumentSpan()) {
752        Value tmp = this->writeExpression(*arg);
753        for (size_t tmpSlot = 0; tmpSlot < tmp.slots(); ++tmpSlot) {
754            result[resultIdx++] = tmp[tmpSlot];
755        }
756    }
757    return result;
758}
759
760Value SkVMGenerator::writeTypeConversion(const Value& src,
761                                         Type::NumberKind srcKind,
762                                         Type::NumberKind dstKind) {
763    // Conversion among "similar" types (floatN <-> halfN), (shortN <-> intN), etc. is a no-op.
764    if (srcKind == dstKind) {
765        return src;
766    }
767
768    // TODO: Handle signed vs. unsigned. GLSL ES 1.0 only has 'int', so no problem yet.
769    Value dst(src.slots());
770    switch (dstKind) {
771        case Type::NumberKind::kFloat:
772            if (srcKind == Type::NumberKind::kSigned) {
773                // int -> float
774                for (size_t i = 0; i < src.slots(); ++i) {
775                    dst[i] = skvm::to_F32(i32(src[i]));
776                }
777                return dst;
778            }
779            if (srcKind == Type::NumberKind::kBoolean) {
780                // bool -> float
781                for (size_t i = 0; i < src.slots(); ++i) {
782                    dst[i] = skvm::select(i32(src[i]), 1.0f, 0.0f);
783                }
784                return dst;
785            }
786            break;
787
788        case Type::NumberKind::kSigned:
789            if (srcKind == Type::NumberKind::kFloat) {
790                // float -> int
791                for (size_t i = 0; i < src.slots(); ++i) {
792                    dst[i] = skvm::trunc(f32(src[i]));
793                }
794                return dst;
795            }
796            if (srcKind == Type::NumberKind::kBoolean) {
797                // bool -> int
798                for (size_t i = 0; i < src.slots(); ++i) {
799                    dst[i] = skvm::select(i32(src[i]), 1, 0);
800                }
801                return dst;
802            }
803            break;
804
805        case Type::NumberKind::kBoolean:
806            if (srcKind == Type::NumberKind::kSigned) {
807                // int -> bool
808                for (size_t i = 0; i < src.slots(); ++i) {
809                    dst[i] = i32(src[i]) != 0;
810                }
811                return dst;
812            }
813            if (srcKind == Type::NumberKind::kFloat) {
814                // float -> bool
815                for (size_t i = 0; i < src.slots(); ++i) {
816                    dst[i] = f32(src[i]) != 0.0;
817                }
818                return dst;
819            }
820            break;
821
822        default:
823            break;
824    }
825    SkDEBUGFAILF("Unsupported type conversion: %d -> %d", (int)srcKind, (int)dstKind);
826    return {};
827}
828
829Value SkVMGenerator::writeConstructorCast(const AnyConstructor& c) {
830    auto arguments = c.argumentSpan();
831    SkASSERT(arguments.size() == 1);
832    const Expression& argument = *arguments.front();
833
834    const Type& srcType = argument.type();
835    const Type& dstType = c.type();
836    Type::NumberKind srcKind = base_number_kind(srcType);
837    Type::NumberKind dstKind = base_number_kind(dstType);
838    Value src = this->writeExpression(argument);
839    return this->writeTypeConversion(src, srcKind, dstKind);
840}
841
842Value SkVMGenerator::writeConstructorSplat(const ConstructorSplat& c) {
843    SkASSERT(c.type().isVector());
844    SkASSERT(c.argument()->type().isScalar());
845    int columns = c.type().columns();
846
847    // Splat the argument across all components of a vector.
848    Value src = this->writeExpression(*c.argument());
849    Value dst(columns);
850    for (int i = 0; i < columns; ++i) {
851        dst[i] = src[0];
852    }
853    return dst;
854}
855
856Value SkVMGenerator::writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& ctor) {
857    const Type& dstType = ctor.type();
858    SkASSERT(dstType.isMatrix());
859    SkASSERT(ctor.argument()->type() == dstType.componentType());
860
861    Value src = this->writeExpression(*ctor.argument());
862    Value dst(dstType.rows() * dstType.columns());
863    size_t dstIndex = 0;
864
865    // Matrix-from-scalar builds a diagonal scale matrix
866    const skvm::F32 zero = fBuilder->splat(0.0f);
867    for (int c = 0; c < dstType.columns(); ++c) {
868        for (int r = 0; r < dstType.rows(); ++r) {
869            dst[dstIndex++] = (c == r ? f32(src) : zero);
870        }
871    }
872
873    SkASSERT(dstIndex == dst.slots());
874    return dst;
875}
876
877Value SkVMGenerator::writeConstructorMatrixResize(const ConstructorMatrixResize& ctor) {
878    const Type& srcType = ctor.argument()->type();
879    const Type& dstType = ctor.type();
880    Value src = this->writeExpression(*ctor.argument());
881    Value dst(dstType.rows() * dstType.columns());
882
883    // Matrix-from-matrix uses src where it overlaps, and fills in missing fields with identity.
884    size_t dstIndex = 0;
885    for (int c = 0; c < dstType.columns(); ++c) {
886        for (int r = 0; r < dstType.rows(); ++r) {
887            if (c < srcType.columns() && r < srcType.rows()) {
888                dst[dstIndex++] = src[c * srcType.rows() + r];
889            } else {
890                dst[dstIndex++] = fBuilder->splat(c == r ? 1.0f : 0.0f);
891            }
892        }
893    }
894
895    SkASSERT(dstIndex == dst.slots());
896    return dst;
897}
898
899size_t SkVMGenerator::fieldSlotOffset(const FieldAccess& expr) {
900    size_t offset = 0;
901    for (int i = 0; i < expr.fieldIndex(); ++i) {
902        offset += (*expr.base()->type().fields()[i].fType).slotCount();
903    }
904    return offset;
905}
906
907Value SkVMGenerator::writeFieldAccess(const FieldAccess& expr) {
908    Value base = this->writeExpression(*expr.base());
909    Value field(expr.type().slotCount());
910    size_t offset = this->fieldSlotOffset(expr);
911    for (size_t i = 0; i < field.slots(); ++i) {
912        field[i] = base[offset + i];
913    }
914    return field;
915}
916
917size_t SkVMGenerator::indexSlotOffset(const IndexExpression& expr) {
918    Value index = this->writeExpression(*expr.index());
919    int indexValue = -1;
920    SkAssertResult(fBuilder->allImm(index[0], &indexValue));
921
922    // When indexing by a literal, the front-end guarantees that we don't go out of bounds.
923    // But when indexing by a loop variable, it's possible to generate out-of-bounds access.
924    // The GLSL spec leaves that behavior undefined - we'll just clamp everything here.
925    indexValue = SkTPin(indexValue, 0, expr.base()->type().columns() - 1);
926
927    size_t stride = expr.type().slotCount();
928    return indexValue * stride;
929}
930
931Value SkVMGenerator::writeIndexExpression(const IndexExpression& expr) {
932    Value base = this->writeExpression(*expr.base());
933    Value element(expr.type().slotCount());
934    size_t offset = this->indexSlotOffset(expr);
935    for (size_t i = 0; i < element.slots(); ++i) {
936        element[i] = base[offset + i];
937    }
938    return element;
939}
940
941Value SkVMGenerator::writeVariableExpression(const VariableReference& expr) {
942    size_t slot = this->getSlot(*expr.variable());
943    Value val(expr.type().slotCount());
944    for (size_t i = 0; i < val.slots(); ++i) {
945        val[i] = fSlots[slot + i].val;
946    }
947    return val;
948}
949
950Value SkVMGenerator::writeMatrixInverse2x2(const Value& m) {
951    SkASSERT(m.slots() == 4);
952    skvm::F32 a = f32(m[0]),
953              b = f32(m[1]),
954              c = f32(m[2]),
955              d = f32(m[3]);
956    skvm::F32 idet = 1.0f / (a*d - b*c);
957
958    Value result(m.slots());
959    result[0] = ( d ** idet);
960    result[1] = (-b ** idet);
961    result[2] = (-c ** idet);
962    result[3] = ( a ** idet);
963    return result;
964}
965
966Value SkVMGenerator::writeMatrixInverse3x3(const Value& m) {
967    SkASSERT(m.slots() == 9);
968    skvm::F32 a11 = f32(m[0]), a12 = f32(m[3]), a13 = f32(m[6]),
969              a21 = f32(m[1]), a22 = f32(m[4]), a23 = f32(m[7]),
970              a31 = f32(m[2]), a32 = f32(m[5]), a33 = f32(m[8]);
971    skvm::F32 idet = 1.0f / (a11*a22*a33 + a12*a23*a31 + a13*a21*a32 -
972                             a11*a23*a32 - a12*a21*a33 - a13*a22*a31);
973
974    Value result(m.slots());
975    result[0] = ((a22**a33 - a23**a32) ** idet);
976    result[1] = ((a23**a31 - a21**a33) ** idet);
977    result[2] = ((a21**a32 - a22**a31) ** idet);
978    result[3] = ((a13**a32 - a12**a33) ** idet);
979    result[4] = ((a11**a33 - a13**a31) ** idet);
980    result[5] = ((a12**a31 - a11**a32) ** idet);
981    result[6] = ((a12**a23 - a13**a22) ** idet);
982    result[7] = ((a13**a21 - a11**a23) ** idet);
983    result[8] = ((a11**a22 - a12**a21) ** idet);
984    return result;
985}
986
987Value SkVMGenerator::writeMatrixInverse4x4(const Value& m) {
988    SkASSERT(m.slots() == 16);
989    skvm::F32 a00 = f32(m[0]), a10 = f32(m[4]), a20 = f32(m[ 8]), a30 = f32(m[12]),
990              a01 = f32(m[1]), a11 = f32(m[5]), a21 = f32(m[ 9]), a31 = f32(m[13]),
991              a02 = f32(m[2]), a12 = f32(m[6]), a22 = f32(m[10]), a32 = f32(m[14]),
992              a03 = f32(m[3]), a13 = f32(m[7]), a23 = f32(m[11]), a33 = f32(m[15]);
993
994    skvm::F32 b00 = a00**a11 - a01**a10,
995              b01 = a00**a12 - a02**a10,
996              b02 = a00**a13 - a03**a10,
997              b03 = a01**a12 - a02**a11,
998              b04 = a01**a13 - a03**a11,
999              b05 = a02**a13 - a03**a12,
1000              b06 = a20**a31 - a21**a30,
1001              b07 = a20**a32 - a22**a30,
1002              b08 = a20**a33 - a23**a30,
1003              b09 = a21**a32 - a22**a31,
1004              b10 = a21**a33 - a23**a31,
1005              b11 = a22**a33 - a23**a32;
1006
1007    skvm::F32 idet = 1.0f / (b00**b11 - b01**b10 + b02**b09 + b03**b08 - b04**b07 + b05**b06);
1008
1009    b00 *= idet;
1010    b01 *= idet;
1011    b02 *= idet;
1012    b03 *= idet;
1013    b04 *= idet;
1014    b05 *= idet;
1015    b06 *= idet;
1016    b07 *= idet;
1017    b08 *= idet;
1018    b09 *= idet;
1019    b10 *= idet;
1020    b11 *= idet;
1021
1022    Value result(m.slots());
1023    result[ 0] = (a11*b11 - a12*b10 + a13*b09);
1024    result[ 1] = (a02*b10 - a01*b11 - a03*b09);
1025    result[ 2] = (a31*b05 - a32*b04 + a33*b03);
1026    result[ 3] = (a22*b04 - a21*b05 - a23*b03);
1027    result[ 4] = (a12*b08 - a10*b11 - a13*b07);
1028    result[ 5] = (a00*b11 - a02*b08 + a03*b07);
1029    result[ 6] = (a32*b02 - a30*b05 - a33*b01);
1030    result[ 7] = (a20*b05 - a22*b02 + a23*b01);
1031    result[ 8] = (a10*b10 - a11*b08 + a13*b06);
1032    result[ 9] = (a01*b08 - a00*b10 - a03*b06);
1033    result[10] = (a30*b04 - a31*b02 + a33*b00);
1034    result[11] = (a21*b02 - a20*b04 - a23*b00);
1035    result[12] = (a11*b07 - a10*b09 - a12*b06);
1036    result[13] = (a00*b09 - a01*b07 + a02*b06);
1037    result[14] = (a31*b01 - a30*b03 - a32*b00);
1038    result[15] = (a20*b03 - a21*b01 + a22*b00);
1039    return result;
1040}
1041
1042Value SkVMGenerator::writeChildCall(const ChildCall& c) {
1043    auto child_it = fVariableMap.find(&c.child());
1044    SkASSERT(child_it != fVariableMap.end());
1045
1046    const Expression* arg = c.arguments()[0].get();
1047    Value argVal = this->writeExpression(*arg);
1048    skvm::Color color;
1049
1050    switch (c.child().type().typeKind()) {
1051        case Type::TypeKind::kShader: {
1052            SkASSERT(c.arguments().size() == 1);
1053            SkASSERT(arg->type() == *fProgram.fContext->fTypes.fFloat2);
1054            skvm::Coord coord = {f32(argVal[0]), f32(argVal[1])};
1055            color = fSampleShader(child_it->second, coord);
1056            break;
1057        }
1058        case Type::TypeKind::kColorFilter: {
1059            SkASSERT(c.arguments().size() == 1);
1060            SkASSERT(arg->type() == *fProgram.fContext->fTypes.fHalf4 ||
1061                     arg->type() == *fProgram.fContext->fTypes.fFloat4);
1062            skvm::Color inColor = {f32(argVal[0]), f32(argVal[1]), f32(argVal[2]), f32(argVal[3])};
1063            color = fSampleColorFilter(child_it->second, inColor);
1064            break;
1065        }
1066        case Type::TypeKind::kBlender: {
1067            SkASSERT(c.arguments().size() == 2);
1068            SkASSERT(arg->type() == *fProgram.fContext->fTypes.fHalf4 ||
1069                     arg->type() == *fProgram.fContext->fTypes.fFloat4);
1070            skvm::Color srcColor = {f32(argVal[0]), f32(argVal[1]), f32(argVal[2]), f32(argVal[3])};
1071
1072            arg = c.arguments()[1].get();
1073            argVal = this->writeExpression(*arg);
1074            SkASSERT(arg->type() == *fProgram.fContext->fTypes.fHalf4 ||
1075                     arg->type() == *fProgram.fContext->fTypes.fFloat4);
1076            skvm::Color dstColor = {f32(argVal[0]), f32(argVal[1]), f32(argVal[2]), f32(argVal[3])};
1077
1078            color = fSampleBlender(child_it->second, srcColor, dstColor);
1079            break;
1080        }
1081        default: {
1082            SkDEBUGFAILF("cannot sample from type '%s'", c.child().type().description().c_str());
1083        }
1084    }
1085
1086    Value result(4);
1087    result[0] = color.r;
1088    result[1] = color.g;
1089    result[2] = color.b;
1090    result[3] = color.a;
1091    return result;
1092}
1093
1094Value SkVMGenerator::writeIntrinsicCall(const FunctionCall& c) {
1095    IntrinsicKind intrinsicKind = c.function().intrinsicKind();
1096    SkASSERT(intrinsicKind != kNotIntrinsic);
1097
1098    const size_t nargs = c.arguments().size();
1099    const size_t kMaxArgs = 3;  // eg: clamp, mix, smoothstep
1100    Value args[kMaxArgs];
1101    SkASSERT(nargs >= 1 && nargs <= SK_ARRAY_COUNT(args));
1102
1103    // All other intrinsics have at most three args, and those can all be evaluated up front:
1104    for (size_t i = 0; i < nargs; ++i) {
1105        args[i] = this->writeExpression(*c.arguments()[i]);
1106    }
1107    Type::NumberKind nk = base_number_kind(c.arguments()[0]->type());
1108
1109    auto binary = [&](auto&& fn) {
1110        // Binary intrinsics are (vecN, vecN), (vecN, float), or (float, vecN)
1111        size_t nslots = std::max(args[0].slots(), args[1].slots());
1112        Value result(nslots);
1113        SkASSERT(args[0].slots() == nslots || args[0].slots() == 1);
1114        SkASSERT(args[1].slots() == nslots || args[1].slots() == 1);
1115
1116        for (size_t i = 0; i < nslots; ++i) {
1117            result[i] = fn({fBuilder, args[0][args[0].slots() == 1 ? 0 : i]},
1118                           {fBuilder, args[1][args[1].slots() == 1 ? 0 : i]});
1119        }
1120        return result;
1121    };
1122
1123    auto ternary = [&](auto&& fn) {
1124        // Ternary intrinsics are some combination of vecN and float
1125        size_t nslots = std::max({args[0].slots(), args[1].slots(), args[2].slots()});
1126        Value result(nslots);
1127        SkASSERT(args[0].slots() == nslots || args[0].slots() == 1);
1128        SkASSERT(args[1].slots() == nslots || args[1].slots() == 1);
1129        SkASSERT(args[2].slots() == nslots || args[2].slots() == 1);
1130
1131        for (size_t i = 0; i < nslots; ++i) {
1132            result[i] = fn({fBuilder, args[0][args[0].slots() == 1 ? 0 : i]},
1133                           {fBuilder, args[1][args[1].slots() == 1 ? 0 : i]},
1134                           {fBuilder, args[2][args[2].slots() == 1 ? 0 : i]});
1135        }
1136        return result;
1137    };
1138
1139    auto dot = [&](const Value& x, const Value& y) {
1140        SkASSERT(x.slots() == y.slots());
1141        skvm::F32 result = f32(x[0]) * f32(y[0]);
1142        for (size_t i = 1; i < x.slots(); ++i) {
1143            result += f32(x[i]) * f32(y[i]);
1144        }
1145        return result;
1146    };
1147
1148    switch (intrinsicKind) {
1149        case k_radians_IntrinsicKind:
1150            return unary(args[0], [](skvm::F32 deg) { return deg * (SK_FloatPI / 180); });
1151        case k_degrees_IntrinsicKind:
1152            return unary(args[0], [](skvm::F32 rad) { return rad * (180 / SK_FloatPI); });
1153
1154        case k_sin_IntrinsicKind: return unary(args[0], skvm::approx_sin);
1155        case k_cos_IntrinsicKind: return unary(args[0], skvm::approx_cos);
1156        case k_tan_IntrinsicKind: return unary(args[0], skvm::approx_tan);
1157
1158        case k_asin_IntrinsicKind: return unary(args[0], skvm::approx_asin);
1159        case k_acos_IntrinsicKind: return unary(args[0], skvm::approx_acos);
1160
1161        case k_atan_IntrinsicKind: return nargs == 1 ? unary(args[0], skvm::approx_atan)
1162                                                 : binary(skvm::approx_atan2);
1163
1164        case k_pow_IntrinsicKind:
1165            return binary([](skvm::F32 x, skvm::F32 y) { return skvm::approx_powf(x, y); });
1166        case k_exp_IntrinsicKind:  return unary(args[0], skvm::approx_exp);
1167        case k_log_IntrinsicKind:  return unary(args[0], skvm::approx_log);
1168        case k_exp2_IntrinsicKind: return unary(args[0], skvm::approx_pow2);
1169        case k_log2_IntrinsicKind: return unary(args[0], skvm::approx_log2);
1170
1171        case k_sqrt_IntrinsicKind: return unary(args[0], skvm::sqrt);
1172        case k_inversesqrt_IntrinsicKind:
1173            return unary(args[0], [](skvm::F32 x) { return 1.0f / skvm::sqrt(x); });
1174
1175        case k_abs_IntrinsicKind: return unary(args[0], skvm::abs);
1176        case k_sign_IntrinsicKind:
1177            return unary(args[0], [](skvm::F32 x) { return select(x < 0, -1.0f,
1178                                                           select(x > 0, +1.0f, 0.0f)); });
1179        case k_floor_IntrinsicKind: return unary(args[0], skvm::floor);
1180        case k_ceil_IntrinsicKind:  return unary(args[0], skvm::ceil);
1181        case k_fract_IntrinsicKind: return unary(args[0], skvm::fract);
1182        case k_mod_IntrinsicKind:
1183            return binary([](skvm::F32 x, skvm::F32 y) { return x - y*skvm::floor(x / y); });
1184
1185        case k_min_IntrinsicKind:
1186            return binary([](skvm::F32 x, skvm::F32 y) { return skvm::min(x, y); });
1187        case k_max_IntrinsicKind:
1188            return binary([](skvm::F32 x, skvm::F32 y) { return skvm::max(x, y); });
1189        case k_clamp_IntrinsicKind:
1190            return ternary(
1191                    [](skvm::F32 x, skvm::F32 lo, skvm::F32 hi) { return skvm::clamp(x, lo, hi); });
1192        case k_saturate_IntrinsicKind:
1193            return unary(args[0], [](skvm::F32 x) { return skvm::clamp01(x); });
1194        case k_mix_IntrinsicKind:
1195            return ternary(
1196                    [](skvm::F32 x, skvm::F32 y, skvm::F32 t) { return skvm::lerp(x, y, t); });
1197        case k_step_IntrinsicKind:
1198            return binary([](skvm::F32 edge, skvm::F32 x) { return select(x < edge, 0.0f, 1.0f); });
1199        case k_smoothstep_IntrinsicKind:
1200            return ternary([](skvm::F32 edge0, skvm::F32 edge1, skvm::F32 x) {
1201                skvm::F32 t = skvm::clamp01((x - edge0) / (edge1 - edge0));
1202                return t ** t ** (3 - 2 ** t);
1203            });
1204
1205        case k_length_IntrinsicKind: return skvm::sqrt(dot(args[0], args[0]));
1206        case k_distance_IntrinsicKind: {
1207            Value vec = binary([](skvm::F32 x, skvm::F32 y) { return x - y; });
1208            return skvm::sqrt(dot(vec, vec));
1209        }
1210        case k_dot_IntrinsicKind: return dot(args[0], args[1]);
1211        case k_cross_IntrinsicKind: {
1212            skvm::F32 ax = f32(args[0][0]), ay = f32(args[0][1]), az = f32(args[0][2]),
1213                      bx = f32(args[1][0]), by = f32(args[1][1]), bz = f32(args[1][2]);
1214            Value result(3);
1215            result[0] = ay**bz - az**by;
1216            result[1] = az**bx - ax**bz;
1217            result[2] = ax**by - ay**bx;
1218            return result;
1219        }
1220        case k_normalize_IntrinsicKind: {
1221            skvm::F32 invLen = 1.0f / skvm::sqrt(dot(args[0], args[0]));
1222            return unary(args[0], [&](skvm::F32 x) { return x ** invLen; });
1223        }
1224        case k_faceforward_IntrinsicKind: {
1225            const Value &N    = args[0],
1226                        &I    = args[1],
1227                        &Nref = args[2];
1228
1229            skvm::F32 dotNrefI = dot(Nref, I);
1230            return unary(N, [&](skvm::F32 n) { return select(dotNrefI<0, n, -n); });
1231        }
1232        case k_reflect_IntrinsicKind: {
1233            const Value &I = args[0],
1234                        &N = args[1];
1235
1236            skvm::F32 dotNI = dot(N, I);
1237            return binary([&](skvm::F32 i, skvm::F32 n) {
1238                return i - 2**dotNI**n;
1239            });
1240        }
1241        case k_refract_IntrinsicKind: {
1242            const Value &I  = args[0],
1243                        &N  = args[1];
1244            skvm::F32   eta = f32(args[2]);
1245
1246            skvm::F32 dotNI = dot(N, I),
1247                      k     = 1 - eta**eta**(1 - dotNI**dotNI);
1248            return binary([&](skvm::F32 i, skvm::F32 n) {
1249                return select(k<0, 0.0f, eta**i - (eta**dotNI + sqrt(k))**n);
1250            });
1251        }
1252
1253        case k_matrixCompMult_IntrinsicKind:
1254            return binary([](skvm::F32 x, skvm::F32 y) { return x ** y; });
1255        case k_inverse_IntrinsicKind: {
1256            switch (args[0].slots()) {
1257                case  4: return this->writeMatrixInverse2x2(args[0]);
1258                case  9: return this->writeMatrixInverse3x3(args[0]);
1259                case 16: return this->writeMatrixInverse4x4(args[0]);
1260                default:
1261                    SkDEBUGFAIL("Invalid call to inverse");
1262                    return {};
1263            }
1264        }
1265
1266        case k_lessThan_IntrinsicKind:
1267            return nk == Type::NumberKind::kFloat
1268                           ? binary([](skvm::F32 x, skvm::F32 y) { return x < y; })
1269                           : binary([](skvm::I32 x, skvm::I32 y) { return x < y; });
1270        case k_lessThanEqual_IntrinsicKind:
1271            return nk == Type::NumberKind::kFloat
1272                           ? binary([](skvm::F32 x, skvm::F32 y) { return x <= y; })
1273                           : binary([](skvm::I32 x, skvm::I32 y) { return x <= y; });
1274        case k_greaterThan_IntrinsicKind:
1275            return nk == Type::NumberKind::kFloat
1276                           ? binary([](skvm::F32 x, skvm::F32 y) { return x > y; })
1277                           : binary([](skvm::I32 x, skvm::I32 y) { return x > y; });
1278        case k_greaterThanEqual_IntrinsicKind:
1279            return nk == Type::NumberKind::kFloat
1280                           ? binary([](skvm::F32 x, skvm::F32 y) { return x >= y; })
1281                           : binary([](skvm::I32 x, skvm::I32 y) { return x >= y; });
1282
1283        case k_equal_IntrinsicKind:
1284            return nk == Type::NumberKind::kFloat
1285                           ? binary([](skvm::F32 x, skvm::F32 y) { return x == y; })
1286                           : binary([](skvm::I32 x, skvm::I32 y) { return x == y; });
1287        case k_notEqual_IntrinsicKind:
1288            return nk == Type::NumberKind::kFloat
1289                           ? binary([](skvm::F32 x, skvm::F32 y) { return x != y; })
1290                           : binary([](skvm::I32 x, skvm::I32 y) { return x != y; });
1291
1292        case k_any_IntrinsicKind: {
1293            skvm::I32 result = i32(args[0][0]);
1294            for (size_t i = 1; i < args[0].slots(); ++i) {
1295                result |= i32(args[0][i]);
1296            }
1297            return result;
1298        }
1299        case k_all_IntrinsicKind: {
1300            skvm::I32 result = i32(args[0][0]);
1301            for (size_t i = 1; i < args[0].slots(); ++i) {
1302                result &= i32(args[0][i]);
1303            }
1304            return result;
1305        }
1306        case k_not_IntrinsicKind: return unary(args[0], [](skvm::I32 x) { return ~x; });
1307
1308        default:
1309            SkDEBUGFAILF("unsupported intrinsic %s", c.function().description().c_str());
1310            return {};
1311    }
1312    SkUNREACHABLE;
1313}
1314
1315Value SkVMGenerator::writeFunctionCall(const FunctionCall& f) {
1316    if (f.function().isIntrinsic() && !f.function().definition()) {
1317        return this->writeIntrinsicCall(f);
1318    }
1319
1320    const FunctionDeclaration& decl = f.function();
1321
1322    // Evaluate all arguments, gather the results into a contiguous list of IDs
1323    std::vector<skvm::Val> argVals;
1324    for (const auto& arg : f.arguments()) {
1325        Value v = this->writeExpression(*arg);
1326        for (size_t i = 0; i < v.slots(); ++i) {
1327            argVals.push_back(v[i]);
1328        }
1329    }
1330
1331    // Create storage for the return value
1332    const skvm::F32 zero = fBuilder->splat(0.0f);
1333    size_t nslots = f.type().slotCount();
1334    Value result(nslots);
1335    for (size_t i = 0; i < nslots; ++i) {
1336        result[i] = zero;
1337    }
1338
1339    {
1340        // This merges currentFunction().fReturned into fConditionMask. Lanes that conditionally
1341        // returned in the current function would otherwise resume execution within the child.
1342        ScopedCondition m(this, ~currentFunction().fReturned);
1343        SkASSERTF(f.function().definition(), "no definition for function '%s'",
1344                  f.function().description().c_str());
1345        this->writeFunction(*f.function().definition(), SkMakeSpan(argVals), result.asSpan());
1346    }
1347
1348    // Propagate new values of any 'out' params back to the original arguments
1349    const std::unique_ptr<Expression>* argIter = f.arguments().begin();
1350    size_t valIdx = 0;
1351    for (const Variable* p : decl.parameters()) {
1352        nslots = p->type().slotCount();
1353        if (p->modifiers().fFlags & Modifiers::kOut_Flag) {
1354            Value v(nslots);
1355            for (size_t i = 0; i < nslots; ++i) {
1356                v[i] = argVals[valIdx + i];
1357            }
1358            const std::unique_ptr<Expression>& arg = *argIter;
1359            this->writeStore(*arg, v);
1360        }
1361        valIdx += nslots;
1362        argIter++;
1363    }
1364
1365    return result;
1366}
1367
1368Value SkVMGenerator::writeExternalFunctionCall(const ExternalFunctionCall& c) {
1369    // Evaluate all arguments, gather the results into a contiguous list of F32
1370    std::vector<skvm::F32> args;
1371    for (const auto& arg : c.arguments()) {
1372        Value v = this->writeExpression(*arg);
1373        for (size_t i = 0; i < v.slots(); ++i) {
1374            args.push_back(f32(v[i]));
1375        }
1376    }
1377
1378    // Create storage for the return value
1379    size_t nslots = c.type().slotCount();
1380    std::vector<skvm::F32> result(nslots, fBuilder->splat(0.0f));
1381
1382    c.function().call(fBuilder, args.data(), result.data(), this->mask());
1383
1384    // Convert from 'vector of F32' to Value
1385    Value resultVal(nslots);
1386    for (size_t i = 0; i < nslots; ++i) {
1387        resultVal[i] = result[i];
1388    }
1389
1390    return resultVal;
1391}
1392
1393Value SkVMGenerator::writeLiteral(const Literal& l) {
1394    if (l.type().isFloat()) {
1395        return fBuilder->splat(l.as<Literal>().floatValue());
1396    }
1397    if (l.type().isInteger()) {
1398        return fBuilder->splat(static_cast<int>(l.as<Literal>().intValue()));
1399    }
1400    SkASSERT(l.type().isBoolean());
1401    return fBuilder->splat(l.as<Literal>().boolValue() ? ~0 : 0);
1402}
1403
1404Value SkVMGenerator::writePrefixExpression(const PrefixExpression& p) {
1405    Value val = this->writeExpression(*p.operand());
1406
1407    switch (p.getOperator().kind()) {
1408        case Token::Kind::TK_PLUSPLUS:
1409        case Token::Kind::TK_MINUSMINUS: {
1410            bool incr = p.getOperator().kind() == Token::Kind::TK_PLUSPLUS;
1411
1412            switch (base_number_kind(p.type())) {
1413                case Type::NumberKind::kFloat:
1414                    val = f32(val) + fBuilder->splat(incr ? 1.0f : -1.0f);
1415                    break;
1416                case Type::NumberKind::kSigned:
1417                    val = i32(val) + fBuilder->splat(incr ? 1 : -1);
1418                    break;
1419                default:
1420                    SkASSERT(false);
1421                    return {};
1422            }
1423            return this->writeStore(*p.operand(), val);
1424        }
1425        case Token::Kind::TK_MINUS: {
1426            switch (base_number_kind(p.type())) {
1427                case Type::NumberKind::kFloat:
1428                    return this->unary(val, [](skvm::F32 x) { return -x; });
1429                case Type::NumberKind::kSigned:
1430                    return this->unary(val, [](skvm::I32 x) { return -x; });
1431                default:
1432                    SkASSERT(false);
1433                    return {};
1434            }
1435        }
1436        case Token::Kind::TK_LOGICALNOT:
1437        case Token::Kind::TK_BITWISENOT:
1438            return this->unary(val, [](skvm::I32 x) { return ~x; });
1439        default:
1440            SkASSERT(false);
1441            return {};
1442    }
1443}
1444
1445Value SkVMGenerator::writePostfixExpression(const PostfixExpression& p) {
1446    switch (p.getOperator().kind()) {
1447        case Token::Kind::TK_PLUSPLUS:
1448        case Token::Kind::TK_MINUSMINUS: {
1449            Value old = this->writeExpression(*p.operand()),
1450                  val = old;
1451            SkASSERT(val.slots() == 1);
1452            bool incr = p.getOperator().kind() == Token::Kind::TK_PLUSPLUS;
1453
1454            switch (base_number_kind(p.type())) {
1455                case Type::NumberKind::kFloat:
1456                    val = f32(val) + fBuilder->splat(incr ? 1.0f : -1.0f);
1457                    break;
1458                case Type::NumberKind::kSigned:
1459                    val = i32(val) + fBuilder->splat(incr ? 1 : -1);
1460                    break;
1461                default:
1462                    SkASSERT(false);
1463                    return {};
1464            }
1465            this->writeStore(*p.operand(), val);
1466            return old;
1467        }
1468        default:
1469            SkASSERT(false);
1470            return {};
1471    }
1472}
1473
1474Value SkVMGenerator::writeSwizzle(const Swizzle& s) {
1475    Value base = this->writeExpression(*s.base());
1476    Value swizzled(s.components().size());
1477    for (size_t i = 0; i < s.components().size(); ++i) {
1478        swizzled[i] = base[s.components()[i]];
1479    }
1480    return swizzled;
1481}
1482
1483Value SkVMGenerator::writeTernaryExpression(const TernaryExpression& t) {
1484    skvm::I32 test = i32(this->writeExpression(*t.test()));
1485    Value ifTrue, ifFalse;
1486
1487    {
1488        ScopedCondition m(this, test);
1489        ifTrue = this->writeExpression(*t.ifTrue());
1490    }
1491    {
1492        ScopedCondition m(this, ~test);
1493        ifFalse = this->writeExpression(*t.ifFalse());
1494    }
1495
1496    size_t nslots = ifTrue.slots();
1497    SkASSERT(nslots == ifFalse.slots());
1498
1499    Value result(nslots);
1500    for (size_t i = 0; i < nslots; ++i) {
1501        result[i] = skvm::select(test, i32(ifTrue[i]), i32(ifFalse[i]));
1502    }
1503    return result;
1504}
1505
1506Value SkVMGenerator::writeExpression(const Expression& e) {
1507    switch (e.kind()) {
1508        case Expression::Kind::kBinary:
1509            return this->writeBinaryExpression(e.as<BinaryExpression>());
1510        case Expression::Kind::kChildCall:
1511            return this->writeChildCall(e.as<ChildCall>());
1512        case Expression::Kind::kConstructorArray:
1513        case Expression::Kind::kConstructorCompound:
1514        case Expression::Kind::kConstructorStruct:
1515            return this->writeAggregationConstructor(e.asAnyConstructor());
1516        case Expression::Kind::kConstructorArrayCast:
1517            return this->writeExpression(*e.as<ConstructorArrayCast>().argument());
1518        case Expression::Kind::kConstructorDiagonalMatrix:
1519            return this->writeConstructorDiagonalMatrix(e.as<ConstructorDiagonalMatrix>());
1520        case Expression::Kind::kConstructorMatrixResize:
1521            return this->writeConstructorMatrixResize(e.as<ConstructorMatrixResize>());
1522        case Expression::Kind::kConstructorScalarCast:
1523        case Expression::Kind::kConstructorCompoundCast:
1524            return this->writeConstructorCast(e.asAnyConstructor());
1525        case Expression::Kind::kConstructorSplat:
1526            return this->writeConstructorSplat(e.as<ConstructorSplat>());
1527        case Expression::Kind::kFieldAccess:
1528            return this->writeFieldAccess(e.as<FieldAccess>());
1529        case Expression::Kind::kIndex:
1530            return this->writeIndexExpression(e.as<IndexExpression>());
1531        case Expression::Kind::kVariableReference:
1532            return this->writeVariableExpression(e.as<VariableReference>());
1533        case Expression::Kind::kLiteral:
1534            return this->writeLiteral(e.as<Literal>());
1535        case Expression::Kind::kFunctionCall:
1536            return this->writeFunctionCall(e.as<FunctionCall>());
1537        case Expression::Kind::kExternalFunctionCall:
1538            return this->writeExternalFunctionCall(e.as<ExternalFunctionCall>());
1539        case Expression::Kind::kPrefix:
1540            return this->writePrefixExpression(e.as<PrefixExpression>());
1541        case Expression::Kind::kPostfix:
1542            return this->writePostfixExpression(e.as<PostfixExpression>());
1543        case Expression::Kind::kSwizzle:
1544            return this->writeSwizzle(e.as<Swizzle>());
1545        case Expression::Kind::kTernary:
1546            return this->writeTernaryExpression(e.as<TernaryExpression>());
1547        case Expression::Kind::kExternalFunctionReference:
1548        default:
1549            SkDEBUGFAIL("Unsupported expression");
1550            return {};
1551    }
1552}
1553
1554Value SkVMGenerator::writeStore(const Expression& lhs, const Value& rhs) {
1555    SkASSERTF(rhs.slots() == lhs.type().slotCount(),
1556              "lhs=%s (%s)\nrhs=%zu slot",
1557              lhs.type().description().c_str(), lhs.description().c_str(), rhs.slots());
1558
1559    // We need to figure out the collection of slots that we're storing into. The l-value (lhs)
1560    // is always a VariableReference, possibly wrapped by one or more Swizzle, FieldAccess, or
1561    // IndexExpressions. The underlying VariableReference has a range of slots for its storage,
1562    // and each expression wrapped around that selects a sub-set of those slots (Field/Index),
1563    // or rearranges them (Swizzle).
1564    SkSTArray<4, size_t, true> slots;
1565    slots.resize(rhs.slots());
1566
1567    // Start with the identity slot map - this basically says that the values from rhs belong in
1568    // slots [0, 1, 2 ... N] of the lhs.
1569    for (size_t i = 0; i < slots.size(); ++i) {
1570        slots[i] = i;
1571    }
1572
1573    // Now, as we peel off each outer expression, adjust 'slots' to be the locations relative to
1574    // the next (inner) expression:
1575    const Expression* expr = &lhs;
1576    while (!expr->is<VariableReference>()) {
1577        switch (expr->kind()) {
1578            case Expression::Kind::kFieldAccess: {
1579                const FieldAccess& fld = expr->as<FieldAccess>();
1580                size_t offset = this->fieldSlotOffset(fld);
1581                for (size_t& s : slots) {
1582                    s += offset;
1583                }
1584                expr = fld.base().get();
1585            } break;
1586            case Expression::Kind::kIndex: {
1587                const IndexExpression& idx = expr->as<IndexExpression>();
1588                size_t offset = this->indexSlotOffset(idx);
1589                for (size_t& s : slots) {
1590                    s += offset;
1591                }
1592                expr = idx.base().get();
1593            } break;
1594            case Expression::Kind::kSwizzle: {
1595                const Swizzle& swz = expr->as<Swizzle>();
1596                for (size_t& s : slots) {
1597                    s = swz.components()[s];
1598                }
1599                expr = swz.base().get();
1600            } break;
1601            default:
1602                // No other kinds of expressions are valid in lvalues. (see Analysis::IsAssignable)
1603                SkDEBUGFAIL("Invalid expression type");
1604                return {};
1605        }
1606    }
1607
1608    // When we get here, 'slots' are all relative to the first slot holding 'var's storage
1609    const Variable& var = *expr->as<VariableReference>().variable();
1610    size_t varSlot = this->getSlot(var);
1611    for (size_t& slot : slots) {
1612        SkASSERT(slot < var.type().slotCount());
1613        slot += varSlot;
1614    }
1615
1616    // `slots` are now absolute indices into `fSlots`.
1617    skvm::I32 mask = this->mask();
1618    for (size_t i = 0; i < rhs.slots(); ++i) {
1619        int slotNum = slots[i];
1620        skvm::Val conditionalStore = this->writeConditionalStore(fSlots[slotNum].val, rhs[i], mask);
1621        this->writeToSlot(slotNum, conditionalStore);
1622    }
1623
1624    return rhs;
1625}
1626
1627skvm::Val SkVMGenerator::writeConditionalStore(skvm::Val lhs, skvm::Val rhs, skvm::I32 mask) {
1628    return select(mask, f32(rhs), f32(lhs)).id;
1629}
1630
1631void SkVMGenerator::writeBlock(const Block& b) {
1632    for (const std::unique_ptr<Statement>& stmt : b.children()) {
1633        this->writeStatement(*stmt);
1634    }
1635}
1636
1637void SkVMGenerator::writeBreakStatement() {
1638    // Any active lanes stop executing for the duration of the current loop
1639    fLoopMask &= ~this->mask();
1640}
1641
1642void SkVMGenerator::writeContinueStatement() {
1643    // Any active lanes stop executing for the current iteration.
1644    // Remember them in fContinueMask, to be re-enabled later.
1645    skvm::I32 mask = this->mask();
1646    fLoopMask &= ~mask;
1647    fContinueMask |= mask;
1648}
1649
1650void SkVMGenerator::writeForStatement(const ForStatement& f) {
1651    // We require that all loops be ES2-compliant (unrollable), and actually unroll them here
1652    SkASSERT(f.unrollInfo());
1653    const LoopUnrollInfo& loop = *f.unrollInfo();
1654    SkASSERT(loop.fIndex->type().slotCount() == 1);
1655
1656    size_t indexSlot = this->getSlot(*loop.fIndex);
1657    double val = loop.fStart;
1658
1659    const skvm::I32 zero      = fBuilder->splat(0);
1660    skvm::I32 oldLoopMask     = fLoopMask,
1661              oldContinueMask = fContinueMask;
1662
1663    const Type::NumberKind indexKind = base_number_kind(loop.fIndex->type());
1664
1665    for (int i = 0; i < loop.fCount; ++i) {
1666        this->writeToSlot(indexSlot, (indexKind == Type::NumberKind::kFloat)
1667                                        ? fBuilder->splat(static_cast<float>(val)).id
1668                                        : fBuilder->splat(static_cast<int>(val)).id);
1669
1670        fContinueMask = zero;
1671        this->writeStatement(*f.statement());
1672        fLoopMask |= fContinueMask;
1673
1674        this->emitTraceLine(f.test() ? f.test()->fLine : f.fLine);
1675        val += loop.fDelta;
1676    }
1677
1678    fLoopMask     = oldLoopMask;
1679    fContinueMask = oldContinueMask;
1680}
1681
1682void SkVMGenerator::writeIfStatement(const IfStatement& i) {
1683    Value test = this->writeExpression(*i.test());
1684    {
1685        ScopedCondition ifTrue(this, i32(test));
1686        this->writeStatement(*i.ifTrue());
1687    }
1688    if (i.ifFalse()) {
1689        ScopedCondition ifFalse(this, ~i32(test));
1690        this->writeStatement(*i.ifFalse());
1691    }
1692}
1693
1694void SkVMGenerator::writeReturnStatement(const ReturnStatement& r) {
1695    skvm::I32 returnsHere = this->mask();
1696
1697    if (r.expression()) {
1698        Value val = this->writeExpression(*r.expression());
1699
1700        int i = 0;
1701        for (skvm::Val& slot : currentFunction().fReturnValue) {
1702            slot = select(returnsHere, f32(val[i]), f32(slot)).id;
1703            i++;
1704        }
1705    }
1706
1707    currentFunction().fReturned |= returnsHere;
1708}
1709
1710void SkVMGenerator::writeSwitchStatement(const SwitchStatement& s) {
1711    skvm::I32 falseValue = fBuilder->splat( 0);
1712    skvm::I32 trueValue  = fBuilder->splat(~0);
1713
1714    // Create a "switchFallthough" scratch variable, initialized to false.
1715    skvm::I32 switchFallthrough = falseValue;
1716
1717    // Loop masks behave just like for statements. When a break is encountered, it masks off all
1718    // lanes for the rest of the body of the switch.
1719    skvm::I32 oldLoopMask       = fLoopMask;
1720    Value switchValue           = this->writeExpression(*s.value());
1721
1722    for (const std::unique_ptr<Statement>& stmt : s.cases()) {
1723        const SwitchCase& c = stmt->as<SwitchCase>();
1724        if (c.value()) {
1725            Value caseValue = this->writeExpression(*c.value());
1726
1727            // We want to execute this switch case if we're falling through from a previous case, or
1728            // if the case value matches.
1729            ScopedCondition conditionalCaseBlock(
1730                    this,
1731                    switchFallthrough | (i32(caseValue) == i32(switchValue)));
1732            this->writeStatement(*c.statement());
1733
1734            // If we are inside the case block, we set the fallthrough flag to true (`break` still
1735            // works to stop the flow of execution regardless, since it zeroes out the loop-mask).
1736            switchFallthrough.id = this->writeConditionalStore(switchFallthrough.id, trueValue.id,
1737                                                               this->mask());
1738        } else {
1739            // This is the default case. Since it's always last, we can just dump in the code.
1740            this->writeStatement(*c.statement());
1741        }
1742    }
1743
1744    // Restore state.
1745    fLoopMask = oldLoopMask;
1746}
1747
1748void SkVMGenerator::writeVarDeclaration(const VarDeclaration& decl) {
1749    size_t slot   = this->getSlot(decl.var()),
1750           nslots = decl.var().type().slotCount();
1751
1752    Value val = decl.value() ? this->writeExpression(*decl.value()) : Value{};
1753    for (size_t i = 0; i < nslots; ++i) {
1754        this->writeToSlot(slot + i, val ? val[i] : fBuilder->splat(0.0f).id);
1755    }
1756}
1757
1758void SkVMGenerator::emitTraceLine(int line) {
1759    if (fDebugInfo && line > 0) {
1760        fBuilder->trace_line(this->mask(), line);
1761    }
1762}
1763
1764void SkVMGenerator::writeStatement(const Statement& s) {
1765    this->emitTraceLine(s.fLine);
1766
1767    switch (s.kind()) {
1768        case Statement::Kind::kBlock:
1769            this->writeBlock(s.as<Block>());
1770            break;
1771        case Statement::Kind::kBreak:
1772            this->writeBreakStatement();
1773            break;
1774        case Statement::Kind::kContinue:
1775            this->writeContinueStatement();
1776            break;
1777        case Statement::Kind::kExpression:
1778            this->writeExpression(*s.as<ExpressionStatement>().expression());
1779            break;
1780        case Statement::Kind::kFor:
1781            this->writeForStatement(s.as<ForStatement>());
1782            break;
1783        case Statement::Kind::kIf:
1784            this->writeIfStatement(s.as<IfStatement>());
1785            break;
1786        case Statement::Kind::kReturn:
1787            this->writeReturnStatement(s.as<ReturnStatement>());
1788            break;
1789        case Statement::Kind::kSwitch:
1790            this->writeSwitchStatement(s.as<SwitchStatement>());
1791            break;
1792        case Statement::Kind::kVarDeclaration:
1793            this->writeVarDeclaration(s.as<VarDeclaration>());
1794            break;
1795        case Statement::Kind::kDiscard:
1796        case Statement::Kind::kDo:
1797            SkDEBUGFAIL("Unsupported control flow");
1798            break;
1799        case Statement::Kind::kInlineMarker:
1800        case Statement::Kind::kNop:
1801            break;
1802        default:
1803            SkDEBUGFAIL("Unrecognized statement");
1804            break;
1805    }
1806}
1807
1808skvm::Color ProgramToSkVM(const Program& program,
1809                          const FunctionDefinition& function,
1810                          skvm::Builder* builder,
1811                          SkVMDebugInfo* debugInfo,
1812                          SkSpan<skvm::Val> uniforms,
1813                          skvm::Coord device,
1814                          skvm::Coord local,
1815                          skvm::Color inputColor,
1816                          skvm::Color destColor,
1817                          SampleShaderFn sampleShader,
1818                          SampleColorFilterFn sampleColorFilter,
1819                          SampleBlenderFn sampleBlender) {
1820    skvm::Val zero = builder->splat(0.0f).id;
1821    skvm::Val result[4] = {zero,zero,zero,zero};
1822
1823    skvm::Val args[8];  // At most 8 arguments (half4 srcColor, half4 dstColor)
1824    size_t argSlots = 0;
1825    for (const SkSL::Variable* param : function.declaration().parameters()) {
1826        switch (param->modifiers().fLayout.fBuiltin) {
1827            case SK_MAIN_COORDS_BUILTIN:
1828                SkASSERT(param->type().slotCount() == 2);
1829                SkASSERT((argSlots + 2) <= SK_ARRAY_COUNT(args));
1830                args[argSlots++] = local.x.id;
1831                args[argSlots++] = local.y.id;
1832                break;
1833            case SK_INPUT_COLOR_BUILTIN:
1834                SkASSERT(param->type().slotCount() == 4);
1835                SkASSERT((argSlots + 4) <= SK_ARRAY_COUNT(args));
1836                args[argSlots++] = inputColor.r.id;
1837                args[argSlots++] = inputColor.g.id;
1838                args[argSlots++] = inputColor.b.id;
1839                args[argSlots++] = inputColor.a.id;
1840                break;
1841            case SK_DEST_COLOR_BUILTIN:
1842                SkASSERT(param->type().slotCount() == 4);
1843                SkASSERT((argSlots + 4) <= SK_ARRAY_COUNT(args));
1844                args[argSlots++] = destColor.r.id;
1845                args[argSlots++] = destColor.g.id;
1846                args[argSlots++] = destColor.b.id;
1847                args[argSlots++] = destColor.a.id;
1848                break;
1849            default:
1850                SkDEBUGFAIL("Invalid parameter to main()");
1851                return {};
1852        }
1853    }
1854    SkASSERT(argSlots <= SK_ARRAY_COUNT(args));
1855
1856    SkVMGenerator generator(program, builder, debugInfo, std::move(sampleShader),
1857                            std::move(sampleColorFilter), std::move(sampleBlender));
1858    generator.writeProgram(uniforms, device, function, {args, argSlots}, SkMakeSpan(result));
1859
1860    return skvm::Color{{builder, result[0]},
1861                       {builder, result[1]},
1862                       {builder, result[2]},
1863                       {builder, result[3]}};
1864}
1865
1866bool ProgramToSkVM(const Program& program,
1867                   const FunctionDefinition& function,
1868                   skvm::Builder* b,
1869                   SkVMDebugInfo* debugInfo,
1870                   SkSpan<skvm::Val> uniforms,
1871                   SkVMSignature* outSignature) {
1872    SkVMSignature ignored,
1873                  *signature = outSignature ? outSignature : &ignored;
1874
1875    std::vector<skvm::Ptr> argPtrs;
1876    std::vector<skvm::Val> argVals;
1877
1878    for (const Variable* p : function.declaration().parameters()) {
1879        size_t slots = p->type().slotCount();
1880        signature->fParameterSlots += slots;
1881        for (size_t i = 0; i < slots; ++i) {
1882            argPtrs.push_back(b->varying<float>());
1883            argVals.push_back(b->loadF(argPtrs.back()).id);
1884        }
1885    }
1886
1887    std::vector<skvm::Ptr> returnPtrs;
1888    std::vector<skvm::Val> returnVals;
1889
1890    signature->fReturnSlots = function.declaration().returnType().slotCount();
1891    for (size_t i = 0; i < signature->fReturnSlots; ++i) {
1892        returnPtrs.push_back(b->varying<float>());
1893        returnVals.push_back(b->splat(0.0f).id);
1894    }
1895
1896    bool sampledChildEffects = false;
1897    auto sampleShader = [&](int, skvm::Coord) {
1898        sampledChildEffects = true;
1899        return skvm::Color{};
1900    };
1901    auto sampleColorFilter = [&](int, skvm::Color) {
1902        sampledChildEffects = true;
1903        return skvm::Color{};
1904    };
1905    auto sampleBlender = [&](int, skvm::Color, skvm::Color) {
1906        sampledChildEffects = true;
1907        return skvm::Color{};
1908    };
1909
1910    skvm::F32 zero = b->splat(0.0f);
1911    skvm::Coord zeroCoord = {zero, zero};
1912    SkVMGenerator generator(program, b, debugInfo, sampleShader, sampleColorFilter, sampleBlender);
1913    generator.writeProgram(uniforms, /*device=*/zeroCoord,
1914                           function, SkMakeSpan(argVals), SkMakeSpan(returnVals));
1915
1916    // If the SkSL tried to use any shader, colorFilter, or blender objects - we don't have a
1917    // mechanism (yet) for binding to those.
1918    if (sampledChildEffects) {
1919        return false;
1920    }
1921
1922    // generateCode has updated the contents of 'argVals' for any 'out' or 'inout' parameters.
1923    // Propagate those changes back to our varying buffers:
1924    size_t argIdx = 0;
1925    for (const Variable* p : function.declaration().parameters()) {
1926        size_t nslots = p->type().slotCount();
1927        if (p->modifiers().fFlags & Modifiers::kOut_Flag) {
1928            for (size_t i = 0; i < nslots; ++i) {
1929                b->storeF(argPtrs[argIdx + i], skvm::F32{b, argVals[argIdx + i]});
1930            }
1931        }
1932        argIdx += nslots;
1933    }
1934
1935    // It's also updated the contents of 'returnVals' with the return value of the entry point.
1936    // Store that as well:
1937    for (size_t i = 0; i < signature->fReturnSlots; ++i) {
1938        b->storeF(returnPtrs[i], skvm::F32{b, returnVals[i]});
1939    }
1940
1941    return true;
1942}
1943
1944const FunctionDefinition* Program_GetFunction(const Program& program, const char* function) {
1945    for (const ProgramElement* e : program.elements()) {
1946        if (e->is<FunctionDefinition>() &&
1947            e->as<FunctionDefinition>().declaration().name() == function) {
1948            return &e->as<FunctionDefinition>();
1949        }
1950    }
1951    return nullptr;
1952}
1953
1954static void gather_uniforms(UniformInfo* info, const Type& type, const String& name) {
1955    switch (type.typeKind()) {
1956        case Type::TypeKind::kStruct:
1957            for (const auto& f : type.fields()) {
1958                gather_uniforms(info, *f.fType, name + "." + f.fName);
1959            }
1960            break;
1961        case Type::TypeKind::kArray:
1962            for (int i = 0; i < type.columns(); ++i) {
1963                gather_uniforms(info, type.componentType(),
1964                                String::printf("%s[%d]", name.c_str(), i));
1965            }
1966            break;
1967        case Type::TypeKind::kScalar:
1968        case Type::TypeKind::kVector:
1969        case Type::TypeKind::kMatrix:
1970            info->fUniforms.push_back({name, base_number_kind(type), type.rows(), type.columns(),
1971                                       info->fUniformSlotCount});
1972            info->fUniformSlotCount += type.columns() * type.rows();
1973            break;
1974        default:
1975            break;
1976    }
1977}
1978
1979std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program) {
1980    auto info = std::make_unique<UniformInfo>();
1981    for (const ProgramElement* e : program.elements()) {
1982        if (!e->is<GlobalVarDeclaration>()) {
1983            continue;
1984        }
1985        const GlobalVarDeclaration& decl = e->as<GlobalVarDeclaration>();
1986        const Variable& var = decl.declaration()->as<VarDeclaration>().var();
1987        if (var.modifiers().fFlags & Modifiers::kUniform_Flag) {
1988            gather_uniforms(info.get(), var.type(), String(var.name()));
1989        }
1990    }
1991    return info;
1992}
1993
1994/*
1995 * Testing utility function that emits program's "main" with a minimal harness. Used to create
1996 * representative skvm op sequences for SkSL tests.
1997 */
1998bool testingOnly_ProgramToSkVMShader(const Program& program,
1999                                     skvm::Builder* builder,
2000                                     SkVMDebugInfo* debugInfo) {
2001    const SkSL::FunctionDefinition* main = Program_GetFunction(program, "main");
2002    if (!main) {
2003        return false;
2004    }
2005
2006    size_t uniformSlots = 0;
2007    int childSlots = 0;
2008    for (const SkSL::ProgramElement* e : program.elements()) {
2009        if (e->is<GlobalVarDeclaration>()) {
2010            const GlobalVarDeclaration& decl = e->as<GlobalVarDeclaration>();
2011            const Variable& var = decl.declaration()->as<VarDeclaration>().var();
2012            if (var.type().isEffectChild()) {
2013                childSlots++;
2014            } else if (is_uniform(var)) {
2015                uniformSlots += var.type().slotCount();
2016            }
2017        }
2018    }
2019
2020    skvm::Uniforms uniforms(builder->uniform(), 0);
2021
2022    auto new_uni = [&]() { return builder->uniformF(uniforms.pushF(0.0f)); };
2023
2024    // Assume identity CTM
2025    skvm::Coord device = {pun_to_F32(builder->index()), new_uni()};
2026    skvm::Coord local  = device;
2027
2028    struct Child {
2029        skvm::Uniform addr;
2030        skvm::I32     rowBytesAsPixels;
2031    };
2032
2033    std::vector<Child> children;
2034    for (int i = 0; i < childSlots; ++i) {
2035        children.push_back({uniforms.pushPtr(nullptr), builder->uniform32(uniforms.push(0))});
2036    }
2037
2038    auto sampleShader = [&](int i, skvm::Coord coord) {
2039        skvm::PixelFormat pixelFormat = skvm::SkColorType_to_PixelFormat(kRGBA_F32_SkColorType);
2040        skvm::I32 index  = trunc(coord.x);
2041                  index += trunc(coord.y) * children[i].rowBytesAsPixels;
2042        return gather(pixelFormat, children[i].addr, index);
2043    };
2044
2045    std::vector<skvm::Val> uniformVals;
2046    for (size_t i = 0; i < uniformSlots; ++i) {
2047        uniformVals.push_back(new_uni().id);
2048    }
2049
2050    skvm::Color inColor = builder->uniformColor(SkColors::kWhite, &uniforms);
2051    skvm::Color destColor = builder->uniformColor(SkColors::kBlack, &uniforms);
2052
2053    skvm::Color result = SkSL::ProgramToSkVM(program, *main, builder, debugInfo,
2054                                             SkMakeSpan(uniformVals), device, local, inColor,
2055                                             destColor, sampleShader, /*sampleColorFilter=*/nullptr,
2056                                             /*sampleBlender=*/nullptr);
2057
2058    storeF(builder->varying<float>(), result.r);
2059    storeF(builder->varying<float>(), result.g);
2060    storeF(builder->varying<float>(), result.b);
2061    storeF(builder->varying<float>(), result.a);
2062
2063    return true;
2064}
2065
2066}  // namespace SkSL
2067