Skip to content

cranelift: Add a new, optional flag for preserving frame pointers#4469

Merged
fitzgen merged 1 commit intobytecodealliance:mainfrom
fitzgen:preserve-frame-pointers-flag
Jul 20, 2022
Merged

cranelift: Add a new, optional flag for preserving frame pointers#4469
fitzgen merged 1 commit intobytecodealliance:mainfrom
fitzgen:preserve-frame-pointers-flag

Conversation

@fitzgen
Copy link
Member

@fitzgen fitzgen commented Jul 19, 2022

This flag is optional, but when it is set then Cranelift backends must preserve frame pointers (or ISA equivalent).

Preserving frame pointers -- even inside leaf functions -- makes it easy to capture the stack of a running program, without requiring any side tables or metadata (like .eh_frame sections). Many sampling profilers and similar tools walk frame pointers to capture stacks. Enabling this option will play nice with those tools.

Our x64 backend already unconditionally preserves frame pointers, so it is trivially conforming with this new flag.

Our aarch64 backend preserves frame pointers or not based on whether the machinst framework tells it to or not by calling gen_prologue_frame_setup, and the machinst framework looks at this new flag when deciding whether to call the backend's implementation of gen_prologue_frame_setup, so it is conforming with this new flag.

However, our s390x backend has an empty implementation of gen_prologue_frame_setup, and so it is not preserving frame pointers when this new flag is set. We will need a follow up PR that brings our s390x backend into conformance with this new flag.

cc @uweigand, your help with this last bit would be very appreciated, as I'm not familiar with s390x.

cc #4431

Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

LGTM, and good to merge once we have s390x supported as well -- thanks!

@fitzgen
Copy link
Member Author

fitzgen commented Jul 19, 2022

@cfallin

good to merge once we have s390x supported as well

As I just said over in #4431 (comment) I think we can actually land this PR now, since it doesn't break anything by itself, as nothing is relying on the frame pointers yet.

But we shouldn't land #4431 until there is s390x support for this flag, for sure, as that would break Wasmtime on s390x.

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:meta Everything related to the meta-language. labels Jul 19, 2022
@cfallin
Copy link
Member

cfallin commented Jul 19, 2022

Ah, my mistake, I had misunderstood the intended order of changes; yep, r+ for merging this now. Thanks!

Preserving frame pointers -- even inside leaf functions -- makes it easy to
capture the stack of a running program, without requiring any side tables or
metadata (like `.eh_frame` sections). Many sampling profilers and similar tools
walk frame pointers to capture stacks. Enabling this option will play nice with
those tools.
@fitzgen fitzgen force-pushed the preserve-frame-pointers-flag branch from 14f6155 to 709a762 Compare July 19, 2022 22:24
@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jul 19, 2022
@github-actions
Copy link

Subscribe to Label Action

cc @peterhuene

Details This issue or pull request has been labeled: "cranelift", "cranelift:area:machinst", "cranelift:meta", "wasmtime:api"

Thus the following users have been cc'd because of the following labels:

  • peterhuene: wasmtime:api

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@uweigand
Copy link
Member

cc @uweigand, your help with this last bit would be very appreciated, as I'm not familiar with s390x.

Sure, I will post a follow-up PR to add s390x support as soon as this is merged.

@fitzgen fitzgen merged commit 22d91a7 into bytecodealliance:main Jul 20, 2022
@fitzgen fitzgen deleted the preserve-frame-pointers-flag branch July 20, 2022 15:02
@fitzgen
Copy link
Member Author

fitzgen commented Jul 20, 2022

Sure, I will post a follow-up PR to add s390x support as soon as this is merged.

Thanks!

uweigand added a commit to uweigand/wasmtime that referenced this pull request Jul 20, 2022
On s390x, we do not have a frame pointer that can be used to chain
stack frames for easy unwinding.  Instead, our ABI defines a stack
"backchain" mechanism that can be used to the same effect.

This PR uses that backchain mechanism to implement the new
preserve_frame_pointers flags introduced here:
bytecodealliance#4469
uweigand added a commit to uweigand/wasmtime that referenced this pull request Jul 20, 2022
On s390x, we do not have a frame pointer that can be used to chain
stack frames for easy unwinding.  Instead, our ABI defines a stack
"backchain" mechanism that can be used to the same effect.

This PR uses that backchain mechanism to implement the new
preserve_frame_pointers flags introduced here:
bytecodealliance#4469
Enabling this option will play nice with those tools.
"#,
false,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

LLVM has a tri-state value of "always", "non-leaf" and "may-omit". It would be nice if Cranelift could have the same so I don't have to use the equivalent of "always" if "non-leaf" was chosen by the rustc target. For example Apple AArch64 targets require "non-leaf".

Copy link
Member Author

Choose a reason for hiding this comment

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

This is something we can do in the future as needed.

Copy link
Contributor

@akirilov-arm akirilov-arm Jul 22, 2022

Choose a reason for hiding this comment

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

Actually with the way the AArch64 backend works right now it is either "always" (i.e. preserve_frame_pointers being true) or "non-leaf" (i.e. false value). "may-omit" sounds as if it would be fine to treat it as "non-leaf".

P.S. Technically even leaf functions always preserve frame pointers as long as they use the stack, e.g. define an explicit stack slot.

Copy link
Contributor

Choose a reason for hiding this comment

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

The name preserve_frame_pointers makes it seem like false means that frame pointers may always be omitted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, thinking a bit more about this, actually the AArch64 backend always "preserves" the frame pointer (i.e. it always has a valid value - either 0 or an address that points to a frame record on the stack), since the respective register is not allocatable; it is just that some functions (simple leaf functions that do not use the stack) opt not to update it, and keep its value as set by their callers.

fitzgen pushed a commit that referenced this pull request Jul 21, 2022
On s390x, we do not have a frame pointer that can be used to chain
stack frames for easy unwinding.  Instead, our ABI defines a stack
"backchain" mechanism that can be used to the same effect.

This PR uses that backchain mechanism to implement the new
preserve_frame_pointers flags introduced here:
#4469
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:meta Everything related to the meta-language. cranelift Issues related to the Cranelift code generator wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants