diff --git a/clang/include/clang/AST/OperationKinds.def b/clang/include/clang/AST/OperationKinds.def index 8dd98730dff74..ef05072800f11 100644 --- a/clang/include/clang/AST/OperationKinds.def +++ b/clang/include/clang/AST/OperationKinds.def @@ -361,6 +361,9 @@ CAST_OPERATION(AddressSpaceConversion) // Convert an integer initializer to an OpenCL sampler. CAST_OPERATION(IntToOCLSampler) +// Truncate a vector type by dropping elements from the end (HLSL only). +CAST_OPERATION(HLSLVectorTruncation) + //===- Binary Operations -------------------------------------------------===// // Operators listed in order of precedence. // Note that additions to this should also update the StmtVisitor class, diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 935d94e38cc37..3f83dab879733 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -12112,6 +12112,9 @@ def err_hlsl_operator_unsupported : Error< def err_hlsl_param_qualifier_mismatch : Error<"conflicting parameter qualifier %0 on parameter %1">; +def warn_hlsl_impcast_vector_truncation : Warning< + "implicit conversion truncates vector: %0 to %1">, InGroup; + // Layout randomization diagnostics. def err_non_designated_init_used : Error< "a randomized struct can only be initialized with a designated initializer">; diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h index 9b342c0916844..e4717dd5baf1e 100644 --- a/clang/include/clang/Sema/Overload.h +++ b/clang/include/clang/Sema/Overload.h @@ -195,6 +195,9 @@ class Sema; /// Fixed point type conversions according to N1169. ICK_Fixed_Point_Conversion, + /// HLSL vector truncation. + ICK_HLSL_Vector_Truncation, + /// The number of conversion kinds ICK_Num_Conversion_Kinds, }; @@ -271,6 +274,12 @@ class Sema; /// pointer-to-member conversion, or boolean conversion. ImplicitConversionKind Second : 8; + /// Element - Between the second and third conversion a vector or matrix + /// element conversion may occur. If this is not ICK_Identity this + /// conversion is applied element-wise to each element in the vector or + /// matrix. + ImplicitConversionKind Element : 8; + /// Third - The third conversion can be a qualification conversion /// or a function conversion. ImplicitConversionKind Third : 8; @@ -367,7 +376,8 @@ class Sema; void setAsIdentityConversion(); bool isIdentityConversion() const { - return Second == ICK_Identity && Third == ICK_Identity; + return Second == ICK_Identity && Element == ICK_Identity && + Third == ICK_Identity; } ImplicitConversionRank getRank() const; diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 8b10e28958326..cc0407131d793 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1897,6 +1897,7 @@ bool CastExpr::CastConsistency() const { case CK_FixedPointToIntegral: case CK_IntegralToFixedPoint: case CK_MatrixCast: + case CK_HLSLVectorTruncation: assert(!getType()->isBooleanType() && "unheralded conversion to bool"); goto CheckNoBasePath; diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 010010120cf6d..fcf8f6591a792 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -13980,6 +13980,7 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { case CK_FixedPointCast: case CK_IntegralToFixedPoint: case CK_MatrixCast: + case CK_HLSLVectorTruncation: llvm_unreachable("invalid cast kind for integral value"); case CK_BitCast: @@ -14818,6 +14819,7 @@ bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { case CK_FixedPointToIntegral: case CK_IntegralToFixedPoint: case CK_MatrixCast: + case CK_HLSLVectorTruncation: llvm_unreachable("invalid cast kind for complex value"); case CK_LValueToRValue: diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 4a2f3caad6588..59a7fe8925001 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -5180,6 +5180,7 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { case CK_FixedPointToIntegral: case CK_IntegralToFixedPoint: case CK_MatrixCast: + case CK_HLSLVectorTruncation: return EmitUnsupportedLValue(E, "unexpected cast lvalue"); case CK_Dependent: diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 22f55fe9aac90..d0d6202974fe9 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -933,6 +933,7 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) { case CK_BuiltinFnToFnPtr: case CK_ZeroToOCLOpaqueType: case CK_MatrixCast: + case CK_HLSLVectorTruncation: case CK_IntToOCLSampler: case CK_FloatingToFixedPoint: @@ -1457,6 +1458,7 @@ static bool castPreservesZero(const CastExpr *CE) { case CK_MatrixCast: case CK_NonAtomicToAtomic: case CK_AtomicToNonAtomic: + case CK_HLSLVectorTruncation: return true; case CK_BaseToDerivedMemberPointer: diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index 9ddf0e763f139..176a7e00141f9 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -564,6 +564,7 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op, case CK_FixedPointToIntegral: case CK_IntegralToFixedPoint: case CK_MatrixCast: + case CK_HLSLVectorTruncation: llvm_unreachable("invalid cast kind for complex value"); case CK_FloatingRealToComplex: diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index a054f38e860b7..75286dceb13a7 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -1225,6 +1225,7 @@ class ConstExprEmitter : case CK_IntegralToFixedPoint: case CK_ZeroToOCLOpaqueType: case CK_MatrixCast: + case CK_HLSLVectorTruncation: return nullptr; } llvm_unreachable("Invalid CastKind"); diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index aa805f291d175..13526de3e7642 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2408,6 +2408,12 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { CE->getExprLoc()); case CK_IntegralCast: { + if (E->getType()->isExtVectorType() && DestTy->isExtVectorType()) { + QualType SrcElTy = E->getType()->castAs()->getElementType(); + return Builder.CreateIntCast(Visit(E), ConvertType(DestTy), + SrcElTy->isSignedIntegerOrEnumerationType(), + "conv"); + } ScalarConversionOpts Opts; if (auto *ICE = dyn_cast(CE)) { if (!ICE->isPartOfExplicitCast()) @@ -2416,9 +2422,50 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { return EmitScalarConversion(Visit(E), E->getType(), DestTy, CE->getExprLoc(), Opts); } - case CK_IntegralToFloating: - case CK_FloatingToIntegral: - case CK_FloatingCast: + case CK_IntegralToFloating: { + if (E->getType()->isVectorType() && DestTy->isVectorType()) { + // TODO: Support constrained FP intrinsics. + assert(!Builder.getIsFPConstrained() && + "FP Constrained vector casts not supported yet."); + QualType SrcElTy = E->getType()->castAs()->getElementType(); + if (SrcElTy->isSignedIntegerOrEnumerationType()) + return Builder.CreateSIToFP(Visit(E), ConvertType(DestTy), "conv"); + return Builder.CreateUIToFP(Visit(E), ConvertType(DestTy), "conv"); + } + CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE); + return EmitScalarConversion(Visit(E), E->getType(), DestTy, + CE->getExprLoc()); + } + case CK_FloatingToIntegral: { + if (E->getType()->isVectorType() && DestTy->isVectorType()) { + // TODO: Support constrained FP intrinsics. + assert(!Builder.getIsFPConstrained() && + "FP Constrained vector casts not supported yet."); + QualType DstElTy = DestTy->castAs()->getElementType(); + if (DstElTy->isSignedIntegerOrEnumerationType()) + return Builder.CreateFPToSI(Visit(E), ConvertType(DestTy), "conv"); + return Builder.CreateFPToUI(Visit(E), ConvertType(DestTy), "conv"); + } + CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE); + return EmitScalarConversion(Visit(E), E->getType(), DestTy, + CE->getExprLoc()); + } + case CK_FloatingCast: { + if (E->getType()->isVectorType() && DestTy->isVectorType()) { + // TODO: Support constrained FP intrinsics. + assert(!Builder.getIsFPConstrained() && + "FP Constrained vector casts not supported yet."); + QualType SrcElTy = E->getType()->castAs()->getElementType(); + QualType DstElTy = DestTy->castAs()->getElementType(); + if (DstElTy->castAs()->getKind() < + SrcElTy->castAs()->getKind()) + return Builder.CreateFPTrunc(Visit(E), ConvertType(DestTy), "conv"); + return Builder.CreateFPExt(Visit(E), ConvertType(DestTy), "conv"); + } + CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE); + return EmitScalarConversion(Visit(E), E->getType(), DestTy, + CE->getExprLoc()); + } case CK_FixedPointToFloating: case CK_FloatingToFixedPoint: { CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE); @@ -2468,6 +2515,17 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { case CK_IntToOCLSampler: return CGF.CGM.createOpenCLIntToSamplerConversion(E, CGF); + case CK_HLSLVectorTruncation: { + assert(DestTy->isVectorType() && "Expected dest type to be vector type"); + Value *Vec = Visit(const_cast(E)); + SmallVector Mask; + unsigned NumElts = DestTy->castAs()->getNumElements(); + for (unsigned I = 0; I != NumElts; ++I) + Mask.push_back(I); + + return Builder.CreateShuffleVector(Vec, Mask, "trunc"); + } + } // end of switch llvm_unreachable("unknown scalar cast"); diff --git a/clang/lib/Edit/RewriteObjCFoundationAPI.cpp b/clang/lib/Edit/RewriteObjCFoundationAPI.cpp index 2e123e89b0036..22f2c47e1d6a1 100644 --- a/clang/lib/Edit/RewriteObjCFoundationAPI.cpp +++ b/clang/lib/Edit/RewriteObjCFoundationAPI.cpp @@ -1083,6 +1083,10 @@ static bool rewriteToNumericBoxedExpression(const ObjCMessageExpr *Msg, case CK_BooleanToSignedIntegral: llvm_unreachable("OpenCL-specific cast in Objective-C?"); + case CK_HLSLVectorTruncation: + llvm_unreachable("HLSL-specific cast in Objective-C?"); + break; + case CK_FloatingToFixedPoint: case CK_FixedPointToFloating: case CK_FixedPointCast: diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index afe2673479e40..8e76338477444 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -15676,11 +15676,18 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T, if (S.SourceMgr.isInSystemMacro(CC)) return; return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); + } else if (S.getLangOpts().HLSL && + Target->castAs()->getNumElements() < + Source->castAs()->getNumElements()) { + // Diagnose vector truncation but don't return. We may also want to + // diagnose an element conversion. + DiagnoseImpCast(S, E, T, CC, diag::warn_hlsl_impcast_vector_truncation); } // If the vector cast is cast between two vectors of the same size, it is - // a bitcast, not a conversion. - if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) + // a bitcast, not a conversion, except under HLSL where it is a conversion. + if (!S.getLangOpts().HLSL && + S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) return; Source = cast(Source)->getElementType().getTypePtr(); diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index f2b89135af21c..801ce7b35ac47 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -4762,6 +4762,22 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType, CK_ZeroToOCLOpaqueType, From->getValueKind()).get(); break; + case ICK_HLSL_Vector_Truncation: { + // Note: HLSL built-in vectors are ExtVectors. Since this truncates a vector + // to a smaller vector, this can only operate on arguments where the source + // and destination types are ExtVectors. + assert(From->getType()->isExtVectorType() && ToType->isExtVectorType() && + "HLSL vector truncation should only apply to ExtVectors"); + auto *FromVec = From->getType()->castAs(); + auto *ToVec = ToType->castAs(); + QualType ElType = FromVec->getElementType(); + QualType TruncTy = + Context.getExtVectorType(ElType, ToVec->getNumElements()); + From = ImpCastExprToType(From, TruncTy, CK_HLSLVectorTruncation, + From->getValueKind()) + .get(); + break; + } case ICK_Lvalue_To_Rvalue: case ICK_Array_To_Pointer: @@ -4774,6 +4790,76 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType, llvm_unreachable("Improper second standard conversion"); } + if (SCS.Element != ICK_Identity) { + // If SCS.Element is not ICK_Identity the To and From types must be HLSL + // vectors or matrices. + + // TODO: Support HLSL matrices. + assert((!From->getType()->isMatrixType() && !ToType->isMatrixType()) && + "Element conversion for matrix types is not implemented yet."); + assert(From->getType()->isVectorType() && ToType->isVectorType() && + "Element conversion is only supported for vector types."); + assert(From->getType()->getAs()->getNumElements() == + ToType->getAs()->getNumElements() && + "Element conversion is only supported for vectors with the same " + "element counts."); + QualType FromElTy = From->getType()->getAs()->getElementType(); + unsigned NumElts = ToType->getAs()->getNumElements(); + switch (SCS.Element) { + case ICK_Boolean_Conversion: + // Perform half-to-boolean conversion via float. + if (FromElTy->isHalfType()) { + QualType FPExtType = Context.getExtVectorType(FromElTy, NumElts); + From = ImpCastExprToType(From, FPExtType, CK_FloatingCast).get(); + FromType = FPExtType; + } + + From = + ImpCastExprToType(From, ToType, ScalarTypeToBooleanCastKind(FromElTy), + VK_PRValue, + /*BasePath=*/nullptr, CCK) + .get(); + break; + case ICK_Integral_Promotion: + case ICK_Integral_Conversion: + if (ToType->isBooleanType()) { + assert(FromType->castAs()->getDecl()->isFixed() && + SCS.Second == ICK_Integral_Promotion && + "only enums with fixed underlying type can promote to bool"); + From = ImpCastExprToType(From, ToType, CK_IntegralToBoolean, VK_PRValue, + /*BasePath=*/nullptr, CCK) + .get(); + } else { + From = ImpCastExprToType(From, ToType, CK_IntegralCast, VK_PRValue, + /*BasePath=*/nullptr, CCK) + .get(); + } + break; + + case ICK_Floating_Promotion: + case ICK_Floating_Conversion: + From = ImpCastExprToType(From, ToType, CK_FloatingCast, VK_PRValue, + /*BasePath=*/nullptr, CCK) + .get(); + break; + case ICK_Floating_Integral: + if (ToType->isRealFloatingType()) + From = + ImpCastExprToType(From, ToType, CK_IntegralToFloating, VK_PRValue, + /*BasePath=*/nullptr, CCK) + .get(); + else + From = + ImpCastExprToType(From, ToType, CK_FloatingToIntegral, VK_PRValue, + /*BasePath=*/nullptr, CCK) + .get(); + break; + case ICK_Identity: + default: + llvm_unreachable("Improper element standard conversion"); + } + } + switch (SCS.Third) { case ICK_Identity: // Nothing to do. diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index b6de06464cd6f..0fd458837163e 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -6432,7 +6432,7 @@ void InitializationSequence::InitializeFrom(Sema &S, // For HLSL ext vector types we allow list initialization behavior for C++ // constructor syntax. This is accomplished by converting initialization // arguments an InitListExpr late. - if (S.getLangOpts().HLSL && DestType->isExtVectorType() && + if (S.getLangOpts().HLSL && Args.size() > 1 && DestType->isExtVectorType() && (SourceType.isNull() || !Context.hasSameUnqualifiedType(SourceType, DestType))) { diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 5c6d463bb895d..f7645422348b6 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -159,6 +159,7 @@ ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) { ICR_C_Conversion, ICR_C_Conversion_Extension, ICR_Conversion, + ICR_Conversion, }; static_assert(std::size(Rank) == (int)ICK_Num_Conversion_Kinds); return Rank[(int)Kind]; @@ -199,6 +200,7 @@ static const char *GetImplicitConversionName(ImplicitConversionKind Kind) { "C specific type conversion", "Incompatible pointer conversion", "Fixed point conversion", + "HLSL vector truncation", }; static_assert(std::size(Name) == (int)ICK_Num_Conversion_Kinds); return Name[Kind]; @@ -209,6 +211,7 @@ static const char *GetImplicitConversionName(ImplicitConversionKind Kind) { void StandardConversionSequence::setAsIdentityConversion() { First = ICK_Identity; Second = ICK_Identity; + Element = ICK_Identity; Third = ICK_Identity; DeprecatedStringLiteralToCharPtr = false; QualificationIncludesObjCLifetime = false; @@ -227,11 +230,13 @@ void StandardConversionSequence::setAsIdentityConversion() { /// implicit conversions. ImplicitConversionRank StandardConversionSequence::getRank() const { ImplicitConversionRank Rank = ICR_Exact_Match; - if (GetConversionRank(First) > Rank) + if (GetConversionRank(First) > Rank) Rank = GetConversionRank(First); - if (GetConversionRank(Second) > Rank) + if (GetConversionRank(Second) > Rank) Rank = GetConversionRank(Second); - if (GetConversionRank(Third) > Rank) + if (GetConversionRank(Element) > Rank) + Rank = GetConversionRank(Element); + if (GetConversionRank(Third) > Rank) Rank = GetConversionRank(Third); return Rank; } @@ -1828,13 +1833,86 @@ bool Sema::IsFunctionConversion(QualType FromType, QualType ToType, return true; } +/// Determine whether the conversion from FromType to ToType is a valid +/// floating point conversion. +/// +static bool IsFloatingPointConversion(Sema &S, QualType FromType, + QualType ToType) { + if (!FromType->isRealFloatingType() || !ToType->isRealFloatingType()) + return false; + // FIXME: disable conversions between long double, __ibm128 and __float128 + // if their representation is different until there is back end support + // We of course allow this conversion if long double is really double. + + // Conversions between bfloat16 and float16 are currently not supported. + if ((FromType->isBFloat16Type() && + (ToType->isFloat16Type() || ToType->isHalfType())) || + (ToType->isBFloat16Type() && + (FromType->isFloat16Type() || FromType->isHalfType()))) + return false; + + // Conversions between IEEE-quad and IBM-extended semantics are not + // permitted. + const llvm::fltSemantics &FromSem = S.Context.getFloatTypeSemantics(FromType); + const llvm::fltSemantics &ToSem = S.Context.getFloatTypeSemantics(ToType); + if ((&FromSem == &llvm::APFloat::PPCDoubleDouble() && + &ToSem == &llvm::APFloat::IEEEquad()) || + (&FromSem == &llvm::APFloat::IEEEquad() && + &ToSem == &llvm::APFloat::PPCDoubleDouble())) + return false; + return true; +} + +static bool IsVectorElementConversion(Sema &S, QualType FromType, + QualType ToType, + ImplicitConversionKind &ICK, Expr *From) { + if (S.Context.hasSameUnqualifiedType(FromType, ToType)) + return true; + + if (S.IsFloatingPointPromotion(FromType, ToType)) { + ICK = ICK_Floating_Promotion; + return true; + } + + if (IsFloatingPointConversion(S, FromType, ToType)) { + ICK = ICK_Floating_Conversion; + return true; + } + + if (ToType->isBooleanType() && FromType->isArithmeticType()) { + ICK = ICK_Boolean_Conversion; + return true; + } + + if (S.IsIntegralPromotion(From, FromType, ToType)) { + ICK = ICK_Integral_Promotion; + return true; + } + + if (FromType->isIntegralOrUnscopedEnumerationType() && + ToType->isIntegralType(S.Context)) { + ICK = ICK_Integral_Conversion; + return true; + } + + if ((FromType->isRealFloatingType() && ToType->isIntegralType(S.Context)) || + (FromType->isIntegralOrUnscopedEnumerationType() && + ToType->isRealFloatingType())) { + ICK = ICK_Floating_Integral; + return true; + } + + return false; +} + /// Determine whether the conversion from FromType to ToType is a valid /// vector conversion. /// /// \param ICK Will be set to the vector conversion kind, if this is a vector /// conversion. static bool IsVectorConversion(Sema &S, QualType FromType, QualType ToType, - ImplicitConversionKind &ICK, Expr *From, + ImplicitConversionKind &ICK, + ImplicitConversionKind &ElConv, Expr *From, bool InOverloadResolution, bool CStyle) { // We need at least one of these types to be a vector type to have a vector // conversion. @@ -1847,10 +1925,28 @@ static bool IsVectorConversion(Sema &S, QualType FromType, QualType ToType, // There are no conversions between extended vector types, only identity. if (ToType->isExtVectorType()) { - // There are no conversions between extended vector types other than the - // identity conversion. - if (FromType->isExtVectorType()) + if (FromType->isExtVectorType()) { + // HLSL allows implicit truncation of vector types. + if (S.getLangOpts().HLSL) { + unsigned FromElts = FromType->getAs()->getNumElements(); + unsigned ToElts = ToType->getAs()->getNumElements(); + if (FromElts < ToElts) + return false; + if (FromElts == ToElts) + ICK = ICK_Identity; + else + ICK = ICK_HLSL_Vector_Truncation; + + QualType FromElTy = FromType->getAs()->getElementType(); + QualType ToElTy = ToType->getAs()->getElementType(); + if (S.Context.hasSameUnqualifiedType(FromElTy, ToElTy)) + return true; + return IsVectorElementConversion(S, FromElTy, ToElTy, ElConv, From); + } + // There are no conversions between extended vector types other than the + // identity conversion. return false; + } // Vector splat from any arithmetic type to a vector. if (FromType->isArithmeticType()) { @@ -2056,6 +2152,7 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, // conversion. bool IncompatibleObjC = false; ImplicitConversionKind SecondICK = ICK_Identity; + ImplicitConversionKind ElementICK = ICK_Identity; if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { // The unqualified versions of the types are the same: there's no // conversion to do. @@ -2094,29 +2191,7 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, // Complex-real conversions (C99 6.3.1.7) SCS.Second = ICK_Complex_Real; FromType = ToType.getUnqualifiedType(); - } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { - // FIXME: disable conversions between long double, __ibm128 and __float128 - // if their representation is different until there is back end support - // We of course allow this conversion if long double is really double. - - // Conversions between bfloat16 and float16 are currently not supported. - if ((FromType->isBFloat16Type() && - (ToType->isFloat16Type() || ToType->isHalfType())) || - (ToType->isBFloat16Type() && - (FromType->isFloat16Type() || FromType->isHalfType()))) - return false; - - // Conversions between IEEE-quad and IBM-extended semantics are not - // permitted. - const llvm::fltSemantics &FromSem = - S.Context.getFloatTypeSemantics(FromType); - const llvm::fltSemantics &ToSem = S.Context.getFloatTypeSemantics(ToType); - if ((&FromSem == &llvm::APFloat::PPCDoubleDouble() && - &ToSem == &llvm::APFloat::IEEEquad()) || - (&FromSem == &llvm::APFloat::IEEEquad() && - &ToSem == &llvm::APFloat::PPCDoubleDouble())) - return false; - + } else if (IsFloatingPointConversion(S, FromType, ToType)) { // Floating point conversions (C++ 4.8). SCS.Second = ICK_Floating_Conversion; FromType = ToType.getUnqualifiedType(); @@ -2143,18 +2218,18 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, InOverloadResolution, FromType)) { // Pointer to member conversions (4.11). SCS.Second = ICK_Pointer_Member; - } else if (IsVectorConversion(S, FromType, ToType, SecondICK, From, - InOverloadResolution, CStyle)) { + } else if (IsVectorConversion(S, FromType, ToType, SecondICK, ElementICK, + From, InOverloadResolution, CStyle)) { SCS.Second = SecondICK; + SCS.Element = ElementICK; FromType = ToType.getUnqualifiedType(); } else if (!S.getLangOpts().CPlusPlus && S.Context.typesAreCompatible(ToType, FromType)) { // Compatible conversions (Clang extension for C function overloading) SCS.Second = ICK_Compatible_Conversion; FromType = ToType.getUnqualifiedType(); - } else if (IsTransparentUnionStandardConversion(S, From, ToType, - InOverloadResolution, - SCS, CStyle)) { + } else if (IsTransparentUnionStandardConversion( + S, From, ToType, InOverloadResolution, SCS, CStyle)) { SCS.Second = ICK_TransparentUnionConversion; FromType = ToType; } else if (tryAtomicConversion(S, From, ToType, InOverloadResolution, SCS, @@ -2449,6 +2524,11 @@ bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { return true; } + // In HLSL an rvalue of integral type can be promoted to an rvalue of a larger + // integral type. + if (Context.getLangOpts().HLSL) + return Context.getTypeSize(FromType) < Context.getTypeSize(ToType); + return false; } @@ -5042,6 +5122,7 @@ TryReferenceInit(Sema &S, Expr *Init, QualType DeclType, : (RefConv & Sema::ReferenceConversions::ObjC) ? ICK_Compatible_Conversion : ICK_Identity; + ICS.Standard.Element = ICK_Identity; // FIXME: As a speculative fix to a defect introduced by CWG2352, we rank // a reference binding that performs a non-top-level qualification // conversion as a qualification conversion, not as an identity conversion. @@ -5968,6 +6049,7 @@ static bool CheckConvertedConstantConversions(Sema &S, case ICK_C_Only_Conversion: case ICK_Incompatible_Pointer_Conversion: case ICK_Fixed_Point_Conversion: + case ICK_HLSL_Vector_Truncation: return false; case ICK_Lvalue_To_Rvalue: @@ -6245,6 +6327,7 @@ Sema::EvaluateConvertedConstantExpression(Expr *E, QualType T, APValue &Value, static void dropPointerConversion(StandardConversionSequence &SCS) { if (SCS.Second == ICK_Pointer_Conversion) { SCS.Second = ICK_Identity; + SCS.Element = ICK_Identity; SCS.Third = ICK_Identity; SCS.ToTypePtrs[2] = SCS.ToTypePtrs[1] = SCS.ToTypePtrs[0]; } diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp index 7e431f7e598c4..c3fc56ac30ee9 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp @@ -520,7 +520,8 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex, // Various C++ casts that are not handled yet. case CK_ToUnion: case CK_MatrixCast: - case CK_VectorSplat: { + case CK_VectorSplat: + case CK_HLSLVectorTruncation: { QualType resultType = CastE->getType(); if (CastE->isGLValue()) resultType = getContext().getPointerType(resultType); diff --git a/clang/test/CodeGenHLSL/BasicFeatures/standard_conversion_sequences.hlsl b/clang/test/CodeGenHLSL/BasicFeatures/standard_conversion_sequences.hlsl new file mode 100644 index 0000000000000..06e3cc5af87e1 --- /dev/null +++ b/clang/test/CodeGenHLSL/BasicFeatures/standard_conversion_sequences.hlsl @@ -0,0 +1,119 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s + +// CHECK-LABEL: f3_to_d4 +// CHECK: [[f3:%.*]] = alloca <3 x float> +// CHECK: [[d4:%.*]] = alloca <4 x double> +// CHECK: store <3 x float> , ptr [[f3]] +// CHECK: [[vecf3:%.*]] = load <3 x float>, ptr [[f3]] +// CHECK: [[vecf4:%.*]] = shufflevector <3 x float> [[vecf3]], <3 x float> poison, <4 x i32> +// CHECK: [[vecd4:%.*]] = fpext <4 x float> [[vecf4]] to <4 x double> +// CHECK: store <4 x double> [[vecd4]], ptr [[d4]] +void f3_to_d4() { + vector f3 = 1.0; + vector d4 = f3.xyzx; +} + +// CHECK-LABEL: f3_to_f2 +// CHECK: [[f3:%.*]] = alloca <3 x float> +// CHECK: [[f2:%.*]] = alloca <2 x float> +// CHECK: store <3 x float> , ptr [[f3]] +// CHECK: [[vecf3:%.*]] = load <3 x float>, ptr [[f3]] +// CHECK: [[vecf2:%.*]] = shufflevector <3 x float> [[vecf3]], <3 x float> poison, <2 x i32> +// CHECK: store <2 x float> [[vecf2]], ptr [[f2]] +void f3_to_f2() { + vector f3 = 2.0; + vector f2 = f3; +} + +// CHECK-LABEL: d4_to_f2 +// CHECK: [[d4:%.*]] = alloca <4 x double> +// CHECK: [[f2:%.*]] = alloca <2 x float> +// CHECK: store <4 x double> , ptr [[d4]] +// CHECK: [[vecd4:%.*]] = load <4 x double>, ptr [[d4]] +// CHECK: [[vecd2:%.*]] = shufflevector <4 x double> [[vecd4]], <4 x double> poison, <2 x i32> +// CHECK: [[vecf2:%.*]] = fptrunc <2 x double> [[vecd2]] to <2 x float> +// CHECK: store <2 x float> [[vecf2]], ptr [[f2]] +void d4_to_f2() { + vector d4 = 3.0; + vector f2 = d4; +} + +// CHECK-LABEL: f2_to_i2 +// CHECK: [[f2:%.*]] = alloca <2 x float> +// CHECK: [[i2:%.*]] = alloca <2 x i32> +// CHECK: store <2 x float> , ptr [[f2]] +// CHECK: [[vecf2:%.*]] = load <2 x float>, ptr [[f2]] +// CHECK: [[veci2:%.*]] = fptosi <2 x float> [[vecf2]] to <2 x i32> +// CHECK: store <2 x i32> [[veci2]], ptr [[i2]] +void f2_to_i2() { + vector f2 = 4.0; + vector i2 = f2; +} + +// CHECK-LABEL: d4_to_i2 +// CHECK: [[f4:%.*]] = alloca <4 x double> +// CHECK: [[i2:%.*]] = alloca <2 x i32> +// CHECK: store <4 x double> , ptr [[d4]] +// CHECK: [[vecd4:%.*]] = load <4 x double>, ptr [[d4]] +// CHECK: [[vecd2:%.*]] = shufflevector <4 x double> [[vecd4]], <4 x double> poison, <2 x i32> +// CHECK: [[veci2]] = fptosi <2 x double> [[vecd2]] to <2 x i32> +// CHECK: store <2 x i32> [[veci2]], ptr [[i2]] +void d4_to_i2() { + vector d4 = 5.0; + vector i2 = d4; +} + +// CHECK-LABEL: d4_to_l4 +// CHECK: [[d4:%.*]] = alloca <4 x double> +// CHECK: [[l4:%.*]] = alloca <4 x i64> +// CHECK: store <4 x double> , ptr [[d4]] +// CHECK: [[vecd4:%.*]] = load <4 x double>, ptr [[d4]] +// CHECK: [[vecl4:%.*]] = fptosi <4 x double> [[vecd4]] to <4 x i64> +// CHECK: store <4 x i64> [[vecl4]], ptr [[l4]] +void d4_to_l4() { + vector d4 = 6.0; + vector l4 = d4; +} + + +// CHECK-LABEL: l4_to_i2 +// CHECK: [[l4:%.*]] = alloca <4 x i64> +// CHECK: [[i2:%.*]] = alloca <2 x i32> +// CHECK: store <4 x i64> , ptr [[l4]] +// CHECK: [[vecl4:%.*]] = load <4 x i64>, ptr [[l4]] +// CHECK: [[vecl2:%.*]] = shufflevector <4 x i64> [[vecl4]], <4 x i64> poison, <2 x i32> +// CHECK: [[veci2:%.*]] = trunc <2 x i64> [[vecl2]] to <2 x i32> +// CHECK: store <2 x i32> [[veci2]], ptr [[i2]] +void l4_to_i2() { + vector l4 = 7; + vector i2 = l4; +} + +// CHECK-LABEL: i2_to_b2 +// CHECK: [[l2:%.*]] = alloca <2 x i32> +// CHECK: [[b2:%.*]] = alloca i8 +// CHECK: store <2 x i32> , ptr [[i2]] +// CHECK: [[veci2:%.*]] = load <2 x i32>, ptr [[i2]] +// CHECK: [[vecb2:%.*]] = icmp ne <2 x i32> [[veci2]], zeroinitializer +// CHECK: [[vecb8:%.*]] = shufflevector <2 x i1> [[vecb2]], <2 x i1> poison, <8 x i32> +// CHECK: [[i8:%.*]] = bitcast <8 x i1> [[vecb8]] to i8 +// CHECK: store i8 [[i8]], ptr [[b2]] +void i2_to_b2() { + vector i2 = 8; + vector b2 = i2; +} + +// CHECK-LABEL: d4_to_b2 +// CHECK: [[d4:%.*]] = alloca <4 x double> +// CHECK: [[b2:%.*]] = alloca i8 +// CHECK: store <4 x double> , ptr [[d4]] +// CHECK: [[vecd4:%.*]] = load <4 x double>, ptr [[d4]] +// CHECK: [[vecd2:%.*]] = shufflevector <4 x double> [[vecd4]], <4 x double> poison, <2 x i32> +// CHECK: [[vecb2:%.*]] = fcmp une <2 x double> [[vecd2]], zeroinitializer +// CHECK: [[vecb8:%.*]] = shufflevector <2 x i1> [[vecb2]], <2 x i1> poison, <8 x i32> +// CHECK: [[i8:%.*]] = bitcast <8 x i1> [[vecb8]] to i8 +// CHECK: store i8 [[i8]], ptr [[b2]] +void d4_to_b2() { + vector d4 = 9.0; + vector b2 = d4; +} diff --git a/clang/test/CodeGenHLSL/builtins/ScalarSwizzles.hlsl b/clang/test/CodeGenHLSL/builtins/ScalarSwizzles.hlsl index d560d4b945fea..36f71f6860c06 100644 --- a/clang/test/CodeGenHLSL/builtins/ScalarSwizzles.hlsl +++ b/clang/test/CodeGenHLSL/builtins/ScalarSwizzles.hlsl @@ -85,7 +85,9 @@ float4 FillTwoPointFiveFloat(){ // CHECK: [[vec1Ptr:%.*]] = alloca <1 x float>, align 4 // CHECK: store <1 x float> , ptr [[vec1Ptr]], align 4 // CHECK: [[vec1:%.*]] = load <1 x float>, ptr [[vec1Ptr]], align 4 -// CHECK: [[vec1Ret:%.*]] = shufflevector <1 x float> [[vec1]], <1 x float> poison, <1 x i32> zeroinitializer +// CHECK: [[el0:%.*]] = extractelement <1 x float> [[vec1]], i32 0 +// CHECK: [[vec1Splat:%.*]] = insertelement <1 x float> poison, float [[el0]], i64 0 +// CHECK: [[vec1Ret:%.*]] = shufflevector <1 x float> [[vec1Splat]], <1 x float> poison, <1 x i32> zeroinitializer // CHECK: ret <1 x float> [[vec1Ret]] vector FillOneHalfFloat(){ return .5f.r; @@ -113,32 +115,13 @@ float2 HowManyFloats(float V) { // up nicely too. // CHECK-LABEL: AllRighty -// CHECK: [[XTmp:%.*]] = alloca <1 x double>, align 8 -// CHECK: [[YTmp:%.*]] = alloca <1 x double>, align 8 -// CHECK: [[ZTmp:%.*]] = alloca <1 x double>, align 8 - -// CHECK: store <1 x double> , ptr [[XTmp]], align 8 -// CHECK: [[XVec:%.*]] = load <1 x double>, ptr [[XTmp]], align 8 -// CHECK: [[XVec3:%.*]] = shufflevector <1 x double> [[XVec]], <1 x double> poison, <3 x i32> zeroinitializer -// CHECK: [[XVal:%.*]] = extractelement <3 x double> [[XVec3]], i32 0 -// CHECK: [[XValF:%.*]] = fptrunc double [[XVal]] to float -// CHECK: [[Vec3F1:%.*]] = insertelement <3 x float> poison, float [[XValF]], i32 0 - -// CHECK: store <1 x double> , ptr [[YTmp]], align 8 -// CHECK: [[YVec:%.*]] = load <1 x double>, ptr [[YTmp]], align 8 -// CHECK: [[YVec3:%.*]] = shufflevector <1 x double> [[YVec]], <1 x double> poison, <3 x i32> zeroinitializer -// CHECK: [[YVal:%.*]] = extractelement <3 x double> [[YVec3]], i32 1 -// CHECK: [[YValF:%.*]] = fptrunc double [[YVal]] to float -// CHECK: [[Vec3F2:%.*]] = insertelement <3 x float> [[Vec3F1]], float [[YValF]], i32 1 - -// CHECK: store <1 x double> , ptr [[ZTmp]], align 8 -// CHECK: [[ZVec:%.*]] = load <1 x double>, ptr [[ZTmp]], align 8 -// CHECK: [[ZVec3:%.*]] = shufflevector <1 x double> [[ZVec]], <1 x double> poison, <3 x i32> zeroinitializer -// CHECK: [[ZVal:%.*]] = extractelement <3 x double> [[ZVec3]], i32 2 -// CHECK: [[ZValF:%.*]] = fptrunc double [[ZVal]] to float -// CHECK: [[Vec3F3:%.*]] = insertelement <3 x float> [[Vec3F2]], float [[ZValF]], i32 2 - -// ret <3 x float> [[Vec3F3]] +// CHECK: [[Tmp:%.*]] = alloca <1 x double>, align 8 +// CHECK: store <1 x double> , ptr [[Tmp]], align 8 +// CHECK: [[vec1:%.*]] = load <1 x double>, ptr [[Tmp]], align 8 +// CHECK: [[vec3:%.*]] = shufflevector <1 x double> [[vec1]], <1 x double> poison, <3 x i32> zeroinitializer +// CHECK: [[vec3f:%.*]] = fptrunc <3 x double> [[vec3]] to <3 x float> +// CHECK: ret <3 x float> [[vec3f]] + float3 AllRighty() { return 1..rrr; } diff --git a/clang/test/CodeGenHLSL/builtins/sqrt.hlsl b/clang/test/CodeGenHLSL/builtins/sqrt.hlsl index 776269395a662..2c2a09617cf86 100644 --- a/clang/test/CodeGenHLSL/builtins/sqrt.hlsl +++ b/clang/test/CodeGenHLSL/builtins/sqrt.hlsl @@ -1,9 +1,6 @@ // RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \ // RUN: dxil-pc-shadermodel6.2-library %s -fnative-half-type \ // RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s -// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \ -// RUN: dxil-pc-shadermodel6.2-library %s -emit-llvm -disable-llvm-passes \ -// RUN: -o - | FileCheck %s --check-prefix=NO_HALF using hlsl::sqrt; @@ -30,5 +27,3 @@ half sqrt_h(half x) // CHECK: define noundef half @"?sqrt_h@@YA$f16@$f16@@Z"( // CHECK: call half @llvm.sqrt.f16(half %0) -// NO_HALF: define noundef float @"?sqrt_h@@YA$halff@$halff@@Z"( -// NO_HALF: call float @llvm.sqrt.f32(float %0) diff --git a/clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl b/clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl index 73b5a192793e0..7f6bdc7e67836 100644 --- a/clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl +++ b/clang/test/SemaHLSL/BuiltIns/vector-constructors-erros.hlsl @@ -18,5 +18,5 @@ void entry() { S s; float2 GettingStrange = float2(s, s); // expected-error{{no viable conversion from 'S' to 'float'}} expected-error{{no viable conversion from 'S' to 'float'}} S2 s2; - float2 EvenStranger = float2(s2); // expected-error{{no viable conversion from 'S2' to 'float'}} expected-error{{too few elements in vector initialization (expected 2 elements, have 1)}} + float2 EvenStranger = float2(s2); // expected-error{{cannot convert 'S2' to 'float2' (vector of 2 'float' values) without a conversion operator}} } diff --git a/clang/test/SemaHLSL/OverloadResolutionBugs.hlsl b/clang/test/SemaHLSL/OverloadResolutionBugs.hlsl new file mode 100644 index 0000000000000..135d6cf335c13 --- /dev/null +++ b/clang/test/SemaHLSL/OverloadResolutionBugs.hlsl @@ -0,0 +1,39 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -o - -fsyntax-only %s -verify +// XFAIL: * + +// https://github.com/llvm/llvm-project/issues/81047 + +// expected-no-diagnostics +void Fn3(double2 D); +void Fn3(float2 F); + +void Call3(half2 H) { + Fn3(H); +} + +void Fn5(double2 D); + +void Call5(half2 H) { + Fn5(H); +} + +void Fn4(int64_t2 L); +void Fn4(int2 I); + +void Call4(int16_t H) { + Fn4(H); +} + +// https://github.com/llvm/llvm-project/issues/81049 + +// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \ +// RUN: dxil-pc-shadermodel6.2-library %s -emit-llvm -disable-llvm-passes \ +// RUN: -o - | FileCheck %s --check-prefix=NO_HALF + +half sqrt_h(half x) +{ + return sqrt(x); +} + +// NO_HALF: define noundef float @"?sqrt_h@@YA$halff@$halff@@Z"( +// NO_HALF: call float @llvm.sqrt.f32(float %0) diff --git a/clang/test/SemaHLSL/Types/BuiltinVector/ScalarSwizzles.hlsl b/clang/test/SemaHLSL/Types/BuiltinVector/ScalarSwizzles.hlsl index 4ffbcacf3f30c..a2e9a5f865ece 100644 --- a/clang/test/SemaHLSL/Types/BuiltinVector/ScalarSwizzles.hlsl +++ b/clang/test/SemaHLSL/Types/BuiltinVector/ScalarSwizzles.hlsl @@ -80,7 +80,7 @@ float4 FillTwoPointFiveFloat(){ // initialze the returned vector. // CHECK-LABEL: FillOneHalfFloat -// CHECK: InitListExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(1)))' +// CHECK: ImplicitCastExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(1)))' // CHECK-NEXT: ExtVectorElementExpr {{.*}} 'float' r // CHECK-NEXT: ImplicitCastExpr {{.*}} 'float __attribute__((ext_vector_type(1)))' // CHECK-NEXT: FloatingLiteral {{.*}} 'float' 5.000000e-01 @@ -113,31 +113,10 @@ int64_t4 HooBoy() { // list with float truncation casts. // CHECK-LABEL: AllRighty -// CHECK: InitListExpr {{.*}} 'float3':'float __attribute__((ext_vector_type(3)))' - -// Vector element 0: -// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float' -// CHECK-NEXT: ArraySubscriptExpr {{.*}} 'double' -// CHECK-NEXT: ExtVectorElementExpr {{.*}} 'double __attribute__((ext_vector_type(3)))' rrr -// CHECK-NEXT: ImplicitCastExpr {{.*}} 'double __attribute__((ext_vector_type(1)))' -// CHECK-NEXT: FloatingLiteral {{.*}} 'double' 1.000000e+00 -// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 0 - -// Vector element 1: -// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float' -// CHECK-NEXT: ArraySubscriptExpr {{.*}} 'double' -// CHECK-NEXT: ExtVectorElementExpr {{.*}} 'double __attribute__((ext_vector_type(3)))' rrr -// CHECK-NEXT: ImplicitCastExpr {{.*}} 'double __attribute__((ext_vector_type(1)))' -// CHECK-NEXT: FloatingLiteral {{.*}} 'double' 1.000000e+00 -// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 1 - -// Vector element 2: -// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float' -// CHECK-NEXT: ArraySubscriptExpr {{.*}} 'double' +// CHECK: ImplicitCastExpr {{.*}} 'float3':'float __attribute__((ext_vector_type(3)))' // CHECK-NEXT: ExtVectorElementExpr {{.*}} 'double __attribute__((ext_vector_type(3)))' rrr // CHECK-NEXT: ImplicitCastExpr {{.*}} 'double __attribute__((ext_vector_type(1)))' // CHECK-NEXT: FloatingLiteral {{.*}} 'double' 1.000000e+00 -// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 2 float3 AllRighty() { return 1..rrr; diff --git a/clang/test/SemaHLSL/VectorOverloadResolution.hlsl b/clang/test/SemaHLSL/VectorOverloadResolution.hlsl new file mode 100644 index 0000000000000..e07391f803f89 --- /dev/null +++ b/clang/test/SemaHLSL/VectorOverloadResolution.hlsl @@ -0,0 +1,30 @@ +// RUN: %clang_cc1 -triple dxil-unknown-shadermodel6.6-library -S -fnative-half-type -finclude-default-header -o - -ast-dump %s | FileCheck %s +void Fn(double2 D); +void Fn(half2 H); + +// CHECK: FunctionDecl {{.*}} Call 'void (float2)' +// CHECK: CallExpr {{.*}}'void' +// CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(double2)' +// CHECK-NEXT: DeclRefExpr {{.*}}'void (double2)' lvalue Function {{.*}} 'Fn' 'void (double2)' +// CHECK-NEXT: ImplicitCastExpr {{.*}} 'double2':'double __attribute__((ext_vector_type(2)))' +// CHECK-NEXT: ImplicitCastExpr {{.*}} 'float2':'float __attribute__((ext_vector_type(2)))' +// CHECK-NEXT: DeclRefExpr {{.*}} 'float2':'float __attribute__((ext_vector_type(2)))' lvalue ParmVar {{.*}} 'F' 'float2':'float __attribute__((ext_vector_type(2)))' + +void Call(float2 F) { + Fn(F); +} + +void Fn2(int64_t2 L); +void Fn2(int16_t2 S); + +// CHECK: FunctionDecl {{.*}} Call2 'void (int2)' +// CHECK: CallExpr {{.*}} 'void' +// CHECK-NEXT: ImplicitCastExpr {{.*}} 'void (*)(int64_t2)' +// CHECK-NEXT: DeclRefExpr {{.*}} 'void (int64_t2)' lvalue Function {{.*}} 'Fn2' 'void (int64_t2)' +// CHECK-NEXT: ImplicitCastExpr {{.*}} 'int64_t2':'long __attribute__((ext_vector_type(2)))' +// CHECK-NEXT: ImplicitCastExpr {{.*}} 'int2':'int __attribute__((ext_vector_type(2)))' +// CHECK-NEXT: DeclRefExpr {{.*}} 'int2':'int __attribute__((ext_vector_type(2)))' lvalue ParmVar {{.*}} 'I' 'int2':'int __attribute__((ext_vector_type(2)))' + +void Call2(int2 I) { + Fn2(I); +} diff --git a/clang/test/SemaHLSL/standard_conversion_sequences.hlsl b/clang/test/SemaHLSL/standard_conversion_sequences.hlsl new file mode 100644 index 0000000000000..a0d398105f15d --- /dev/null +++ b/clang/test/SemaHLSL/standard_conversion_sequences.hlsl @@ -0,0 +1,92 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -Wconversion -verify -o - %s +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -Wno-conversion -DNO_ERR -ast-dump %s | FileCheck %s + +void test() { + + // CHECK: VarDecl {{.*}} used f3 'vector':'float __attribute__((ext_vector_type(3)))' cinit + // CHECK-NEXt: ImplicitCastExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(3)))' + // CHECK-NEXt: ImplicitCastExpr {{.*}} 'float' + // CHECK-NEXt: FloatingLiteral {{.*}} 'double' 1.000000e+00 + vector f3 = 1.0; // No warning for splatting to a vector from a literal. + + + // CHECK: VarDecl {{.*}} used d4 'vector':'double __attribute__((ext_vector_type(4)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' + // CHECK-NEXT: ExtVectorElementExpr {{.*}} 'float __attribute__((ext_vector_type(4)))' xyzx + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(3)))' lvalue Var {{.*}} 'f3' 'vector':'float __attribute__((ext_vector_type(3)))' + vector d4 = f3.xyzx; // No warnings for promotion or explicit extension. + + // CHECK: VarDecl {{.*}} used f2 'vector':'float __attribute__((ext_vector_type(2)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'float __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(3)))' + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(3)))' lvalue Var {{.*}} 'f3' 'vector':'float __attribute__((ext_vector_type(3)))' + // expected-warning@#f2{{implicit conversion truncates vector: 'vector' (vector of 3 'float' values) to 'float __attribute__((ext_vector_type(2)))' (vector of 2 'float' values)}} + vector f2 = f3; // #f2 + + // CHECK: VarDecl {{.*}} f2_2 'vector':'float __attribute__((ext_vector_type(2)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'double __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' lvalue Var {{.*}} 'd4' 'vector':'double __attribute__((ext_vector_type(4)))' + // expected-warning@#f2_2{{implicit conversion truncates vector: 'vector' (vector of 4 'double' values) to 'vector' (vector of 2 'float' values)}} + // expected-warning@#f2_2{{implicit conversion loses floating-point precision: 'vector' (vector of 4 'double' values) to 'vector' (vector of 2 'float' values)}} + vector f2_2 = d4; // #f2_2 + + // CHECK: VarDecl {{.*}} i2 'vector':'int __attribute__((ext_vector_type(2)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'int __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'float __attribute__((ext_vector_type(2)))' lvalue Var {{.*}} 'f2' 'vector':'float __attribute__((ext_vector_type(2)))' + // expected-warning@#i2{{mplicit conversion turns floating-point number into integer: 'vector' (vector of 2 'float' values) to 'vector' (vector of 2 'int' values)}} + vector i2 = f2; // #i2 + + // CHECK: VarDecl {{.*}} i2_2 'vector':'int __attribute__((ext_vector_type(2)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'int __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'double __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' lvalue Var {{.*}} 'd4' 'vector':'double __attribute__((ext_vector_type(4)))' + // expected-warning@#i2_2{{implicit conversion truncates vector: 'vector' (vector of 4 'double' values) to 'vector' (vector of 2 'int' values)}} + // expected-warning@#i2_2{{implicit conversion turns floating-point number into integer: 'vector' (vector of 4 'double' values) to 'vector' (vector of 2 'int' values)}} + vector i2_2 = d4; // #i2_2 + + + // CHECK: VarDecl {{.*}} used i64_4 'vector':'long __attribute__((ext_vector_type(4)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'long __attribute__((ext_vector_type(4)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' lvalue Var {{.*}} 'd4' 'vector':'double __attribute__((ext_vector_type(4)))' + // expected-warning@#i64_4{{implicit conversion turns floating-point number into integer: 'vector' (vector of 4 'double' values) to 'vector' (vector of 4 'long' values)}} + vector i64_4 = d4; // #i64_4 + + // CHECK: VarDecl {{.*}} used i2_3 'vector':'int __attribute__((ext_vector_type(2)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'int __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'long __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'long __attribute__((ext_vector_type(4)))' + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'long __attribute__((ext_vector_type(4)))' lvalue Var {{.*}} 'i64_4' 'vector':'long __attribute__((ext_vector_type(4)))' + // expected-warning@#i2_3{{implicit conversion loses integer precision: 'vector' (vector of 4 'long' values) to 'vector' (vector of 2 'int' values)}} + // expected-warning@#i2_3{{implicit conversion truncates vector: 'vector' (vector of 4 'long' values) to 'vector' (vector of 2 'int' values)}} + vector i2_3 = i64_4; // #i2_3 + + //CHECK: VarDecl {{.*}} b2 'vector':'bool __attribute__((ext_vector_type(2)))' cinit + //CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'bool __attribute__((ext_vector_type(2)))' + //CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'int __attribute__((ext_vector_type(2)))' + //CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'int __attribute__((ext_vector_type(2)))' lvalue Var {{.*}} 'i2_3' 'vector':'int __attribute__((ext_vector_type(2)))' + vector b2 = i2_3; // No warning for integer to bool conversion. + + // CHECK: VarDecl {{.*}} b2_2 'vector':'bool __attribute__((ext_vector_type(2)))' cinit + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'bool __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'double __attribute__((ext_vector_type(2)))' + // CHECK-NEXT: ImplicitCastExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' + // CHECK-NEXT: DeclRefExpr {{.*}} 'vector':'double __attribute__((ext_vector_type(4)))' lvalue Var {{.*}} 'd4' 'vector':'double __attribute__((ext_vector_type(4)))' + // expected-warning@#b2_2{{implicit conversion truncates vector: 'vector' (vector of 4 'double' values) to 'vector' (vector of 2 'bool' values)}} + // expected-warning@#b2_2{{implicit conversion turns floating-point number into integer: 'vector' (vector of 4 'double' values) to 'vector' (vector of 2 'bool' values)}} + vector b2_2 = d4; // #b2_2 +} + +#ifndef NO_ERR + +void illegal() { + // vector extension is illegal + vector f3 = 1.0; + vector f4 = f3; // expected-error{{cannot initialize a variable of type 'vector<[...], 4>' with an lvalue of type 'vector<[...], 3>'}} +} + +#endif