[tint][ir] Call wgsl Lower() and Raise() where needed

Change-Id: Ib5fa6e0d791a5173fb324358674fd0aeb4159010
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/153600
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/fuzzers/tint_ir_roundtrip_fuzzer.cc b/src/tint/fuzzers/tint_ir_roundtrip_fuzzer.cc
index 58cd879..0f1cc86 100644
--- a/src/tint/fuzzers/tint_ir_roundtrip_fuzzer.cc
+++ b/src/tint/fuzzers/tint_ir_roundtrip_fuzzer.cc
@@ -17,9 +17,11 @@
 #include <unordered_set>
 
 #include "src/tint/fuzzers/apply_substitute_overrides.h"
+#include "src/tint/lang/wgsl/reader/lower/lower.h"
 #include "src/tint/lang/wgsl/reader/parser/parser.h"
 #include "src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.h"
 #include "src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.h"
+#include "src/tint/lang/wgsl/writer/raise/raise.h"
 #include "src/tint/lang/wgsl/writer/writer.h"
 
 [[noreturn]] void TintInternalCompilerErrorReporter(const tint::InternalCompilerError& err) {
@@ -77,6 +79,16 @@
         __builtin_trap();
     }
 
+    if (auto res = tint::wgsl::reader::Lower(ir.Get()); !res) {
+        std::cerr << res.Failure() << std::endl;
+        __builtin_trap();
+    }
+
+    if (auto res = tint::wgsl::writer::Raise(ir.Get()); !res) {
+        std::cerr << res.Failure() << std::endl;
+        __builtin_trap();
+    }
+
     auto dst = tint::wgsl::writer::IRToProgram(ir.Get());
     if (!dst.IsValid()) {
 #if TINT_BUILD_WGSL_WRITER
diff --git a/src/tint/lang/msl/writer/BUILD.bazel b/src/tint/lang/msl/writer/BUILD.bazel
index b3e89ef..4348b90 100644
--- a/src/tint/lang/msl/writer/BUILD.bazel
+++ b/src/tint/lang/msl/writer/BUILD.bazel
@@ -44,6 +44,7 @@
     "//src/tint/lang/wgsl",
     "//src/tint/lang/wgsl/ast",
     "//src/tint/lang/wgsl/program",
+    "//src/tint/lang/wgsl/reader/lower",
     "//src/tint/lang/wgsl/reader/program_to_ir",
     "//src/tint/lang/wgsl/sem",
     "//src/tint/utils/containers",
diff --git a/src/tint/lang/msl/writer/BUILD.cmake b/src/tint/lang/msl/writer/BUILD.cmake
index 15752d1..d3291bc 100644
--- a/src/tint/lang/msl/writer/BUILD.cmake
+++ b/src/tint/lang/msl/writer/BUILD.cmake
@@ -51,6 +51,7 @@
   tint_lang_wgsl
   tint_lang_wgsl_ast
   tint_lang_wgsl_program
+  tint_lang_wgsl_reader_lower
   tint_lang_wgsl_reader_program_to_ir
   tint_lang_wgsl_sem
   tint_utils_containers
diff --git a/src/tint/lang/msl/writer/BUILD.gn b/src/tint/lang/msl/writer/BUILD.gn
index 560486c..9f13b86 100644
--- a/src/tint/lang/msl/writer/BUILD.gn
+++ b/src/tint/lang/msl/writer/BUILD.gn
@@ -43,6 +43,7 @@
       "${tint_src_dir}/lang/wgsl",
       "${tint_src_dir}/lang/wgsl/ast",
       "${tint_src_dir}/lang/wgsl/program",
+      "${tint_src_dir}/lang/wgsl/reader/lower",
       "${tint_src_dir}/lang/wgsl/reader/program_to_ir",
       "${tint_src_dir}/lang/wgsl/sem",
       "${tint_src_dir}/utils/containers",
diff --git a/src/tint/lang/msl/writer/writer.cc b/src/tint/lang/msl/writer/writer.cc
index aa69687..54fffe0 100644
--- a/src/tint/lang/msl/writer/writer.cc
+++ b/src/tint/lang/msl/writer/writer.cc
@@ -20,6 +20,7 @@
 #include "src/tint/lang/msl/writer/ast_printer/ast_printer.h"
 #include "src/tint/lang/msl/writer/printer/printer.h"
 #include "src/tint/lang/msl/writer/raise/raise.h"
+#include "src/tint/lang/wgsl/reader/lower/lower.h"
 #include "src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.h"
 
 namespace tint::msl::writer {
@@ -40,10 +41,14 @@
 
         auto ir = converted.Move();
 
-        // Raise the IR to the MSL dialect.
-        auto raised = raise::Raise(ir);
-        if (!raised) {
-            return raised.Failure();
+        // Lower from WGSL-dialect to core-dialect
+        if (auto res = wgsl::reader::Lower(ir); !res) {
+            return res.Failure();
+        }
+
+        // Raise from core-dialect to MSL-dialect.
+        if (auto res = raise::Raise(ir); !res) {
+            return res.Failure();
         }
 
         // Generate the MSL code.
diff --git a/src/tint/lang/spirv/writer/BUILD.bazel b/src/tint/lang/spirv/writer/BUILD.bazel
index 4123059..3f9d6e9 100644
--- a/src/tint/lang/spirv/writer/BUILD.bazel
+++ b/src/tint/lang/spirv/writer/BUILD.bazel
@@ -48,6 +48,7 @@
     "//src/tint/lang/wgsl",
     "//src/tint/lang/wgsl/ast",
     "//src/tint/lang/wgsl/program",
+    "//src/tint/lang/wgsl/reader/lower",
     "//src/tint/lang/wgsl/reader/program_to_ir",
     "//src/tint/lang/wgsl/sem",
     "//src/tint/utils/containers",
diff --git a/src/tint/lang/spirv/writer/BUILD.cmake b/src/tint/lang/spirv/writer/BUILD.cmake
index d62c4a1..5dd6423 100644
--- a/src/tint/lang/spirv/writer/BUILD.cmake
+++ b/src/tint/lang/spirv/writer/BUILD.cmake
@@ -55,6 +55,7 @@
   tint_lang_wgsl
   tint_lang_wgsl_ast
   tint_lang_wgsl_program
+  tint_lang_wgsl_reader_lower
   tint_lang_wgsl_reader_program_to_ir
   tint_lang_wgsl_sem
   tint_utils_containers
diff --git a/src/tint/lang/spirv/writer/BUILD.gn b/src/tint/lang/spirv/writer/BUILD.gn
index a7dd432..bf60f83 100644
--- a/src/tint/lang/spirv/writer/BUILD.gn
+++ b/src/tint/lang/spirv/writer/BUILD.gn
@@ -51,6 +51,7 @@
       "${tint_src_dir}/lang/wgsl",
       "${tint_src_dir}/lang/wgsl/ast",
       "${tint_src_dir}/lang/wgsl/program",
+      "${tint_src_dir}/lang/wgsl/reader/lower",
       "${tint_src_dir}/lang/wgsl/reader/program_to_ir",
       "${tint_src_dir}/lang/wgsl/sem",
       "${tint_src_dir}/utils/containers",
diff --git a/src/tint/lang/spirv/writer/writer.cc b/src/tint/lang/spirv/writer/writer.cc
index 08a83fd..5f96226 100644
--- a/src/tint/lang/spirv/writer/writer.cc
+++ b/src/tint/lang/spirv/writer/writer.cc
@@ -21,6 +21,7 @@
 #include "src/tint/lang/spirv/writer/ast_printer/ast_printer.h"
 #include "src/tint/lang/spirv/writer/printer/printer.h"
 #include "src/tint/lang/spirv/writer/raise/raise.h"
+#include "src/tint/lang/wgsl/reader/lower/lower.h"
 #include "src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.h"
 
 // Included by 'ast_printer.h', included again here for './tools/run gen' track the dependency.
@@ -51,16 +52,20 @@
 
         auto ir = converted.Move();
 
+        // Lower from WGSL-dialect to core-dialect
+        if (auto res = wgsl::reader::Lower(ir); !res) {
+            return res.Failure();
+        }
+
         // Apply transforms as required by writer options.
         auto remapper = core::ir::transform::BindingRemapper(ir, options.binding_remapper_options);
         if (!remapper) {
             return remapper.Failure();
         }
 
-        // Raise the IR to the SPIR-V dialect.
-        auto raised = raise::Raise(ir, options);
-        if (!raised) {
-            return std::move(raised.Failure());
+        // Raise from core-dialect to SPIR-V-dialect.
+        if (auto res = raise::Raise(ir, options); !res) {
+            return std::move(res.Failure());
         }
 
         // Generate the SPIR-V code.
diff --git a/src/tint/lang/wgsl/writer/raise/raise.cc b/src/tint/lang/wgsl/writer/raise/raise.cc
index daacf36..5843d5f 100644
--- a/src/tint/lang/wgsl/writer/raise/raise.cc
+++ b/src/tint/lang/wgsl/writer/raise/raise.cc
@@ -21,7 +21,7 @@
 #include "src/tint/lang/wgsl/builtin_fn.h"
 #include "src/tint/lang/wgsl/ir/builtin_call.h"
 
-namespace tint::wgsl::writer::raise {
+namespace tint::wgsl::writer {
 namespace {
 
 wgsl::BuiltinFn Convert(core::BuiltinFn fn) {
@@ -166,4 +166,4 @@
     return Success;
 }
 
-}  // namespace tint::wgsl::writer::raise
+}  // namespace tint::wgsl::writer
diff --git a/src/tint/lang/wgsl/writer/raise/raise.h b/src/tint/lang/wgsl/writer/raise/raise.h
index a5b1487..46a68b5 100644
--- a/src/tint/lang/wgsl/writer/raise/raise.h
+++ b/src/tint/lang/wgsl/writer/raise/raise.h
@@ -19,13 +19,13 @@
 #include "src/tint/utils/diagnostic/diagnostic.h"
 #include "src/tint/utils/result/result.h"
 
-namespace tint::wgsl::writer::raise {
+namespace tint::wgsl::writer {
 
 /// Raise converts a core-dialect IR module to a WGSL-dialect IR module
 /// @param  mod the IR module
 /// @return the result of the operation
 Result<SuccessType> Raise(core::ir::Module& mod);
 
-}  // namespace tint::wgsl::writer::raise
+}  // namespace tint::wgsl::writer
 
 #endif  // SRC_TINT_LANG_WGSL_WRITER_RAISE_RAISE_H_
diff --git a/src/tint/lang/wgsl/writer/writer.cc b/src/tint/lang/wgsl/writer/writer.cc
index 737f173..94bdbbd 100644
--- a/src/tint/lang/wgsl/writer/writer.cc
+++ b/src/tint/lang/wgsl/writer/writer.cc
@@ -56,7 +56,7 @@
 
 Result<Output> WgslFromIR(core::ir::Module& module) {
     // core-dialect -> WGSL-dialect
-    if (auto res = raise::Raise(module); !res) {
+    if (auto res = Raise(module); !res) {
         return res.Failure();
     }