[clang] [CIR] Support builtin matrix type (PR #221773)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 19 01:36:29 PDT 2026
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/221773
>From 3ac09d854026b79bc6cd0d989c80cfed654f0cbf Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Sun, 6 Sep 2026 19:56:44 +0200
Subject: [PATCH 1/5] [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 3a52960000a14..29f1a64ad1d17 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 802de1e2dd383..4b5f9148c632e 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
@@ -366,6 +366,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 d0bf90e02b5f3..1f6d3546e97ce 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 0243d2859071b..3f0f29e14fb8f 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 38ef8409634c7..51ff38bc25ead 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -760,8 +760,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 0dafc4f723779..48696aea88494 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 0b64a37cb6bf4..5e57ca4701f48 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 0000000000000..89c4c55149c6b
--- /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 abec530f474f5..8f9d5fd45a922 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 at +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 0000000000000..f8bb3c5d3ba73
--- /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 at +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 at +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 c07f43e9c08a778d45cc497909594a0af8f8f93f Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Tue, 8 Sep 2026 20:37:00 +0200
Subject: [PATCH 2/5] 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 4b5f9148c632e..5e3ded6faa6ea 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
@@ -371,8 +371,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 1f6d3546e97ce..47f278e58e3fa 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 48696aea88494..b9263609e4fe4 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 5e57ca4701f48..487be3ee2f560 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 0000000000000..e0262694c533a
--- /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 51bb5f972708954a61211339d1397a42d1cbccd3 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Tue, 8 Sep 2026 21:59:29 +0200
Subject: [PATCH 3/5] 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 f8bb3c5d3ba73..7c234dcdf9a96 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 at +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 ce1d75218f898c5a4b10ba6aec9826bb5610b45d Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Fri, 18 Sep 2026 18:32:34 +0200
Subject: [PATCH 4/5] Update matrix type docs
---
clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index 47f278e58e3fa..85c792b8cd8c1 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -495,7 +495,7 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", [
```
vector-type ::= !cir.vector<size x element-type>
- size ::= (decimal-literal | `[` decimal-literal `]`)
+ size ::= (decimal-literal)
element-type ::= float-type | integer-type | pointer-type
```
@@ -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 f474bc96fa5549b40ce81aa61e1d9fb5eebcaebd Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Fri, 18 Sep 2026 18:54:50 +0200
Subject: [PATCH 5/5] 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 7e9a4d9458c93..03fb909e9fc32 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -725,7 +725,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;
}
@@ -783,13 +783,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 =
@@ -2836,8 +2841,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 eaca334e88d07..e5d277e3ef272 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -324,6 +324,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);
@@ -2218,8 +2224,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) &&
@@ -2244,6 +2250,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);
@@ -2381,8 +2393,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() &&
@@ -2429,8 +2441,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 3f0f29e14fb8f..b24711ecb161f 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");
More information about the cfe-commits
mailing list