Skip to content

Commit 7bd6043

Browse files
Ethan NicholasSkia Commit-Bot
authored andcommitted
moved SkSL Block's data into IRNode
Change-Id: Id1066c7c2aeffce47fcd6823cb01a58b52e450c2 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/319676 Commit-Queue: Ethan Nicholas <ethannicholas@google.com> Reviewed-by: John Stiles <johnstiles@google.com>
1 parent aeae3a5 commit 7bd6043

19 files changed

+230
-121
lines changed

src/sksl/SkSLAnalysis.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,10 @@ bool ProgramVisitor::visitStatement(const Statement& s) {
315315
// Leaf statements just return false
316316
return false;
317317
case Statement::Kind::kBlock:
318-
for (const std::unique_ptr<Statement>& blockStmt : s.as<Block>().fStatements) {
319-
if (this->visitStatement(*blockStmt)) { return true; }
318+
for (const std::unique_ptr<Statement>& stmt : s.as<Block>().children()) {
319+
if (this->visitStatement(*stmt)) {
320+
return true;
321+
}
320322
}
321323
return false;
322324
case Statement::Kind::kDo: {

src/sksl/SkSLByteCodeGenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,8 +1643,8 @@ std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Ex
16431643
}
16441644

16451645
void ByteCodeGenerator::writeBlock(const Block& b) {
1646-
for (const auto& s : b.fStatements) {
1647-
this->writeStatement(*s);
1646+
for (const std::unique_ptr<Statement>& stmt : b.children()) {
1647+
this->writeStatement(*stmt);
16481648
}
16491649
}
16501650

src/sksl/SkSLCFGGenerator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,13 @@ static bool is_true(Expression& expr) {
484484

485485
void CFGGenerator::addStatement(CFG& cfg, std::unique_ptr<Statement>* s) {
486486
switch ((*s)->kind()) {
487-
case Statement::Kind::kBlock:
488-
for (auto& child : (*s)->as<Block>().fStatements) {
487+
case Statement::Kind::kBlock: {
488+
Block& block = (*s)->as<Block>();
489+
for (std::unique_ptr<Statement>& child : block.children()) {
489490
addStatement(cfg, &child);
490491
}
491492
break;
493+
}
492494
case Statement::Kind::kIf: {
493495
IfStatement& ifs = (*s)->as<IfStatement>();
494496
this->addExpression(cfg, &ifs.fTest, /*constantPropagate=*/true);

src/sksl/SkSLCPPCodeGenerator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
605605
fOut = &buffer;
606606
if (decl.fName == "main") {
607607
fInMain = true;
608-
for (const auto& s : f.fBody->as<Block>().fStatements) {
608+
for (const std::unique_ptr<Statement>& s : f.fBody->as<Block>().children()) {
609609
this->writeStatement(*s);
610610
this->writeLine();
611611
}
@@ -618,14 +618,14 @@ void CPPCodeGenerator::writeFunction(const FunctionDefinition& f) {
618618
this->addExtraEmitCodeLine("SkString " + decl.fName + "_name;");
619619
String args = "const GrShaderVar " + decl.fName + "_args[] = { ";
620620
const char* separator = "";
621-
for (const auto& param : decl.fParameters) {
621+
for (const Variable* param : decl.fParameters) {
622622
args += String(separator) + "GrShaderVar(\"" + param->fName + "\", " +
623623
glsltype_string(fContext, param->type()) + ")";
624624
separator = ", ";
625625
}
626626
args += "};";
627627
this->addExtraEmitCodeLine(args.c_str());
628-
for (const auto& s : f.fBody->as<Block>().fStatements) {
628+
for (const std::unique_ptr<Statement>& s : f.fBody->as<Block>().children()) {
629629
this->writeStatement(*s);
630630
this->writeLine();
631631
}

src/sksl/SkSLCompiler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,13 +1169,13 @@ static void move_all_but_break(std::unique_ptr<Statement>& stmt,
11691169
Block& block = static_cast<Block&>(*stmt);
11701170

11711171
std::vector<std::unique_ptr<Statement>> blockStmts;
1172-
blockStmts.reserve(block.fStatements.size());
1173-
for (std::unique_ptr<Statement>& statementInBlock : block.fStatements) {
1174-
move_all_but_break(statementInBlock, &blockStmts);
1172+
blockStmts.reserve(block.children().size());
1173+
for (std::unique_ptr<Statement>& stmt : block.children()) {
1174+
move_all_but_break(stmt, &blockStmts);
11751175
}
11761176

11771177
target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
1178-
block.fSymbols, block.fIsScope));
1178+
block.symbolTable(), block.isScope()));
11791179
break;
11801180
}
11811181

src/sksl/SkSLDehydrator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,12 @@ void Dehydrator::write(const Statement* s) {
393393
case Statement::Kind::kBlock: {
394394
const Block& b = s->as<Block>();
395395
this->writeU8(Rehydrator::kBlock_Command);
396-
AutoDehydratorSymbolTable symbols(this, b.fSymbols);
397-
this->writeU8(b.fStatements.size());
398-
for (const std::unique_ptr<Statement>& blockStmt : b.fStatements) {
396+
AutoDehydratorSymbolTable symbols(this, b.symbolTable());
397+
this->writeU8(b.children().size());
398+
for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
399399
this->write(blockStmt.get());
400400
}
401-
this->writeU8(b.fIsScope);
401+
this->writeU8(b.isScope());
402402
break;
403403
}
404404
case Statement::Kind::kBreak:

src/sksl/SkSLGLSLCodeGenerator.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,12 @@ void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) {
10811081
OutputStream* oldOut = fOut;
10821082
StringStream buffer;
10831083
fOut = &buffer;
1084-
this->writeStatements(f.fBody->as<Block>().fStatements);
1084+
for (const std::unique_ptr<Statement>& stmt : f.fBody->as<Block>().children()) {
1085+
if (!stmt->isEmpty()) {
1086+
this->writeStatement(*stmt);
1087+
this->writeLine();
1088+
}
1089+
}
10851090

10861091
fIndentation--;
10871092
this->writeLine("}");
@@ -1343,22 +1348,19 @@ void GLSLCodeGenerator::writeStatement(const Statement& s) {
13431348
}
13441349
}
13451350

1346-
void GLSLCodeGenerator::writeStatements(const std::vector<std::unique_ptr<Statement>>& statements) {
1347-
for (const auto& s : statements) {
1348-
if (!s->isEmpty()) {
1349-
this->writeStatement(*s);
1350-
this->writeLine();
1351-
}
1352-
}
1353-
}
1354-
13551351
void GLSLCodeGenerator::writeBlock(const Block& b) {
1356-
if (b.fIsScope) {
1352+
bool isScope = b.isScope();
1353+
if (isScope) {
13571354
this->writeLine("{");
13581355
fIndentation++;
13591356
}
1360-
this->writeStatements(b.fStatements);
1361-
if (b.fIsScope) {
1357+
for (const std::unique_ptr<Statement>& stmt : b.children()) {
1358+
if (!stmt->isEmpty()) {
1359+
this->writeStatement(*stmt);
1360+
this->writeLine();
1361+
}
1362+
}
1363+
if (isScope) {
13621364
fIndentation--;
13631365
this->write("}");
13641366
}

src/sksl/SkSLGLSLCodeGenerator.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ class GLSLCodeGenerator : public CodeGenerator {
179179

180180
void writeStatement(const Statement& s);
181181

182-
void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
183-
184182
void writeBlock(const Block& b);
185183

186184
virtual void writeIfStatement(const IfStatement& stmt);

src/sksl/SkSLIRGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ void IRGenerator::convertFunction(const ASTNode& f) {
10901090
body = this->applyInvocationIDWorkaround(std::move(body));
10911091
}
10921092
if (Program::kVertex_Kind == fKind && funcData.fName == "main" && fRTAdjust) {
1093-
body->fStatements.insert(body->fStatements.end(), this->getNormalizeSkPositionCode());
1093+
body->children().push_back(this->getNormalizeSkPositionCode());
10941094
}
10951095
auto result = std::make_unique<FunctionDefinition>(f.fOffset, *decl, std::move(body),
10961096
std::move(fReferencedIntrinsics));

src/sksl/SkSLInliner.cpp

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ static int count_returns_at_end_of_control_flow(const FunctionDefinition& funcDe
9494
switch (stmt.kind()) {
9595
case Statement::Kind::kBlock: {
9696
// Check only the last statement of a block.
97-
const auto& blockStmts = stmt.as<Block>().fStatements;
98-
return (blockStmts.size() > 0) ? this->visitStatement(*blockStmts.back())
99-
: false;
97+
const auto& block = stmt.as<Block>();
98+
return block.children().size() &&
99+
this->visitStatement(*block.children().back());
100100
}
101101
case Statement::Kind::kSwitch:
102102
case Statement::Kind::kWhile:
@@ -210,7 +210,7 @@ static Statement* find_parent_statement(const std::vector<std::unique_ptr<Statem
210210
// Anything counts as a parent statement other than a scopeless Block.
211211
for (; iter != stmtStack.rend(); ++iter) {
212212
Statement* stmt = (*iter)->get();
213-
if (!stmt->is<Block>() || stmt->as<Block>().fIsScope) {
213+
if (!stmt->is<Block>() || stmt->as<Block>().isScope()) {
214214
return stmt;
215215
}
216216
}
@@ -243,23 +243,23 @@ void Inliner::ensureScopedBlocks(Statement* inlinedBody, Statement* parentStmt)
243243
// issues--if we don't represent the Block textually somehow, we run the risk of accidentally
244244
// absorbing the following statement into our loop--so we also add a scope to these.
245245
for (Block* nestedBlock = &block;; ) {
246-
if (nestedBlock->fIsScope) {
246+
if (nestedBlock->isScope()) {
247247
// We found an explicit scope; all is well.
248248
return;
249249
}
250-
if (nestedBlock->fStatements.size() != 1) {
250+
if (nestedBlock->children().size() != 1) {
251251
// We found a block with multiple (or zero) statements, but no scope? Let's add a scope
252252
// to the outermost block.
253-
block.fIsScope = true;
253+
block.setIsScope(true);
254254
return;
255255
}
256-
if (!nestedBlock->fStatements[0]->is<Block>()) {
256+
if (!nestedBlock->children()[0]->is<Block>()) {
257257
// This block has exactly one thing inside, and it's not another block. No need to scope
258258
// it.
259259
return;
260260
}
261261
// We have to go deeper.
262-
nestedBlock = &nestedBlock->fStatements[0]->as<Block>();
262+
nestedBlock = &nestedBlock->children()[0]->as<Block>();
263263
}
264264
}
265265

@@ -400,6 +400,13 @@ std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
400400
}
401401
return nullptr;
402402
};
403+
auto blockStmts = [&](const Block& block) {
404+
std::vector<std::unique_ptr<Statement>> result;
405+
for (const std::unique_ptr<Statement>& child : block.children()) {
406+
result.push_back(stmt(child));
407+
}
408+
return result;
409+
};
403410
auto stmts = [&](const std::vector<std::unique_ptr<Statement>>& ss) {
404411
std::vector<std::unique_ptr<Statement>> result;
405412
for (const auto& s : ss) {
@@ -416,7 +423,7 @@ std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
416423
switch (statement.kind()) {
417424
case Statement::Kind::kBlock: {
418425
const Block& b = statement.as<Block>();
419-
return std::make_unique<Block>(offset, stmts(b.fStatements), b.fSymbols, b.fIsScope);
426+
return std::make_unique<Block>(offset, blockStmts(b), b.symbolTable(), b.isScope());
420427
}
421428

422429
case Statement::Kind::kBreak:
@@ -558,14 +565,16 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
558565
/*symbols=*/nullptr,
559566
/*isScope=*/false);
560567

561-
std::vector<std::unique_ptr<Statement>>& inlinedBody = inlinedCall.fInlinedBody->fStatements;
562-
inlinedBody.reserve(1 + // Inline marker
563-
1 + // Result variable
564-
arguments.size() + // Function arguments (passing in)
565-
arguments.size() + // Function arguments (copy out-parameters back)
566-
1); // Inlined code (either as a Block or do-while loop)
568+
Block& inlinedBody = *inlinedCall.fInlinedBody;
569+
inlinedBody.children().reserve(1 + // Inline marker
570+
1 + // Result variable
571+
arguments.size() + // Function arguments (passing in)
572+
arguments.size() + // Function arguments (copy out-parameters
573+
// back)
574+
1); // Inlined code (either as a Block or do-while
575+
// loop)
567576

568-
inlinedBody.push_back(std::make_unique<InlineMarker>(call->fFunction));
577+
inlinedBody.children().push_back(std::make_unique<InlineMarker>(call->fFunction));
569578

570579
auto makeInlineVar = [&](const String& baseName, const Type* type, Modifiers modifiers,
571580
std::unique_ptr<Expression>* initialValue) -> const Variable* {
@@ -605,7 +614,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
605614
}
606615

607616
// Add the new variable-declaration statement to our block of extra statements.
608-
inlinedBody.push_back(std::make_unique<VarDeclarationsStatement>(
617+
inlinedBody.children().push_back(std::make_unique<VarDeclarationsStatement>(
609618
std::make_unique<VarDeclarations>(offset, type, std::move(variables))));
610619

611620
return variableSymbol;
@@ -643,24 +652,24 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
643652

644653
const Block& body = function.fBody->as<Block>();
645654
auto inlineBlock = std::make_unique<Block>(offset, std::vector<std::unique_ptr<Statement>>{});
646-
inlineBlock->fStatements.reserve(body.fStatements.size());
647-
for (const std::unique_ptr<Statement>& stmt : body.fStatements) {
648-
inlineBlock->fStatements.push_back(this->inlineStatement(
655+
inlineBlock->children().reserve(body.children().size());
656+
for (const std::unique_ptr<Statement>& stmt : body.children()) {
657+
inlineBlock->children().push_back(this->inlineStatement(
649658
offset, &varMap, symbolTableForCall, resultExpr, hasEarlyReturn, *stmt));
650659
}
651660
if (hasEarlyReturn) {
652661
// Since we output to backends that don't have a goto statement (which would normally be
653662
// used to perform an early return), we fake it by wrapping the function in a
654663
// do { } while (false); and then use break statements to jump to the end in order to
655664
// emulate a goto.
656-
inlinedBody.push_back(std::make_unique<DoStatement>(
665+
inlinedBody.children().push_back(std::make_unique<DoStatement>(
657666
/*offset=*/-1,
658667
std::move(inlineBlock),
659668
std::make_unique<BoolLiteral>(*fContext, offset, /*value=*/false)));
660669
} else {
661670
// No early returns, so we can just dump the code in. We still need to keep the block so we
662671
// don't get name conflicts with locals.
663-
inlinedBody.push_back(std::move(inlineBlock));
672+
inlinedBody.children().push_back(std::move(inlineBlock));
664673
}
665674

666675
// Copy the values of `out` parameters into their destinations.
@@ -675,7 +684,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
675684
continue;
676685
}
677686
auto varRef = std::make_unique<VariableReference>(offset, varMap[p]);
678-
inlinedBody.push_back(std::make_unique<ExpressionStatement>(
687+
inlinedBody.children().push_back(std::make_unique<ExpressionStatement>(
679688
std::make_unique<BinaryExpression>(offset,
680689
arguments[i]->clone(),
681690
Token::Kind::TK_EQ,
@@ -805,12 +814,12 @@ bool Inliner::analyze(Program& program) {
805814

806815
case Statement::Kind::kBlock: {
807816
Block& block = (*stmt)->as<Block>();
808-
if (block.fSymbols) {
809-
fSymbolTableStack.push_back(block.fSymbols.get());
817+
if (block.symbolTable()) {
818+
fSymbolTableStack.push_back(block.symbolTable().get());
810819
}
811820

812-
for (std::unique_ptr<Statement>& blockStmt : block.fStatements) {
813-
this->visitStatement(&blockStmt);
821+
for (std::unique_ptr<Statement>& stmt : block.children()) {
822+
this->visitStatement(&stmt);
814823
}
815824
break;
816825
}
@@ -1082,7 +1091,7 @@ bool Inliner::analyze(Program& program) {
10821091
// After:
10831092
// fInlinedBody = null
10841093
// fEnclosingStmt = Block{ stmt1, stmt2, stmt3, stmt4 }
1085-
inlinedCall.fInlinedBody->fStatements.push_back(std::move(*candidate.fEnclosingStmt));
1094+
inlinedCall.fInlinedBody->children().push_back(std::move(*candidate.fEnclosingStmt));
10861095
*candidate.fEnclosingStmt = std::move(inlinedCall.fInlinedBody);
10871096
}
10881097

0 commit comments

Comments
 (0)