[ir] Convert MergeReturn to a free function

Bug: tint:1718
Change-Id: Id8ab5143f7b53e5fb86ce78fb6aa8b48d0e0b4f4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/143822
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/core/ir/transform/merge_return.cc b/src/tint/lang/core/ir/transform/merge_return.cc
index 4ba6bf6..7534f48 100644
--- a/src/tint/lang/core/ir/transform/merge_return.cc
+++ b/src/tint/lang/core/ir/transform/merge_return.cc
@@ -18,23 +18,20 @@
 
 #include "src/tint/lang/core/ir/builder.h"
 #include "src/tint/lang/core/ir/module.h"
+#include "src/tint/lang/core/ir/validator.h"
 #include "src/tint/utils/containers/reverse.h"
 #include "src/tint/utils/containers/transform.h"
 #include "src/tint/utils/rtti/switch.h"
 
-TINT_INSTANTIATE_TYPEINFO(tint::ir::transform::MergeReturn);
-
 using namespace tint::builtin::fluent_types;  // NOLINT
 using namespace tint::number_suffixes;        // NOLINT
 
 namespace tint::ir::transform {
 
-MergeReturn::MergeReturn() = default;
-
-MergeReturn::~MergeReturn() = default;
+namespace {
 
 /// PIMPL state for the transform, for a single function.
-struct MergeReturn::State {
+struct State {
     /// The IR module.
     Module* ir = nullptr;
 
@@ -287,11 +284,20 @@
     }
 };
 
-void MergeReturn::Run(Module* ir) const {
+}  // namespace
+
+Result<SuccessType, std::string> MergeReturn(Module* ir) {
+    auto result = ValidateAndDumpIfNeeded(*ir, "MergeReturn transform");
+    if (!result) {
+        return result;
+    }
+
     // Process each function.
     for (auto* fn : ir->functions) {
         State{ir}.Process(fn);
     }
+
+    return Success;
 }
 
 }  // namespace tint::ir::transform
diff --git a/src/tint/lang/core/ir/transform/merge_return.h b/src/tint/lang/core/ir/transform/merge_return.h
index c86f116..aa8c564 100644
--- a/src/tint/lang/core/ir/transform/merge_return.h
+++ b/src/tint/lang/core/ir/transform/merge_return.h
@@ -15,25 +15,22 @@
 #ifndef SRC_TINT_LANG_CORE_IR_TRANSFORM_MERGE_RETURN_H_
 #define SRC_TINT_LANG_CORE_IR_TRANSFORM_MERGE_RETURN_H_
 
-#include "src/tint/lang/core/ir/transform/transform.h"
+#include <string>
+
+#include "src/tint/utils/result/result.h"
+
+// Forward declarations.
+namespace tint::ir {
+class Module;
+}
 
 namespace tint::ir::transform {
 
 /// MergeReturn is a transform merges multiple return statements in a function into a single return
 /// at the end of the function.
-class MergeReturn final : public Castable<MergeReturn, Transform> {
-  public:
-    /// Constructor
-    MergeReturn();
-    /// Destructor
-    ~MergeReturn() override;
-
-    /// @copydoc Transform::Run
-    void Run(ir::Module* module) const override;
-
-  private:
-    struct State;
-};
+/// @param module the module to transform
+/// @returns an error string on failure
+Result<SuccessType, std::string> MergeReturn(Module* module);
 
 }  // namespace tint::ir::transform
 
diff --git a/src/tint/lang/core/ir/transform/merge_return_test.cc b/src/tint/lang/core/ir/transform/merge_return_test.cc
index e36c2ff..6724f09 100644
--- a/src/tint/lang/core/ir/transform/merge_return_test.cc
+++ b/src/tint/lang/core/ir/transform/merge_return_test.cc
@@ -45,7 +45,7 @@
 
     auto* expect = src;
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -85,7 +85,7 @@
 
     auto* expect = src;
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -142,7 +142,7 @@
 
     auto* expect = src;
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -193,7 +193,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -246,7 +246,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -309,7 +309,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -373,7 +373,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -437,7 +437,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -489,7 +489,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -563,7 +563,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -639,7 +639,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -785,7 +785,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -906,7 +906,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1054,7 +1054,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1098,7 +1098,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1212,7 +1212,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1317,7 +1317,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1431,7 +1431,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1495,7 +1495,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1604,7 +1604,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1687,7 +1687,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1749,7 +1749,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
@@ -1822,7 +1822,7 @@
 }
 )";
 
-    Run<MergeReturn>();
+    Run(MergeReturn);
 
     EXPECT_EQ(expect, str());
 }
diff --git a/src/tint/lang/spirv/writer/printer/printer.cc b/src/tint/lang/spirv/writer/printer/printer.cc
index f13488e..ecf24cb 100644
--- a/src/tint/lang/spirv/writer/printer/printer.cc
+++ b/src/tint/lang/spirv/writer/printer/printer.cc
@@ -111,8 +111,8 @@
     ir::transform::DemoteToHelper{}.Run(module);
     ir::transform::ExpandImplicitSplats{}.Run(module);
     ir::transform::HandleMatrixArithmetic{}.Run(module);
-    ir::transform::MergeReturn{}.Run(module);
 
+    RUN_TRANSFORM(MergeReturn);
     RUN_TRANSFORM(ShaderIOSpirv);
     RUN_TRANSFORM(Std140);
     RUN_TRANSFORM(VarForDynamicIndex);