Repository navigation
perf(x64): optimize i8 multiplication by constants #14796
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
Open
shrayjaiswal050-design
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
shrayjaiswal050-design:fix/x64-i8-imul-imm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| test compile | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this use the |
||
| set unwind_info=false | ||
| target x86_64 | ||
| set opt_level=speed | ||
| target x86_64 | ||
|
|
||
| ;; Regression test for #14660: use a 32-bit immediate multiply for an i8 | ||
| ;; constant on either side, without a byte multiply or constant-pool load. | ||
|
|
||
| function %imul_i8_const_rhs(i8) -> i8 { | ||
| block0(v0: i8): | ||
| v1 = iconst.i8 97 | ||
| v2 = imul v0, v1 | ||
| return v2 | ||
| } | ||
|
|
||
| ; not: mulb | ||
| ; not: %rip | ||
| ; check: block0: | ||
| ; nextln: imull $$0x61, %edi, %eax | ||
| ; nextln: movq %rbp, %rsp | ||
| ; nextln: popq %rbp | ||
| ; nextln: retq | ||
| ; not: mulb | ||
| ; not: %rip | ||
|
|
||
| function %imul_i8_const_lhs(i8) -> i8 { | ||
| block0(v0: i8): | ||
| v1 = iconst.i8 97 | ||
| v2 = imul v1, v0 | ||
| return v2 | ||
| } | ||
|
|
||
| ; not: mulb | ||
| ; not: %rip | ||
| ; check: block0: | ||
| ; nextln: imull $$0x61, %edi, %eax | ||
| ; nextln: movq %rbp, %rsp | ||
| ; nextln: popq %rbp | ||
| ; nextln: retq | ||
| ; not: mulb | ||
| ; not: %rip | ||
|
|
||
| ;; The i8 load must stay a byte load. In particular, do not fold it into | ||
| ;; a 32-bit memory operand of imull, which would read past the input byte. | ||
| function %imul_i8_load_const_rhs(i64) -> i8 { | ||
| block0(v0: i64): | ||
| v1 = load.i8 v0 | ||
| v2 = iconst.i8 97 | ||
| v3 = imul v1, v2 | ||
| return v3 | ||
| } | ||
|
|
||
| ; not: mulb | ||
| ; not: %rip | ||
| ; check: block0: | ||
| ; nextln: movzb$(=q|l) (%rdi), %$(=[a-z0-9]+) | ||
| ; nextln: imull $$0x61, %$(=[a-z0-9]+), %eax | ||
| ; nextln: movq %rbp, %rsp | ||
| ; nextln: popq %rbp | ||
| ; nextln: retq | ||
| ; not: mulb | ||
| ; not: %rip | ||
|
|
||
| function %imul_i8_load_const_lhs(i64) -> i8 { | ||
| block0(v0: i64): | ||
| v1 = load.i8 v0 | ||
| v2 = iconst.i8 97 | ||
| v3 = imul v2, v1 | ||
| return v3 | ||
| } | ||
|
|
||
| ; not: mulb | ||
| ; not: %rip | ||
| ; check: block0: | ||
| ; nextln: movzb$(=q|l) (%rdi), %$(=[a-z0-9]+) | ||
| ; nextln: imull $$0x61, %$(=[a-z0-9]+), %eax | ||
| ; nextln: movq %rbp, %rsp | ||
| ; nextln: popq %rbp | ||
| ; nextln: retq | ||
| ; not: mulb | ||
| ; not: %rip | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| test interpret | ||
| test run | ||
| target x86_64 | ||
| set opt_level=speed | ||
| target x86_64 | ||
|
|
||
| ;; Regression test for #14660: widening an i8 constant multiply to i32 must | ||
| ;; preserve wrapping i8 results, including negative and unsigned constants. | ||
|
|
||
| function %imul_i8_const_rhs(i8) -> i8 { | ||
| block0(v0: i8): | ||
| v1 = iconst.i8 97 | ||
| v2 = imul v0, v1 | ||
| return v2 | ||
| } | ||
| ; run: %imul_i8_const_rhs(0) == 0 | ||
| ; run: %imul_i8_const_rhs(2) == 0xc2 | ||
| ; run: %imul_i8_const_rhs(0x7f) == 0x1f | ||
| ; run: %imul_i8_const_rhs(0x80) == 0x80 | ||
| ; run: %imul_i8_const_rhs(0xff) == 0x9f | ||
|
|
||
| function %imul_i8_const_lhs(i8) -> i8 { | ||
| block0(v0: i8): | ||
| v1 = iconst.i8 193 | ||
| v2 = imul v1, v0 | ||
| return v2 | ||
| } | ||
| ; run: %imul_i8_const_lhs(1) == 0xc1 | ||
| ; run: %imul_i8_const_lhs(2) == 0x82 | ||
| ; run: %imul_i8_const_lhs(0x7f) == 0xbf | ||
| ; run: %imul_i8_const_lhs(0xff) == 0x3f | ||
|
|
||
| ;; ireduce can leave junk in the register's high bits. Only the low byte of | ||
| ;; the product may survive the uextend, even when that byte has its sign bit set. | ||
| function %imul_i8_ireduce_negative(i64) -> i32 { | ||
| block0(v0: i64): | ||
| v1 = ireduce.i8 v0 | ||
| v2 = iconst.i8 -97 | ||
| v3 = imul v1, v2 | ||
| v4 = uextend.i32 v3 | ||
| return v4 | ||
| } | ||
| ; run: %imul_i8_ireduce_negative(0x1234_5678_9abc_de00) == 0 | ||
| ; run: %imul_i8_ireduce_negative(0x1234_5678_9abc_de01) == 159 | ||
| ; run: %imul_i8_ireduce_negative(0x1234_5678_9abc_de02) == 62 | ||
| ; run: %imul_i8_ireduce_negative(0x1234_5678_9abc_de80) == 128 | ||
| ; run: %imul_i8_ireduce_negative(-1) == 97 | ||
|
|
||
| function %imul_i8_loaded(i64) -> i8 { | ||
| block0(v0: i64): | ||
| v1 = load.i8 v0 | ||
| v2 = iconst.i8 193 | ||
| v3 = imul v2, v1 | ||
| return v3 | ||
| } | ||
|
|
||
| ;; Use a separate function so store-to-load forwarding cannot remove the load. | ||
| ;; The compile test also checks that the memory access is not widened. | ||
| function %imul_i8_load(i8) -> i8 { | ||
| ss0 = explicit_slot 1 | ||
| fn0 = %imul_i8_loaded(i64) -> i8 | ||
|
|
||
| block0(v0: i8): | ||
| v1 = stack_addr.i64 ss0 | ||
| store v0, v1 | ||
| v2 = call fn0(v1) | ||
| return v2 | ||
| } | ||
| ; run: %imul_i8_load(2) == 0x82 | ||
| ; run: %imul_i8_load(0x7f) == 0xbf | ||
| ; run: %imul_i8_load(0xff) == 0x3f |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this perhaps have rules for
$I16as well since it seems equally applicable? Also -- are you sure this doesn't need to extend thexandyarguments appropriately? I forget the rules around multiplication and bits so I'm not sure myself