Migrate from using ast::Module to Program

Enforce all places where Dawn passes in or returns a ast::Module, now takes a `const Program* ` or returns a `Program`.

As the end goal of all this is to have immutable Programs, all Program inputs take a pointer instead of moving the actual object.

As consumers of a Program are now all const, we have to const_cast to work around all the places we've been incorrectly mutating a ast::Module.
These const_casts are temporary, and will be fixed in the next set of changes.

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

Bug: tint:390
Change-Id: Ie05b112b16134937d1b601e9b713ea4ec4e1c677
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38541
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2021-01-26 16:57:10 +00:00
parent be610ba987
commit c40f627bea
252 changed files with 1978 additions and 2017 deletions

View File

@@ -47,15 +47,15 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
tint::Source::File file("test.wgsl", str);
// Parse the wgsl, create the src module
// Parse the wgsl, create the src program
tint::reader::wgsl::ParserImpl parser(&file);
parser.set_max_errors(1);
if (!parser.Parse()) {
return 0;
}
auto src = parser.module();
auto src = parser.program();
// Clone the src module to dst
// Clone the src program to dst
auto dst = src.Clone();
// Expect the demangled AST printed with to_str() to match
@@ -78,24 +78,25 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
ASSERT_EQ(src_types.count(dst_type.second), 0u);
}
// Regenerate the wgsl for the src module. We use this instead of the original
// source so that reformatting doesn't impact the final wgsl comparision.
// Note that the src module is moved into the generator and this generator has
// a limited scope, so that the src module is released before we attempt to
// print the dst module.
// This guarantee that all the source module nodes and types are destructed
// and freed.
// ASAN should error if there's any remaining references in dst when we try to
// reconstruct the WGSL.
// Regenerate the wgsl for the src program. We use this instead of the
// original source so that reformatting doesn't impact the final wgsl
// comparison.
std::string src_wgsl;
{
tint::writer::wgsl::Generator src_gen(std::move(src));
tint::writer::wgsl::Generator src_gen(&src);
ASSERT_TRUE(src_gen.Generate());
src_wgsl = src_gen.result();
// Move the src program to a temporary that'll be dropped, so that the src
// program is released before we attempt to print the dst program. This
// guarantee that all the source program nodes and types are destructed and
// freed. ASAN should error if there's any remaining references in dst when
// we try to reconstruct the WGSL.
auto tmp = std::move(src);
}
// Print the dst module, check it matches the original source
tint::writer::wgsl::Generator dst_gen(std::move(dst));
// Print the dst program, check it matches the original source
tint::writer::wgsl::Generator dst_gen(&dst);
ASSERT_TRUE(dst_gen.Generate());
auto dst_wgsl = dst_gen.result();
ASSERT_EQ(src_wgsl, dst_wgsl);

View File

@@ -76,23 +76,23 @@ int CommonFuzzer::Run(const uint8_t* data, size_t size) {
return 0;
}
auto mod = parser->module();
if (!mod.IsValid()) {
auto program = parser->program();
if (!program.IsValid()) {
return 0;
}
TypeDeterminer td(&mod);
TypeDeterminer td(&program);
if (!td.Determine()) {
return 0;
}
Validator v;
if (!v.Validate(&mod)) {
if (!v.Validate(&program)) {
return 0;
}
if (inspector_enabled_) {
inspector::Inspector inspector(mod);
inspector::Inspector inspector(&program);
auto entry_points = inspector.GetEntryPoints();
if (inspector.has_error()) {
@@ -154,12 +154,12 @@ int CommonFuzzer::Run(const uint8_t* data, size_t size) {
}
if (transform_manager_) {
auto out = transform_manager_->Run(&mod);
auto out = transform_manager_->Run(&program);
if (out.diagnostics.contains_errors()) {
return 0;
}
mod = std::move(out.module);
program = std::move(out.program);
}
std::unique_ptr<writer::Writer> writer;
@@ -167,22 +167,22 @@ int CommonFuzzer::Run(const uint8_t* data, size_t size) {
switch (output_) {
case OutputFormat::kWGSL:
#if TINT_BUILD_WGSL_WRITER
writer = std::make_unique<writer::wgsl::Generator>(std::move(mod));
writer = std::make_unique<writer::wgsl::Generator>(&program);
#endif // TINT_BUILD_WGSL_WRITER
break;
case OutputFormat::kSpv:
#if TINT_BUILD_SPV_WRITER
writer = std::make_unique<writer::spirv::Generator>(std::move(mod));
writer = std::make_unique<writer::spirv::Generator>(&program);
#endif // TINT_BUILD_SPV_WRITER
break;
case OutputFormat::kHLSL:
#if TINT_BUILD_HLSL_WRITER
writer = std::make_unique<writer::hlsl::Generator>(std::move(mod));
writer = std::make_unique<writer::hlsl::Generator>(&program);
#endif // TINT_BUILD_HLSL_WRITER
break;
case OutputFormat::kMSL:
#if TINT_BUILD_MSL_WRITER
writer = std::make_unique<writer::msl::Generator>(std::move(mod));
writer = std::make_unique<writer::msl::Generator>(&program);
#endif // TINT_BUILD_MSL_WRITER
break;
case OutputFormat::kNone: