https://github.com/tbaederr created 
https://github.com/llvm/llvm-project/pull/204516

We're not necessarily using an APSInt of the appropriate size from the start, 
so get it to the right bitwidth here.

>From d24e10491c597418c65ac48bd8539bd7d0115468 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Thu, 18 Jun 2026 08:19:20 +0200
Subject: [PATCH] [clang][bytecode] `extOrTrunc()` when pushing builtin results

We're not necessarily using an APSInt of the appropriate size from the
start, so get it to the right bitwidth here.
---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp      | 4 ++--
 clang/test/AST/ByteCode/builtin-functions.cpp | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 55907bf11506b..b16a34543757b 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -100,7 +100,7 @@ static void pushInteger(InterpState &S, const APSInt &Val, 
QualType QT) {
   if (T == PT_IntAPS) {
     unsigned BitWidth = S.getASTContext().getIntWidth(QT);
     auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
-    Result.copy(Val);
+    Result.copy(Val.extOrTrunc(BitWidth));
     S.Stk.push<IntegralAP<true>>(Result);
     return;
   }
@@ -108,7 +108,7 @@ static void pushInteger(InterpState &S, const APSInt &Val, 
QualType QT) {
   if (T == PT_IntAP) {
     unsigned BitWidth = S.getASTContext().getIntWidth(QT);
     auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
-    Result.copy(Val);
+    Result.copy(Val.extOrTrunc(BitWidth));
     S.Stk.push<IntegralAP<false>>(Result);
     return;
   }
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp 
b/clang/test/AST/ByteCode/builtin-functions.cpp
index 57157392f6a6e..3074a84986520 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -856,6 +856,8 @@ namespace ctz {
   char ctz53[__builtin_ctzg((unsigned __int128)0x10, 42) == 4 ? 1 : -1];
   char ctz54[__builtin_ctzg((unsigned __int128)1 << (BITSIZE(__int128) - 1)) 
== BITSIZE(__int128) - 1 ? 1 : -1];
   char ctz55[__builtin_ctzg((unsigned __int128)1 << (BITSIZE(__int128) - 1), 
42) == BITSIZE(__int128) - 1 ? 1 : -1];
+
+  static_assert(__builtin_elementwise_ctzg((__int128)42) == 1, "");
 #endif
 #ifndef __AVR__
   int ctz56 = __builtin_ctzg((unsigned _BitInt(128))0);

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to