Move symbol-table push/pop methods out of IRGenerator.

This will allow us to use AutoSymbolTable in more places (and helps cut
down IRGenerator, as we transition away from it).

Change-Id: Ie88c149e0f7eee080a8a4c8cbbc34c8a2a5401d1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/449756
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
John Stiles 2021-09-17 10:40:47 -04:00 committed by SkCQ
parent a59925fb1d
commit de2b609fdd
6 changed files with 55 additions and 46 deletions

View File

@ -139,11 +139,12 @@ protected:
void onDraw(int loops, SkCanvas*) override { void onDraw(int loops, SkCanvas*) override {
for (int i = 0; i < loops; i++) { for (int i = 0; i < loops; i++) {
fCompiler.irGenerator().pushSymbolTable(); {
SkSL::Parser parser(fSrc, *fCompiler.irGenerator().symbolTable(), SkSL::AutoSymbolTable table(&fCompiler.irGenerator().symbolTable());
fCompiler.errorReporter()); SkSL::Parser parser(fSrc, *fCompiler.irGenerator().symbolTable(),
parser.compilationUnit(); fCompiler.errorReporter());
fCompiler.irGenerator().popSymbolTable(); parser.compilationUnit();
}
if (fCompiler.errorCount()) { if (fCompiler.errorCount()) {
SK_ABORT("shader compilation failed: %s\n", fCompiler.errorText().c_str()); SK_ABORT("shader compilation failed: %s\n", fCompiler.errorText().c_str());
} }

View File

@ -62,35 +62,9 @@
namespace SkSL { namespace SkSL {
class AutoSymbolTable {
public:
AutoSymbolTable(IRGenerator* ir)
: fIR(ir)
, fPrevious(fIR->fSymbolTable) {
fIR->pushSymbolTable();
}
~AutoSymbolTable() {
fIR->popSymbolTable();
SkASSERT(fPrevious == fIR->fSymbolTable);
}
IRGenerator* fIR;
std::shared_ptr<SymbolTable> fPrevious;
};
IRGenerator::IRGenerator(const Context* context) IRGenerator::IRGenerator(const Context* context)
: fContext(*context) {} : fContext(*context) {}
void IRGenerator::pushSymbolTable() {
auto childSymTable = std::make_shared<SymbolTable>(std::move(fSymbolTable), fIsBuiltinCode);
fSymbolTable = std::move(childSymTable);
}
void IRGenerator::popSymbolTable() {
fSymbolTable = fSymbolTable->fParent;
}
std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) { std::unique_ptr<Extension> IRGenerator::convertExtension(int offset, skstd::string_view name) {
if (this->programKind() != ProgramKind::kFragment && if (this->programKind() != ProgramKind::kFragment &&
this->programKind() != ProgramKind::kVertex) { this->programKind() != ProgramKind::kVertex) {
@ -136,7 +110,7 @@ std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTNode& statemen
std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) { std::unique_ptr<Block> IRGenerator::convertBlock(const ASTNode& block) {
SkASSERT(block.fKind == ASTNode::Kind::kBlock); SkASSERT(block.fKind == ASTNode::Kind::kBlock);
AutoSymbolTable table(this); AutoSymbolTable table(&fSymbolTable);
StatementArray statements; StatementArray statements;
for (const auto& child : block) { for (const auto& child : block) {
std::unique_ptr<Statement> statement = this->convertStatement(child); std::unique_ptr<Statement> statement = this->convertStatement(child);
@ -398,7 +372,7 @@ std::unique_ptr<Statement> IRGenerator::convertIf(const ASTNode& n) {
std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) { std::unique_ptr<Statement> IRGenerator::convertFor(const ASTNode& f) {
SkASSERT(f.fKind == ASTNode::Kind::kFor); SkASSERT(f.fKind == ASTNode::Kind::kFor);
AutoSymbolTable table(this); AutoSymbolTable table(&fSymbolTable);
std::unique_ptr<Statement> initializer; std::unique_ptr<Statement> initializer;
auto iter = f.begin(); auto iter = f.begin();
if (*iter) { if (*iter) {
@ -470,7 +444,7 @@ std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
if (!value) { if (!value) {
return nullptr; return nullptr;
} }
AutoSymbolTable table(this); AutoSymbolTable table(&fSymbolTable);
ExpressionArray caseValues; ExpressionArray caseValues;
StatementArray caseStatements; StatementArray caseStatements;
for (; iter != s.end(); ++iter) { for (; iter != s.end(); ++iter) {
@ -714,7 +688,7 @@ void IRGenerator::convertFunction(const ASTNode& f) {
fIsBuiltinCode)); fIsBuiltinCode));
} else { } else {
// Compile function body. // Compile function body.
AutoSymbolTable table(this); AutoSymbolTable table(&fSymbolTable);
for (const Variable* param : decl->parameters()) { for (const Variable* param : decl->parameters()) {
fSymbolTable->addWithoutOwnership(param); fSymbolTable->addWithoutOwnership(param);
} }
@ -778,7 +752,7 @@ std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const A
std::vector<Type::Field> fields; std::vector<Type::Field> fields;
auto iter = intf.begin(); auto iter = intf.begin();
{ {
AutoSymbolTable table(this); AutoSymbolTable table(&fSymbolTable);
symbols = fSymbolTable; symbols = fSymbolTable;
for (size_t i = 0; i < id.fDeclarationCount; ++i) { for (size_t i = 0; i < id.fDeclarationCount; ++i) {
StatementArray decls = this->convertVarDeclarations(*(iter++), StatementArray decls = this->convertVarDeclarations(*(iter++),
@ -1326,7 +1300,7 @@ void IRGenerator::start(const ParsedModule& base,
fRTAdjust = nullptr; fRTAdjust = nullptr;
fRTAdjustInterfaceBlock = nullptr; fRTAdjustInterfaceBlock = nullptr;
fDefinedStructs.clear(); fDefinedStructs.clear();
this->pushSymbolTable(); SymbolTable::Push(&fSymbolTable, fIsBuiltinCode);
if (this->settings().fExternalFunctions) { if (this->settings().fExternalFunctions) {
// Add any external values to the new symbol table, so they're only visible to this Program. // Add any external values to the new symbol table, so they're only visible to this Program.

View File

@ -133,9 +133,6 @@ public:
fSymbolTable = symbolTable; fSymbolTable = symbolTable;
} }
void pushSymbolTable();
void popSymbolTable();
static void CheckModifiers(const Context& context, static void CheckModifiers(const Context& context,
int offset, int offset,
const Modifiers& modifiers, const Modifiers& modifiers,

View File

@ -15,11 +15,11 @@ namespace SkSL {
namespace dsl { namespace dsl {
void PushSymbolTable() { void PushSymbolTable() {
DSLWriter::IRGenerator().pushSymbolTable(); SymbolTable::Push(&DSLWriter::IRGenerator().symbolTable());
} }
void PopSymbolTable() { void PopSymbolTable() {
DSLWriter::IRGenerator().popSymbolTable(); SymbolTable::Pop(&DSLWriter::IRGenerator().symbolTable());
} }
std::shared_ptr<SymbolTable> CurrentSymbolTable() { std::shared_ptr<SymbolTable> CurrentSymbolTable() {

View File

@ -9,6 +9,7 @@
#include "include/private/SkSLDefines.h" #include "include/private/SkSLDefines.h"
#include "include/sksl/DSLCore.h" #include "include/sksl/DSLCore.h"
#include "include/sksl/DSLSymbols.h"
#include "include/sksl/SkSLErrorReporter.h" #include "include/sksl/SkSLErrorReporter.h"
#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
@ -94,8 +95,8 @@ const std::shared_ptr<SkSL::SymbolTable>& DSLWriter::SymbolTable() {
} }
void DSLWriter::Reset() { void DSLWriter::Reset() {
IRGenerator().popSymbolTable(); dsl::PopSymbolTable();
IRGenerator().pushSymbolTable(); dsl::PushSymbolTable();
ProgramElements().clear(); ProgramElements().clear();
Instance().fModifiersPool->clear(); Instance().fModifiersPool->clear();
} }
@ -119,7 +120,7 @@ void DSLWriter::StartFragmentProcessor(GrFragmentProcessor::ProgramImpl* process
DSLWriter& instance = Instance(); DSLWriter& instance = Instance();
instance.fStack.push({processor, emitArgs, StatementArray{}}); instance.fStack.push({processor, emitArgs, StatementArray{}});
CurrentEmitArgs()->fFragBuilder->fDeclarations.swap(instance.fStack.top().fSavedDeclarations); CurrentEmitArgs()->fFragBuilder->fDeclarations.swap(instance.fStack.top().fSavedDeclarations);
IRGenerator().pushSymbolTable(); dsl::PushSymbolTable();
} }
void DSLWriter::EndFragmentProcessor() { void DSLWriter::EndFragmentProcessor() {
@ -127,7 +128,7 @@ void DSLWriter::EndFragmentProcessor() {
SkASSERT(!instance.fStack.empty()); SkASSERT(!instance.fStack.empty());
CurrentEmitArgs()->fFragBuilder->fDeclarations.swap(instance.fStack.top().fSavedDeclarations); CurrentEmitArgs()->fFragBuilder->fDeclarations.swap(instance.fStack.top().fSavedDeclarations);
instance.fStack.pop(); instance.fStack.pop();
IRGenerator().popSymbolTable(); dsl::PopSymbolTable();
} }
GrGLSLUniformHandler::UniformHandle DSLWriter::VarUniformHandle(const DSLGlobalVar& var) { GrGLSLUniformHandler::UniformHandle DSLWriter::VarUniformHandle(const DSLGlobalVar& var) {

View File

@ -37,6 +37,22 @@ public:
, fBuiltin(builtin) , fBuiltin(builtin)
, fContext(parent->fContext) {} , fContext(parent->fContext) {}
/** Replaces the passed-in SymbolTable with a newly-created child symbol table. */
static void Push(std::shared_ptr<SymbolTable>* table) {
Push(table, (*table)->isBuiltin());
}
static void Push(std::shared_ptr<SymbolTable>* table, bool isBuiltin) {
*table = std::make_shared<SymbolTable>(*table, isBuiltin);
}
/**
* Replaces the passed-in SymbolTable with its parent. If the child symbol table is otherwise
* unreferenced, it will be deleted.
*/
static void Pop(std::shared_ptr<SymbolTable>* table) {
*table = (*table)->fParent;
}
/** /**
* If the input is a built-in symbol table, returns a new empty symbol table as a child of the * If the input is a built-in symbol table, returns a new empty symbol table as a child of the
* input table. If the input is not a built-in symbol table, returns it as-is. Built-in symbol * input table. If the input is not a built-in symbol table, returns it as-is. Built-in symbol
@ -146,6 +162,26 @@ private:
friend class Dehydrator; friend class Dehydrator;
}; };
/**
* While in scope, the passed-in symbol table is replaced with a child symbol table.
*/
class AutoSymbolTable {
public:
AutoSymbolTable(std::shared_ptr<SymbolTable>* s)
: fSymbolTable(s) {
SkDEBUGCODE(fPrevious = fSymbolTable->get();)
SymbolTable::Push(fSymbolTable);
}
~AutoSymbolTable() {
SymbolTable::Pop(fSymbolTable);
SkASSERT(fPrevious == fSymbolTable->get());
}
std::shared_ptr<SymbolTable>* fSymbolTable;
SkDEBUGCODE(SymbolTable* fPrevious;)
};
} // namespace SkSL } // namespace SkSL
#endif #endif