https://github.com/AmrDeveloper updated 
https://github.com/llvm/llvm-project/pull/221773

>From ad0f8c6727733021bf91a0232a193d71695ceca0 Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Sun, 6 Sep 2026 19:56:44 +0200
Subject: [PATCH 1/6] [CIR] Support builtin matrix type

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h      |  2 +
 .../CIR/Dialect/IR/CIRTypeConstraints.td      | 10 ++++
 .../include/clang/CIR/Dialect/IR/CIRTypes.td  | 53 +++++++++++++++++++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp         | 13 +++--
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |  4 +-
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         | 30 +++++++++++
 clang/lib/CIR/Lowering/LoweringHelpers.cpp    | 11 ++++
 clang/test/CIR/CodeGen/matrix.cpp             | 20 +++++++
 .../CodeGenHLSL/matrix-element-expr-load.hlsl |  4 +-
 clang/test/CIR/IR/invalid-matrix.cir          | 32 +++++++++++
 10 files changed, 169 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/matrix.cpp
 create mode 100644 clang/test/CIR/IR/invalid-matrix.cir

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 3a52960000a14f..29f1a64ad1d17f 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -131,6 +131,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
       return cir::ZeroAttr::get(arrTy);
     if (auto vecTy = mlir::dyn_cast<cir::VectorType>(ty))
       return cir::ZeroAttr::get(vecTy);
+    if (auto matrixTy = mlir::dyn_cast<cir::MatrixType>(ty))
+      return cir::ZeroAttr::get(matrixTy);
     if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty))
       return getConstNullPtrAttr(ptrTy);
     if (auto recordTy = mlir::dyn_cast<cir::RecordType>(ty))
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td 
b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
index 69f0d696f228bb..1fffe0adb3bf17 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
@@ -372,6 +372,16 @@ def CIR_AnyBitwiseType
     : AnyTypeOf<[CIR_AnyIntType, CIR_AnyBoolType, CIR_VectorOfIntOrBoolType],
                 "integer, boolean, or vector of bool or integer">;
 
+//===----------------------------------------------------------------------===//
+// Matrix Type predicates
+//===----------------------------------------------------------------------===//
+
+def CIR_MatrixElementType
+    : AnyTypeOf<[CIR_AnyBoolType, CIR_AnyIntOrFloatType, CIR_AnyPtrType],
+                "any cir boolean, integer, floating point or pointer type"> {
+  let cppFunctionName = "isValidMatrixTypeElementType";
+}
+
 
//===----------------------------------------------------------------------===//
 // Data member type predicates
 
//===----------------------------------------------------------------------===//
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td 
b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index f9cee11c81a9e3..8335de7c8fa499 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -543,6 +543,59 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", [
   let hasCustomAssemblyFormat = 1;
 }
 
+//===----------------------------------------------------------------------===//
+// MatrixType
+//===----------------------------------------------------------------------===//
+
+def CIR_MatrixType : CIR_Type<"Matrix", "matrix", [
+    DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
+]> {
+  let summary = "CIR matrix type";
+  let description = [{
+    The `!cir.matrix` type represents a fixed-size 2-dimensional matrices.
+    It takes three parameters: the element type, the number of rows
+    and columns.
+
+    Syntax:
+
+    ```
+    matrix-type ::= !cir.vector<row x colum x element-type>
+    size ::= (decimal-literal | `[` decimal-literal `]`)
+    element-type ::= float-type | integer-type | pointer-type
+    ```
+
+    The `element-type` must be a scalar CIR type. Zero-sized matrices are not
+    allowed. The `row` and `column` count must be a positive integer.
+
+    Examples:
+
+    ```
+    !cir.matrix<3 x 3 x !cir.int<u, 8>>
+    !cir.matrix<2 x 4 x !cir.float>
+    ```
+  }];
+
+  let parameters = (ins
+    CIR_MatrixElementType:$element_type,
+    "uint64_t":$row_num,
+    "uint64_t":$column_num
+  );
+
+  let builders = [
+    TypeBuilderWithInferredContext<(ins
+      "mlir::Type":$element_type, "uint64_t":$row_num, "uint64_t":$column_num
+    ), [{
+        return $_get(element_type.getContext(), element_type, row_num, 
column_num);
+    }]>,
+  ];
+
+  let assemblyFormat = [{
+    `<` $row_num `x` $column_num `x` $element_type `>`
+  }];
+
+  let genVerifyDecl = 1;
+}
+
 
//===----------------------------------------------------------------------===//
 // FuncType
 
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp 
b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index 0243d2859071b9..3f0f29e14fb8f3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -608,6 +608,14 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
     break;
   }
 
+  case Type::ConstantMatrix: {
+    const ConstantMatrixType *mt = cast<ConstantMatrixType>(ty);
+    const mlir::Type elemTy = convertType(mt->getElementType());
+    resultType =
+        cir::MatrixType::get(elemTy, mt->getNumRows(), mt->getNumColumns());
+    break;
+  }
+
   case Type::Enum: {
     const auto *ed = ty->castAsEnumDecl();
     if (auto integerType = ed->getIntegerType(); !integerType.isNull())
@@ -691,11 +699,6 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
 
 mlir::Type CIRGenTypes::convertTypeForMem(clang::QualType qualType,
                                           bool forBitField) {
-  if (qualType->isConstantMatrixType()) {
-    cgm.errorNYI("Matrix type conversion");
-    return cgm.sInt32Ty;
-  }
-
   mlir::Type convertedType = convertType(qualType);
 
   assert(!forBitField && "Bit fields NYI");
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 6bbf338fcbda12..b994d19ea1477b 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -759,8 +759,8 @@ static LogicalResult checkConstantTypes(mlir::Operation 
*op, mlir::Type opType,
   }
 
   if (isa<cir::ZeroAttr>(attrType)) {
-    if (isa<cir::RecordType, cir::ArrayType, cir::VectorType, 
cir::ComplexType>(
-            opType))
+    if (isa<cir::RecordType, cir::ArrayType, cir::MatrixType, cir::VectorType,
+            cir::ComplexType>(opType))
       return success();
     return op->emitOpError(
         "zero expects struct, array, vector, or complex type");
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp 
b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index 0dafc4f723779a..48696aea884946 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -1583,6 +1583,36 @@ void cir::VectorType::print(mlir::AsmPrinter 
&odsPrinter) const {
   odsPrinter << ">";
 }
 
+//===----------------------------------------------------------------------===//
+// MatrixType Definitions
+//===----------------------------------------------------------------------===//
+
+llvm::TypeSize cir::MatrixType::getTypeSizeInBits(
+    const ::mlir::DataLayout &dataLayout,
+    ::mlir::DataLayoutEntryListRef params) const {
+  return llvm::TypeSize::getFixed(
+      getRowNum() * getColumnNum() *
+      dataLayout.getTypeSizeInBits(getElementType()));
+}
+
+uint64_t
+cir::MatrixType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
+                                 ::mlir::DataLayoutEntryListRef params) const {
+  // This hook answers in bytes, not bits.
+  return llvm::PowerOf2Ceil(
+      llvm::divideCeil(dataLayout.getTypeSizeInBits(*this), 8u));
+}
+
+mlir::LogicalResult cir::MatrixType::verify(
+    llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
+    mlir::Type elementType, uint64_t row, uint64_t column) {
+  if (row == 0)
+    return emitError() << "the number of matrix rows must be non-zero";
+  if (column == 0)
+    return emitError() << "the number of matrix columns must be non-zero";
+  return success();
+}
+
 
//===----------------------------------------------------------------------===//
 // AddressSpace definitions
 
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp 
b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
index 0b64a37cb6bf49..5e57ca4701f483 100644
--- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp
+++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
@@ -46,6 +46,17 @@ mlir::Type convertTypeForMemory(const mlir::TypeConverter 
&converter,
                                   dataLayout.getTypeSizeInBits(type));
   }
 
+  if (auto matrixTy = mlir::dyn_cast<cir::MatrixType>(type)) {
+    if (mlir::isa<cir::BoolType>(matrixTy.getElementType())) {
+      llvm_unreachable(
+          "convertTypeForMemory: Matrix with bool as element type");
+    }
+
+    uint64_t size = matrixTy.getRowNum() * matrixTy.getColumnNum();
+    mlir::Type elementType = converter.convertType(matrixTy.getElementType());
+    return mlir::LLVM::LLVMArrayType::get(elementType, size);
+  }
+
   if (auto vecTy = mlir::dyn_cast<cir::VectorType>(type)) {
     if (mlir::isa<cir::BoolType>(vecTy.getElementType())) {
       assert(!cir::MissingFeatures::hlsl());
diff --git a/clang/test/CIR/CodeGen/matrix.cpp 
b/clang/test/CIR/CodeGen/matrix.cpp
new file mode 100644
index 00000000000000..89c4c55149c6b4
--- /dev/null
+++ b/clang/test/CIR/CodeGen/matrix.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fenable-matrix -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fenable-matrix -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value 
-fenable-matrix -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
+
+typedef float matrix3x3 __attribute__((matrix_type(3, 3)));
+
+matrix3x3 a;
+
+// CIR: cir.global external @a = #cir.zero : !cir.matrix<3 x 3 x !cir.float>
+// LLVM: @a = global [9 x float] zeroinitializer, align 4
+
+void local_matrix() {
+  matrix3x3 a;
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.matrix<3 x 3 x 
!cir.float>>
+// LLVM: %[[A_ADDR:.*]] = alloca [9 x float], align 4
diff --git a/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl 
b/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl
index abec530f474f5c..8f9d5fd45a9222 100644
--- a/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl
+++ b/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl
@@ -1,8 +1,6 @@
-// RUN: %clang_cc1 -x hlsl -finclude-default-header -triple 
spirv-unknown-vulkan-library %s \
+// RUN: not %clang_cc1 -x hlsl -finclude-default-header -triple 
spirv-unknown-vulkan-library %s \
 // RUN:   -fclangir -emit-cir -disable-llvm-passes -verify
 
-// expected-error@*:* {{ClangIR code gen Not Yet Implemented: processing of 
type: ConstantMatrix}}
-// expected-error@*:* {{ClangIR code gen Not Yet Implemented: Matrix type 
conversion}}
 float test_zero_indexed(float2x2 M) {
   // expected-error@+1 {{ClangIR code gen Not Yet Implemented: 
ScalarExprEmitter: matrix element}}
   return M._m00;
