Import Tint changes from Dawn

Changes:
  - 8f0607a8c88023c4f01ab4f1ebf73a54dcef4399 Fix D3D12 shader interstage truncating transform by shrekshao <shrekshao@google.com>
  - 559e5a2cde42dff56c853ce59099d179e7199f03 tint/transform: Fix ICE in HLSL backend by Ben Clayton <bclayton@google.com>
GitOrigin-RevId: 8f0607a8c88023c4f01ab4f1ebf73a54dcef4399
Change-Id: I07c14ffd255d88821efabc89d0eaf3563472aba5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/127800
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/transform/simplify_pointers.cc b/src/tint/transform/simplify_pointers.cc
index bec9019..b5af458 100644
--- a/src/tint/transform/simplify_pointers.cc
+++ b/src/tint/transform/simplify_pointers.cc
@@ -132,6 +132,13 @@
         utils::Hashmap<const ast::Expression*, Symbol, 8> saved_vars;
 
         bool needs_transform = false;
+        for (auto* ty : ctx.src->Types()) {
+            if (ty->Is<type::Pointer>()) {
+                // Program contains pointers which need removing.
+                needs_transform = true;
+                break;
+            }
+        }
 
         // Find all the pointer-typed `let` declarations.
         // Note that these must be function-scoped, as module-scoped `let`s are not
diff --git a/src/tint/transform/simplify_pointers_test.cc b/src/tint/transform/simplify_pointers_test.cc
index b05dfc4..4390c7b 100644
--- a/src/tint/transform/simplify_pointers_test.cc
+++ b/src/tint/transform/simplify_pointers_test.cc
@@ -28,16 +28,6 @@
     EXPECT_FALSE(ShouldRun<SimplifyPointers>(src));
 }
 
-TEST_F(SimplifyPointersTest, NoAddressOf) {
-    auto* src = R"(
-fn f(p : ptr<function, i32>) {
-  var v : i32;
-}
-)";
-
-    EXPECT_FALSE(ShouldRun<SimplifyPointers>(src));
-}
-
 TEST_F(SimplifyPointersTest, FoldPointer) {
     auto* src = R"(
 fn f() {
@@ -59,6 +49,36 @@
     EXPECT_EQ(expect, str(got));
 }
 
+TEST_F(SimplifyPointersTest, UnusedPointerParam) {
+    auto* src = R"(
+fn f(p : ptr<function, i32>) {
+}
+)";
+
+    auto* expect = src;
+
+    auto got = Run<Unshadow, SimplifyPointers>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(SimplifyPointersTest, ReferencedPointerParam) {
+    auto* src = R"(
+fn f(p : ptr<function, i32>) {
+  let x = p;
+}
+)";
+
+    auto* expect = R"(
+fn f(p : ptr<function, i32>) {
+}
+)";
+
+    auto got = Run<Unshadow, SimplifyPointers>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
+
 TEST_F(SimplifyPointersTest, AddressOfDeref) {
     auto* src = R"(
 fn f() {
diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc
index 5ae7650..a666eaa 100644
--- a/src/tint/writer/glsl/generator_impl.cc
+++ b/src/tint/writer/glsl/generator_impl.cc
@@ -2982,9 +2982,8 @@
             out << "x" << mat->rows();
         }
     } else if (TINT_UNLIKELY(type->Is<type::Pointer>())) {
-        TINT_ICE(Writer, diagnostics_)
-            << "Attempting to emit pointer type. These should have been removed "
-               "with the InlinePointerLets transform";
+        TINT_ICE(Writer, diagnostics_) << "Attempting to emit pointer type. These should have been "
+                                          "removed with the SimplifyPointers transform";
         return false;
     } else if (type->Is<type::Sampler>()) {
         return false;
diff --git a/src/tint/writer/glsl/generator_impl_sanitizer_test.cc b/src/tint/writer/glsl/generator_impl_sanitizer_test.cc
index 79a3ea3..097b519 100644
--- a/src/tint/writer/glsl/generator_impl_sanitizer_test.cc
+++ b/src/tint/writer/glsl/generator_impl_sanitizer_test.cc
@@ -228,7 +228,7 @@
     EXPECT_EQ(expect, got);
 }
 
