Migrate from tint::ast::Module to tint::Program

Depends on: https://dawn-review.googlesource.com/c/tint/+/38556

Bug: tint:390
Change-Id: I12104d80370ddc1a3277770ca051cbd0992b758e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/38522
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-01-26 12:24:48 +00:00 committed by Commit Bot service account
parent b3177d418e
commit e0feccacaf
9 changed files with 102 additions and 100 deletions

View File

@ -179,7 +179,7 @@ namespace dawn_native {
}
#ifdef DAWN_ENABLE_WGSL
ResultOrError<tint::ast::Module> ParseWGSL(const tint::Source::File* file) {
ResultOrError<tint::Program> ParseWGSL(const tint::Source::File* file) {
std::ostringstream errorStream;
errorStream << "Tint WGSL reader failure:" << std::endl;
@ -192,22 +192,23 @@ namespace dawn_native {
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
}
tint::ast::Module module = parser.module();
if (!module.IsValid()) {
errorStream << "Invalid module generated..." << std::endl;
tint::Program program = parser.program();
if (!program.IsValid()) {
errorStream << "Invalid program generated..." << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
}
tint::TypeDeterminer typeDeterminer(&module);
if (!typeDeterminer.Determine()) {
errorStream << "Type Determination: " << typeDeterminer.error();
tint::diag::List diagnostics = tint::TypeDeterminer::Run(&program);
if (diagnostics.contains_errors()) {
std::string err = tint::diag::Formatter{}.format(diagnostics);
errorStream << "Type Determination: " << err;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
}
return std::move(module);
return std::move(program);
}
ResultOrError<tint::ast::Module> ParseSPIRV(const std::vector<uint32_t>& spirv) {
ResultOrError<tint::Program> ParseSPIRV(const std::vector<uint32_t>& spirv) {
std::ostringstream errorStream;
errorStream << "Tint SPIRV reader failure:" << std::endl;
@ -217,27 +218,28 @@ namespace dawn_native {
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
}
tint::ast::Module module = parser.module();
if (!module.IsValid()) {
errorStream << "Invalid module generated..." << std::endl;
tint::Program program = parser.program();
if (!program.IsValid()) {
errorStream << "Invalid program generated..." << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
}
tint::TypeDeterminer typeDeterminer(&module);
if (!typeDeterminer.Determine()) {
errorStream << "Type Determination: " << typeDeterminer.error();
tint::diag::List diagnostics = tint::TypeDeterminer::Run(&program);
if (diagnostics.contains_errors()) {
std::string err = tint::diag::Formatter{}.format(diagnostics);
errorStream << "Type Determination: " << err;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
}
return std::move(module);
return std::move(program);
}
MaybeError ValidateModule(tint::ast::Module* module) {
MaybeError ValidateModule(tint::Program* program) {
std::ostringstream errorStream;
errorStream << "Tint module validation" << std::endl;
errorStream << "Tint program validation" << std::endl;
tint::Validator validator;
if (!validator.Validate(module)) {
if (!validator.Validate(program)) {
auto err = tint::diag::Formatter{}.format(validator.diagnostics());
errorStream << "Validation: " << err << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
@ -246,11 +248,11 @@ namespace dawn_native {
return {};
}
ResultOrError<std::vector<uint32_t>> ModuleToSPIRV(tint::ast::Module module) {
ResultOrError<std::vector<uint32_t>> ModuleToSPIRV(tint::Program program) {
std::ostringstream errorStream;
errorStream << "Tint SPIR-V writer failure:" << std::endl;
tint::writer::spirv::Generator generator(std::move(module));
tint::writer::spirv::Generator generator(&program);
if (!generator.Generate()) {
errorStream << "Generator: " << generator.error() << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
@ -691,14 +693,14 @@ namespace dawn_native {
// PopulateMetadataUsingSPIRVCross will be removed.
ResultOrError<EntryPointMetadataTable> ReflectShaderUsingTint(
DeviceBase*,
const tint::ast::Module& module) {
ASSERT(module.IsValid());
const tint::Program& program) {
ASSERT(program.IsValid());
EntryPointMetadataTable result;
std::ostringstream errorStream;
errorStream << "Tint Reflection failure:" << std::endl;
tint::inspector::Inspector inspector(module);
tint::inspector::Inspector inspector(&program);
auto entryPoints = inspector.GetEntryPoints();
if (inspector.has_error()) {
errorStream << "Inspector: " << inspector.error() << std::endl;
@ -857,12 +859,12 @@ namespace dawn_native {
std::vector<uint32_t> spirv(spirvDesc->code, spirvDesc->code + spirvDesc->codeSize);
if (device->IsToggleEnabled(Toggle::UseTintGenerator)) {
#ifdef DAWN_ENABLE_WGSL
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, ParseSPIRV(spirv));
tint::Program program;
DAWN_TRY_ASSIGN(program, ParseSPIRV(spirv));
if (device->IsValidationEnabled()) {
DAWN_TRY(ValidateModule(&module));
DAWN_TRY(ValidateModule(&program));
}
parseResult.tintModule = std::make_unique<tint::ast::Module>(std::move(module));
parseResult.tintProgram = std::make_unique<tint::Program>(std::move(program));
#else
return DAWN_VALIDATION_ERROR("Using Tint is not enabled in this build.");
#endif // DAWN_ENABLE_WGSL
@ -883,37 +885,37 @@ namespace dawn_native {
tint::Source::File file("", wgslDesc->source);
if (device->IsToggleEnabled(Toggle::UseTintGenerator)) {
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, ParseWGSL(&file));
tint::Program program;
DAWN_TRY_ASSIGN(program, ParseWGSL(&file));
if (device->IsValidationEnabled()) {
DAWN_TRY(ValidateModule(&module));
DAWN_TRY(ValidateModule(&program));
}
parseResult.tintModule = std::make_unique<tint::ast::Module>(std::move(module));
parseResult.tintProgram = std::make_unique<tint::Program>(std::move(program));
} else {
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, ParseWGSL(&file));
tint::Program program;
DAWN_TRY_ASSIGN(program, ParseWGSL(&file));
{
tint::transform::Manager transformManager;
transformManager.append(
std::make_unique<tint::transform::EmitVertexPointSize>());
DAWN_TRY_ASSIGN(module, RunTransforms(&transformManager, &module));
DAWN_TRY_ASSIGN(program, RunTransforms(&transformManager, &program));
}
if (device->IsValidationEnabled()) {
DAWN_TRY(ValidateModule(&module));
DAWN_TRY(ValidateModule(&program));
}
// Keep the Tint module around. The Metal backend will use it for vertex
// Keep the Tint program around. The Metal backend will use it for vertex
// pulling since we can't go WGSL->point size transform->spirv->Tint.
// Tint's spirv reader doesn't understand point size. crbug.com/tint/412.
auto tintModule = std::make_unique<tint::ast::Module>(module.Clone());
auto tintProgram = std::make_unique<tint::Program>(program.Clone());
std::vector<uint32_t> spirv;
DAWN_TRY_ASSIGN(spirv, ModuleToSPIRV(std::move(module)));
DAWN_TRY_ASSIGN(spirv, ModuleToSPIRV(std::move(program)));
DAWN_TRY(ValidateSpirv(spirv.data(), spirv.size()));
parseResult.tintModule = std::move(tintModule);
parseResult.tintProgram = std::move(tintProgram);
parseResult.spirv = std::move(spirv);
}
break;
@ -940,19 +942,19 @@ namespace dawn_native {
}
#ifdef DAWN_ENABLE_WGSL
ResultOrError<tint::ast::Module> RunTransforms(tint::transform::Manager* manager,
tint::ast::Module* module) {
tint::transform::Transform::Output output = manager->Run(module);
ResultOrError<tint::Program> RunTransforms(tint::transform::Manager* manager,
tint::Program* program) {
tint::transform::Transform::Output output = manager->Run(program);
if (output.diagnostics.contains_errors()) {
std::string err =
"Tint transform failure: " + tint::diag::Formatter{}.format(output.diagnostics);
return DAWN_VALIDATION_ERROR(err.c_str());
}
if (!output.module.IsValid()) {
return DAWN_VALIDATION_ERROR("Tint transform did not produce valid module.");
if (!output.program.IsValid()) {
return DAWN_VALIDATION_ERROR("Tint transform did not produce valid program.");
}
return std::move(output.module);
return std::move(output.program);
}
std::unique_ptr<tint::transform::VertexPulling> MakeVertexPullingTransform(
@ -1080,14 +1082,14 @@ namespace dawn_native {
const VertexStateDescriptor& vertexState,
const std::string& entryPoint,
BindGroupIndex pullingBufferBindingSet) const {
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, ParseSPIRV(spirv));
tint::Program program;
DAWN_TRY_ASSIGN(program, ParseSPIRV(spirv));
return GeneratePullingSpirv(&module, vertexState, entryPoint, pullingBufferBindingSet);
return GeneratePullingSpirv(&program, vertexState, entryPoint, pullingBufferBindingSet);
}
ResultOrError<std::vector<uint32_t>> ShaderModuleBase::GeneratePullingSpirv(
tint::ast::Module* moduleIn,
tint::Program* programIn,
const VertexStateDescriptor& vertexState,
const std::string& entryPoint,
BindGroupIndex pullingBufferBindingSet) const {
@ -1104,10 +1106,10 @@ namespace dawn_native {
// transformManager.append(std::make_unique<tint::transform::BoundArrayAccessors>());
}
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, RunTransforms(&transformManager, moduleIn));
tint::Program program;
DAWN_TRY_ASSIGN(program, RunTransforms(&transformManager, programIn));
tint::writer::spirv::Generator generator(std::move(module));
tint::writer::spirv::Generator generator(&program);
if (!generator.Generate()) {
errorStream << "Generator: " << generator.error() << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
@ -1124,7 +1126,7 @@ namespace dawn_native {
MaybeError ShaderModuleBase::InitializeBase(ShaderModuleParseResult* parseResult) {
#ifdef DAWN_ENABLE_WGSL
tint::ast::Module* module = parseResult->tintModule.get();
tint::Program* program = parseResult->tintProgram.get();
#endif
mSpirv = std::move(parseResult->spirv);
@ -1142,13 +1144,15 @@ namespace dawn_native {
std::vector<uint32_t> localSpirv;
if (GetDevice()->IsToggleEnabled(Toggle::UseTintGenerator)) {
#ifdef DAWN_ENABLE_WGSL
ASSERT(module != nullptr);
tint::ast::Module clonedModule = module->Clone();
tint::TypeDeterminer typeDeterminer(&clonedModule);
if (!typeDeterminer.Determine()) {
return DAWN_VALIDATION_ERROR(typeDeterminer.error().c_str());
ASSERT(program != nullptr);
tint::Program clonedProgram = program->Clone();
tint::diag::List diagnostics = tint::TypeDeterminer::Run(&clonedProgram);
if (diagnostics.contains_errors()) {
std::string err = tint::diag::Formatter{}.format(diagnostics);
return DAWN_VALIDATION_ERROR(err.c_str());
}
DAWN_TRY_ASSIGN(localSpirv, ModuleToSPIRV(std::move(clonedModule)));
DAWN_TRY_ASSIGN(localSpirv, ModuleToSPIRV(std::move(clonedProgram)));
DAWN_TRY(ValidateSpirv(localSpirv.data(), localSpirv.size()));
spirvPtr = &localSpirv;
#else
@ -1158,18 +1162,18 @@ namespace dawn_native {
if (GetDevice()->IsToggleEnabled(Toggle::UseTintInspector)) {
#ifdef DAWN_ENABLE_WGSL
tint::ast::Module localModule;
tint::Program localProgram;
tint::ast::Module* modulePtr = module;
tint::Program* programPtr = program;
if (!GetDevice()->IsToggleEnabled(Toggle::UseTintGenerator)) {
// We have mSpirv, but no Tint module
DAWN_TRY_ASSIGN(localModule, ParseSPIRV(mSpirv));
DAWN_TRY(ValidateModule(&localModule));
modulePtr = &localModule;
// We have mSpirv, but no Tint program
DAWN_TRY_ASSIGN(localProgram, ParseSPIRV(mSpirv));
DAWN_TRY(ValidateModule(&localProgram));
programPtr = &localProgram;
}
EntryPointMetadataTable table;
DAWN_TRY_ASSIGN(table, ReflectShaderUsingTint(GetDevice(), *modulePtr));
DAWN_TRY_ASSIGN(table, ReflectShaderUsingTint(GetDevice(), *programPtr));
DAWN_TRY(PopulateMetadataUsingSPIRVCross(GetDevice(), *spirvPtr, &table));
mEntryPoints = std::move(table);
#else

View File

@ -33,9 +33,7 @@
namespace tint {
namespace ast {
class Module;
} // namespace ast
class Program;
namespace transform {
class Manager;
@ -63,7 +61,7 @@ namespace dawn_native {
ShaderModuleParseResult& operator=(ShaderModuleParseResult&& rhs);
#ifdef DAWN_ENABLE_WGSL
std::unique_ptr<tint::ast::Module> tintModule;
std::unique_ptr<tint::Program> tintProgram;
#endif
std::vector<uint32_t> spirv;
};
@ -78,8 +76,8 @@ namespace dawn_native {
RequiredBufferSizes ComputeRequiredBufferSizesForLayout(const EntryPointMetadata& entryPoint,
const PipelineLayoutBase* layout);
#ifdef DAWN_ENABLE_WGSL
ResultOrError<tint::ast::Module> RunTransforms(tint::transform::Manager* manager,
tint::ast::Module* module);
ResultOrError<tint::Program> RunTransforms(tint::transform::Manager* manager,
tint::Program* program);
std::unique_ptr<tint::transform::VertexPulling> MakeVertexPullingTransform(
const VertexStateDescriptor& vertexState,
@ -88,7 +86,7 @@ namespace dawn_native {
#endif
// Contains all the reflection data for a valid (ShaderModule, entryPoint, stage). They are
// stored in the ShaderModuleBase and destroyed only when the shader module is destroyed so
// stored in the ShaderModuleBase and destroyed only when the shader program is destroyed so
// pointers to EntryPointMetadata are safe to store as long as you also keep a Ref to the
// ShaderModuleBase.
struct EntryPointMetadata {
@ -132,7 +130,7 @@ namespace dawn_native {
static ShaderModuleBase* MakeError(DeviceBase* device);
// Return true iff the module has an entrypoint called `entryPoint`.
// Return true iff the program has an entrypoint called `entryPoint`.
bool HasEntryPoint(const std::string& entryPoint) const;
// Returns the metadata for the given `entryPoint`. HasEntryPoint with the same argument
@ -156,7 +154,7 @@ namespace dawn_native {
BindGroupIndex pullingBufferBindingSet) const;
ResultOrError<std::vector<uint32_t>> GeneratePullingSpirv(
tint::ast::Module* module,
tint::Program* program,
const VertexStateDescriptor& vertexState,
const std::string& entryPoint,
BindGroupIndex pullingBufferBindingSet) const;

View File

@ -188,7 +188,7 @@ namespace dawn_native { namespace d3d12 {
MaybeError ShaderModule::Initialize(ShaderModuleParseResult* parseResult) {
DAWN_TRY(InitializeBase(parseResult));
#ifdef DAWN_ENABLE_WGSL
mTintModule = std::move(parseResult->tintModule);
mTintProgram = std::move(parseResult->tintProgram);
#endif
return {};
}
@ -217,8 +217,8 @@ namespace dawn_native { namespace d3d12 {
transformManager.append(std::move(transformer));
}
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, RunTransforms(&transformManager, mTintModule.get()));
tint::Program program;
DAWN_TRY_ASSIGN(program, RunTransforms(&transformManager, mTintProgram.get()));
if (firstOffsetTransform != nullptr) {
// Functions are only available after transform has been performed
@ -235,10 +235,10 @@ namespace dawn_native { namespace d3d12 {
}
ASSERT(remappedEntryPointName != nullptr);
tint::inspector::Inspector inspector(module);
tint::inspector::Inspector inspector(&program);
*remappedEntryPointName = inspector.GetRemappedNameForEntryPoint(entryPointName);
tint::writer::hlsl::Generator generator(std::move(module));
tint::writer::hlsl::Generator generator(&program);
// TODO: Switch to GenerateEntryPoint once HLSL writer supports it.
if (!generator.Generate()) {
errorStream << "Generator: " << generator.error() << std::endl;

View File

@ -78,7 +78,7 @@ namespace dawn_native { namespace d3d12 {
uint64_t GetD3DCompilerVersion() const;
#ifdef DAWN_ENABLE_WGSL
std::unique_ptr<tint::ast::Module> mTintModule;
std::unique_ptr<tint::Program> mTintProgram;
#endif
};

View File

@ -71,7 +71,7 @@ namespace dawn_native { namespace metal {
MaybeError Initialize(ShaderModuleParseResult* parseResult);
#ifdef DAWN_ENABLE_WGSL
std::unique_ptr<tint::ast::Module> mTintModule;
std::unique_ptr<tint::Program> mTintProgram;
#endif
};

View File

@ -50,7 +50,7 @@ namespace dawn_native { namespace metal {
MaybeError ShaderModule::Initialize(ShaderModuleParseResult* parseResult) {
DAWN_TRY(InitializeBase(parseResult));
#ifdef DAWN_ENABLE_WGSL
mTintModule = std::move(parseResult->tintModule);
mTintProgram = std::move(parseResult->tintProgram);
#endif
return {};
}
@ -88,14 +88,14 @@ namespace dawn_native { namespace metal {
}
transformManager.append(std::make_unique<tint::transform::BoundArrayAccessors>());
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, RunTransforms(&transformManager, mTintModule.get()));
tint::Program program;
DAWN_TRY_ASSIGN(program, RunTransforms(&transformManager, mTintProgram.get()));
ASSERT(remappedEntryPointName != nullptr);
tint::inspector::Inspector inspector(module);
tint::inspector::Inspector inspector(&program);
*remappedEntryPointName = inspector.GetRemappedNameForEntryPoint(entryPointName);
tint::writer::msl::Generator generator(std::move(module));
tint::writer::msl::Generator generator(&program);
if (!generator.Generate()) {
errorStream << "Generator: " << generator.error() << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
@ -123,9 +123,9 @@ namespace dawn_native { namespace metal {
std::vector<uint32_t> pullingSpirv;
if (GetDevice()->IsToggleEnabled(Toggle::MetalEnableVertexPulling) &&
stage == SingleShaderStage::Vertex) {
if (mTintModule) {
if (mTintProgram) {
DAWN_TRY_ASSIGN(pullingSpirv,
GeneratePullingSpirv(mTintModule.get(),
GeneratePullingSpirv(mTintProgram.get(),
*renderPipeline->GetVertexStateDescriptor(),
entryPointName, kPullingBufferBindingSet));
} else {

View File

@ -82,7 +82,7 @@ namespace dawn_native { namespace opengl {
DAWN_TRY(InitializeBase(parseResult));
if (GetDevice()->IsToggleEnabled(Toggle::UseTintGenerator)) {
#ifdef DAWN_ENABLE_WGSL
tint::ast::Module module = std::move(*parseResult->tintModule.release());
tint::Program program = std::move(*parseResult->tintProgram.release());
std::ostringstream errorStream;
errorStream << "Tint SPIR-V (for GLSL) writer failure:" << std::endl;
@ -91,9 +91,9 @@ namespace dawn_native { namespace opengl {
transformManager.append(std::make_unique<tint::transform::BoundArrayAccessors>());
transformManager.append(std::make_unique<tint::transform::EmitVertexPointSize>());
DAWN_TRY_ASSIGN(module, RunTransforms(&transformManager, &module));
DAWN_TRY_ASSIGN(program, RunTransforms(&transformManager, &program));
tint::writer::spirv::Generator generator(std::move(module));
tint::writer::spirv::Generator generator(&program);
if (!generator.Generate()) {
errorStream << "Generator: " << generator.error() << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());

View File

@ -53,7 +53,7 @@ namespace dawn_native { namespace vulkan {
const std::vector<uint32_t>* spirvPtr;
if (GetDevice()->IsToggleEnabled(Toggle::UseTintGenerator)) {
#ifdef DAWN_ENABLE_WGSL
tint::ast::Module module = std::move(*std::move(parseResult->tintModule).release());
tint::Program program = std::move(*std::move(parseResult->tintProgram).release());
std::ostringstream errorStream;
errorStream << "Tint SPIR-V writer failure:" << std::endl;
@ -62,9 +62,9 @@ namespace dawn_native { namespace vulkan {
transformManager.append(std::make_unique<tint::transform::BoundArrayAccessors>());
transformManager.append(std::make_unique<tint::transform::EmitVertexPointSize>());
DAWN_TRY_ASSIGN(module, RunTransforms(&transformManager, &module));
DAWN_TRY_ASSIGN(program, RunTransforms(&transformManager, &program));
tint::writer::spirv::Generator generator(std::move(module));
tint::writer::spirv::Generator generator(&program);
if (!generator.Generate()) {
errorStream << "Generator: " << generator.error() << std::endl;
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());

View File

@ -79,17 +79,17 @@ namespace DawnSPIRVCrossFuzzer {
return 0;
}
tint::ast::Module module = parser.module();
if (!module.IsValid()) {
tint::Program program = parser.program();
if (!program.IsValid()) {
return 0;
}
tint::TypeDeterminer type_determiner(&module);
tint::TypeDeterminer type_determiner(&program);
if (!type_determiner.Determine()) {
return 0;
}
tint::writer::spirv::Generator generator(std::move(module));
tint::writer::spirv::Generator generator(&program);
if (!generator.Generate()) {
return 0;
}