From fcc0de0b865edefc7f5b4a6ce6f76759e379a4fd Mon Sep 17 00:00:00 2001 From: James Price Date: Mon, 12 Jul 2021 12:28:52 +0000 Subject: [PATCH] wgsl: Implement invariant attribute Make sure the other backends ICE on unrecognized attributes. Add E2E tests, currently skipped for the other backends. Bug: tint:772 Change-Id: I4e68d111ff79b19ebb6c574058a91debcb746011 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57642 Commit-Queue: Ben Clayton Kokoro: Kokoro Reviewed-by: Ben Clayton --- src/reader/wgsl/parser_impl.cc | 6 ++++++ .../wgsl/parser_impl_function_header_test.cc | 14 ++++++++++++++ src/writer/hlsl/generator_impl.cc | 5 +++++ src/writer/msl/generator_impl.cc | 5 +++++ src/writer/wgsl/generator_impl.cc | 3 +++ test/shader_io/invariant.wgsl | 4 ++++ test/shader_io/invariant.wgsl.expected.hlsl | 10 ++++++++++ test/shader_io/invariant.wgsl.expected.msl | 10 ++++++++++ test/shader_io/invariant.wgsl.expected.spvasm | 9 +++++++++ test/shader_io/invariant.wgsl.expected.wgsl | 4 ++++ test/shader_io/invariant_struct_member.wgsl | 8 ++++++++ .../invariant_struct_member.wgsl.expected.hlsl | 10 ++++++++++ .../invariant_struct_member.wgsl.expected.msl | 10 ++++++++++ .../invariant_struct_member.wgsl.expected.spvasm | 14 ++++++++++++++ .../invariant_struct_member.wgsl.expected.wgsl | 9 +++++++++ 15 files changed, 121 insertions(+) create mode 100644 test/shader_io/invariant.wgsl create mode 100644 test/shader_io/invariant.wgsl.expected.hlsl create mode 100644 test/shader_io/invariant.wgsl.expected.msl create mode 100644 test/shader_io/invariant.wgsl.expected.spvasm create mode 100644 test/shader_io/invariant.wgsl.expected.wgsl create mode 100644 test/shader_io/invariant_struct_member.wgsl create mode 100644 test/shader_io/invariant_struct_member.wgsl.expected.hlsl create mode 100644 test/shader_io/invariant_struct_member.wgsl.expected.msl create mode 100644 test/shader_io/invariant_struct_member.wgsl.expected.spvasm create mode 100644 test/shader_io/invariant_struct_member.wgsl.expected.wgsl diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 3e8f37e13a..322a52308d 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -24,6 +24,7 @@ #include "src/ast/external_texture.h" #include "src/ast/fallthrough_statement.h" #include "src/ast/if_statement.h" +#include "src/ast/invariant_decoration.h" #include "src/ast/loop_statement.h" #include "src/ast/override_decoration.h" #include "src/ast/return_statement.h" @@ -111,6 +112,7 @@ const char kBlockDecoration[] = "block"; const char kBuiltinDecoration[] = "builtin"; const char kGroupDecoration[] = "group"; const char kInterpolateDecoration[] = "interpolate"; +const char kInvariantDecoration[] = "invariant"; const char kLocationDecoration[] = "location"; const char kOverrideDecoration[] = "override"; const char kSizeDecoration[] = "size"; @@ -3002,6 +3004,10 @@ Maybe ParserImpl::decoration() { }); } + if (s == kInvariantDecoration) { + return create(t.source()); + } + if (s == kBuiltinDecoration) { return expect_paren_block("builtin decoration", [&]() -> Result { auto builtin = expect_builtin(); diff --git a/src/reader/wgsl/parser_impl_function_header_test.cc b/src/reader/wgsl/parser_impl_function_header_test.cc index f15699ffba..b3f38c8054 100644 --- a/src/reader/wgsl/parser_impl_function_header_test.cc +++ b/src/reader/wgsl/parser_impl_function_header_test.cc @@ -61,6 +61,20 @@ TEST_F(ParserImplTest, FunctionHeader_DecoratedReturnType) { EXPECT_EQ(loc->value(), 1u); } +TEST_F(ParserImplTest, FunctionHeader_InvariantReturnType) { + auto p = parser("fn main() -> [[invariant]] f32"); + auto f = p->function_header(); + ASSERT_FALSE(p->has_error()) << p->error(); + EXPECT_TRUE(f.matched); + EXPECT_FALSE(f.errored); + + EXPECT_EQ(f->name, "main"); + EXPECT_EQ(f->params.size(), 0u); + EXPECT_TRUE(f->return_type->Is()); + ASSERT_EQ(f->return_type_decorations.size(), 1u); + EXPECT_TRUE(f->return_type_decorations[0]->Is()); +} + TEST_F(ParserImplTest, FunctionHeader_DecoratedReturnType_WithArrayStride) { auto p = parser("fn main() -> [[location(1), stride(16)]] array"); auto f = p->function_header(); diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 616a3a4304..d4c0ed6f8d 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -3092,6 +3092,11 @@ bool GeneratorImpl::EmitStructType(const sem::Struct* str) { return false; } pre += mod; + + } else if (!deco->IsAnyOf()) { + TINT_ICE(Writer, diagnostics_) + << "unhandled struct member attribute: " << deco->name(); } } diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 5f82e30fc6..96ebba6af2 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -2134,6 +2134,11 @@ bool GeneratorImpl::EmitStructType(const sem::Struct* str) { return false; } out << " [[" << attr << "]]"; + } else if (!deco->IsAnyOf()) { + TINT_ICE(Writer, diagnostics_) + << "unhandled struct member attribute: " << deco->name(); } } diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index bb96dd6ffe..38e7d62a8e 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -30,6 +30,7 @@ #include "src/ast/i32.h" #include "src/ast/internal_decoration.h" #include "src/ast/interpolate_decoration.h" +#include "src/ast/invariant_decoration.h" #include "src/ast/matrix.h" #include "src/ast/module.h" #include "src/ast/multisampled_texture.h" @@ -676,6 +677,8 @@ bool GeneratorImpl::EmitDecorations(std::ostream& out, out << ", " << interpolate->sampling(); } out << ")"; + } else if (deco->Is()) { + out << "invariant"; } else if (auto* override_deco = deco->As()) { out << "override"; if (override_deco->HasValue()) { diff --git a/test/shader_io/invariant.wgsl b/test/shader_io/invariant.wgsl new file mode 100644 index 0000000000..66b0649ee4 --- /dev/null +++ b/test/shader_io/invariant.wgsl @@ -0,0 +1,4 @@ +[[stage(vertex)]] +fn main() -> [[builtin(position), invariant]] vec4 { + return vec4(); +} diff --git a/test/shader_io/invariant.wgsl.expected.hlsl b/test/shader_io/invariant.wgsl.expected.hlsl new file mode 100644 index 0000000000..f0fa6fe3a9 --- /dev/null +++ b/test/shader_io/invariant.wgsl.expected.hlsl @@ -0,0 +1,10 @@ +SKIP: FAILED + +../../src/writer/hlsl/generator_impl.cc:3056 internal compiler error: unhandled struct member attribute: invariant +******************************************************************** +* The tint shader compiler has encountered an unexpected error. * +* * +* Please help us fix this issue by submitting a bug report at * +* crbug.com/tint with the source program that triggered the bug. * +******************************************************************** + diff --git a/test/shader_io/invariant.wgsl.expected.msl b/test/shader_io/invariant.wgsl.expected.msl new file mode 100644 index 0000000000..6e1ba46bf0 --- /dev/null +++ b/test/shader_io/invariant.wgsl.expected.msl @@ -0,0 +1,10 @@ +SKIP: FAILED + +../../src/writer/msl/generator_impl.cc:1990 internal compiler error: unhandled struct member attribute: invariant +******************************************************************** +* The tint shader compiler has encountered an unexpected error. * +* * +* Please help us fix this issue by submitting a bug report at * +* crbug.com/tint with the source program that triggered the bug. * +******************************************************************** + diff --git a/test/shader_io/invariant.wgsl.expected.spvasm b/test/shader_io/invariant.wgsl.expected.spvasm new file mode 100644 index 0000000000..93feddc6c7 --- /dev/null +++ b/test/shader_io/invariant.wgsl.expected.spvasm @@ -0,0 +1,9 @@ +SKIP: FAILED + + +[[stage(vertex)]] +fn main() -> [[builtin(position), invariant]] vec4 { + return vec4(); +} + +Failed to generate: unknown decoration diff --git a/test/shader_io/invariant.wgsl.expected.wgsl b/test/shader_io/invariant.wgsl.expected.wgsl new file mode 100644 index 0000000000..66b0649ee4 --- /dev/null +++ b/test/shader_io/invariant.wgsl.expected.wgsl @@ -0,0 +1,4 @@ +[[stage(vertex)]] +fn main() -> [[builtin(position), invariant]] vec4 { + return vec4(); +} diff --git a/test/shader_io/invariant_struct_member.wgsl b/test/shader_io/invariant_struct_member.wgsl new file mode 100644 index 0000000000..97d813f184 --- /dev/null +++ b/test/shader_io/invariant_struct_member.wgsl @@ -0,0 +1,8 @@ +struct Out { + [[builtin(position), invariant]] pos : vec4; +}; + +[[stage(vertex)]] +fn main() -> Out { + return Out(); +} diff --git a/test/shader_io/invariant_struct_member.wgsl.expected.hlsl b/test/shader_io/invariant_struct_member.wgsl.expected.hlsl new file mode 100644 index 0000000000..f0fa6fe3a9 --- /dev/null +++ b/test/shader_io/invariant_struct_member.wgsl.expected.hlsl @@ -0,0 +1,10 @@ +SKIP: FAILED + +../../src/writer/hlsl/generator_impl.cc:3056 internal compiler error: unhandled struct member attribute: invariant +******************************************************************** +* The tint shader compiler has encountered an unexpected error. * +* * +* Please help us fix this issue by submitting a bug report at * +* crbug.com/tint with the source program that triggered the bug. * +******************************************************************** + diff --git a/test/shader_io/invariant_struct_member.wgsl.expected.msl b/test/shader_io/invariant_struct_member.wgsl.expected.msl new file mode 100644 index 0000000000..6e1ba46bf0 --- /dev/null +++ b/test/shader_io/invariant_struct_member.wgsl.expected.msl @@ -0,0 +1,10 @@ +SKIP: FAILED + +../../src/writer/msl/generator_impl.cc:1990 internal compiler error: unhandled struct member attribute: invariant +******************************************************************** +* The tint shader compiler has encountered an unexpected error. * +* * +* Please help us fix this issue by submitting a bug report at * +* crbug.com/tint with the source program that triggered the bug. * +******************************************************************** + diff --git a/test/shader_io/invariant_struct_member.wgsl.expected.spvasm b/test/shader_io/invariant_struct_member.wgsl.expected.spvasm new file mode 100644 index 0000000000..08c28fce85 --- /dev/null +++ b/test/shader_io/invariant_struct_member.wgsl.expected.spvasm @@ -0,0 +1,14 @@ +SKIP: FAILED + + +struct Out { + [[builtin(position), invariant]] + pos : vec4; +}; + +[[stage(vertex)]] +fn main() -> Out { + return Out(); +} + +Failed to generate: unknown decoration diff --git a/test/shader_io/invariant_struct_member.wgsl.expected.wgsl b/test/shader_io/invariant_struct_member.wgsl.expected.wgsl new file mode 100644 index 0000000000..03d919306a --- /dev/null +++ b/test/shader_io/invariant_struct_member.wgsl.expected.wgsl @@ -0,0 +1,9 @@ +struct Out { + [[builtin(position), invariant]] + pos : vec4; +}; + +[[stage(vertex)]] +fn main() -> Out { + return Out(); +}