tint: Implement uniformity analaysis

This implements the uniformity analysis as currently described in the
WGSL specification. Uniformity issues are presented as warnings, and
will be switched to errors in a future release.

A follow-up patch will improve the error messages, which currently
just show the point at which a uniformity was detected.

In a future release, once we have obtained initial feedback from
users, uniformity issues will become errors.

Bug: tint:880
Change-Id: I7d0b3080932c786c5d50b55720fec6d19f00d356
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/88368
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Baker <alanbaker@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
James Price
2022-05-11 22:05:15 +00:00
parent 3683c46b57
commit be656f7984
41 changed files with 7423 additions and 0 deletions

View File

@@ -375,6 +375,8 @@ libtint_source_set("libtint_core_all_src") {
"resolver/resolver_constants.cc",
"resolver/sem_helper.cc",
"resolver/sem_helper.h",
"resolver/uniformity.cc",
"resolver/uniformity.h",
"resolver/validator.cc",
"resolver/validator.h",
"scope_stack.h",

View File

@@ -256,6 +256,8 @@ set(TINT_LIB_SRCS
resolver/resolver.h
resolver/sem_helper.cc
resolver/sem_helper.h
resolver/uniformity.cc
resolver/uniformity.h
resolver/validator.cc
resolver/validator.h
scope_stack.h
@@ -833,6 +835,13 @@ if(TINT_BUILD_TESTS)
writer/text_generator_test.cc
)
# Uniformity analysis tests depend on WGSL reader
if(${TINT_BUILD_WGSL_READER})
list(APPEND TINT_TEST_SRCS
resolver/uniformity_test.cc
)
endif()
# Inspector tests depend on WGSL reader
if(${TINT_BUILD_WGSL_READER})
list(APPEND TINT_TEST_SRCS

View File

@@ -50,6 +50,7 @@
#include "src/tint/ast/variable_decl_statement.h"
#include "src/tint/ast/vector.h"
#include "src/tint/ast/workgroup_attribute.h"
#include "src/tint/resolver/uniformity.h"
#include "src/tint/sem/array.h"
#include "src/tint/sem/atomic.h"
#include "src/tint/sem/call.h"
@@ -145,6 +146,10 @@ bool Resolver::ResolveInternal() {
return false;
}
if (!AnalyzeUniformity(builder_, dependencies_)) {
// TODO(jrprice): Reject programs that fail uniformity analysis.
}
bool result = true;
for (auto* node : builder_->ASTNodes().Objects()) {
if (marked_.count(node) == 0) {

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,36 @@
// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_TINT_RESOLVER_UNIFORMITY_H_
#define SRC_TINT_RESOLVER_UNIFORMITY_H_
// Forward declarations.
namespace tint {
namespace resolver {
struct DependencyGraph;
} // namespace resolver
class ProgramBuilder;
} // namespace tint
namespace tint::resolver {
/// Analyze the uniformity of a program.
/// @param builder the program to analyze
/// @param dependency_graph the dependency-ordered module-scope declarations
/// @returns true if there are no uniformity issues, false otherwise
bool AnalyzeUniformity(ProgramBuilder* builder, const resolver::DependencyGraph& dependency_graph);
} // namespace tint::resolver
#endif // SRC_TINT_RESOLVER_UNIFORMITY_H_

File diff suppressed because it is too large Load Diff

View File

@@ -71,6 +71,16 @@ class ScopeStack {
return V{};
}
/// Return the top scope of the stack.
/// @returns the top scope of the stack
const std::unordered_map<K, V>& Top() const { return stack_.back(); }
/// Clear the scope stack.
void Clear() {
stack_.clear();
stack_.push_back({});
}
private:
std::vector<std::unordered_map<K, V>> stack_;
};

View File

@@ -67,5 +67,25 @@ TEST_F(ScopeStackTest, Set) {
EXPECT_EQ(s.Get(b), 25u);
}
TEST_F(ScopeStackTest, Clear) {
ScopeStack<Symbol, uint32_t> s;
Symbol a(1, ID());
Symbol b(2, ID());
EXPECT_EQ(s.Set(a, 5u), 0u);
EXPECT_EQ(s.Get(a), 5u);
s.Push();
EXPECT_EQ(s.Set(b, 10u), 0u);
EXPECT_EQ(s.Get(b), 10u);
s.Push();
s.Clear();
EXPECT_EQ(s.Get(a), 0u);
EXPECT_EQ(s.Get(b), 0u);
}
} // namespace
} // namespace tint