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

Chris B via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 12 13:06:13 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)
+    SrcTy = SrcVecTy->getElementType();
+
+  if (ContainsBitField(DestTy))
+    return false;
+
----------------
llvm-beanz wrote:

I assume we should also add something like:
```
if (DestTy->isHLSLIntangibleType())
  return false;
```

I assume you can't initialize an aggregate that contains an intangible member because there would be no valid cast from the scalar to the intangible member's type.

Does that seem correct?

Note: It looks like later on you do check for convertibility of the types, maybe that is good enough? It will really depend on the quality of the diagnostic.

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


More information about the cfe-commits mailing list