Skip to content

Commit

Permalink
Reorder commutative ops involving constant variables or XOR.
Browse files Browse the repository at this point in the history
Previously, we would reorder `2 * x` into `x * 2` (since the latter
can use an immediate op), but we would not have reordered `PI * x`
because `PI` is a constant variable, not a Literal. We now detect
those cases and rewrite as `x * PI` (allowing us to save an
instruction via `mul_imm_float`).

Also, we now apply commutative reordering to ^ and ^^, since the xor
op now has an immediate-mode version.

Change-Id: I1e1da2d31e8df48ec4c57c544dcc5bca7944f25e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/684099
Commit-Queue: Brian Osman <[email protected]>
Auto-Submit: John Stiles <[email protected]>
Reviewed-by: Brian Osman <[email protected]>
  • Loading branch information
johnstiles-google authored and SkCQ committed Apr 27, 2023
1 parent a21a30c commit 0102755
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/sksl/codegen/SkSLRasterPipelineCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2261,11 +2261,15 @@ bool Generator::pushBinaryExpression(const Expression& left, Operator op, const
// Builder more opportunities to use immediate-mode ops.
case OperatorKind::PLUS:
case OperatorKind::STAR:
if (left.is<Literal>() && !right.is<Literal>()) {
case OperatorKind::BITWISEXOR:
case OperatorKind::LOGICALXOR: {
double unused;
if (ConstantFolder::GetConstantValue(left, &unused) &&
!ConstantFolder::GetConstantValue(right, &unused)) {
return this->pushBinaryExpression(right, op, left);
}
break;

}
// Emit comma expressions.
case OperatorKind::COMMA:
if (Analysis::HasSideEffects(left)) {
Expand Down
10 changes: 4 additions & 6 deletions tests/sksl/folding/ArrayFolding.skrp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ copy_slot_unmasked _7_two = $13
copy_constant $14 = 0x00000003 (4.203895e-45)
copy_slot_unmasked $0 = $14
copy_slot_unmasked _13_noFlatten2 = $0
copy_constant $0 = 0x00000001 (1.401298e-45)
copy_slot_unmasked $1 = _11_noFlatten0
cmpeq_int $0 = equal($0, $1)
copy_slot_unmasked $0 = _11_noFlatten0
cmpeq_imm_int $0 = equal($0, 0x00000001)
copy_slot_unmasked $1 = _9_flatten1
copy_slot_unmasked $2 = _12_noFlatten1
cmpeq_int $1 = equal($1, $2)
bitwise_and_int $0 &= $1
copy_constant $1 = 0x00000003 (4.203895e-45)
copy_slot_unmasked $2 = _13_noFlatten2
cmpeq_int $1 = equal($1, $2)
copy_slot_unmasked $1 = _13_noFlatten2
cmpeq_imm_int $1 = equal($1, 0x00000003)
bitwise_and_int $0 &= $1
swizzle_4 $0..3 = ($0..3).xxxx
copy_4_uniforms $4..7 = colorRed
Expand Down
5 changes: 2 additions & 3 deletions tests/sksl/folding/ShortCircuitBoolFolding.skrp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ copy_slot_masked _2_bad = Mask($2)
load_condition_mask CondMask = $0
add_imm_int _1_ok += 0x00000001
store_condition_mask $0 = CondMask
copy_constant $1 = 0xFFFFFFFF
copy_slot_unmasked $2 = _0_expr
bitwise_xor_int $1 ^= $2
copy_slot_unmasked $1 = _0_expr
bitwise_xor_imm_int $1 ^= 0xFFFFFFFF
merge_condition_mask CondMask = $0 & $1
copy_slot_unmasked $2 = _2_bad
add_imm_int $2 += 0x00000001
Expand Down

0 comments on commit 0102755

Please sign in to comment.