[clang] 2ceae9c - [Clang][Sema] Fix Matrix HLSLAggregateSplatCast with CStyleCast (#239737)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 03:53:25 PST 2026


Author: Amr Hesham
Date: 2026-11-07T12:53:20+01:00
New Revision: 2ceae9c341fc4cc7fdf4d8e21d50463a3eb363d7

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

LOG: [Clang][Sema] Fix Matrix HLSLAggregateSplatCast with CStyleCast (#239737)

Add missing preparation implicit conversion to fix the MatrixType
HLSLAggregateSplatCast with CStyleCast expr

Fixes: #239736

Added: 
    

Modified: 
    clang/lib/Sema/SemaCast.cpp
    clang/test/CodeGenHLSL/BasicFeatures/MatrixSplat.hlsl

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index cea9a593e58ada..825a98f3882ec2 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -2972,6 +2972,7 @@ bool CastOperation::CheckHLSLCStyleCast(CheckedConversionKind CCK) {
       SrcExpr = Self.ImpCastExprToType(
           SrcExpr.get(), MT->getElementType(), CK_HLSLMatrixTruncation,
           SrcExpr.get()->getValueKind(), nullptr, CCK);
+
     // Inserting a scalar cast here allows for a simplified codegen in
     // the case the destTy is a vector
     if (const VectorType *DVT = DestType->getAs<VectorType>())
@@ -2979,6 +2980,15 @@ bool CastOperation::CheckHLSLCStyleCast(CheckedConversionKind CCK) {
           SrcExpr.get(), DVT->getElementType(),
           Self.PrepareScalarCast(SrcExpr, DVT->getElementType()),
           SrcExpr.get()->getValueKind(), nullptr, CCK);
+
+    // Inserting a scalar cast here allows for a simplified codegen in
+    // the case the destTy is a matrix
+    if (const MatrixType *DMT = DestType->getAs<MatrixType>())
+      SrcExpr = Self.ImpCastExprToType(
+          SrcExpr.get(), DMT->getElementType(),
+          Self.PrepareScalarCast(SrcExpr, DMT->getElementType()),
+          SrcExpr.get()->getValueKind(), nullptr, CCK);
+
     Kind = CK_HLSLAggregateSplatCast;
     return true;
   }

diff  --git a/clang/test/CodeGenHLSL/BasicFeatures/MatrixSplat.hlsl b/clang/test/CodeGenHLSL/BasicFeatures/MatrixSplat.hlsl
index 597733fce66e62..39b1f5d879f4b4 100644
--- a/clang/test/CodeGenHLSL/BasicFeatures/MatrixSplat.hlsl
+++ b/clang/test/CodeGenHLSL/BasicFeatures/MatrixSplat.hlsl
@@ -202,3 +202,23 @@ void ImplicitFloatToBoolCastThenSplat(float Value) {
 void ImplicitBoolToFloatCastThenSplat(bool Value) {
     float3x2 M = Value;
 }
+
+// CHECK-LABEL: define hidden void @_Z32ExplicitFloatToBoolCastThenSplatf(
+// CHECK-SAME: float noundef nofpclass(nan inf) [[VALUE:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[TMP0:%.*]] = call token @llvm.experimental.convergence.entry()
+// CHECK-NEXT:    [[VALUE_ADDR:%.*]] = alloca float, align 4
+// CHECK-NEXT:    [[M:%.*]] = alloca [3 x <2 x i32>], align 4
+// CHECK-NEXT:    store float [[VALUE]], ptr [[VALUE_ADDR]], align 4
+// CHECK-NEXT:    [[TMP1:%.*]] = load float, ptr [[VALUE_ADDR]], align 4
+// CHECK-NEXT:    [[TOBOOL:%.*]] = fcmp reassoc nnan ninf nsz arcp afn une float [[TMP1]], 0.000000e+00
+// CHECK-NEXT:    [[SPLAT_SPLATINSERT:%.*]] = insertelement <6 x i1> poison, i1 [[TOBOOL]], i64 0
+// CHECK-NEXT:    [[SPLAT_SPLAT:%.*]] = shufflevector <6 x i1> [[SPLAT_SPLATINSERT]], <6 x i1> poison, <6 x i32> zeroinitializer
+// CHECK-NEXT:    [[TMP2:%.*]] = zext <6 x i1> [[SPLAT_SPLAT]] to <6 x i32>
+// CHECK-NEXT:    store <6 x i32> [[TMP2]], ptr [[M]], align 4
+// CHECK-NEXT:    ret void
+//
+void ExplicitFloatToBoolCastThenSplat(float Value) {
+    bool2x3 M = (bool2x3) Value;
+}
+


        


More information about the cfe-commits mailing list