Skip to content

Commit 848315d

Browse files
committed
self review
1 parent 844ba82 commit 848315d

File tree

6 files changed

+21
-29
lines changed

6 files changed

+21
-29
lines changed

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class SemaHLSL : public SemaBase {
144144
bool CanPerformScalarCast(QualType SrcTy, QualType DestTy);
145145
bool ContainsBitField(QualType BaseTy);
146146
bool CanPerformElementwiseCast(Expr *Src, QualType DestType);
147-
bool CanPerformSplat(Expr *Src, QualType DestType);
147+
bool CanPerformSplatCast(Expr *Src, QualType DestType);
148148
ExprResult ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg);
149149

150150
QualType getInoutParameterType(QualType Ty);

clang/lib/CodeGen/CGExprAgg.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -500,25 +500,19 @@ static void EmitHLSLSplatCast(CodeGenFunction &CGF, Address DestVal,
500500
// ^^ Flattened accesses to DestVal we want to store into
501501
CGF.FlattenAccessAndType(DestVal, DestTy, StoreGEPList, DestTypes);
502502

503-
if (const VectorType *VT = SrcTy->getAs<VectorType>()) {
504-
assert(VT->getNumElements() == 1 && "Invalid HLSL splat cast.");
505-
506-
SrcTy = VT->getElementType();
507-
SrcVal = CGF.Builder.CreateExtractElement(SrcVal, (uint64_t)0, "vec.load");
508-
}
509503
assert(SrcTy->isScalarType() && "Invalid HLSL splat cast.");
510-
for (unsigned i = 0; i < StoreGEPList.size(); i++) {
504+
for (unsigned I = 0, Size = StoreGEPList.size(); I < Size; I++) {
511505
llvm::Value *Cast =
512-
CGF.EmitScalarConversion(SrcVal, SrcTy, DestTypes[i], Loc);
506+
CGF.EmitScalarConversion(SrcVal, SrcTy, DestTypes[I], Loc);
513507

514508
// store back
515-
llvm::Value *Idx = StoreGEPList[i].second;
509+
llvm::Value *Idx = StoreGEPList[I].second;
516510
if (Idx) {
517511
llvm::Value *V =
518-
CGF.Builder.CreateLoad(StoreGEPList[i].first, "load.for.insert");
512+
CGF.Builder.CreateLoad(StoreGEPList[I].first, "load.for.insert");
519513
Cast = CGF.Builder.CreateInsertElement(V, Cast, Idx);
520514
}
521-
CGF.Builder.CreateStore(Cast, StoreGEPList[i].first);
515+
CGF.Builder.CreateStore(Cast, StoreGEPList[I].first);
522516
}
523517
}
524518

@@ -1002,12 +996,10 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {
1002996
Address DestVal = Dest.getAddress();
1003997
SourceLocation Loc = E->getExprLoc();
1004998

1005-
if (RV.isScalar()) {
1006-
llvm::Value *SrcVal = RV.getScalarVal();
1007-
EmitHLSLSplatCast(CGF, DestVal, DestTy, SrcVal, SrcTy, Loc);
1008-
break;
1009-
}
1010-
llvm_unreachable("RHS of HLSL splat cast must be a scalar or vector.");
999+
assert (RV.isScalar() && "RHS of HLSL splat cast must be a scalar.");
1000+
llvm::Value *SrcVal = RV.getScalarVal();
1001+
EmitHLSLSplatCast(CGF, DestVal, DestTy, SrcVal, SrcTy, Loc);
1002+
break;
10111003
}
10121004
case CK_HLSLElementwiseCast: {
10131005
Expr *Src = E->getSubExpr();

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,19 +2796,14 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
27962796
return Builder.CreateExtractElement(Vec, Zero, "cast.vtrunc");
27972797
}
27982798
case CK_HLSLSplatCast: {
2799-
// This code should only handle splatting from vectors of length 1.
2799+
// This cast should only handle splatting from vectors of length 1.
2800+
// But in Sema a cast should have been inserted to convert the vec1 to a scalar
28002801
assert(DestTy->isVectorType() && "Destination type must be a vector.");
28012802
auto *DestVecTy = DestTy->getAs<VectorType>();
28022803
QualType SrcTy = E->getType();
28032804
SourceLocation Loc = CE->getExprLoc();
28042805
Value *V = Visit(const_cast<Expr *>(E));
2805-
assert(SrcTy->isVectorType() && "Invalid HLSL splat cast.");
2806-
2807-
auto *VecTy = SrcTy->getAs<VectorType>();
2808-
assert(VecTy->getNumElements() == 1 && "Invalid HLSL splat cast.");
2809-
2810-
V = CGF.Builder.CreateExtractElement(V, (uint64_t)0, "vec.load");
2811-
SrcTy = VecTy->getElementType();
2806+
assert(SrcTy->isBuiltinType() && "Invalid HLSL splat cast.");
28122807

28132808
Value *Cast =
28142809
EmitScalarConversion(V, SrcTy, DestVecTy->getElementType(), Loc);

clang/lib/Sema/SemaCast.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2780,7 +2780,12 @@ void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
27802780
// This case should not trigger on regular vector splat
27812781
QualType SrcTy = SrcExpr.get()->getType();
27822782
if (Self.getLangOpts().HLSL &&
2783-
Self.HLSL().CanPerformSplat(SrcExpr.get(), DestType)) {
2783+
Self.HLSL().CanPerformSplatCast(SrcExpr.get(), DestType)) {
2784+
const VectorType *VT = SrcTy->getAs<VectorType>();
2785+
// change splat from vec1 case to splat from scalar
2786+
if (VT && VT->getNumElements() == 1)
2787+
SrcExpr = Self.ImpCastExprToType(SrcExpr.get(), VT->getElementType(),
2788+
CK_HLSLVectorTruncation, VK_PRValue, nullptr, CCK);
27842789
Kind = CK_HLSLSplatCast;
27852790
return;
27862791
}

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2807,7 +2807,7 @@ bool SemaHLSL::ContainsBitField(QualType BaseTy) {
28072807
// Can perform an HLSL splat cast if the Dest is an aggregate and the
28082808
// Src is a scalar or a vector of length 1
28092809
// Or if Dest is a vector and Src is a vector of length 1
2810-
bool SemaHLSL::CanPerformSplat(Expr *Src, QualType DestTy) {
2810+
bool SemaHLSL::CanPerformSplatCast(Expr *Src, QualType DestTy) {
28112811

28122812
QualType SrcTy = Src->getType();
28132813
if (SrcTy->isScalarType() && DestTy->isVectorType())

clang/test/SemaHLSL/Language/SplatCasts.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ struct S {
2222
// CHECK-NEXt: IntegerLiteral {{.*}} 'int' 5
2323
export void call2() {
2424
S s = (S)5;
25-
}
25+
}

0 commit comments

Comments
 (0)