Fold casts of known values at compile time.

A cast like `float(five)` or `int4(colorGreen)` will now detect const
variables and replace the expression with its compile-time constant
equivalent value. This can unblock further constant folding
opportunities.

(This CL is very similar in spirit to http://review.skia.org/404676)

Change-Id: If78a2091770777b0caaaec696fe15a0f55d88c24
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/405683
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
John Stiles 2021-05-07 11:22:45 -04:00 committed by Skia Commit-Bot
parent 27193d4bce
commit dabb2891c4
3 changed files with 13 additions and 11 deletions

View File

@ -7,6 +7,7 @@
#include "src/sksl/ir/SkSLConstructorCompoundCast.h"
#include "src/sksl/SkSLConstantFolder.h"
#include "src/sksl/ir/SkSLConstructor.h"
#include "src/sksl/ir/SkSLConstructorCompound.h"
#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
@ -78,6 +79,12 @@ std::unique_ptr<Expression> ConstructorCompoundCast::Make(const Context& context
if (type == arg->type()) {
return arg;
}
// When optimization is on, look up the value of constant variables. This allows expressions
// like `int4(colorGreen)` to be replaced with the compile-time constant `int4(0, 1, 0, 1)`,
// which is eligible for constant folding.
if (context.fConfig->fSettings.fOptimize) {
arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg));
}
// We can cast a vector of compile-time constants at compile-time.
if (arg->isCompileTimeConstant()) {
return cast_constant_composite(context, type, std::move(arg));

View File

@ -5,6 +5,7 @@
* found in the LICENSE file.
*/
#include "src/sksl/SkSLConstantFolder.h"
#include "src/sksl/ir/SkSLConstructorScalarCast.h"
namespace SkSL {
@ -88,6 +89,11 @@ std::unique_ptr<Expression> ConstructorScalarCast::Make(const Context& context,
if (arg->type() == type) {
return arg;
}
// When optimization is on, look up the value of constant variables. This allows expressions
// like `int(zero)` to be replaced with a literal zero.
if (context.fConfig->fSettings.fOptimize) {
arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg));
}
// We can cast scalar literals at compile-time.
if (std::unique_ptr<Expression> converted = cast_scalar_literal(type, *arg)) {
return converted;

View File

@ -3,17 +3,6 @@ out vec4 sk_FragColor;
uniform vec4 colorRed;
uniform vec4 colorGreen;
vec4 main() {
const float _0_floatOne = 1.0;
const int _1_intOne = 1;
const vec4 _2_half4One = vec4(1.0);
const ivec4 _3_int4One = ivec4(1);
bool _4_ok = true;
_4_ok = _4_ok && int(_0_floatOne) == _1_intOne;
_4_ok = _4_ok && float(_1_intOne) == _0_floatOne;
_4_ok = _4_ok && ivec4(_2_half4One) == _3_int4One;
_4_ok = _4_ok && vec4(_3_int4One) == _2_half4One;
_4_ok = _4_ok && ivec4(_2_half4One) == ivec4(1);
_4_ok = _4_ok && vec4(_3_int4One) == vec4(_0_floatOne);
_4_ok = _4_ok && vec4(float(_1_intOne)) == vec4(1.0);
return _4_ok ? colorGreen : colorRed;
}