WebAssembly / WebAssembly/binaryen

optimise LSBit mask (followed by branch) to use `ctz`

Open
#5,752 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
8.6k
Forks
885
Avg merge
1d 19h
Merged PRs (30d)
69

Description

I have code snippets like

i32.const 1
i32.and
i32.eqz
br_if 1 (;@1;)

which could be optimised to

i32.ctz
br_if 1 (;@1;)

Similar analogous transformations:

  • i32.const 1; i32.and; i32.eqz; if to i32.ctz; if
  • i32.const 1; i32.and; if to i32.ctz; if (with legs swapped)
  • i32.const 2_147_483_648; i32.and; if to i32.clz; if (with legs swapped)
  • i32.const 31; i32.shr_u; if to i32.clz; if (with legs swapped)
  • possibly 64-bit variants (but here probably instructions for truncation need to be added)
  • what about signedness testing? (cmp against 0 --> clz)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file, test, or entry point is named. Start by tracing the optimizer support for i32.and, i32.eqz, branches, ctz, and clz, then assess the listed 32-bit and possible 64-bit patterns; done means implementing agreed transformations with coverage for the applicable cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.