Skip to content

Commit 1288f6d

Browse files
authored
[clang][bytecode] Implement __builtin_reduce_or (#118292)
1 parent 39337ff commit 1288f6d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ template <typename T>
104104
static void pushInteger(InterpState &S, T Val, QualType QT) {
105105
if constexpr (std::is_same_v<T, APInt>)
106106
pushInteger(S, APSInt(Val, !std::is_signed_v<T>), QT);
107+
else if constexpr (std::is_same_v<T, APSInt>)
108+
pushInteger(S, Val, QT);
107109
else
108110
pushInteger(S,
109111
APSInt(APInt(sizeof(T) * 8, static_cast<uint64_t>(Val),
@@ -1726,11 +1728,13 @@ static bool interp__builtin_vector_reduce(InterpState &S, CodePtr OpPC,
17261728

17271729
} else if (ID == Builtin::BI__builtin_reduce_and) {
17281730
(void)T::bitAnd(Result, Elem, BitWidth, &Result);
1731+
} else if (ID == Builtin::BI__builtin_reduce_or) {
1732+
(void)T::bitOr(Result, Elem, BitWidth, &Result);
17291733
} else {
17301734
llvm_unreachable("Unhandled vector reduce builtin");
17311735
}
17321736
}
1733-
pushInteger(S, Result, Call->getType());
1737+
pushInteger(S, Result.toAPSInt(), Call->getType());
17341738
});
17351739

17361740
return true;
@@ -2210,6 +2214,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
22102214
case Builtin::BI__builtin_reduce_add:
22112215
case Builtin::BI__builtin_reduce_mul:
22122216
case Builtin::BI__builtin_reduce_and:
2217+
case Builtin::BI__builtin_reduce_or:
22132218
if (!interp__builtin_vector_reduce(S, OpPC, Frame, F, Call))
22142219
return false;
22152220
break;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,22 @@ namespace ReduceAnd {
10801080
#endif
10811081
}
10821082

1083+
namespace ReduceOr {
1084+
static_assert(__builtin_reduce_or((vector4char){}) == 0);
1085+
static_assert(__builtin_reduce_or((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == (char)0xFF);
1086+
static_assert(__builtin_reduce_or((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == (short)0xFFFF);
1087+
static_assert(__builtin_reduce_or((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == (int)0xFFFFFFFF);
1088+
#if __INT_WIDTH__ == 32
1089+
static_assert(__builtin_reduce_or((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0xFFFFFFFFFFFFFFFFL);
1090+
static_assert(__builtin_reduce_or((vector4char){(char)0, (char)0x22, (char)0x44, (char)0x88}) == ~0x11);
1091+
static_assert(__builtin_reduce_or((vector4short){(short)0x1111, (short)0, (short)0x4444, (short)0x8888}) == ~0x2222);
1092+
static_assert(__builtin_reduce_or((vector4int){(int)0x11111111, (int)0x22222222, (int)0, (int)0x88888888}) == ~0x44444444);
1093+
static_assert(__builtin_reduce_or((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0}) == ~0x8888888888888888L);
1094+
static_assert(__builtin_reduce_or((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0xFFFFFFFFU);
1095+
static_assert(__builtin_reduce_or((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0xFFFFFFFFFFFFFFFFL);
1096+
#endif
1097+
}
1098+
10831099
namespace BuiltinMemcpy {
10841100
constexpr int simple() {
10851101
int a = 12;

0 commit comments

Comments
 (0)