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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

koachan
Copy link
Contributor

@koachan koachan commented Apr 22, 2025

On 64-bit platforms, libgcc doesn't ship with __clzsi2, so __builtin_clz gets lowered to __clzdi2. A check already exists for GCC, but as of commit 8210ca0 clang also lowers __builtin_clz to __clzdi2 on sparc64.

Update the check so that building __clzdi2 with clang/sparc64 also works.

…parc64

On 64-bit platforms, libgcc doesn't ship with __clzsi2, so __builtin_clz
gets lowered to __clzdi2. A check already exists for GCC, but as of
commit 8210ca0 clang also lowers __builtin_clz
to __clzdi2 on sparc64.

Update the check so that building __clzdi2 with clang/sparc64 also works.
@koachan
Copy link
Contributor Author

koachan commented Apr 22, 2025

Pinging @wzssyqa and @topperc too since it appears that MIPS and RISC-V also use this check, just to make sure that this PR won't break things there.

(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
// ctz instruction, resolves __builtin_clz resolves to __clzdi2 rather than
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// ctz instruction, resolves __builtin_clz resolves to __clzdi2 rather than
// ctz instruction, `__builtin_clz` resolves to `__clzdi2` rather than

Comment on lines 21 to 22
// __clzsi2, leading to infinite recursion.
// This is because on those platforms, libgcc doesn't ship with __clzsi2.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// __clzsi2, leading to infinite recursion.
// This is because on those platforms, libgcc doesn't ship with __clzsi2.
// `__clzsi2` as libgcc does not ship with `__clzsi2`, leading to infinite recursion.

Comment on lines +20 to +22
// ctz instruction, `__builtin_clz` resolves to `__clzdi2` rather than
// __clzsi2 as libgcc does not ship with `__clzsi2`, leading to infinite
// recursion.
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.

@s-barannikov
Copy link
Contributor

s-barannikov commented Apr 23, 2025

Note that __ctzdi2 has a similar workaround. Currently, it doesn't call itself recursively, but it will start doing so in the future, once we start promoting 32-bit CTTZ_ZERO_UNDEF.

@koachan
Copy link
Contributor Author

koachan commented Apr 23, 2025

Note that __ctzdi2 has a similar workaround. Currently, it doesn't call itself recursively, but it will start doing so in the future, once we start promoting 32-bit CTTZ_ZERO_UNDEF.

Noted, will do another PR for it after this lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants