[clang] [CIR] Implement HLSL vector truncation cast (PR #239356)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 12:03:48 PST 2026
https://github.com/AmrDeveloper created https://github.com/llvm/llvm-project/pull/239356
Implement support for the HLSL vector truncation cast
>From bb5e7fbc8a9da3a10a7e4f90f3533818ad57b478 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Thu, 5 Nov 2026 20:58:23 +0100
Subject: [PATCH] [CIR] Implement HLSL vector truncation cast
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 14 +++++++
.../CIR/CodeGenHLSL/vector-truncation.hlsl | 42 +++++++++++++++++++
2 files changed, 56 insertions(+)
create mode 100644 clang/test/CIR/CodeGenHLSL/vector-truncation.hlsl
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index cf819fcee1296..f0c1714ba3cc1 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -2940,6 +2940,20 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
case CK_FunctionToPointerDecay:
return cgf.emitLValue(subExpr).getPointer();
+ case CK_HLSLVectorTruncation: {
+ assert((destTy->isVectorType() || destTy->isBuiltinType()) &&
+ "Destination type must be a vector or builtin type.");
+ mlir::Value vec = Visit(subExpr);
+ mlir::Location loc = cgf.getLoc(subExpr->getSourceRange());
+ if (auto *vecTy = destTy->getAs<VectorType>()) {
+ unsigned numElts = vecTy->getNumElements();
+ llvm::SmallVector<int64_t, 4> mask(numElts);
+ std::iota(mask.begin(), mask.end(), 0);
+ return builder.createVecShuffle(loc, vec, mask);
+ }
+ return builder.createExtractElement(loc, vec, /*idx=*/0);
+ }
+
default:
cgf.getCIRGenModule().errorNYI(subExpr->getSourceRange(),
"CastExpr: ", ce->getCastKindName());
diff --git a/clang/test/CIR/CodeGenHLSL/vector-truncation.hlsl b/clang/test/CIR/CodeGenHLSL/vector-truncation.hlsl
new file mode 100644
index 0000000000000..baa04ebd486d1
--- /dev/null
+++ b/clang/test/CIR/CodeGenHLSL/vector-truncation.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 vector_truncation_to_scalar() {
+ float4 a;
+ float b = (float) a;
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.vector<4 x !cir.float>>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!cir.float>
+// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!cir.vector<4 x !cir.float>>, !cir.vector<4 x !cir.float>
+// CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !u64i
+// CIR: %[[RESULT:.*]] = cir.vec.extract %[[TMP_A]][%[[CONST_0]] : !u64i] : !cir.vector<4 x !cir.float>
+// CIR: cir.store {{.*}} %[[RESULT]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float>
+
+// LLVM: %[[A_ADDR:.*]] = alloca <4 x float>, align 4
+// LLVM: %[[B_ADDR:.*]] = alloca float, align 4
+// LLVM: %[[TMP_A:.*]] = load <4 x float>, ptr %[[A_ADDR]], align 4
+// LLVM: %[[RESULT:.*]] = extractelement <4 x float> %[[TMP_A]], i64 0
+// LLVM: store float %[[RESULT]], ptr %[[B_ADDR]], align 4
+
+void vector_truncation_to_vector() {
+ float4 a;
+ float2 b = (float2) a;
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.vector<4 x !cir.float>>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!cir.vector<2 x !cir.float>>
+// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!cir.vector<4 x !cir.float>>, !cir.vector<4 x !cir.float>
+// CIR: %[[POISON:.*]] = cir.const #cir.poison : !cir.vector<4 x !cir.float>
+// CIR: %[[RESULT:.*]] = cir.vec.shuffle(%[[TMP_A]], %[[POISON]] : !cir.vector<4 x !cir.float>) [#cir.int<0> : !s32i, #cir.int<1> : !s32i] : !cir.vector<2 x !cir.float>
+// CIR: cir.store {{.*}} %[[RESULT]], %[[B_ADDR]] : !cir.vector<2 x !cir.float>, !cir.ptr<!cir.vector<2 x !cir.float>>
+
+// LLVM: %[[A_ADDR:.*]] = alloca <4 x float>, align 4
+// LLVM: %[[B_ADDR:.*]] = alloca <2 x float>, align 4
+// LLVM: %[[TMP_A:.*]] = load <4 x float>, ptr %[[A_ADDR]], align 4
+// LLVM: %[[RESULT:.*]] = shufflevector <4 x float> %[[TMP_A]], <4 x float> poison, <2 x i32> <i32 0, i32 1>
+// LLVM: store <2 x float> %[[RESULT]], ptr %[[B_ADDR]], align 4
More information about the cfe-commits
mailing list