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

riscv64: Initial SIMD Vector Implementation #6240

Merged
merged 14 commits into from
Apr 20, 2023

Conversation

afonso360
Copy link
Contributor

@afonso360 afonso360 commented Apr 19, 2023

👋 Hey,

This is the initial PR for implementing SIMD types with RISC-V Vectors, as described in #6118.

This PR implements only iadd,load,store for SIMD types, the minimum required to get an iadd runtest working.

We currently only support 128bit SIMD types, but It's really easy to change that to 64bit or 32bit or any other width SIMD types as long as they fit in a single register, so I'm also planning on doing that in a future PR.

I didn't include any calling conventions changes, so we have a non standard calling convention for now. The RISC-V Calling Convention passes everything via stack and does not use vector registers to pass arguments. We've inherited the Floating Point part of the calling convention, so we are passing arguments in vector registers. I didn't want to make the initial PR too large, so I decided to make that part of a future PR.

QEMU's register size is set to 256 bits to avoid accidentally depending on 128b vector registers in the future.

None of the vector instructions are recognized by capstone so they show up as .byte directives. I've been manually checking these against llvm-objdump.

@afonso360 afonso360 added the cranelift:area:riscv64 Issues related to the RISC-V 64 backend. label Apr 19, 2023
@afonso360 afonso360 requested review from a team as code owners April 19, 2023 12:12
@afonso360 afonso360 requested review from abrown and removed request for a team April 19, 2023 12:12
Comment on lines +2849 to +2853
let addr = writable_spilltmp_reg();
LoadConstant::U64(offset as u64)
.load_constant_and_add(addr, from_reg)
.into_iter()
.for_each(|inst| inst.emit(&[], sink, emit_info, state));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This ends up generating a lot of instructions, and is far from ideal. But It's also what we do in regular loads/stores, so I figured it would be best to tackle those together in a future PR.

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. labels Apr 19, 2023
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Some drive-by thoughts from me, but this great to see! Thanks for working on this!

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Out of curiosity, @afonso360 do you have a planned reviewer or set-of-reviewers for the SIMD support for RISC-V? If not I'm giving my stamp of approval here as this is well designed, comprehensively tested, and follows the design of #6118 as well (at least all in my own opinion). I'm happy of course though to defer to the more seasoned Cranelift developers as well too.

@afonso360
Copy link
Contributor Author

Out of curiosity, @afonso360 do you have a planned reviewer or set-of-reviewers for the SIMD support for RISC-V?

Not really, I didn't have anyone in mind. So if anyone would like to volunteer it would be appreciated! Otherwise I'll merge this later today / tomorrow.

Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

I'll add a second r+ for the EmitState bits to update implicit vector-machine state: they are indeed exactly how I expected them to be as well, and the infra is minimally intrusive. Thanks!

Copy link
Contributor

@jameysharp jameysharp left a comment

Choose a reason for hiding this comment

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

Oh, thanks for reminding me this is ready for review, Alex!

I have no concerns about the (quite minimal!) changes to the target-agnostic parts of Cranelift, and I think the implementation of the riscv-specific parts looks great too. I do have one question about the latter, but Afonso, I'll take your word for it either way. So feel free to merge if you're happy with it!

.emit(&[], sink, emit_info, state);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not too familiar with how islands work, so: Does the emission of this instruction need to happen after let mut start_off = sink.cur_offset(); below?

Copy link
Contributor Author

@afonso360 afonso360 Apr 20, 2023

Choose a reason for hiding this comment

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

I think calling emit before that check is correct. Since we recurse into Inst::emit for the vsetivli, we pass through that check again before emitting the instruction, and then once that is emitted we check it again before pushing the original instruction.

@afonso360 afonso360 added this pull request to the merge queue Apr 20, 2023
@afonso360
Copy link
Contributor Author

🎉

Merged via the queue into bytecodealliance:main with commit 60e4a00 Apr 20, 2023
@afonso360 afonso360 deleted the riscv-vector branch April 20, 2023 22:25
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Apr 28, 2023
* riscv64: Remove unused code

* riscv64: Add vector types

* riscv64: Initial Vector ABI Load/Stores

* riscv64: Vector Loads/Stores

* riscv64: Fix `vsetvli` encoding error

* riscv64: Add SIMD `iadd` runtests

* riscv64: Rename `VecSew`

The SEW name is correct, but only for VType. We also use this type
in loads/stores as the Efective Element Width, so the name isn't
quite correct in that case.

* ci: Add V extension to RISC-V QEMU

* riscv64: Misc Cleanups

* riscv64: Check V extension in `load`/`store` for SIMD

* riscv64: Fix `sumop` doc comment

* cranelift: Fix comment typo

* riscv64: Add convert for VType and VecElementWidth

* riscv64: Remove VecElementWidth converter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:riscv64 Issues related to the RISC-V 64 backend. cranelift Issues related to the Cranelift code generator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants