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

@@ -0,0 +1,29 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 15
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %I "I"
OpName %main "main"
%int = OpTypeInt 32 1
%_ptr_Private_int = OpTypePointer Private %int
%4 = OpConstantNull %int
%I = OpVariable %_ptr_Private_int Private %4
%void = OpTypeVoid
%5 = OpTypeFunction %void
%int_123 = OpConstant %int 123
%int_100 = OpConstant %int 100
%int_20 = OpConstant %int 20
%int_3 = OpConstant %int 3
%main = OpFunction %void None %5
%8 = OpLabel
OpStore %I %int_123
%12 = OpIAdd %int %int_100 %int_20
%14 = OpIAdd %int %12 %int_3
OpStore %I %14
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,9 @@
int I = 0;
[numthreads(1, 1, 1)]
void main() {
I = 123;
I = ((100 + 20) + 3);
return;
}

View File

@@ -0,0 +1 @@
SKIP: TINT_UNIMPLEMENTED crbug.com/tint/726: module-scope private and workgroup variables not yet implemented

View File

@@ -0,0 +1,29 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 15
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %I "I"
OpName %main "main"
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Private_int = OpTypePointer Private %int
%I = OpVariable %_ptr_Private_int Private %int_0
%void = OpTypeVoid
%5 = OpTypeFunction %void
%int_123 = OpConstant %int 123
%int_100 = OpConstant %int 100
%int_20 = OpConstant %int 20
%int_3 = OpConstant %int 3
%main = OpFunction %void None %5
%8 = OpLabel
OpStore %I %int_123
%12 = OpIAdd %int %int_100 %int_20
%14 = OpIAdd %int %12 %int_3
OpStore %I %14
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,8 @@
var<private> I : i32 = 0;
[[stage(compute)]]
fn main() {
I = 123;
I = ((100 + 20) + 3);
return;
}

View File

@@ -0,0 +1,7 @@
var<private> I : i32;
[[stage(compute)]]
fn main() {
I = 123; // constant
I = 100 + 20 + 3; // dynamic
}

View File

@@ -0,0 +1,9 @@
int I;
[numthreads(1, 1, 1)]
void main() {
I = 123;
I = ((100 + 20) + 3);
return;
}

View File

@@ -0,0 +1 @@
SKIP: TINT_UNIMPLEMENTED crbug.com/tint/726: module-scope private and workgroup variables not yet implemented

View File

@@ -0,0 +1,29 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 15
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %I "I"
OpName %main "main"
%int = OpTypeInt 32 1
%_ptr_Private_int = OpTypePointer Private %int
%4 = OpConstantNull %int
%I = OpVariable %_ptr_Private_int Private %4
%void = OpTypeVoid
%5 = OpTypeFunction %void
%int_123 = OpConstant %int 123
%int_100 = OpConstant %int 100
%int_20 = OpConstant %int 20
%int_3 = OpConstant %int 3
%main = OpFunction %void None %5
%8 = OpLabel
OpStore %I %int_123
%12 = OpIAdd %int %int_100 %int_20
%14 = OpIAdd %int %12 %int_3
OpStore %I %14
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,7 @@
var<private> I : i32;
[[stage(compute)]]
fn main() {
I = 123;
I = ((100 + 20) + 3);
}

View File

@@ -0,0 +1,30 @@
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpSource GLSL 450
OpName %main "main"
OpName %S "S"
OpMemberName %S 0 "i"
OpName %V "V"
OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize
%void = OpTypeVoid
%3 = OpTypeFunction %void
%int = OpTypeInt 32 1
%S = OpTypeStruct %int
%_ptr_Private_S = OpTypePointer Private %S
%V = OpVariable %_ptr_Private_S Private
%int_0 = OpConstant %int 0
%int_5 = OpConstant %int 5
%_ptr_Private_int = OpTypePointer Private %int
%uint = OpTypeInt 32 0
%v3uint = OpTypeVector %uint 3
%uint_1 = OpConstant %uint 1
%gl_WorkGroupSize = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_1
%main = OpFunction %void None %3
%5 = OpLabel
%13 = OpAccessChain %_ptr_Private_int %V %int_0
OpStore %13 %int_5
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1 @@
SKIP: error: pointers not supported in HLSL

View File

@@ -0,0 +1 @@
SKIP: TINT_UNIMPLEMENTED crbug.com/tint/726: module-scope private and workgroup variables not yet implemented

View File

@@ -0,0 +1,31 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 15
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %S "S"
OpMemberName %S 0 "i"
OpName %V "V"
OpName %main "main"
OpMemberDecorate %S 0 Offset 0
%int = OpTypeInt 32 1
%S = OpTypeStruct %int
%_ptr_Private_S = OpTypePointer Private %S
%5 = OpConstantNull %S
%V = OpVariable %_ptr_Private_S Private %5
%void = OpTypeVoid
%6 = OpTypeFunction %void
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%_ptr_Private_int = OpTypePointer Private %int
%int_5 = OpConstant %int 5
%main = OpFunction %void None %6
%9 = OpLabel
%13 = OpAccessChain %_ptr_Private_int %V %uint_0
OpStore %13 %int_5
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,11 @@
struct S {
i : i32;
};
var<private> V : S;
[[stage(compute)]]
fn main() {
V.i = 5;
return;
}

View File

@@ -0,0 +1,30 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 18
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %main "main"
OpName %i "i"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%int_123 = OpConstant %int 123
%_ptr_Function_int = OpTypePointer Function %int
%9 = OpConstantNull %int
%int_100 = OpConstant %int 100
%int_20 = OpConstant %int 20
%int_3 = OpConstant %int 3
%main = OpFunction %void None %1
%4 = OpLabel
%i = OpVariable %_ptr_Function_int Function %9
OpStore %i %int_123
OpStore %i %int_123
%15 = OpIAdd %int %int_100 %int_20
%17 = OpIAdd %int %15 %int_3
OpStore %i %17
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,9 @@
[numthreads(1, 1, 1)]
void main() {
int i = 0;
i = 123;
i = 123;
i = ((100 + 20) + 3);
return;
}

View File

@@ -0,0 +1,11 @@
#include <metal_stdlib>
using namespace metal;
kernel void tint_symbol() {
int i = 0;
i = 123;
i = 123;
i = ((100 + 20) + 3);
return;
}

View File

@@ -0,0 +1,32 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 16
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %main "main"
OpName %i "i"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Function_int = OpTypePointer Function %int
%9 = OpConstantNull %int
%int_123 = OpConstant %int 123
%int_100 = OpConstant %int 100
%int_20 = OpConstant %int 20
%int_3 = OpConstant %int 3
%main = OpFunction %void None %1
%4 = OpLabel
%i = OpVariable %_ptr_Function_int Function %9
OpStore %i %int_0
OpStore %i %int_123
OpStore %i %int_123
%13 = OpIAdd %int %int_100 %int_20
%15 = OpIAdd %int %13 %int_3
OpStore %i %15
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,8 @@
[[stage(compute)]]
fn main() {
var i : i32 = 0;
i = 123;
i = 123;
i = ((100 + 20) + 3);
return;
}

View File

@@ -0,0 +1,7 @@
[[stage(compute)]]
fn main() {
var i : i32 = 123;
let p : ptr<function, i32> = &i;
*p = 123; // constant
*p = 100 + 20 + 3; // dynamic
}

View File

@@ -0,0 +1 @@
SKIP: error: pointers not supported in HLSL

View File

@@ -0,0 +1,11 @@
#include <metal_stdlib>
using namespace metal;
kernel void tint_symbol() {
int i = 123;
int* const p = &(i);
*(p) = 123;
*(p) = ((100 + 20) + 3);
return;
}

View File

@@ -0,0 +1,30 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 18
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %main "main"
OpName %i "i"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%int_123 = OpConstant %int 123
%_ptr_Function_int = OpTypePointer Function %int
%9 = OpConstantNull %int
%int_100 = OpConstant %int 100
%int_20 = OpConstant %int 20
%int_3 = OpConstant %int 3
%main = OpFunction %void None %1
%4 = OpLabel
%i = OpVariable %_ptr_Function_int Function %9
OpStore %i %int_123
OpStore %i %int_123
%15 = OpIAdd %int %int_100 %int_20
%17 = OpIAdd %int %15 %int_3
OpStore %i %17
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,7 @@
[[stage(compute)]]
fn main() {
var i : i32 = 123;
let p : ptr<function, i32> = &(i);
*(p) = 123;
*(p) = ((100 + 20) + 3);
}

View File

@@ -0,0 +1,30 @@
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpSource GLSL 450
OpName %main "main"
OpName %S "S"
OpMemberName %S 0 "i"
OpName %V "V"
OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize
%void = OpTypeVoid
%3 = OpTypeFunction %void
%int = OpTypeInt 32 1
%S = OpTypeStruct %int
%_ptr_Function_S = OpTypePointer Function %S
%int_0 = OpConstant %int 0
%int_5 = OpConstant %int 5
%_ptr_Function_int = OpTypePointer Function %int
%uint = OpTypeInt 32 0
%v3uint = OpTypeVector %uint 3
%uint_1 = OpConstant %uint 1
%gl_WorkGroupSize = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_1
%main = OpFunction %void None %3
%5 = OpLabel
%V = OpVariable %_ptr_Function_S Function
%13 = OpAccessChain %_ptr_Function_int %V %int_0
OpStore %13 %int_5
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,11 @@
struct S {
int i;
};
[numthreads(1, 1, 1)]
void main() {
S V = {0};
V.i = 5;
return;
}

View File

