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 {
for (int i = 0; i < loops; i++) {
fCompiler.irGenerator().pushSymbolTable();
SkSL::Parser parser(fSrc, *fCompiler.irGenerator().symbolTable(),
fCompiler.errorReporter());
parser.compilationUnit();
fCompiler.irGenerator().popSymbolTable();
{
SkSL::AutoSymbolTable table(&fCompiler.irGenerator().symbolTable());
SkSL::Parser parser(fSrc, *fCompiler.irGenerator().symbolTable(),
fCompiler.errorReporter());
parser.compilationUnit();
}
if (fCompiler.errorCount()) {
SK_ABORT("shader compilation failed: %s\n", fCompiler.errorText().c_str());
}

View File

@ -62,35 +62,9 @@
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)
: 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) {
if (this->programKind() != ProgramKind::kFragment &&
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) {
SkASSERT(block.fKind == ASTNode::Kind::kBlock);
AutoSymbolTable table(this);
AutoSymbolTable table(&fSymbolTable);
StatementArray statements;
for (const auto& child : block) {
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) {
SkASSERT(f.fKind == ASTNode::Kind::kFor);
AutoSymbolTable table(this);
AutoSymbolTable table(&fSymbolTable);
std::unique_ptr<Statement> initializer;
auto iter = f.begin();
if (*iter) {
@ -470,7 +444,7 @@ std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTNode& s) {
if (!value) {
return nullptr;
}
AutoSymbolTable table(this);
AutoSymbolTable table(&fSymbolTable);
ExpressionArray caseValues;
StatementArray caseStatements;
for (; iter != s.end(); ++iter) {
@ -714,7 +688,7 @@ void IRGenerator::convertFunction(const ASTNode& f) {
fIsBuiltinCode));
} else {
// Compile function body.
AutoSymbolTable table(this);
AutoSymbolTable table(&fSymbolTable);
for (const Variable* param : decl->parameters()) {
fSymbolTable->addWithoutOwnership(param);
}
@ -778,7 +752,7 @@ std::unique_ptr<SkSL::InterfaceBlock> IRGenerator::convertInterfaceBlock(const A
std::vector<Type::Field> fields;
auto iter = intf.begin();
{
AutoSymbolTable table(this);
AutoSymbolTable table(&fSymbolTable);
symbols = fSymbolTable;
for (size_t i = 0; i < id.fDeclarationCount; ++i) {
StatementArray decls = this->convertVarDeclarations(*(iter++),
@ -1326,7 +1300,7 @@ void IRGenerator::start(const ParsedModule& base,
fRTAdjust = nullptr;
fRTAdjustInterfaceBlock = nullptr;
fDefinedStructs.clear();
this->pushSymbolTable();
SymbolTable::Push(&fSymbolTable, fIsBuiltinCode);
if (this->settings().fExternalFunctions) {
// 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;
}
void pushSymbolTable();
void popSymbolTable();
static void CheckModifiers(const Context& context,
int offset,
const Modifiers& modifiers,

View File

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

View File

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

View File

@ -37,6 +37,22 @@ public:
, fBuiltin(builtin)
, 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
* 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;
};
/**
* 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
#endif