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..1a7b8f5f0dba1 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -2551,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 b06339f594257..a56bc4877eb33 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..710ce40f1c3ca 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -487,6 +487,12 @@ 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. Enabling both `packed-stack` and +`backchain` attributes is incompatible with the default hard-float ABI. +Enable soft-float ABI to use these attributes. + ## panic This option lets you control what happens when the code panics.