[clang] [HLSL] Implement HLSL splatting (PR #118992)

Ashley Coleman via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 12 10:43:19 PST 2025


================
@@ -2804,6 +2804,42 @@ bool SemaHLSL::ContainsBitField(QualType BaseTy) {
   return false;
 }
 
+// Can perform an HLSL splat cast if the Dest is an aggregate and the
+// Src is a scalar or a vector of length 1
+// Or if Dest is a vector and Src is a vector of length 1
+bool SemaHLSL::CanPerformSplatCast(Expr *Src, QualType DestTy) {
+
+  QualType SrcTy = Src->getType();
+  // Not a valid HLSL Splat cast if Dest is a scalar or if this is going to
+  // be a vector splat from a scalar.
+  if ((SrcTy->isScalarType() && DestTy->isVectorType()) ||
+      DestTy->isScalarType())
+    return false;
+
+  const VectorType *SrcVecTy = SrcTy->getAs<VectorType>();
+
+  // Src isn't a scalar or a vector of length 1
+  if (!SrcTy->isScalarType() && !(SrcVecTy && SrcVecTy->getNumElements() == 1))
+    return false;
+
+  if (SrcVecTy)
----------------
V-FEXrt wrote:

I know it's because of short circuiting rules and the weirdness around picking one of the sides but it feels strange checking this here after having the previous check. I couldn't think of a better control flow so this comment isn't actionable. Just a thought in case you happen to have a different way to formulate it

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


More information about the cfe-commits mailing list