[clang] [lldb] [llvm] [OpenCL][Clang] Add support for cooperative matrix extension (PR #221328)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 22 15:32:07 PDT 2026


================
@@ -0,0 +1,184 @@
+// Negative tests for diagnostic paths.
+//
+// RUN: %clang_cc1 -triple spirv64-unknown-unknown \
+// RUN:   -cl-std=CL2.0 -cl-ext=+cl_khr_cooperative_matrix \
+// RUN:   -finclude-default-header -fsyntax-only -verify %s
+
+#define SCOPE     memory_scope_sub_group
+#define USE_A     CLK_COOPERATIVE_MATRIX_A
+#define USE_B     CLK_COOPERATIVE_MATRIX_B
+#define USE_C     CLK_COOPERATIVE_MATRIX_ACCUMULATOR
+#define ROW_MAJOR CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR
+
+// Use of cooperative matrix type without specifying pragma
+typedef float __attribute__((coop_mat(SCOPE, 16, 16, USE_A))) MatX_t; // expected-error {{cooperative matrix types require OpenCL extension 'cl_khr_cooperative_matrix' to be enabled via pragma}}
+
+#pragma OPENCL EXTENSION cl_khr_cooperative_matrix : enable
+
+typedef float __attribute__((coop_mat(SCOPE, 16, 16, USE_A))) MatA_t; // expected-note {{declared here}}
+typedef float __attribute__((coop_mat(SCOPE, 16, 16, USE_B))) MatB_t;
+typedef int   __attribute__((coop_mat(SCOPE, 16, 16, USE_C))) MatC_int_t;
+
+// Invalid 'scope' test
+typedef float __attribute__((coop_mat(0, 16, 16, USE_A))) MatBadScope; // expected-error {{invalid 'scope' argument of cooperative matrix attribute}}
+
+// Invalid 'use' test
+typedef float __attribute__((coop_mat(SCOPE, 16, 16, 99))) MatBadUse;  // expected-error {{invalid 'use' argument of cooperative matrix attribute}}
+
+// Mismatched element types in coop_mat_mulAdd
+kernel void test_muladd_type_mismatch(__global float *fptr,
+                                      __global int   *iptr) {
+    MatA_t     a;
+    MatB_t     b;
+    MatC_int_t c;
+    a = coop_mat_load(fptr, ROW_MAJOR, 16);
+    b = coop_mat_load(fptr, ROW_MAJOR, 16);
+    c = coop_mat_load(iptr, ROW_MAJOR, 16);
+
+    MatC_int_t result;
+    result = coop_mat_mulAdd(a, b, c); // expected-error {{inconsistent cooperative matrix element types}}
+    (void)result;
+
+    MatC_int_t result_2 = coop_mat_mulAdd(a, b, c); // expected-error {{inconsistent cooperative matrix element types}}
+    (void)result_2;
+}
+
+// Assignment of coop_mat_load result to a plain scalar
+kernel void test_bad_assignment(__global float *ptr) {
+    float bad;
+	bad = coop_mat_load(ptr, ROW_MAJOR, 16); // expected-error {{builtin return value should be assigned to cooperative matrix type variable}}
+    (void)bad;
+}
+
+// Kernel parameter cannot be of cooperative matrix type.
+kernel void test_bad_kernel_param(MatA_t a) {} // expected-error {{cannot be used as the type of a kernel parameter}}
+
+// Invalid memory layout for cooperative matrix load.
+kernel void test_invalid_load_layout(__global float *ptr) {
+    MatA_t a;
+    a = coop_mat_load(ptr, 99, 16); // expected-error {{memory layout for cooperative matrix load/store should be CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR or CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR}}
+}
+
+// Invalid memory layout for cooperative matrix store.
+kernel void test_invalid_store_layout(__global float *ptr) {
+    MatA_t a;
+    coop_mat_store(ptr, a, 99, 16); // expected-error {{memory layout for cooperative matrix load/store should be CLK_COOPERATIVE_MATRIX_LAYOUT_ROW_MAJOR or CLK_COOPERATIVE_MATRIX_LAYOUT_COLUMN_MAJOR}}
+}
+
+// Invalid stride type for cooperative matrix load.
+kernel void test_invalid_load_stride(__global float *ptr) {
+    MatA_t a;
+    int stride = 16;
+    a = coop_mat_load(ptr, ROW_MAJOR, stride); // expected-error {{stride argument for load/store of cooperative matrix must have type 'size_t'}}
+}
+
+// Invalid stride type for cooperative matrix store.
+kernel void test_invalid_store_stride(__global float *ptr) {
+    MatA_t a;
+    int stride = 16;
+    coop_mat_store(ptr, a, ROW_MAJOR, stride); // expected-error {{stride argument for load/store of cooperative matrix must have type 'size_t'}}
+}
+
+// Invalid value for fourth coop_mat_mulAdd argument.
+kernel void test_invalid_muladd_matrix_operand_type() {
+    MatA_t a;
+    MatB_t b;
+    MatC_int_t c;
+
+    (void)coop_mat_mulAdd(a, b, c, 16.2f); // expected-error {{fourth argument of 'coop_mat_mulAdd' must have enum or integer type}}
+}
+
+// Tests for first three arguments of coop_mat_mulAdd.
+kernel void test_invalid_muladd_argument(__global float *ptr) {
+    MatA_t a;
+    MatB_t b;
+    MatC_int_t c;
+
+    (void)coop_mat_mulAdd(a, b, 1.0f); // expected-error {{argument must be a valid cooperative matrix}}
+}
+
+// Tests for first argument of coop_mat_mulAdd.
+kernel void test_invalid_muladd_a_use(__global float *ptr) {
+    MatB_t b;
+    MatB_t b2;
+    MatC_int_t c;
+
+    (void)coop_mat_mulAdd(b, b2, c); // expected-error {{argument of cooperative matrix must have 'CLK_COOPERATIVE_MATRIX_A' use}}
+}
+
+
+// Tests for second argument of coop_mat_mulAdd.
+kernel void test_invalid_muladd_b_use(__global float *ptr) {
+    MatA_t a;
+    MatA_t a2;
+    MatC_int_t c;
+
+    (void)coop_mat_mulAdd(a, a2, c); // expected-error {{argument of cooperative matrix must have 'CLK_COOPERATIVE_MATRIX_B' use}}
+}
+
+// Tests for third argument of coop_mat_mulAdd.
+kernel void test_invalid_muladd_accumulator_use(__global float *ptr) {
+    MatA_t a;
+    MatB_t b;
+    MatA_t c;
+
+    (void)coop_mat_mulAdd(a, b, c); // expected-error {{argument of cooperative matrix must have 'CLK_COOPERATIVE_MATRIX_ACCUMULATOR' use}}
+}
+
+// Invalid cooperative matrix element type.
+typedef bool __attribute__((coop_mat(SCOPE, 16, 16, USE_A))) MatBadElement_t; // expected-error {{'bool' is invalid cooperative matrix element type}}
----------------
asudarsa-qti wrote:

I added the tests for pointers, arrays, structs and vectors

Thanks

https://github.com/llvm/llvm-project/pull/221328


More information about the cfe-commits mailing list