Skip to content

Commit 25cdfcd

Browse files
ebinjose02Honey Goyal
authored andcommitted
[clang codegen] Fix __builtin_bswapg with bool operand (llvm#169285)
Prevents assertion in CGBuiltin for i1 - returns identity. Created test file for the same. Fixes llvm#168690.
1 parent 4e175ba commit 25cdfcd

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,12 +3583,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
35833583
Value *ArgValue = EmitScalarExpr(E->getArg(0));
35843584
llvm::IntegerType *IntTy = cast<llvm::IntegerType>(ArgValue->getType());
35853585
assert(IntTy && "LLVM's __builtin_bswapg only supports integer variants");
3586-
assert(((IntTy->getBitWidth() % 16 == 0 && IntTy->getBitWidth() != 0) ||
3587-
IntTy->getBitWidth() == 8) &&
3586+
if (IntTy->getBitWidth() == 1 || IntTy->getBitWidth() == 8)
3587+
return RValue::get(ArgValue);
3588+
assert(((IntTy->getBitWidth() % 16 == 0 && IntTy->getBitWidth() != 0)) &&
35883589
"LLVM's __builtin_bswapg only supports integer variants that has a "
35893590
"multiple of 16 bits as well as a single byte");
3590-
if (IntTy->getBitWidth() == 8)
3591-
return RValue::get(ArgValue);
35923591
return RValue::get(
35933592
emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::bswap));
35943593
}

clang/test/CodeGen/builtins.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,10 @@ void test_builtin_ctzg(unsigned char uc, unsigned short us, unsigned int ui,
916916

917917
#endif
918918

919+
#include <stdbool.h>
919920
// CHECK-LABEL: define{{.*}} void @test_builtin_bswapg
920921
void test_builtin_bswapg(unsigned char uc, unsigned short us, unsigned int ui,
921-
unsigned long ul, unsigned long long ull,
922+
unsigned long ul, unsigned long long ull, bool b,
922923
#ifdef __SIZEOF_INT128__
923924
unsigned __int128 ui128,
924925
#endif
@@ -929,9 +930,14 @@ void test_builtin_bswapg(unsigned char uc, unsigned short us, unsigned int ui,
929930
int x = 0;
930931
x = x * 2;
931932
#endif
933+
b = __builtin_bswapg(b);
934+
// CHECK: %{{.*}} = load i8, ptr %b.addr
935+
// CHECK: %{{.*}} = trunc i8 %{{.*}} to i1
936+
// CHECK: %{{.*}} = zext i1 %{{.*}} to i8
937+
// CHECK: store i8 %{{.*}}, ptr %b.addr
932938
uc = __builtin_bswapg(uc);
933-
// CHECK: %1 = load i8, ptr %uc.addr
934-
// CHECK: store i8 %1, ptr %uc.addr
939+
// CHECK: %{{.*}} = load i8, ptr %uc.addr
940+
// CHECK: store i8 %{{.*}}, ptr %uc.addr
935941
us = __builtin_bswapg(us);
936942
// CHECK: call i16 @llvm.bswap.i16
937943
ui = __builtin_bswapg(ui);

0 commit comments

Comments
 (0)