[all-commits] [llvm/llvm-project] 5566c8: [Clang] Fix boolean vector bit counting on big-end...

Victor Campos via All-commits all-commits at lists.llvm.org
Tue Jul 14 01:36:10 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 5566c87df36cacb1f006535476108dc5e4c8c18f
      https://github.com/llvm/llvm-project/commit/5566c87df36cacb1f006535476108dc5e4c8c18f
  Author: Victor Campos <victor.campos at arm.com>
  Date:   2026-07-14 (Tue, 14 Jul 2026)

  Changed paths:
    M clang/lib/CodeGen/CGBuiltin.cpp
    M clang/test/CodeGen/builtins.c

  Log Message:
  -----------
  [Clang] Fix boolean vector bit counting on big-endian targets (#209146)

`__builtin_ctzg` and `__builtin_clzg` (count leading/trailing zeroes)
can accept boolean vectors as arguments. In this case, they operate in
logical vector lane order. However, `EmitBitCountExpr`, used to generate
the argument to those builtins, bitcasts boolean vectors directly to
integer bitfields, whose bit order is reversed on big-endian targets.

This caused libc's SIMD `find_first_set` and `find_last_set` helpers to
count lanes from the wrong end in big-endian mode. But the issue was
general and could affect any user.

This patch normalizes the integer representation with `llvm.bitreverse`
on big-endian targets before emitting bit-count intrinsics. On top of
that, the codegen test has been refactored to run on more targets and to
cover big-endian mode as well.

As a side effect of this change, and because of the way the code is
structured, the bit reverse operation also happens for
`__builtin_popcountg`. For this intrinsic, the integer bitfield's bit
order doesn't matter because it simply counts set bits. However, to make
this change only affect the count leading/trailing intrinsics would be
too intrusive, and in any case, with optimizations enabled, the
pointless bit reversing operation on popcount is optimized away. This
behaviour is covered in the test.

Assisted-by: codex. It was used for the test, not the implementation.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list