resolver: Allow parameters to shadow globals

In https://dawn-review.googlesource.com/c/tint/+/62444 the Resolver validated that there are no parameters of the same function with the same name, but this also introduced validation that errors if parameters shadow a module-scope variable.

The WGSL spec allows for shadowing, but Tint so far has not implemented this support.

There are transforms that generate functions that presume parameter <-> module-scope variable shadowing is okay. DecomposeMemoryAccess is one of these.

This fixes those transforms which could generate programs that fail validation.

Bug: chromium:1242330
Fixed: tint:1136
Change-Id: Id6ec59bbdb398b3b2a23312115a7c1dadf433e98
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/62900
Reviewed-by: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2021-08-26 15:40:06 +00:00
committed by Tint LUCI CQ
parent f9d19719fd
commit 9021eb5594
7 changed files with 95 additions and 3 deletions

View File

@@ -58,7 +58,7 @@ TEST_F(ResolverFunctionValidationTest, ParameterNamesMustBeUnique_fail) {
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
R"(12:34 error: redefinition of 'common_name'
R"(12:34 error: redefinition of parameter 'common_name'
56:78 note: previous definition is here)");
}
@@ -72,6 +72,17 @@ TEST_F(ResolverFunctionValidationTest, ParameterNamesMustBeUnique_pass) {
EXPECT_EQ(r()->error(), "");
}
TEST_F(ResolverFunctionValidationTest,
ParameterNamesMustBeUniqueShadowsGlobal_pass) {
// var<private> common_name : f32;
// fn func(common_name : f32) { }
Global("common_name", ty.f32(), ast::StorageClass::kPrivate);
Func("func", {Param("common_name", ty.f32())}, ty.void_(), {});
EXPECT_TRUE(r()->Resolve());
EXPECT_EQ(r()->error(), "");
}
TEST_F(ResolverFunctionValidationTest,
VoidFunctionEndWithoutReturnStatement_Pass) {
// fn func { var a:i32 = 2; }

View File

@@ -1696,11 +1696,18 @@ bool Resolver::Function(ast::Function* func) {
variable_stack_.push_scope();
uint32_t parameter_index = 0;
std::unordered_map<Symbol, Source> parameter_names;
for (auto* param : func->params()) {
Mark(param);
if (!ValidateNoDuplicateDefinition(param->symbol(), param->source())) {
return false;
{ // Check the parameter name is unique for the function
auto emplaced = parameter_names.emplace(param->symbol(), param->source());
if (!emplaced.second) {
auto name = builder_->Symbols().NameFor(param->symbol());
AddError("redefinition of parameter '" + name + "'", param->source());
AddNote("previous definition is here", emplaced.first->second);
return false;
}
}
auto* param_info =