From 30628cad224aeeaaa5fc5a655742e5e712bad77f Mon Sep 17 00:00:00 2001 From: "Eddy (Eduard) Stefes" Date: Fri, 28 Nov 2025 18:48:54 +0100 Subject: [PATCH 1/2] added support for s390x target features `soft-float` and `packed-stack` --- compiler/rustc_codegen_llvm/src/attributes.rs | 16 ++++++++++++++++ compiler/rustc_session/src/options.rs | 2 ++ compiler/rustc_target/src/spec/mod.rs | 11 +++++++++++ compiler/rustc_target/src/target_features.rs | 12 +++++++----- src/doc/rustc/src/codegen-options/index.md | 5 +++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index a25ce9e5a90ac..84d79037c3b16 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -311,6 +311,18 @@ fn backchain_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attrib if found_positive { Some(llvm::CreateAttrString(cx.llcx, "backchain")) } else { None } } +fn packed_stack_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> { + if sess.target.arch != Arch::S390x { + return None; + } + + if sess.opts.cg.packed_stack { + Some(llvm::CreateAttrString(cx.llcx, "packed-stack")) + } else { + None + } +} + pub(crate) fn target_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> &'ll Attribute { let target_cpu = llvm_util::target_cpu(sess); llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu) @@ -525,6 +537,10 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>( if let Some(backchain) = backchain_attr(cx, sess) { to_add.push(backchain); } + if let Some(packed_stack) = packed_stack_attr(cx, sess) { + to_add.push(packed_stack); + } + to_add.extend(patchable_function_entry_attrs( cx, sess, diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 21fa3321a30ad..7a30fe669a481 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -2182,6 +2182,8 @@ options! { #[rustc_lint_opt_deny_field_access("use `Session::overflow_checks` instead of this field")] overflow_checks: Option = (None, parse_opt_bool, [TRACKED], "use overflow checks for integer arithmetic"), + packed_stack: bool = (false, parse_bool, [TRACKED], + "use packed stack frames (s390x only) (default: no)"), #[rustc_lint_opt_deny_field_access("use `Session::panic_strategy` instead of this field")] panic: Option = (None, parse_opt_panic_strategy, [TRACKED], "panic strategy to compile crate with"), diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index b06339f594257..3cc01ebf4c1ef 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -3242,6 +3242,17 @@ impl Target { )); } } + + // If both `packed-stack` and `backchain` are set we need to use soft-float ABI. + if self.arch == Arch::S390x + && features_enabled.contains("packed-stack") + && features_enabled.contains("backchain") + && !matches!(self.abi, Abi::SoftFloat) + { + return Err(format!( + "Enabling both, `packed-stack` and `backchain` attributes is incompatible with the hard-float ABI. Enable soft-float ABI to use these attributes." + )); + } } Ok(()) diff --git a/compiler/rustc_target/src/target_features.rs b/compiler/rustc_target/src/target_features.rs index 40cc4f40a3336..aff204e8f2e77 100644 --- a/compiler/rustc_target/src/target_features.rs +++ b/compiler/rustc_target/src/target_features.rs @@ -1211,11 +1211,13 @@ impl Target { } } Arch::S390x => { - // We don't currently support a softfloat target on this architecture. - // As usual, we have to reject swapping the `soft-float` target feature. - // The "vector" target feature does not affect the ABI for floats - // because the vector and float registers overlap. - FeatureConstraints { required: &[], incompatible: &["soft-float"] } + // On s390x only the hard-float ABI is valid. However for kernel compilation we need to + // allow soft-float if and only if the ABI is explicitly set to soft-float. + if matches!(self.abi, Abi::SoftFloat) { + FeatureConstraints { required: &["soft-float"], incompatible: &[] } + } else { + FeatureConstraints { required: &[], incompatible: &["soft-float"] } + } } _ => NOTHING, } diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md index f0f991ed0c909..820bab74ff068 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -487,6 +487,11 @@ one of the following values: If not specified, overflow checks are enabled if [debug-assertions](#debug-assertions) are enabled, disabled otherwise. +## packed-stack + +This flag enables packed StackFrames on s390x. +If backchain is also enabled, switch to the soft-float ABI. + ## panic This option lets you control what happens when the code panics. From ee0d4c28d45b2c302456d7cb86d73a1da9a4a0f9 Mon Sep 17 00:00:00 2001 From: "Eddy (Eduard) Stefes" Date: Thu, 8 Jan 2026 11:14:29 +0100 Subject: [PATCH 2/2] fixed wording and moved arg to unstable options --- compiler/rustc_session/src/options.rs | 4 ++-- compiler/rustc_target/src/spec/mod.rs | 2 +- src/doc/rustc/src/codegen-options/index.md | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 7a30fe669a481..1a7b8f5f0dba1 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -2182,8 +2182,6 @@ options! { #[rustc_lint_opt_deny_field_access("use `Session::overflow_checks` instead of this field")] overflow_checks: Option = (None, parse_opt_bool, [TRACKED], "use overflow checks for integer arithmetic"), - packed_stack: bool = (false, parse_bool, [TRACKED], - "use packed stack frames (s390x only) (default: no)"), #[rustc_lint_opt_deny_field_access("use `Session::panic_strategy` instead of this field")] panic: Option = (None, parse_opt_panic_strategy, [TRACKED], "panic strategy to compile crate with"), @@ -2553,6 +2551,8 @@ options! { "pass `-install_name @rpath/...` to the macOS linker (default: no)"), packed_bundled_libs: bool = (false, parse_bool, [TRACKED], "change rlib format to store native libraries as archives"), + packed_stack: bool = (false, parse_bool, [TRACKED], + "use packed stack frames (s390x only) (default: no)"), panic_abort_tests: bool = (false, parse_bool, [TRACKED], "support compiling tests with panic=abort (default: no)"), panic_in_drop: PanicStrategy = (PanicStrategy::Unwind, parse_panic_strategy, [TRACKED], diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 3cc01ebf4c1ef..a56bc4877eb33 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -3250,7 +3250,7 @@ impl Target { && !matches!(self.abi, Abi::SoftFloat) { return Err(format!( - "Enabling both, `packed-stack` and `backchain` attributes is incompatible with the hard-float ABI. Enable soft-float ABI to use these attributes." + "Enabling both `packed-stack` and `backchain` attributes is incompatible with the hard-float ABI. Enable soft-float ABI to use these attributes." )); } } diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md index 820bab74ff068..710ce40f1c3ca 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -489,8 +489,9 @@ If not specified, overflow checks are enabled if ## packed-stack -This flag enables packed StackFrames on s390x. -If backchain is also enabled, switch to the soft-float ABI. +This flag enables packed StackFrames on s390x. Enabling both `packed-stack` and +`backchain` attributes is incompatible with the default hard-float ABI. +Enable soft-float ABI to use these attributes. ## panic