Skip to content

Commit af0e0de

Browse files
authored
[clang] constexpr built-in reduce and function. (#116822)
Part of #51787. Follow up of #116626. This patch adds constexpr support for the built-in reduce and function.
1 parent 18b02bb commit af0e0de

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ Non-comprehensive list of changes in this release
357357

358358
- ``__builtin_reduce_add`` function can now be used in constant expressions.
359359
- ``__builtin_reduce_mul`` function can now be used in constant expressions.
360+
- ``__builtin_reduce_and`` function can now be used in constant expressions.
360361

361362
New Compiler Flags
362363
------------------

clang/include/clang/Basic/Builtins.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ def ReduceOr : Builtin {
14981498

14991499
def ReduceAnd : Builtin {
15001500
let Spellings = ["__builtin_reduce_and"];
1501-
let Attributes = [NoThrow, Const, CustomTypeChecking];
1501+
let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr];
15021502
let Prototype = "void(...)";
15031503
}
15041504

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13528,7 +13528,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1352813528
}
1352913529

1353013530
case Builtin::BI__builtin_reduce_add:
13531-
case Builtin::BI__builtin_reduce_mul: {
13531+
case Builtin::BI__builtin_reduce_mul:
13532+
case Builtin::BI__builtin_reduce_and: {
1353213533
APValue Source;
1353313534
if (!EvaluateAsRValue(Info, E->getArg(0), Source))
1353413535
return false;
@@ -13553,6 +13554,10 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1355313554
return false;
1355413555
break;
1355513556
}
13557+
case Builtin::BI__builtin_reduce_and: {
13558+
Reduced &= Source.getVectorElt(EltNum).getInt();
13559+
break;
13560+
}
1355613561
}
1355713562
}
1355813563

clang/test/Sema/constant_builtins_vector.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,15 @@ constexpr long long reduceMulLong2 = __builtin_reduce_mul((vector4long){(1LL <<
765765
// expected-note@-1 {{outside the range of representable values of type 'long long'}}
766766
static_assert(__builtin_reduce_mul((vector4uint){~0U, 1, 1, 2}) == ~0U - 1);
767767
static_assert(__builtin_reduce_mul((vector4ulong){~0ULL, 1, 1, 2}) == ~0ULL - 1);
768+
769+
static_assert(__builtin_reduce_and((vector4char){}) == 0);
770+
static_assert(__builtin_reduce_and((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == 0);
771+
static_assert(__builtin_reduce_and((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == 0);
772+
static_assert(__builtin_reduce_and((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == 0);
773+
static_assert(__builtin_reduce_and((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == 0L);
774+
static_assert(__builtin_reduce_and((vector4char){(char)-1, (char)~0x22, (char)~0x44, (char)~0x88}) == 0x11);
775+
static_assert(__builtin_reduce_and((vector4short){(short)~0x1111, (short)-1, (short)~0x4444, (short)~0x8888}) == 0x2222);
776+
static_assert(__builtin_reduce_and((vector4int){(int)~0x11111111, (int)~0x22222222, (int)-1, (int)~0x88888888}) == 0x44444444);
777+
static_assert(__builtin_reduce_and((vector4long){(long long)~0x1111111111111111L, (long long)~0x2222222222222222L, (long long)~0x4444444444444444L, (long long)-1}) == 0x8888888888888888L);
778+
static_assert(__builtin_reduce_and((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0U);
779+
static_assert(__builtin_reduce_and((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0L);

0 commit comments

Comments
 (0)