@@ -491,6 +491,33 @@ static bool isTrivialFiller(Expr *E) {
491491 return false ;
492492}
493493
494+ static void EmitHLSLSplatCast (CodeGenFunction &CGF, Address DestVal,
495+ QualType DestTy, llvm::Value *SrcVal,
496+ QualType SrcTy, SourceLocation Loc) {
497+ // Flatten our destination
498+ SmallVector<QualType> DestTypes; // Flattened type
499+ SmallVector<llvm::Value *, 4 > IdxList;
500+ SmallVector<std::pair<Address, llvm::Value *>, 16 > StoreGEPList;
501+ // ^^ Flattened accesses to DestVal we want to store into
502+ CGF.FlattenAccessAndType (DestVal, DestTy, IdxList, StoreGEPList,
503+ DestTypes);
504+
505+ if (const VectorType *VT = SrcTy->getAs <VectorType>()) {
506+ assert (VT->getNumElements () == 1 && " Invalid HLSL splat cast." );
507+
508+ SrcTy = VT->getElementType ();
509+ SrcVal = CGF.Builder .CreateExtractElement (SrcVal, (uint64_t )0 ,
510+ " vec.load" );
511+ }
512+ assert (SrcTy->isScalarType () && " Invalid HLSL splat cast." );
513+ for (unsigned i = 0 ; i < StoreGEPList.size (); i ++) {
514+ llvm::Value *Cast = CGF.EmitScalarConversion (SrcVal, SrcTy,
515+ DestTypes[i],
516+ Loc);
517+ CGF.PerformStore (StoreGEPList[i], Cast);
518+ }
519+ }
520+
494521// emit a flat cast where the RHS is a scalar, including vector
495522static void EmitHLSLScalarFlatCast (CodeGenFunction &CGF, Address DestVal,
496523 QualType DestTy, llvm::Value *SrcVal,
@@ -963,6 +990,21 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {
963990 case CK_HLSLArrayRValue:
964991 Visit (E->getSubExpr ());
965992 break ;
993+ case CK_HLSLSplatCast: {
994+ Expr *Src = E->getSubExpr ();
995+ QualType SrcTy = Src->getType ();
996+ RValue RV = CGF.EmitAnyExpr (Src);
997+ QualType DestTy = E->getType ();
998+ Address DestVal = Dest.getAddress ();
999+ SourceLocation Loc = E->getExprLoc ();
1000+
1001+ if (RV.isScalar ()) {
1002+ llvm::Value *SrcVal = RV.getScalarVal ();
1003+ EmitHLSLSplatCast (CGF, DestVal, DestTy, SrcVal, SrcTy, Loc);
1004+ break ;
1005+ }
1006+ llvm_unreachable (" RHS of HLSL splat cast must be a scalar or vector." );
1007+ }
9661008 case CK_HLSLElementwiseCast: {
9671009 Expr *Src = E->getSubExpr ();
9681010 QualType SrcTy = Src->getType ();
0 commit comments