Skip to content

Commit 8d41d93

Browse files
Address some misc comments; added a diagnostic and expanded macros in
testing.
1 parent 9493c0f commit 8d41d93

File tree

4 files changed

+625
-15
lines changed

4 files changed

+625
-15
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2954,7 +2954,7 @@ for the implementation of various target-specific header files like
29542954
// Concatenate every other element of 8-element vectors V1 and V2.
29552955
__builtin_shufflevector(V1, V2, 0, 2, 4, 6, 8, 10, 12, 14)
29562956
2957-
// Shuffle v1 with some elements being undefined
2957+
// Shuffle v1 with some elements being undefined. Not allowed in constexpr.
29582958
__builtin_shufflevector(v1, v1, 3, -1, 1, -1)
29592959
29602960
**Description**:
@@ -2967,6 +2967,7 @@ starting with the first vector, continuing into the second vector. Thus, if
29672967
``vec1`` is a 4-element vector, index 5 would refer to the second element of
29682968
``vec2``. An index of -1 can be used to indicate that the corresponding element
29692969
in the returned vector is a don't care and can be optimized by the backend.
2970+
Values of -1 are not supported in constant expressions.
29702971
29712972
The result of ``__builtin_shufflevector`` is a vector with the same element
29722973
type as ``vec1``/``vec2`` but that has an element count equal to the number of

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10331,10 +10331,12 @@ def err_shufflevector_argument_too_large : Error<
1033110331
"index for __builtin_shufflevector must be less than the total number "
1033210332
"of vector elements">;
1033310333
def err_shufflevector_minus_one_is_undefined_behavior_constexpr : Error<
10334-
"index for __builtin_shufflevector must be within the bounds of the input vectors in a constexpr context. An index of -1 at position %0 was found. -1 is only allowed at runtime.">;
10334+
"index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position %0 not permitted in a constexpr context.">;
1033510335

1033610336
def err_convertvector_non_vector : Error<
1033710337
"first argument to __builtin_convertvector must be a vector">;
10338+
def err_convertvector_constexpr_unsupported_vector_cast : Error<
10339+
"unsupported vector cast from %0 to %1 in a constant expression.">;
1033810340
def err_builtin_non_vector_type : Error<
1033910341
"%0 argument to %1 must be of vector type">;
1034010342
def err_convertvector_incompatible_vector : Error<

clang/lib/AST/ExprConstant.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10995,6 +10995,9 @@ static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
1099510995
DestTy, Result.getInt());
1099610996
}
1099710997
}
10998+
10999+
Info.FFDiag(E, diag::err_convertvector_constexpr_unsupported_vector_cast)
11000+
<< SourceTy << DestTy;
1099811001
return false;
1099911002
}
1100011003

@@ -11027,24 +11030,28 @@ static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
1102711030
QualType ElemType, APValue const &VecVal1,
1102811031
APValue const &VecVal2, unsigned EltNum,
1102911032
APValue &Result) {
11030-
unsigned const TotalElementsInInputVector = VecVal1.getVectorLength();
11033+
unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();
11034+
unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();
1103111035

1103211036
APSInt IndexVal = E->getShuffleMaskIdx(Info.Ctx, EltNum);
1103311037
int64_t index = IndexVal.getExtValue();
1103411038
// The spec says that -1 should be treated as undef for optimizations,
11035-
// but in constexpr we need to choose a value. We'll choose 0.
11039+
// but in constexpr we'd have to produce an APValue::Indeterminate,
11040+
// which is prohibited from being a top-level constant value. Emit a
11041+
// diagnostic instead.
1103611042
if (index == -1) {
1103711043
Info.FFDiag(
1103811044
E, diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
1103911045
<< EltNum;
1104011046
return false;
1104111047
}
1104211048

11043-
if (index < 0 || index >= TotalElementsInInputVector * 2)
11049+
if (index < 0 ||
11050+
index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)
1104411051
llvm_unreachable("Out of bounds shuffle index");
1104511052

11046-
if (index >= TotalElementsInInputVector)
11047-
Result = VecVal2.getVectorElt(index - TotalElementsInInputVector);
11053+
if (index >= TotalElementsInInputVector1)
11054+
Result = VecVal2.getVectorElt(index - TotalElementsInInputVector1);
1104811055
else
1104911056
Result = VecVal1.getVectorElt(index);
1105011057
return true;
@@ -11061,9 +11068,6 @@ bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
1106111068
return false;
1106211069

1106311070
VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
11064-
if (!DestVecTy)
11065-
return false;
11066-
1106711071
QualType DestElTy = DestVecTy->getElementType();
1106811072

1106911073
auto TotalElementsInOutputVector = DestVecTy->getNumElements();

0 commit comments

Comments
 (0)