-TEST_F(GlslSanitizerTest, InlinePtrLetsBasic) {
+TEST_F(GlslSanitizerTest, SimplifyPointersBasic) {
     // var v : i32;
     // let p : ptr<function, i32> = &v;
     // let x : i32 = *p;
@@ -267,7 +267,7 @@
     EXPECT_EQ(expect, got);
 }
 
-TEST_F(GlslSanitizerTest, InlinePtrLetsComplexChain) {
+TEST_F(GlslSanitizerTest, SimplifyPointersComplexChain) {
     // var a : array<mat4x4<f32>, 4u>;
     // let ap : ptr<function, array<mat4x4<f32>, 4u>> = &a;
     // let mp : ptr<function, mat4x4<f32>> = &(*ap)[3i];
diff --git a/src/tint/writer/hlsl/generator.h b/src/tint/writer/hlsl/generator.h
index 9b79ad1..804b449 100644
--- a/src/tint/writer/hlsl/generator.h
+++ b/src/tint/writer/hlsl/generator.h
@@ -74,6 +74,9 @@
     /// This is potentially used for truncating unused interstage outputs at current shader stage.
     std::bitset<16> interstage_locations;
 
+    /// Set to `true` to run the TruncateInterstageVariables transform.
+    bool truncate_interstage_variables = false;
+
     /// Set to `true` to generate polyfill for `reflect` builtin for vec2<f32>
     bool polyfill_reflect_vec2_f32 = false;
 
diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc
index 0dc6586..5e4ae33 100644
--- a/src/tint/writer/hlsl/generator_impl.cc
+++ b/src/tint/writer/hlsl/generator_impl.cc
@@ -241,13 +241,11 @@
     // CanonicalizeEntryPointIO must come after Robustness
     manager.Add<transform::CanonicalizeEntryPointIO>();
 
-    if (options.interstage_locations.any()) {
+    if (options.truncate_interstage_variables) {
         // When interstage_locations is empty, it means there's no user-defined interstage variables
-        // being used in the next stage. This is treated as a special case.
-        // TruncateInterstageVariables transform is trying to solve the HLSL compiler register
-        // mismatch issue. So it is not needed if no register is assigned to any interstage
-        // variables. As a result we only add this transform when there is at least one interstage
-        // locations being used.
+        // being used in the next stage. Still, HLSL compiler register mismatch could happen, if
+        // there's builtin inputs used in the next stage. So we still run
+        // TruncateInterstageVariables transform.
 
         // TruncateInterstageVariables itself will skip when interstage_locations matches exactly
         // with the current stage output.
@@ -281,15 +279,14 @@
     // TODO(crbug.com/tint/1752): This is only necessary when FXC is being used.
     manager.Add<transform::DemoteToHelper>();
 
-    // ArrayLengthFromUniform must come after InlinePointerLets and Simplify, as
-    // it assumes that the form of the array length argument is &var.array.
+    // ArrayLengthFromUniform must come after SimplifyPointers as it assumes that the form of the
+    // array length argument is &var.array.
     manager.Add<transform::ArrayLengthFromUniform>();
     data.Add<transform::ArrayLengthFromUniform::Config>(std::move(array_length_from_uniform_cfg));
     // DecomposeMemoryAccess must come after:
-    // * InlinePointerLets, as we cannot take the address of calls to
-    //   DecomposeMemoryAccess::Intrinsic.
-    // * Simplify, as we need to fold away the address-of and dereferences of
-    // `*(&(intrinsic_load()))` expressions.
+    // * SimplifyPointers, as we cannot take the address of calls to
+    //   DecomposeMemoryAccess::Intrinsic and we need to fold away the address-of and dereferences
+    //   of `*(&(intrinsic_load()))` expressions.
     // * RemovePhonies, as phonies can be assigned a pointer to a
     //   non-constructible buffer, or dynamic array, which DMA cannot cope with.
     manager.Add<transform::DecomposeMemoryAccess>();
@@ -4070,9 +4067,8 @@
             return true;
         },
         [&](const type::Pointer*) {
-            TINT_ICE(Writer, diagnostics_)
-                << "Attempting to emit pointer type. These should have been "
-                   "removed with the InlinePointerLets transform";
+            TINT_ICE(Writer, diagnostics_) << "Attempting to emit pointer type. These should have "
+                                              "been removed with the SimplifyPointers transform";
             return false;
         },
         [&](const type::Sampler* sampler) {
diff --git a/src/tint/writer/hlsl/generator_impl_sanitizer_test.cc b/src/tint/writer/hlsl/generator_impl_sanitizer_test.cc
index cc8c253..c511a83 100644
--- a/src/tint/writer/hlsl/generator_impl_sanitizer_test.cc
+++ b/src/tint/writer/hlsl/generator_impl_sanitizer_test.cc
@@ -237,7 +237,7 @@
     EXPECT_EQ(expect, got);
 }
 
-TEST_F(HlslSanitizerTest, InlinePtrLetsBasic) {
+TEST_F(HlslSanitizerTest, SimplifyPointersBasic) {
     // var v : i32;
     // let p : ptr<function, i32> = &v;
     // let x : i32 = *p;
@@ -269,7 +269,7 @@
     EXPECT_EQ(expect, got);
 }
 
-TEST_F(HlslSanitizerTest, InlinePtrLetsComplexChain) {
+TEST_F(HlslSanitizerTest, SimplifyPointersComplexChain) {
     // var a : array<mat4x4<f32>, 4u>;
     // let ap : ptr<function, array<mat4x4<f32>, 4u>> = &a;
     // let mp : ptr<function, mat4x4<f32>> = &(*ap)[3i];