Skip to content

add rustc option -Zpacked-stack - #152432

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
fneddy:s390x_packed_stack_attribute
Mar 31, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
fneddy:s390x_packed_stack_attribute

Conversation

@fneddy

@fneddy fneddy commented Feb 10, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

this enables -Zpacked-stack just as -mpacked-stack in clang and gcc. packed-stack is needed on s390x for kernel development.

For reference: #151154 and #150766

look at @uweigand s post for full explanation of what this does. Here a wrap-up:

#150766 (comment)

[...]
packed-stack [...] modifies how the compiler-generated function prolog/epilog code makes use of the 160 byte register save area provided by a caller to the callee [...] this variant is not actually incompatible with the ABI - packed-stack and regular functions can freely call each other without ABI issues.
[...]
combination of -mpacked-stack and -mbackchain [...] the location in the stack frame where the backchain link ought to be stored is not available. [...] is not supported at all with the default ABI
[...]
However, in the special case of also using soft-float, our (implied) soft-float ABI provides a different location for the backchain that is compatible with -mpacked-stack, so that combination should be supported
[...]

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 10, 2026
@rustbot

rustbot commented Feb 10, 2026

Copy link
Copy Markdown
Collaborator

r? @chenyukang

rustbot has assigned @chenyukang.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 66 candidates
  • Random selection from 14 candidates

@fneddy

fneddy commented Feb 10, 2026

Copy link
Copy Markdown
Contributor Author

@fneddy
fneddy marked this pull request as draft February 10, 2026 10:24
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 10, 2026
@fneddy

fneddy commented Feb 10, 2026

Copy link
Copy Markdown
Contributor Author

I'll add tests for assembly output and for target incompatibility.

@rust-log-analyzer

This comment has been minimized.

@fneddy
fneddy force-pushed the s390x_packed_stack_attribute branch from 5ddcabb to 4ebec9d Compare February 10, 2026 11:29
Comment thread compiler/rustc_target/src/spec/mod.rs Resolved
Comment thread compiler/rustc_session/src/session.rs Resolved
@fneddy
fneddy marked this pull request as ready for review February 13, 2026 11:19
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 13, 2026
@rust-bors

This comment has been minimized.

@fneddy
fneddy force-pushed the s390x_packed_stack_attribute branch from 0d6d167 to 0c182af Compare February 13, 2026 12:58
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@fneddy
fneddy force-pushed the s390x_packed_stack_attribute branch from 0c182af to 7fef6ea Compare February 16, 2026 09:35
@rustbot

This comment has been minimized.

@fneddy

fneddy commented Feb 16, 2026

Copy link
Copy Markdown
Contributor Author

ok, i think most should be ok now.

@rust-log-analyzer

This comment has been minimized.

@fneddy
fneddy force-pushed the s390x_packed_stack_attribute branch from 7fef6ea to 31a40dc Compare February 16, 2026 09:50
Comment thread compiler/rustc_interface/src/util.rs Resolved
Comment thread compiler/rustc_interface/src/util.rs Resolved
Comment thread compiler/rustc_interface/src/util.rs Outdated
if sess.target.arch == Arch::S390x
&& sess.opts.unstable_opts.packed_stack
&& sess.unstable_target_features.contains(&Symbol::intern(&"backchain"))
&& sess.target.abi != Abi::SoftFloat

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

target.abi is just in charge of cfg(target_abi), it doesn't control the actual ABI. Please check rustc_abi which controls the actual ABI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed it to check on the soft-float unstable_target_feature which llvm will evaluate to switch abi for s390x.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think checking rustc_abi makes most sense.

