Stream: cranelift

Topic: Adding byteswap


view this post on Zulip Evan Thompson (Oct 13 2022 at 23:04):

Hey folks, I work on a networking project that currently rolls its own JIT for x64/aarch64, and am taking a look into swapping that over to use Cranelift instead. The main issue I'm seeing currently is a performance issue with the generated machine code - byteswaps are common in this space (endinaness conversion) and Cranelift has an open item but no existing support. I was thinking of working on adding a new opcode (Bswap, and possibly also Bswap32 and Bswap16) to the IR, and adding support to the codegen backends for emitting x64 and aarch64 bswap instructions. Would that be the right way to go about this, and is the project generally accepting of that sort of pull request?

There is an x86 instruction for this.

view this post on Zulip Afonso Bordado (Oct 13 2022 at 23:10):

Yeah, having a bswap instruction would be useful, and there are other cranelift users that need them! cg_clif a rust compiler that uses cranelift has to do some interesting... codegen to get around this.

It'd be great to have native support for that!

In general we support a generic instruction and have a polymorphic type, so for example, we only have one band but it has an implicit type operator that we can pass, (or it can be inferred) that makes it usable for many types, (i.e. band.i16 or band.i64, etc..)

Cranelift based backend for rustc. Contribute to bjorn3/rustc_codegen_cranelift development by creating an account on GitHub.

view this post on Zulip Afonso Bordado (Oct 13 2022 at 23:14):

If you need any help adding that to cranelift let me know, and I can guide you through the process.

view this post on Zulip Evan Thompson (Oct 13 2022 at 23:25):

Yeah, when trying out Cranelift I wrote some similarly-weird codegen to work around the lack of bswap, but it ended up being ~3x-5x slower than having a real bswap instruction at runtime, which is a real bummer in network code that's doing a bunch of host-to-network-order conversions. Thanks for the advice about the typing! I'll take a stab at this and reach out when I have questions

view this post on Zulip Afonso Bordado (Oct 13 2022 at 23:25):

:thumbs_up: Good luck!

view this post on Zulip Evan Thompson (Oct 27 2022 at 18:53):

Ok, continuing this discussion from a few weeks ago, here's a PR to add bswap: https://github.com/bytecodealliance/wasmtime/pull/5147

Adds Bswap to the Cranelift IR. Implements the Bswap instruction in the x64 and aarch64 codegen backends. Cranelift users can now: builder.ins().bswap(value) to get a native byteswap instruction. ...

view this post on Zulip Evan Thompson (Oct 27 2022 at 18:55):

This is my first time contributing to the project, please let me know who should be added as reviewers etc. I added unit tests for instruction encoding, and did some manual testing with a toy program and x86_64 and aarch64 machines to verify the bswap instruction worked. Please let me know what other test coverage might be expected

view this post on Zulip Chris Fallin (Oct 27 2022 at 18:59):

We'll take a look -- thanks a bunch for this! In general for reviews we don't really assign, we just have enough people watching the incoming PRs that someone will pick it up. One of us will get to it soon!

view this post on Zulip Evan Thompson (Oct 27 2022 at 19:00):

Awesome, thanks!


Last updated: Oct 23 2024 at 20:03 UTC