[msl] Update Raise to validate subgroup matrices.

This CL adds an MSL specific transform to validate that any used
subgroup matrices conform to the MSL requirements of being 8x8 and f32 or
f16.

Fixed: 459779146
Change-Id: Ic77740a5a8ab72fd6e8774892766c907f773db2e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/275094
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/msl/writer/raise/BUILD.bazel b/src/tint/lang/msl/writer/raise/BUILD.bazel
index c01e05a..0672101 100644
--- a/src/tint/lang/msl/writer/raise/BUILD.bazel
+++ b/src/tint/lang/msl/writer/raise/BUILD.bazel
@@ -49,6 +49,7 @@
     "raise.cc",
     "shader_io.cc",
     "simd_ballot.cc",
+    "validate_subgroup_matrix.cc",
   ],
   hdrs = [
     "argument_buffers.h",
@@ -61,6 +62,7 @@
     "raise.h",
     "shader_io.h",
     "simd_ballot.h",
+    "validate_subgroup_matrix.h",
   ],
   deps = [
     "//src/tint/api/common",
@@ -68,6 +70,7 @@
     "//src/tint/lang/core/constant",
     "//src/tint/lang/core/intrinsic",
     "//src/tint/lang/core/ir",
+    "//src/tint/lang/core/ir/analysis",
     "//src/tint/lang/core/ir/transform",
     "//src/tint/lang/core/type",
     "//src/tint/lang/msl",
@@ -107,6 +110,7 @@
     "packed_vec3_test.cc",
     "shader_io_test.cc",
     "simd_ballot_test.cc",
+    "validate_subgroup_matrix_test.cc",
   ],
   deps = [
     "//src/tint/api/common",
diff --git a/src/tint/lang/msl/writer/raise/BUILD.cmake b/src/tint/lang/msl/writer/raise/BUILD.cmake
index 18cbafa..73ddb9c 100644
--- a/src/tint/lang/msl/writer/raise/BUILD.cmake
+++ b/src/tint/lang/msl/writer/raise/BUILD.cmake
@@ -61,6 +61,8 @@
   lang/msl/writer/raise/shader_io.h
   lang/msl/writer/raise/simd_ballot.cc
   lang/msl/writer/raise/simd_ballot.h
+  lang/msl/writer/raise/validate_subgroup_matrix.cc
+  lang/msl/writer/raise/validate_subgroup_matrix.h
 )
 
 tint_target_add_dependencies(tint_lang_msl_writer_raise lib
@@ -69,6 +71,7 @@
   tint_lang_core_constant
   tint_lang_core_intrinsic
   tint_lang_core_ir
+  tint_lang_core_ir_analysis
   tint_lang_core_ir_transform
   tint_lang_core_type
   tint_lang_msl
@@ -114,6 +117,7 @@
   lang/msl/writer/raise/packed_vec3_test.cc
   lang/msl/writer/raise/shader_io_test.cc
   lang/msl/writer/raise/simd_ballot_test.cc
+  lang/msl/writer/raise/validate_subgroup_matrix_test.cc
 )
 
 tint_target_add_dependencies(tint_lang_msl_writer_raise_test test
diff --git a/src/tint/lang/msl/writer/raise/BUILD.gn b/src/tint/lang/msl/writer/raise/BUILD.gn
index 69e6b1f..5b3f475 100644
--- a/src/tint/lang/msl/writer/raise/BUILD.gn
+++ b/src/tint/lang/msl/writer/raise/BUILD.gn
@@ -65,6 +65,8 @@
       "shader_io.h",
       "simd_ballot.cc",
       "simd_ballot.h",
+      "validate_subgroup_matrix.cc",
+      "validate_subgroup_matrix.h",
     ]
     deps = [
       "${dawn_root}/src/utils",
@@ -73,6 +75,7 @@
       "${tint_src_dir}/lang/core/constant",
       "${tint_src_dir}/lang/core/intrinsic",
       "${tint_src_dir}/lang/core/ir",
+      "${tint_src_dir}/lang/core/ir/analysis",
       "${tint_src_dir}/lang/core/ir/transform",
       "${tint_src_dir}/lang/core/type",
       "${tint_src_dir}/lang/msl",
@@ -109,6 +112,7 @@
         "packed_vec3_test.cc",
         "shader_io_test.cc",
         "simd_ballot_test.cc",
+        "validate_subgroup_matrix_test.cc",
       ]
       deps = [
         "${dawn_root}/src/utils",
diff --git a/src/tint/lang/msl/writer/raise/raise.cc b/src/tint/lang/msl/writer/raise/raise.cc
index 15fd9a5..c4daa9f 100644
--- a/src/tint/lang/msl/writer/raise/raise.cc
+++ b/src/tint/lang/msl/writer/raise/raise.cc
@@ -69,6 +69,7 @@
 #include "src/tint/lang/msl/writer/raise/packed_vec3.h"
 #include "src/tint/lang/msl/writer/raise/shader_io.h"
 #include "src/tint/lang/msl/writer/raise/simd_ballot.h"
+#include "src/tint/lang/msl/writer/raise/validate_subgroup_matrix.h"
 
 namespace tint::msl::writer {
 
@@ -86,6 +87,8 @@
     RUN_TRANSFORM(core::ir::transform::SubstituteOverrides, module,
                   options.substitute_overrides_config);
 
+    RUN_TRANSFORM(raise::ValidateSubgroupMatrix, module);
+
     RaiseResult raise_result;
 
     // VertexPulling must come before BindingRemapper and Robustness.
diff --git a/src/tint/lang/msl/writer/raise/validate_subgroup_matrix.cc b/src/tint/lang/msl/writer/raise/validate_subgroup_matrix.cc
new file mode 100644
index 0000000..579514d
--- /dev/null
+++ b/src/tint/lang/msl/writer/raise/validate_subgroup_matrix.cc
@@ -0,0 +1,95 @@
+// Copyright 2025 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+//    contributors may be used to endorse or promote products derived from
+//    this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "src/tint/lang/msl/writer/raise/validate_subgroup_matrix.h"
+
+#include <cstdint>
+#include <utility>
+
+#include "src/tint/lang/core/ir/analysis/subgroup_matrix.h"
+#include "src/tint/lang/core/ir/builder.h"
+#include "src/tint/lang/core/ir/validator.h"
+#include "src/tint/lang/core/type/manager.h"
+
+namespace tint::msl::writer::raise {
+namespace {
+
+using namespace tint::core::fluent_types;  // NOLINT
+
+struct State {
+    /// The IR module.
+    core::ir::Module& ir;
+
+    diag::List diagnostics_{};
+
+    /// Process the module.
+    diag::Result<SuccessType> Process() {
+        auto info = core::ir::analysis::GatherSubgroupMatrixInfo(ir);
+
+        for (auto& i : info.configs) {
+            if (i.columns != 8 || i.rows != 8) {
+                diagnostics_.AddError(Source{})
+                    << "subgroup_matrix requires dimensions of 8x8 for the selected device";
+                break;
+            }
+
+            if (i.type != core::ir::analysis::SubgroupMatrixType::kF32 &&
+                i.type != core::ir::analysis::SubgroupMatrixType::kF16) {
+                diagnostics_.AddError(Source{})
+                    << "subgroup_matrix requires a type of `f32` or `f16` for the selected device";
+                break;
+            }
+        }
+
+        if (!diagnostics_.IsEmpty()) {
+            return diag::Failure{diagnostics_};
+        }
+        return Success;
+    }
+};
+
+}  // namespace
+
+Result<SuccessType> ValidateSubgroupMatrix(core::ir::Module& ir) {
+    auto result = ValidateAndDumpIfNeeded(ir, "msl.ValidateSubgroupMatrix",
+                                          tint::core::ir::Capabilities{
+                                              core::ir::Capability::kAllowResourceBinding,
+                                              core::ir::Capability::kAllow8BitIntegers,
+                                          });
+    if (result != Success) {
+        return result;
+    }
+
+    auto res = State{ir}.Process();
+    if (res != Success) {
+        return Failure{res.Failure().reason.Str()};
+    }
+
+    return Success;
+}
+
+}  // namespace tint::msl::writer::raise
diff --git a/src/tint/lang/msl/writer/raise/validate_subgroup_matrix.h b/src/tint/lang/msl/writer/raise/validate_subgroup_matrix.h
new file mode 100644
index 0000000..3e42870
--- /dev/null
+++ b/src/tint/lang/msl/writer/raise/validate_subgroup_matrix.h
@@ -0,0 +1,50 @@
+// Copyright 2025 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+//    contributors may be used to endorse or promote products derived from
+//    this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef SRC_TINT_LANG_MSL_WRITER_RAISE_VALIDATE_SUBGROUP_MATRIX_H_
+#define SRC_TINT_LANG_MSL_WRITER_RAISE_VALIDATE_SUBGROUP_MATRIX_H_
+
+#include "src/tint/utils/result.h"
+
+// Forward declarations.
+namespace tint::core::ir {
+class Module;
+}  // namespace tint::core::ir
+
+namespace tint::msl::writer::raise {
+
+/// Validates that any subgroup matrix used in the module is valid for the MSL restrictions.
+///  * subtype f16 or f32
+///  * dimensions 8x8
+///
+/// @param module the module to transform
+/// @returns success or failure
+Result<SuccessType> ValidateSubgroupMatrix(core::ir::Module& module);
+
+}  // namespace tint::msl::writer::raise
+
+#endif  // SRC_TINT_LANG_MSL_WRITER_RAISE_VALIDATE_SUBGROUP_MATRIX_H_
diff --git a/src/tint/lang/msl/writer/raise/validate_subgroup_matrix_test.cc b/src/tint/lang/msl/writer/raise/validate_subgroup_matrix_test.cc
new file mode 100644
index 0000000..3295310
--- /dev/null
+++ b/src/tint/lang/msl/writer/raise/validate_subgroup_matrix_test.cc
@@ -0,0 +1,417 @@
+// Copyright 2025 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+//    contributors may be used to endorse or promote products derived from
+//    this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "src/tint/lang/msl/writer/raise/validate_subgroup_matrix.h"
+
+#include "gtest/gtest.h"
+#include "src/tint/lang/core/fluent_types.h"
+#include "src/tint/lang/core/ir/transform/helper_test.h"
+#include "src/tint/lang/core/number.h"
+
+using namespace tint::core::fluent_types;     // NOLINT
+using namespace tint::core::number_suffixes;  // NOLINT
+
+namespace tint::msl::writer::raise {
+namespace {
+
+using MslWriter_ValidateSubgroupMatrixTest = core::ir::transform::TransformTest;
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, F16_8x8) {
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.f16(), 8u, 8u)));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    %v:ptr<function, subgroup_matrix_left<f16, 8, 8>, read_write> = var undef
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = src;
+
+    Run(ValidateSubgroupMatrix);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, F32_8x8) {
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.f32(), 8u, 8u)));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    %v:ptr<function, subgroup_matrix_left<f32, 8, 8>, read_write> = var undef
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = src;
+
+    Run(ValidateSubgroupMatrix);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, 8x2) {
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.f32(), 8u, 2u)));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    %v:ptr<function, subgroup_matrix_left<f32, 8, 2>, read_write> = var undef
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto result = RunWithFailure(ValidateSubgroupMatrix);
+    ASSERT_NE(result, Success);
+    EXPECT_EQ(result.Failure().reason,
+              R"(error: subgroup_matrix requires dimensions of 8x8 for the selected device)");
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, i8) {
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.i8(), 8u, 8u)));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    %v:ptr<function, subgroup_matrix_left<i8, 8, 8>, read_write> = var undef
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto result = RunWithFailure(ValidateSubgroupMatrix);
+    ASSERT_NE(result, Success);
+    EXPECT_EQ(
+        result.Failure().reason,
+        R"(error: subgroup_matrix requires a type of `f32` or `f16` for the selected device)");
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, FunctionParam) {
+    auto* f2 = b.Function("f", ty.void_());
+    f2->AppendParam(b.FunctionParam("p", ty.subgroup_matrix_left(ty.f32(), 8u, 8u)));
+    b.Append(f2->Block(), [&] {  //
+        b.Return(f2);
+    });
+
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        auto* v = b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.f32(), 8u, 8u)));
+        b.Call(ty.void_(), f2, b.Load(v));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%f = func(%p:subgroup_matrix_left<f32, 8, 8>):void {
+  $B1: {
+    ret
+  }
+}
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B2: {
+    %v:ptr<function, subgroup_matrix_left<f32, 8, 8>, read_write> = var undef
+    %5:subgroup_matrix_left<f32, 8, 8> = load %v
+    %6:void = call %f, %5
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = src;
+
+    Run(ValidateSubgroupMatrix);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, InvalidFunctionParam_i8) {
+    auto* f2 = b.Function("f", ty.void_());
+    auto* p = b.FunctionParam("p", ty.subgroup_matrix_left(ty.i8(), 8u, 8u));
+    f2->AppendParam(p);
+    b.Append(f2->Block(), [&] {  //
+        b.Return(f2);
+    });
+
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        auto* v = b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.i8(), 8u, 8u)));
+        b.Call(ty.void_(), f2, b.Load(v));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%f = func(%p:subgroup_matrix_left<i8, 8, 8>):void {
+  $B1: {
+    ret
+  }
+}
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B2: {
+    %v:ptr<function, subgroup_matrix_left<i8, 8, 8>, read_write> = var undef
+    %5:subgroup_matrix_left<i8, 8, 8> = load %v
+    %6:void = call %f, %5
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto result = RunWithFailure(ValidateSubgroupMatrix);
+    ASSERT_NE(result, Success);
+    EXPECT_EQ(
+        result.Failure().reason,
+        R"(error: subgroup_matrix requires a type of `f32` or `f16` for the selected device)");
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, FunctionReturn) {
+    auto* f2 = b.Function("f", ty.subgroup_matrix_left(ty.f32(), 8u, 8u));
+    b.Append(f2->Block(), [&] {  //
+        b.Return(f2, b.Composite(ty.subgroup_matrix_left(ty.f32(), 8u, 8u), 5_f));
+    });
+
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        b.Let("v", b.Call(ty.subgroup_matrix_left(ty.f32(), 8u, 8u), f2));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%f = func():subgroup_matrix_left<f32, 8, 8> {
+  $B1: {
+    ret subgroup_matrix_left<f32, 8, 8>(5.0f)
+  }
+}
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B2: {
+    %3:subgroup_matrix_left<f32, 8, 8> = call %f
+    %v:subgroup_matrix_left<f32, 8, 8> = let %3
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = src;
+
+    Run(ValidateSubgroupMatrix);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, InvalidFunctionReturn_i32) {
+    auto* f2 = b.Function("f", ty.subgroup_matrix_left(ty.i32(), 8u, 8u));
+    b.Append(f2->Block(), [&] {  //
+        b.Return(f2, b.Composite(ty.subgroup_matrix_left(ty.i32(), 8u, 8u), 5_i));
+    });
+
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        auto* c = b.Call(ty.subgroup_matrix_left(ty.i32(), 8u, 8u), f2);
+        b.Let("v", c);
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%f = func():subgroup_matrix_left<i32, 8, 8> {
+  $B1: {
+    ret subgroup_matrix_left<i32, 8, 8>(5i)
+  }
+}
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B2: {
+    %3:subgroup_matrix_left<i32, 8, 8> = call %f
+    %v:subgroup_matrix_left<i32, 8, 8> = let %3
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto result = RunWithFailure(ValidateSubgroupMatrix);
+    ASSERT_NE(result, Success);
+    EXPECT_EQ(
+        result.Failure().reason,
+        R"(error: subgroup_matrix requires a type of `f32` or `f16` for the selected device)");
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, InStruct_F32_8x8) {
+    auto* s = ty.Struct(mod.symbols.New("S"),
+                        {
+                            {mod.symbols.New("a"), ty.subgroup_matrix_left(ty.f32(), 8u, 8u)},
+                        });
+
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        b.Var("v", ty.ptr(function, s));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+S = struct @align(4) {
+  a:subgroup_matrix_left<f32, 8, 8> @offset(0)
+}
+
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    %v:ptr<function, S, read_write> = var undef
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = src;
+
+    Run(ValidateSubgroupMatrix);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, InStruct_8x2) {
+    auto* s = ty.Struct(mod.symbols.New("S"),
+                        {
+                            {mod.symbols.New("a"), ty.subgroup_matrix_left(ty.f32(), 8u, 2u)},
+                        });
+
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        b.Var("v", ty.ptr(function, s));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+S = struct @align(4) {
+  a:subgroup_matrix_left<f32, 8, 2> @offset(0)
+}
+
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    %v:ptr<function, S, read_write> = var undef
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto result = RunWithFailure(ValidateSubgroupMatrix);
+    ASSERT_NE(result, Success);
+    EXPECT_EQ(result.Failure().reason,
+              R"(error: subgroup_matrix requires dimensions of 8x8 for the selected device)");
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, Nested_F32_8x8) {
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        auto* if_ = b.If(true);
+        b.Append(if_->True(), [&] {
+            b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.f32(), 8u, 8u)));
+            b.ExitIf(if_);
+        });
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    if true [t: $B2] {  # if_1
+      $B2: {  # true
+        %v:ptr<function, subgroup_matrix_left<f32, 8, 8>, read_write> = var undef
+        exit_if  # if_1
+      }
+    }
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = src;
+
+    Run(ValidateSubgroupMatrix);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ValidateSubgroupMatrixTest, Nested_F32_8x2) {
+    auto* func = b.ComputeFunction("main");
+    b.Append(func->Block(), [&] {  //
+        auto* if_ = b.If(true);
+        b.Append(if_->True(), [&] {
+            b.Var("v", ty.ptr(function, ty.subgroup_matrix_left(ty.f32(), 8u, 2u)));
+            b.ExitIf(if_);
+        });
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%main = @compute @workgroup_size(1u, 1u, 1u) func():void {
+  $B1: {
+    if true [t: $B2] {  # if_1
+      $B2: {  # true
+        %v:ptr<function, subgroup_matrix_left<f32, 8, 2>, read_write> = var undef
+        exit_if  # if_1
+      }
+    }
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto result = RunWithFailure(ValidateSubgroupMatrix);
+    ASSERT_NE(result, Success);
+    EXPECT_EQ(result.Failure().reason,
+              R"(error: subgroup_matrix requires dimensions of 8x8 for the selected device)");
+}
+
+}  // namespace
+}  // namespace tint::msl::writer::raise