[llvm] [SPARC] Use lzcnt to implement CTLZ when we have VIS3 (PR #135715)

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 21 19:06:07 PDT 2025


koachan wrote:

> gcc resolves __builtin_clz to __clzdi2 rather than __clzsi2

IIRC at least on sparc64 it's because GCC's runtime doesn't seem to ship with `__clzsi2`, for some reason.

> It does. I'm attaching disassembly without and with this patch.
> 
> [556111.dis.txt](https://github.com/user-attachments/files/19837305/556111.dis.txt) [556112.dis.txt](https://github.com/user-attachments/files/19837306/556112.dis.txt)

Hmmm so upon reading the clzdi2.c source, it seems to rely on `clzsi`, which resolves to `__builtin_clz`.
```
// clzdi2.c
  return clzsi((x.s.high & ~f) | (x.s.low & f)) +
         (f & ((si_int)(sizeof(si_int) * CHAR_BIT)));

// int_types.h
#define clzsi __builtin_clz
```
And now that `__builtin_clz` gets lowered back to `__clzdi2` the implementation ends up being an unbounded recursive call.
I'll try to remove the clang check in the source and see if that helps with things.

https://github.com/llvm/llvm-project/pull/135715


More information about the llvm-commits mailing list