@@ -0,0 +1,13 @@
#include <metal_stdlib>
using namespace metal;
struct S {
int i;
};
kernel void tint_symbol() {
S V = {};
V.i = 5;
return;
}

View File

@@ -0,0 +1,31 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 15
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %main "main"
OpName %S "S"
OpMemberName %S 0 "i"
OpName %V "V"
OpMemberDecorate %S 0 Offset 0
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%S = OpTypeStruct %int
%_ptr_Function_S = OpTypePointer Function %S
%9 = OpConstantNull %S
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%_ptr_Function_int = OpTypePointer Function %int
%int_5 = OpConstant %int 5
%main = OpFunction %void None %1
%4 = OpLabel
%V = OpVariable %_ptr_Function_S Function %9
%13 = OpAccessChain %_ptr_Function_int %V %uint_0
OpStore %13 %int_5
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,10 @@
struct S {
i : i32;
};
[[stage(compute)]]
fn main() {
var V : S;
V.i = 5;
return;
}

View File

@@ -0,0 +1,35 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 18
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %func "func"
OpName %value "value"
OpName %pointer "pointer"
OpName %main "main"
OpName %i "i"
%void = OpTypeVoid
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%1 = OpTypeFunction %void %int %_ptr_Function_int
%10 = OpTypeFunction %void
%int_123 = OpConstant %int 123
%15 = OpConstantNull %int
%func = OpFunction %void None %1
%value = OpFunctionParameter %int
%pointer = OpFunctionParameter %_ptr_Function_int
%8 = OpLabel
OpStore %pointer %value
OpReturn
OpFunctionEnd
%main = OpFunction %void None %10
%12 = OpLabel
%i = OpVariable %_ptr_Function_int Function %15
OpStore %i %int_123
%16 = OpFunctionCall %void %func %int_123 %i
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1 @@
SKIP: error: pointers not supported in HLSL

View File

@@ -0,0 +1,15 @@
#include <metal_stdlib>
using namespace metal;
void func(int value, int* pointer) {
*(pointer) = value;
return;
}
kernel void tint_symbol() {
int i = 0;
i = 123;
func(123, &(i));
return;
}

View File

@@ -0,0 +1,37 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 19
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %func "func"
OpName %value "value"
OpName %pointer "pointer"
OpName %main "main"
OpName %i "i"
%void = OpTypeVoid
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%1 = OpTypeFunction %void %int %_ptr_Function_int
%10 = OpTypeFunction %void
%int_0 = OpConstant %int 0
%15 = OpConstantNull %int
%int_123 = OpConstant %int 123
%func = OpFunction %void None %1
%value = OpFunctionParameter %int
%pointer = OpFunctionParameter %_ptr_Function_int
%8 = OpLabel
OpStore %pointer %value
OpReturn
OpFunctionEnd
%main = OpFunction %void None %10
%12 = OpLabel
%i = OpVariable %_ptr_Function_int Function %15
OpStore %i %int_0
OpStore %i %int_123
%17 = OpFunctionCall %void %func %int_123 %i
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,12 @@
fn func(value : i32, pointer : ptr<function, i32>) {
*(pointer) = value;
return;
}
[[stage(compute)]]
fn main() {
var i : i32 = 0;
i = 123;
func(123, &(i));
return;
}

View File

@@ -0,0 +1,9 @@
fn func(value : i32, pointer : ptr<function, i32>) {
*pointer = value;
}
[[stage(compute)]]
fn main() {
var i : i32 = 123;
func(123, &i);
}

View File

@@ -0,0 +1 @@
SKIP: error: pointers not supported in HLSL

View File

@@ -0,0 +1,13 @@
#include <metal_stdlib>
using namespace metal;
void func(int value, int* pointer) {
*(pointer) = value;
}
kernel void tint_symbol() {
int i = 123;
func(123, &(i));
return;
}

View File

@@ -0,0 +1,35 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 18
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %func "func"
OpName %value "value"
OpName %pointer "pointer"
OpName %main "main"
OpName %i "i"
%void = OpTypeVoid
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%1 = OpTypeFunction %void %int %_ptr_Function_int
%10 = OpTypeFunction %void
%int_123 = OpConstant %int 123
%15 = OpConstantNull %int
%func = OpFunction %void None %1
%value = OpFunctionParameter %int
%pointer = OpFunctionParameter %_ptr_Function_int
%8 = OpLabel
OpStore %pointer %value
OpReturn
OpFunctionEnd
%main = OpFunction %void None %10
%12 = OpLabel
%i = OpVariable %_ptr_Function_int Function %15
OpStore %i %int_123
%16 = OpFunctionCall %void %func %int_123 %i
OpReturn
OpFunctionEnd

View File

@@ -0,0 +1,9 @@
fn func(value : i32, pointer : ptr<function, i32>) {
*(pointer) = value;
}
[[stage(compute)]]
fn main() {
var i : i32 = 123;
func(123, &(i));
}