Skip to content

riscv64: Add Zvbb (Vector Basic Bit-manipulation) extension support#13738

Open
rajeshgangam wants to merge 1 commit into
bytecodealliance:mainfrom
rajeshgangam:zvbb-extension
Open

riscv64: Add Zvbb (Vector Basic Bit-manipulation) extension support#13738
rajeshgangam wants to merge 1 commit into
bytecodealliance:mainfrom
rajeshgangam:zvbb-extension

Conversation

@rajeshgangam

Copy link
Copy Markdown

Summary

Add support for the RISC-V Zvbb extension in the Cranelift backend, implementing instruction selection and encoding for 13 vector bit-manipulation instructions.

  • Add has_zvbb feature flag for the riscv64 ISA
  • Implement encoding for vandn, vrol, vror (two-operand), vror.vi (immediate), and vbrev, vbrev8, vrev8, vclz, vctz, vcpop (single-operand VXUNARY0)
  • Add lowering rules gated by has_zvbb: band(x, bnot(y))vandn, popcntvcpop, rotlvrol.vx, rotrvror.vx
  • Without Zvbb, all operations fall back to existing software emulation paths

Instructions implemented

Instruction Encoding Cranelift IR pattern
vandn.vv / .vx funct6=0b000001, OPIVV/OPIVX band(x, bnot(y))
vrol.vv / .vx funct6=0b010101, OPIVV/OPIVX rotl(vec, scalar)
vror.vv / .vx / .vi funct6=0b010100, OPIVV/OPIVX/OPIVI rotr(vec, scalar)
vbrev.v funct6=0b010010, vs1=01010 (helper only)
vbrev8.v funct6=0b010010, vs1=01000 (helper only)
vrev8.v funct6=0b010010, vs1=01001 (helper only)
vclz.v funct6=0b010010, vs1=01100 (helper only)
vctz.v funct6=0b010010, vs1=01101 (helper only)
vcpop.v funct6=0b010010, vs1=01110 popcnt(vec)

Note: vclz, vctz, vbrev, vbrev8, vrev8 have ISLE helpers and correct encoding but no lowering rules yet because clz, ctz, bitrev, bswap are scalar-only in Cranelift IR. They are ready for use when vector-typed versions of these ops are added.

vwsll (widening shift left) is deferred to a follow-up PR due to the large number of widening combination rules needed.

Resolves #7187

Test plan

  • test compile precise-output tests verify correct instruction selection and encoding across i8x16, i32x4, i64x2 element types (zvbb.clif)
  • test run runtime tests verify correctness under QEMU riscv64 with zvbb=true (zvbb-run.clif)
  • All 235 existing riscv64 filetests pass (no regressions)
  • Fallback paths still produce correct results when has_zvbb is not set

Add support for the RISC-V Zvbb extension in the Cranelift backend.
This adds the `has_zvbb` feature flag and implements instruction
selection for the following Zvbb instructions:

Two-operand (VecAluOpRRR):
- vandn.vv / vandn.vx — vector AND-NOT
- vrol.vv / vrol.vx — vector rotate left
- vror.vv / vror.vx — vector rotate right

Immediate (VecAluOpRRImm5):
- vror.vi — vector rotate right by immediate

Single-operand (VecAluOpRR, VXUNARY0 encoding):
- vbrev.v — bit reverse
- vbrev8.v — bit reverse within bytes
- vrev8.v — byte reverse
- vclz.v — count leading zeros
- vctz.v — count trailing zeros
- vcpop.v — population count

Lowering rules (gated by has_zvbb):
- band(x, bnot(y)) → vandn.vv / vandn.vx
- popcnt → vcpop.v
- rotl → vrol.vx
- rotr → vror.vx

Resolves bytecodealliance#7187
@rajeshgangam rajeshgangam requested a review from a team as a code owner June 26, 2026 02:42
@rajeshgangam rajeshgangam requested review from cfallin and removed request for a team June 26, 2026 02:42
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:meta Everything related to the meta-language. isle Related to the ISLE domain-specific language labels Jun 26, 2026
@github-actions

Copy link
Copy Markdown

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "cranelift:meta", "isle"

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

  • cfallin: isle
  • fitzgen: isle

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

Learn more.

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Just one file-placement nit but otherwise LGTM. Happy to merge once that's done.

@@ -0,0 +1,39 @@
test run

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Run-tests should be in filetests/runtests/. The purpose of that shared (non-ISA-specific) location is so that we can enable other platforms too as the test becomes runnable on them.

Can you move it there, and then add at least Pulley (assuming it does not have missing vector support here) and also other platforms if they work?

@cfallin

cfallin commented Jun 28, 2026

Copy link
Copy Markdown
Member

(Logistical note: I'm on PTO and mid-roadtrip right now; was happy to review since it was on my queue but I'll reassign to @fitzgen for final signoff once the runtest is moved as per above)

@cfallin cfallin requested a review from fitzgen June 28, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:meta Everything related to the meta-language. cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

riscv64: Implement Zvbb - Vector Basic Bit-manipulation Extension

2 participants