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: Load constants from the constant pool #6933

Merged
merged 8 commits into from
Aug 30, 2023

Conversation

afonso360
Copy link
Contributor

👋 Hey,

This is a follow up to #6915 (and #5578 I guess). This PR enables loading ISLE constants from the constant pool.

Not all constants are loaded from the constant pool. Constants that are observed at emit time are still loaded inline since I couldn't find a way of registering them with the MachBuffer for loading.

This also causes a slight pessimization of the dynamic instruction count since we currently force the address to be fully loaded into a register before a load when resolving labels. This is easily fixable, but I'm going to do it in a follow-up PR.

@afonso360 afonso360 added the cranelift:area:riscv64 Issues related to the RISC-V 64 backend. label Aug 30, 2023
@afonso360 afonso360 requested a review from a team as a code owner August 30, 2023 13:41
@afonso360 afonso360 requested review from abrown and removed request for a team August 30, 2023 13:41
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.

Nice! One comment about something that's perhaps preexisting, but this looks good to me regardless of that.

Constants that are observed at emit time are still loaded inline since I couldn't find a way of registering them with the MachBuffer for loading.

My guess is that if possible it'd be best to break up the macro-instruction into its components to handle this. That way instead of using macro-instructions the helpers in ISLE can be used. This may not always be applicable though.

I did look at a few of these in emit.rs though and they were related to things like sp adjustments and stack probe loops which I think could also reasonably be switched over to asserting that the constant fits in an instruction rather than requiring a pool. (so long as the range matches what one would reasonably expect for these situations)

Comment on lines +1555 to +1559
(rule 0 (imm (ty_int ty) c)
(emit_load
(LoadOP.Ld)
(mem_flags_trusted)
(gen_const_amode (emit_u64_le_const c))))
Copy link
Member

Choose a reason for hiding this comment

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

This is preexisting I think, and probably affects other backends too, but technically emitting a 64-bit constant here is over-approximating how large it needs to be, right? In that only the ty-width sized bit-pattern of c is all that matters so for a 32-bit constant the 64-bit size is overkill?

No need to fix here since this preserves what was done before, but may be worth trying to fix later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My plan is to eventually fix #6922 and then we should be able to change this to only ever trigger for 64 bit constants, since all other constants should be able to be emitted with the previous rules (i.e. with constant materialization instructions).

@afonso360
Copy link
Contributor Author

afonso360 commented Aug 30, 2023

My guess is that if possible it'd be best to break up the macro-instruction into its components to handle this. That way instead of using macro-instructions the helpers in ISLE can be used. This may not always be applicable though.

I did look at a few of these in emit.rs though and they were related to things like sp adjustments and stack probe loops which I think could also reasonably be switched over to asserting that the constant fits in an instruction rather than requiring a pool. (so long as the range matches what one would reasonably expect for these situations)

We already sort of do this. We have Inst::load_constant_u64 that tries to pattern match either a single addi or a addi+lui before falling back to the LoadInlineConst instruction as a last resort.

I'd eventually like to try and fully merge the load constant logic with some ISLE rules, but I'm not entirely sure how to do that.

Right now both Inst::load_constant_u64 and ISLE call into Inst::generate_imm, but I'd like to build some more complex pattern matching in ISLE and still have it available in emit.rs

@afonso360 afonso360 enabled auto-merge August 30, 2023 14:59
@alexcrichton
Copy link
Member

Makes sense! I was thinking that the "solution" there is to slowly whittle away at callers to Inst::load_constant_u64 as opposed to refactoring the internals of that to use ISLE (which would probably be different given the required contexts), but that's naturally much easier said than done

@afonso360 afonso360 added this pull request to the merge queue Aug 30, 2023
Merged via the queue into bytecodealliance:main with commit 134dddc Aug 30, 2023
@afonso360 afonso360 deleted the riscv-pool-constants branch August 30, 2023 16:16
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Sep 6, 2023
* riscv64: Use `Inst::load_u64_constant` when loading load offsets

* riscv64: Load ISLE constants from pool

* riscv64: Delete `offset32_imm`

* riscv64: Delete `pack_float_rounding_mode`

* riscv64: Delete `LoadConst32` instruction

* riscv64: Delete LoadConstant code

Move that logic into `LoadInlineConst`

* riscv64: Fix `LoadAddr` instructions for RegOffset

We were accidentally overriding the offset with the base.

* riscv64: Update inst_worst_case_size
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants