[clang] [HLSL] Allow truncation to scalar (PR #104844)

Chris B via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 9 14:45:13 PDT 2024


================
@@ -4828,18 +4830,23 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
     }
     case ICK_HLSL_Vector_Truncation: {
       // Note: HLSL built-in vectors are ExtVectors. Since this truncates a
-      // vector to a smaller vector, this can only operate on arguments where
-      // the source and destination types are ExtVectors.
-      assert(From->getType()->isExtVectorType() && ToType->isExtVectorType() &&
-             "HLSL vector truncation should only apply to ExtVectors");
+      // vector to a smaller vector or to a scalar, this can only operate on
+      // arguments where the source type is an ExtVector and the destination
+      // type is destination type is either an ExtVectorType or a builtin scalar
+      // type.
       auto *FromVec = From->getType()->castAs<VectorType>();
-      auto *ToVec = ToType->castAs<VectorType>();
       QualType ElType = FromVec->getElementType();
-      QualType TruncTy =
-          Context.getExtVectorType(ElType, ToVec->getNumElements());
-      From = ImpCastExprToType(From, TruncTy, CK_HLSLVectorTruncation,
-                               From->getValueKind())
-                 .get();
+      if (auto *ToVec = ToType->getAs<VectorType>()) {
+        QualType TruncTy =
+            Context.getExtVectorType(ElType, ToVec->getNumElements());
+        From = ImpCastExprToType(From, TruncTy, CK_HLSLVectorTruncation,
+                                 From->getValueKind())
+                   .get();
+      } else {
+        From = ImpCastExprToType(From, ElType, CK_HLSLVectorTruncation,
+                                 From->getValueKind())
+                   .get();
+      }
----------------
llvm-beanz wrote:

I changed the naming on this because `ToType` is already used, so this caused shadowing, but otherwise I adopted this structure.

https://github.com/llvm/llvm-project/pull/104844


More information about the cfe-commits mailing list