Skip to content

[compiler-rt] Make sure __clzdi2 doesn't call itself recursively on sparc64 #136737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler-rt/lib/builtins/clzdi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

// Returns: the number of leading 0-bits

#if !defined(__clang__) && \
((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) || \
#if ((defined(__sparc__) && defined(__arch64__)) || defined(__mips64) || \
(defined(__riscv) && __SIZEOF_POINTER__ >= 8))
// On 64-bit architectures with neither a native clz instruction nor a native
// ctz instruction, gcc resolves __builtin_clz to __clzdi2 rather than
// __clzsi2, leading to infinite recursion.
// ctz instruction, `__builtin_clz` resolves to `__clzdi2` rather than
// __clzsi2 as libgcc does not ship with `__clzsi2`, leading to infinite
// recursion.
Comment on lines +20 to +22
Copy link
Contributor

@s-barannikov s-barannikov Apr 23, 2025

Choose a reason for hiding this comment

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

That doesn't seem to be true on RISC-V/Mips (clang).
https://godbolt.org/z/z8Yd9dr5K

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, looking at the respective backends, seems like they are Expanding CTTZ instead of lowering it to a LibCall in absence of native instructions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could rewrite the check so that the clang check is omitted only if compiling on SPARC, but wouldn't that be too complicated?

Copy link
Contributor

@s-barannikov s-barannikov Apr 25, 2025

Choose a reason for hiding this comment

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

I'm fine with any solution as long as it doesn't break/pessimize other targets.
I think RISC-V will eventually require the same change, not sure about Mips.

#define __builtin_clz(a) __clzsi2(a)
extern int __clzsi2(si_int);
#endif
Expand Down
Loading