[clang] [Clang] Fixes builtin_bswapg builtin for bool type (PR #179177)
Simon Pilgrim via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 4 05:23:31 PST 2026
https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/179177
>From 70e38884cf3accb78585390628d026b1f3b7ad4a Mon Sep 17 00:00:00 2001
From: woruyu <1214539920 at qq.com>
Date: Mon, 2 Feb 2026 15:30:07 +0800
Subject: [PATCH 1/3] [Clang] Fixes builtin_bswapg builtin for bool type
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 2 +-
clang/lib/AST/ExprConstant.cpp | 3 ++-
clang/lib/Sema/SemaChecking.cpp | 2 +-
clang/test/AST/ByteCode/builtin-functions.cpp | 1 +
clang/test/CodeGen/builtins.c | 1 +
clang/test/CodeGenCXX/builtins.cpp | 21 ++++++++++++++-----
clang/test/Sema/constant-builtins-2.c | 1 +
clang/test/Sema/constant-builtins.c | 1 +
clang/test/SemaCXX/builtin-bswapg.cpp | 1 +
9 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 4cf6898df3692..1cdfa30e8301b 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1048,7 +1048,7 @@ static bool interp__builtin_bswap(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const CallExpr *Call) {
const APSInt &Val = popToAPSInt(S, Call->getArg(0));
- if (Val.getBitWidth() == 8)
+ if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
pushInteger(S, Val, Call->getType());
else
pushInteger(S, Val.byteSwap(), Call->getType());
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 857688ed8039d..ed19335c83978 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -16170,7 +16170,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
APSInt Val;
if (!EvaluateInteger(E->getArg(0), Val, Info))
return false;
- if (Val.getBitWidth() == 8)
+ // if (Val.getBitWidth() <= 8)
+ if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
return Success(Val, E);
return Success(Val.byteSwap(), E);
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index e2e1b37572364..22a299f430a3a 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2249,7 +2249,7 @@ static bool BuiltinBswapg(Sema &S, CallExpr *TheCall) {
return true;
}
if (const auto *BT = dyn_cast<BitIntType>(ArgTy)) {
- if (BT->getNumBits() % 16 != 0 && BT->getNumBits() != 8) {
+ if (BT->getNumBits() % 16 != 0 && BT->getNumBits() != 8 && BT->getNumBits() != 1) {
S.Diag(Arg->getBeginLoc(), diag::err_bswapg_invalid_bit_width)
<< ArgTy << BT->getNumBits();
return true;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index b2f0213cfea05..10f7ff294ae00 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -841,6 +841,7 @@ namespace bswap {
int h7 = __builtin_bswapg(0x1234) == 0x3412 ? 1 : f();
int h8 = __builtin_bswapg(0x00001234) == 0x34120000 ? 1 : f();
int h9 = __builtin_bswapg(0x0000000000001234) == 0x3412000000000000 ? 1 : f();
+ int h9a = __builtin_bswapg(true) == true ? 1 : f();
#ifndef __AVR__
int h10 = __builtin_bswapg((_BitInt(8))0x12) == (_BitInt(8))0x12 ? 1 : f();
int h11 = __builtin_bswapg((_BitInt(16))0x1234) == (_BitInt(16))0x3412 ? 1 : f();
diff --git a/clang/test/CodeGen/builtins.c b/clang/test/CodeGen/builtins.c
index 3545588aec654..0d0104515235c 100644
--- a/clang/test/CodeGen/builtins.c
+++ b/clang/test/CodeGen/builtins.c
@@ -135,6 +135,7 @@ int main(void) {
P(object_size, (s0, 3));
// Whatever
+ P(bswapg, ((_Bool)N));
P(bswapg, ((char)N));
P(bswapg, ((short)N));
P(bswapg, ((int)N));
diff --git a/clang/test/CodeGenCXX/builtins.cpp b/clang/test/CodeGenCXX/builtins.cpp
index ef30a25a17922..ad0d06cdb2637 100644
--- a/clang/test/CodeGenCXX/builtins.cpp
+++ b/clang/test/CodeGenCXX/builtins.cpp
@@ -120,17 +120,28 @@ void test_char_reference(char& a) {
// CHECK-NOT: call i8 @llvm.bswap.i8
// CHECK: ret void
+void test_bool_reference(bool& a) {
+ __builtin_bswapg(a);
+}
+// CHECK-LABEL: @_Z19test_bool_referenceRb
+// CHECK: store ptr %a, ptr
+// CHECK: load ptr, ptr
+// CHECK-NOT: call i8 @llvm.bswap.i8
+// CHECK: ret void
+
void test_bitint() {
- _BitInt(8) a = 0x12;
+ bool a = true;
__builtin_bswapg(a);
- _BitInt(16) b = 0x1234;
+ _BitInt(8) b = 0x12;
__builtin_bswapg(b);
- _BitInt(32) c = 0x00001234;
+ _BitInt(16) c = 0x1234;
__builtin_bswapg(c);
- _BitInt(64) d = 0x0000000000001234;
+ _BitInt(32) d = 0x00001234;
__builtin_bswapg(d);
- _BitInt(128) e = ~(_BitInt(128))0;
+ _BitInt(64) e = 0x0000000000001234;
__builtin_bswapg(e);
+ _BitInt(128) f = ~(_BitInt(128))0;
+ __builtin_bswapg(f);
}
// CHECK-LABEL: @_Z11test_bitintv
// CHECK-NOT: call i8 @llvm.bswap.i8
diff --git a/clang/test/Sema/constant-builtins-2.c b/clang/test/Sema/constant-builtins-2.c
index 9579052b1f76b..e875863e7fa91 100644
--- a/clang/test/Sema/constant-builtins-2.c
+++ b/clang/test/Sema/constant-builtins-2.c
@@ -479,6 +479,7 @@ int h0 = __builtin_types_compatible_p(int, float);
int h3 = __builtin_bswap16(0x1234) == 0x3412 ? 1 : f();
int h4 = __builtin_bswap32(0x1234) == 0x34120000 ? 1 : f();
int h5 = __builtin_bswap64(0x1234) == 0x3412000000000000 ? 1 : f();
+int h5a = __builtin_bswapg((_Bool)(0x0)) == (_Bool)(0x0) ? 1 : f();
int h6 = __builtin_bswapg((char)(0x12)) == (char)(0x12) ? 1 : f();
int h7 = __builtin_bswapg((short)(0x1234)) == (short)(0x3412) ? 1 : f();
int h8 = __builtin_bswapg(0x00001234) == 0x34120000 ? 1 : f();
diff --git a/clang/test/Sema/constant-builtins.c b/clang/test/Sema/constant-builtins.c
index 6c13fe96b6b4a..565547a16dde5 100644
--- a/clang/test/Sema/constant-builtins.c
+++ b/clang/test/Sema/constant-builtins.c
@@ -25,6 +25,7 @@ int h0 = __builtin_types_compatible_p(int,float);
int h3 = __builtin_bswap16(0x1234) == 0x3412 ? 1 : f();
int h4 = __builtin_bswap32(0x1234) == 0x34120000 ? 1 : f();
int h5 = __builtin_bswap64(0x1234) == 0x3412000000000000 ? 1 : f();
+int h5a = __builtin_bswapg((_Bool)1) == (_Bool)1 ? 1 : f();
int h6 = __builtin_bswapg((char)0x12) == (char)0x12 ? 1 : f();
int h7 = __builtin_bswapg((short)(0x1234)) == (short)(0x3412) ? 1 : f();
int h8 = __builtin_bswapg(0x00001234) == 0x34120000 ? 1 : f();
diff --git a/clang/test/SemaCXX/builtin-bswapg.cpp b/clang/test/SemaCXX/builtin-bswapg.cpp
index 539390fa9b606..815cc0085f89e 100644
--- a/clang/test/SemaCXX/builtin-bswapg.cpp
+++ b/clang/test/SemaCXX/builtin-bswapg.cpp
@@ -2,6 +2,7 @@
// RUN: %clang_cc1 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
void test_basic_type_checks() {
+ static_assert(__is_same(bool, decltype(__builtin_bswapg((bool)0))), "");
static_assert(__is_same(char, decltype(__builtin_bswapg((char)0))), "");
static_assert(__is_same(unsigned char, decltype(__builtin_bswapg((unsigned char)0))), "");
static_assert(__is_same(short, decltype(__builtin_bswapg((short)0))), "");
>From a77af09bdd957b356a1efb97474dc6e929514311 Mon Sep 17 00:00:00 2001
From: woruyu <1214539920 at qq.com>
Date: Mon, 2 Feb 2026 15:36:18 +0800
Subject: [PATCH 2/3] fix: format
---
clang/lib/Sema/SemaChecking.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 22a299f430a3a..9fe759664481b 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2249,7 +2249,8 @@ static bool BuiltinBswapg(Sema &S, CallExpr *TheCall) {
return true;
}
if (const auto *BT = dyn_cast<BitIntType>(ArgTy)) {
- if (BT->getNumBits() % 16 != 0 && BT->getNumBits() != 8 && BT->getNumBits() != 1) {
+ if (BT->getNumBits() % 16 != 0 && BT->getNumBits() != 8 &&
+ BT->getNumBits() != 1) {
S.Diag(Arg->getBeginLoc(), diag::err_bswapg_invalid_bit_width)
<< ArgTy << BT->getNumBits();
return true;
>From b09dbe536deef1704d5eb5ddf037d14b95b6c3a0 Mon Sep 17 00:00:00 2001
From: woruyu <1214539920 at qq.com>
Date: Mon, 2 Feb 2026 16:29:36 +0800
Subject: [PATCH 3/3] Update clang/lib/AST/ExprConstant.cpp
Co-authored-by: Cheng Lingfei <53817093+clingfei at users.noreply.github.com>
---
clang/lib/AST/ExprConstant.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index ed19335c83978..585460abdbc27 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -16170,7 +16170,6 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
APSInt Val;
if (!EvaluateInteger(E->getArg(0), Val, Info))
return false;
- // if (Val.getBitWidth() <= 8)
if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
return Success(Val, E);
More information about the cfe-commits
mailing list