Stub sanitizer transforms for the hlsl, msl & spirv writers

These transforms will perform work to massage the Program into something consumable by the given writer.

Change-Id: I8989e8d4bc1a9cae7ce1f8764c8f3811db3bd04d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41483
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 699411a..4147124 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -539,6 +539,8 @@
 
 source_set("libtint_spv_writer_src") {
   sources = [
+    "src/transform/spirv.cc",
+    "src/transform/spirv.h",
     "src/writer/spirv/binary_writer.cc",
     "src/writer/spirv/binary_writer.h",
     "src/writer/spirv/builder.cc",
@@ -609,6 +611,8 @@
 
 source_set("libtint_msl_writer_src") {
   sources = [
+    "src/transform/msl.cc",
+    "src/transform/msl.h",
     "src/writer/msl/generator.cc",
     "src/writer/msl/generator.h",
     "src/writer/msl/generator_impl.cc",
@@ -630,6 +634,8 @@
 
 source_set("libtint_hlsl_writer_src") {
   sources = [
+    "src/transform/hlsl.cc",
+    "src/transform/hlsl.h",
     "src/writer/hlsl/generator.cc",
     "src/writer/hlsl/generator.h",
     "src/writer/hlsl/generator_impl.cc",
diff --git a/include/tint/tint.h b/include/tint/tint.h
index 19c69d1..74f2b15 100644
--- a/include/tint/tint.h
+++ b/include/tint/tint.h
@@ -44,6 +44,7 @@
 
 #if TINT_BUILD_SPV_WRITER
 #include "spirv-tools/libspirv.hpp"
+#include "src/transform/spirv.h"
 #include "src/writer/spirv/generator.h"
 #endif  // TINT_BUILD_SPV_WRITER
 
@@ -52,10 +53,12 @@
 #endif  // TINT_BUILD_WGSL_WRITER
 
 #if TINT_BUILD_MSL_WRITER
+#include "src/transform/msl.h"
 #include "src/writer/msl/generator.h"
 #endif  // TINT_BUILD_MSL_WRITER
 
 #if TINT_BUILD_HLSL_WRITER
+#include "src/transform/hlsl.h"
 #include "src/writer/hlsl/generator.h"
 #endif  // TINT_BUILD_HLSL_WRITER
 
diff --git a/samples/main.cc b/samples/main.cc
index 3c6c5b5..182f42c 100644
--- a/samples/main.cc
+++ b/samples/main.cc
@@ -538,6 +538,27 @@
     }
   }
 
+  switch (options.format) {
+#if TINT_BUILD_SPV_WRITER
+    case Format::kSpirv:
+    case Format::kSpvAsm:
+      transform_manager.append(std::make_unique<tint::transform::Spirv>());
+      break;
+#endif  // TINT_BUILD_SPV_WRITER
+#if TINT_BUILD_MSL_WRITER
+    case Format::kMsl:
+      transform_manager.append(std::make_unique<tint::transform::Msl>());
+      break;
+#endif  // TINT_BUILD_MSL_WRITER
+#if TINT_BUILD_HLSL_WRITER
+    case Format::kHlsl:
+      transform_manager.append(std::make_unique<tint::transform::Hlsl>());
+      break;
+#endif  // TINT_BUILD_HLSL_WRITER
+    default:
+      break;
+  }
+
   auto out = transform_manager.Run(program.get());
   if (out.diagnostics.contains_errors()) {
     diag_formatter.format(out.diagnostics, diag_printer.get());
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7458513..4026aac 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -329,6 +329,8 @@
 
 if(${TINT_BUILD_SPV_WRITER})
   list(APPEND TINT_LIB_SRCS
+    transform/spirv.cc
+    transform/spirv.h
     writer/spirv/binary_writer.cc
     writer/spirv/binary_writer.h
     writer/spirv/builder.cc
@@ -355,6 +357,8 @@
 
 if(${TINT_BUILD_MSL_WRITER})
   list(APPEND TINT_LIB_SRCS
+    transform/msl.cc
+    transform/msl.h
     writer/msl/generator.cc
     writer/msl/generator.h
     writer/msl/generator_impl.cc
@@ -366,6 +370,8 @@
 
 if(${TINT_BUILD_HLSL_WRITER})
   list(APPEND TINT_LIB_SRCS
+    transform/hlsl.cc
+    transform/hlsl.h
     writer/hlsl/generator.cc
     writer/hlsl/generator.h
     writer/hlsl/generator_impl.cc
diff --git a/src/transform/hlsl.cc b/src/transform/hlsl.cc
new file mode 100644
index 0000000..acaba88
--- /dev/null
+++ b/src/transform/hlsl.cc
@@ -0,0 +1,34 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/transform/hlsl.h"
+
+#include <utility>
+
+#include "src/program_builder.h"
+
+namespace tint {
+namespace transform {
+
+Hlsl::Hlsl() = default;
+Hlsl::~Hlsl() = default;
+
+Transform::Output Hlsl::Run(const Program* in) {
+  ProgramBuilder out;
+  CloneContext(&out, in).Clone();
+  return Output{Program(std::move(out))};
+}
+
+}  // namespace transform
+}  // namespace tint
diff --git a/src/transform/hlsl.h b/src/transform/hlsl.h
new file mode 100644
index 0000000..c8a04cc
--- /dev/null
+++ b/src/transform/hlsl.h
@@ -0,0 +1,44 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_TRANSFORM_HLSL_H_
+#define SRC_TRANSFORM_HLSL_H_
+
+#include "src/transform/transform.h"
+
+namespace tint {
+namespace transform {
+
+/// Hlsl is a transform used to sanitize a Program for use with the Hlsl writer.
+/// Passing a non-sanitized Program to the Hlsl writer will result in undefined
+/// behavior.
+class Hlsl : public Transform {
+ public:
+  /// Constructor
+  Hlsl();
+  ~Hlsl() override;
+
+  /// Runs the transform on `program`, returning the transformation result.
+  /// @note Users of Tint should register the transform with transform manager
+  /// and invoke its Run(), instead of directly calling the transform's Run().
+  /// Calling Run() directly does not perform program state cleanup operations.
+  /// @param program the source program to transform
+  /// @returns the transformation result
+  Output Run(const Program* program) override;
+};
+
+}  // namespace transform
+}  // namespace tint
+
+#endif  // SRC_TRANSFORM_HLSL_H_
diff --git a/src/transform/msl.cc b/src/transform/msl.cc
new file mode 100644
index 0000000..bb6d8e9
--- /dev/null
+++ b/src/transform/msl.cc
@@ -0,0 +1,34 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/transform/msl.h"
+
+#include <utility>
+
+#include "src/program_builder.h"
+
+namespace tint {
+namespace transform {
+
+Msl::Msl() = default;
+Msl::~Msl() = default;
+
+Transform::Output Msl::Run(const Program* in) {
+  ProgramBuilder out;
+  CloneContext(&out, in).Clone();
+  return Output{Program(std::move(out))};
+}
+
+}  // namespace transform
+}  // namespace tint
diff --git a/src/transform/msl.h b/src/transform/msl.h
new file mode 100644
index 0000000..b437608
--- /dev/null
+++ b/src/transform/msl.h
@@ -0,0 +1,44 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_TRANSFORM_MSL_H_
+#define SRC_TRANSFORM_MSL_H_
+
+#include "src/transform/transform.h"
+
+namespace tint {
+namespace transform {
+
+/// Msl is a transform used to sanitize a Program for use with the Msl writer.
+/// Passing a non-sanitized Program to the Msl writer will result in undefined
+/// behavior.
+class Msl : public Transform {
+ public:
+  /// Constructor
+  Msl();
+  ~Msl() override;
+
+  /// Runs the transform on `program`, returning the transformation result.
+  /// @note Users of Tint should register the transform with transform manager
+  /// and invoke its Run(), instead of directly calling the transform's Run().
+  /// Calling Run() directly does not perform program state cleanup operations.
+  /// @param program the source program to transform
+  /// @returns the transformation result
+  Output Run(const Program* program) override;
+};
+
+}  // namespace transform
+}  // namespace tint
+
+#endif  // SRC_TRANSFORM_MSL_H_
diff --git a/src/transform/spirv.cc b/src/transform/spirv.cc
new file mode 100644
index 0000000..07797d9
--- /dev/null
+++ b/src/transform/spirv.cc
@@ -0,0 +1,34 @@
+// Copyright 2020 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/transform/spirv.h"
+
+#include <utility>
+
+#include "src/program_builder.h"
+
+namespace tint {
+namespace transform {
+
+Spirv::Spirv() = default;
+Spirv::~Spirv() = default;
+
+Transform::Output Spirv::Run(const Program* in) {
+  ProgramBuilder out;
+  CloneContext(&out, in).Clone();
+  return Output{Program(std::move(out))};
+}
+
+}  // namespace transform
+}  // namespace tint
diff --git a/src/transform/spirv.h b/src/transform/spirv.h
new file mode 100644
index 0000000..3644445
--- /dev/null
+++ b/src/transform/spirv.h
@@ -0,0 +1,44 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_TRANSFORM_SPIRV_H_
+#define SRC_TRANSFORM_SPIRV_H_
+
+#include "src/transform/transform.h"
+
+namespace tint {
+namespace transform {
+
+/// Spirv is a transform used to sanitize a Program for use with the Spirv
+/// writer. Passing a non-sanitized Program to the Spirv writer will result in
+/// undefined behavior.
+class Spirv : public Transform {
+ public:
+  /// Constructor
+  Spirv();
+  ~Spirv() override;
+
+  /// Runs the transform on `program`, returning the transformation result.
+  /// @note Users of Tint should register the transform with transform manager
+  /// and invoke its Run(), instead of directly calling the transform's Run().
+  /// Calling Run() directly does not perform program state cleanup operations.
+  /// @param program the source program to transform
+  /// @returns the transformation result
+  Output Run(const Program* program) override;
+};
+
+}  // namespace transform
+}  // namespace tint
+
+#endif  // SRC_TRANSFORM_SPIRV_H_