Author: Ebin Jose
Date: 2025-12-05T09:51:34-08:00
New Revision: 74f49a240edff4570189b139dc19a22b86ce54b7

URL: 
https://github.com/llvm/llvm-project/commit/74f49a240edff4570189b139dc19a22b86ce54b7
DIFF: 
https://github.com/llvm/llvm-project/commit/74f49a240edff4570189b139dc19a22b86ce54b7.diff

LOG: [clang codegen] Fix __builtin_bswapg with bool operand (#169285)

Prevents assertion in CGBuiltin for i1 - returns identity. Created test
file for the same.

Fixes #168690.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGBuiltin.cpp
    clang/test/CodeGen/builtins.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 86b0bc68f1525..01be374422d93 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3583,12 +3583,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const 
GlobalDecl GD, unsigned BuiltinID,
     Value *ArgValue = EmitScalarExpr(E->getArg(0));
     llvm::IntegerType *IntTy = cast<llvm::IntegerType>(ArgValue->getType());
     assert(IntTy && "LLVM's __builtin_bswapg only supports integer variants");
-    assert(((IntTy->getBitWidth() % 16 == 0 && IntTy->getBitWidth() != 0) ||
-            IntTy->getBitWidth() == 8) &&
+    if (IntTy->getBitWidth() == 1 || IntTy->getBitWidth() == 8)
+      return RValue::get(ArgValue);
+    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/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 <stdbool.h>
 // 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);


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

Reply via email to