Skip to content

cranelift: Always consider sret arguments used#8664

Merged
jameysharp merged 2 commits intobytecodealliance:mainfrom
jameysharp:fix-8659
May 20, 2024
Merged

cranelift: Always consider sret arguments used#8664
jameysharp merged 2 commits intobytecodealliance:mainfrom
jameysharp:fix-8659

Conversation

@jameysharp
Copy link
Contributor

In #8438 we stopped emitting register bindings for unused arguments, based on the use-counts from compute_use_states. However, that doesn't count the use of the struct-return argument that's automatically added after lowering when the rets instruction is generated in the epilogue. As a result, using a struct-return argument caused register allocation to panic due to the VReg not being defined anywhere.

This commit adds a use to the struct-return argument so that it's always available in the epilogue.

Fixes #8659

In bytecodealliance#8438 we stopped emitting register bindings for unused arguments,
based on the use-counts from `compute_use_states`. However, that doesn't
count the use of the struct-return argument that's automatically added
after lowering when the `rets` instruction is generated in the epilogue.
As a result, using a struct-return argument caused register allocation
to panic due to the VReg not being defined anywhere.

This commit adds a use to the struct-return argument so that it's always
available in the epilogue.

Fixes bytecodealliance#8659
@jameysharp jameysharp requested a review from a team as a code owner May 20, 2024 17:51
@jameysharp jameysharp requested review from fitzgen and removed request for a team May 20, 2024 17:51
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!

let mut value_ir_uses = SecondaryMap::with_default(ValueUseState::Unused);

if let Some(sret_param) = sret_param {
value_ir_uses[sret_param] = ValueUseState::Once;
Copy link
Member

Choose a reason for hiding this comment

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

Could we add a comment here to note it starts at Once (for the one use that is the implicit return), but can still be updated below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, should I just start it at Multiple in case there are multiple epilogues? The definition of the sret vreg can't participate in load-sinking or anything so I don't think the value really matters, but I guess it should be "correct" anyway?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yes, that makes more sense actually, I agree. It's unlikely to matter as you say but might as well get the analysis right...

@jameysharp jameysharp enabled auto-merge May 20, 2024 19:05
@jameysharp jameysharp added this pull request to the merge queue May 20, 2024
Merged via the queue into bytecodealliance:main with commit c1cf56c May 20, 2024
@jameysharp jameysharp deleted the fix-8659 branch May 20, 2024 19:27
alexcrichton pushed a commit to alexcrichton/wasmtime that referenced this pull request May 20, 2024
* cranelift: Always consider sret arguments used

In bytecodealliance#8438 we stopped emitting register bindings for unused arguments,
based on the use-counts from `compute_use_states`. However, that doesn't
count the use of the struct-return argument that's automatically added
after lowering when the `rets` instruction is generated in the epilogue.
As a result, using a struct-return argument caused register allocation
to panic due to the VReg not being defined anywhere.

This commit adds a use to the struct-return argument so that it's always
available in the epilogue.

Fixes bytecodealliance#8659

* Review comments
alexcrichton added a commit that referenced this pull request May 20, 2024
* cranelift: Always consider sret arguments used

In #8438 we stopped emitting register bindings for unused arguments,
based on the use-counts from `compute_use_states`. However, that doesn't
count the use of the struct-return argument that's automatically added
after lowering when the `rets` instruction is generated in the epilogue.
As a result, using a struct-return argument caused register allocation
to panic due to the VReg not being defined anywhere.

This commit adds a use to the struct-return argument so that it's always
available in the epilogue.

Fixes #8659

* Review comments

Co-authored-by: Jamey Sharp <jsharp@fastly.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cranelift: sret argument without use causes bad SSA to be passed to regalloc

2 participants