From 3d81fdcbd232f45b86a722e3d8758a6eca64d258 Mon Sep 17 00:00:00 2001 From: Brian Osman Date: Wed, 13 Jan 2021 17:17:56 -0500 Subject: [PATCH] Add unit tests for for-loop unrolling Change-Id: I350a6768ac124362b0d3e0f17e7a026265acf804 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/353627 Reviewed-by: Mike Klein Commit-Queue: Brian Osman --- gn/sksl_tests.gni | 2 ++ src/core/SkVM.cpp | 4 +++ tests/sksl/runtime/LoopFloat.rte | 40 ++++++++++++++++++++++++ tests/sksl/runtime/LoopInt.rte | 31 ++++++++++++++++++ tests/sksl/runtime/golden/LoopFloat.skvm | 10 ++++++ tests/sksl/runtime/golden/LoopInt.skvm | 10 ++++++ 6 files changed, 97 insertions(+) create mode 100644 tests/sksl/runtime/LoopFloat.rte create mode 100644 tests/sksl/runtime/LoopInt.rte create mode 100644 tests/sksl/runtime/golden/LoopFloat.skvm create mode 100644 tests/sksl/runtime/golden/LoopInt.skvm diff --git a/gn/sksl_tests.gni b/gn/sksl_tests.gni index f45d06cc38..ac75baa1a9 100644 --- a/gn/sksl_tests.gni +++ b/gn/sksl_tests.gni @@ -485,6 +485,8 @@ sksl_settings_tests = [ sksl_rte_tests = [ "$_tests/sksl/runtime/ConversionConstructors.rte", + "$_tests/sksl/runtime/LoopInt.rte", + "$_tests/sksl/runtime/LoopFloat.rte", "$_tests/sksl/runtime/SampleWithConstantMatrix.rte", "$_tests/sksl/runtime/SampleWithExplicitCoord.rte", "$_tests/sksl/runtime/SampleWithUniformMatrix.rte", diff --git a/src/core/SkVM.cpp b/src/core/SkVM.cpp index bd9688f0bd..bd4318ff93 100644 --- a/src/core/SkVM.cpp +++ b/src/core/SkVM.cpp @@ -920,16 +920,20 @@ namespace skvm { I32 Builder:: eq(I32 x, I32 y) { if (x.id == y.id) { return splat(~0); } + if (int X,Y; this->allImm(x.id,&X, y.id,&Y)) { return splat(X==Y ? ~0 : 0); } return {this, this->push(Op:: eq_i32, x.id, y.id)}; } I32 Builder::neq(I32 x, I32 y) { + if (int X,Y; this->allImm(x.id,&X, y.id,&Y)) { return splat(X!=Y ? ~0 : 0); } return ~(x == y); } I32 Builder:: gt(I32 x, I32 y) { + if (int X,Y; this->allImm(x.id,&X, y.id,&Y)) { return splat(X> Y ? ~0 : 0); } return {this, this->push(Op:: gt_i32, x.id, y.id)}; } I32 Builder::gte(I32 x, I32 y) { if (x.id == y.id) { return splat(~0); } + if (int X,Y; this->allImm(x.id,&X, y.id,&Y)) { return splat(X>=Y ? ~0 : 0); } return ~(x < y); } I32 Builder:: lt(I32 x, I32 y) { return y>x; } diff --git a/tests/sksl/runtime/LoopFloat.rte b/tests/sksl/runtime/LoopFloat.rte new file mode 100644 index 0000000000..ec19f8e4e2 --- /dev/null +++ b/tests/sksl/runtime/LoopFloat.rte @@ -0,0 +1,40 @@ +// Should return 5 +float return_loop() { + for (float i = 0; i < 10; ++i) { + if (i == 5) { return i; } + } + return 0; +} + +// Should return 35 +float continue_loop() { + float sum = 0; + for (float i = 0; i < 10; ++i) { + if (i < 5) { continue; } + sum += i; + } + return sum; +} + +// Should return 15 +float break_loop() { + float sum = 0; + for (float i = 0; i < 10; ++i) { + if (i > 5) { break; } + sum += i; + } + return sum; +} + +// Should return ~1.725 +float float_loop() { + float sum = 0; + for (float i = 0.123; i < 0.6; i += 0.111) { + sum += i; + } + return sum; +} + +half4 main() { + return half4(return_loop(), continue_loop(), break_loop(), float_loop()); +} diff --git a/tests/sksl/runtime/LoopInt.rte b/tests/sksl/runtime/LoopInt.rte new file mode 100644 index 0000000000..ce36ffe70f --- /dev/null +++ b/tests/sksl/runtime/LoopInt.rte @@ -0,0 +1,31 @@ +// Should return 5 +int return_loop() { + for (int i = 0; i < 10; ++i) { + if (i == 5) { return i; } + } + return 0; +} + +// Should return 35 +int continue_loop() { + int sum = 0; + for (int i = 0; i < 10; ++i) { + if (i < 5) { continue; } + sum += i; + } + return sum; +} + +// Should return 15 +int break_loop() { + int sum = 0; + for (int i = 0; i < 10; ++i) { + if (i > 5) { break; } + sum += i; + } + return sum; +} + +half4 main() { + return half4(return_loop(), continue_loop(), break_loop(), 1); +} diff --git a/tests/sksl/runtime/golden/LoopFloat.skvm b/tests/sksl/runtime/golden/LoopFloat.skvm new file mode 100644 index 0000000000..85795b9f93 --- /dev/null +++ b/tests/sksl/runtime/golden/LoopFloat.skvm @@ -0,0 +1,10 @@ +4 registers, 8 instructions: +0 r0 = splat 40A00000 (5) +1 r1 = splat 420C0000 (35) +2 r2 = splat 41700000 (15) +3 r3 = splat 3FDCCCCD (1.725) +loop: +4 store32 ptr1 r0 +5 store32 ptr2 r1 +6 store32 ptr3 r2 +7 store32 ptr4 r3 diff --git a/tests/sksl/runtime/golden/LoopInt.skvm b/tests/sksl/runtime/golden/LoopInt.skvm new file mode 100644 index 0000000000..b1c3c4d802 --- /dev/null +++ b/tests/sksl/runtime/golden/LoopInt.skvm @@ -0,0 +1,10 @@ +4 registers, 8 instructions: +0 r0 = splat 40A00000 (5) +1 r1 = splat 420C0000 (35) +2 r2 = splat 41700000 (15) +3 r3 = splat 3F800000 (1) +loop: +4 store32 ptr1 r0 +5 store32 ptr2 r1 +6 store32 ptr3 r2 +7 store32 ptr4 r3