From 4baf6aac54499c5fdbeab33a4dc956487b4b5c02 Mon Sep 17 00:00:00 2001 From: ebinjose02 Date: Mon, 24 Nov 2025 07:02:26 +0000 Subject: [PATCH 1/2] Fixes #168690 Prevents assertion in CGBuiltin for i1 - returns identity. Created test file for the same. --- clang/lib/CodeGen/CGBuiltin.cpp | 2 ++ clang/test/CodeGen/builtin_bswapg.c | 8 ++++++++ 2 files changed, 10 insertions(+) create mode 100644 clang/test/CodeGen/builtin_bswapg.c diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 93f691e4c2267..8bcc2c8e7d8fe 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -3653,6 +3653,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, Value *ArgValue = EmitScalarExpr(E->getArg(0)); llvm::IntegerType *IntTy = cast(ArgValue->getType()); assert(IntTy && "LLVM's __builtin_bswapg only supports integer variants"); + if (IntTy->getBitWidth() == 1) + return RValue::get(ArgValue); assert(((IntTy->getBitWidth() % 16 == 0 && IntTy->getBitWidth() != 0) || IntTy->getBitWidth() == 8) && "LLVM's __builtin_bswapg only supports integer variants that has a " diff --git a/clang/test/CodeGen/builtin_bswapg.c b/clang/test/CodeGen/builtin_bswapg.c new file mode 100644 index 0000000000000..35f4728d15d3a --- /dev/null +++ b/clang/test/CodeGen/builtin_bswapg.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -triple x86_64 -O0 -emit-llvm -o - %s | FileCheck %s +#include +bool test_bswapg(bool c) { + return __builtin_bswapg(c); +} + +// CHECK-LABEL: define{{.*}} i1 @_Z11test_bswapgb( +// CHECK: ret i1 %{{.*}} From 2da2524e161a8d84dc1a3cdd9de6350b33830502 Mon Sep 17 00:00:00 2001 From: ebinjose02 Date: Tue, 2 Dec 2025 06:28:01 +0000 Subject: [PATCH 2/2] Modified CGBuiltins Added test to an already existin test file --- clang/lib/CodeGen/CGBuiltin.cpp | 7 ++----- clang/test/CodeGen/builtin_bswapg.c | 8 -------- clang/test/CodeGen/builtins.c | 12 +++++++++--- 3 files changed, 11 insertions(+), 16 deletions(-) delete mode 100644 clang/test/CodeGen/builtin_bswapg.c diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 8bcc2c8e7d8fe..d640bd79553ad 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -3653,14 +3653,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, Value *ArgValue = EmitScalarExpr(E->getArg(0)); llvm::IntegerType *IntTy = cast(ArgValue->getType()); assert(IntTy && "LLVM's __builtin_bswapg only supports integer variants"); - if (IntTy->getBitWidth() == 1) + if (IntTy->getBitWidth() == 1 || IntTy->getBitWidth() == 8) return RValue::get(ArgValue); - assert(((IntTy->getBitWidth() % 16 == 0 && IntTy->getBitWidth() != 0) || - IntTy->getBitWidth() == 8) && + assert(((IntTy->getBitWidth() % 16 == 0 && IntTy->getBitWidth() != 0)) && "LLVM's __builtin_bswapg only supports integer variants that has a " "multiple of 16 bits as well as a single byte"); - if (IntTy->getBitWidth() == 8) - return RValue::get(ArgValue); return RValue::get( emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::bswap)); } diff --git a/clang/test/CodeGen/builtin_bswapg.c b/clang/test/CodeGen/builtin_bswapg.c deleted file mode 100644 index 35f4728d15d3a..0000000000000 --- a/clang/test/CodeGen/builtin_bswapg.c +++ /dev/null @@ -1,8 +0,0 @@ -// RUN: %clang_cc1 -triple x86_64 -O0 -emit-llvm -o - %s | FileCheck %s -#include -bool test_bswapg(bool c) { - return __builtin_bswapg(c); -} - -// CHECK-LABEL: define{{.*}} i1 @_Z11test_bswapgb( -// CHECK: ret i1 %{{.*}} diff --git a/clang/test/CodeGen/builtins.c b/clang/test/CodeGen/builtins.c index a97e8932acf61..3545588aec654 100644 --- a/clang/test/CodeGen/builtins.c +++ b/clang/test/CodeGen/builtins.c @@ -916,9 +916,10 @@ void test_builtin_ctzg(unsigned char uc, unsigned short us, unsigned int ui, #endif +#include // CHECK-LABEL: define{{.*}} void @test_builtin_bswapg void test_builtin_bswapg(unsigned char uc, unsigned short us, unsigned int ui, - unsigned long ul, unsigned long long ull, + unsigned long ul, unsigned long long ull, bool b, #ifdef __SIZEOF_INT128__ unsigned __int128 ui128, #endif @@ -929,9 +930,14 @@ void test_builtin_bswapg(unsigned char uc, unsigned short us, unsigned int ui, int x = 0; x = x * 2; #endif + b = __builtin_bswapg(b); + // CHECK: %{{.*}} = load i8, ptr %b.addr + // CHECK: %{{.*}} = trunc i8 %{{.*}} to i1 + // CHECK: %{{.*}} = zext i1 %{{.*}} to i8 + // CHECK: store i8 %{{.*}}, ptr %b.addr uc = __builtin_bswapg(uc); - // CHECK: %1 = load i8, ptr %uc.addr - // CHECK: store i8 %1, ptr %uc.addr + // CHECK: %{{.*}} = load i8, ptr %uc.addr + // CHECK: store i8 %{{.*}}, ptr %uc.addr us = __builtin_bswapg(us); // CHECK: call i16 @llvm.bswap.i16 ui = __builtin_bswapg(ui);