https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/118177
None >From 69c3275b5119adc049821aacf2b9f01641d73aad Mon Sep 17 00:00:00 2001 From: c8ef <c...@outlook.com> Date: Sun, 1 Dec 2024 00:10:58 +0800 Subject: [PATCH] constexpr elementwise bitreverse --- clang/docs/LanguageExtensions.rst | 2 +- clang/docs/ReleaseNotes.rst | 1 + clang/include/clang/Basic/Builtins.td | 2 +- clang/lib/AST/ExprConstant.cpp | 22 ++++++++++++++----- .../test/CodeGen/builtins-elementwise-math.c | 2 +- clang/test/Sema/constant_builtins_vector.cpp | 5 +++++ 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index c053a5ab3c528c..52032e935928f1 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -648,7 +648,7 @@ elementwise to the input. Unless specified otherwise operation(±0) = ±0 and operation(±infinity) = ±infinity The integer elementwise intrinsics, including ``__builtin_elementwise_popcount``, -can be called in a ``constexpr`` context. +``__builtin_elementwise_bitreverse``, can be called in a ``constexpr`` context. ============================================== ====================================================================== ========================================= Name Operation Supported element types diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index e44aefa90ab386..7a4d58f170d297 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -402,6 +402,7 @@ Non-comprehensive list of changes in this release - ``__builtin_reduce_and`` function can now be used in constant expressions. - ``__builtin_reduce_or`` and ``__builtin_reduce_xor`` functions can now be used in constant expressions. - ``__builtin_elementwise_popcount`` function can now be used in constant expressions. +- ``__builtin_elementwise_bitreverse`` function can now be used in constant expressions. New Compiler Flags ------------------ diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index db5cd73fba8ad1..f953f869d726bb 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -1270,7 +1270,7 @@ def ElementwiseATan2 : Builtin { def ElementwiseBitreverse : Builtin { let Spellings = ["__builtin_elementwise_bitreverse"]; - let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr]; let Prototype = "void(...)"; } diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index bb5ab67328fbc6..a99e91ff7b786e 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -11310,7 +11310,8 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { switch (E->getBuiltinCallee()) { default: return false; - case Builtin::BI__builtin_elementwise_popcount: { + case Builtin::BI__builtin_elementwise_popcount: + case Builtin::BI__builtin_elementwise_bitreverse: { APValue Source; if (!EvaluateAsRValue(Info, E->getArg(0), Source)) return false; @@ -11322,9 +11323,19 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) { APSInt Elt = Source.getVectorElt(EltNum).getInt(); - ResultElements.push_back( - APValue(APSInt(APInt(Info.Ctx.getIntWidth(DestEltTy), Elt.popcount()), - DestEltTy->isUnsignedIntegerOrEnumerationType()))); + switch (E->getBuiltinCallee()) { + case Builtin::BI__builtin_elementwise_popcount: + ResultElements.push_back(APValue( + APSInt(APInt(Info.Ctx.getIntWidth(DestEltTy), Elt.popcount()), + DestEltTy->isUnsignedIntegerOrEnumerationType()))); + break; + case Builtin::BI__builtin_elementwise_bitreverse: + ResultElements.push_back( + APValue(APSInt(Elt.reverseBits(), + DestEltTy->isUnsignedIntegerOrEnumerationType()) + .extOrTrunc(Info.Ctx.getIntWidth(DestEltTy)))); + break; + } } return Success(APValue(ResultElements.data(), ResultElements.size()), E); @@ -12833,7 +12844,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, case Builtin::BI__builtin_bitreverse8: case Builtin::BI__builtin_bitreverse16: case Builtin::BI__builtin_bitreverse32: - case Builtin::BI__builtin_bitreverse64: { + case Builtin::BI__builtin_bitreverse64: + case Builtin::BI__builtin_elementwise_bitreverse: { APSInt Val; if (!EvaluateInteger(E->getArg(0), Val, Info)) return false; diff --git a/clang/test/CodeGen/builtins-elementwise-math.c b/clang/test/CodeGen/builtins-elementwise-math.c index f1f34432ca0ea1..82f82dd1ed7944 100644 --- a/clang/test/CodeGen/builtins-elementwise-math.c +++ b/clang/test/CodeGen/builtins-elementwise-math.c @@ -443,7 +443,7 @@ void test_builtin_elementwise_bitreverse(si8 vi1, si8 vi2, // CHECK-NEXT: call i32 @llvm.bitreverse.i32(i32 [[IA1]]) b = __builtin_elementwise_bitreverse(int_as_one); - // CHECK: call i32 @llvm.bitreverse.i32(i32 -10) + // CHECK: store i32 1879048191, ptr @b, align 4 b = __builtin_elementwise_bitreverse(-10); // CHECK: [[SI:%.+]] = load i16, ptr %si.addr, align 2 diff --git a/clang/test/Sema/constant_builtins_vector.cpp b/clang/test/Sema/constant_builtins_vector.cpp index 772a682141ce41..238b9bbb09b840 100644 --- a/clang/test/Sema/constant_builtins_vector.cpp +++ b/clang/test/Sema/constant_builtins_vector.cpp @@ -817,3 +817,8 @@ static_assert(__builtin_elementwise_popcount(~0U) == 8 * sizeof(int)); static_assert(__builtin_elementwise_popcount(0L) == 0); static_assert(__builtin_elementwise_popcount(0xF0F0L) == 8); static_assert(__builtin_elementwise_popcount(~0LL) == 8 * sizeof(long long)); + +static_assert(__builtin_elementwise_bitreverse(0x12345678) == 0x1E6A2C48); +static_assert(__builtin_elementwise_bitreverse(0x0123456789ABCDEFULL) == 0xF7B3D591E6A2C480); +static_assert(__builtin_bit_cast(unsigned, __builtin_elementwise_bitreverse((vector4char){1, 2, 4, 8})) == (LITTLE_END ? 0x10204080 : 0x80402010)); +static_assert(__builtin_bit_cast(unsigned long, __builtin_elementwise_bitreverse((vector4short){1, 2, 4, 8})) == (LITTLE_END ? 0x1000200040008000 : 0x8000400020001000)); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits