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

Document wasm abi quirks #663

Merged
merged 2 commits into from
Aug 28, 2024
Merged

Conversation

Manishearth
Copy link
Contributor

Related: #661

I've done all this research, we should document it so that it can be easily referenced.


In "padded direct" mode, every transitive scalar field is passed through as an argument just like "direct", however _padding is passed through as well_.

`MyStruct` above contains 3 bytes of padding between the two fields. In "padded direct" mode, this would be invoked as `wasm.takes_struct(a, 0, 0, 0, b)`, with each 0 subbing in for padding. The LLVM IR would look something like:
Copy link
Contributor

Choose a reason for hiding this comment

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

oh my

It seems like it would be clean and less error-prone for there to be an ABI that passed all the transitive scalars, but without these padding arguments that don't do anything.

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! But there are also some downsides of direct passing for large structs where indirect passing is nicer.

It would be really nice if there were ways to manipulate the wasm stack whilst indirect passing so you don't have to heap allocate.

An optimization we could do is have a shared parameter-passing mini allocator for Diplomat.

```llvm
; Edited from IR produced by rustc with (default) -Zwasm-c-abi=legacy
%MyStruct = type { i8, [3 x i8], i32 }
define dso_local void @takes_struct(%MyStruct %0) unnamed_addr #0 { ... }
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion (optional): consider including the WebAssembly Text (WAT) of the exported function signature, or at least mention how this IR is translated to WebAssembly Text. As a reader of this document, I feel fairly comfortable with WebAssembly Text but not as much in LLVM IR.

For example, one thing that confuses me is that this signature seems to take 5 integers, but 4 of them are i8s, and i8 is not a type in Wasm. Only i32 and i64.

https://webassembly.github.io/spec/core/syntax/types.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new section on parameter types (previously, the "u64" section) explains this


## u64

While all "normal" scalar parameters in wasm are regular Number types, `u64` translates to a `BigInt`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a browser engine thing, not a WebAssembly or Rust thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a JS thing, not a browser thing, but I'll rephrase this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved it to the top of the file

@Manishearth Manishearth merged commit beb493c into rust-diplomat:main Aug 28, 2024
16 checks passed
@Manishearth Manishearth deleted the wasm-abi-quirks branch August 28, 2024 06:53
Copy link
Contributor

@sffc sffc left a comment

Choose a reason for hiding this comment

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

Thanks for the updates!

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.

2 participants