| 1 |
From 2b94912a4d8d8576116046955167a1ca32c13fa1 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Kaylee Lubick <[email protected]> |
| 3 |
Date: Mon, 27 Apr 2026 15:55:24 +0000 |
| 4 |
Subject: [PATCH] Avoid removing too many stack entries in SkRP during discard_stack |
| 5 |
|
| 6 |
Consider an sksl snippet like: |
| 7 |
``` |
| 8 |
half4 main(float2 xy) { |
| 9 |
float4 v = float4(xy.x, -1.0, -2.0, -3.0); |
| 10 |
v = abs(v); |
| 11 |
return half4(v); |
| 12 |
} |
| 13 |
``` |
| 14 |
|
| 15 |
this could be turned into (unoptimized) instructions like [1] |
| 16 |
``` |
| 17 |
store_src_rg xy = src.rg |
| 18 |
init_lane_masks CondMask = LoopMask = RetMask = true |
| 19 |
copy_slot_unmasked v(0) = xy(0) |
| 20 |
copy_constant v(1) = 0xBF800000 (-1.0) |
| 21 |
copy_constant v(2) = 0xC0000000 (-2.0) |
| 22 |
copy_constant v(3) = 0xC0400000 (-3.0) |
| 23 |
copy_4_slots_unmasked $0..3 = v # unnecessary |
| 24 |
bitwise_and_imm_4_ints $0..3 &= 0x7FFFFFFF |
| 25 |
copy_4_slots_unmasked v = $0..3 |
| 26 |
load_src src.rgba = $0..3 |
| 27 |
``` |
| 28 |
( the bitwise_and_imm_4_ints instruction is the abs() part, masking |
| 29 |
off the sign bit) |
| 30 |
|
| 31 |
We can optimize cases where we push to the stack (e.g. |
| 32 |
the copy_4_slots_unmasked), do an operation (the &=) and then |
| 33 |
pop the value off the stack into just doing the operation w/o |
| 34 |
involving the stack. (We might have to push the result to the |
| 35 |
stack for further use). |
| 36 |
``` |
| 37 |
... |
| 38 |
copy_constant v(3) = 0xC0400000 (-3.0) |
| 39 |
bitwise_and_imm_4_ints v &= 0x7FFFFFFF |
| 40 |
copy_4_slots_unmasked $0..3 = v |
| 41 |
load_src src.rgba = $0..3 |
| 42 |
``` |
| 43 |
|
| 44 |
There was a bug in the optimization where we removed *all* the |
| 45 |
slots for an instruction (4 in this case because it's a float4) |
| 46 |
even though the call was discard_stack(1). This would be followed |
| 47 |
up by a call to discard_stack(3) (the remainder of the previous |
| 48 |
instruction's stack) and we'd underflow the stack. |
| 49 |
|
| 50 |
Problematic sksl: |
| 51 |
``` |
| 52 |
half4 main(float2 xy) { |
| 53 |
float4 v; |
| 54 |
v.x += xy.x; |
| 55 |
(v = abs(v)).xyz; # drop 1 channel (the .w) |
| 56 |
return half4(v); |
| 57 |
} |
| 58 |
// This was in the output |
| 59 |
bitwise_and_imm_4_ints v &= 0x7FFFFFFF |
| 60 |
copy_4_slots_unmasked ExternalPtr(0..3) = v |
| 61 |
load_src src.rgba = ExternalPtr(0..3) |
| 62 |
``` |
| 63 |
|
| 64 |
where ExternalPtr is referring to memory outside any variables, |
| 65 |
uniforms, or the temporary stack. Yikes! I'll keep that in mind |
| 66 |
for the future. |
| 67 |
|
| 68 |
Anyway, for the fix, we'll only remove N slots if the previous |
| 69 |
call was N slots wide. |
| 70 |
|
| 71 |
[1] e.g. bazel build //tools/skslc && bazel-bin/tools/skslc/skslc input.sksl output.skrp |
| 72 |
|
| 73 |
Bug: https://issues.chromium.org/issues/500080194 |
| 74 |
Change-Id: Ib43c62dbec9a2c1d03c2d4960a2925b131504550 |
| 75 |
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1218502 |
| 76 |
Commit-Queue: Kaylee Lubick <[email protected]> |
| 77 |
Reviewed-by: Florin Malita <[email protected]> |
| 78 |
--- |
| 79 |
.../codegen/SkSLRasterPipelineBuilder.cpp | 4 +- |
| 80 |
1 file changed, 2 insertions(+), 2 deletions(-) |
| 81 |
|
| 82 |
diff --git a/src/sksl/codegen/SkSLRasterPipelineBuilder.cpp b/src/sksl/codegen/SkSLRasterPipelineBuilder.cpp |
| 83 |
index 9df02016bf..cac2b68d9a 100644 |
| 84 |
--- a/src/sksl/codegen/SkSLRasterPipelineBuilder.cpp |
| 85 |
+++ b/src/sksl/codegen/SkSLRasterPipelineBuilder.cpp |
| 86 |
@@ -387,7 +387,7 @@ void Builder::discard_stack(int32_t count, int stackID) { |
| 87 |
case BuilderOp::copy_stack_to_slots_unmasked: { |
| 88 |
// Look for a pattern of `push, immediate-ops, pop` and simplify it down to an |
| 89 |
// immediate-op directly to the value slot. |
| 90 |
- if (count == 1) { |
| 91 |
+ if (count == lastInstruction->fImmA) { |
| 92 |
if (this->simplifyImmediateUnmaskedOp()) { |
| 93 |
return; |
| 94 |
} |
| 95 |
@@ -1374,7 +1374,7 @@ Program::StackDepths Program::tempStackMaxDepths() const { |
| 96 |
current[stackID] += stack_usage(inst); |
| 97 |
largest[stackID] = std::max(current[stackID], largest[stackID]); |
| 98 |
// If we assert here, the generated program has popped off the top of the stack. |
| 99 |
- SkASSERTF(current[stackID] >= 0, "unbalanced temp stack push/pop on stack %d", stackID); |
| 100 |
+ SkASSERTF_RELEASE(current[stackID] >= 0, "unbalanced temp stack push/pop on stack"); |
| 101 |
} |
| 102 |
|
| 103 |
// Ensure that when the program is complete, our stacks are fully balanced. |
| 104 |
-- |
| 105 |
2.43.0 |
| 106 |
|