[clang] [CIR] Implement matrix to matrix int & fp casts (PR #238862)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 08:53:32 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Amr Hesham (AmrDeveloper)
<details>
<summary>Changes</summary>
Implement support for fp_to_int and int_to_fp casts between two matrices
---
Full diff: https://github.com/llvm/llvm-project/pull/238862.diff
4 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+9-7)
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+17)
- (added) clang/test/CIR/CodeGenHLSL/matrix-element-type-cast.hlsl (+42)
- (modified) clang/test/CIR/IR/invalid-cast.cir (+12)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index ed15c81b87845..40a97d5f23380 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -518,15 +518,18 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
mlir::Value emitScalarCast(mlir::Value src, QualType srcType,
QualType dstType, mlir::Type srcTy,
mlir::Type dstTy, ScalarConversionOpts opts) {
- assert(!srcType->isMatrixType() && !dstType->isMatrixType() &&
- "Internal error: matrix types not handled by this function.");
assert(!(mlir::isa<mlir::IntegerType>(srcTy) ||
mlir::isa<mlir::IntegerType>(dstTy)) &&
"Obsolete code. Don't use mlir::IntegerType with CIR.");
mlir::Type fullDstTy = dstTy;
- if (mlir::isa<cir::VectorType>(srcTy) &&
- mlir::isa<cir::VectorType>(dstTy)) {
+ if (mlir::isa<cir::MatrixType>(srcTy) &&
+ mlir::isa<cir::MatrixType>(dstTy)) {
+ // Use the element types of the matrices to figure out the CastKind.
+ srcTy = mlir::dyn_cast<cir::MatrixType>(srcTy).getElementType();
+ dstTy = mlir::dyn_cast<cir::MatrixType>(dstTy).getElementType();
+ } else if (mlir::isa<cir::VectorType>(srcTy) &&
+ mlir::isa<cir::VectorType>(dstTy)) {
// Use the element types of the vectors to figure out the CastKind.
srcTy = mlir::dyn_cast<cir::VectorType>(srcTy).getElementType();
dstTy = mlir::dyn_cast<cir::VectorType>(dstTy).getElementType();
@@ -1146,9 +1149,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
}
if (srcType->isMatrixType() && dstType->isMatrixType()) {
- cgf.getCIRGenModule().errorNYI(loc,
- "matrix type to matrix type conversion");
- return {};
+ return emitScalarCast(src, srcType, dstType, mlirSrcType, mlirDstType,
+ opts);
}
assert(!srcType->isMatrixType() && !dstType->isMatrixType() &&
"Internal error: conversion between matrix type and scalar type");
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index dae8005f870d7..333780e9cd1f4 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -973,6 +973,23 @@ LogicalResult cir::CastOp::verify() {
resType = resVTy.getElementType();
}
+ auto srcMatTy = mlir::dyn_cast<cir::MatrixType>(srcType);
+ auto resMatTy = mlir::dyn_cast<cir::MatrixType>(resType);
+ if (srcMatTy && resMatTy) {
+ if ((kind == cir::CastKind::int_to_float ||
+ kind == cir::CastKind::float_to_int) &&
+ (srcMatTy.getNumRows() != resMatTy.getNumRows() ||
+ srcMatTy.getNumColumns() != resMatTy.getNumColumns())) {
+ return emitOpError()
+ << "matrix float-to-int and int-to-float casts require "
+ "source and destination matrices to have the same number of "
+ "rows and columns";
+ }
+
+ srcType = srcMatTy.getElementType();
+ resType = resMatTy.getElementType();
+ }
+
switch (kind) {
case cir::CastKind::int_to_bool: {
if (!mlir::isa<cir::BoolType>(resType))
diff --git a/clang/test/CIR/CodeGenHLSL/matrix-element-type-cast.hlsl b/clang/test/CIR/CodeGenHLSL/matrix-element-type-cast.hlsl
new file mode 100644
index 0000000000000..7e88468482fdd
--- /dev/null
+++ b/clang/test/CIR/CodeGenHLSL/matrix-element-type-cast.hlsl
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -x hlsl -std=hlsl2021 -triple spirv-unknown-vulkan-library -finclude-default-header -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -x hlsl -std=hlsl2021 -triple spirv-unknown-vulkan-library -finclude-default-header -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -x hlsl -std=hlsl2021 -triple spirv-unknown-vulkan-library -finclude-default-header -emit-llvm -disable-llvm-passes %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
+
+void fp_matrix_to_int_matrix() {
+ float3x2 a;
+ int3x2 b = (int3x2) a;
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.matrix<3 x 2 x !cir.float>>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!cir.matrix<3 x 2 x !s32i>>
+// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!cir.matrix<3 x 2 x !cir.float>>, !cir.matrix<3 x 2 x !cir.float>
+// CIR: %[[RESULT:.*]] = cir.cast float_to_int %[[TMP_A]] : !cir.matrix<3 x 2 x !cir.float> -> !cir.matrix<3 x 2 x !s32i>
+// CIR: cir.store {{.*}} %[[RESULT]], %[[B_ADDR]] : !cir.matrix<3 x 2 x !s32i>, !cir.ptr<!cir.matrix<3 x 2 x !s32i>>
+
+// LLVM: %[[A_ADDR:.*]] = alloca [2 x <3 x float>], align 4
+// LLVM: %[[B_ADDR:.*]] = alloca [2 x <3 x i32>], align 4
+// LLVM: %[[TMP_A:.*]] = load <6 x float>, ptr %[[A_ADDR]], align 4
+// LLVM: %[[RESULT:.*]] = fptosi <6 x float> %[[TMP_A]] to <6 x i32>
+// LLVM: store <6 x i32> %[[RESULT]], ptr %[[B_ADDR]], align 4
+
+void int_matrix_to_fp_matrix() {
+ int3x2 a;
+ float3x2 b = (float3x2) a;
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.matrix<3 x 2 x !s32i>>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!cir.matrix<3 x 2 x !cir.float>>
+// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!cir.matrix<3 x 2 x !s32i>>, !cir.matrix<3 x 2 x !s32i>
+// CIR: %[[RESULT:.*]] = cir.cast int_to_float %[[TMP_A]] : !cir.matrix<3 x 2 x !s32i> -> !cir.matrix<3 x 2 x !cir.float>
+// CIR: cir.store {{.*}} %[[RESULT]], %[[B_ADDR]] : !cir.matrix<3 x 2 x !cir.float>, !cir.ptr<!cir.matrix<3 x 2 x !cir.float>>
+
+// TODO(CIR): Update sitofp check once fast math flags are implemented
+
+// LLVM: %[[A_ADDR:.*]] = alloca [2 x <3 x i32>], align 4
+// LLVM: %[[B_ADDR:.*]] = alloca [2 x <3 x float>], align 4
+// LLVM: %[[TMP_A:.*]] = load <6 x i32>, ptr %[[A_ADDR]], align 4
+// LLVM: %[[RESULT:.*]] = sitofp{{.*}} <6 x i32> %[[TMP_A]] to <6 x float>
+// LLVM: store <6 x float> %[[RESULT]], ptr %[[B_ADDR]], align 4
diff --git a/clang/test/CIR/IR/invalid-cast.cir b/clang/test/CIR/IR/invalid-cast.cir
index 67e3dbe68306e..29eb8713db837 100644
--- a/clang/test/CIR/IR/invalid-cast.cir
+++ b/clang/test/CIR/IR/invalid-cast.cir
@@ -40,6 +40,18 @@ module {
// -----
+!s8i = !cir.int<s, 8>
+
+module {
+ cir.func @int_to_float(%in: !cir.matrix<3 x 2 x !s8i>) {
+ // expected-error at +1 {{matrix float-to-int and int-to-float casts require source and destination matrices to have the same number of rows and columns}}
+ cir.cast int_to_float %in : !cir.matrix<3 x 2 x !s8i> -> !cir.matrix<2 x 3 x !cir.float>
+ cir.return
+ }
+}
+
+// -----
+
!s32i = !cir.int<s, 32>
module {
``````````
</details>
https://github.com/llvm/llvm-project/pull/238862
More information about the cfe-commits
mailing list