Skip to content

Commit b2f4d35

Browse files
committed
constexpr reduce_add
1 parent 9f06129 commit b2f4d35

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ Non-comprehensive list of changes in this release
353353
The flexible array member (FAM) can now be accessed immediately without causing
354354
issues with the sanitizer because the counter is automatically set.
355355

356+
- ``__builtin_reduce_add`` function can now be used in constant expressions.
357+
356358
New Compiler Flags
357359
------------------
358360

clang/include/clang/Basic/Builtins.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ def ReduceAnd : Builtin {
15041504

15051505
def ReduceAdd : Builtin {
15061506
let Spellings = ["__builtin_reduce_add"];
1507-
let Attributes = [NoThrow, Const, CustomTypeChecking];
1507+
let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr];
15081508
let Prototype = "void(...)";
15091509
}
15101510

clang/lib/AST/ExprConstant.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13528,6 +13528,20 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1352813528
return Success(DidOverflow, E);
1352913529
}
1353013530

13531+
case Builtin::BI__builtin_reduce_add: {
13532+
APValue Source;
13533+
if (!EvaluateAsRValue(Info, E->getArg(0), Source))
13534+
return false;
13535+
13536+
auto SourceLen = Source.getVectorLength();
13537+
APSInt Reduced = Source.getVectorElt(0).getInt();
13538+
for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
13539+
Reduced += Source.getVectorElt(EltNum).getInt();
13540+
}
13541+
13542+
return Success(Reduced, E);
13543+
}
13544+
1353113545
case clang::X86::BI__builtin_ia32_addcarryx_u32:
1353213546
case clang::X86::BI__builtin_ia32_addcarryx_u64:
1353313547
case clang::X86::BI__builtin_ia32_subborrow_u32:

clang/test/Sema/constant_builtins_vector.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,9 @@ not within the bounds of the input vectors; index of -1 found at position 0 is n
723723
permitted in a constexpr context}}
724724
vector4charConst1,
725725
vector4charConst2, -1, -1, -1, -1);
726+
727+
static_assert(__builtin_reduce_add((vector4char){}) == 0);
728+
static_assert(__builtin_reduce_add((vector4char){1, 2, 3, 4}) == 10);
729+
static_assert(__builtin_reduce_add((vector4short){10, 20, 30, 40}) == 100);
730+
static_assert(__builtin_reduce_add((vector4int){100, 200, 300, 400}) == 1000);
731+
static_assert(__builtin_reduce_add((vector4long){1000, 2000, 3000, 4000}) == 10000);

0 commit comments

Comments
 (0)