Skip to content

Commit 01b05e5

Browse files
Ethan NicholasSkia Commit-Bot
authored andcommitted
moved SkSL Switch data into IRNode
Change-Id: I0373cccfd3acc56417f8d1545bbe7320dc2dfa05 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/327256 Commit-Queue: Ethan Nicholas <ethannicholas@google.com> Reviewed-by: John Stiles <johnstiles@google.com>
1 parent 060503e commit 01b05e5

16 files changed

+204
-109
lines changed

src/sksl/SkSLAnalysis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,14 @@ bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitStatement(STMT s) {
482482
}
483483
case Statement::Kind::kSwitch: {
484484
auto& sw = s.template as<SwitchStatement>();
485-
if (this->visitExpression(*sw.fValue)) {
485+
if (this->visitExpression(*sw.value())) {
486486
return true;
487487
}
488-
for (auto& c : sw.fCases) {
489-
if (c->fValue && this->visitExpression(*c->fValue)) {
488+
for (const auto& c : sw.cases()) {
489+
if (c.value() && this->visitExpression(*c.value())) {
490490
return true;
491491
}
492-
for (auto& st : c->fStatements) {
492+
for (auto& st : c.statements()) {
493493
if (this->visitStatement(*st)) {
494494
return true;
495495
}

src/sksl/SkSLCFGGenerator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,26 +612,26 @@ void CFGGenerator::addStatement(CFG& cfg, std::unique_ptr<Statement>* s) {
612612
}
613613
case Statement::Kind::kSwitch: {
614614
SwitchStatement& ss = (*s)->as<SwitchStatement>();
615-
this->addExpression(cfg, &ss.fValue, /*constantPropagate=*/true);
615+
this->addExpression(cfg, &ss.value(), /*constantPropagate=*/true);
616616
cfg.currentBlock().fNodes.push_back(BasicBlock::MakeStatement(s));
617617
BlockId start = cfg.fCurrent;
618618
BlockId switchExit = cfg.newIsolatedBlock();
619619
fLoopExits.push(switchExit);
620-
for (const auto& c : ss.fCases) {
620+
for (auto& c : ss.cases()) {
621621
cfg.newBlock();
622622
cfg.addExit(start, cfg.fCurrent);
623-
if (c->fValue) {
623+
if (c.value()) {
624624
// technically this should go in the start block, but it doesn't actually matter
625625
// because it must be constant. Not worth running two loops for.
626-
this->addExpression(cfg, &c->fValue, /*constantPropagate=*/true);
626+
this->addExpression(cfg, &c.value(), /*constantPropagate=*/true);
627627
}
628-
for (auto& caseStatement : c->fStatements) {
628+
for (auto& caseStatement : c.statements()) {
629629
this->addStatement(cfg, &caseStatement);
630630
}
631631
}
632632
cfg.addExit(cfg.fCurrent, switchExit);
633633
// note that unlike GLSL, our grammar requires the default case to be last
634-
if (ss.fCases.empty() || ss.fCases.back()->fValue) {
634+
if (ss.cases().empty() || ss.cases().back().value()) {
635635
// switch does not have a default clause, mark that it can skip straight to the end
636636
cfg.addExit(start, switchExit);
637637
}

src/sksl/SkSLCPPCodeGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ void CPPCodeGenerator::writeReturnStatement(const ReturnStatement& s) {
380380
}
381381

382382
void CPPCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
383-
if (s.fIsStatic) {
383+
if (s.isStatic()) {
384384
this->write("@");
385385
}
386386
INHERITED::writeSwitchStatement(s);

src/sksl/SkSLCompiler.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,9 @@ static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatemen
12051205
// We have to be careful to not move any of the pointers until after we're sure we're going to
12061206
// succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
12071207
// of action. First, find the switch-case we are interested in.
1208-
auto iter = switchStatement->fCases.begin();
1209-
for (; iter != switchStatement->fCases.end(); ++iter) {
1210-
if (iter->get() == caseToCapture) {
1208+
auto iter = switchStatement->cases().begin();
1209+
for (; iter != switchStatement->cases().end(); ++iter) {
1210+
if (&*iter == caseToCapture) {
12111211
break;
12121212
}
12131213
}
@@ -1217,8 +1217,8 @@ static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatemen
12171217
// statements that we can use for simplification.
12181218
auto startIter = iter;
12191219
Statement* unconditionalBreakStmt = nullptr;
1220-
for (; iter != switchStatement->fCases.end(); ++iter) {
1221-
for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1220+
for (; iter != switchStatement->cases().end(); ++iter) {
1221+
for (std::unique_ptr<Statement>& stmt : iter->statements()) {
12221222
if (contains_conditional_break(*stmt)) {
12231223
// We can't reduce switch-cases to a block when they have conditional breaks.
12241224
return nullptr;
@@ -1243,7 +1243,7 @@ static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatemen
12431243

12441244
// We can move over most of the statements as-is.
12451245
while (startIter != iter) {
1246-
for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1246+
for (std::unique_ptr<Statement>& stmt : startIter->statements()) {
12471247
caseStmts.push_back(std::move(stmt));
12481248
}
12491249
++startIter;
@@ -1252,7 +1252,7 @@ static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatemen
12521252
// If we found an unconditional break at the end, we need to move what we can while avoiding
12531253
// that break.
12541254
if (unconditionalBreakStmt != nullptr) {
1255-
for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1255+
for (std::unique_ptr<Statement>& stmt : startIter->statements()) {
12561256
if (stmt.get() == unconditionalBreakStmt) {
12571257
move_all_but_break(stmt, &caseStmts);
12581258
unconditionalBreakStmt = nullptr;
@@ -1266,7 +1266,7 @@ static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatemen
12661266
SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
12671267

12681268
// Return our newly-synthesized block.
1269-
return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
1269+
return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->symbols());
12701270
}
12711271

12721272
void Compiler::simplifyStatement(DefinitionMap& definitions,
@@ -1334,28 +1334,30 @@ void Compiler::simplifyStatement(DefinitionMap& definitions,
13341334
case Statement::Kind::kSwitch: {
13351335
SwitchStatement& s = stmt->as<SwitchStatement>();
13361336
int64_t switchValue;
1337-
if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) {
1337+
if (fIRGenerator->getConstantInt(*s.value(), &switchValue)) {
13381338
// switch is constant, replace it with the case that matches
13391339
bool found = false;
13401340
SwitchCase* defaultCase = nullptr;
1341-
for (const std::unique_ptr<SwitchCase>& c : s.fCases) {
1342-
if (!c->fValue) {
1343-
defaultCase = c.get();
1341+
for (SwitchCase& c : s.cases()) {
1342+
if (!c.value()) {
1343+
defaultCase = &c;
13441344
continue;
13451345
}
13461346
int64_t caseValue;
1347-
SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue));
1347+
SkAssertResult(fIRGenerator->getConstantInt(*c.value(), &caseValue));
13481348
if (caseValue == switchValue) {
1349-
std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1349+
std::unique_ptr<Statement> newBlock = block_for_case(&s, &c);
13501350
if (newBlock) {
13511351
(*iter)->setStatement(std::move(newBlock));
13521352
found = true;
13531353
break;
13541354
} else {
1355-
if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
1355+
if (s.isStatic() && !(fFlags & kPermitInvalidStaticTests_Flag) &&
1356+
optimizationContext->fSilences.find(&s) ==
1357+
optimizationContext->fSilences.end()) {
13561358
this->error(s.fOffset,
13571359
"static switch contains non-static conditional break");
1358-
s.fIsStatic = false;
1360+
optimizationContext->fSilences.insert(&s);
13591361
}
13601362
return; // can't simplify
13611363
}
@@ -1368,10 +1370,12 @@ void Compiler::simplifyStatement(DefinitionMap& definitions,
13681370
if (newBlock) {
13691371
(*iter)->setStatement(std::move(newBlock));
13701372
} else {
1371-
if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
1373+
if (s.isStatic() && !(fFlags & kPermitInvalidStaticTests_Flag) &&
1374+
optimizationContext->fSilences.find(&s) ==
1375+
optimizationContext->fSilences.end()) {
13721376
this->error(s.fOffset,
13731377
"static switch contains non-static conditional break");
1374-
s.fIsStatic = false;
1378+
optimizationContext->fSilences.insert(&s);
13751379
}
13761380
return; // can't simplify
13771381
}
@@ -1498,8 +1502,10 @@ bool Compiler::scanCFG(FunctionDefinition& f) {
14981502
++iter;
14991503
break;
15001504
case Statement::Kind::kSwitch:
1501-
if (s.as<SwitchStatement>().fIsStatic &&
1502-
!(fFlags & kPermitInvalidStaticTests_Flag)) {
1505+
if (s.as<SwitchStatement>().isStatic() &&
1506+
!(fFlags & kPermitInvalidStaticTests_Flag) &&
1507+
optimizationContext.fSilences.find(&s) ==
1508+
optimizationContext.fSilences.end()) {
15031509
this->error(s.fOffset, "static switch has non-static test");
15041510
}
15051511
++iter;

src/sksl/SkSLDehydrator.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,14 @@ void Dehydrator::write(const Statement* s) {
466466
case Statement::Kind::kSwitch: {
467467
const SwitchStatement& ss = s->as<SwitchStatement>();
468468
this->writeCommand(Rehydrator::kSwitch_Command);
469-
this->writeU8(ss.fIsStatic);
470-
AutoDehydratorSymbolTable symbols(this, ss.fSymbols);
471-
this->write(ss.fValue.get());
472-
this->writeU8(ss.fCases.size());
473-
for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) {
474-
this->write(sc->fValue.get());
475-
this->writeU8(sc->fStatements.size());
476-
for (const std::unique_ptr<Statement>& stmt : sc->fStatements) {
469+
this->writeU8(ss.isStatic());
470+
AutoDehydratorSymbolTable symbols(this, ss.symbols());
471+
this->write(ss.value().get());
472+
this->writeU8(ss.cases().count());
473+
for (const SwitchCase& sc : ss.cases()) {
474+
this->write(sc.value().get());
475+
this->writeU8(sc.statements().size());
476+
for (const std::unique_ptr<Statement>& stmt : sc.statements()) {
477477
this->write(stmt.get());
478478
}
479479
}

src/sksl/SkSLGLSLCodeGenerator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,19 +1445,19 @@ void GLSLCodeGenerator::writeDoStatement(const DoStatement& d) {
14451445

14461446
void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
14471447
this->write("switch (");
1448-
this->writeExpression(*s.fValue, kTopLevel_Precedence);
1448+
this->writeExpression(*s.value(), kTopLevel_Precedence);
14491449
this->writeLine(") {");
14501450
fIndentation++;
1451-
for (const auto& c : s.fCases) {
1452-
if (c->fValue) {
1451+
for (const SwitchCase& c : s.cases()) {
1452+
if (c.value()) {
14531453
this->write("case ");
1454-
this->writeExpression(*c->fValue, kTopLevel_Precedence);
1454+
this->writeExpression(*c.value(), kTopLevel_Precedence);
14551455
this->writeLine(":");
14561456
} else {
14571457
this->writeLine("default:");
14581458
}
14591459
fIndentation++;
1460-
for (const auto& stmt : c->fStatements) {
1460+
for (const auto& stmt : c.statements()) {
14611461
this->writeStatement(*stmt);
14621462
this->writeLine();
14631463
}

src/sksl/SkSLInliner.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,14 @@ std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
535535
case Statement::Kind::kSwitch: {
536536
const SwitchStatement& ss = statement.as<SwitchStatement>();
537537
std::vector<std::unique_ptr<SwitchCase>> cases;
538-
cases.reserve(ss.fCases.size());
539-
for (const auto& sc : ss.fCases) {
540-
cases.push_back(std::make_unique<SwitchCase>(offset, expr(sc->fValue),
541-
stmts(sc->fStatements)));
538+
cases.reserve(ss.cases().count());
539+
for (const SwitchCase& sc : ss.cases()) {
540+
cases.push_back(std::make_unique<SwitchCase>(offset, expr(sc.value()),
541+
stmts(sc.statements())));
542542
}
543-
return std::make_unique<SwitchStatement>(offset, ss.fIsStatic, expr(ss.fValue),
543+
return std::make_unique<SwitchStatement>(offset, ss.isStatic(), expr(ss.value()),
544544
std::move(cases),
545-
SymbolTable::WrapIfBuiltin(ss.fSymbols));
545+
SymbolTable::WrapIfBuiltin(ss.symbols()));
546546
}
547547
case Statement::Kind::kVarDeclaration: {
548548
const VarDeclaration& decl = statement.as<VarDeclaration>();
@@ -935,14 +935,14 @@ class InlineCandidateAnalyzer {
935935
}
936936
case Statement::Kind::kSwitch: {
937937
SwitchStatement& switchStmt = (*stmt)->as<SwitchStatement>();
938-
if (switchStmt.fSymbols) {
939-
fSymbolTableStack.push_back(switchStmt.fSymbols.get());
938+
if (switchStmt.symbols()) {
939+
fSymbolTableStack.push_back(switchStmt.symbols().get());
940940
}
941941

942-
this->visitExpression(&switchStmt.fValue);
943-
for (std::unique_ptr<SwitchCase>& switchCase : switchStmt.fCases) {
942+
this->visitExpression(&switchStmt.value());
943+
for (SwitchCase& switchCase : switchStmt.cases()) {
944944
// The switch-case's fValue cannot be a FunctionCall; skip it.
945-
for (std::unique_ptr<Statement>& caseBlock : switchCase->fStatements) {
945+
for (std::unique_ptr<Statement>& caseBlock : switchCase.statements()) {
946946
this->visitStatement(&caseBlock);
947947
}
948948
}

src/sksl/SkSLMetalCodeGenerator.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,19 +1365,19 @@ void MetalCodeGenerator::writeDoStatement(const DoStatement& d) {
13651365

13661366
void MetalCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
13671367
this->write("switch (");
1368-
this->writeExpression(*s.fValue, kTopLevel_Precedence);
1368+
this->writeExpression(*s.value(), kTopLevel_Precedence);
13691369
this->writeLine(") {");
13701370
fIndentation++;
1371-
for (const auto& c : s.fCases) {
1372-
if (c->fValue) {
1371+
for (const SwitchCase& c : s.cases()) {
1372+
if (c.value()) {
13731373
this->write("case ");
1374-
this->writeExpression(*c->fValue, kTopLevel_Precedence);
1374+
this->writeExpression(*c.value(), kTopLevel_Precedence);
13751375
this->writeLine(":");
13761376
} else {
13771377
this->writeLine("default:");
13781378
}
13791379
fIndentation++;
1380-
for (const auto& stmt : c->fStatements) {
1380+
for (const auto& stmt : c.statements()) {
13811381
this->writeStatement(*stmt);
13821382
this->writeLine();
13831383
}
@@ -1801,9 +1801,9 @@ MetalCodeGenerator::Requirements MetalCodeGenerator::requirements(const Statemen
18011801
}
18021802
case Statement::Kind::kSwitch: {
18031803
const SwitchStatement& sw = s->as<SwitchStatement>();
1804-
Requirements result = this->requirements(sw.fValue.get());
1805-
for (const auto& c : sw.fCases) {
1806-
for (const auto& st : c->fStatements) {
1804+
Requirements result = this->requirements(sw.value().get());
1805+
for (const SwitchCase& sc : sw.cases()) {
1806+
for (const auto& st : sc.statements()) {
18071807
result |= this->requirements(st.get());
18081808
}
18091809
}

src/sksl/SkSLPipelineStageCodeGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void PipelineStageCodeGenerator::writeIfStatement(const IfStatement& s) {
164164
}
165165

166166
void PipelineStageCodeGenerator::writeSwitchStatement(const SwitchStatement& s) {
167-
if (s.fIsStatic) {
167+
if (s.isStatic()) {
168168
this->write("@");
169169
}
170170
INHERITED::writeSwitchStatement(s);

src/sksl/SkSLSPIRVCodeGenerator.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,16 +3048,17 @@ void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& ou
30483048
}
30493049

30503050
void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) {
3051-
SpvId value = this->writeExpression(*s.fValue, out);
3051+
SpvId value = this->writeExpression(*s.value(), out);
30523052
std::vector<SpvId> labels;
30533053
SpvId end = this->nextId();
30543054
SpvId defaultLabel = end;
30553055
fBreakTarget.push(end);
30563056
int size = 3;
3057-
for (const auto& c : s.fCases) {
3057+
auto cases = s.cases();
3058+
for (const SwitchCase& c : cases) {
30583059
SpvId label = this->nextId();
30593060
labels.push_back(label);
3060-
if (c->fValue) {
3061+
if (c.value()) {
30613062
size += 2;
30623063
} else {
30633064
defaultLabel = label;
@@ -3068,16 +3069,16 @@ void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputSt
30683069
this->writeOpCode(SpvOpSwitch, size, out);
30693070
this->writeWord(value, out);
30703071
this->writeWord(defaultLabel, out);
3071-
for (size_t i = 0; i < s.fCases.size(); ++i) {
3072-
if (!s.fCases[i]->fValue) {
3072+
for (int i = 0; i < cases.count(); ++i) {
3073+
if (!cases[i].value()) {
30733074
continue;
30743075
}
3075-
this->writeWord(s.fCases[i]->fValue->as<IntLiteral>().value(), out);
3076+
this->writeWord(cases[i].value()->as<IntLiteral>().value(), out);
30763077
this->writeWord(labels[i], out);
30773078
}
3078-
for (size_t i = 0; i < s.fCases.size(); ++i) {
3079+
for (int i = 0; i < cases.count(); ++i) {
30793080
this->writeLabel(labels[i], out);
3080-
for (const auto& stmt : s.fCases[i]->fStatements) {
3081+
for (const auto& stmt : cases[i].statements()) {
30813082
this->writeStatement(*stmt, out);
30823083
}
30833084
if (fCurrentBlock) {

0 commit comments

Comments
 (0)