Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,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;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe panic here given that the frontend should already have emitted an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm packed_stack_attr(...) is called every time llfn_attrs_from_instance is called indifferent of target arch.

And I think it would not be a good thing to add an arch check to llfn_attrs_from_instance as there are no other checks in that function. It blindly calls all possibilities and assumes that checks are already handled elsewhere.

}

if sess.opts.unstable_opts.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)
Expand Down Expand Up @@ -533,6 +545,9 @@ 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,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,7 @@ pub(crate) struct UnexpectedBuiltinCfg {
pub(crate) cfg_name: Symbol,
pub(crate) controlled_by: &'static str,
}

#[derive(Diagnostic)]
#[diag("`-Zpacked-stack` is only supported on s390x")]
pub(crate) struct UnsupportedPackedStack;
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,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],
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,12 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
sess.dcx().emit_warn(errors::SoftFloatIgnored);
}
}

if sess.opts.unstable_opts.packed_stack {
if sess.target.arch != Arch::S390x {
sess.dcx().emit_err(errors::UnsupportedPackedStack);
}
Copy link
Member

Choose a reason for hiding this comment

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

This seems like the most reasonable place to check the backchain condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes i will move the other check here

Copy link
Contributor Author

@fneddy fneddy Feb 12, 2026

Choose a reason for hiding this comment

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

unfortunately feature flags are only parsed during codegen_backend.init(sess) which is one step after rustc_session::build_session(...). So no feature flags available to check at this point.

-> i need to check them in codegen_llvm or codegen_ssa.

Copy link
Member

Choose a reason for hiding this comment

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

Please avoid duplicating the check across the codegen backends.

The check seems to be somewhat similar in nature to

util::check_abi_required_features(&sess);
. So maybe rename check_abi_required_features to something slightly more general and do the check there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes this seems, also to me, the only option left.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is another way.

I could try to talk to the llvm guys if its possible to make packed-stack a proper target-feature so we dont have to go over a cli argument.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure if that would help. Target feature conflicts currently also need ad-hoc checks, we don't have a framework for dealing with them at the moment.

}
}

/// Holds data on the current incremental compilation session, if there is one.
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3258,6 +3258,16 @@ 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 default s390x target. Switch to s390x-unknown-none-softfloat if you need both attributes."
));
}
Copy link
Member

Choose a reason for hiding this comment

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

This only checks the default-enabled features but ignores -Ctarget-features, so this check is largely ineffective. Also it checks for the packed-stack target feature which doesn't exist! Please add a test to confirm this, then move the check to a better place where it actually works.

}

Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/doc/rustc/src/codegen-options/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Since the option isn't stable yet, this is the wrong place to document it I think?


This flag enables packed StackFrames on s390x. Enabling both `packed-stack` and `backchain` attributes is incompatible
with the default s390x target. Switch to s390x-unknown-none-softfloat if you need both attributes.

## panic

This option lets you control what happens when the code panics.
Expand Down
Loading