Skip to content

Commit ccc471f

Browse files
authored
[clang][bytecode] Implement __builtin_reduce_and (#118289)
1 parent e48c7fe commit ccc471f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ static bool interp__builtin_vector_reduce(InterpState &S, CodePtr OpPC,
17001700
PrimType ElemT = *S.getContext().classify(ElemType);
17011701
unsigned NumElems = Arg.getNumElems();
17021702

1703-
INT_TYPE_SWITCH(ElemT, {
1703+
INT_TYPE_SWITCH_NO_BOOL(ElemT, {
17041704
T Result = Arg.atIndex(0).deref<T>();
17051705
unsigned BitWidth = Result.bitWidth();
17061706
for (unsigned I = 1; I != NumElems; ++I) {
@@ -1723,6 +1723,9 @@ static bool interp__builtin_vector_reduce(InterpState &S, CodePtr OpPC,
17231723
Elem.toAPSInt(OverflowBits)));
17241724
return false;
17251725
}
1726+
1727+
} else if (ID == Builtin::BI__builtin_reduce_and) {
1728+
(void)T::bitAnd(Result, Elem, BitWidth, &Result);
17261729
} else {
17271730
llvm_unreachable("Unhandled vector reduce builtin");
17281731
}
@@ -2206,6 +2209,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
22062209

22072210
case Builtin::BI__builtin_reduce_add:
22082211
case Builtin::BI__builtin_reduce_mul:
2212+
case Builtin::BI__builtin_reduce_and:
22092213
if (!interp__builtin_vector_reduce(S, OpPC, Frame, F, Call))
22102214
return false;
22112215
break;

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,22 @@ namespace ReduceMul {
10641064
static_assert(__builtin_reduce_mul((vector4ulong){~0ULL, 1, 1, 2}) == ~0ULL - 1);
10651065
}
10661066

1067+
namespace ReduceAnd {
1068+
static_assert(__builtin_reduce_and((vector4char){}) == 0);
1069+
static_assert(__builtin_reduce_and((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == 0);
1070+
static_assert(__builtin_reduce_and((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == 0);
1071+
static_assert(__builtin_reduce_and((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == 0);
1072+
#if __INT_WIDTH__ == 32
1073+
static_assert(__builtin_reduce_and((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == 0L);
1074+
static_assert(__builtin_reduce_and((vector4char){(char)-1, (char)~0x22, (char)~0x44, (char)~0x88}) == 0x11);
1075+
static_assert(__builtin_reduce_and((vector4short){(short)~0x1111, (short)-1, (short)~0x4444, (short)~0x8888}) == 0x2222);
1076+
static_assert(__builtin_reduce_and((vector4int){(int)~0x11111111, (int)~0x22222222, (int)-1, (int)~0x88888888}) == 0x44444444);
1077+
static_assert(__builtin_reduce_and((vector4long){(long long)~0x1111111111111111L, (long long)~0x2222222222222222L, (long long)~0x4444444444444444L, (long long)-1}) == 0x8888888888888888L);
1078+
static_assert(__builtin_reduce_and((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0U);
1079+
static_assert(__builtin_reduce_and((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0L);
1080+
#endif
1081+
}
1082+
10671083
namespace BuiltinMemcpy {
10681084
constexpr int simple() {
10691085
int a = 12;

0 commit comments

Comments
 (0)