Skip to content

x64: Remove unnecessary register use when comparing against constants#4645

Merged
elliottt merged 4 commits into
bytecodealliance:mainfrom
elliottt:trevor/x64-opt-cmp-with-constant
Aug 9, 2022
Merged

x64: Remove unnecessary register use when comparing against constants#4645
elliottt merged 4 commits into
bytecodealliance:mainfrom
elliottt:trevor/x64-opt-cmp-with-constant

Conversation

@elliottt
Copy link
Copy Markdown
Member

@elliottt elliottt commented Aug 8, 2022

When lowering instructions like icmp eq (iconst 0), v2, we currently store the constant 0 in a register before performing the comparison. The result is that for inputs like the following:

  v1 = iconst.i32 1
  v2 = load.i32 v0
  v3 = icmp eq v1, v2
  brnz v3, block1

we generate this assembly:

  movl    0(%rdi), %r9d                                                              
  movl    $1, %r10d                                                                  
  cmpl    %r9d, %r10d                                                                
  jz      label1; j label2 

This PR introduces a case into emit_cmp that handles constants on the left-hand side of a comparison by swapping the order and reversing the operation. With this new rule, we emit the following assembly for the clif snippet above:

  movl    0(%rdi), %r8d                                                          
  cmpl    $1, %r8d 
  jz      label1; j label2 

Comment thread cranelift/codegen/src/isa/x64/inst.isle Outdated
;; As a special case, reverse the arguments to the comparison when the LHS is a
;; constant. This ensures that we avoid moving the constant into a register when
;; performing the comparison.
(rule 1 (emit_cmp cc (and (simm32_from_value a) (value_type ty)) b)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not super happy with the need for a rule priority here, and would have expected the simm32_from_value use to render it unnecessary. Any idea why it doesn't fire without higher priority?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This can go away once #4661 is merged.

@elliottt elliottt marked this pull request as ready for review August 8, 2022 20:39
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:area:x64 Issues related to x64 codegen isle Related to the ISLE domain-specific language labels Aug 8, 2022
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Aug 8, 2022

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "cranelift:area:x64", "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.

Copy link
Copy Markdown
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.

This looks good to me as-is, but I'm curious to see if the rule-priority tweak we talked about today removes the need for the priority here before we merge it...

@elliottt
Copy link
Copy Markdown
Member Author

elliottt commented Aug 9, 2022

This looks good to me as-is, but I'm curious to see if the rule-priority tweak we talked about today removes the need for the priority here before we merge it...

The bugfix you linked removes the need for the explicit priority here, so I'll make a separate PR for that before merging this without the priority 👍

@elliottt elliottt force-pushed the trevor/x64-opt-cmp-with-constant branch from bb4bbfb to 8bb3209 Compare August 9, 2022 20:11
@elliottt elliottt enabled auto-merge (squash) August 9, 2022 20:43
@elliottt elliottt force-pushed the trevor/x64-opt-cmp-with-constant branch from 8bb3209 to 8f6762b Compare August 9, 2022 21:13
@elliottt elliottt force-pushed the trevor/x64-opt-cmp-with-constant branch from 8f6762b to dfc12a0 Compare August 9, 2022 22:34
@elliottt elliottt merged commit 63c2d1e into bytecodealliance:main Aug 9, 2022
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 isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants