[hlsl] Add some existing transforms

This CL adds existing transforms which are needed for HLSL to the HLSL
IR backend.

Bug: 42251045
Change-Id: I940d60c34f7516b27f1733fc173b2384bf7f6202
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/196074
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/lang/hlsl/writer/access_test.cc b/src/tint/lang/hlsl/writer/access_test.cc
index e26f5b1..02d78f1 100644
--- a/src/tint/lang/hlsl/writer/access_test.cc
+++ b/src/tint/lang/hlsl/writer/access_test.cc
@@ -167,7 +167,7 @@
     EXPECT_EQ(output_.hlsl, R"(
 void foo(int idx) {
   int4 vec = (0).xxxx;
-  vec[idx] = 42;
+  vec[min(uint(idx), 3u)] = 42;
 }
 
 [numthreads(1, 1, 1)]
@@ -221,7 +221,7 @@
 [numthreads(1, 1, 1)]
 void a() {
   S v = (S)0;
-  float x = v.c.e[1];
+  float x = v.c.e[1u];
 }
 
 )");
diff --git a/src/tint/lang/hlsl/writer/function_test.cc b/src/tint/lang/hlsl/writer/function_test.cc
index 594a37a..5a2e0c7 100644
--- a/src/tint/lang/hlsl/writer/function_test.cc
+++ b/src/tint/lang/hlsl/writer/function_test.cc
@@ -869,9 +869,10 @@
 
     ASSERT_TRUE(Generate()) << err_ << output_.hlsl;
     EXPECT_EQ(output_.hlsl, R"(
+static bool continue_execution = true;
 void my_func(int a) {
   if ((a == 0)) {
-    discard;
+    continue_execution = false;
   }
 }
 
diff --git a/src/tint/lang/hlsl/writer/raise/BUILD.bazel b/src/tint/lang/hlsl/writer/raise/BUILD.bazel
index a152b37..3f7094e 100644
--- a/src/tint/lang/hlsl/writer/raise/BUILD.bazel
+++ b/src/tint/lang/hlsl/writer/raise/BUILD.bazel
@@ -57,6 +57,7 @@
   deps = [
     "//src/tint/api/common",
     "//src/tint/lang/core",
+    "//src/tint/lang/core/common",
     "//src/tint/lang/core/constant",
     "//src/tint/lang/core/intrinsic",
     "//src/tint/lang/core/ir",
diff --git a/src/tint/lang/hlsl/writer/raise/BUILD.cmake b/src/tint/lang/hlsl/writer/raise/BUILD.cmake
index 90c19e8..5d38ac3 100644
--- a/src/tint/lang/hlsl/writer/raise/BUILD.cmake
+++ b/src/tint/lang/hlsl/writer/raise/BUILD.cmake
@@ -56,6 +56,7 @@
 tint_target_add_dependencies(tint_lang_hlsl_writer_raise lib
   tint_api_common
   tint_lang_core
+  tint_lang_core_common
   tint_lang_core_constant
   tint_lang_core_intrinsic
   tint_lang_core_ir
diff --git a/src/tint/lang/hlsl/writer/raise/BUILD.gn b/src/tint/lang/hlsl/writer/raise/BUILD.gn
index 6f30f45..4a4d467 100644
--- a/src/tint/lang/hlsl/writer/raise/BUILD.gn
+++ b/src/tint/lang/hlsl/writer/raise/BUILD.gn
@@ -60,6 +60,7 @@
   deps = [
     "${tint_src_dir}/api/common",
     "${tint_src_dir}/lang/core",
+    "${tint_src_dir}/lang/core/common",
     "${tint_src_dir}/lang/core/constant",
     "${tint_src_dir}/lang/core/intrinsic",
     "${tint_src_dir}/lang/core/ir",
diff --git a/src/tint/lang/hlsl/writer/raise/raise.cc b/src/tint/lang/hlsl/writer/raise/raise.cc
index 257a973..e4db631 100644
--- a/src/tint/lang/hlsl/writer/raise/raise.cc
+++ b/src/tint/lang/hlsl/writer/raise/raise.cc
@@ -27,10 +27,23 @@
 
 #include "src/tint/lang/hlsl/writer/raise/raise.h"
 
+#include <unordered_set>
+
 #include "src/tint/lang/core/ir/transform/add_empty_entry_point.h"
+#include "src/tint/lang/core/ir/transform/array_length_from_uniform.h"
 #include "src/tint/lang/core/ir/transform/binary_polyfill.h"
+#include "src/tint/lang/core/ir/transform/binding_remapper.h"
+#include "src/tint/lang/core/ir/transform/builtin_polyfill.h"
+#include "src/tint/lang/core/ir/transform/conversion_polyfill.h"
+#include "src/tint/lang/core/ir/transform/demote_to_helper.h"
+#include "src/tint/lang/core/ir/transform/multiplanar_external_texture.h"
 #include "src/tint/lang/core/ir/transform/remove_terminator_args.h"
+#include "src/tint/lang/core/ir/transform/rename_conflicts.h"
+#include "src/tint/lang/core/ir/transform/robustness.h"
 #include "src/tint/lang/core/ir/transform/value_to_let.h"
+#include "src/tint/lang/core/ir/transform/vectorize_scalar_matrix_constructors.h"
+#include "src/tint/lang/core/ir/transform/zero_init_workgroup_memory.h"
+#include "src/tint/lang/hlsl/writer/common/option_helpers.h"
 #include "src/tint/lang/hlsl/writer/common/options.h"
 #include "src/tint/lang/hlsl/writer/raise/builtin_polyfill.h"
 #include "src/tint/lang/hlsl/writer/raise/decompose_memory_access.h"
@@ -50,6 +63,26 @@
         }                                          \
     } while (false)
 
+    tint::transform::multiplanar::BindingsMap multiplanar_map{};
+    RemapperData remapper_data{};
+    ArrayLengthFromUniformOptions array_length_from_uniform_options{};
+    PopulateBindingRelatedOptions(options, remapper_data, multiplanar_map,
+                                  array_length_from_uniform_options);
+
+    {
+        auto result = core::ir::transform::ArrayLengthFromUniform(
+            module,
+            BindingPoint{array_length_from_uniform_options.ubo_binding.group,
+                         array_length_from_uniform_options.ubo_binding.binding},
+            array_length_from_uniform_options.bindpoint_to_size_index);
+        if (result != Success) {
+            return Failure();
+        }
+    }
+
+    RUN_TRANSFORM(core::ir::transform::BindingRemapper, remapper_data);
+    RUN_TRANSFORM(core::ir::transform::MultiplanarExternalTexture, multiplanar_map);
+
     {
         core::ir::transform::BinaryPolyfillConfig binary_polyfills{};
         binary_polyfills.int_div_mod = !options.disable_polyfill_integer_div_mod;
@@ -57,6 +90,46 @@
         RUN_TRANSFORM(core::ir::transform::BinaryPolyfill, binary_polyfills);
     }
 
+    {
+        // TODO(dsinclair): Add missing polyfills
+
+        core::ir::transform::BuiltinPolyfillConfig core_polyfills{};
+        // core_polyfills.acosh = ast::transform::BuiltinPolyfill::Level::kFull;
+        // core_polyfills.asinh = true;
+        // core_polyfills.atanh = ast::transform::BuiltinPolyfill::Level::kFull;
+        // core_polyfills.bitshift_modulo = true;
+        core_polyfills.clamp_int = true;
+        core_polyfills.dot_4x8_packed = options.polyfill_dot_4x8_packed;
+
+        // TODO(crbug.com/tint/1449): Some of these can map to HLSL's `firstbitlow`
+        // and `firstbithigh`.
+        core_polyfills.count_leading_zeros = true;
+        core_polyfills.count_trailing_zeros = true;
+        core_polyfills.extract_bits = core::ir::transform::BuiltinPolyfillLevel::kFull;
+        core_polyfills.first_leading_bit = true;
+        core_polyfills.first_trailing_bit = true;
+        // core_polyfills.fwidth_fine = true;
+        core_polyfills.insert_bits = core::ir::transform::BuiltinPolyfillLevel::kFull;
+        // core_polyfills.int_div_mod = !options.disable_polyfill_integer_div_mod;
+
+        // Currently Pack4xU8Clamp() must be polyfilled because on latest DXC pack_clamp_u8()
+        // receives an int32_t4 as its input.
+        // See https://github.com/microsoft/DirectXShaderCompiler/issues/5091 for more details.
+        core_polyfills.pack_4xu8_clamp = true;
+        core_polyfills.pack_unpack_4x8 = options.polyfill_pack_unpack_4x8;
+        // core_polyfills.precise_float_mod = true;
+        // core_polyfills.reflect_vec2_f32 = options.polyfill_reflect_vec2_f32;
+        core_polyfills.texture_sample_base_clamp_to_edge_2d_f32 = true;
+        // core_polyfills.workgroup_uniform_load = true;
+        RUN_TRANSFORM(core::ir::transform::BuiltinPolyfill, core_polyfills);
+    }
+
+    {
+        core::ir::transform::ConversionPolyfillConfig conversion_polyfills{};
+        conversion_polyfills.ftoi = true;
+        RUN_TRANSFORM(core::ir::transform::ConversionPolyfill, conversion_polyfills);
+    }
+
     RUN_TRANSFORM(core::ir::transform::AddEmptyEntryPoint);
 
     RUN_TRANSFORM(raise::DecomposeMemoryAccess);
@@ -65,12 +138,36 @@
         RUN_TRANSFORM(raise::FxcPolyfill);
     }
 
+    if (!options.disable_robustness) {
+        core::ir::transform::RobustnessConfig config{};
+        config.bindings_ignored = std::unordered_set<BindingPoint>(
+            options.bindings.ignored_by_robustness_transform.cbegin(),
+            options.bindings.ignored_by_robustness_transform.cend());
+
+        // Direct3D guarantees to return zero for any resource that is accessed out of bounds, and
+        // according to the description of the assembly store_uav_typed, out of bounds addressing
+        // means nothing gets written to memory.
+        //
+        // TODO(dsinclair): Need to translate this into new robustness.
+        // config.texture_action = ast::transform::Robustness::Action::kIgnore;
+
+        RUN_TRANSFORM(core::ir::transform::Robustness, config);
+    }
+    if (!options.disable_workgroup_init) {
+        RUN_TRANSFORM(core::ir::transform::ZeroInitWorkgroupMemory);
+    }
+
     RUN_TRANSFORM(raise::ShaderIO);
     RUN_TRANSFORM(raise::BuiltinPolyfill);
+    RUN_TRANSFORM(core::ir::transform::VectorizeScalarMatrixConstructors);
+
+    // DemoteToHelper must come before any transform that introduces non-core instructions.
+    RUN_TRANSFORM(core::ir::transform::DemoteToHelper);
 
     // These transforms need to be run last as various transforms introduce terminator arguments,
     // naming conflicts, and expressions that need to be explicitly not inlined.
     RUN_TRANSFORM(core::ir::transform::RemoveTerminatorArgs);
+    RUN_TRANSFORM(core::ir::transform::RenameConflicts);
     RUN_TRANSFORM(core::ir::transform::ValueToLet);
 
     // Anything which runs after this needs to handle `Capabilities::kAllowModuleScopedLets`