[tint][fuzz] Use renamer transform if running HLSL output through DXC

Without pre-transforming with the renamer HLSL can fail to validate easily.

Change-Id: Ia569a5f5d38e40872e4af9f081c70157223d3c74
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/190920
Auto-Submit: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/lang/hlsl/writer/BUILD.cmake b/src/tint/lang/hlsl/writer/BUILD.cmake
index 67af1a7..5c44e5f 100644
--- a/src/tint/lang/hlsl/writer/BUILD.cmake
+++ b/src/tint/lang/hlsl/writer/BUILD.cmake
@@ -155,6 +155,7 @@
   tint_lang_hlsl_writer_common
   tint_lang_wgsl
   tint_lang_wgsl_ast
+  tint_lang_wgsl_ast_transform
   tint_lang_wgsl_features
   tint_lang_wgsl_program
   tint_lang_wgsl_sem
diff --git a/src/tint/lang/hlsl/writer/BUILD.gn b/src/tint/lang/hlsl/writer/BUILD.gn
index 2111f1c..263c7f2 100644
--- a/src/tint/lang/hlsl/writer/BUILD.gn
+++ b/src/tint/lang/hlsl/writer/BUILD.gn
@@ -136,6 +136,7 @@
       "${tint_src_dir}/lang/hlsl/writer/common",
       "${tint_src_dir}/lang/wgsl",
       "${tint_src_dir}/lang/wgsl/ast",
+      "${tint_src_dir}/lang/wgsl/ast/transform",
       "${tint_src_dir}/lang/wgsl/features",
       "${tint_src_dir}/lang/wgsl/program",
       "${tint_src_dir}/lang/wgsl/sem",
diff --git a/src/tint/lang/hlsl/writer/writer_ast_fuzz.cc b/src/tint/lang/hlsl/writer/writer_ast_fuzz.cc
index 568a636..2c360d3 100644
--- a/src/tint/lang/hlsl/writer/writer_ast_fuzz.cc
+++ b/src/tint/lang/hlsl/writer/writer_ast_fuzz.cc
@@ -27,6 +27,8 @@
 
 // GEN_BUILD:CONDITION(tint_build_wgsl_reader)
 
+#include <iostream>
+#include <sstream>
 #include <string>
 #include <unordered_map>
 
@@ -34,7 +36,9 @@
 #include "src/tint/lang/hlsl/validate/validate.h"
 #include "src/tint/lang/hlsl/writer/writer.h"
 #include "src/tint/lang/wgsl/ast/module.h"
+#include "src/tint/lang/wgsl/ast/transform/renamer.h"
 #include "src/tint/utils/command/command.h"
+#include "src/tint/utils/text/string.h"
 
 namespace tint::hlsl::writer {
 namespace {
@@ -44,38 +48,61 @@
         return;
     }
 
-    auto res = tint::hlsl::writer::Generate(program, options);
-    if (res == Success) {
-        const char* dxc_path = validate::kDxcDLLName;
-        bool must_validate = false;
-        if (!context.options.dxc.empty()) {
-            must_validate = true;
-            dxc_path = context.options.dxc.c_str();
+    // Currently disabled, as DXC can error on HLSL emitted by Tint. For example: post optimization
+    // infinite loops will fail to compile, but these are beyond Tint's analysis capabilities.
+    constexpr bool must_validate = false;
+
+    const char* dxc_path = validate::kDxcDLLName;
+    Result<tint::hlsl::writer::Output> res;
+    if (!context.options.dxc.empty()) {
+        dxc_path = context.options.dxc.c_str();
+        ast::transform::DataMap inputs, outputs;
+        inputs.Add<ast::transform::Renamer::Config>(ast::transform::Renamer::Target::kHlslKeywords,
+                                                    /* preserve_unicode */ false);
+        if (auto renamer_res = tint::ast::transform::Renamer{}.Apply(program, inputs, outputs)) {
+            if (!renamer_res->IsValid()) {
+                TINT_ICE() << renamer_res->Diagnostics();
+            }
+            res = tint::hlsl::writer::Generate(*renamer_res, options);
         }
 
-        auto dxc = tint::Command::LookPath(dxc_path);
-        if (dxc.Found()) {
-            uint32_t hlsl_shader_model = 60;
-            bool require_16bit_types = false;
-            auto enable_list = program.AST().Enables();
-            for (auto* enable : enable_list) {
-                if (enable->HasExtension(tint::wgsl::Extension::kF16)) {
-                    hlsl_shader_model = 62;
-                    require_16bit_types = true;
-                    break;
-                }
+    } else {
+        res = tint::hlsl::writer::Generate(program, options);
+    }
+
+    if (res != Success) {
+        return;
+    }
+
+    auto dxc = tint::Command::LookPath(dxc_path);
+    if (dxc.Found()) {
+        uint32_t hlsl_shader_model = 60;
+        bool require_16bit_types = false;
+        auto enable_list = program.AST().Enables();
+        for (auto* enable : enable_list) {
+            if (enable->HasExtension(tint::wgsl::Extension::kF16)) {
+                hlsl_shader_model = 62;
+                require_16bit_types = true;
+                break;
             }
-
-            auto validate_res = validate::ValidateUsingDXC(dxc.Path(), res->hlsl, res->entry_points,
-                                                           require_16bit_types, hlsl_shader_model);
-
-            if (must_validate && validate_res.failed) {
-                TINT_ICE() << "DXC was expected to succeed, but failed: " << validate_res.output;
-            }
-
-        } else if (must_validate) {
-            TINT_ICE() << "DXC path was explicitly specified, but was not found: " << dxc_path;
         }
+
+        auto validate_res = validate::ValidateUsingDXC(dxc.Path(), res->hlsl, res->entry_points,
+                                                       require_16bit_types, hlsl_shader_model);
+
+        if (must_validate && validate_res.failed) {
+            size_t line_num = 1;
+            std::stringstream err;
+            err << "DXC was expected to succeed, but failed:\n\n";
+            for (auto line : Split(res->hlsl, "\n")) {
+                err << line_num++ << ": " << line << "\n";
+            }
+            err << "\n\n" << validate_res.output;
+            TINT_ICE() << err.str();
+        }
+
+    } else if (must_validate) {
+        TINT_ICE() << "cannot validate with DXC as it was not found at: " << dxc_path;
     }
 }