Comment thread compiler/rustc_interface/src/errors.rs Resolved
Comment thread compiler/rustc_interface/src/util.rs Outdated
// this is the first place in the `run_compiler` flow where all this is available
if sess.target.arch == Arch::S390x
&& sess.opts.unstable_opts.packed_stack
&& sess.unstable_target_features.contains(&Symbol::intern(&"backchain"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What happens if someone tries to enable backchain just for a particular function via #[target_feature] instead of -Ctarget-feature?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ufff yes we have to check this for every call to llfn_attrs_from_instance at compiler runtime. is it ok to do ....contains(&Symbol::intern(... every time?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

from_target_feature_attr is probably a better, higher-level place to check this?

You can add backchain as a pre-interned symbol in compiler/rustc_span/src/symbol.rs so you don't have to re-intern it every time.

@fneddy fneddy Feb 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am back on working on this. yes in from_target_feature_attr we can check for the #[target_feature(...)] attributes. BUT unfortunately not for the -Ctarget-feature... attributes ..

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah the handling for -Ctarget-feature and #[target_feature] is entirely separate. Maybe it's possible to introduce a new helper that gets called form those 2 places and that performs these kinds of consistency checks?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah I see you're now doing the check in packed_stack_attr. That also seems reasonable.

@rust-log-analyzer

This comment has been minimized.

@fneddy
fneddy force-pushed the s390x_packed_stack_attribute branch from d886fbf to f39c52b Compare February 16, 2026 14:18
@fneddy

fneddy commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@nikic

nikic commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

The interaction with LLVM looks fine.

A question I have is whether it is normal to emit errors during codegen attribute lowering?

Do I understand correctly that if -Z packed-stack is used, then any use of #[target_feature(enable = "backchain")] becomes a hard error? That seems quite unusual. (Does anyone have a reference for why s390x has a per-function backchain feature that is not integrated with the usual frame pointer options?)

@RalfJung

Copy link
Copy Markdown
Member

Do I understand correctly that if -Z packed-stack is used, then any use of #[target_feature(enable = "backchain")] becomes a hard error? That seems quite unusual.

Yeah it's a hack to deal with a somewhat ill-behaved target.

Can you think of a better way of dealing with this?

@nikic

nikic commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Do I understand correctly that if -Z packed-stack is used, then any use of #[target_feature(enable = "backchain")] becomes a hard error? That seems quite unusual.

Yeah it's a hack to deal with a somewhat ill-behaved target.

Can you think of a better way of dealing with this?

Not having backchain as a per-function target feature would be one way... Another would be to have packed-stack as part of the target definition.

@fneddy

fneddy commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Options:

disallow backchain- per-function

this would be via an explicit check during parsing or apply of codegen_fn_attrs and error if it is set? I don't see a way to mark a target_feature as allowed via target_spec_file AND cli AND NOT inline

packed_stack-part of target definition

I think we don't want to add packed-stack to any specific target. A dev could decide to use it with the regular target or with the softfloat target. Also packed-stack is currently NOT part of the target_features in llvm. Its just a frontend-flag that will set the function attribute. So we cannot add packed-stack to IBMZ_FEATURES.

Sidenote

If one tries to feed the backend with a function where backchain and packed-stack and not soft-float is set the backend will panic.

@uweigand

Copy link
Copy Markdown
Contributor

Does anyone have a reference for why s390x has a per-function backchain feature that is not integrated with the usual frame pointer options?

s390x has a frame pointer, which is used for accessing local variables and stack slots in functions using variable-sized stack allocation - the main purpose of frame pointers everywhere. On some other platforms (in particular x86 and arm), the frame pointer can also be used for stack unwinding, but that does not work on s390x, because our frame pointer indicates the bottom of the stack frame (like the stack pointer), not the top of the stack frame. The main reason for this is that some instructions do not support negative displacements, so a pointer to the top of the stack frame would be awkward to use.

Because the frame pointer is not usable for stack unwinding, there's no point in trying to use something like -fno-omit-frame-pointer on s390x. Instead, we have a separate stack backchain feature (which is unrelated to the frame pointer) that can be activated via -mbackchain; this is only used for frame unwinding in certain scenarios where DWARF CFI is not available, and it is not active by default.

@uweigand

Copy link
Copy Markdown
Contributor

Not sure whether the per-function notion is a particular problem, but this aspect isn't really important - users typically enable backchain for the whole compilation unit. The fact that this is modeled in LLVM as a "target feature" is really more of an implementation detail. If this simplifies the Rust logic, I'd also be fine with only supporting a command line option like -Z backchain and prohibit use of the target feature.

The one important use case is the Linux kernel, which is fully built with backchain, packed-stack, and soft-float. This combination must work (and be active for the kernel target).

@RalfJung

RalfJung commented Mar 31, 2026 •

Copy link
Copy Markdown
Member

I don't see a way to mark a target_feature as allowed via target_spec_file AND cli AND NOT inline

Yeah this is currently not a thing. Conceptually it's not that hard to add, but doing it in a way that the target feature table does not become a lot more verbose could be tricky.

This is all still unstable obviously so we could also just leave a note on the backchain tracking issue that this should probably not be stabilized for per-function use, only for -C target-feature. (EDIT: I added a note in #150259.)

@nikic

nikic commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Because the frame pointer is not usable for stack unwinding, there's no point in trying to use something like -fno-omit-frame-pointer on s390x. Instead, we have a separate stack backchain feature (which is unrelated to the frame pointer) that can be activated via -mbackchain; this is only used for frame unwinding in certain scenarios where DWARF CFI is not available, and it is not active by default.

I think in practice, "I want a frame pointer" really means "I want non-DWARF unwind", which is why I consider frame pointers and backchain to be the same thing, even if they technically aren't... But I guess the ship here has sailed in terms of option naming.

@nikic

nikic commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

@bors r+

As everything involved here is unstable anyway, I think this is fine to land as-is...

@rust-bors

rust-bors Bot commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f39fa9e has been approved by nikic

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 31, 2026
rust-bors Bot pushed a commit that referenced this pull request Mar 31, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #150752 (Update libc to v0.2.183)
 - #152432 (add rustc option -Zpacked-stack)
 - #154634 (Use `Hcx`/`hcx` consistently for `StableHashingContext`.)
 - #154635 (./x run miri: default to edition 2021)
@rust-bors
rust-bors Bot merged commit f883d62 into rust-lang:main Mar 31, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 31, 2026
rust-timer added a commit that referenced this pull request Mar 31, 2026
Rollup merge of #152432 - fneddy:s390x_packed_stack_attribute, r=nikic

add rustc option -Zpacked-stack

this enables `-Zpacked-stack` just as `-mpacked-stack` in clang and gcc. packed-stack is needed on s390x for kernel development.

For reference: #151154 and #150766

look at @uweigand s post for full explanation of what this does. Here a wrap-up:

#150766 (comment)
> [...]
> packed-stack [...] modifies how the compiler-generated function prolog/epilog code makes use of the 160 byte register save area provided by a caller to the callee [...]  this variant is not actually incompatible with the ABI - packed-stack and regular functions can freely call each other without ABI issues.
> [...]
> combination of -mpacked-stack and -mbackchain [...]  the location in the stack frame where the backchain link ought to be stored is not available. [...] is not supported at all with the default ABI
> [...]
> However, in the special case of also using soft-float, our (implied) soft-float ABI provides a different location for the backchain that is compatible with -mpacked-stack, so that combination should be supported
> [...]
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 6, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - rust-lang/rust#150752 (Update libc to v0.2.183)
 - rust-lang/rust#152432 (add rustc option -Zpacked-stack)
 - rust-lang/rust#154634 (Use `Hcx`/`hcx` consistently for `StableHashingContext`.)
 - rust-lang/rust#154635 (./x run miri: default to edition 2021)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 22, 2026
…ywiser

s390x: replace -Ctarget-feature=+backchain with -Cforce-frame-pointers

# Summary

On s390x, the `backchain` LLVM function attribute saves the "unwind" (backchain) pointer (r15) into the caller's parameter area, enabling external stack unwinders (e.g. Linux perf) to walk the call chain. Previously this was enabled via `-Ctarget-feature=+backchain`, which piggy-backed on the target-feature machinery even though backchain is not a CPU or ISA feature.

This commit introduces a cleaner mechanism:

- `-Cforce-frame-pointers` now enables the `"backchain"` LLVM *function attribute* on s390x, rather than the generic `"frame-pointer"` attribute used on other architectures.
- `-Ctarget-feature=+backchain` emits a error pointing to the new mechanism.
- Other reasons to set frame pointers (e.g. `-Zinstrument-mcount`) are unaffected and still emit `"frame-pointer"`.
- All s390x-specific frame-pointer/backchain/packed-stack logic is consolidated in `s390x_fn_attrs()` in `attributes.rs`.

This implements the proposed solution discussed on Zulip [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/with/628127751).

# Background

## Frame pointers on s390x

Setting `-Cforce-frame-pointers=yes` on s390x forces the `"frame-pointer"` LLVM attribute, but this does **not** form a traversable linked list. As [@uweigand] explained ([rust-lang#150766 comment]):

> Even *if* we force frame pointer registers to be used, those are not stored on the stack in a way that forms a linked list that can be followed. The `-mbackchain` option simply generates prolog code that manages an equivalent linked list of stack frames at runtime.

Therefore `-Cforce-frame-pointers` on s390x provides **no stack-walking benefit**.

## Backchain

The [s390x ELF ABI](https://github.com/IBM/s390x-abi/releases/tag/v1.6.1) reserves **backchain slot** - a pointer back to the caller's frame. This can be used to walk the entire call chain without DWARF CFI. LLVM exposes this as the `"backchain"` function attribute, equivalent to GCC's `-mbackchain`.

Backchain is **not** a CPU capability. It is a whole-binary code-generation convention, similar in nature to `-fno-omit-frame-pointer` on x86. This is reflected in LLVM modelling it as a *function attribute*, not a target feature [rust-lang#150766 comment 2].

Enabling backchain does **not** break ABI compatibility: backchain-enabled and -disabled functions can freely call each other.

The one notable exception is `packed-stack`: combining `-Zpacked-stack` with backchain on a non-softfloat target is rejected at compile time([rust-lang#152432]).

## rustc arg discussion

Initially backchain was introduced as an unstable `target-feature` mimicking the gcc and clang behavior. Within the grater effort of stabilizing arguments that are needed for Linux Kernel development(Rust-for-Linux/linux#2), attempts where made to stabilize the `target-feature=+backchain`. This lead to discussions and the overall consent is that:

1. **on s390x, enabling *normal* `frame-poitners` will *never* be done by the user.**

   > There is never any reason to do the latter, and always forcing it would simply cause extra unnecessary run-time overhead when you only actually want the backchain.
   >  @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619573547)

   > The problem is that *even when we have a frame pointer*, it cannot be used for unwinding because it *points to the wrong location*.
   > @uweigand, [rust-lang#158612](rust-lang#158612 (comment))

2. **if a user wants `frame-pointers` the user actually wants: stack-unwinding on x86 via `frame-pointers` but on `s390x` via `backchain`**

   > People being told by the internet that -Cforce-frame-pointers=yes makes their gdb bt/perf stack trace collection/etc" work get the right behaviour on s390x as well.
   > @nagisa, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

   > upstream would prefer to reuse fp rather than adding a new option (because fp cannot work for frame-pointer unwinding anyway [...])
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

   > we'd like to understand why `-Cforce-frame-pointers` can't be treated as "not perfect" name for `-mbackchain` on s390x
   > @Mark-Simulacrum, [rust-lang#158612](rust-lang#158612 (comment))

3. **`-Cforce-frame-pointers=yes` should on `s390x` enable `backchain` and **NOT** the `frame-pointer` attribute**

   > As one of the s390x target maintainers, I would be OK with using -Cforce-frame-pointers to enable backchain on s390x while not forcing actual framepointer (r11) usage.
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619778159)

   > I'm happy to second this with the "re-use -Cforce-frame-pointers" approach above (implemented requiring -Zunstable-options on s390x initially)
   > @davidtwco, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/624886154)

4. **the old `-Ctarget-feature=+backchain` pattern should emit a nice error and point to `-Cforce-frame-pointers=yes`**

   > ... which also leads to a nicer error. You can set hard_error: true for that so we don't have to worry about the validation logic.
   > @RalfJung [rust-lang#163315](rust-lang#163315 (comment))

5. **the kernel folks are ok with an instant change of behavior without a deprecation period**

   > a hard switch would be perfectly fine. The grace period actually caused some confusion on my side
   > @dev-japo   [rust-lang#163315](rust-lang#163315 (comment))

# Alternatives

Most alternatives where discussed and ruled out:
- make `-Ctarget-feature=+backchain` stable: backchain is not a real target-feature as its not part of the ISA or architecture. We rather don't want `backchain` to remain a `target-feature`
- create a new rustc flag `-Zbackhain`: If the purpose of `backchain` is already covered by other flags or options we shouldn't add a new flag.
- create a new rustc flag to enable stack-walking (e.g.`-Cstack-walking-support`): Most people mean this by setting `-Cforce-frame-pointers`. We should rather make the compiler behave as expected.

# Concerns

Enabling backchain via `-Cforce-frame-pointers` is low-risk and not a dead end. Backchain is an established, documented option in LLVM, Clang and GCC. Mixing code with and without it is safe. Reusing the flag matches how users and other tools expect `frame-pointers` to behave.

   > there are no ABI concerns with this feature (having some parts of the code compiled with and some without can lead to poor backtraces but doesn't break anything worse than that)
   > @RalfJung , relaying @uweigand , [rust-lang#158612](rust-lang#158612 (comment))

   > reusing the option would make things easier for existing users
   > @uweigand , [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

A real frame pointer can still get its own flag if anyone ever needs it.

   > if they do actually want the actual frame pointers in the wrong place of the stack, we can add -Cforce-frame-pointers=the-broken-s390x-option for them or something
   > @nagisa [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

# References

- rust-lang#127506
- rust-lang#129927
- rust-lang#129940
- rust-lang#142412
- rust-lang#150259
- rust-lang#150766
- rust-lang#151154
- rust-lang#152432
- rust-lang#158014
- rust-lang#158612
- rust-lang#163315
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 22, 2026
…ywiser

s390x: replace -Ctarget-feature=+backchain with -Cforce-frame-pointers

# Summary

On s390x, the `backchain` LLVM function attribute saves the "unwind" (backchain) pointer (r15) into the caller's parameter area, enabling external stack unwinders (e.g. Linux perf) to walk the call chain. Previously this was enabled via `-Ctarget-feature=+backchain`, which piggy-backed on the target-feature machinery even though backchain is not a CPU or ISA feature.

This commit introduces a cleaner mechanism:

- `-Cforce-frame-pointers` now enables the `"backchain"` LLVM *function attribute* on s390x, rather than the generic `"frame-pointer"` attribute used on other architectures.
- `-Ctarget-feature=+backchain` emits a error pointing to the new mechanism.
- Other reasons to set frame pointers (e.g. `-Zinstrument-mcount`) are unaffected and still emit `"frame-pointer"`.
- All s390x-specific frame-pointer/backchain/packed-stack logic is consolidated in `s390x_fn_attrs()` in `attributes.rs`.

This implements the proposed solution discussed on Zulip [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/with/628127751).

# Background

## Frame pointers on s390x

Setting `-Cforce-frame-pointers=yes` on s390x forces the `"frame-pointer"` LLVM attribute, but this does **not** form a traversable linked list. As [@uweigand] explained ([rust-lang#150766 comment]):

> Even *if* we force frame pointer registers to be used, those are not stored on the stack in a way that forms a linked list that can be followed. The `-mbackchain` option simply generates prolog code that manages an equivalent linked list of stack frames at runtime.

Therefore `-Cforce-frame-pointers` on s390x provides **no stack-walking benefit**.

## Backchain

The [s390x ELF ABI](https://github.com/IBM/s390x-abi/releases/tag/v1.6.1) reserves **backchain slot** - a pointer back to the caller's frame. This can be used to walk the entire call chain without DWARF CFI. LLVM exposes this as the `"backchain"` function attribute, equivalent to GCC's `-mbackchain`.

Backchain is **not** a CPU capability. It is a whole-binary code-generation convention, similar in nature to `-fno-omit-frame-pointer` on x86. This is reflected in LLVM modelling it as a *function attribute*, not a target feature [rust-lang#150766 comment 2].

Enabling backchain does **not** break ABI compatibility: backchain-enabled and -disabled functions can freely call each other.

The one notable exception is `packed-stack`: combining `-Zpacked-stack` with backchain on a non-softfloat target is rejected at compile time([rust-lang#152432]).

## rustc arg discussion

Initially backchain was introduced as an unstable `target-feature` mimicking the gcc and clang behavior. Within the grater effort of stabilizing arguments that are needed for Linux Kernel development(Rust-for-Linux/linux#2), attempts where made to stabilize the `target-feature=+backchain`. This lead to discussions and the overall consent is that:

1. **on s390x, enabling *normal* `frame-poitners` will *never* be done by the user.**

   > There is never any reason to do the latter, and always forcing it would simply cause extra unnecessary run-time overhead when you only actually want the backchain.
   >  @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619573547)

   > The problem is that *even when we have a frame pointer*, it cannot be used for unwinding because it *points to the wrong location*.
   > @uweigand, [rust-lang#158612](rust-lang#158612 (comment))

2. **if a user wants `frame-pointers` the user actually wants: stack-unwinding on x86 via `frame-pointers` but on `s390x` via `backchain`**

   > People being told by the internet that -Cforce-frame-pointers=yes makes their gdb bt/perf stack trace collection/etc" work get the right behaviour on s390x as well.
   > @nagisa, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

   > upstream would prefer to reuse fp rather than adding a new option (because fp cannot work for frame-pointer unwinding anyway [...])
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

   > we'd like to understand why `-Cforce-frame-pointers` can't be treated as "not perfect" name for `-mbackchain` on s390x
   > @Mark-Simulacrum, [rust-lang#158612](rust-lang#158612 (comment))

3. **`-Cforce-frame-pointers=yes` should on `s390x` enable `backchain` and **NOT** the `frame-pointer` attribute**

   > As one of the s390x target maintainers, I would be OK with using -Cforce-frame-pointers to enable backchain on s390x while not forcing actual framepointer (r11) usage.
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619778159)

   > I'm happy to second this with the "re-use -Cforce-frame-pointers" approach above (implemented requiring -Zunstable-options on s390x initially)
   > @davidtwco, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/624886154)

4. **the old `-Ctarget-feature=+backchain` pattern should emit a nice error and point to `-Cforce-frame-pointers=yes`**

   > ... which also leads to a nicer error. You can set hard_error: true for that so we don't have to worry about the validation logic.
   > @RalfJung [rust-lang#163315](rust-lang#163315 (comment))

5. **the kernel folks are ok with an instant change of behavior without a deprecation period**

   > a hard switch would be perfectly fine. The grace period actually caused some confusion on my side
   > @dev-japo   [rust-lang#163315](rust-lang#163315 (comment))

# Alternatives

Most alternatives where discussed and ruled out:
- make `-Ctarget-feature=+backchain` stable: backchain is not a real target-feature as its not part of the ISA or architecture. We rather don't want `backchain` to remain a `target-feature`
- create a new rustc flag `-Zbackhain`: If the purpose of `backchain` is already covered by other flags or options we shouldn't add a new flag.
- create a new rustc flag to enable stack-walking (e.g.`-Cstack-walking-support`): Most people mean this by setting `-Cforce-frame-pointers`. We should rather make the compiler behave as expected.

# Concerns

Enabling backchain via `-Cforce-frame-pointers` is low-risk and not a dead end. Backchain is an established, documented option in LLVM, Clang and GCC. Mixing code with and without it is safe. Reusing the flag matches how users and other tools expect `frame-pointers` to behave.

   > there are no ABI concerns with this feature (having some parts of the code compiled with and some without can lead to poor backtraces but doesn't break anything worse than that)
   > @RalfJung , relaying @uweigand , [rust-lang#158612](rust-lang#158612 (comment))

   > reusing the option would make things easier for existing users
   > @uweigand , [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

A real frame pointer can still get its own flag if anyone ever needs it.

   > if they do actually want the actual frame pointers in the wrong place of the stack, we can add -Cforce-frame-pointers=the-broken-s390x-option for them or something
   > @nagisa [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

# References

- rust-lang#127506
- rust-lang#129927
- rust-lang#129940
- rust-lang#142412
- rust-lang#150259
- rust-lang#150766
- rust-lang#151154
- rust-lang#152432
- rust-lang#158014
- rust-lang#158612
- rust-lang#163315
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Rollup merge of #163315 - fneddy:s390x_framepointer, r=wesleywiser

s390x: replace -Ctarget-feature=+backchain with -Cforce-frame-pointers

# Summary

On s390x, the `backchain` LLVM function attribute saves the "unwind" (backchain) pointer (r15) into the caller's parameter area, enabling external stack unwinders (e.g. Linux perf) to walk the call chain. Previously this was enabled via `-Ctarget-feature=+backchain`, which piggy-backed on the target-feature machinery even though backchain is not a CPU or ISA feature.

This commit introduces a cleaner mechanism:

- `-Cforce-frame-pointers` now enables the `"backchain"` LLVM *function attribute* on s390x, rather than the generic `"frame-pointer"` attribute used on other architectures.
- `-Ctarget-feature=+backchain` emits a error pointing to the new mechanism.
- Other reasons to set frame pointers (e.g. `-Zinstrument-mcount`) are unaffected and still emit `"frame-pointer"`.
- All s390x-specific frame-pointer/backchain/packed-stack logic is consolidated in `s390x_fn_attrs()` in `attributes.rs`.

This implements the proposed solution discussed on Zulip [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/with/628127751).

# Background

## Frame pointers on s390x

Setting `-Cforce-frame-pointers=yes` on s390x forces the `"frame-pointer"` LLVM attribute, but this does **not** form a traversable linked list. As [@uweigand] explained ([#150766 comment]):

> Even *if* we force frame pointer registers to be used, those are not stored on the stack in a way that forms a linked list that can be followed. The `-mbackchain` option simply generates prolog code that manages an equivalent linked list of stack frames at runtime.

Therefore `-Cforce-frame-pointers` on s390x provides **no stack-walking benefit**.

## Backchain

The [s390x ELF ABI](https://github.com/IBM/s390x-abi/releases/tag/v1.6.1) reserves **backchain slot** - a pointer back to the caller's frame. This can be used to walk the entire call chain without DWARF CFI. LLVM exposes this as the `"backchain"` function attribute, equivalent to GCC's `-mbackchain`.

Backchain is **not** a CPU capability. It is a whole-binary code-generation convention, similar in nature to `-fno-omit-frame-pointer` on x86. This is reflected in LLVM modelling it as a *function attribute*, not a target feature [#150766 comment 2].

Enabling backchain does **not** break ABI compatibility: backchain-enabled and -disabled functions can freely call each other.

The one notable exception is `packed-stack`: combining `-Zpacked-stack` with backchain on a non-softfloat target is rejected at compile time([#152432]).

## rustc arg discussion

Initially backchain was introduced as an unstable `target-feature` mimicking the gcc and clang behavior. Within the grater effort of stabilizing arguments that are needed for Linux Kernel development(Rust-for-Linux/linux#2), attempts where made to stabilize the `target-feature=+backchain`. This lead to discussions and the overall consent is that:

1. **on s390x, enabling *normal* `frame-poitners` will *never* be done by the user.**

   > There is never any reason to do the latter, and always forcing it would simply cause extra unnecessary run-time overhead when you only actually want the backchain.
   >  @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619573547)

   > The problem is that *even when we have a frame pointer*, it cannot be used for unwinding because it *points to the wrong location*.
   > @uweigand, [#158612](#158612 (comment))

2. **if a user wants `frame-pointers` the user actually wants: stack-unwinding on x86 via `frame-pointers` but on `s390x` via `backchain`**

   > People being told by the internet that -Cforce-frame-pointers=yes makes their gdb bt/perf stack trace collection/etc" work get the right behaviour on s390x as well.
   > @nagisa, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

   > upstream would prefer to reuse fp rather than adding a new option (because fp cannot work for frame-pointer unwinding anyway [...])
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

   > we'd like to understand why `-Cforce-frame-pointers` can't be treated as "not perfect" name for `-mbackchain` on s390x
   > @Mark-Simulacrum, [#158612](#158612 (comment))

3. **`-Cforce-frame-pointers=yes` should on `s390x` enable `backchain` and **NOT** the `frame-pointer` attribute**

   > As one of the s390x target maintainers, I would be OK with using -Cforce-frame-pointers to enable backchain on s390x while not forcing actual framepointer (r11) usage.
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619778159)

   > I'm happy to second this with the "re-use -Cforce-frame-pointers" approach above (implemented requiring -Zunstable-options on s390x initially)
   > @davidtwco, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/624886154)

4. **the old `-Ctarget-feature=+backchain` pattern should emit a nice error and point to `-Cforce-frame-pointers=yes`**

   > ... which also leads to a nicer error. You can set hard_error: true for that so we don't have to worry about the validation logic.
   > @RalfJung [#163315](#163315 (comment))

5. **the kernel folks are ok with an instant change of behavior without a deprecation period**

   > a hard switch would be perfectly fine. The grace period actually caused some confusion on my side
   > @dev-japo   [#163315](#163315 (comment))

# Alternatives

Most alternatives where discussed and ruled out:
- make `-Ctarget-feature=+backchain` stable: backchain is not a real target-feature as its not part of the ISA or architecture. We rather don't want `backchain` to remain a `target-feature`
- create a new rustc flag `-Zbackhain`: If the purpose of `backchain` is already covered by other flags or options we shouldn't add a new flag.
- create a new rustc flag to enable stack-walking (e.g.`-Cstack-walking-support`): Most people mean this by setting `-Cforce-frame-pointers`. We should rather make the compiler behave as expected.

# Concerns

Enabling backchain via `-Cforce-frame-pointers` is low-risk and not a dead end. Backchain is an established, documented option in LLVM, Clang and GCC. Mixing code with and without it is safe. Reusing the flag matches how users and other tools expect `frame-pointers` to behave.

   > there are no ABI concerns with this feature (having some parts of the code compiled with and some without can lead to poor backtraces but doesn't break anything worse than that)
   > @RalfJung , relaying @uweigand , [#158612](#158612 (comment))

   > reusing the option would make things easier for existing users
   > @uweigand , [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

A real frame pointer can still get its own flag if anyone ever needs it.

   > if they do actually want the actual frame pointers in the wrong place of the stack, we can add -Cforce-frame-pointers=the-broken-s390x-option for them or something
   > @nagisa [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

# References

- #127506
- #129927
- #129940
- #142412
- #150259
- #150766
- #151154
- #152432
- #158014
- #158612
- #163315
github-actions Bot pushed a commit to rust-lang/stdarch that referenced this pull request Nov 23, 2026
s390x: replace -Ctarget-feature=+backchain with -Cforce-frame-pointers

# Summary

On s390x, the `backchain` LLVM function attribute saves the "unwind" (backchain) pointer (r15) into the caller's parameter area, enabling external stack unwinders (e.g. Linux perf) to walk the call chain. Previously this was enabled via `-Ctarget-feature=+backchain`, which piggy-backed on the target-feature machinery even though backchain is not a CPU or ISA feature.

This commit introduces a cleaner mechanism:

- `-Cforce-frame-pointers` now enables the `"backchain"` LLVM *function attribute* on s390x, rather than the generic `"frame-pointer"` attribute used on other architectures.
- `-Ctarget-feature=+backchain` emits a error pointing to the new mechanism.
- Other reasons to set frame pointers (e.g. `-Zinstrument-mcount`) are unaffected and still emit `"frame-pointer"`.
- All s390x-specific frame-pointer/backchain/packed-stack logic is consolidated in `s390x_fn_attrs()` in `attributes.rs`.

This implements the proposed solution discussed on Zulip [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/with/628127751).

# Background

## Frame pointers on s390x

Setting `-Cforce-frame-pointers=yes` on s390x forces the `"frame-pointer"` LLVM attribute, but this does **not** form a traversable linked list. As [@uweigand] explained ([rust-lang/rust#150766 comment]):

> Even *if* we force frame pointer registers to be used, those are not stored on the stack in a way that forms a linked list that can be followed. The `-mbackchain` option simply generates prolog code that manages an equivalent linked list of stack frames at runtime.

Therefore `-Cforce-frame-pointers` on s390x provides **no stack-walking benefit**.

## Backchain

The [s390x ELF ABI](https://github.com/IBM/s390x-abi/releases/tag/v1.6.1) reserves **backchain slot** - a pointer back to the caller's frame. This can be used to walk the entire call chain without DWARF CFI. LLVM exposes this as the `"backchain"` function attribute, equivalent to GCC's `-mbackchain`.

Backchain is **not** a CPU capability. It is a whole-binary code-generation convention, similar in nature to `-fno-omit-frame-pointer` on x86. This is reflected in LLVM modelling it as a *function attribute*, not a target feature [rust-lang/rust#150766 comment 2].

Enabling backchain does **not** break ABI compatibility: backchain-enabled and -disabled functions can freely call each other.

The one notable exception is `packed-stack`: combining `-Zpacked-stack` with backchain on a non-softfloat target is rejected at compile time([rust-lang/rust#152432]).

## rustc arg discussion

Initially backchain was introduced as an unstable `target-feature` mimicking the gcc and clang behavior. Within the grater effort of stabilizing arguments that are needed for Linux Kernel development(Rust-for-Linux/linux#2), attempts where made to stabilize the `target-feature=+backchain`. This lead to discussions and the overall consent is that:

1. **on s390x, enabling *normal* `frame-poitners` will *never* be done by the user.**

   > There is never any reason to do the latter, and always forcing it would simply cause extra unnecessary run-time overhead when you only actually want the backchain.
   >  @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619573547)

   > The problem is that *even when we have a frame pointer*, it cannot be used for unwinding because it *points to the wrong location*.
   > @uweigand, [#158612](rust-lang/rust#158612 (comment))

2. **if a user wants `frame-pointers` the user actually wants: stack-unwinding on x86 via `frame-pointers` but on `s390x` via `backchain`**

   > People being told by the internet that -Cforce-frame-pointers=yes makes their gdb bt/perf stack trace collection/etc" work get the right behaviour on s390x as well.
   > @nagisa, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

   > upstream would prefer to reuse fp rather than adding a new option (because fp cannot work for frame-pointer unwinding anyway [...])
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

   > we'd like to understand why `-Cforce-frame-pointers` can't be treated as "not perfect" name for `-mbackchain` on s390x
   > @Mark-Simulacrum, [#158612](rust-lang/rust#158612 (comment))

3. **`-Cforce-frame-pointers=yes` should on `s390x` enable `backchain` and **NOT** the `frame-pointer` attribute**

   > As one of the s390x target maintainers, I would be OK with using -Cforce-frame-pointers to enable backchain on s390x while not forcing actual framepointer (r11) usage.
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619778159)

   > I'm happy to second this with the "re-use -Cforce-frame-pointers" approach above (implemented requiring -Zunstable-options on s390x initially)
   > @davidtwco, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/624886154)

4. **the old `-Ctarget-feature=+backchain` pattern should emit a nice error and point to `-Cforce-frame-pointers=yes`**

   > ... which also leads to a nicer error. You can set hard_error: true for that so we don't have to worry about the validation logic.
   > @RalfJung [#163315](rust-lang/rust#163315 (comment))

5. **the kernel folks are ok with an instant change of behavior without a deprecation period**

   > a hard switch would be perfectly fine. The grace period actually caused some confusion on my side
   > @dev-japo   [#163315](rust-lang/rust#163315 (comment))

# Alternatives

Most alternatives where discussed and ruled out:
- make `-Ctarget-feature=+backchain` stable: backchain is not a real target-feature as its not part of the ISA or architecture. We rather don't want `backchain` to remain a `target-feature`
- create a new rustc flag `-Zbackhain`: If the purpose of `backchain` is already covered by other flags or options we shouldn't add a new flag.
- create a new rustc flag to enable stack-walking (e.g.`-Cstack-walking-support`): Most people mean this by setting `-Cforce-frame-pointers`. We should rather make the compiler behave as expected.

# Concerns

Enabling backchain via `-Cforce-frame-pointers` is low-risk and not a dead end. Backchain is an established, documented option in LLVM, Clang and GCC. Mixing code with and without it is safe. Reusing the flag matches how users and other tools expect `frame-pointers` to behave.

   > there are no ABI concerns with this feature (having some parts of the code compiled with and some without can lead to poor backtraces but doesn't break anything worse than that)
   > @RalfJung , relaying @uweigand , [#158612](rust-lang/rust#158612 (comment))

   > reusing the option would make things easier for existing users
   > @uweigand , [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

A real frame pointer can still get its own flag if anyone ever needs it.

   > if they do actually want the actual frame pointers in the wrong place of the stack, we can add -Cforce-frame-pointers=the-broken-s390x-option for them or something
   > @nagisa [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

# References

- rust-lang/rust#127506
- rust-lang/rust#129927
- rust-lang/rust#129940
- rust-lang/rust#142412
- rust-lang/rust#150259
- rust-lang/rust#150766
- rust-lang/rust#151154
- rust-lang/rust#152432
- rust-lang/rust#158014
- rust-lang/rust#158612
- rust-lang/rust#163315
github-merge-queue Bot pushed a commit to rust-lang/miri that referenced this pull request Nov 23, 2026
s390x: replace -Ctarget-feature=+backchain with -Cforce-frame-pointers

# Summary

On s390x, the `backchain` LLVM function attribute saves the "unwind" (backchain) pointer (r15) into the caller's parameter area, enabling external stack unwinders (e.g. Linux perf) to walk the call chain. Previously this was enabled via `-Ctarget-feature=+backchain`, which piggy-backed on the target-feature machinery even though backchain is not a CPU or ISA feature.

This commit introduces a cleaner mechanism:

- `-Cforce-frame-pointers` now enables the `"backchain"` LLVM *function attribute* on s390x, rather than the generic `"frame-pointer"` attribute used on other architectures.
- `-Ctarget-feature=+backchain` emits a error pointing to the new mechanism.
- Other reasons to set frame pointers (e.g. `-Zinstrument-mcount`) are unaffected and still emit `"frame-pointer"`.
- All s390x-specific frame-pointer/backchain/packed-stack logic is consolidated in `s390x_fn_attrs()` in `attributes.rs`.

This implements the proposed solution discussed on Zulip [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/with/628127751).

# Background

## Frame pointers on s390x

Setting `-Cforce-frame-pointers=yes` on s390x forces the `"frame-pointer"` LLVM attribute, but this does **not** form a traversable linked list. As [@uweigand] explained ([rust-lang/rust#150766 comment]):

> Even *if* we force frame pointer registers to be used, those are not stored on the stack in a way that forms a linked list that can be followed. The `-mbackchain` option simply generates prolog code that manages an equivalent linked list of stack frames at runtime.

Therefore `-Cforce-frame-pointers` on s390x provides **no stack-walking benefit**.

## Backchain

The [s390x ELF ABI](https://github.com/IBM/s390x-abi/releases/tag/v1.6.1) reserves **backchain slot** - a pointer back to the caller's frame. This can be used to walk the entire call chain without DWARF CFI. LLVM exposes this as the `"backchain"` function attribute, equivalent to GCC's `-mbackchain`.

Backchain is **not** a CPU capability. It is a whole-binary code-generation convention, similar in nature to `-fno-omit-frame-pointer` on x86. This is reflected in LLVM modelling it as a *function attribute*, not a target feature [rust-lang/rust#150766 comment 2].

Enabling backchain does **not** break ABI compatibility: backchain-enabled and -disabled functions can freely call each other.

The one notable exception is `packed-stack`: combining `-Zpacked-stack` with backchain on a non-softfloat target is rejected at compile time([rust-lang/rust#152432]).

## rustc arg discussion

Initially backchain was introduced as an unstable `target-feature` mimicking the gcc and clang behavior. Within the grater effort of stabilizing arguments that are needed for Linux Kernel development(Rust-for-Linux/linux#2), attempts where made to stabilize the `target-feature=+backchain`. This lead to discussions and the overall consent is that:

1. **on s390x, enabling *normal* `frame-poitners` will *never* be done by the user.**

   > There is never any reason to do the latter, and always forcing it would simply cause extra unnecessary run-time overhead when you only actually want the backchain.
   >  @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619573547)

   > The problem is that *even when we have a frame pointer*, it cannot be used for unwinding because it *points to the wrong location*.
   > @uweigand, [#158612](rust-lang/rust#158612 (comment))

2. **if a user wants `frame-pointers` the user actually wants: stack-unwinding on x86 via `frame-pointers` but on `s390x` via `backchain`**

   > People being told by the internet that -Cforce-frame-pointers=yes makes their gdb bt/perf stack trace collection/etc" work get the right behaviour on s390x as well.
   > @nagisa, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

   > upstream would prefer to reuse fp rather than adding a new option (because fp cannot work for frame-pointer unwinding anyway [...])
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

   > we'd like to understand why `-Cforce-frame-pointers` can't be treated as "not perfect" name for `-mbackchain` on s390x
   > @Mark-Simulacrum, [#158612](rust-lang/rust#158612 (comment))

3. **`-Cforce-frame-pointers=yes` should on `s390x` enable `backchain` and **NOT** the `frame-pointer` attribute**

   > As one of the s390x target maintainers, I would be OK with using -Cforce-frame-pointers to enable backchain on s390x while not forcing actual framepointer (r11) usage.
   > @uweigand, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/619778159)

   > I'm happy to second this with the "re-use -Cforce-frame-pointers" approach above (implemented requiring -Zunstable-options on s390x initially)
   > @davidtwco, [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/624886154)

4. **the old `-Ctarget-feature=+backchain` pattern should emit a nice error and point to `-Cforce-frame-pointers=yes`**

   > ... which also leads to a nicer error. You can set hard_error: true for that so we don't have to worry about the validation logic.
   > @RalfJung [#163315](rust-lang/rust#163315 (comment))

5. **the kernel folks are ok with an instant change of behavior without a deprecation period**

   > a hard switch would be perfectly fine. The grace period actually caused some confusion on my side
   > @dev-japo   [#163315](rust-lang/rust#163315 (comment))

# Alternatives

Most alternatives where discussed and ruled out:
- make `-Ctarget-feature=+backchain` stable: backchain is not a real target-feature as its not part of the ISA or architecture. We rather don't want `backchain` to remain a `target-feature`
- create a new rustc flag `-Zbackhain`: If the purpose of `backchain` is already covered by other flags or options we shouldn't add a new flag.
- create a new rustc flag to enable stack-walking (e.g.`-Cstack-walking-support`): Most people mean this by setting `-Cforce-frame-pointers`. We should rather make the compiler behave as expected.

# Concerns

Enabling backchain via `-Cforce-frame-pointers` is low-risk and not a dead end. Backchain is an established, documented option in LLVM, Clang and GCC. Mixing code with and without it is safe. Reusing the flag matches how users and other tools expect `frame-pointers` to behave.

   > there are no ABI concerns with this feature (having some parts of the code compiled with and some without can lead to poor backtraces but doesn't break anything worse than that)
   > @RalfJung , relaying @uweigand , [#158612](rust-lang/rust#158612 (comment))

   > reusing the option would make things easier for existing users
   > @uweigand , [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614979602)

A real frame pointer can still get its own flag if anyone ever needs it.

   > if they do actually want the actual frame pointers in the wrong place of the stack, we can add -Cforce-frame-pointers=the-broken-s390x-option for them or something
   > @nagisa [#t-compiler/major changes > Introduce new -C flag for cross-target c… compiler-team#1027 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/233931-t-compiler.2Fmajor-changes/topic/Introduce.20new.20-C.20flag.20for.20cross-target.20c.E2.80.A6.20compiler-team.231027/near/614754887)

# References

- rust-lang/rust#127506
- rust-lang/rust#129927
- rust-lang/rust#129940
- rust-lang/rust#142412
- rust-lang/rust#150259
- rust-lang/rust#150766
- rust-lang/rust#151154
- rust-lang/rust#152432
- rust-lang/rust#158014
- rust-lang/rust#158612
- rust-lang/rust#163315
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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.