[tint][ast] Fix DirectVariableAccess with uncalled function ptr params

If a function has no calls, and in turn calls another function with a function-address-space pointer parameter, then we could incorrectly attempt to create a parameter with a pointer-to-pointer type.
The code was missing an `UnwrapPtrOrRef` - this wasn't noticed as most originating variables are `ref` types, and `CreateASTTypeFor()` will construct the storage type for a `ref` type.

Bug: 339684346
Change-Id: I0c32536acb6f582ae521868e1599c22c77f656bc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/187689
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/lang/wgsl/ast/transform/direct_variable_access.cc b/src/tint/lang/wgsl/ast/transform/direct_variable_access.cc
index dac33c7..d9093ec 100644
--- a/src/tint/lang/wgsl/ast/transform/direct_variable_access.cc
+++ b/src/tint/lang/wgsl/ast/transform/direct_variable_access.cc
@@ -834,9 +834,9 @@
                     if (auto incoming_shape = variant_sig.Get(param)) {
                         auto& symbols = *variant.ptr_param_symbols.Get(param);
                         if (symbols.base_ptr.IsValid()) {
-                            auto base_ptr_ty =
-                                b.ty.ptr(incoming_shape->root.address_space,
-                                         CreateASTTypeFor(ctx, incoming_shape->root.type));
+                            auto base_ptr_ty = b.ty.ptr(
+                                incoming_shape->root.address_space,
+                                CreateASTTypeFor(ctx, incoming_shape->root.type->UnwrapPtrOrRef()));
                             params.Push(b.Param(symbols.base_ptr, base_ptr_ty));
                         }
                         if (symbols.indices.IsValid()) {
diff --git a/src/tint/lang/wgsl/ast/transform/direct_variable_access_test.cc b/src/tint/lang/wgsl/ast/transform/direct_variable_access_test.cc
index 04666b2..9b18a40 100644
--- a/src/tint/lang/wgsl/ast/transform/direct_variable_access_test.cc
+++ b/src/tint/lang/wgsl/ast/transform/direct_variable_access_test.cc
@@ -3209,6 +3209,33 @@
     EXPECT_EQ(expect, str(got));
 }
 
+TEST_F(DirectVariableAccessFunctionASTest, PointerForwarding_NoUse) {
+    auto* src = R"(
+fn a(p : ptr<function, i32>) -> i32 {
+  return *p;
+}
+
+fn b(p : ptr<function, i32>) -> i32 {
+  return a(p);
+}
+)";
+
+    auto* expect =
+        R"(
+fn a_F(p : ptr<function, i32>) -> i32 {
+  return *(p);
+}
+
+fn b(p : ptr<function, i32>) -> i32 {
+  return a_F(p);
+}
+)";
+
+    auto got = Run<DirectVariableAccess>(src, EnableFunction());
+
+    EXPECT_EQ(expect, str(got));
+}
+
 }  // namespace function_as_tests
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/test/tint/bug/chromium/339684346.wgsl b/test/tint/bug/chromium/339684346.wgsl
new file mode 100644
index 0000000..ce6d5fe
--- /dev/null
+++ b/test/tint/bug/chromium/339684346.wgsl
@@ -0,0 +1,8 @@
+fn a(x : ptr<function, i32>) {
+}
+
+fn b(x : ptr<function, i32>) {
+  a(x);
+}
+
+//it
diff --git a/test/tint/bug/chromium/339684346.wgsl.expected.dxc.hlsl b/test/tint/bug/chromium/339684346.wgsl.expected.dxc.hlsl
new file mode 100644
index 0000000..c44ac00
--- /dev/null
+++ b/test/tint/bug/chromium/339684346.wgsl.expected.dxc.hlsl
@@ -0,0 +1,11 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void a(inout int x) {
+}
+
+void b(inout int x) {
+  a(x);
+}
diff --git a/test/tint/bug/chromium/339684346.wgsl.expected.fxc.hlsl b/test/tint/bug/chromium/339684346.wgsl.expected.fxc.hlsl
new file mode 100644
index 0000000..c44ac00
--- /dev/null
+++ b/test/tint/bug/chromium/339684346.wgsl.expected.fxc.hlsl
@@ -0,0 +1,11 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void a(inout int x) {
+}
+
+void b(inout int x) {
+  a(x);
+}
diff --git a/test/tint/bug/chromium/339684346.wgsl.expected.glsl b/test/tint/bug/chromium/339684346.wgsl.expected.glsl
new file mode 100644
index 0000000..376bfa7
--- /dev/null
+++ b/test/tint/bug/chromium/339684346.wgsl.expected.glsl
@@ -0,0 +1,13 @@
+#version 310 es
+
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void unused_entry_point() {
+  return;
+}
+void a(inout int x) {
+}
+
+void b(inout int x) {
+  a(x);
+}
+
diff --git a/test/tint/bug/chromium/339684346.wgsl.expected.msl b/test/tint/bug/chromium/339684346.wgsl.expected.msl
new file mode 100644
index 0000000..03231cd
--- /dev/null
+++ b/test/tint/bug/chromium/339684346.wgsl.expected.msl
@@ -0,0 +1,10 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void a(thread int* const x) {
+}
+
+void b(thread int* const x) {
+  a(x);
+}
+
diff --git a/test/tint/bug/chromium/339684346.wgsl.expected.spvasm b/test/tint/bug/chromium/339684346.wgsl.expected.spvasm
new file mode 100644
index 0000000..6ac03bf
--- /dev/null
+++ b/test/tint/bug/chromium/339684346.wgsl.expected.spvasm
@@ -0,0 +1,34 @@
+; 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 %a_F "a_F"
+               OpName %x "x"
+               OpName %b "b"
+               OpName %x_0 "x"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+%_ptr_Function_int = OpTypePointer Function %int
+          %5 = OpTypeFunction %void %_ptr_Function_int
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+        %a_F = OpFunction %void None %5
+          %x = OpFunctionParameter %_ptr_Function_int
+         %10 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %b = OpFunction %void None %5
+        %x_0 = OpFunctionParameter %_ptr_Function_int
+         %13 = OpLabel
+         %14 = OpFunctionCall %void %a_F %x_0
+               OpReturn
+               OpFunctionEnd
diff --git a/test/tint/bug/chromium/339684346.wgsl.expected.wgsl b/test/tint/bug/chromium/339684346.wgsl.expected.wgsl
new file mode 100644
index 0000000..67bdeb0
--- /dev/null
+++ b/test/tint/bug/chromium/339684346.wgsl.expected.wgsl
@@ -0,0 +1,6 @@
+fn a(x : ptr<function, i32>) {
+}
+
+fn b(x : ptr<function, i32>) {
+  a(x);
+}