diff options
author | Erich Keane <erich.keane@intel.com> | 2019-12-05 06:17:39 -0800 |
---|---|---|
committer | Erich Keane <erich.keane@intel.com> | 2020-01-13 13:27:20 -0800 |
commit | 349636d2bfc39a5c81a835a95d203a42d9f9301a (patch) | |
tree | c759730bf327f0a94c57cca11db5870a5eaf7a09 /clang/lib | |
parent | 4268e4f4b84b85266426e99050d31ec63f3ce8aa (diff) | |
download | bcm5719-llvm-349636d2bfc39a5c81a835a95d203a42d9f9301a.tar.gz bcm5719-llvm-349636d2bfc39a5c81a835a95d203a42d9f9301a.zip |
Implement VectorType conditional operator GNU extension.
GCC supports the conditional operator on VectorTypes that acts as a
'select' in C++ mode. This patch implements the support. Types are
converted as closely to GCC's behavior as possible, though in a few
places consistency with our existing vector type support was preferred.
Note that this implementation is different from the OpenCL version in a
number of ways, so it unfortunately required a different implementation.
First, the SEMA rules and promotion rules are significantly different.
Secondly, GCC implements COND[i] != 0 ? LHS[i] : RHS[i] (where i is in
the range 0- VectorSize, for each element). In OpenCL, the condition is
COND[i] < 0 ? LHS[i]: RHS[i].
In the process of implementing this, it was also required to make the
expression COND ? LHS : RHS type dependent if COND is type dependent,
since the type is now dependent on the condition. For example:
T ? 1 : 2;
Is not typically type dependent, since the result can be deduced from
the operands. HOWEVER, if T is a VectorType now, it could change this
to a 'select' (basically a swizzle with a non-constant mask) with the 1
and 2 being promoted to vectors themselves.
While this is a change, it is NOT a standards incompatible change. Based
on my (and D. Gregor's, at the time of writing the code) reading of the
standard, the expression is supposed to be type dependent if ANY
sub-expression is type dependent.
Differential Revision: https://reviews.llvm.org/D71463
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 1 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGExprScalar.cpp | 15 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExprCXX.cpp | 162 |
3 files changed, 163 insertions, 15 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 8a074538612..c4b27b5d1da 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -9378,6 +9378,7 @@ namespace { bool VisitUnaryImag(const UnaryOperator *E); // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, // binary comparisons, binary and/or/xor, + // conditional operator (for GNU conditional select), // shufflevector, ExtVectorElementExpr }; } // end anonymous namespace diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 31487accf41..3f23fe11e4f 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -4311,6 +4311,21 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { return tmp5; } + if (condExpr->getType()->isVectorType()) { + CGF.incrementProfileCounter(E); + + llvm::Value *CondV = CGF.EmitScalarExpr(condExpr); + llvm::Value *LHS = Visit(lhsExpr); + llvm::Value *RHS = Visit(rhsExpr); + + llvm::Type *CondType = ConvertType(condExpr->getType()); + auto *VecTy = cast<llvm::VectorType>(CondType); + llvm::Value *ZeroVec = llvm::Constant::getNullValue(VecTy); + + CondV = Builder.CreateICmpNE(CondV, ZeroVec, "vector_cond"); + return Builder.CreateSelect(CondV, LHS, RHS, "vector_select"); + } + // If this is a really simple expression (like x ? 4 : 5), emit this as a // select instead of as control flow. We can only do this if it is cheap and // safe to evaluate the LHS and RHS unconditionally. diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index e9d075c4c40..a73e6906fce 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -5747,38 +5747,157 @@ static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { return false; } +// Check the condition operand of ?: to see if it is valid for the GCC +// extension. +static bool isValidVectorForConditionalCondition(ASTContext &Ctx, + QualType CondTy) { + if (!CondTy->isVectorType() || CondTy->isExtVectorType()) + return false; + const QualType EltTy = + cast<VectorType>(CondTy.getCanonicalType())->getElementType(); + + assert(!EltTy->isBooleanType() && !EltTy->isEnumeralType() && + "Vectors cant be boolean or enum types"); + return EltTy->isIntegralType(Ctx); +} + +QualType Sema::CheckGNUVectorConditionalTypes(ExprResult &Cond, ExprResult &LHS, + ExprResult &RHS, + SourceLocation QuestionLoc) { + LHS = DefaultFunctionArrayLvalueConversion(LHS.get()); + RHS = DefaultFunctionArrayLvalueConversion(RHS.get()); + + QualType CondType = Cond.get()->getType(); + const auto *CondVT = CondType->getAs<VectorType>(); + QualType CondElementTy = CondVT->getElementType(); + unsigned CondElementCount = CondVT->getNumElements(); + QualType LHSType = LHS.get()->getType(); + const auto *LHSVT = LHSType->getAs<VectorType>(); + QualType RHSType = RHS.get()->getType(); + const auto *RHSVT = RHSType->getAs<VectorType>(); + + QualType ResultType; + + // FIXME: In the future we should define what the Extvector conditional + // operator looks like. + if (LHSVT && isa<ExtVectorType>(LHSVT)) { + Diag(QuestionLoc, diag::err_conditional_vector_operand_type) + << /*isExtVector*/ true << LHSType; + return {}; + } + + if (RHSVT && isa<ExtVectorType>(RHSVT)) { + Diag(QuestionLoc, diag::err_conditional_vector_operand_type) + << /*isExtVector*/ true << RHSType; + return {}; + } + + if (LHSVT && RHSVT) { + // If both are vector types, they must be the same type. + if (!Context.hasSameType(LHSType, RHSType)) { + Diag(QuestionLoc, diag::err_conditional_vector_mismatched_vectors) + << LHSType << RHSType; + return {}; + } + ResultType = LHSType; + } else if (LHSVT || RHSVT) { + ResultType = CheckVectorOperands( + LHS, RHS, QuestionLoc, /*isCompAssign*/ false, /*AllowBothBool*/ true, + /*AllowBoolConversions*/ false); + if (ResultType.isNull()) + return {}; + } else { + // Both are scalar. + QualType ResultElementTy; + LHSType = LHSType.getCanonicalType().getUnqualifiedType(); + RHSType = RHSType.getCanonicalType().getUnqualifiedType(); + + if (Context.hasSameType(LHSType, RHSType)) + ResultElementTy = LHSType; + else + ResultElementTy = + UsualArithmeticConversions(LHS, RHS, QuestionLoc, ACK_Conditional); + + if (ResultElementTy->isEnumeralType()) { + Diag(QuestionLoc, diag::err_conditional_vector_operand_type) + << /*isExtVector*/ false << ResultElementTy; + return {}; + } + ResultType = Context.getVectorType( + ResultElementTy, CondType->getAs<VectorType>()->getNumElements(), + VectorType::GenericVector); + + LHS = ImpCastExprToType(LHS.get(), ResultType, CK_VectorSplat); + RHS = ImpCastExprToType(RHS.get(), ResultType, CK_VectorSplat); + } + + assert(!ResultType.isNull() && ResultType->isVectorType() && + "Result should have been a vector type"); + QualType ResultElementTy = ResultType->getAs<VectorType>()->getElementType(); + unsigned ResultElementCount = + ResultType->getAs<VectorType>()->getNumElements(); + + if (ResultElementCount != CondElementCount) { + Diag(QuestionLoc, diag::err_conditional_vector_size) << CondType + << ResultType; + return {}; + } + + if (Context.getTypeSize(ResultElementTy) != + Context.getTypeSize(CondElementTy)) { + Diag(QuestionLoc, diag::err_conditional_vector_element_size) << CondType + << ResultType; + return {}; + } + + return ResultType; +} + /// Check the operands of ?: under C++ semantics. /// /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y /// extension. In this case, LHS == Cond. (But they're not aliases.) +/// +/// This function also implements GCC's vector extension for conditionals. +/// GCC's vector extension permits the use of a?b:c where the type of +/// a is that of a integer vector with the same number of elements and +/// size as the vectors of b and c. If one of either b or c is a scalar +/// it is implicitly converted to match the type of the vector. +/// Otherwise the expression is ill-formed. If both b and c are scalars, +/// then b and c are checked and converted to the type of a if possible. +/// Unlike the OpenCL ?: operator, the expression is evaluated as +/// (a[0] != 0 ? b[0] : c[0], .. , a[n] != 0 ? b[n] : c[n]). QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, ExprValueKind &VK, ExprObjectKind &OK, SourceLocation QuestionLoc) { - // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ - // interface pointers. + // FIXME: Handle C99's complex types, block pointers and Obj-C++ interface + // pointers. + + // Assume r-value. + VK = VK_RValue; + OK = OK_Ordinary; + bool IsVectorConditional = + isValidVectorForConditionalCondition(Context, Cond.get()->getType()); // C++11 [expr.cond]p1 // The first expression is contextually converted to bool. - // - // FIXME; GCC's vector extension permits the use of a?b:c where the type of - // a is that of a integer vector with the same number of elements and - // size as the vectors of b and c. If one of either b or c is a scalar - // it is implicitly converted to match the type of the vector. - // Otherwise the expression is ill-formed. If both b and c are scalars, - // then b and c are checked and converted to the type of a if possible. - // Unlike the OpenCL ?: operator, the expression is evaluated as - // (a[0] != 0 ? b[0] : c[0], .. , a[n] != 0 ? b[n] : c[n]). if (!Cond.get()->isTypeDependent()) { - ExprResult CondRes = CheckCXXBooleanCondition(Cond.get()); + ExprResult CondRes = IsVectorConditional + ? DefaultFunctionArrayLvalueConversion(Cond.get()) + : CheckCXXBooleanCondition(Cond.get()); if (CondRes.isInvalid()) return QualType(); Cond = CondRes; + } else { + // To implement C++, the first expression typically doesn't alter the result + // type of the conditional, however the GCC compatible vector extension + // changes the result type to be that of the conditional. Since we cannot + // know if this is a vector extension here, delay the conversion of the + // LHS/RHS below until later. + return Context.DependentTy; } - // Assume r-value. - VK = VK_RValue; - OK = OK_Ordinary; // Either of the arguments dependent? if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) @@ -5797,6 +5916,17 @@ QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, // and value category of the other. bool LThrow = isa<CXXThrowExpr>(LHS.get()->IgnoreParenImpCasts()); bool RThrow = isa<CXXThrowExpr>(RHS.get()->IgnoreParenImpCasts()); + + // Void expressions aren't legal in the vector-conditional expressions. + if (IsVectorConditional) { + SourceRange DiagLoc = + LVoid ? LHS.get()->getSourceRange() : RHS.get()->getSourceRange(); + bool IsThrow = LVoid ? LThrow : RThrow; + Diag(DiagLoc.getBegin(), diag::err_conditional_vector_has_void) + << DiagLoc << IsThrow; + return QualType(); + } + if (LThrow != RThrow) { Expr *NonThrow = LThrow ? RHS.get() : LHS.get(); VK = NonThrow->getValueKind(); @@ -5819,6 +5949,8 @@ QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, } // Neither is void. + if (IsVectorConditional) + return CheckGNUVectorConditionalTypes(Cond, LHS, RHS, QuestionLoc); // C++11 [expr.cond]p3 // Otherwise, if the second and third operand have different types, and |