Moved SymbolTable from IRGenerator to Compiler

Change-Id: I5971a4650ad9d85e7c9a0420697822dac5d87bc4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/459856
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
This commit is contained in:
Ethan Nicholas 2021-10-15 11:30:57 -04:00 committed by SkCQ
parent a750dfcce2
commit 0e55a137dd
8 changed files with 58 additions and 67 deletions

View File

@ -509,12 +509,12 @@ bool Compiler::runInliner(const std::vector<std::unique_ptr<ProgramElement>>& el
// irGenerator.convertIdentifier() to look up `length`. convertIdentifier() needs a valid symbol
// table to find the declaration of `length`. To allow this chain of events to succeed, we
// re-insert the program's symbol table back into the IRGenerator temporarily.
SkASSERT(!fIRGenerator->fSymbolTable);
fIRGenerator->fSymbolTable = symbols;
SkASSERT(!fSymbolTable);
fSymbolTable = symbols;
bool result = fInliner.analyze(elements, symbols, usage);
fIRGenerator->fSymbolTable = nullptr;
fSymbolTable = nullptr;
return result;
}
@ -547,7 +547,7 @@ bool Compiler::toSPIRV(Program& program, OutputStream& out) {
settings.fDSLUseMemoryPool = false;
dsl::Start(this, program.fConfig->fKind, settings);
dsl::SetErrorReporter(&fErrorReporter);
ThreadContext::IRGenerator().fSymbolTable = program.fSymbols;
fSymbolTable = program.fSymbols;
#ifdef SK_ENABLE_SPIRV_VALIDATION
StringStream buffer;
SPIRVCodeGenerator cg(fContext.get(), &program, &buffer);

View File

@ -256,6 +256,9 @@ private:
Mangler fMangler;
Inliner fInliner;
std::unique_ptr<IRGenerator> fIRGenerator;
// This is the current symbol table of the code we are processing, and therefore changes during
// compilation
std::shared_ptr<SymbolTable> fSymbolTable;
String fErrorText;

View File

@ -67,7 +67,7 @@ IRGenerator::IRGenerator(const Context* context)
: fContext(*context) {}
std::unique_ptr<Expression> IRGenerator::convertIdentifier(int line, skstd::string_view name) {
const Symbol* result = (*fSymbolTable)[name];
const Symbol* result = (*ThreadContext::SymbolTable())[name];
if (!result) {
this->errorReporter().error(line, "unknown identifier '" + name + "'");
return nullptr;
@ -118,67 +118,64 @@ std::unique_ptr<Expression> IRGenerator::convertIdentifier(int line, skstd::stri
}
}
void IRGenerator::start(const ParsedModule& base) {
fSymbolTable = base.fSymbols;
void IRGenerator::start(std::shared_ptr<SymbolTable>& symbols) {
fInputs = {};
fDefinedStructs.clear();
SymbolTable::Push(&fSymbolTable, fContext.fConfig->fIsBuiltinCode);
SymbolTable::Push(&symbols, fContext.fConfig->fIsBuiltinCode);
if (this->settings().fExternalFunctions) {
// Add any external values to the new symbol table, so they're only visible to this Program.
for (const std::unique_ptr<ExternalFunction>& ef : *this->settings().fExternalFunctions) {
fSymbolTable->addWithoutOwnership(ef.get());
symbols->addWithoutOwnership(ef.get());
}
}
if (this->isRuntimeEffect() && !fContext.fConfig->fSettings.fEnforceES2Restrictions) {
// We're compiling a runtime effect, but we're not enforcing ES2 restrictions. Add various
// non-ES2 types to our symbol table to allow them to be tested.
fSymbolTable->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
fSymbolTable->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
fSymbolTable->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
fSymbolTable->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
fSymbolTable->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
fSymbolTable->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
fSymbolTable->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
fSymbolTable->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
fSymbolTable->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
symbols->addAlias("mat2x2", fContext.fTypes.fFloat2x2.get());
symbols->addAlias("mat2x3", fContext.fTypes.fFloat2x3.get());
symbols->addAlias("mat2x4", fContext.fTypes.fFloat2x4.get());
symbols->addAlias("mat3x2", fContext.fTypes.fFloat3x2.get());
symbols->addAlias("mat3x3", fContext.fTypes.fFloat3x3.get());
symbols->addAlias("mat3x4", fContext.fTypes.fFloat3x4.get());
symbols->addAlias("mat4x2", fContext.fTypes.fFloat4x2.get());
symbols->addAlias("mat4x3", fContext.fTypes.fFloat4x3.get());
symbols->addAlias("mat4x4", fContext.fTypes.fFloat4x4.get());
fSymbolTable->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
fSymbolTable->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
fSymbolTable->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
fSymbolTable->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
fSymbolTable->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
fSymbolTable->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
symbols->addAlias("float2x3", fContext.fTypes.fFloat2x3.get());
symbols->addAlias("float2x4", fContext.fTypes.fFloat2x4.get());
symbols->addAlias("float3x2", fContext.fTypes.fFloat3x2.get());
symbols->addAlias("float3x4", fContext.fTypes.fFloat3x4.get());
symbols->addAlias("float4x2", fContext.fTypes.fFloat4x2.get());
symbols->addAlias("float4x3", fContext.fTypes.fFloat4x3.get());
fSymbolTable->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
fSymbolTable->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
fSymbolTable->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
fSymbolTable->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
fSymbolTable->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
fSymbolTable->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
symbols->addAlias("half2x3", fContext.fTypes.fHalf2x3.get());
symbols->addAlias("half2x4", fContext.fTypes.fHalf2x4.get());
symbols->addAlias("half3x2", fContext.fTypes.fHalf3x2.get());
symbols->addAlias("half3x4", fContext.fTypes.fHalf3x4.get());
symbols->addAlias("half4x2", fContext.fTypes.fHalf4x2.get());
symbols->addAlias("half4x3", fContext.fTypes.fHalf4x3.get());
fSymbolTable->addAlias("uint", fContext.fTypes.fUInt.get());
fSymbolTable->addAlias("uint2", fContext.fTypes.fUInt2.get());
fSymbolTable->addAlias("uint3", fContext.fTypes.fUInt3.get());
fSymbolTable->addAlias("uint4", fContext.fTypes.fUInt4.get());
symbols->addAlias("uint", fContext.fTypes.fUInt.get());
symbols->addAlias("uint2", fContext.fTypes.fUInt2.get());
symbols->addAlias("uint3", fContext.fTypes.fUInt3.get());
symbols->addAlias("uint4", fContext.fTypes.fUInt4.get());
fSymbolTable->addAlias("short", fContext.fTypes.fShort.get());
fSymbolTable->addAlias("short2", fContext.fTypes.fShort2.get());
fSymbolTable->addAlias("short3", fContext.fTypes.fShort3.get());
fSymbolTable->addAlias("short4", fContext.fTypes.fShort4.get());
symbols->addAlias("short", fContext.fTypes.fShort.get());
symbols->addAlias("short2", fContext.fTypes.fShort2.get());
symbols->addAlias("short3", fContext.fTypes.fShort3.get());
symbols->addAlias("short4", fContext.fTypes.fShort4.get());
fSymbolTable->addAlias("ushort", fContext.fTypes.fUShort.get());
fSymbolTable->addAlias("ushort2", fContext.fTypes.fUShort2.get());
fSymbolTable->addAlias("ushort3", fContext.fTypes.fUShort3.get());
fSymbolTable->addAlias("ushort4", fContext.fTypes.fUShort4.get());
symbols->addAlias("ushort", fContext.fTypes.fUShort.get());
symbols->addAlias("ushort2", fContext.fTypes.fUShort2.get());
symbols->addAlias("ushort3", fContext.fTypes.fUShort3.get());
symbols->addAlias("ushort4", fContext.fTypes.fUShort4.get());
}
}
IRGenerator::IRBundle IRGenerator::finish() {
return IRBundle{std::move(fSymbolTable),
fInputs};
return IRBundle{fInputs};
}
} // namespace SkSL

View File

@ -53,11 +53,10 @@ public:
IRGenerator(const Context* context);
struct IRBundle {
std::shared_ptr<SymbolTable> fSymbolTable;
Program::Inputs fInputs;
Program::Inputs fInputs;
};
void start(const ParsedModule& base);
void start(std::shared_ptr<SymbolTable>& symbols);
/**
* If externalFunctions is supplied, those values are registered in the symbol table of the
@ -73,14 +72,6 @@ public:
ErrorReporter& errorReporter() const { return *fContext.fErrors; }
std::shared_ptr<SymbolTable>& symbolTable() {
return fSymbolTable;
}
void setSymbolTable(std::shared_ptr<SymbolTable>& symbolTable) {
fSymbolTable = symbolTable;
}
std::unique_ptr<Expression> convertIdentifier(int line, skstd::string_view identifier);
const Context& fContext;
@ -108,7 +99,6 @@ private:
Program::Inputs fInputs;
std::shared_ptr<SymbolTable> fSymbolTable = nullptr;
std::unordered_set<const Type*> fDefinedStructs;
friend class AutoSymbolTable;

View File

@ -46,12 +46,14 @@ ThreadContext::ThreadContext(SkSL::Compiler* compiler, SkSL::ProgramKind kind,
fCompiler->fContext->fIntrinsics->resetAlreadyIncluded();
}
fCompiler->fIRGenerator->start(module);
fCompiler->fSymbolTable = module.fSymbols;
fCompiler->fIRGenerator->start(fCompiler->fSymbolTable);
}
ThreadContext::~ThreadContext() {
if (SymbolTable()) {
fCompiler->fIRGenerator->finish();
fCompiler->fSymbolTable = nullptr;
fProgramElements.clear();
} else {
// We should only be here with a null symbol table if ReleaseProgram was called
@ -65,7 +67,6 @@ ThreadContext::~ThreadContext() {
}
}
SkSL::IRGenerator& ThreadContext::IRGenerator() {
return *Compiler().fIRGenerator;
}
@ -82,8 +83,8 @@ SkSL::Program::Inputs& ThreadContext::Inputs() {
return IRGenerator().fInputs;
}
const std::shared_ptr<SkSL::SymbolTable>& ThreadContext::SymbolTable() {
return IRGenerator().fSymbolTable;
std::shared_ptr<SkSL::SymbolTable>& ThreadContext::SymbolTable() {
return Compiler().fSymbolTable;
}
const SkSL::Modifiers* ThreadContext::Modifiers(const SkSL::Modifiers& modifiers) {

View File

@ -88,9 +88,9 @@ public:
}
/**
* Returns the SymbolTable of the current thread's IRGenerator.
* Returns the current SymbolTable.
*/
static const std::shared_ptr<SkSL::SymbolTable>& SymbolTable();
static std::shared_ptr<SkSL::SymbolTable>& SymbolTable();
/**
* Returns the current memory pool.

View File

@ -83,7 +83,7 @@ public:
std::move(instance.fProgramElements),
std::move(instance.fSharedElements),
std::move(instance.fModifiersPool),
std::move(bundle.fSymbolTable),
std::move(compiler.fSymbolTable),
std::move(instance.fPool),
bundle.fInputs);
bool success = false;

View File

@ -21,15 +21,15 @@ static bool is_type_in_symbol_table(skstd::string_view name, SkSL::SymbolTable*
}
void PushSymbolTable() {
SymbolTable::Push(&ThreadContext::IRGenerator().symbolTable());
SymbolTable::Push(&ThreadContext::SymbolTable());
}
void PopSymbolTable() {
SymbolTable::Pop(&ThreadContext::IRGenerator().symbolTable());
SymbolTable::Pop(&ThreadContext::SymbolTable());
}
std::shared_ptr<SymbolTable> CurrentSymbolTable() {
return ThreadContext::IRGenerator().symbolTable();
return ThreadContext::SymbolTable();
}
DSLPossibleExpression Symbol(skstd::string_view name, PositionInfo pos) {