Optimize ternary tests that check a const variable.
This enables the ternary to be optimized away in code like: const bool SHINY = true; color = SHINY ? add_shine(x) : x; // to --> `color = add_shine(x);` Without constant propagation. Also, I added a unit test for ternary expression simplification; I wasn't able to find an existing one. When the optimization flag is disabled, this CL actually removes the optimization of `true ? x : y` --> `x` entirely; previously, this substitution would be made regardless of optimization settings. Change-Id: I93a8b9d4027902d35f8a19cfd6417170b209d056 Bug: skia:11343 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/379297 Reviewed-by: Brian Osman <brianosman@google.com> Commit-Queue: John Stiles <johnstiles@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
parent
3dc6c190da
commit
28054added
@ -431,6 +431,7 @@ sksl_shared_tests = [
|
||||
"/sksl/shared/SwizzleScalar.sksl",
|
||||
"/sksl/shared/TernaryAsLValueEntirelyFoldable.sksl",
|
||||
"/sksl/shared/TernaryAsLValueFoldableTest.sksl",
|
||||
"/sksl/shared/TernaryExpression.sksl",
|
||||
"/sksl/shared/Texture1D.sksl",
|
||||
"/sksl/shared/Texture2D.sksl",
|
||||
"/sksl/shared/TextureSharpen.sksl",
|
||||
|
26
resources/sksl/shared/TernaryExpression.sksl
Normal file
26
resources/sksl/shared/TernaryExpression.sksl
Normal file
@ -0,0 +1,26 @@
|
||||
uniform half4 colorGreen, colorRed;
|
||||
|
||||
half4 main() {
|
||||
const bool TRUE = true;
|
||||
const bool FALSE = false;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
// Literal test
|
||||
ok = ok && (true ? true : false);
|
||||
ok = ok && (false ? false : true);
|
||||
|
||||
// Constant boolean test
|
||||
ok = ok && (TRUE ? true : false);
|
||||
ok = ok && (FALSE ? false : true);
|
||||
|
||||
// Constant-foldable test
|
||||
ok = ok && (1 == 1 ? true : false);
|
||||
ok = ok && (0 == 1 ? false : true);
|
||||
|
||||
// Unknown-value test
|
||||
ok = ok && (colorGreen.g == 1 ? true : false);
|
||||
ok = ok && (colorGreen.r == 1 ? false : true);
|
||||
|
||||
return ok ? colorGreen : colorRed;
|
||||
}
|
@ -719,6 +719,9 @@ void Compiler::simplifyExpression(DefinitionMap& definitions,
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::kTernary: {
|
||||
// TODO(skia:11319): this optimization logic is redundant with the optimization code
|
||||
// found in SkSLTernaryExpression.cpp.
|
||||
|
||||
TernaryExpression* t = &expr->as<TernaryExpression>();
|
||||
if (t->test()->is<BoolLiteral>()) {
|
||||
// ternary has a constant test, replace it with either the true or
|
||||
|
@ -5,6 +5,7 @@
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "src/sksl/SkSLConstantFolder.h"
|
||||
#include "src/sksl/SkSLContext.h"
|
||||
#include "src/sksl/SkSLOperators.h"
|
||||
#include "src/sksl/SkSLProgramSettings.h"
|
||||
@ -63,14 +64,15 @@ std::unique_ptr<Expression> TernaryExpression::Make(const Context& context,
|
||||
SkASSERT(!ifTrue->type().componentType().isOpaque());
|
||||
SkASSERT(!context.fConfig->strictES2Mode() || !ifTrue->type().isOrContainsArray());
|
||||
|
||||
if (test->is<BoolLiteral>()) {
|
||||
// static boolean test, just return one of the branches
|
||||
if (test->as<BoolLiteral>().value()) {
|
||||
return ifTrue;
|
||||
} else {
|
||||
return ifFalse;
|
||||
if (context.fConfig->fSettings.fOptimize) {
|
||||
const Expression* testExpr = ConstantFolder::GetConstantValueForVariable(*test);
|
||||
if (testExpr->is<BoolLiteral>()) {
|
||||
// static boolean test, just return one of the branches
|
||||
return testExpr->as<BoolLiteral>().value() ? std::move(ifTrue)
|
||||
: std::move(ifFalse);
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_unique<TernaryExpression>(test->fOffset, std::move(test),
|
||||
std::move(ifTrue), std::move(ifFalse));
|
||||
}
|
||||
|
@ -183,6 +183,7 @@ SKSL_TEST(SkSLSwizzleOpt, "shared/SwizzleOpt.sksl")
|
||||
SKSL_TEST(SkSLSwizzleScalar, "shared/SwizzleScalar.sksl")
|
||||
SKSL_TEST(SkSLTernaryAsLValueEntirelyFoldable, "shared/TernaryAsLValueEntirelyFoldable.sksl")
|
||||
SKSL_TEST(SkSLTernaryAsLValueFoldableTest, "shared/TernaryAsLValueFoldableTest.sksl")
|
||||
SKSL_TEST(SkSLTernaryExpression, "shared/TernaryExpression.sksl")
|
||||
SKSL_TEST(SkSLUnaryPositiveNegative, "shared/UnaryPositiveNegative.sksl")
|
||||
SKSL_TEST(SkSLUnusedVariables, "shared/UnusedVariables.sksl")
|
||||
SKSL_TEST(SkSLVectorConstructors, "shared/VectorConstructors.sksl")
|
||||
|
101
tests/sksl/shared/TernaryExpression.asm.frag
Normal file
101
tests/sksl/shared/TernaryExpression.asm.frag
Normal file
@ -0,0 +1,101 @@
|
||||
OpCapability Shader
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint Fragment %_entrypoint "_entrypoint" %sk_FragColor %sk_Clockwise
|
||||
OpExecutionMode %_entrypoint OriginUpperLeft
|
||||
OpName %sk_FragColor "sk_FragColor"
|
||||
OpName %sk_Clockwise "sk_Clockwise"
|
||||
OpName %_UniformBuffer "_UniformBuffer"
|
||||
OpMemberName %_UniformBuffer 0 "colorGreen"
|
||||
OpMemberName %_UniformBuffer 1 "colorRed"
|
||||
OpName %_entrypoint "_entrypoint"
|
||||
OpName %main "main"
|
||||
OpName %ok "ok"
|
||||
OpDecorate %sk_FragColor RelaxedPrecision
|
||||
OpDecorate %sk_FragColor Location 0
|
||||
OpDecorate %sk_FragColor Index 0
|
||||
OpDecorate %sk_Clockwise RelaxedPrecision
|
||||
OpDecorate %sk_Clockwise BuiltIn FrontFacing
|
||||
OpMemberDecorate %_UniformBuffer 0 Offset 0
|
||||
OpMemberDecorate %_UniformBuffer 0 RelaxedPrecision
|
||||
OpMemberDecorate %_UniformBuffer 1 Offset 16
|
||||
OpMemberDecorate %_UniformBuffer 1 RelaxedPrecision
|
||||
OpDecorate %_UniformBuffer Block
|
||||
OpDecorate %10 Binding 0
|
||||
OpDecorate %10 DescriptorSet 0
|
||||
OpDecorate %27 RelaxedPrecision
|
||||
OpDecorate %33 RelaxedPrecision
|
||||
OpDecorate %37 RelaxedPrecision
|
||||
OpDecorate %42 RelaxedPrecision
|
||||
OpDecorate %49 RelaxedPrecision
|
||||
OpDecorate %52 RelaxedPrecision
|
||||
OpDecorate %53 RelaxedPrecision
|
||||
%float = OpTypeFloat 32
|
||||
%v4float = OpTypeVector %float 4
|
||||
%_ptr_Output_v4float = OpTypePointer Output %v4float
|
||||
%sk_FragColor = OpVariable %_ptr_Output_v4float Output
|
||||
%bool = OpTypeBool
|
||||
%_ptr_Input_bool = OpTypePointer Input %bool
|
||||
%sk_Clockwise = OpVariable %_ptr_Input_bool Input
|
||||
%_UniformBuffer = OpTypeStruct %v4float %v4float
|
||||
%_ptr_Uniform__UniformBuffer = OpTypePointer Uniform %_UniformBuffer
|
||||
%10 = OpVariable %_ptr_Uniform__UniformBuffer Uniform
|
||||
%void = OpTypeVoid
|
||||
%15 = OpTypeFunction %void
|
||||
%18 = OpTypeFunction %v4float
|
||||
%_ptr_Function_bool = OpTypePointer Function %bool
|
||||
%true = OpConstantTrue %bool
|
||||
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
|
||||
%int = OpTypeInt 32 1
|
||||
%int_0 = OpConstant %int 0
|
||||
%float_1 = OpConstant %float 1
|
||||
%false = OpConstantFalse %bool
|
||||
%_ptr_Function_v4float = OpTypePointer Function %v4float
|
||||
%int_1 = OpConstant %int 1
|
||||
%_entrypoint = OpFunction %void None %15
|
||||
%16 = OpLabel
|
||||
%17 = OpFunctionCall %v4float %main
|
||||
OpStore %sk_FragColor %17
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%main = OpFunction %v4float None %18
|
||||
%19 = OpLabel
|
||||
%ok = OpVariable %_ptr_Function_bool Function
|
||||
%43 = OpVariable %_ptr_Function_v4float Function
|
||||
OpStore %ok %true
|
||||
%23 = OpAccessChain %_ptr_Uniform_v4float %10 %int_0
|
||||
%27 = OpLoad %v4float %23
|
||||
%28 = OpCompositeExtract %float %27 1
|
||||
%30 = OpFOrdEqual %bool %28 %float_1
|
||||
%31 = OpSelect %bool %30 %true %false
|
||||
OpStore %ok %31
|
||||
%33 = OpLoad %bool %ok
|
||||
OpSelectionMerge %35 None
|
||||
OpBranchConditional %33 %34 %35
|
||||
%34 = OpLabel
|
||||
%36 = OpAccessChain %_ptr_Uniform_v4float %10 %int_0
|
||||
%37 = OpLoad %v4float %36
|
||||
%38 = OpCompositeExtract %float %37 0
|
||||
%39 = OpFOrdEqual %bool %38 %float_1
|
||||
%40 = OpSelect %bool %39 %false %true
|
||||
OpBranch %35
|
||||
%35 = OpLabel
|
||||
%41 = OpPhi %bool %false %19 %40 %34
|
||||
OpStore %ok %41
|
||||
%42 = OpLoad %bool %ok
|
||||
OpSelectionMerge %47 None
|
||||
OpBranchConditional %42 %45 %46
|
||||
%45 = OpLabel
|
||||
%48 = OpAccessChain %_ptr_Uniform_v4float %10 %int_0
|
||||
%49 = OpLoad %v4float %48
|
||||
OpStore %43 %49
|
||||
OpBranch %47
|
||||
%46 = OpLabel
|
||||
%50 = OpAccessChain %_ptr_Uniform_v4float %10 %int_1
|
||||
%52 = OpLoad %v4float %50
|
||||
OpStore %43 %52
|
||||
OpBranch %47
|
||||
%47 = OpLabel
|
||||
%53 = OpLoad %v4float %43
|
||||
OpReturnValue %53
|
||||
OpFunctionEnd
|
10
tests/sksl/shared/TernaryExpression.glsl
Normal file
10
tests/sksl/shared/TernaryExpression.glsl
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
out vec4 sk_FragColor;
|
||||
uniform vec4 colorGreen;
|
||||
uniform vec4 colorRed;
|
||||
vec4 main() {
|
||||
bool ok = true;
|
||||
ok = colorGreen.y == 1.0 ? true : false;
|
||||
ok = ok && (colorGreen.x == 1.0 ? false : true);
|
||||
return ok ? colorGreen : colorRed;
|
||||
}
|
23
tests/sksl/shared/TernaryExpression.metal
Normal file
23
tests/sksl/shared/TernaryExpression.metal
Normal file
@ -0,0 +1,23 @@
|
||||
#include <metal_stdlib>
|
||||
#include <simd/simd.h>
|
||||
using namespace metal;
|
||||
struct Uniforms {
|
||||
float4 colorGreen;
|
||||
float4 colorRed;
|
||||
};
|
||||
struct Inputs {
|
||||
};
|
||||
struct Outputs {
|
||||
float4 sk_FragColor [[color(0)]];
|
||||
};
|
||||
|
||||
|
||||
fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
|
||||
Outputs _out;
|
||||
(void)_out;
|
||||
bool ok = true;
|
||||
ok = _uniforms.colorGreen.y == 1.0 ? true : false;
|
||||
ok = ok && (_uniforms.colorGreen.x == 1.0 ? false : true);
|
||||
_out.sk_FragColor = ok ? _uniforms.colorGreen : _uniforms.colorRed;
|
||||
return _out;
|
||||
}
|
Loading…
Reference in New Issue
Block a user