Skip to content
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

Fix soft float support, split musl triples by float ABI, and enable CI #21269

Merged
merged 15 commits into from
Sep 12, 2024

Conversation

alexrp
Copy link
Contributor

@alexrp alexrp commented Aug 31, 2024

This is the first round of soft float work that I'm doing.

There are 3 goals of this PR:

  1. Get soft float actually working without generating unexpected FPU code.
  2. Split musl target triples into soft float and hard float where relevant.
  3. Start testing soft float targets in CI.

I'm completely ignoring -mfloat-abi=softfp support (soft float ABI with FPU computations) and in fact intentionally suppressing it in the LLVM backend as a result of the use-soft-float change. The only thing that matters for this first patch set is correctness; the next round of patches will re-enable support for this optimization in a way that actually functions correctly.

Note that a core premise of my work in this area is that, if the user specifies a soft float target triple, the default assumption should be that no FPU code is generated, even if the target CPU model happens to have FPU features. There's a very strong established precedent for this in the C/C++ world where you have to explicitly opt into this behavior by using -mfloat-abi=softfp instead of -mfloat-abi=soft (the latter being implied by the soft float triple). But even without that, everyone understands e.g. arm-linux-gnueabi to mean soft float, and it's just astonishing as a user if the resulting binary from zig build -Dtarget=arm-linux-gnueabi has FPU instructions in it and can't run on a fully soft float system.

I initially thought that adding soft float targets to CI wasn't necessary, but the state of our soft float support was broken enough that I've changed my mind on this, so this PR also includes those test additions. Let's see if it causes a noticeable slowdown, and if so, we can maybe limit it to Arm or something. (As usual, the MIPS O32 targets are gated by -Dtest-slow-targets which is off by default both locally and in CI.) This branch passes zig build test -fqemu -fwasmtime --glibc-runtimes <...> -Dtest-slow-targets locally for me with all the added targets. Finally, I also changed the Arm target triples for tests to use v7 instead of v8 because a) v7 is much more common for 32-bit Arm, and b) v8 adds floating point features that don't exist in v7 so things were broken for v7 and it went unnoticed.

Closes #10961.
Closes #21184.

@alexrp alexrp changed the title Soft float Fix soft float support, split musl triples by float ABI, and enable CI Aug 31, 2024
@@ -1442,19 +1442,22 @@ test "store vector with memset" {
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

if (builtin.zig_backend == .stage2_llvm) {
// LLVM 16 ERROR: "Converting bits to bytes lost precision"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this test is being re-enabled for all targets in #21183.

@alexrp
Copy link
Contributor Author

alexrp commented Aug 31, 2024

Just to give an idea of the direction I'm working towards, the next PR will include:

  • A proper mechanism for separating out certain "CPU" features from std.Target that really represent ABI, codegen, or internal LLVM features that the user shouldn't be able to modify since there are proper frontend flags for them. This would include soft_float, hard_float, and hard_float_abi, among other things.
    • This also implies that we wouldn't default these options to off as we do for every CPU feature today. See the special case for soft_float I added in this PR for why status quo can get a bit awkward for these.
  • Frontend flags for controlling FPU codegen, roughly equivalent to -mfloat-abi and -mno-implicit-float. Maybe we can combine these into one flag? Not sure yet.
  • Re-enablement of softfp support in the LLVM backend based on the above.

@alexrp
Copy link
Contributor Author

alexrp commented Sep 1, 2024

Seems like the CI impact here is within reason.

Comment on lines +3112 to +3125
// `use-soft-float` means "use software routines for floating point computations". In
// other words, it configures how LLVM lowers basic float instructions like `fcmp`,
// `fadd`, etc. The float calling convention is configured on `TargetMachine` and is
// mostly an orthogonal concept, although obviously we do need hardware float operations
// to actually be able to pass float values in float registers.
//
// Ideally, we would support something akin to the `-mfloat-abi=softfp` option that GCC
// and Clang support for Arm32 and CSKY. We don't currently expose such an option in
// Zig, and using CPU features as the source of truth for this makes for a miserable
// user experience since people expect e.g. `arm-linux-gnueabi` to mean full soft float
// unless the compiler has explicitly been told otherwise. (And note that our baseline
// CPU models almost all include FPU features!)
//
// Revisit this at some point.
Copy link
Member

@andrewrk andrewrk Sep 12, 2024

Choose a reason for hiding this comment

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

What makes sense to me is:

  • float ABI in the target triple
  • whether hard float instructions can be used in the CPU featureset

Furthermore, baseline for soft float ABI target triples can default to not using float instructions in the CPU features.

it's unclear to me if this comment is in agreement with this or not.

@andrewrk andrewrk merged commit 4fba733 into ziglang:master Sep 12, 2024
10 checks passed
@andrewrk
Copy link
Member

Thanks for working on this. So far I'm unconvinced that Zig needs another option for controlling the ABI or CPU features. Based on your description it seems to me like this use case can be fully addressed by adjusting the baseline CPU featureset that is selected for soft float ABI targets.

@alexrp alexrp deleted the soft-float branch September 12, 2024 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants