Implement Pointers and References

This change implements pointers and references as described by the WGSL
specification change in https://github.com/gpuweb/gpuweb/pull/1569.

reader/spirv:
* Now emits address-of `&expr` and indirection `*expr` operators as
  needed.
* As an identifier may now resolve to a pointer or reference type
  depending on whether the declaration is a `var`, `let` or
  parameter, `Function::identifier_values_` has been changed from
  an ID set to an ID -> Type* map.

resolver:
* Now correctly resolves all expressions to either a value type,
  reference type or pointer type.
* Validates pointer / reference rules on assignment, `var` and `let`
  construction, and usage.
* Handles the address-of and indirection operators.
* No longer does any implicit loads of pointer types.
* Storage class validation is still TODO (crbug.com/tint/809)

writer/spirv:
* Correctly handles variables and expressions of pointer and
  reference types, emitting OpLoads where necessary.

test:
* Lots of new test cases

Fixed: tint:727
Change-Id: I77d3281590e35e5a3122f5b74cdeb71a6fe51f74
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50740
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2021-05-18 10:28:48 +00:00
committed by Commit Bot service account
parent d1232670ae
commit 9b54a2e53c
192 changed files with 6289 additions and 1680 deletions

View File

@@ -139,7 +139,7 @@ Function::VariableBindings Function::ReferencedStorageTextureVariables() const {
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Type()->UnwrapAccess();
auto* unwrapped_type = var->Type()->UnwrapRef();
auto* storage_texture = unwrapped_type->As<sem::StorageTexture>();
if (storage_texture == nullptr) {
continue;
@@ -156,7 +156,7 @@ Function::VariableBindings Function::ReferencedDepthTextureVariables() const {
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Type()->UnwrapAccess();
auto* unwrapped_type = var->Type()->UnwrapRef();
auto* storage_texture = unwrapped_type->As<sem::DepthTexture>();
if (storage_texture == nullptr) {
continue;
@@ -174,7 +174,7 @@ Function::VariableBindings Function::ReferencedExternalTextureVariables()
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Type()->UnwrapAccess();
auto* unwrapped_type = var->Type()->UnwrapRef();
auto* external_texture = unwrapped_type->As<sem::ExternalTexture>();
if (external_texture == nullptr) {
continue;
@@ -201,7 +201,7 @@ Function::VariableBindings Function::ReferencedSamplerVariablesImpl(
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Type()->UnwrapAccess();
auto* unwrapped_type = var->Type()->UnwrapRef();
auto* sampler = unwrapped_type->As<sem::Sampler>();
if (sampler == nullptr || sampler->kind() != kind) {
continue;
@@ -219,7 +219,7 @@ Function::VariableBindings Function::ReferencedSampledTextureVariablesImpl(
VariableBindings ret;
for (auto* var : ReferencedModuleVariables()) {
auto* unwrapped_type = var->Type()->UnwrapAccess();
auto* unwrapped_type = var->Type()->UnwrapRef();
auto* texture = unwrapped_type->As<sem::Texture>();
if (texture == nullptr) {
continue;

View File

@@ -19,6 +19,7 @@
#include "src/sem/i32_type.h"
#include "src/sem/matrix_type.h"
#include "src/sem/pointer_type.h"
#include "src/sem/reference_type.h"
#include "src/sem/sampler_type.h"
#include "src/sem/texture_type.h"
#include "src/sem/u32_type.h"
@@ -43,21 +44,17 @@ const Type* Type::UnwrapPtr() const {
return type;
}
const Type* Type::UnwrapAccess() const {
// TODO(amaiorano): Delete this function
const Type* Type::UnwrapRef() const {
auto* type = this;
if (auto* ref = type->As<sem::Reference>()) {
type = ref->StoreType();
}
return type;
}
const Type* Type::UnwrapAll() const {
const Type* Type::UnwrapAccess() const {
// TODO(amaiorano): Delete this function
auto* type = this;
while (true) {
if (auto* ptr = type->As<sem::Pointer>()) {
type = ptr->StoreType();
} else {
break;
}
}
return type;
}

View File

@@ -49,15 +49,13 @@ class Type : public Castable<Type, Node> {
/// otherwise
const Type* UnwrapPtr() const;
/// @returns the inner type if this is a reference, `this` otherwise
const Type* UnwrapRef() const;
/// @returns the inner most type if this is an access control, `this`
/// otherwise
const Type* UnwrapAccess() const;
/// Returns the type found after removing all layers of access control and
/// pointer
/// @returns the unwrapped type
const Type* UnwrapAll() const;
/// @returns true if this type is a scalar
bool is_scalar() const;
/// @returns true if this type is a numeric scalar