[clang] [clang]Implement the c23 stdc bit builtins (PR #185978)
NagaChaitanya Vellanki via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 10 10:46:19 PDT 2026
chaitanyav wrote:
> > > > Typically we don't bother to implement these unless libc starts using these, or needs them. Making builtins for these functions without the library needs them is kinda silly.
> > > > DO we have a request for these coming from the libc maintainers?
> > >
> > >
> > > I think they're important for two reasons: 1) we're going to want constexpr support for these for the same reason we want constexpr support for `strlen` in C so defining them as recognized library builtins is the way we do that, and 2) I think libc is going to want to have full support for bit-precise integer types and that's easier to support from a builtin currently. That said, CC @michaelrj-google for additional opinions
> >
> >
> > From the libc side having these builtins would be handy for both of the reasons Aaron mentioned. For optimization it would be helpful if the compiler could replace the libcall with a builtin, since things like `leading_zeros` can sometimes be reduced to a single instruction. Actually calling these bit functions as functions is unlikely to be optimal. CC: @enh-google
>
> We also should run this through our test suites to see if there is any discrepancy, or missing tests for edge cases.
@enh-google I have added those tests to the patch. Also, have verified the outputs with gcc.
```
╰─ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/15.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust,cobol --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.2.1 20260209 (GCC)
```
[stdbit_result.txt](https://github.com/user-attachments/files/26636938/stdbit_result.txt)
https://github.com/llvm/llvm-project/pull/185978
More information about the cfe-commits
mailing list