[clang] eaa4b4b - [CIR] Implement HLSL matrix to scalar truncation (#248650)

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 02:54:25 PST 2026


Author: Amr Hesham
Date: 2026-12-04T11:54:20+01:00
New Revision: eaa4b4bfeb5243eeebfc320c0fa32dc748b2f15c

URL: https://github.com/llvm/llvm-project/commit/eaa4b4bfeb5243eeebfc320c0fa32dc748b2f15c
DIFF: https://github.com/llvm/llvm-project/commit/eaa4b4bfeb5243eeebfc320c0fa32dc748b2f15c.diff

LOG: [CIR] Implement HLSL matrix to scalar truncation (#248650)

Implement support for HLSL matrix truncation cast to scalar

Added: 
    

Modified: 
    clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
    clang/test/CIR/CodeGenHLSL/matrixx-truncation.hlsl

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index a89e6faa1fa7b3..01a0a85a18f225 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -3048,10 +3048,8 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
       return builder.createVecShuffle(loc, matrix, resultTy, mask);
     }
 
-    cgf.getCIRGenModule().errorNYI(subExpr->getSourceRange(),
-                                   "ScalarExprEmitter::VisitCastExpr: HLSL "
-                                   "Matrix truncation with non-matrix dest");
-    return {};
+    cir::ConstantOp zero = builder.getNullValue(cgf.sizeTy, loc);
+    return builder.createMatrixExtract(loc, matrix, zero, zero);
   }
   default:
     cgf.getCIRGenModule().errorNYI(subExpr->getSourceRange(),

diff  --git a/clang/test/CIR/CodeGenHLSL/matrixx-truncation.hlsl b/clang/test/CIR/CodeGenHLSL/matrixx-truncation.hlsl
index 10cef611a3f968..08415b9483efee 100644
--- a/clang/test/CIR/CodeGenHLSL/matrixx-truncation.hlsl
+++ b/clang/test/CIR/CodeGenHLSL/matrixx-truncation.hlsl
@@ -1,9 +1,9 @@
 // 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: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM,LLVMCIR
 // 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
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM,OGCG
 
 void trunc_cast() {
   int4x4 a;
@@ -22,3 +22,22 @@ void trunc_cast() {
 // LLVM: %[[TMP_A:.*]] = load <16 x i32>, ptr %[[A_ADDR]], align 4
 // LLVM: %[[RESULT:.*]] = shufflevector <16 x i32> %[[TMP_A]], <16 x i32> poison, <12 x i32> <i32 0, i32 1, i32 2, i32 4, i32 5, i32 6, i32 8, i32 9, i32 10, i32 12, i32 13, i32 14>
 // LLVM: store <12 x i32> %[[RESULT]], ptr %[[B_ADDR]], align 4
+
+void trunc_cast_matrix_to_scalar() {
+  int4x4 a;
+  int b = (int)a;
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.matrix<4 x 4 x !s32i>>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!s32i>
+// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!cir.matrix<4 x 4 x !s32i>>, !cir.matrix<4 x 4 x !s32i>
+// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !u32i
+// CIR: %[[RESULT:.*]] = cir.matrix.extract %[[TMP_A]][%[[ZERO]] : !u32i] [%[[ZERO]] : !u32i] : !cir.matrix<4 x 4 x !s32i>
+// CIR: cir.store {{.*}} %[[RESULT]], %[[B_ADDR]] : !s32i, !cir.ptr<!s32i>
+
+// LLVM: %[[A_ADDR:.*]] = alloca [4 x <4 x i32>], align 4
+// LLVM: %[[B_ADDR:.*]] = alloca i32, align 4
+// LLVM: %[[TMP_A:.*]] = load <16 x i32>, ptr %[[A_ADDR]], align 4
+// LLVMCIR: %[[RESULT:.*]] = extractelement <16 x i32> %[[TMP_A]], i64 0
+// OGCG: %[[RESULT:.*]] = extractelement <16 x i32> %[[TMP_A]], i32 0
+// LLVM: store i32 %[[RESULT]], ptr %[[B_ADDR]], align 4


        


More information about the cfe-commits mailing list