Skip to content

Conversation

@ebinjose02
Copy link
Contributor

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

github-actions bot commented Nov 28, 2025

🐧 Linux x64 Test Results

  • 111776 tests passed
  • 4481 tests skipped

✅ The build succeeded and all tests passed.

Prevents assertion in CGBuiltin for i1 - returns identity.
Created test file for the same.
// RUN: %clang_cc1 -triple x86_64 -O0 -emit-llvm -o - %s | FileCheck %s
#include <stdbool.h>
bool test_bswapg(bool c) {
return __builtin_bswapg(c);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some existing test file where we can add this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a testfile that check __builtin_bswapg along with other builtins (test/codegen/builtins.c) . I can modify this file.

@@ -3583,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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if (IntTy->getBitWidth() == 1 ||IntTy->getBitWidth() == 8)?

Added test to an already existin test file
Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@efriedma-quic
Copy link
Collaborator

It looks like this isn't merging cleanly; please update your branch.

@ebinjose02
Copy link
Contributor Author

It looks like this isn't merging cleanly; please update your branch.

There were no conflicts showing up. Still I have updated the ticket. Thanks

@efriedma-quic efriedma-quic merged commit 74f49a2 into llvm:main Dec 5, 2025
10 checks passed
@github-actions
Copy link

github-actions bot commented Dec 5, 2025

@ebinjose02 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

honeygoyal pushed a commit to honeygoyal/llvm-project that referenced this pull request Dec 9, 2025
Prevents assertion in CGBuiltin for i1 - returns identity. Created test
file for the same.

Fixes llvm#168690.
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

3 participants