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

Cranelift: Improve codegen of store_imm on x64 #6979

Merged
merged 3 commits into from
Sep 11, 2023

Conversation

mchesser
Copy link
Contributor

@mchesser mchesser commented Sep 8, 2023

Improve code generation of store_imm on x64 by adding a new rule that directly handles stores of small immediates.

Currently storing to constant to memory is achieved by first copying the constant into a temporary register, then performing a store of the register value to memory. For example:

    v1 = load.i64 v0
    v2 = iconst.i8 1
    store v2, v1

Is currently compiled to:

    MOV RCX,qword ptr [RDI]
    MOV EDX,0x1
    MOV byte ptr [RCX],DL

On x86 constants can be stored directly to memory, e.g., after this patch, the constant is inlined into the store instruction removing the extra temporary register.

    MOV RCX,qword ptr [RDI]
    MOV byte ptr [RCX],0x1

The overall performance impact is probably very minimal, but it could help with frontend/decoder pressure in cases where this pattern is common.

(Draft PR since this was partially an exercise in understanding ISLE so I could be missing something here).

Adds a new x64 rule for directly lowering stores of immediates with a MOV instruction.
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:x64 Issues related to x64 codegen labels Sep 8, 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.

Thanks for this!

There's a few tests which need to be updated which can be done with CRANELIFT_TEST_BLESS=1 cargo run test filetests from the cranelift directory. Otherwise though could you additionally add some *.clif tests in the cranelift/filetests/filetests/isa/x64/ directory specifically for these lowering rules? I think it'd be good to showcase a range of constants being stored to ensure that they call get codegen'd correctly.

Comment on lines 2915 to 2918
;; IMM stores
(rule 2 (lower (store flags (has_type (fits_in_64 ty) (iconst (u64_from_imm64 value))) address offset))
(side_effect
(x64_movimm_m ty (to_amode flags address offset) value)))
Copy link
Member

Choose a reason for hiding this comment

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

If you're up for it I think it might be good to update the MovImmM variant itself. Looking at emit.rs it will panic if the low 32-bits of the value here don't sign-extend into the full 64-bits, so the MovImmM should I think store i32 instead of i64. With that you could add extra matching here that value fits within a 32-bit signed integer.

@mchesser
Copy link
Contributor Author

Thanks for the review.

I've updated the MovImmM variant to only allow i32s. Based on my understanding, I believe using the simm32 helper for converting the imm is sufficient (since it returns None when the value doesn't fit), and it looks like the fallback case is hit when the value doesn't fit in the updated tests -- but let me know if this isn't the right way to do things.

Changing MovImmM also affected winch, however it looks like winch already panics for large values in this case so I just tried to keep the behavior the same.

@mchesser mchesser marked this pull request as ready for review September 10, 2023 04:00
@mchesser mchesser requested a review from a team as a code owner September 10, 2023 04:00
@mchesser mchesser requested review from elliottt and removed request for a team September 10, 2023 04:00
@github-actions github-actions bot added the winch Winch issues or pull requests label Sep 10, 2023
@github-actions
Copy link

Subscribe to Label Action

cc @saulecabrera

This issue or pull request has been labeled: "cranelift", "cranelift:area:x64", "winch"

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

  • saulecabrera: winch

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

Learn more.

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.

Thanks again for this!

Comment on lines +118 to +123
I::I64(v) => match v.try_into() {
Ok(v) => self.asm.mov_im(v, &dst, size),
Err(_) => {
panic!("Immediate-to-memory moves require immediate operand to sign-extend to 64 bits.");
}
},
Copy link
Member

Choose a reason for hiding this comment

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

cc @saulecabrera, this is making the preexisting panic more explicit but it's one where I think the Err case will want to be handled via a move-to-register then store instruction.

(in a future PR, no need to do anything here @mchesser)

@alexcrichton alexcrichton added this pull request to the merge queue Sep 11, 2023
Merged via the queue into bytecodealliance:main with commit 2186668 Sep 11, 2023
19 checks passed
alexcrichton pushed a commit that referenced this pull request Sep 12, 2023
* Improve lowering of store_imm on x64

Adds a new x64 rule for directly lowering stores of immediates with a MOV instruction.

* Ensure that the MovImmM operand fits in an i32 and add tests.

* Update winch to handle MovImmM change
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Sep 13, 2023
* Improve lowering of store_imm on x64

Adds a new x64 rule for directly lowering stores of immediates with a MOV instruction.

* Ensure that the MovImmM operand fits in an i32 and add tests.

* Update winch to handle MovImmM change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator winch Winch issues or pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants