Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winch: x64 wait, notify and fence #10092

Merged
merged 10 commits into from
Jan 27, 2025

Conversation

MarinPostma
Copy link
Contributor

This PR implements the following instruction in winch for the x64 backend:

  • atomic.fence
  • memory.atomic.wait32
  • memory.atomic.wait64
  • memory.atomic.notify

@MarinPostma MarinPostma requested review from a team as code owners January 23, 2025 18:18
@MarinPostma MarinPostma requested review from cfallin and pchickey and removed request for a team January 23, 2025 18:18
@saulecabrera
Copy link
Member

I can help with this one.

@saulecabrera saulecabrera requested review from saulecabrera and removed request for cfallin and pchickey January 23, 2025 18:26
@github-actions github-actions bot added the winch Winch issues or pull requests label Jan 23, 2025
Copy link

Subscribe to Label Action

cc @saulecabrera

This issue or pull request has been labeled: "winch"

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

  • saulecabrera: winch

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

Learn more.

Comment on lines 1454 to 1463
// Put the target memory index in a register, and push it as the first argument.
let mem = self.context.any_gpr(self.masm)?;
self.masm.mov(
writable!(mem),
RegImm::i32(arg.memory as i32),
OperandSize::S32,
)?;
self.context
.stack
.push(TypedReg::new(WasmValType::I32, mem).into());
Copy link
Member

Choose a reason for hiding this comment

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

Given that the target memory is an immediate, and immediate values in the stack don't affect the stack ordering principle (since they are not spilled), instead of moving it to a register, it should be possible to insert the immediate directly, this would save one memory write and load at the builtin call site.

We use such pattern extensively in the table operations.

Comment on lines 1465 to 1470
// compute the offset if necessary.
self.masm.extend(
writable!(addr.reg),
addr.reg,
ExtendKind::Unsigned(Extend::I64Extend32),
)?;
Copy link
Member

Choose a reason for hiding this comment

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

I think this extend shouldn't be emitted unconditionally: in the case of memory64, the address is expected to be 64 bits, so there shouldn't be a need to emit this.

Also, 32-bit loads are zero extend by default, so I think we can omit this extend entirely?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me check that again, if I recall correctly I added it because it didn't work like I expected with negative offsets

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nevermind, I must have imagined that...

Comment on lines 1520 to 1540
// pop the arguments from the stack.
let count = self.context.pop_to_reg(self.masm, None)?;
let addr = self.context.pop_to_reg(self.masm, None)?;

// put the target memrory index in a register, and push it as the first argument.
let mem = self.context.any_gpr(self.masm)?;
self.masm.mov(
writable!(mem),
RegImm::i32(arg.memory as i32),
OperandSize::S32,
)?;
self.context
.stack
.push(TypedReg::new(WasmValType::I32, mem).into());

// compute offset if necessary.
self.masm.extend(
writable!(addr.reg),
addr.reg,
ExtendKind::Unsigned(Extend::I64Extend32),
)?;
Copy link
Member

Choose a reason for hiding this comment

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

Similar comments here regarding the immediate and the stack and also the extend.

@MarinPostma
Copy link
Contributor Author

MarinPostma commented Jan 27, 2025

I made the suggested changes. I think we could avoid popping the stack in the case where we don't have to compute an offset, and just insert the target memory instead, but it complicates the implementation slightly. Happy to do it if you think it's better.

Copy link
Member

@saulecabrera saulecabrera left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

@saulecabrera saulecabrera added this pull request to the merge queue Jan 27, 2025
Merged via the queue into bytecodealliance:main with commit 1bd66bf Jan 27, 2025
39 checks passed
@saulecabrera saulecabrera mentioned this pull request Jan 27, 2025
67 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
winch Winch issues or pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants