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

Commit 39616ec

Browse files
johnstiles-googleSkia Commit-Bot
authored andcommitted
The inlined function body is now returned as a single Statement.
If only a single statement is necessary, it is returned as-is. If multiple statements are needed, they are wrapped in a non-scoped Block statement. If no statements at all are needed (!), null will be returned. Change-Id: I6ba373f73d339b8c2e7b8d39dfb28f13655e3d03 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/313911 Commit-Queue: John Stiles <johnstiles@google.com> Reviewed-by: Ethan Nicholas <ethannicholas@google.com> Reviewed-by: Brian Osman <brianosman@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
1 parent 44e96be commit 39616ec

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

src/gpu/effects/generated/GrColorMatrixFragmentProcessor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class GrGLSLColorMatrixFragmentProcessor : public GrGLSLFragmentProcessor {
4747
{
4848
_inlineResulthalf4unpremulhalf40 = half4(inputColor.xyz / max(inputColor.w, 9.9999997473787516e-05), inputColor.w);
4949
}
50+
5051
inputColor = _inlineResulthalf4unpremulhalf40;
5152
5253
}

src/gpu/effects/generated/GrHighContrastFilterEffect.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ half4 _inlineResulthalf4unpremulhalf40;
5656
{
5757
_inlineResulthalf4unpremulhalf40 = half4(inColor.xyz / max(inColor.w, 9.9999997473787516e-05), inColor.w);
5858
}
59+
5960
half4 color = _inlineResulthalf4unpremulhalf40;
6061
6162
@if (%s) {

src/sksl/SkSLIRGenerator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,9 +1993,9 @@ std::unique_ptr<Expression> IRGenerator::call(int offset,
19931993
if (fCanInline && fInliner.isSafeToInline(*funcCall, fSettings->fInlineThreshold)) {
19941994
Inliner::InlinedCall inlinedCall = fInliner.inlineCall(std::move(funcCall),
19951995
fSymbolTable.get());
1996-
fExtraStatements.insert(fExtraStatements.end(),
1997-
std::make_move_iterator(inlinedCall.fInlinedBody.begin()),
1998-
std::make_move_iterator(inlinedCall.fInlinedBody.end()));
1996+
if (inlinedCall.fInlinedBody) {
1997+
fExtraStatements.push_back(std::move(inlinedCall.fInlinedBody));
1998+
}
19991999
return std::move(inlinedCall.fReplacementExpr);
20002000
}
20012001

src/sksl/SkSLInliner.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ Inliner::InlinedCall Inliner::inlineCall(std::unique_ptr<FunctionCall> call,
434434
std::vector<std::unique_ptr<Expression>>& arguments = call->fArguments;
435435
const FunctionDefinition& function = *call->fFunction.fDefinition;
436436
InlinedCall inlinedCall;
437+
std::vector<std::unique_ptr<Statement>> inlinedBody;
437438

438439
// Use unique variable names based on the function signature. Otherwise there are situations in
439440
// which an inlined function is later inlined into another function, and we end up with
@@ -474,7 +475,7 @@ Inliner::InlinedCall Inliner::inlineCall(std::unique_ptr<FunctionCall> call,
474475
}
475476

476477
// Add the new variable-declaration statement to our block of extra statements.
477-
inlinedCall.fInlinedBody.push_back(std::make_unique<VarDeclarationsStatement>(
478+
inlinedBody.push_back(std::make_unique<VarDeclarationsStatement>(
478479
std::make_unique<VarDeclarations>(offset, &type, std::move(variables))));
479480

480481
return variableSymbol;
@@ -525,14 +526,14 @@ Inliner::InlinedCall Inliner::inlineCall(std::unique_ptr<FunctionCall> call,
525526
// used to perform an early return), we fake it by wrapping the function in a
526527
// do { } while (false); and then use break statements to jump to the end in order to
527528
// emulate a goto.
528-
inlinedCall.fInlinedBody.push_back(std::make_unique<DoStatement>(
529+
inlinedBody.push_back(std::make_unique<DoStatement>(
529530
/*offset=*/-1,
530531
std::move(inlineBlock),
531532
std::make_unique<BoolLiteral>(*fContext, offset, /*value=*/false)));
532533
} else {
533534
// No early returns, so we can just dump the code in. We need to use a block so we don't get
534535
// name conflicts with locals.
535-
inlinedCall.fInlinedBody.push_back(std::move(inlineBlock));
536+
inlinedBody.push_back(std::move(inlineBlock));
536537
}
537538

538539
// Copy the values of `out` parameters into their destinations.
@@ -547,7 +548,7 @@ Inliner::InlinedCall Inliner::inlineCall(std::unique_ptr<FunctionCall> call,
547548
continue;
548549
}
549550
auto varRef = std::make_unique<VariableReference>(offset, *varMap[p]);
550-
inlinedCall.fInlinedBody.push_back(std::make_unique<ExpressionStatement>(
551+
inlinedBody.push_back(std::make_unique<ExpressionStatement>(
551552
std::make_unique<BinaryExpression>(offset,
552553
arguments[i]->clone(),
553554
Token::Kind::TK_EQ,
@@ -566,6 +567,19 @@ Inliner::InlinedCall Inliner::inlineCall(std::unique_ptr<FunctionCall> call,
566567
/*value=*/false);
567568
}
568569

570+
switch (inlinedBody.size()) {
571+
case 0:
572+
break;
573+
case 1:
574+
inlinedCall.fInlinedBody = std::move(inlinedBody.front());
575+
break;
576+
default:
577+
inlinedCall.fInlinedBody = std::make_unique<Block>(offset, std::move(inlinedBody),
578+
/*symbols=*/nullptr,
579+
/*isScope=*/false);
580+
break;
581+
}
582+
569583
return inlinedCall;
570584
}
571585

src/sksl/SkSLInliner.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ class Inliner {
3636

3737
/**
3838
* Processes the passed-in FunctionCall expression. The FunctionCall expression should be
39-
* replaced with `fReplacementExpr`. Any statements in `fInlinedBody` should be inserted
40-
* immediately above the statement containing the inlined expression.
39+
* replaced with `fReplacementExpr`. If non-null, `fInlinedBody` should be inserted immediately
40+
* above the statement containing the inlined expression.
4141
*/
4242
struct InlinedCall {
43-
std::vector<std::unique_ptr<Statement>> fInlinedBody;
43+
std::unique_ptr<Statement> fInlinedBody;
4444
std::unique_ptr<Expression> fReplacementExpr;
4545
};
4646
InlinedCall inlineCall(std::unique_ptr<FunctionCall>, SymbolTable*);

tests/SkSLFPTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ R"SkSL(half4 _inlineResulthalf4fliphalf40;
10491049
{
10501050
_inlineResulthalf4fliphalf40 = %s.wzyx;
10511051
}
1052+
10521053
%s = _inlineResulthalf4fliphalf40;
10531054
10541055
)SkSL"
@@ -1150,6 +1151,7 @@ R"SkSL(half4 _inlineResulthalf4switchyhalf40;
11501151
result = %s.zzzz;
11511152
_inlineResulthalf4switchyhalf40 = result;
11521153
}
1154+
11531155
%s = _inlineResulthalf4switchyhalf40;
11541156
11551157
)SkSL"
@@ -1221,6 +1223,7 @@ R"SkSL(half4 _inlineResulthalf4switchyhalf40;
12211223
}
12221224
_inlineResulthalf4switchyhalf40 = result;
12231225
}
1226+
12241227
%s = _inlineResulthalf4switchyhalf40;
12251228
12261229
)SkSL"
@@ -1257,6 +1260,7 @@ R"SkSL(half4 _inlineResulthalf4loopyhalf40;
12571260
pix = %s.zzzz;
12581261
_inlineResulthalf4loopyhalf40 = pix;
12591262
}
1263+
12601264
%s = _inlineResulthalf4loopyhalf40;
12611265
12621266
)SkSL"
@@ -1284,6 +1288,7 @@ R"SkSL(half4 _inlineResulthalf4branchyhalf40;
12841288
{
12851289
if (%s.z == %s.w) _inlineResulthalf4branchyhalf40 = %s.yyyy; else _inlineResulthalf4branchyhalf40 = %s.zzzz;
12861290
}
1291+
12871292
%s = _inlineResulthalf4branchyhalf40;
12881293
12891294
)SkSL"
@@ -1315,6 +1320,7 @@ R"SkSL(half4 _inlineResulthalf4blockyhalf40;
13151320
_inlineResulthalf4blockyhalf40 = %s;
13161321
}
13171322
}
1323+
13181324
%s = _inlineResulthalf4blockyhalf40;
13191325
13201326
)SkSL"
@@ -1354,6 +1360,7 @@ do {
13541360
break;
13551361
}
13561362
} while (false);
1363+
13571364
%s = _inlineResulthalf4returnyhalf40;
13581365
13591366
)SkSL"
@@ -1404,6 +1411,7 @@ half4 _inlineArghalf4branchyhalf41_0 = %s;
14041411
_inlineArghalf4branchyhalf41_0 *= 0.5;
14051412
if (_inlineArghalf4branchyhalf41_0.x > 0.0) _inlineResulthalf4branchyhalf40 = _inlineArghalf4branchyhalf41_0.xxxx; else if (_inlineArghalf4branchyhalf41_0.y > 0.0) _inlineResulthalf4branchyhalf40 = _inlineArghalf4branchyhalf41_0.yyyy; else if (_inlineArghalf4branchyhalf41_0.z > 0.0) _inlineResulthalf4branchyhalf40 = _inlineArghalf4branchyhalf41_0.zzzz; else _inlineResulthalf4branchyhalf40 = _inlineArghalf4branchyhalf41_0.wwww;
14061413
}
1414+
14071415
half4 _inlineResulthalf4branchyAndBlockyhalf42;
14081416
{
14091417
{
@@ -1425,6 +1433,7 @@ half4 _inlineResulthalf4branchyAndBlockyhalf42;
14251433
}
14261434
}
14271435
}
1436+
14281437
%s = _inlineResulthalf4branchyhalf40 * _inlineResulthalf4branchyAndBlockyhalf42;
14291438
14301439
)SkSL"

tests/SkSLGLSLTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ DEF_TEST(SkSLFunctions, r) {
114114
" {\n"
115115
" _inlineResultfloatfoofloat20 = y[0] * y[1];\n"
116116
" }\n"
117+
"\n"
117118
" z = _inlineResultfloatfoofloat20;\n"
118119
"\n"
119120
" x = z;\n"
@@ -274,6 +275,7 @@ DEF_TEST(SkSLFunctionInlineArguments, r) {
274275
" _inlineArghalfparameterWritehalf1_0 *= 2.0;\n"
275276
" _inlineResulthalfparameterWritehalf0 = _inlineArghalfparameterWritehalf1_0;\n"
276277
" }\n"
278+
"\n"
277279
" sk_FragColor.x = _inlineResulthalfparameterWritehalf0;\n"
278280
"\n"
279281
"}\n");
@@ -329,6 +331,7 @@ DEF_TEST(SkSLFunctionInlineArguments, r) {
329331
" foo(_inlineArghalfbarhalf1_0);\n"
330332
" }\n"
331333
"\n"
334+
"\n"
332335
" sk_FragColor.x = 0.0;\n"
333336
"}\n");
334337
}

0 commit comments

Comments
 (0)