Skip to content

Add -Zmips-nan2008 to enable IEEE 754-2008 NaN encoding on MIPS#159565

Draft
fo40225 wants to merge 1 commit into
rust-lang:mainfrom
fo40225:fix_mips_nan2008_tm
Draft

Add -Zmips-nan2008 to enable IEEE 754-2008 NaN encoding on MIPS#159565
fo40225 wants to merge 1 commit into
rust-lang:mainfrom
fo40225:fix_mips_nan2008_tm

Conversation

@fo40225

@fo40225 fo40225 commented Jul 19, 2026

Copy link
Copy Markdown

Summary

Add a new unstable compiler flag -Zmips-nan2008 that enables the IEEE 754-2008 NaN encoding on MIPS targets by injecting the LLVM +nan2008 target feature.

Motivation

MIPS hardware and software ecosystems can be built with either the legacy MIPS NaN encoding or the IEEE 754-2008 NaN encoding. GCC's -mnan=2008 flag selects the 2008 encoding, and some system ABIs (particularly for MIPS Release 6 cores and certain vendor toolchains) mandate it. When linking Rust code against C libraries or system code compiled with -mnan=2008, the NaN encoding must match across the entire binary to avoid runtime failures and undefined behavior.

Currently, Rust has no way to select the 2008 NaN encoding, making it impossible to interoperate with such ecosystems without resorting to custom target JSON specifications. This PR adds an explicit, architecture-gated unstable flag to enable the feature safely.

What this PR does

  • New flag -Zmips-nan2008 (unstable): when set, injects +nan2008 into the LLVM feature string for MIPS targets. The flag is only accepted on mips, mips32r6, mips64, and mips64r6 architectures; attempting to use it on other architectures emits a fatal diagnostic (MipsNan2008InvalidArch).
  • Target modifier: the option is registered with [TRACKED] { TARGET_MODIFIER: MipsNan2008 } in options.rs, so linking crates compiled with different values is a hard error by default (leveraging the existing target-modifier machinery). The error can be bypassed with -Cunsafe-allow-abi-mismatch=mips-nan2008 if the user knows the ABIs are actually compatible.
  • LLVM feature injection: in llvm_util.rs, the llvm_features_by_flags() function now checks sess.opts.unstable_opts.mips_nan2008 and appends +nan2008 to the feature vector when the target architecture is a MIPS variant; otherwise it emits the fatal diagnostic.
  • Docs: unstable-book page at compiler-flags/mips-nan2008.md with usage examples and ABI compatibility warnings.

Tests

  • tests/ui/target_modifiers/incompatible_mips_nan2008.rs (+ auxiliary crate mips_nan2008.rs): verifies the target-modifier mismatch error is emitted when linking crates with conflicting -Zmips-nan2008 settings, the error can be suppressed with -Cunsafe-allow-abi-mismatch=mips-nan2008, and matching settings compile cleanly. Mirrors the structure of incompatible_fixedx18.rs.
  • tests/codegen-llvm/mips-nan2008.rs: checks that the +nan2008 feature appears (or does not appear) in the LLVM "target-features" function attribute depending on whether the flag is set.
  • Updated compiler/rustc_interface/src/tests.rs to track the new mips_nan2008 option in the tracked-options test.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 19, 2026
@rust-log-analyzer

This comment has been minimized.

Add a new unstable flag -Zmips-nan2008 that injects the LLVM nan2008
subtarget feature on MIPS targets, selecting the IEEE 754-2008 NaN
encoding instead of the legacy MIPS encoding. This makes it possible to
link against MIPS libraries and toolchains built with GCC's -mnan=2008.

The option is registered as a target modifier, so mixing crates
compiled with different values of the flag is a hard error at link
time, bypassable with -Cunsafe-allow-abi-mismatch=mips-nan2008. Using
the flag on non-MIPS architectures is a fatal error.
@fo40225
fo40225 force-pushed the fix_mips_nan2008_tm branch from 4a684cf to 5b0f115 Compare July 19, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants