Stream: git-wasmtime

Topic: wasmtime / issue #1067 Bitfield Extract/Create instructions


view this post on Zulip Wasmtime GitHub notifications bot (May 04 2022 at 20:42):

cfallin labeled issue #1067:

This would add two new instructions to cranelift, bextr and bmak. They provide a easy to optimize, and easy to generate mechanism for bitfield manipulation.

bextr

a = bextr target, size, offset

bextr will extract a _n_ bit large field at the provided offset, and return it in a, shifted so that the LSB of the field is also the LSB of a.
a's type is inferred from target

Visual example

offset = 4
size = 4
typeof target = i8

xxxx **** 
\__/----V
0000 xxxx

bmak

a = bmak target, size, offset

bmak will fill a _n_ bit large field at the provided offset with 1s, the rest with 0. The generated field will then be applied to target using a bitwise AND, and the result will be returned in a.
a's type is inferred from target
bmak is designed so that you can simply use a bitwise or to combine the fields.

Visual Example

offset = 2
size = 4
typeof target = i8

xx0101xx
  \__/
  /  \
00010100

view this post on Zulip Wasmtime GitHub notifications bot (May 04 2022 at 20:42):

cfallin labeled issue #1067:

This would add two new instructions to cranelift, bextr and bmak. They provide a easy to optimize, and easy to generate mechanism for bitfield manipulation.

bextr

a = bextr target, size, offset

bextr will extract a _n_ bit large field at the provided offset, and return it in a, shifted so that the LSB of the field is also the LSB of a.
a's type is inferred from target

Visual example

offset = 4
size = 4
typeof target = i8

xxxx **** 
\__/----V
0000 xxxx

bmak

a = bmak target, size, offset

bmak will fill a _n_ bit large field at the provided offset with 1s, the rest with 0. The generated field will then be applied to target using a bitwise AND, and the result will be returned in a.
a's type is inferred from target
bmak is designed so that you can simply use a bitwise or to combine the fields.

Visual Example

offset = 2
size = 4
typeof target = i8

xx0101xx
  \__/
  /  \
00010100

view this post on Zulip Wasmtime GitHub notifications bot (Nov 20 2025 at 18:04):

cfallin commented on issue #1067:

We discussed this during old-issue triage today and agreed that it is probably better to have users implement this behavior with shifts/masks and then pattern-match that during isel if a particular target ISA has a nice instruction for it. This fits better with the general trend of making CLIF a simple core IR, especially as not every ISA has such instructions -- this would have to be re-expanded in backends otherwise, and only after the point that that expansion could be optimized. I'll close this issue but thank you for the discussion!

view this post on Zulip Wasmtime GitHub notifications bot (Nov 20 2025 at 18:04):

cfallin closed issue #1067:

This would add two new instructions to cranelift, bextr and bmak. They provide a easy to optimize, and easy to generate mechanism for bitfield manipulation.

bextr

a = bextr target, size, offset

bextr will extract a _n_ bit large field at the provided offset, and return it in a, shifted so that the LSB of the field is also the LSB of a.
a's type is inferred from target

Visual example

offset = 4
size = 4
typeof target = i8

xxxx **** 
\__/----V
0000 xxxx

bmak

a = bmak target, size, offset

bmak will fill a _n_ bit large field at the provided offset with 1s, the rest with 0. The generated field will then be applied to target using a bitwise AND, and the result will be returned in a.
a's type is inferred from target
bmak is designed so that you can simply use a bitwise or to combine the fields.

Visual Example

offset = 2
size = 4
typeof target = i8

xx0101xx
  \__/
  /  \
00010100

view this post on Zulip Wasmtime GitHub notifications bot (Nov 20 2025 at 19:28):

moonheart08 commented on issue #1067:

We discussed this during old-issue triage today and agreed that it is probably better to have users implement this behavior with shifts/masks and then pattern-match that during isel if a particular target ISA has a nice instruction for it. This fits better with the general trend of making CLIF a simple core IR, especially as not every ISA has such instructions -- this would have to be re-expanded in backends otherwise, and only after the point that that expansion could be optimized. I'll close this issue but thank you for the discussion!

7 years later with a stronger understanding of compilers and architectures, and I agree this is probably the right approach. Yet another issue I didn't remember creating resolved today!

view this post on Zulip Wasmtime GitHub notifications bot (Aug 21 2026 at 04:11):

Rafferty97 commented on issue #1067:

Hi, I'm writing a compiler that does a lot of bit manipulation, and using cranelift for the backend.

I've noticed that quite a few instances where two instructions could be compressed into a single UBFM or SBFM instruction.

I'd be willing to open a PR that adds the appropriate ISLE rules, if it's likely to be merged?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 21 2026 at 04:19):

cfallin commented on issue #1067:

@Rafferty97 yes, PRs that improve instruction selection are always welcome!

view this post on Zulip Wasmtime GitHub notifications bot (Aug 21 2026 at 04:20):

Rafferty97 commented on issue #1067:

@Rafferty97 yes, PRs that improve instruction selection are always welcome!

Awesome, thanks for the quick response! I'll work on it now and put up a PR when I can. Should I file a GitHub issue too or is a detailed PR sufficient?

view this post on Zulip Wasmtime GitHub notifications bot (Aug 21 2026 at 04:32):

cfallin commented on issue #1067:

A PR is fine, with a reasonable description and ideally a test showing the new lowering -- no need to file a specific issue beforehand.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 21 2026 at 09:14):

Rafferty97 commented on issue #1067:

Hey @cfallin, it was more effort than I anticipated, but I finally got a PR up :)

https://github.com/bytecodealliance/wasmtime/pull/14187


Last updated: Aug 30 2026 at 09:07 UTC