Skip to content

Conversation

@ebinjose02
Copy link

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

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Nov 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2025

@llvm/pr-subscribers-clang

Author: Ebin Jose (ebinjose02)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/169285.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGBuiltin.cpp (+2)
  • (added) clang/test/CodeGen/builtin_bswapg.cpp (+8)
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<llvm::IntegerType>(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.cpp b/clang/test/CodeGen/builtin_bswapg.cpp
new file mode 100644
index 0000000000000..8e9af98a58d35
--- /dev/null
+++ b/clang/test/CodeGen/builtin_bswapg.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple x86_64 -O0 -emit-llvm -o - %s | FileCheck %s
+
+auto test_bswapg(bool c) {
+  return __builtin_bswapg(c);
+}
+
+// CHECK-LABEL: define{{.*}} i1 @_Z11test_bswapgb(
+// CHECK: ret i1 %{{.*}}

@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2025

@llvm/pr-subscribers-clang-codegen

Author: Ebin Jose (ebinjose02)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/169285.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGBuiltin.cpp (+2)
  • (added) clang/test/CodeGen/builtin_bswapg.cpp (+8)
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<llvm::IntegerType>(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.cpp b/clang/test/CodeGen/builtin_bswapg.cpp
new file mode 100644
index 0000000000000..8e9af98a58d35
--- /dev/null
+++ b/clang/test/CodeGen/builtin_bswapg.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple x86_64 -O0 -emit-llvm -o - %s | FileCheck %s
+
+auto test_bswapg(bool c) {
+  return __builtin_bswapg(c);
+}
+
+// CHECK-LABEL: define{{.*}} i1 @_Z11test_bswapgb(
+// CHECK: ret i1 %{{.*}}

@github-actions
Copy link

🐧 Linux x64 Test Results

  • 84739 tests passed
  • 1104 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

Clang

Clang.CodeGen/builtin_bswapg.c
Exit Code: 2

Command Output (stdout):
--
# RUN: at line 1
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang -cc1 -internal-isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lib/clang/22/include -nostdsysteminc -triple x86_64 -O0 -emit-llvm -o - /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c | /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang -cc1 -internal-isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lib/clang/22/include -nostdsysteminc -triple x86_64 -O0 -emit-llvm -o - /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c
# .---command stderr------------
# | /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c:3:18: error: unknown type name 'bool'
# |     3 | auto test_bswapg(bool c) {
# |       |                  ^
# | /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c:3:6: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
# |     3 | auto test_bswapg(bool c) {
# |       | ~~~~ ^
# |       | int
# | /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c:3:1: error: illegal storage class on function
# |     3 | auto test_bswapg(bool c) {
# |       | ^
# | 3 errors generated.
# `-----------------------------
# error: command failed with exit status: 1
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c
# .---command stderr------------
# | FileCheck error: '<stdin>' is empty.
# | FileCheck command line:  /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/FileCheck /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/CodeGen/builtin_bswapg.c
# `-----------------------------
# error: command failed with exit status: 2

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

Prevents assertion in CGBuiltin for i1 - returns identity.
Created test file for the same.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clang] __builtin_bswapg crashes on bool

2 participants