This is an archive of the discontinued LLVM Phabricator instance.

[Driver][Sparc] Default to -mcpu=v9 for SparcV8 on Linux
Needs ReviewPublic

Authored by ro on Jul 28 2022, 1:07 AM.

Details

Summary

This is the Debian/sparc64 equivalent of D86621: since that distro only supports SPARC V9 CPUs, it should default to -mcpu=v9, among others to allow inlining of atomics even in 32-bit code.

Tested on sparc64-pc-linux-gnu, sparcv9-sun-solaris2.11, and x86_64-pc-linux-gnu.

There's currently one issue, though: while this is the right thing for Debian/sparc64 (I don't think Debian/sparc which ran on non-V9 CPUs is supported any longer), it's wrong for current Linux distributions for 32-bit CPUs (LEON Linux AFAIK, there may be others). While the driver can distringuish between Debian and non-Debian distributions for that, I've found no way to do so in the testsuite. Thus, clang/test/Preprocessor/predefined-arch-macros.c currently FAILs the CHECK_SPARC tests: it needs to check for __sparc_v9__ on Debian, but keep __sparcv8 on others. Any suggestions on how to handle this?

Diff Detail

Event Timeline

ro created this revision.Jul 28 2022, 1:07 AM
ro requested review of this revision.Jul 28 2022, 1:07 AM
glaubitz added inline comments.Jul 28 2022, 7:41 AM
clang/lib/Driver/ToolChains/Arch/Sparc.cpp
139

Can we do it "IsLinux()" instead of "IsDebian()"?

I think Gentoo should profit from this change as well.

ro added inline comments.Jul 28 2022, 10:01 AM
clang/lib/Driver/ToolChains/Arch/Sparc.cpp
139

No, for the reason I mentioned: there are Linux distributions with SPARC support that don't support SPARC v9, like LEON Linux. Solaris is easy here because it's v9-only.

LLVM explicitly has extensive LEON support, so we shouldn't break it there!

I don't really known which Linux distributions which are considered supported by current LLVM, have SPARC support at all and require v9 CPUs. That's for the community to tell.

Making the behavior different for different Linux distributions is not great, but if it matches the practice, I think it is fine.
I don't know Sparc enough to suggest anything for 32-bit, though...

ro added a comment.Jul 29 2022, 12:39 AM

Making the behavior different for different Linux distributions is not great, but if it matches the practice, I think it is fine.

It's the best we can do in clang, I fear, due to the compiler's desire to decide as much as possible at runtime. GCC is way more static here, and whoever builds it can decide on the default CPU with --with-cpu=v9 e.g. at configure time.

I don't know Sparc enough to suggest anything for 32-bit, though...

An alternative would be to just check the host CPU with getHostCPUName as updated by D130272. That CPU name then needs to be checked if it supports the V9 ISA. That could be done using getCPUGeneration(CPU) == CG_V9 in clang/lib/Basic/Targets/Sparc.h. However, SparcTargetInfo (or TargetInfo in general) is not currently used inside the driver code.

Of course, this only works in the native case, but that's true for distro checks, too.

ro added a comment.Aug 18 2022, 2:39 AM

It's almost three weeks since the last comments. Any suggestions on how to proceed with this patch? Given that the original issue (atomics not inlined with -m32) is now worked around by always linking with --as-needed -latomic --no-as-needed, the patch is no longer needed to fix build failures. However, it still would make a nice and cheap optimization on distros that require v9 CPUs, if we can figure out how to reliably identify them. Maybe someone from the LEON community could chime in?

In D130688#3731577, @ro wrote:

It's almost three weeks since the last comments. Any suggestions on how to proceed with this patch? Given that the original issue (atomics not inlined with -m32) is now worked around by always linking with --as-needed -latomic --no-as-needed, the patch is no longer needed to fix build failures. However, it still would make a nice and cheap optimization on distros that require v9 CPUs, if we can figure out how to reliably identify them. Maybe someone from the LEON community could chime in?

Yeah, someone from the LEON community should comment whether they would be OK to default to V9 on all Linux targets. I don't really want to omit Gentoo here which still support Linux on SPARC.

ro added a comment.Aug 18 2022, 2:49 AM

Yeah, someone from the LEON community should comment whether they would be OK to default to V9 on all Linux targets. I don't really want to omit Gentoo here which still support Linux on SPARC.

How could they? LEON is V8 (with extensions) only!

In D130688#3731619, @ro wrote:

Yeah, someone from the LEON community should comment whether they would be OK to default to V9 on all Linux targets. I don't really want to omit Gentoo here which still support Linux on SPARC.

How could they? LEON is V8 (with extensions) only!

Well, they could patch LLVM locally. I assume they don't use the pristine upstream sources anyway in such custom setups.

ro added a comment.Aug 18 2022, 3:25 AM
In D130688#3731619, @ro wrote:

Yeah, someone from the LEON community should comment whether they would be OK to default to V9 on all Linux targets. I don't really want to omit Gentoo here which still support Linux on SPARC.

How could they? LEON is V8 (with extensions) only!

Well, they could patch LLVM locally. I assume they don't use the pristine upstream sources anyway in such custom setups.

Why should they need to? AFAICS all the LEON CPU support is upstream already, so why break upstream for them without need?