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: Delete SelectIf instruction #5888

Merged
merged 5 commits into from
Apr 11, 2023

Conversation

afonso360
Copy link
Contributor

👋 Hey,

This PR deletes the Inst::SelectIf pseudo instruction from the RISC-V backend. This instruction was only used when lowering the select_spectre_guard clif instruction in a specific case of the input being preceded by an icmp. That being said, the instruction doesn't really do anything different from the regular select.

This PR removes the instruction and defaults to the regular Inst::Select. We get pretty much exactly the same codegen, with the exception that an additional andi is now added into the generated code. That doesn't seem to be to bad, and we can probably remove it with some additional effort if needed.

@afonso360 afonso360 added cranelift Issues related to the Cranelift code generator cranelift:area:riscv64 Issues related to the RISC-V 64 backend. labels Feb 27, 2023
@afonso360 afonso360 marked this pull request as draft February 28, 2023 19:10
@afonso360 afonso360 force-pushed the riscv64-remove-selectif branch from 1dbba89 to 50a723d Compare March 2, 2023 11:38
@afonso360

This comment was marked as outdated.

@afonso360 afonso360 marked this pull request as ready for review March 2, 2023 11:41
@afonso360 afonso360 force-pushed the riscv64-remove-selectif branch from 50a723d to 497922f Compare April 7, 2023 13:24
@afonso360 afonso360 requested a review from a team as a code owner April 7, 2023 13:24
@afonso360 afonso360 requested review from elliottt and removed request for a team April 7, 2023 13:24
Copy link
Member

@elliottt elliottt left a comment

Choose a reason for hiding this comment

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

This is great! I had a question about perhaps reusing lower_bmask and reworking the SelectReg lowering, but they're not blockers in my mind.

cranelift/codegen/src/isa/riscv64/lower.isle Outdated Show resolved Hide resolved
(c Reg (rv_srli b (imm12_const 63)))
(non_zero Reg (rv_andi c (imm12_const 1)))
;; Based on the 0 or 1 value we now build a mask that is either 0 or -1
(mask Reg (rv_addi non_zero (imm12_const -1)))
Copy link
Member

Choose a reason for hiding this comment

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

Is the reason we can't use lower_bmask here its ultimate use of gen_select_reg? Would it be bad to rework the lowering of SelectReg to take this approach instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hadn't even considered that this was a bmask operation! And yeah, I think this is a better lowering than what we currently have. However I check what LLVM produces for bmask and It's slightly better, so I'm going to rework bmask to use that and use that here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm much happier with the output of this now, Thanks!


I'm now re-reading your comment, and realizing there was a second part about SelectReg that I missed.

I have somewhat mixed feelings about making Inst::SelectReg do something like this, It would be quite a bit longer (~4 instructions I think?). But it would be really nice since we could move it entirely into ISLE. However I don't think that justifies it, it's a really common operation in the backend.

I'm willing to be persuaded otherwise though!

Copy link
Member

Choose a reason for hiding this comment

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

My worry with recommending lower_bmask was that it used gen_select_reg internally, which will produce jumps to select the result. Since lower_bmask no longer relies on gen_select_reg, I don't see any reason to rework the SelectReg emit code :)

Copy link
Member

@elliottt elliottt left a comment

Choose a reason for hiding this comment

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

This looks great to me, thanks Afonso!

Comment on lines +15 to +17
; li t1,42
; andi t4,a0,255
; andi t1,t1,255
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice if normalize_cmp_value didn't normalize constants whose value already fit in the target type, but there's no need to fix that on this PR.

@afonso360 afonso360 added this pull request to the merge queue Apr 11, 2023
Merged via the queue into bytecodealliance:main with commit 4c32dd7 Apr 11, 2023
@afonso360 afonso360 deleted the riscv64-remove-selectif branch April 11, 2023 18:15
brendandburns pushed a commit to brendandburns/wasmtime that referenced this pull request Apr 13, 2023
* riscv64: Delete `SelectIf` instruction

* riscv64: Fix typo in comment

Co-authored-by: Trevor Elliott <[email protected]>

* riscv64: Improve `bmask` codegen

* riscv64: Use `lower_bmask` in `select_spectre_guard`

* riscv64: Use `lower_bmask` to extend values in `select_spectre_guard`

Co-authored-by: Trevor Elliott <[email protected]>

---------

Co-authored-by: Trevor Elliott <[email protected]>
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Apr 16, 2023
* riscv64: Delete `SelectIf` instruction

* riscv64: Fix typo in comment

Co-authored-by: Trevor Elliott <[email protected]>

* riscv64: Improve `bmask` codegen

* riscv64: Use `lower_bmask` in `select_spectre_guard`

* riscv64: Use `lower_bmask` to extend values in `select_spectre_guard`

Co-authored-by: Trevor Elliott <[email protected]>

---------

Co-authored-by: Trevor Elliott <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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