Repository navigation
imul instruction size misprediction - #57179
Merged
Merged
Conversation
kunalspathak
marked this pull request as ready for review
August 12, 2021 14:18
Contributor
Author
|
@dotnet/jit-contrib |
Contributor
Author
|
Can someone take a look so we can get this in before next week? @echesakov or @tannergooding |
tannergooding
approved these changes
Aug 13, 2021
tannergooding
left a comment
Member
There was a problem hiding this comment.
Changes LGTM for a quick fix.
We should log an issue to track fixing this properly (likely by centralizing the size computation logic so we don't risk duplicating checks, etc).
Contributor
Author
Sure, I have created #57368 and included links to multiple issues that talks about it. |
briansull
approved these changes
Aug 13, 2021
briansull
left a comment
Contributor
There was a problem hiding this comment.
I reviewed it and it looks OK
(and better than before)
echesakov
reviewed
Aug 13, 2021
Contributor
Author
|
no asmdiff in benchmarks/libraries. Minor improvements in coreclr_tests: |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
During emitting
imul, we wrongly predict the size of instruction as16 byteswhereas it should be just15 bytes(which is debatable, see below) comprising of 1 byteREX prefix+ 1 byteopcode+ 1 bytemodR/M+ 4 bytesRIP-relative displacement+ 4 bytes ofimmediate. Last row in https://www.felixcloutier.com/x86/imul table.Now as per Intel docs:
which means the RIP immediate above should be just 4-bytes and the instruction size should be just
11 bytes. But inemitInsSizeCV()we have it calculated asvalSize == 8.When calculating the size, it seems that we double calculate the
REX prefix size, once inruntime/src/coreclr/jit/emitxarch.cpp
Line 2612 in 53a1ddc
and then further in
emitInsSize() -> emitGetPrefixSize()inruntime/src/coreclr/jit/emitxarch.cpp
Lines 1188 to 1191 in 53a1ddc
Because of above conditions, we end up with size of
16 bytesbut when we save the size ininstrDesc, we cap it to15 bytesbecause of #12840.runtime/src/coreclr/jit/emit.h
Lines 887 to 896 in d20606c
Further, we account the wrong size of instruction
16 bytesinstead of the one we have inid->CodeSize()of15 bytes.runtime/src/coreclr/jit/emitxarch.cpp
Lines 5596 to 5601 in 53a1ddc
Due to this, the
emitCurIGSizeand henceig->igOffsbecomes off by 1 byte and during emitting, we think that we just over-estimated the instruction by 4 bytes = 15 (id->CodeSize()) - 11 (actual encoding size) but the offsets were calculated assuming the instruction size of16 bytes. That difference of 1 byte leads to an assert in #57041.To summarize, there are 3 problems:
This is fixed by tracking if we have already counted REX prefix size and if yes, do not include it in
emitGetPrefixSize().valSize > 4for RIP-immediate . I have added an assert in this draft PR to see if there are any instructions other thanmovfor which we getvalSize > 4other wise, we should just update this code to capvalSizeto 4 bytes.This is fixed by making sure we cap
valSizeto 4 bytes for all instructions exceptmov.This problem should never arise because I have removed the code that caps
szto 15 bytes and added an assert forsz <= 15. With that, it will be safe to useemitCurIGsize += sz.Thanks @tannergooding for pointing me to manuals.
Fixes: #12840, #57041