1 /*
2  * Copyright 2019 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/SkColor.h"
9 #include "include/core/SkData.h"
10 #include "include/core/SkM44.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSpan.h"
16 #include "include/core/SkStream.h"
17 #include "include/core/SkTypes.h"
18 #include "include/private/SkSLProgramKind.h"
19 #include "include/private/SkSLString.h"
20 #include "include/private/base/SkFloatingPoint.h"
21 #include "include/private/base/SkTemplates.h"
22 #include "src/core/SkVM.h"
23 #include "src/sksl/SkSLCompiler.h"
24 #include "src/sksl/SkSLProgramSettings.h"
25 #include "src/sksl/SkSLUtil.h"
26 #include "src/sksl/codegen/SkSLVMCodeGenerator.h"
27 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
28 #include "src/sksl/ir/SkSLProgram.h"
29 #include "src/sksl/tracing/SkVMDebugTrace.h"
30 #include "tests/Test.h"
31 
32 #include <cmath>
33 #include <cstdint>
34 #include <cstdio>
35 #include <cstring>
36 #include <memory>
37 #include <string>
38 #include <string_view>
39 #include <vector>
40 
41 namespace SkSL { class FunctionDefinition; }
42 
43 struct ProgramBuilder {
ProgramBuilderProgramBuilder44     ProgramBuilder(skiatest::Reporter* r, const char* src)
45             : fCompiler(&fCaps) {
46         SkSL::ProgramSettings settings;
47         // The SkSL inliner is well tested in other contexts. Here, we disable inlining entirely,
48         // to stress-test the VM generator's handling of function calls with varying signatures.
49         settings.fInlineThreshold = 0;
50 
51         fProgram = fCompiler.convertProgram(SkSL::ProgramKind::kGeneric, std::string(src),
52                                             settings);
53         if (!fProgram) {
54             ERRORF(r, "Program failed to compile:\n%s\n%s\n", src, fCompiler.errorText().c_str());
55         }
56     }
57 
operator boolProgramBuilder58     explicit operator bool() const { return fProgram != nullptr; }
operator *ProgramBuilder59     SkSL::Program& operator*() { return *fProgram; }
operator ->ProgramBuilder60     SkSL::Program* operator->() { return fProgram.get(); }
61 
62     SkSL::ShaderCaps fCaps;
63     SkSL::Compiler fCompiler;
64     std::unique_ptr<SkSL::Program> fProgram;
65 };
66 
verify_values(skiatest::Reporter * r,const char * src,const float * actual,const float * expected,int N,bool exactCompare)67 static void verify_values(skiatest::Reporter* r,
68                           const char* src,
69                           const float* actual,
70                           const float* expected,
71                           int N,
72                           bool exactCompare) {
73     auto exact_equiv = [](float x, float y) {
74         return x == y
75             || (std::isnan(x) && std::isnan(y));
76     };
77 
78     bool valid = true;
79     for (int i = 0; i < N; ++i) {
80         if (exactCompare && !exact_equiv(actual[i], expected[i])) {
81             valid = false;
82         }
83         if (!exactCompare && !SkScalarNearlyEqual(actual[i], expected[i])) {
84             valid = false;
85         }
86     }
87 
88     if (!valid) {
89         printf("for program: %s\n", src);
90         printf("    expected (");
91         {
92             auto separator = SkSL::String::Separator();
93             for (int i = 0; i < N; ++i) {
94                 printf("%s%f", separator().c_str(), expected[i]);
95             }
96         }
97         printf("), but received (");
98         {
99             auto separator = SkSL::String::Separator();
100             for (int i = 0; i < N; ++i) {
101                 printf("%s%f", separator().c_str(), actual[i]);
102             }
103         }
104         printf(")\n");
105     }
106     REPORTER_ASSERT(r, valid);
107 }
108 
test(skiatest::Reporter * r,const char * src,float * in,const float * expected,bool exactCompare=true)109 void test(skiatest::Reporter* r, const char* src, float* in, const float* expected,
110           bool exactCompare = true) {
111     ProgramBuilder program(r, src);
112     if (!program) { return; }
113 
114     const SkSL::FunctionDeclaration* main = program->getFunction("main");
115     REPORTER_ASSERT(r, main);
116 
117     skvm::Builder b;
118     SkSL::SkVMSignature sig;
119     SkSL::ProgramToSkVM(*program, *main->definition(), &b, /*debugTrace=*/nullptr,
120                         /*uniforms=*/{}, &sig);
121     skvm::Program p = b.done();
122 
123     REPORTER_ASSERT(r, p.nargs() == (int)(sig.fParameterSlots + sig.fReturnSlots));
124 
125     auto out = std::make_unique<float[]>(sig.fReturnSlots);
126     auto args = std::make_unique<void*[]>(sig.fParameterSlots + sig.fReturnSlots);
127     for (size_t i = 0; i < sig.fParameterSlots; ++i) {
128         args[i] = in + i;
129     }
130     for (size_t i = 0; i < sig.fReturnSlots; ++i) {
131         args[sig.fParameterSlots + i] = out.get() + i;
132     }
133 
134     // TODO: Test with and without JIT?
135     p.eval(1, args.get());
136 
137     verify_values(r, src, out.get(), expected, sig.fReturnSlots, exactCompare);
138 }
139 
test(skiatest::Reporter * r,const char * src,float inR,float inG,float inB,float inA,float exR,float exG,float exB,float exA)140 void test(skiatest::Reporter* r, const char* src,
141           float inR, float inG, float inB, float inA,
142           float exR, float exG, float exB, float exA) {
143     ProgramBuilder program(r, src);
144     if (!program) { return; }
145 
146     const SkSL::FunctionDeclaration* main = program->getFunction("main");
147     REPORTER_ASSERT(r, main);
148 
149     skvm::Builder b;
150     SkSL::ProgramToSkVM(*program, *main->definition(), &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
151     skvm::Program p = b.done();
152 
153     // TODO: Test with and without JIT?
154     p.eval(1, &inR, &inG, &inB, &inA);
155 
156     float actual[4]   = { inR, inG, inB, inA };
157     float expected[4] = { exR, exG, exB, exA };
158 
159     verify_values(r, src, actual, expected, 4, /*exactCompare=*/true);
160 
161     // TODO: vec_test with skvm
162 }
163 
DEF_TEST(SkSLInterpreterAdd,r)164 DEF_TEST(SkSLInterpreterAdd, r) {
165     test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1,
166          0.75, 0, 0);
167     test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5);
168     test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1,
169          0.5, 1, 1.5, 2);
170     test(r, "void main(inout half4 color) { color.r = half(int(color.r) + int(color.g)); }", 1, 3, 0, 0,
171          4, 3, 0, 0);
172 }
173 
DEF_TEST(SkSLInterpreterSubtract,r)174 DEF_TEST(SkSLInterpreterSubtract, r) {
175     test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25,
176          0.75, 0, 0);
177     test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1);
178     test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1,
179          0, 0, 0, 0);
180     test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1);
181     test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1);
182     test(r, "void main(inout half4 color) { color.r = half(int(color.r) - int(color.g)); }", 3, 1, 0, 0,
183          2, 1, 0, 0);
184 }
185 
DEF_TEST(SkSLInterpreterMultiply,r)186 DEF_TEST(SkSLInterpreterMultiply, r) {
187     test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0,
188          0);
189     test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12,
190          20);
191     test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1,
192          16, 9, 4, 1);
193     test(r, "void main(inout half4 color) { color.r = half(int(color.r) * int(color.g)); }", 3, -2, 0, 0,
194          -6, -2, 0, 0);
195 }
196 
DEF_TEST(SkSLInterpreterDivide,r)197 DEF_TEST(SkSLInterpreterDivide, r) {
198     test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0,
199          0);
200     test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6,
201          4, 3);
202     test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1,
203          1, 1, 1, 1);
204     test(r, "void main(inout half4 color) { color.r = half(int(color.r) / int(color.g)); }", 8, -2, 0, 0,
205          -4, -2, 0, 0);
206 }
207 
DEF_TEST(SkSLInterpreterAnd,r)208 DEF_TEST(SkSLInterpreterAnd, r) {
209     test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
210             "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
211     test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
212             "color = half4(color.a); }", 1, 1, 0, 3, 1, 1, 0, 3);
213     test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) "
214             "color = half4(color.a); }", 2, 1, 1, 3, 2, 1, 1, 3);
215     test(r, "half global; bool update() { global = 123; return true; }"
216             "void main(inout half4 color) { global = 0;  if (color.r > color.g && update()) "
217             "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 123);
218     test(r, "half global; bool update() { global = 123; return true; }"
219             "void main(inout half4 color) { global = 0;  if (color.r > color.g && update()) "
220             "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 1, 1, 1, 0);
221 }
222 
DEF_TEST(SkSLInterpreterOr,r)223 DEF_TEST(SkSLInterpreterOr, r) {
224     test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
225             "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3);
226     test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
227             "color = half4(color.a); }", 1, 1, 0, 3, 3, 3, 3, 3);
228     test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) "
229             "color = half4(color.a); }", 1, 1, 1, 3, 1, 1, 1, 3);
230     test(r, "half global; bool update() { global = 123; return true; }"
231             "void main(inout half4 color) { global = 0;  if (color.r > color.g || update()) "
232             "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 3, 3, 3, 123);
233     test(r, "half global; bool update() { global = 123; return true; }"
234             "void main(inout half4 color) { global = 0;  if (color.r > color.g || update()) "
235             "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 0);
236 }
237 
DEF_TEST(SkSLInterpreterMatrix,r)238 DEF_TEST(SkSLInterpreterMatrix, r) {
239     float in[16];
240     float expected[16];
241 
242     // Constructing matrix from scalar produces a diagonal matrix
243     in[0] = 2.0f;
244     expected[0] = 4.0f;
245     test(r, "float main(float x) { float4x4 m = float4x4(x); return m[1][1] + m[1][2] + m[2][2]; }",
246          in, expected);
247 
248     // Constructing from a different-sized matrix fills the remaining space with the identity matrix
249     expected[0] = 3.0f;
250     test(r, "float main(float x) {"
251          "float2x2 m = float2x2(x);"
252          "float4x4 m2 = float4x4(m);"
253          "return m2[0][0] + m2[3][3]; }",
254          in, expected);
255 
256     // Constructing a matrix from vectors or scalars fills in values in column-major order
257     in[0] = 1.0f;
258     in[1] = 2.0f;
259     in[2] = 4.0f;
260     in[3] = 8.0f;
261     expected[0] = 6.0f;
262     test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
263          in, expected);
264 
265     expected[0] = 10.0f;
266     test(r, "float main(float4 v) {"
267          "float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
268          "return m[0][1] + m[1][0]; }",
269          in, expected);
270 
271     // Initialize 16 values to be used as inputs to matrix tests
272     for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
273 
274     // M+M, M-S, S-M
275     for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
276     test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
277     for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
278     test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
279     test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
280 
281     // M-M, M-S, S-M
282     for (int i = 0; i < 4; ++i) { expected[i] = 4.0f; }
283     test(r, "float2x2 main(float2x2 m1, float2x2 m2) { return m2 - m1; }", in, expected);
284     for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
285     test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, expected);
286     for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
287     test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
288 
289     // M*S, S*M, M/S, S/M
290     for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
291     test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
292     test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
293     for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
294     test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
295     for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
296     test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
297 
298     // Matrix negation
299     for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); }
300     test(r, "float4x4 main(float4x4 m) { return -m; }", in, expected);
301 
302     // M*V, V*M
303     for (int i = 0; i < 3; ++i) {
304         expected[i] = 9.0f*i + 10.0f*(i+3) + 11.0f*(i+6);
305     }
306     test(r, "float3 main(float3x3 m, float3 v) { return m * v; }", in, expected);
307     for (int i = 0; i < 3; ++i) {
308         expected[i] = 9.0f*(3*i) + 10.0f*(3*i+1) + 11.0f*(3*i+2);
309     }
310     test(r, "float3 main(float3x3 m, float3 v) { return v * m; }", in, expected);
311 
312     // M*M
313     {
314         SkM44 m = SkM44::ColMajor(in);
315         SkM44 m2;
316         float in2[16];
317         for (int i = 0; i < 16; ++i) {
318             in2[i] = (i + 4) % 16;
319         }
320         m2 = SkM44::ColMajor(in2);
321         m.setConcat(m, m2);
322         // Rearrange the columns on the RHS so we detect left-hand/right-hand errors
323         test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
324              in, (float*)&m);
325     }
326 }
327 
DEF_TEST(SkSLInterpreterTernary,r)328 DEF_TEST(SkSLInterpreterTernary, r) {
329     test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
330          0, 1, 2, 0, 2, 1, 2, 0);
331     test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }",
332          0, 3, 2, 0, 3, 3, 2, 0);
333 }
334 
DEF_TEST(SkSLInterpreterCast,r)335 DEF_TEST(SkSLInterpreterCast, r) {
336     union Val {
337         float    f;
338         int32_t  s;
339     };
340 
341     Val input[2];
342     Val expected[2];
343 
344     input[0].s = 3;
345     input[1].s = -5;
346     expected[0].f = 3.0f;
347     expected[1].f = -5.0f;
348     test(r, "float  main(int  x) { return float (x); }", (float*)input, (float*)expected);
349     test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
350 
351     input[0].f = 3.0f;
352     input[1].f = -5.0f;
353     expected[0].s = 3;
354     expected[1].s = -5;
355     test(r, "int  main(float  x) { return int (x); }", (float*)input, (float*)expected);
356     test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
357 
358     input[0].s = 3;
359     expected[0].f = 3.0f;
360     expected[1].f = 3.0f;
361     test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
362 }
363 
DEF_TEST(SkSLInterpreterIf,r)364 DEF_TEST(SkSLInterpreterIf, r) {
365     test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0,
366          5, 3, 0, 1);
367     test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0,
368          5, 5, 0, 0);
369     test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0,
370          5, 6, 0, 0);
371     test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0,
372          3, 5, 0, 1);
373     test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0,
374          5, 5, 0, 0);
375     test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0,
376          6, 5, 0, 0);
377     test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0,
378          5, 3, 0, 1);
379     test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0,
380          5, 5, 0, 1);
381     test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0,
382          5, 6, 0, 0);
383     test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0,
384          3, 5, 0, 1);
385     test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0,
386          5, 5, 0, 1);
387     test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0,
388          6, 5, 0, 0);
389     test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0,
390          2, 2, 0, 1);
391     test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0,
392          2, -2, 0, 0);
393     test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0,
394          2, 2, 0, 0);
395     test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0,
396          2, -2, 0, 1);
397     test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0,
398          2, 2, 0, 0);
399     test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0,
400          2, -2, 0, 1);
401     test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
402          "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1);
403     test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else "
404          "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2);
405 }
406 
DEF_TEST(SkSLInterpreterIfVector,r)407 DEF_TEST(SkSLInterpreterIfVector, r) {
408     test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
409          1, 2, 1, 2, 1, 2, 1, 1);
410     test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }",
411          1, 2, 3, 2, 1, 2, 3, 2);
412     test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
413          1, 2, 1, 2, 1, 2, 1, 2);
414     test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }",
415          1, 2, 3, 2, 1, 2, 3, 1);
416 }
417 
DEF_TEST(SkSLInterpreterFor,r)418 DEF_TEST(SkSLInterpreterFor, r) {
419     test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += half(i); }",
420          0, 0, 0, 0,
421          55, 0, 0, 0);
422     test(r,
423          "void main(inout half4 color) {"
424          "    for (int i = 1; i <= 10; ++i)"
425          "        for (int j = 1; j <= 10; ++j)"
426          "            if (j >= i) { color.r += half(j); }"
427          "}",
428          0, 0, 0, 0,
429          385, 0, 0, 0);
430     test(r,
431          "void main(inout half4 color) {"
432          "    for (int i = 1; i <= 10; ++i)"
433          "        for (int j = 1; j < 20 ; ++j) {"
434          "            if (i == j) continue;"
435          "            if (j > 10) break;"
436          "            color.r += half(j);"
437          "        }"
438          "}",
439          0, 0, 0, 0,
440          495, 0, 0, 0);
441 }
442 
DEF_TEST(SkSLInterpreterPrefixPostfix,r)443 DEF_TEST(SkSLInterpreterPrefixPostfix, r) {
444     test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4);
445     test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4);
446 }
447 
DEF_TEST(SkSLInterpreterSwizzle,r)448 DEF_TEST(SkSLInterpreterSwizzle, r) {
449     test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1);
450     test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7,
451          6, 4);
452     test(r, "void main(inout half4 color) { color.bgr = half3(5, 6, 7); }", 1, 2, 3, 4, 7, 6,
453          5, 4);
454 }
455 
DEF_TEST(SkSLInterpreterGlobal,r)456 DEF_TEST(SkSLInterpreterGlobal, r) {
457     test(r, "int x; void main(inout half4 color) { x = 10; color.b = half(x); }", 1, 2, 3, 4, 1, 2,
458          10, 4);
459     test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }",
460          1, 2, 3, 4, 2, 4, 6, 8);
461     test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }",
462          1, 2, 3, 4, 8, 7, 6, 5);
463     test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }",
464          1, 2, 3, 4, 8, 7, 6, 5);
465 }
466 
DEF_TEST(SkSLInterpreterGeneric,r)467 DEF_TEST(SkSLInterpreterGeneric, r) {
468     float value1 = 5;
469     float expected1 = 25;
470     test(r, "float main(float x) { return x * x; }", &value1, &expected1);
471     float value2[2] = { 5, 25 };
472     float expected2[2] = { 25, 625 };
473     test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
474 }
475 
DEF_TEST(SkSLInterpreterFieldAccessComplex,r)476 DEF_TEST(SkSLInterpreterFieldAccessComplex, r) {
477     const char* src = R"(
478         struct P { float x; float y; };
479         P make_point() { P p; p.x = 7; p.y = 3; return p; }
480         float main() { return make_point().y; }
481     )";
482 
483     float expected = 3.0f;
484     test(r, src, /*in=*/nullptr, &expected);
485 }
486 
DEF_TEST(SkSLInterpreterIndexComplex,r)487 DEF_TEST(SkSLInterpreterIndexComplex, r) {
488     const char* src = R"(
489         float2x2 make_mtx() { return float2x2(1, 2, 3, 4); }
490         float main() { return make_mtx()[1][0]; }
491     )";
492 
493     float expected = 3.0f;
494     test(r, src, /*in=*/nullptr, &expected);
495 }
496 
DEF_TEST(SkSLInterpreterCompound,r)497 DEF_TEST(SkSLInterpreterCompound, r) {
498     struct RectAndColor { SkIRect fRect; SkColor4f fColor; };
499     struct ManyRects { int fNumRects; RectAndColor fRects[4]; };
500 
501     const char* src =
502         // Some struct definitions
503         "struct Point { int x; int y; };\n"
504         "struct Rect {  Point p0; Point p1; };\n"
505         "struct RectAndColor { Rect r; float4 color; };\n"
506 
507         // Structs as globals, parameters, return values
508         "RectAndColor temp;\n"
509         "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n"
510         "RectAndColor make_blue_rect(int w, int h) {\n"
511         "  temp.r.p0.x = temp.r.p0.y = 0;\n"
512         "  temp.r.p1.x = w; temp.r.p1.y = h;\n"
513         "  temp.color = float4(0, 1, 0, 1);\n"
514         "  return temp;\n"
515         "}\n"
516 
517         // Initialization and assignment of types larger than 4 slots
518         "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n"
519         "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n"
520 
521         // Same for arrays, including some non-constant indexing
522         "int median(int a[15]) { return a[7]; }\n"
523 
524         "float tempFloats[8];\n"
525         "float sums(float a[8]) {\n"
526         "  tempFloats[0] = a[0];\n"
527         "  for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n"
528         "  return tempFloats[7];\n"
529         "}\n"
530 
531         // Uniforms, array-of-structs
532         "uniform Rect gRects[4];\n"
533         "Rect get_rect_2() { return gRects[2]; }\n"
534 
535         // Kitchen sink (swizzles, inout, SoAoS)
536         "struct ManyRects { int numRects; RectAndColor rects[4]; };\n"
537         "void fill_rects(inout ManyRects mr) {\n"
538         "  for (int i = 0; i < 4; ++i) {\n"
539         "    if (i >= mr.numRects) { break; }\n"
540         "    mr.rects[i].r = gRects[i];\n"
541         "    float b = float(mr.rects[i].r.p1.y);\n"
542         "    mr.rects[i].color = float4(b, b, b, b);\n"
543         "  }\n"
544         "}\n";
545 
546     ProgramBuilder program(r, src);
547 
548     const SkSL::FunctionDeclaration *rect_height    = program->getFunction("rect_height"),
549                                     *make_blue_rect = program->getFunction("make_blue_rect"),
550                                     *median         = program->getFunction("median"),
551                                     *sums           = program->getFunction("sums"),
552                                     *get_rect_2     = program->getFunction("get_rect_2"),
553                                     *fill_rects     = program->getFunction("fill_rects");
554 
555     SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } };
556 
557     auto build = [&](const SkSL::FunctionDefinition* fn) {
558         skvm::Builder b;
559         skvm::UPtr uniformPtr = b.uniform();
560         skvm::Val uniforms[16];
561         for (int i = 0; i < 16; ++i) {
562             uniforms[i] = b.uniform32(uniformPtr, i * sizeof(int)).id;
563         }
564         SkSL::ProgramToSkVM(*program, *fn, &b, /*debugTrace=*/nullptr, SkSpan(uniforms));
565         return b.done();
566     };
567 
568     struct Args {
569         Args(void* uniformData) { fArgs.push_back(uniformData); }
570         void add(void* base, int n) {
571             for (int i = 0; i < n; ++i) {
572                 fArgs.push_back(SkTAddOffset<void>(base, i * sizeof(float)));
573             }
574         }
575         std::vector<void*> fArgs;
576     };
577 
578     {
579         SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
580         int out = 0;
581         skvm::Program p = build(rect_height->definition());
582         Args args(gRects);
583         args.add(&in, 4);
584         args.add(&out, 1);
585         p.eval(1, args.fArgs.data());
586         REPORTER_ASSERT(r, out == 30);
587     }
588 
589     {
590         int in[2] = { 15, 25 };
591         RectAndColor out;
592         skvm::Program p = build(make_blue_rect->definition());
593         Args args(gRects);
594         args.add(&in, 2);
595         args.add(&out, 8);
596         p.eval(1, args.fArgs.data());
597         REPORTER_ASSERT(r, out.fRect.width() == 15);
598         REPORTER_ASSERT(r, out.fRect.height() == 25);
599         SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
600         REPORTER_ASSERT(r, out.fColor == blue);
601     }
602 
603     {
604         int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
605         int out = 0;
606         skvm::Program p = build(median->definition());
607         Args args(gRects);
608         args.add(&in, 15);
609         args.add(&out, 1);
610         p.eval(1, args.fArgs.data());
611         REPORTER_ASSERT(r, out == 8);
612     }
613 
614     {
615         float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
616         float out = 0;
617         skvm::Program p = build(sums->definition());
618         Args args(gRects);
619         args.add(&in, 8);
620         args.add(&out, 1);
621         p.eval(1, args.fArgs.data());
622         REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2));
623     }
624 
625     {
626         SkIRect out = SkIRect::MakeEmpty();
627         skvm::Program p = build(get_rect_2->definition());
628         Args args(gRects);
629         args.add(&out, 4);
630         p.eval(1, args.fArgs.data());
631         REPORTER_ASSERT(r, out == gRects[2]);
632     }
633 
634     {
635         ManyRects in;
636         memset(&in, 0, sizeof(in));
637         in.fNumRects = 2;
638         skvm::Program p = build(fill_rects->definition());
639         Args args(gRects);
640         args.add(&in, 33);
641         p.eval(1, args.fArgs.data());
642         ManyRects expected;
643         memset(&expected, 0, sizeof(expected));
644         expected.fNumRects = 2;
645         for (int i = 0; i < 2; ++i) {
646             expected.fRects[i].fRect = gRects[i];
647             float c = gRects[i].fBottom;
648             expected.fRects[i].fColor = { c, c, c, c };
649         }
650         REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0);
651     }
652 }
653 
expect_failure(skiatest::Reporter * r,const char * src)654 static void expect_failure(skiatest::Reporter* r, const char* src) {
655     SkSL::ShaderCaps caps;
656     SkSL::Compiler compiler(&caps);
657     SkSL::ProgramSettings settings;
658     auto program = compiler.convertProgram(SkSL::ProgramKind::kGeneric,
659                                            std::string(src), settings);
660     REPORTER_ASSERT(r, !program);
661 }
662 
DEF_TEST(SkSLInterpreterRestrictLoops,r)663 DEF_TEST(SkSLInterpreterRestrictLoops, r) {
664     // while and do-while loops are not allowed
665     expect_failure(r, "void main(inout float x) { while (x < 1) { x++; } }");
666     expect_failure(r, "void main(inout float x) { do { x++; } while (x < 1); }");
667 }
668 
DEF_TEST(SkSLInterpreterReturnThenCall,r)669 DEF_TEST(SkSLInterpreterReturnThenCall, r) {
670     // Test that early returns disable execution in subsequently called functions
671     const char* src = R"(
672         float y;
673         void inc () { ++y; }
674         void maybe_inc() { if (y < 0) return; inc(); }
675         void main(inout float x) { y = x; maybe_inc(); x = y; }
676     )";
677 
678     ProgramBuilder program(r, src);
679     const SkSL::FunctionDeclaration* main = program->getFunction("main");
680     REPORTER_ASSERT(r, main);
681 
682     skvm::Builder b;
683     SkSL::ProgramToSkVM(*program, *main->definition(), &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
684     skvm::Program p = b.done();
685 
686     float xs[] = { -2.0f, 0.0f, 3.0f, -1.0f };
687     p.eval(4, xs);
688 
689     REPORTER_ASSERT(r, xs[0] == -2.0f);
690     REPORTER_ASSERT(r, xs[1] ==  1.0f);
691     REPORTER_ASSERT(r, xs[2] ==  4.0f);
692     REPORTER_ASSERT(r, xs[3] == -1.0f);
693 }
694 
DEF_TEST(SkSLInterpreterEarlyReturn,r)695 DEF_TEST(SkSLInterpreterEarlyReturn, r) {
696     // Test early returns with divergent control flow
697     const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }";
698 
699     ProgramBuilder program(r, src);
700 
701     const SkSL::FunctionDeclaration* main = program->getFunction("main");
702     REPORTER_ASSERT(r, main);
703 
704     skvm::Builder b;
705     SkSL::ProgramToSkVM(*program, *main->definition(), &b, /*debugTrace=*/nullptr, /*uniforms=*/{});
706     skvm::Program p = b.done();
707 
708     float xs[] = { 1.0f, 3.0f },
709           ys[] = { 2.0f, 2.0f };
710     float rets[2];
711     p.eval(2, xs, ys, rets);
712 
713     REPORTER_ASSERT(r, rets[0] == 1.0f);
714     REPORTER_ASSERT(r, rets[1] == 2.0f);
715 }
716 
DEF_TEST(SkSLInterpreterFunctions,r)717 DEF_TEST(SkSLInterpreterFunctions, r) {
718     const char* src =
719         "float sqr(float x) { return x * x; }\n"
720         "float sub(float x, float y) { return x - y; }\n"
721         "float main(float x) { return sub(sqr(x), x); }\n"
722 
723         // Different signatures
724         "float Dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n"
725         "float Dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n"
726         "float Dot3_test(float x) { return dot(float3(x, x + 1, x + 2), float3(1, -1, 2)); }\n"
727         "float Dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n";
728 
729     ProgramBuilder program(r, src);
730 
731     const SkSL::FunctionDeclaration* sub  = program->getFunction("sub");
732     const SkSL::FunctionDeclaration* sqr  = program->getFunction("sqr");
733     const SkSL::FunctionDeclaration* main = program->getFunction("main");
734     const SkSL::FunctionDeclaration* tan  = program->getFunction("tan");
735     const SkSL::FunctionDeclaration* dot3 = program->getFunction("Dot3_test");
736     const SkSL::FunctionDeclaration* dot2 = program->getFunction("Dot2_test");
737 
738     REPORTER_ASSERT(r, sub);
739     REPORTER_ASSERT(r, sqr);
740     REPORTER_ASSERT(r, main);
741     REPORTER_ASSERT(r, !tan);  // Getting a non-existent function should return nullptr
742     REPORTER_ASSERT(r, dot3);
743     REPORTER_ASSERT(r, dot2);
744 
745     auto test_fn = [&](const SkSL::FunctionDeclaration* fn, float in, float expected) {
746         skvm::Builder b;
747         SkSL::ProgramToSkVM(*program, *fn->definition(), &b,
748                             /*debugTrace=*/nullptr, /*uniforms=*/{});
749         skvm::Program p = b.done();
750 
751         float out = 0.0f;
752         p.eval(1, &in, &out);
753         REPORTER_ASSERT(r, out == expected);
754     };
755 
756     test_fn(main, 3.0f, 6.0f);
757     test_fn(dot3, 3.0f, 9.0f);
758     test_fn(dot2, 3.0f, -1.0f);
759 }
760 
DEF_TEST(SkSLInterpreterOutParams,r)761 DEF_TEST(SkSLInterpreterOutParams, r) {
762     test(r,
763          "void oneAlpha(inout half4 color) { color.a = 1; }"
764          "void main(inout half4 color) { oneAlpha(color); }",
765          0, 0, 0, 0, 0, 0, 0, 1);
766     test(r,
767          "half2 tricky(half x, half y, inout half2 color, half z) {"
768          "    color.xy = color.yx;"
769          "    return half2(x + y, z);"
770          "}"
771          "void main(inout half4 color) {"
772          "    half2 t = tricky(1, 2, color.rb, 5);"
773          "    color.ga = t;"
774          "}",
775          1, 2, 3, 4, 3, 3, 1, 5);
776 }
777 
DEF_TEST(SkSLInterpreterSwizzleSingleLvalue,r)778 DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) {
779     test(r,
780          "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }",
781          0, 0, 0, 0, 1, 2, 4, 3);
782 }
783 
DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue,r)784 DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) {
785     test(r,
786          "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }",
787          0, 0, 0, 0, 2, 1, 4, 3);
788 }
789 
DEF_TEST(SkSLInterpreterSwizzleIndexLvalue,r)790 DEF_TEST(SkSLInterpreterSwizzleIndexLvalue, r) {
791     const char* src = R"(
792         void main(inout half4 color) {
793             for (int i = 0; i < 4; i++) {
794                 color.wzyx[i] += half(i);
795             }
796         }
797     )";
798     test(r, src, 0, 0, 0, 0, 3, 2, 1, 0);
799 }
800 
DEF_TEST(SkSLInterpreterMathFunctions,r)801 DEF_TEST(SkSLInterpreterMathFunctions, r) {
802     float value[4], expected[4];
803 
804     value[0] = 0.0f; expected[0] = 0.0f;
805     test(r, "float main(float x) { return sin(x); }", value, expected);
806     test(r, "float main(float x) { return tan(x); }", value, expected);
807 
808     value[0] = 0.0f; expected[0] = 1.0f;
809     test(r, "float main(float x) { return cos(x); }", value, expected);
810 
811     value[0] = 25.0f; expected[0] = 5.0f;
812     test(r, "float main(float x) { return sqrt(x); }", value, expected);
813 
814     value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
815     test(r, "float main(float x) { return radians(x); }", value, expected);
816 
817     value[0] = 1.0f; value[1] = -1.0f;
818     expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
819     test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
820 }
821 
DEF_TEST(SkSLInterpreterVoidFunction,r)822 DEF_TEST(SkSLInterpreterVoidFunction, r) {
823     test(r,
824          "half x; void foo() { x = 1.0; }"
825          "void main(inout half4 color) { foo(); color.r = x; }",
826          0, 0, 0, 0, 1, 0, 0, 0);
827 }
828 
DEF_TEST(SkSLInterpreterMix,r)829 DEF_TEST(SkSLInterpreterMix, r) {
830     float value, expected;
831 
832     value = 0.5f; expected = 0.0f;
833     test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
834     value = 0.75f; expected = 5.0f;
835     test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
836     value = 2.0f; expected = 30.0f;
837     test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
838 
839     float valueVectors[]   = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
840           expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
841     test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
842          expectedVector);
843 }
844 
DEF_TEST(SkSLInterpreterCross,r)845 DEF_TEST(SkSLInterpreterCross, r) {
846     float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f };
847     SkV3 cross = SkV3::Cross({args[0], args[1], args[2]},
848                              {args[3], args[4], args[5]});
849     float expected[] = { cross.x, cross.y, cross.z };
850     test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
851 }
852 
DEF_TEST(SkSLInterpreterInverse,r)853 DEF_TEST(SkSLInterpreterInverse, r) {
854     {
855         SkMatrix m;
856         m.setRotate(30).postScale(1, 2);
857         float args[4] = { m[0], m[3], m[1], m[4] };
858         SkAssertResult(m.invert(&m));
859         float expt[4] = { m[0], m[3], m[1], m[4] };
860         test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
861     }
862     {
863         SkMatrix m;
864         m.setRotate(30).postScale(1, 2).postTranslate(1, 2);
865         float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
866         SkAssertResult(m.invert(&m));
867         float expt[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
868         test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
869     }
870     {
871         float args[16], expt[16];
872         // just some crazy thing that is invertible
873         SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0};
874         m.getColMajor(args);
875         SkAssertResult(m.invert(&m));
876         m.getColMajor(expt);
877         test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
878     }
879 }
880 
DEF_TEST(SkSLInterpreterDot,r)881 DEF_TEST(SkSLInterpreterDot, r) {
882     float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
883     float expected = args[0] * args[2] +
884                      args[1] * args[3];
885     test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
886 
887     expected = args[0] * args[3] +
888                args[1] * args[4] +
889                args[2] * args[5];
890     test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
891 
892     expected = args[0] * args[4] +
893                args[1] * args[5] +
894                args[2] * args[6] +
895                args[3] * args[7];
896     test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
897 }
898 
DEF_TEST(SkSLInterpreterTrace,r)899 DEF_TEST(SkSLInterpreterTrace, r) {
900     SkSL::ShaderCaps caps;
901     SkSL::Compiler compiler(&caps);
902     SkSL::ProgramSettings settings;
903     settings.fOptimize = false;
904 
905     constexpr const char kSrc[] =
906 R"(bool less_than(float left, int right) {
907     bool comparison = left < float(right);
908     if (comparison) {
909         return true;
910     } else {
911         return false;
912     }
913 }
914 
915 int main() {
916     float2 a[2];
917     for (float loop = 10; loop <= 30; loop += 10) {
918         half4 v = half4(loop, loop+1, loop+2, loop+3);
919         float2x2 m = float2x2(v);
920         a[0] = float2(loop, loop+1); a[1] = float2(loop+2, loop+3);
921         bool function_result = less_than(loop, 20);
922     }
923     return 40;
924 }
925 )";
926     skvm::Builder b;
927     std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::ProgramKind::kGeneric,
928                                                                      std::string(kSrc), settings);
929     REPORTER_ASSERT(r, program);
930 
931     const SkSL::FunctionDeclaration* main = program->getFunction("main");
932     SkSL::SkVMDebugTrace debugTrace;
933     SkSL::ProgramToSkVM(*program, *main->definition(), &b, &debugTrace, /*uniforms=*/{});
934     skvm::Program p = b.done();
935     REPORTER_ASSERT(r, p.nargs() == 1);
936 
937     int result;
938     p.eval(1, &result);
939 
940     SkDynamicMemoryWStream streamDump;
941     debugTrace.dump(&streamDump);
942 
943     sk_sp<SkData> dataDump = streamDump.detachAsData();
944     std::string_view trace{static_cast<const char*>(dataDump->data()), dataDump->size()};
945 
946     REPORTER_ASSERT(r, result == 40);
947     REPORTER_ASSERT(r, trace ==
948 R"($0 = [main].result (int, L10)
949 $1 = a[0] (float2 : slot 1/2, L11)
950 $2 = a[0] (float2 : slot 2/2, L11)
951 $3 = a[1] (float2 : slot 1/2, L11)
952 $4 = a[1] (float2 : slot 2/2, L11)
953 $5 = loop (float, L12)
954 $6 = v (float4 : slot 1/4, L13)
955 $7 = v (float4 : slot 2/4, L13)
956 $8 = v (float4 : slot 3/4, L13)
957 $9 = v (float4 : slot 4/4, L13)
958 $10 = m (float2x2 : slot 1/4, L14)
959 $11 = m (float2x2 : slot 2/4, L14)
960 $12 = m (float2x2 : slot 3/4, L14)
961 $13 = m (float2x2 : slot 4/4, L14)
962 $14 = function_result (bool, L16)
963 $15 = [less_than].result (bool, L1)
964 $16 = left (float, L1)
965 $17 = right (int, L1)
966 $18 = comparison (bool, L2)
967 F0 = int main()
968 F1 = bool less_than(float left, int right)
969 
970 enter int main()
971   scope +1
972    line 11
973    a[0].x = 0
974    a[0].y = 0
975    a[1].x = 0
976    a[1].y = 0
977    line 12
978    scope +1
979     loop = 10
980     scope +1
981      line 13
982      v.x = 10
983      v.y = 11
984      v.z = 12
985      v.w = 13
986      line 14
987      m[0][0] = 10
988      m[0][1] = 11
989      m[1][0] = 12
990      m[1][1] = 13
991      line 15
992      a[0].x = 10
993      a[0].y = 11
994      line 15
995      a[1].x = 12
996      a[1].y = 13
997      line 16
998      enter bool less_than(float left, int right)
999        left = 10
1000        right = 20
1001        scope +1
1002         line 2
1003         comparison = true
1004         line 3
1005         scope +1
1006          line 4
1007          [less_than].result = true
1008         scope -1
1009        scope -1
1010      exit bool less_than(float left, int right)
1011      function_result = true
1012     scope -1
1013     line 12
1014     loop = 20
1015     scope +1
1016      line 13
1017      v.x = 20
1018      v.y = 21
1019      v.z = 22
1020      v.w = 23
1021      line 14
1022      m[0][0] = 20
1023      m[0][1] = 21
1024      m[1][0] = 22
1025      m[1][1] = 23
1026      line 15
1027      a[0].x = 20
1028      a[0].y = 21
1029      line 15
1030      a[1].x = 22
1031      a[1].y = 23
1032      line 16
1033      enter bool less_than(float left, int right)
1034        left = 20
1035        right = 20
1036        scope +1
1037         line 2
1038         comparison = false
1039         line 3
1040         scope +1
1041          line 6
1042          [less_than].result = false
1043         scope -1
1044        scope -1
1045      exit bool less_than(float left, int right)
1046      function_result = false
1047     scope -1
1048     line 12
1049     loop = 30
1050     scope +1
1051      line 13
1052      v.x = 30
1053      v.y = 31
1054      v.z = 32
1055      v.w = 33
1056      line 14
1057      m[0][0] = 30
1058      m[0][1] = 31
1059      m[1][0] = 32
1060      m[1][1] = 33
1061      line 15
1062      a[0].x = 30
1063      a[0].y = 31
1064      line 15
1065      a[1].x = 32
1066      a[1].y = 33
1067      line 16
1068      enter bool less_than(float left, int right)
1069        left = 30
1070        right = 20
1071        scope +1
1072         line 2
1073         comparison = false
1074         line 3
1075         scope +1
1076          line 6
1077          [less_than].result = false
1078         scope -1
1079        scope -1
1080      exit bool less_than(float left, int right)
1081      function_result = false
1082     scope -1
1083     line 12
1084    scope -1
1085    line 18
1086    [main].result = 40
1087   scope -1
1088 exit int main()
1089 )", "Trace output does not match expectation:\n%.*s\n", (int)trace.size(), trace.data());
1090 }
1091