From 81fc1098228bdbb8098b1ff6b9c52a47d86e7113 Mon Sep 17 00:00:00 2001 From: James Price Date: Tue, 9 May 2023 10:59:58 +0000 Subject: [PATCH] [tint] Fix unshadowing of abstract const users Unwrap sem::Materialize as well as sem::Load when looking for users of a renamed declaration. Fixed: tint:1934 Change-Id: I7d5ade1f902dbdb4cf3b4f7552b78359f5b3ea9a Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/132060 Commit-Queue: James Price Kokoro: Kokoro Reviewed-by: Ben Clayton --- src/tint/transform/unshadow.cc | 2 +- src/tint/transform/unshadow_test.cc | 20 ++++++++++++ test/tint/bug/tint/1934.wgsl | 5 +++ .../tint/bug/tint/1934.wgsl.expected.dxc.hlsl | 9 ++++++ .../tint/bug/tint/1934.wgsl.expected.fxc.hlsl | 9 ++++++ test/tint/bug/tint/1934.wgsl.expected.glsl | 11 +++++++ test/tint/bug/tint/1934.wgsl.expected.msl | 8 +++++ test/tint/bug/tint/1934.wgsl.expected.spvasm | 31 +++++++++++++++++++ test/tint/bug/tint/1934.wgsl.expected.wgsl | 5 +++ 9 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 test/tint/bug/tint/1934.wgsl create mode 100644 test/tint/bug/tint/1934.wgsl.expected.dxc.hlsl create mode 100644 test/tint/bug/tint/1934.wgsl.expected.fxc.hlsl create mode 100644 test/tint/bug/tint/1934.wgsl.expected.glsl create mode 100644 test/tint/bug/tint/1934.wgsl.expected.msl create mode 100644 test/tint/bug/tint/1934.wgsl.expected.spvasm create mode 100644 test/tint/bug/tint/1934.wgsl.expected.wgsl diff --git a/src/tint/transform/unshadow.cc b/src/tint/transform/unshadow.cc index 1dd4234473..19a1023702 100644 --- a/src/tint/transform/unshadow.cc +++ b/src/tint/transform/unshadow.cc @@ -108,7 +108,7 @@ struct Unshadow::State { ctx.ReplaceAll( [&](const ast::IdentifierExpression* ident) -> const tint::ast::IdentifierExpression* { if (auto* sem_ident = sem.GetVal(ident)) { - if (auto* user = sem_ident->UnwrapLoad()->As()) { + if (auto* user = sem_ident->Unwrap()->As()) { if (auto renamed = renamed_to.Find(user->Variable())) { return b.Expr(*renamed); } diff --git a/src/tint/transform/unshadow_test.cc b/src/tint/transform/unshadow_test.cc index 9d8541d973..e60db426d5 100644 --- a/src/tint/transform/unshadow_test.cc +++ b/src/tint/transform/unshadow_test.cc @@ -779,5 +779,25 @@ fn F() { EXPECT_EQ(expect, str(got)); } +TEST_F(UnshadowTest, RenamedAbstractConstHasUsers) { + auto* src = R"( +fn v() { + const v = 1; + let x = v; +} +)"; + + auto* expect = R"( +fn v() { + const v_1 = 1; + let x = v_1; +} +)"; + + auto got = Run(src); + + EXPECT_EQ(expect, str(got)); +} + } // namespace } // namespace tint::transform diff --git a/test/tint/bug/tint/1934.wgsl b/test/tint/bug/tint/1934.wgsl new file mode 100644 index 0000000000..80c2bb183d --- /dev/null +++ b/test/tint/bug/tint/1934.wgsl @@ -0,0 +1,5 @@ +fn v() { + const v = vec2(1); + let i = 1; + var b = v[i]; +} diff --git a/test/tint/bug/tint/1934.wgsl.expected.dxc.hlsl b/test/tint/bug/tint/1934.wgsl.expected.dxc.hlsl new file mode 100644 index 0000000000..c57428d792 --- /dev/null +++ b/test/tint/bug/tint/1934.wgsl.expected.dxc.hlsl @@ -0,0 +1,9 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void v() { + const int i = 1; + int b = (1).xx[i]; +} diff --git a/test/tint/bug/tint/1934.wgsl.expected.fxc.hlsl b/test/tint/bug/tint/1934.wgsl.expected.fxc.hlsl new file mode 100644 index 0000000000..c57428d792 --- /dev/null +++ b/test/tint/bug/tint/1934.wgsl.expected.fxc.hlsl @@ -0,0 +1,9 @@ +[numthreads(1, 1, 1)] +void unused_entry_point() { + return; +} + +void v() { + const int i = 1; + int b = (1).xx[i]; +} diff --git a/test/tint/bug/tint/1934.wgsl.expected.glsl b/test/tint/bug/tint/1934.wgsl.expected.glsl new file mode 100644 index 0000000000..0848efa201 --- /dev/null +++ b/test/tint/bug/tint/1934.wgsl.expected.glsl @@ -0,0 +1,11 @@ +#version 310 es + +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; +void unused_entry_point() { + return; +} +void v() { + int i = 1; + int b = ivec2(1)[i]; +} + diff --git a/test/tint/bug/tint/1934.wgsl.expected.msl b/test/tint/bug/tint/1934.wgsl.expected.msl new file mode 100644 index 0000000000..0cb3d1ab80 --- /dev/null +++ b/test/tint/bug/tint/1934.wgsl.expected.msl @@ -0,0 +1,8 @@ +#include + +using namespace metal; +void v() { + int const i = 1; + int b = int2(1)[i]; +} + diff --git a/test/tint/bug/tint/1934.wgsl.expected.spvasm b/test/tint/bug/tint/1934.wgsl.expected.spvasm new file mode 100644 index 0000000000..85c86d4f61 --- /dev/null +++ b/test/tint/bug/tint/1934.wgsl.expected.spvasm @@ -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 %unused_entry_point "unused_entry_point" + OpExecutionMode %unused_entry_point LocalSize 1 1 1 + OpName %unused_entry_point "unused_entry_point" + OpName %v "v" + OpName %b "b" + %void = OpTypeVoid + %1 = OpTypeFunction %void + %int = OpTypeInt 32 1 + %int_1 = OpConstant %int 1 + %v2int = OpTypeVector %int 2 + %10 = OpConstantComposite %v2int %int_1 %int_1 +%_ptr_Function_int = OpTypePointer Function %int + %14 = OpConstantNull %int +%unused_entry_point = OpFunction %void None %1 + %4 = OpLabel + OpReturn + OpFunctionEnd + %v = OpFunction %void None %1 + %6 = OpLabel + %b = OpVariable %_ptr_Function_int Function %14 + %11 = OpVectorExtractDynamic %int %10 %int_1 + OpStore %b %11 + OpReturn + OpFunctionEnd diff --git a/test/tint/bug/tint/1934.wgsl.expected.wgsl b/test/tint/bug/tint/1934.wgsl.expected.wgsl new file mode 100644 index 0000000000..80c2bb183d --- /dev/null +++ b/test/tint/bug/tint/1934.wgsl.expected.wgsl @@ -0,0 +1,5 @@ +fn v() { + const v = vec2(1); + let i = 1; + var b = v[i]; +}