Skip to content

Commit 2432d11

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:dd577c05ad0d into amd-gfx:7e1b5aab3054
Local branch amd-gfx 7e1b5aa Merged main:d32509928ba6 into amd-gfx:de1faa086235 Remote branch main dd577c0 [clang] constexpr built-in reduce min/max function. (llvm#120866)
2 parents 7e1b5aa + dd577c0 commit 2432d11

File tree

9 files changed

+142
-134
lines changed

9 files changed

+142
-134
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,10 @@ at the end to the next power of 2.
736736

737737
These reductions support both fixed-sized and scalable vector types.
738738

739-
The integer reduction intrinsics, including ``__builtin_reduce_add``,
740-
``__builtin_reduce_mul``, ``__builtin_reduce_and``, ``__builtin_reduce_or``,
741-
and ``__builtin_reduce_xor``, can be called in a ``constexpr`` context.
739+
The integer reduction intrinsics, including ``__builtin_reduce_max``,
740+
``__builtin_reduce_min``, ``__builtin_reduce_add``, ``__builtin_reduce_mul``,
741+
``__builtin_reduce_and``, ``__builtin_reduce_or``, and ``__builtin_reduce_xor``,
742+
can be called in a ``constexpr`` context.
742743

743744
Example:
744745

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ Non-comprehensive list of changes in this release
421421
``__builtin_reduce_mul``, ``__builtin_reduce_and``, ``__builtin_reduce_or``,
422422
``__builtin_reduce_xor``, ``__builtin_elementwise_popcount``,
423423
``__builtin_elementwise_bitreverse``, ``__builtin_elementwise_add_sat``,
424-
``__builtin_elementwise_sub_sat``.
424+
``__builtin_elementwise_sub_sat``, ``__builtin_reduce_min`` (For integral element type),
425+
``__builtin_reduce_max`` (For integral element type).
425426

426427
- Clang now rejects ``_BitInt`` matrix element types if the bit width is less than ``CHAR_WIDTH`` or
427428
not a power of two, matching preexisting behaviour for vector types.

clang/include/clang/Basic/Builtins.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,13 +1462,13 @@ def ElementwiseSubSat : Builtin {
14621462

14631463
def ReduceMax : Builtin {
14641464
let Spellings = ["__builtin_reduce_max"];
1465-
let Attributes = [NoThrow, Const, CustomTypeChecking];
1465+
let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr];
14661466
let Prototype = "void(...)";
14671467
}
14681468

14691469
def ReduceMin : Builtin {
14701470
let Spellings = ["__builtin_reduce_min"];
1471-
let Attributes = [NoThrow, Const, CustomTypeChecking];
1471+
let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr];
14721472
let Prototype = "void(...)";
14731473
}
14741474

clang/lib/AST/ExprConstant.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13604,7 +13604,9 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1360413604
case Builtin::BI__builtin_reduce_mul:
1360513605
case Builtin::BI__builtin_reduce_and:
1360613606
case Builtin::BI__builtin_reduce_or:
13607-
case Builtin::BI__builtin_reduce_xor: {
13607+
case Builtin::BI__builtin_reduce_xor:
13608+
case Builtin::BI__builtin_reduce_min:
13609+
case Builtin::BI__builtin_reduce_max: {
1360813610
APValue Source;
1360913611
if (!EvaluateAsRValue(Info, E->getArg(0), Source))
1361013612
return false;
@@ -13641,6 +13643,14 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1364113643
Reduced ^= Source.getVectorElt(EltNum).getInt();
1364213644
break;
1364313645
}
13646+
case Builtin::BI__builtin_reduce_min: {
13647+
Reduced = std::min(Reduced, Source.getVectorElt(EltNum).getInt());
13648+
break;
13649+
}
13650+
case Builtin::BI__builtin_reduce_max: {
13651+
Reduced = std::max(Reduced, Source.getVectorElt(EltNum).getInt());
13652+
break;
13653+
}
1364413654
}
1364513655
}
1364613656

clang/test/Sema/constant_builtins_vector.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,21 @@ static_assert(__builtin_reduce_xor((vector4long){(long long)0x1111111111111111L,
798798
static_assert(__builtin_reduce_xor((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0xFFFFFFFFU);
799799
static_assert(__builtin_reduce_xor((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0xFFFFFFFFFFFFFFFFUL);
800800

801+
static_assert(__builtin_reduce_min((vector4char){}) == 0);
802+
static_assert(__builtin_reduce_min((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == (char)0x88);
803+
static_assert(__builtin_reduce_min((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == (short)0x8888);
804+
static_assert(__builtin_reduce_min((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == (int)0x88888888);
805+
static_assert(__builtin_reduce_min((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0x8888888888888888L);
806+
static_assert(__builtin_reduce_min((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0x11111111U);
807+
static_assert(__builtin_reduce_min((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0x1111111111111111UL);
808+
static_assert(__builtin_reduce_max((vector4char){}) == 0);
809+
static_assert(__builtin_reduce_max((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == (char)0x44);
810+
static_assert(__builtin_reduce_max((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == (short)0x4444);
811+
static_assert(__builtin_reduce_max((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == (int)0x44444444);
812+
static_assert(__builtin_reduce_max((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0x4444444444444444L);
813+
static_assert(__builtin_reduce_max((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0x88888888U);
814+
static_assert(__builtin_reduce_max((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0x8888888888888888UL);
815+
801816
static_assert(__builtin_bit_cast(unsigned, __builtin_elementwise_popcount((vector4char){1, 2, 3, 4})) == (LITTLE_END ? 0x01020101 : 0x01010201));
802817
static_assert(__builtin_bit_cast(unsigned long long, __builtin_elementwise_popcount((vector4short){0, 0x0F0F, ~0, ~0x0F0F})) == (LITTLE_END ? 0x0008001000080000 : 0x0000000800100008));
803818
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4int){1, 2, 3, 4})) == 5);

compiler-rt/lib/tysan/tysan_interceptors.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "interception/interception.h"
15+
#include "sanitizer_common/sanitizer_allocator_dlsym.h"
1516
#include "sanitizer_common/sanitizer_common.h"
1617
#include "tysan/tysan.h"
1718

@@ -28,23 +29,11 @@ extern "C" int mallopt(int param, int value);
2829
using namespace __sanitizer;
2930
using namespace __tysan;
3031

31-
static const uptr early_alloc_buf_size = 16384;
32-
static uptr allocated_bytes;
33-
static char early_alloc_buf[early_alloc_buf_size];
34-
35-
static bool isInEarlyAllocBuf(const void *ptr) {
36-
return ((uptr)ptr >= (uptr)early_alloc_buf &&
37-
((uptr)ptr - (uptr)early_alloc_buf) < sizeof(early_alloc_buf));
38-
}
39-
40-
// Handle allocation requests early (before all interceptors are setup). dlsym,
41-
// for example, calls calloc.
42-
static void *handleEarlyAlloc(uptr size) {
43-
void *mem = (void *)&early_alloc_buf[allocated_bytes];
44-
allocated_bytes += size;
45-
CHECK_LT(allocated_bytes, early_alloc_buf_size);
46-
return mem;
47-
}
32+
namespace {
33+
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
34+
static bool UseImpl() { return !tysan_inited; }
35+
};
36+
} // namespace
4837

4938
INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
5039
if (!tysan_inited && REAL(memset) == nullptr)
@@ -111,16 +100,17 @@ INTERCEPTOR(char *, __strdup, const char *s) {
111100
#endif // TYSAN_INTERCEPT___STRDUP
112101

113102
INTERCEPTOR(void *, malloc, uptr size) {
114-
if (tysan_init_is_running && REAL(malloc) == nullptr)
115-
return handleEarlyAlloc(size);
116-
103+
if (DlsymAlloc::Use())
104+
return DlsymAlloc::Allocate(size);
117105
void *res = REAL(malloc)(size);
118106
if (res)
119107
tysan_set_type_unknown(res, size);
120108
return res;
121109
}
122110

123111
INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
112+
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
113+
return DlsymAlloc::Realloc(ptr, size);
124114
void *res = REAL(realloc)(ptr, size);
125115
// We might want to copy the types from the original allocation (although
126116
// that would require that we knew its size).
@@ -130,21 +120,18 @@ INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
130120
}
131121

132122
INTERCEPTOR(void *, calloc, uptr nmemb, uptr size) {
133-
if (tysan_init_is_running && REAL(calloc) == nullptr)
134-
return handleEarlyAlloc(nmemb * size);
135-
123+
if (DlsymAlloc::Use())
124+
return DlsymAlloc::Callocate(nmemb, size);
136125
void *res = REAL(calloc)(nmemb, size);
137126
if (res)
138127
tysan_set_type_unknown(res, nmemb * size);
139128
return res;
140129
}
141130

142-
INTERCEPTOR(void, free, void *p) {
143-
// There are only a few early allocation requests,
144-
// so we simply skip the free.
145-
if (isInEarlyAllocBuf(p))
146-
return;
147-
REAL(free)(p);
131+
INTERCEPTOR(void, free, void *ptr) {
132+
if (DlsymAlloc::PointerIsMine(ptr))
133+
return DlsymAlloc::Free(ptr);
134+
REAL(free)(ptr);
148135
}
149136

150137
INTERCEPTOR(void *, valloc, uptr size) {

libcxx/test/benchmarks/locale/num_get.bench.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10+
// UNSUPPORTED: c++03
11+
1012
#include <ios>
1113
#include <locale>
1214

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/* Indicate that this is LLVM compiled from the amd-gfx branch. */
1818
#define LLVM_HAVE_BRANCH_AMD_GFX
19-
#define LLVM_MAIN_REVISION 522257
19+
#define LLVM_MAIN_REVISION 522261
2020

2121
/* Define if LLVM_ENABLE_DUMP is enabled */
2222
#cmakedefine LLVM_ENABLE_DUMP

0 commit comments

Comments
 (0)