Separate IRGenerator and Compiler into separate DSLWriter fields.

The only actual use of Compiler is in dsl::ReleaseProgram. All other
uses of Compiler in DSLWriter were to gain access to its internal
IRGenerator.

Change-Id: I0169cb1debf837894b1be3321f3d920259f7e91a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/451741
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
John Stiles 2021-09-22 12:23:23 -04:00 committed by SkCQ
parent 1a117d7642
commit fe9bfe6eeb
3 changed files with 37 additions and 24 deletions

View File

@ -34,22 +34,24 @@ void Start(SkSL::Compiler* compiler, ProgramKind kind) {
} }
void Start(SkSL::Compiler* compiler, ProgramKind kind, const ProgramSettings& settings) { void Start(SkSL::Compiler* compiler, ProgramKind kind, const ProgramSettings& settings) {
DSLWriter::SetInstance(std::make_unique<DSLWriter>(&compiler->context(), DSLWriter::SetInstance(std::make_unique<DSLWriter>(&compiler->context(), compiler,
compiler, kind, settings, &compiler->irGenerator(), kind, settings,
compiler->moduleForProgramKind(kind), compiler->moduleForProgramKind(kind),
/*isModule=*/false)); /*isModule=*/false));
} }
void Start(SkSL::Context* context, ProgramKind kind, const ProgramSettings& settings) { void Start(SkSL::Context* context, ProgramKind kind, const ProgramSettings& settings) {
DSLWriter::SetInstance(std::make_unique<DSLWriter>(context, /*compiler=*/nullptr, kind, DSLWriter::SetInstance(std::make_unique<DSLWriter>(context, /*compiler=*/nullptr,
settings, /*module=*/skstd::nullopt, /*irGenerator=*/nullptr, kind, settings,
/*module=*/skstd::nullopt,
/*isModule=*/false)); /*isModule=*/false));
} }
void StartModule(SkSL::Compiler* compiler, ProgramKind kind, const ProgramSettings& settings, void StartModule(SkSL::Compiler* compiler, ProgramKind kind, const ProgramSettings& settings,
SkSL::ParsedModule module) { SkSL::ParsedModule module) {
DSLWriter::SetInstance(std::make_unique<DSLWriter>(&compiler->context(), compiler, kind, DSLWriter::SetInstance(std::make_unique<DSLWriter>(&compiler->context(), compiler,
settings, module, /*isModule=*/true)); &compiler->irGenerator(), kind, settings,
module, /*isModule=*/true));
} }
void End() { void End() {
@ -72,11 +74,12 @@ public:
static std::unique_ptr<SkSL::Program> ReleaseProgram(std::unique_ptr<String> source) { static std::unique_ptr<SkSL::Program> ReleaseProgram(std::unique_ptr<String> source) {
DSLWriter& instance = DSLWriter::Instance(); DSLWriter& instance = DSLWriter::Instance();
SkSL::IRGenerator& ir = DSLWriter::IRGenerator(); SkSL::IRGenerator& ir = DSLWriter::IRGenerator();
SkSL::Compiler& compiler = DSLWriter::Compiler();
IRGenerator::IRBundle bundle = ir.finish(); IRGenerator::IRBundle bundle = ir.finish();
Pool* pool = DSLWriter::Instance().fPool.get(); Pool* pool = DSLWriter::Instance().fPool.get();
auto result = std::make_unique<SkSL::Program>(std::move(source), auto result = std::make_unique<SkSL::Program>(std::move(source),
std::move(instance.fConfig), std::move(instance.fConfig),
DSLWriter::Compiler().fContext, compiler.fContext,
std::move(bundle.fElements), std::move(bundle.fElements),
std::move(bundle.fSharedElements), std::move(bundle.fSharedElements),
std::move(instance.fModifiersPool), std::move(instance.fModifiersPool),
@ -84,9 +87,9 @@ public:
std::move(instance.fPool), std::move(instance.fPool),
bundle.fInputs); bundle.fInputs);
bool success = false; bool success = false;
if (!DSLWriter::Compiler().finalize(*result)) { if (!compiler.finalize(*result)) {
// Do not return programs that failed to compile. // Do not return programs that failed to compile.
} else if (!DSLWriter::Compiler().optimize(*result)) { } else if (!compiler.optimize(*result)) {
// Do not return programs that failed to optimize. // Do not return programs that failed to optimize.
} else { } else {
// We have a successful program! // We have a successful program!

View File

@ -29,11 +29,13 @@ namespace SkSL {
namespace dsl { namespace dsl {
DSLWriter::DSLWriter(SkSL::Context* context, SkSL::Compiler* compiler, SkSL::ProgramKind kind, DSLWriter::DSLWriter(SkSL::Context* context, SkSL::Compiler* compiler,
SkSL::IRGenerator* irGenerator, SkSL::ProgramKind kind,
const SkSL::ProgramSettings& settings, const SkSL::ProgramSettings& settings,
skstd::optional<SkSL::ParsedModule> module, bool isModule) skstd::optional<SkSL::ParsedModule> module, bool isModule)
: fContext(context) : fContext(context)
, fCompiler(compiler) , fCompiler(compiler)
, fIRGenerator(irGenerator)
, fOldErrorReporter(*fContext->fErrors) , fOldErrorReporter(*fContext->fErrors)
, fSettings(settings) , fSettings(settings)
, fIsModule(isModule) { , fIsModule(isModule) {
@ -41,6 +43,9 @@ DSLWriter::DSLWriter(SkSL::Context* context, SkSL::Compiler* compiler, SkSL::Pro
if (fCompiler) { if (fCompiler) {
SkASSERT(fContext == fCompiler->fContext.get()); SkASSERT(fContext == fCompiler->fContext.get());
} }
if (fIRGenerator) {
SkASSERT(fContext == &fIRGenerator->fContext);
}
fOldModifiersPool = fContext->fModifiersPool; fOldModifiersPool = fContext->fModifiersPool;
@ -61,15 +66,15 @@ DSLWriter::DSLWriter(SkSL::Context* context, SkSL::Compiler* compiler, SkSL::Pro
fContext->fConfig = fConfig.get(); fContext->fConfig = fConfig.get();
fContext->fErrors = &fDefaultErrorReporter; fContext->fErrors = &fDefaultErrorReporter;
if (compiler && module.has_value()) { if (fIRGenerator && module.has_value()) {
fCompiler->fIRGenerator->start(*module, isModule, &fProgramElements, &fSharedElements); fIRGenerator->start(*module, isModule, &fProgramElements, &fSharedElements);
} }
} }
DSLWriter::~DSLWriter() { DSLWriter::~DSLWriter() {
if (SymbolTable()) { if (SymbolTable()) {
if (fCompiler) { if (fIRGenerator) {
fCompiler->fIRGenerator->finish(); fIRGenerator->finish();
} }
fProgramElements.clear(); fProgramElements.clear();
} else { } else {
@ -84,8 +89,16 @@ DSLWriter::~DSLWriter() {
} }
} }
SkSL::Compiler& DSLWriter::Compiler() {
SkSL::Compiler* compiler = Instance().fCompiler;
SkASSERT(compiler);
return *compiler;
}
SkSL::IRGenerator& DSLWriter::IRGenerator() { SkSL::IRGenerator& DSLWriter::IRGenerator() {
return *Compiler().fIRGenerator; SkSL::IRGenerator* irGenerator = Instance().fIRGenerator;
SkASSERT(irGenerator);
return *irGenerator;
} }
SkSL::Context& DSLWriter::Context() { SkSL::Context& DSLWriter::Context() {

View File

@ -50,9 +50,9 @@ class DSLVar;
*/ */
class DSLWriter { class DSLWriter {
public: public:
DSLWriter(SkSL::Context* context, SkSL::Compiler* compiler, SkSL::ProgramKind kind, DSLWriter(SkSL::Context* context, SkSL::Compiler* compiler, SkSL::IRGenerator* irGenerator,
const SkSL::ProgramSettings& settings, skstd::optional<SkSL::ParsedModule> module, SkSL::ProgramKind kind, const SkSL::ProgramSettings& settings,
bool isModule); skstd::optional<SkSL::ParsedModule> module, bool isModule);
~DSLWriter(); ~DSLWriter();
@ -64,11 +64,7 @@ public:
/** /**
* Returns the Compiler used by DSL operations in the current thread. * Returns the Compiler used by DSL operations in the current thread.
*/ */
static SkSL::Compiler& Compiler() { static SkSL::Compiler& Compiler();
SkSL::Compiler* compiler = Instance().fCompiler;
SkASSERT(compiler);
return *compiler;
}
/** /**
* Returns the IRGenerator used by DSL operations in the current thread. * Returns the IRGenerator used by DSL operations in the current thread.
@ -282,7 +278,7 @@ public:
} }
/** /**
* Forwards any pending Compiler errors to the DSL ErrorReporter. * Forwards any pending errors to the DSL ErrorReporter.
*/ */
static void ReportErrors(PositionInfo pos); static void ReportErrors(PositionInfo pos);
@ -299,6 +295,7 @@ private:
std::unique_ptr<SkSL::ModifiersPool> fModifiersPool; std::unique_ptr<SkSL::ModifiersPool> fModifiersPool;
SkSL::Context* fContext; SkSL::Context* fContext;
SkSL::Compiler* fCompiler; SkSL::Compiler* fCompiler;
SkSL::IRGenerator* fIRGenerator;
std::unique_ptr<Pool> fPool; std::unique_ptr<Pool> fPool;
SkSL::ProgramConfig* fOldConfig; SkSL::ProgramConfig* fOldConfig;
SkSL::ModifiersPool* fOldModifiersPool; SkSL::ModifiersPool* fOldModifiersPool;