diff --git a/clang/test/CIR/IR/invalid-matrix.cir 
b/clang/test/CIR/IR/invalid-matrix.cir
new file mode 100644
index 00000000000000..f8bb3c5d3ba73d
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-matrix.cir
@@ -0,0 +1,32 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+!s32i = !cir.int<s, 32>
+
+module  {
+
+// expected-error @below {{failed to verify 'element_type'}}
+cir.global external @vec_b = #cir.zero : !cir.matrix<4 x 4 x !cir.array<!s32i 
x 10>>
+
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+cir.func @invalid_row_number() {
+  // expected-error@+1 {{the number of matrix rows must be non-zero}}
+  %3 = cir.alloca !cir.matrix<0 x 4 x !s32i>, !cir.ptr<!cir.matrix<0 x 4 x 
!s32i>>
+  cir.return
+
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+cir.func @invalid_column_number() {
+  // expected-error@+1 {{the number of matrix columns must be non-zero}}
+  %3 = cir.alloca !cir.matrix<4 x 0 x !s32i>, !cir.ptr<!cir.matrix<4 x 0 x 
!s32i>>
+  cir.return
+
+}

>From e2c485b023a91c1f6ea34886d6efcfb05dd0a3ab Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Tue, 8 Sep 2026 20:37:00 +0200
Subject: [PATCH 2/6] Address part of code review comments

---
 .../CIR/Dialect/IR/CIRTypeConstraints.td      |  4 ++--
 .../include/clang/CIR/Dialect/IR/CIRTypes.td  |  6 +++---
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         |  4 +---
 clang/lib/CIR/Lowering/LoweringHelpers.cpp    |  1 +
 clang/test/CIR/IR/matrix.cir                  | 20 +++++++++++++++++++
 5 files changed, 27 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/CIR/IR/matrix.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td 
b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
index 1fffe0adb3bf17..c6afa74ba051fe 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
@@ -377,8 +377,8 @@ def CIR_AnyBitwiseType
 
//===----------------------------------------------------------------------===//
 
 def CIR_MatrixElementType
-    : AnyTypeOf<[CIR_AnyBoolType, CIR_AnyIntOrFloatType, CIR_AnyPtrType],
-                "any cir boolean, integer, floating point or pointer type"> {
+    : AnyTypeOf<[CIR_AnyBoolType, CIR_AnyIntOrFloatType],
+                "any cir boolean, integer, floating point"> {
   let cppFunctionName = "isValidMatrixTypeElementType";
 }
 
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td 
b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index 8335de7c8fa499..8f8d672f74bc52 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -552,14 +552,14 @@ def CIR_MatrixType : CIR_Type<"Matrix", "matrix", [
 ]> {
   let summary = "CIR matrix type";
   let description = [{
-    The `!cir.matrix` type represents a fixed-size 2-dimensional matrices.
-    It takes three parameters: the element type, the number of rows
+    The `!cir.matrix` type represents a fixed-size 2-dimensional matrix.
+    It takes three parameters: the element type, the number of rows,
     and columns.
 
     Syntax:
 
     ```
-    matrix-type ::= !cir.vector<row x colum x element-type>
+    matrix-type ::= !cir.matrix<row x colum x element-type>
     size ::= (decimal-literal | `[` decimal-literal `]`)
     element-type ::= float-type | integer-type | pointer-type
     ```
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp 
b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index 48696aea884946..b9263609e4fe48 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -1598,9 +1598,7 @@ llvm::TypeSize cir::MatrixType::getTypeSizeInBits(
 uint64_t
 cir::MatrixType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
                                  ::mlir::DataLayoutEntryListRef params) const {
-  // This hook answers in bytes, not bits.
-  return llvm::PowerOf2Ceil(
-      llvm::divideCeil(dataLayout.getTypeSizeInBits(*this), 8u));
+  return dataLayout.getTypeABIAlignment(getElementType());
 }
 
 mlir::LogicalResult cir::MatrixType::verify(
diff --git a/clang/lib/CIR/Lowering/LoweringHelpers.cpp 
b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
index 5e57ca4701f483..487be3ee2f5601 100644
--- a/clang/lib/CIR/Lowering/LoweringHelpers.cpp
+++ b/clang/lib/CIR/Lowering/LoweringHelpers.cpp
@@ -48,6 +48,7 @@ mlir::Type convertTypeForMemory(const mlir::TypeConverter 
&converter,
 
   if (auto matrixTy = mlir::dyn_cast<cir::MatrixType>(type)) {
     if (mlir::isa<cir::BoolType>(matrixTy.getElementType())) {
+      assert(!cir::MissingFeatures::hlsl());
       llvm_unreachable(
           "convertTypeForMemory: Matrix with bool as element type");
     }
diff --git a/clang/test/CIR/IR/matrix.cir b/clang/test/CIR/IR/matrix.cir
new file mode 100644
index 00000000000000..e0262694c533a4
--- /dev/null
+++ b/clang/test/CIR/IR/matrix.cir
@@ -0,0 +1,20 @@
+// RUN: cir-opt %s --verify-roundtrip | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+
+module  {
+
+cir.global external @vec_b = #cir.zero : !cir.matrix<4 x 4 x !s32i>
+// CHECK: cir.global external @vec_b = #cir.zero : !cir.matrix<4 x 4 x !s32i>
+
+cir.func @valid_matrix_type() {
+  %matrix_addr = cir.alloca "a" align(4) : !cir.ptr<!cir.matrix<3 x 3 x 
!cir.float>>
+  cir.return
+}
+
+// CHECK: cir.func @valid_matrix_type() {
+// CHECK:   %[[MATRIX_ADDR:.*]] = cir.alloca "a" {{.*}} : 
!cir.ptr<!cir.matrix<3 x 3 x !cir.float>>
+// CHECK:   cir.return
+// CHECK: }
+
+}

>From 093a8bd66935931f8e4b7d8b345c453271d61efd Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Tue, 8 Sep 2026 21:59:29 +0200
Subject: [PATCH 3/6] Add test for negative size

---
 clang/test/CIR/IR/invalid-matrix.cir | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/clang/test/CIR/IR/invalid-matrix.cir 
b/clang/test/CIR/IR/invalid-matrix.cir
index f8bb3c5d3ba73d..7c234dcdf9a965 100644
--- a/clang/test/CIR/IR/invalid-matrix.cir
+++ b/clang/test/CIR/IR/invalid-matrix.cir
@@ -30,3 +30,14 @@ cir.func @invalid_column_number() {
   cir.return
 
 }
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+cir.func @negative_column_number() {
+  // expected-error@+1 {{custom op 'cir.alloca' invalid kind of attribute 
specified}}
+  %3 = cir.alloca !cir.matrix<4 x -1 x !s32i>, !cir.ptr<!cir.matrix<4 x -1 x 
!s32i>>
+  cir.return
+
+}

>From 8f0101a6fd1fcb3037531fc843cdf651061d1c02 Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Fri, 18 Sep 2026 18:32:34 +0200
Subject: [PATCH 4/6] Update matrix type docs

---
 clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td 
b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index 8f8d672f74bc52..e8cad46320b3ed 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -561,7 +561,7 @@ def CIR_MatrixType : CIR_Type<"Matrix", "matrix", [
     ```
     matrix-type ::= !cir.matrix<row x colum x element-type>
     size ::= (decimal-literal | `[` decimal-literal `]`)
-    element-type ::= float-type | integer-type | pointer-type
+    element-type ::= float-type | integer-type | bool-type
     ```
 
     The `element-type` must be a scalar CIR type. Zero-sized matrices are not

>From 9b9ba47dd1a35541b0251135651fa17190546716 Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Fri, 18 Sep 2026 18:54:50 +0200
Subject: [PATCH 5/6] Add missing NYI for MatrixType

---
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp       | 15 ++++++++++----
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 24 ++++++++++++++++------
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp      |  7 +++++--
 3 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 664425b20577f9..bd3f1405f02e48 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -726,7 +726,7 @@ mlir::Value CIRGenFunction::emitFromMemory(mlir::Value 
value, QualType ty) {
 void CIRGenFunction::emitStoreOfScalar(mlir::Value value, LValue lvalue,
                                        bool isInit) {
   if (lvalue.getType()->isConstantMatrixType()) {
-    assert(0 && "NYI: emitStoreOfScalar constant matrix type");
+    cgm.errorNYI("emitStoreOfScalar constant matrix type");
     return;
   }
 
@@ -784,13 +784,18 @@ mlir::Value CIRGenFunction::emitLoadOfScalar(LValue 
lvalue,
 /// returning the rvalue.
 RValue CIRGenFunction::emitLoadOfLValue(LValue lv, SourceLocation loc) {
   assert(!lv.getType()->isFunctionType());
-  assert(!(lv.getType()->isConstantMatrixType()) && "not implemented");
 
   if (lv.isBitField())
     return emitLoadOfBitfieldLValue(lv, loc);
 
-  if (lv.isSimple())
+  if (lv.isSimple()) {
+    if (lv.getType()->isConstantMatrixType()) {
+      cgm.errorNYI(loc, "emitLoadOfLValue: constant matrix type");
+      return RValue::get(nullptr);
+    }
+
     return RValue::get(emitLoadOfScalar(lv, loc));
+  }
 
   if (lv.isVectorElt()) {
     const mlir::Value load =
@@ -2818,8 +2823,10 @@ Address CIRGenFunction::createMemTemp(QualType ty, 
CharUnits align,
                        name, /*arraySize=*/nullptr, alloca, ip);
   if (ty->isConstantMatrixType()) {
     assert(!cir::MissingFeatures::matrixType());
-    cgm.errorNYI(loc, "temporary matrix value");
+    cgm.errorNYI(loc, "createMemTemp constant matrix type");
+    return Address::invalid();
   }
+
   return result;
 }
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 4b59698326f084..a5c4225db69ec9 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -325,6 +325,12 @@ class ScalarExprEmitter : public 
StmtVisitor<ScalarExprEmitter, mlir::Value> {
     return {};
   }
 
+  mlir::Value VisitMatrixSingleSubscriptExpr(MatrixSingleSubscriptExpr *e) {
+    cgf.cgm.errorNYI(e->getSourceRange(),
+                     "ScalarExprEmitter: matrix singel subscript");
+    return {};
+  }
+
   mlir::Value VisitCastExpr(CastExpr *e);
   mlir::Value VisitCallExpr(const CallExpr *e);
 
@@ -2219,8 +2225,8 @@ mlir::Value ScalarExprEmitter::emitMul(const BinOpInfo 
&ops) {
   }
   if (ops.fullType->isConstantMatrixType()) {
     assert(!cir::MissingFeatures::matrixType());
-    cgf.cgm.errorNYI("matrix types");
-    return nullptr;
+    cgf.cgm.errorNYI("ScalarExprEmitter::emitMul: matrix types");
+    return {};
   }
   if (ops.compType->isUnsignedIntegerType() &&
       cgf.sanOpts.has(SanitizerKind::UnsignedIntegerOverflow) &&
@@ -2245,6 +2251,12 @@ mlir::Value ScalarExprEmitter::emitDiv(const BinOpInfo 
&ops) {
     return builder.createFDiv(loc, ops.lhs, ops.rhs);
   }
 
+  if (ops.fullType->isConstantMatrixType()) {
+    assert(!cir::MissingFeatures::matrixType());
+    cgf.cgm.errorNYI("ScalarExprEmitter::emitDiv: matrix types");
+    return {};
+  }
+
   if (ops.isFixedPointOp())
     return emitFixedPointBinOp(ops);
 
@@ -2382,8 +2394,8 @@ mlir::Value ScalarExprEmitter::emitAdd(const BinOpInfo 
&ops) {
   }
   if (ops.fullType->isConstantMatrixType()) {
     assert(!cir::MissingFeatures::matrixType());
-    cgf.cgm.errorNYI("matrix types");
-    return nullptr;
+    cgf.cgm.errorNYI("ScalarExprEmitter::emitAdd: matrix types");
+    return {};
   }
 
   if (ops.compType->isUnsignedIntegerType() &&
@@ -2430,8 +2442,8 @@ mlir::Value ScalarExprEmitter::emitSub(const BinOpInfo 
&ops) {
 
     if (ops.fullType->isConstantMatrixType()) {
       assert(!cir::MissingFeatures::matrixType());
-      cgf.cgm.errorNYI("matrix types");
-      return nullptr;
+      cgf.cgm.errorNYI("ScalarExprEmitter::emitSub: matrix types");
+      return {};
     }
 
     if (ops.compType->isUnsignedIntegerType() &&
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp 
b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index 3f0f29e14fb8f3..b24711ecb161f5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -553,8 +553,6 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
   case Type::Pointer: {
     const PointerType *ptrTy = cast<PointerType>(ty);
     QualType elemTy = ptrTy->getPointeeType();
-    assert(!elemTy->isConstantMatrixType() && "not implemented");
-
     mlir::Type pointeeType = convertType(elemTy);
 
     resultType =
@@ -699,6 +697,11 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
 
 mlir::Type CIRGenTypes::convertTypeForMem(clang::QualType qualType,
                                           bool forBitField) {
+  if (astContext.getLangOpts().HLSL && qualType->isConstantMatrixType()) {
+    cgm.errorNYI("convertTypeForMem: HLSL & ConstantMatrixType");
+    return {};
+  }
+
   mlir::Type convertedType = convertType(qualType);
 
   assert(!forBitField && "Bit fields NYI");

>From f5d8eaf59e69b4cc0a92914b0ee21fce961a75ff Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Sun, 20 Sep 2026 21:34:51 +0200
Subject: [PATCH 6/6] Fix reporting NYI diagnostic in tests

---
 clang/include/clang/CIR/Dialect/IR/CIRTypes.td           | 5 +++--
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp                    | 1 -
 clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td 
b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index e8cad46320b3ed..fd092c3ebb7056 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -549,6 +549,7 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", [
 
 def CIR_MatrixType : CIR_Type<"Matrix", "matrix", [
     DeclareTypeInterfaceMethods<DataLayoutTypeInterface>,
+    DeclareTypeInterfaceMethods<CIR_SizedTypeInterface>,
 ]> {
   let summary = "CIR matrix type";
   let description = [{
@@ -1228,8 +1229,8 @@ def CIR_CatchTokenType : CIR_Type<"CatchToken", 
"catch_token"> {
 
//===----------------------------------------------------------------------===//
 
 def CIR_AnyType : AnyTypeOf<[
-  CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, CIR_IntType,
-  CIR_AnyFloatType, CIR_PointerType, CIR_FuncType, CIR_StructType,
+  CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, CIR_MatrixType,
+  CIR_IntType, CIR_AnyFloatType, CIR_PointerType, CIR_FuncType, CIR_StructType,
   CIR_UnionType, CIR_BitFieldType,
   CIR_ComplexType, CIR_VPtrType, CIR_CUDADeviceSurfaceType,
   CIR_CUDADeviceTextureType,
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp 
b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index b24711ecb161f5..dff1ce12607871 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -699,7 +699,6 @@ mlir::Type CIRGenTypes::convertTypeForMem(clang::QualType 
qualType,
                                           bool forBitField) {
   if (astContext.getLangOpts().HLSL && qualType->isConstantMatrixType()) {
     cgm.errorNYI("convertTypeForMem: HLSL & ConstantMatrixType");
-    return {};
   }
 
   mlir::Type convertedType = convertType(qualType);
diff --git a/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl 
b/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl
index 8f9d5fd45a9222..76a9d0da14dbfb 100644
--- a/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl
+++ b/clang/test/CIR/CodeGenHLSL/matrix-element-expr-load.hlsl
@@ -1,7 +1,8 @@
-// RUN: not %clang_cc1 -x hlsl -finclude-default-header -triple 
spirv-unknown-vulkan-library %s \
+// RUN: %clang_cc1 -x hlsl -finclude-default-header -triple 
spirv-unknown-vulkan-library %s \
 // RUN:   -fclangir -emit-cir -disable-llvm-passes -verify
 
 float test_zero_indexed(float2x2 M) {
+  // expected-error@*:* {{ClangIR code gen Not Yet Implemented: 
convertTypeForMem: HLSL & ConstantMatrixType}}
   // expected-error@+1 {{ClangIR code gen Not Yet Implemented: 
ScalarExprEmitter: matrix element}}
   return M._m00;
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to