Author: c8ef Date: 2024-12-03T09:33:53+08:00 New Revision: 814ed93e48db4d95965258e64e8580056828a264
URL: https://github.com/llvm/llvm-project/commit/814ed93e48db4d95965258e64e8580056828a264 DIFF: https://github.com/llvm/llvm-project/commit/814ed93e48db4d95965258e64e8580056828a264.diff LOG: [clang] constexpr built-in elementwise bitreverse function. (#118177) Part of #51787. This patch adds constexpr support for the built-in elementwise bitreverse function. Added: Modified: clang/docs/LanguageExtensions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Basic/Builtins.td clang/lib/AST/ExprConstant.cpp clang/test/CodeGen/builtins-elementwise-math.c clang/test/Sema/constant_builtins_vector.cpp Removed: ################################################################################ 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 0bb2eb820cd726..20bd27ad52f577 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -404,6 +404,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 130e91103da069..dda44f3abe0160 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..6b5b95aee35522 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,18 @@ 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()))); + break; + } } return Success(APValue(ResultElements.data(), ResultElements.size()), E); @@ -12833,7 +12843,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..45c729f76418d1 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 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