@@ -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,
@@ -965,6 +992,21 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {
965992 case CK_HLSLArrayRValue:
966993 Visit (E->getSubExpr ());
967994 break ;
995+ case CK_HLSLSplatCast: {
996+ Expr *Src = E->getSubExpr ();
997+ QualType SrcTy = Src->getType ();
998+ RValue RV = CGF.EmitAnyExpr (Src);
999+ QualType DestTy = E->getType ();
1000+ Address DestVal = Dest.getAddress ();
1001+ SourceLocation Loc = E->getExprLoc ();
1002+
1003+ if (RV.isScalar ()) {
1004+ llvm::Value *SrcVal = RV.getScalarVal ();
1005+ EmitHLSLSplatCast (CGF, DestVal, DestTy, SrcVal, SrcTy, Loc);
1006+ break ;
1007+ }
1008+ llvm_unreachable (" RHS of HLSL splat cast must be a scalar or vector." );
1009+ }
9681010 case CK_HLSLAggregateCast: {
9691011 Expr *Src = E->getSubExpr ();
9701012 QualType SrcTy = Src->getType ();
0 commit comments