From 26eada5acd993db9d0eb23e9cfe9a5b2ac9c0c7f Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 14:45:05 -0400 Subject: [PATCH 1/9] Replaced callback for parity llvm#160288 --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index a0dcdace854b9..87725ea0de5c4 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -671,15 +671,6 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC, return true; } -static bool interp__builtin_parity(InterpState &S, CodePtr OpPC, - const InterpFrame *Frame, - const CallExpr *Call) { - PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType()); - APSInt Val = popToAPSInt(S.Stk, ArgT); - pushInteger(S, Val.popcount() % 2, Call->getType()); - return true; -} - static bool interp__builtin_clrsb(InterpState &S, CodePtr OpPC, const InterpFrame *Frame, const CallExpr *Call) { @@ -3032,8 +3023,9 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case Builtin::BI__builtin_parity: case Builtin::BI__builtin_parityl: case Builtin::BI__builtin_parityll: - return interp__builtin_parity(S, OpPC, Frame, Call); - + return interp__builtin_elementwise_int_unaryop(S, OpPC, Call, [](const APSInt &Val) -> APInt { + return APInt(Val.getBitWidth(), Val.popcount() % 2); + }); case Builtin::BI__builtin_clrsb: case Builtin::BI__builtin_clrsbl: case Builtin::BI__builtin_clrsbll: From 9d8d082efc3700cea92fbfbb7d103811a4387a7b Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 14:50:57 -0400 Subject: [PATCH 2/9] Replaced callback for clrsb llvm#160288 --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 87725ea0de5c4..2436ca88ca3e5 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -671,15 +671,6 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC, return true; } -static bool interp__builtin_clrsb(InterpState &S, CodePtr OpPC, - const InterpFrame *Frame, - const CallExpr *Call) { - PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType()); - APSInt Val = popToAPSInt(S.Stk, ArgT); - pushInteger(S, Val.getBitWidth() - Val.getSignificantBits(), Call->getType()); - return true; -} - static bool interp__builtin_bitreverse(InterpState &S, CodePtr OpPC, const InterpFrame *Frame, const CallExpr *Call) { @@ -3023,14 +3014,18 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case Builtin::BI__builtin_parity: case Builtin::BI__builtin_parityl: case Builtin::BI__builtin_parityll: - return interp__builtin_elementwise_int_unaryop(S, OpPC, Call, [](const APSInt &Val) -> APInt { - return APInt(Val.getBitWidth(), Val.popcount() % 2); - }); + return interp__builtin_elementwise_int_unaryop( + S, OpPC, Call, [](const APSInt &Val) -> APInt { + return APInt(Val.getBitWidth(), Val.popcount() % 2); + }); case Builtin::BI__builtin_clrsb: case Builtin::BI__builtin_clrsbl: case Builtin::BI__builtin_clrsbll: - return interp__builtin_clrsb(S, OpPC, Frame, Call); - + return interp__builtin_elementwise_int_unaryop( + S, OpPC, Call, [](const APSInt &V) -> APInt { + return APInt(Val.getBitWidth(), + Val.getBitWidth() - Val.getSignificantBits()); + }); case Builtin::BI__builtin_bitreverse8: case Builtin::BI__builtin_bitreverse16: case Builtin::BI__builtin_bitreverse32: From d5e1e6315bccfecc7d3bbb1e3960e0751e4077a5 Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 14:58:40 -0400 Subject: [PATCH 3/9] Replaced callback for bitreverse llvm#160288 --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 2436ca88ca3e5..1683cd0c0a0fb 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -671,15 +671,6 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC, return true; } -static bool interp__builtin_bitreverse(InterpState &S, CodePtr OpPC, - const InterpFrame *Frame, - const CallExpr *Call) { - PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType()); - APSInt Val = popToAPSInt(S.Stk, ArgT); - pushInteger(S, Val.reverseBits(), Call->getType()); - return true; -} - static bool interp__builtin_classify_type(InterpState &S, CodePtr OpPC, const InterpFrame *Frame, const CallExpr *Call) { @@ -3030,7 +3021,9 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case Builtin::BI__builtin_bitreverse16: case Builtin::BI__builtin_bitreverse32: case Builtin::BI__builtin_bitreverse64: - return interp__builtin_bitreverse(S, OpPC, Frame, Call); + return interp__builtin_elementwise_int_unaryop( + S, OpPC, Call, + [](const APSInt &Val) -> APInt { return Val.reverseBits(); }); case Builtin::BI__builtin_classify_type: return interp__builtin_classify_type(S, OpPC, Frame, Call); From b08aa333155485e424228650781fb19704e29cc1 Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 15:00:25 -0400 Subject: [PATCH 4/9] fixed typo for clrsb V -> Val --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 1683cd0c0a0fb..81e6bd55990fa 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -3013,7 +3013,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case Builtin::BI__builtin_clrsbl: case Builtin::BI__builtin_clrsbll: return interp__builtin_elementwise_int_unaryop( - S, OpPC, Call, [](const APSInt &V) -> APInt { + S, OpPC, Call, [](const APSInt &Val) -> APInt { return APInt(Val.getBitWidth(), Val.getBitWidth() - Val.getSignificantBits()); }); @@ -3022,8 +3022,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case Builtin::BI__builtin_bitreverse32: case Builtin::BI__builtin_bitreverse64: return interp__builtin_elementwise_int_unaryop( - S, OpPC, Call, - [](const APSInt &Val) -> APInt { return Val.reverseBits(); }); + S, OpPC, Call, + [](const APSInt &Val) -> APInt { return Val.reverseBits(); }); case Builtin::BI__builtin_classify_type: return interp__builtin_classify_type(S, OpPC, Frame, Call); From 8d9c86c635592342a01f8fd0a802efa2ad9337c0 Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 15:40:01 -0400 Subject: [PATCH 5/9] Replaced callback for ffs llvm#160288 --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 81e6bd55990fa..dc64c347aeddb 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -727,17 +727,6 @@ static bool interp__builtin_rotate(InterpState &S, CodePtr OpPC, return true; } -static bool interp__builtin_ffs(InterpState &S, CodePtr OpPC, - const InterpFrame *Frame, - const CallExpr *Call) { - PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType()); - APSInt Value = popToAPSInt(S.Stk, ArgT); - - uint64_t N = Value.countr_zero(); - pushInteger(S, N == Value.getBitWidth() ? 0 : N + 1, Call->getType()); - return true; -} - static bool interp__builtin_addressof(InterpState &S, CodePtr OpPC, const InterpFrame *Frame, const CallExpr *Call) { @@ -3057,7 +3046,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case Builtin::BI__builtin_ffs: case Builtin::BI__builtin_ffsl: case Builtin::BI__builtin_ffsll: - return interp__builtin_ffs(S, OpPC, Frame, Call); + return interp__builtin_elementwise_int_unaryop(S, OpPC, Call, [](const APSInt& Val){ return APInt(Val.getBitWidth(), (Val.countTrailingZeros()==Val.getBitWidth()) ? 0u : (Val.countTrailingZeros()+1u)); }); case Builtin::BIaddressof: case Builtin::BI__addressof: From ac30e63ee29b7d2c1bef281fcb31857155cb6668 Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 16:05:34 -0400 Subject: [PATCH 6/9] clang formatted --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 3792588a2aa19..b1e7929abf639 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -3204,7 +3204,13 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, case Builtin::BI__builtin_ffs: case Builtin::BI__builtin_ffsl: case Builtin::BI__builtin_ffsll: - return interp__builtin_elementwise_int_unaryop(S, OpPC, Call, [](const APSInt& Val){ return APInt(Val.getBitWidth(), (Val.countTrailingZeros()==Val.getBitWidth()) ? 0u : (Val.countTrailingZeros()+1u)); }); + return interp__builtin_elementwise_int_unaryop( + S, OpPC, Call, [](const APSInt &Val) { + return APInt(Val.getBitWidth(), + (Val.countTrailingZeros() == Val.getBitWidth()) + ? 0u + : (Val.countTrailingZeros() + 1u)); + }); case Builtin::BIaddressof: case Builtin::BI__addressof: From 08f30d2d4224b4d43757deac840dfb154aadfa82 Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 16:28:16 -0400 Subject: [PATCH 7/9] Bytecode: fix rotate; drop accidental ffs lines --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index b1e7929abf639..1525f253f99f7 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -719,6 +719,8 @@ static bool interp__builtin_rotate(InterpState &S, CodePtr OpPC, PrimType AmountT = *S.getContext().classify(Call->getArg(1)->getType()); PrimType ValueT = *S.getContext().classify(Call->getArg(0)->getType()); + uint64_t N = Value.countr_zero(); + pushInteger(S, N == Value.getBitWidth() ? 0 : N + 1, Call->getType()); APSInt Amount = popToAPSInt(S.Stk, AmountT); APSInt Value = popToAPSInt(S.Stk, ValueT); From 808905344bf30016e43b4e2f723255f84614276f Mon Sep 17 00:00:00 2001 From: donneypr Date: Tue, 7 Oct 2025 16:34:20 -0400 Subject: [PATCH 8/9] removed rotate as it was removed in PR #161924 --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 1525f253f99f7..d4754fdeb0480 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -712,30 +712,6 @@ static bool interp__builtin_expect(InterpState &S, CodePtr OpPC, return true; } -/// rotateleft(value, amount) -static bool interp__builtin_rotate(InterpState &S, CodePtr OpPC, - const InterpFrame *Frame, - const CallExpr *Call, bool Right) { - PrimType AmountT = *S.getContext().classify(Call->getArg(1)->getType()); - PrimType ValueT = *S.getContext().classify(Call->getArg(0)->getType()); - - uint64_t N = Value.countr_zero(); - pushInteger(S, N == Value.getBitWidth() ? 0 : N + 1, Call->getType()); - APSInt Amount = popToAPSInt(S.Stk, AmountT); - APSInt Value = popToAPSInt(S.Stk, ValueT); - - APSInt Result; - if (Right) - Result = APSInt(Value.rotr(Amount.urem(Value.getBitWidth())), - /*IsUnsigned=*/true); - else // Left. - Result = APSInt(Value.rotl(Amount.urem(Value.getBitWidth())), - /*IsUnsigned=*/true); - - pushInteger(S, Result, Call->getType()); - return true; -} - static bool interp__builtin_addressof(InterpState &S, CodePtr OpPC, const InterpFrame *Frame, const CallExpr *Call) { From b5d1c9f155bf5fd45e74cd73bc1a42f671f6dc00 Mon Sep 17 00:00:00 2001 From: donneypr Date: Wed, 8 Oct 2025 09:48:08 -0400 Subject: [PATCH 9/9] Check to see if Val is 0 for ffs llvm#160288 --- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index d4754fdeb0480..97897b12204cb 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -3185,9 +3185,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, return interp__builtin_elementwise_int_unaryop( S, OpPC, Call, [](const APSInt &Val) { return APInt(Val.getBitWidth(), - (Val.countTrailingZeros() == Val.getBitWidth()) - ? 0u - : (Val.countTrailingZeros() + 1u)); + Val.isZero() ? 0u : Val.countTrailingZeros() + 1u); }); case Builtin::BIaddressof: