Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8fa3b4e

Browse files
johnstiles-googleSkia Commit-Bot
authored andcommitted
Disallow inlining ternary true/false branches.
This prevents unnecessary work from being done, and also prevents us from executing side-effects from the wrong side. Change-Id: I4dbf3974388807f15e9eadb2abf1b1243d047ce2 Bug: skia:10688 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/314797 Reviewed-by: Brian Osman <brianosman@google.com> Commit-Queue: Brian Osman <brianosman@google.com> Commit-Queue: John Stiles <johnstiles@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
1 parent 648a81e commit 8fa3b4e

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

src/sksl/SkSLIRGenerator.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,13 +1879,18 @@ std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode&
18791879
if (!test) {
18801880
return nullptr;
18811881
}
1882-
std::unique_ptr<Expression> ifTrue = this->convertExpression(*(iter++));
1883-
if (!ifTrue) {
1884-
return nullptr;
1885-
}
1886-
std::unique_ptr<Expression> ifFalse = this->convertExpression(*(iter++));
1887-
if (!ifFalse) {
1888-
return nullptr;
1882+
std::unique_ptr<Expression> ifTrue;
1883+
std::unique_ptr<Expression> ifFalse;
1884+
{
1885+
AutoDisableInline disableInline(this);
1886+
ifTrue = this->convertExpression(*(iter++));
1887+
if (!ifTrue) {
1888+
return nullptr;
1889+
}
1890+
ifFalse = this->convertExpression(*(iter++));
1891+
if (!ifFalse) {
1892+
return nullptr;
1893+
}
18891894
}
18901895
const Type* trueType;
18911896
const Type* falseType;
@@ -1918,10 +1923,10 @@ std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(const ASTNode&
19181923
return ifFalse;
19191924
}
19201925
}
1921-
return std::unique_ptr<Expression>(new TernaryExpression(node.fOffset,
1922-
std::move(test),
1923-
std::move(ifTrue),
1924-
std::move(ifFalse)));
1926+
return std::make_unique<TernaryExpression>(node.fOffset,
1927+
std::move(test),
1928+
std::move(ifTrue),
1929+
std::move(ifFalse));
19251930
}
19261931

19271932
void IRGenerator::copyIntrinsicIfNeeded(const FunctionDeclaration& function) {

tests/SkSLFPTest.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,9 +1088,6 @@ R"SkSL(%s = %s(%s);
10881088
}
10891089

10901090
DEF_TEST(SkSLFPTernaryExpressionsShouldNotInlineResults, r) {
1091-
// NOTE: this test exposes a bug with the inliner. We don't want to inline both sides of a
1092-
// ternary, since only one side needs to be evaluated, and side effects from the wrong side
1093-
// must not occur.
10941091
test(r,
10951092
*SkSL::ShaderCapsFactory::Default(),
10961093
R"__SkSL__(
@@ -1114,30 +1111,30 @@ DEF_TEST(SkSLFPTernaryExpressionsShouldNotInlineResults, r) {
11141111
)__SkSL__",
11151112
/*expectedH=*/{},
11161113
/*expectedCPP=*/{
1117-
R"__Cpp__(fragBuilder->codeAppendf(
1114+
R"__Cpp__(SkString trueSide_name;
1115+
const GrShaderVar trueSide_args[] = { GrShaderVar("v", kHalf4_GrSLType)};
1116+
fragBuilder->emitFunction(kHalf4_GrSLType, "trueSide", 1, trueSide_args,
1117+
R"SkSL(count += 1.0;
1118+
return half4(sin(v.x), sin(v.y), sin(v.z), sin(v.w));
1119+
)SkSL", &trueSide_name);
1120+
SkString falseSide_name;
1121+
const GrShaderVar falseSide_args[] = { GrShaderVar("v", kHalf4_GrSLType)};
1122+
fragBuilder->emitFunction(kHalf4_GrSLType, "falseSide", 1, falseSide_args,
1123+
R"SkSL(count += 1.0;
1124+
return half4(cos(v.y), cos(v.z), cos(v.w), cos(v.z));
1125+
)SkSL", &falseSide_name);
1126+
fragBuilder->codeAppendf(
11181127
R"SkSL(half count = %f;
11191128
bool _0_test;
11201129
{
11211130
_0_test = %s.x <= 0.5;
11221131
}
11231132
1124-
half4 _1_trueSide;
1125-
{
1126-
count += 1.0;
1127-
_1_trueSide = half4(sin(%s.x), sin(%s.y), sin(%s.z), sin(%s.w));
1128-
}
1129-
1130-
half4 _2_falseSide;
1131-
{
1132-
count += 1.0;
1133-
_2_falseSide = half4(cos(%s.y), cos(%s.z), cos(%s.w), cos(%s.z));
1134-
}
1135-
1136-
%s = _0_test ? _1_trueSide : _2_falseSide;
1133+
%s = _0_test ? %s(%s) : %s(%s);
11371134
11381135
%s *= count;
11391136
)SkSL"
1140-
, count, args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fOutputColor, args.fOutputColor);
1137+
, count, args.fUniformHandler->getUniformCStr(colorVar), args.fOutputColor, trueSide_name.c_str(), args.fUniformHandler->getUniformCStr(colorVar), falseSide_name.c_str(), args.fUniformHandler->getUniformCStr(colorVar), args.fOutputColor);
11411138
)__Cpp__"});
11421139
}
11431140

0 commit comments

Comments
 (0)