[clang] [clang][x86][bytecode] Replace interp__builtin_parity/clrsb/bitreverse/ffs with static bool interp__builtin_elementwise_int_unaryop callback (PR #162346)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 7 13:29:00 PDT 2025


https://github.com/donneypr updated https://github.com/llvm/llvm-project/pull/162346

>From 26eada5acd993db9d0eb23e9cfe9a5b2ac9c0c7f Mon Sep 17 00:00:00 2001
From: donneypr <donatoprabahar at gmail.com>
Date: Tue, 7 Oct 2025 14:45:05 -0400
Subject: [PATCH 1/7] 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 <donatoprabahar at gmail.com>
Date: Tue, 7 Oct 2025 14:50:57 -0400
Subject: [PATCH 2/7] 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 <donatoprabahar at gmail.com>
Date: Tue, 7 Oct 2025 14:58:40 -0400
Subject: [PATCH 3/7] 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 <donatoprabahar at gmail.com>
Date: Tue, 7 Oct 2025 15:00:25 -0400
Subject: [PATCH 4/7] 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 <donatoprabahar at gmail.com>
Date: Tue, 7 Oct 2025 15:40:01 -0400
Subject: [PATCH 5/7] 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 <donatoprabahar at gmail.com>
Date: Tue, 7 Oct 2025 16:05:34 -0400
Subject: [PATCH 6/7] 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 <donatoprabahar at gmail.com>
Date: Tue, 7 Oct 2025 16:28:16 -0400
Subject: [PATCH 7/7] 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);
 



More information about the cfe-commits mailing list