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

Make BSWAP16 nodes normalize upper 16 bits #67903

Merged
merged 3 commits into from
Apr 20, 2022

Conversation

jakobbotsch
Copy link
Member

Currently the JIT's constant folding (gtFoldExprConst and VNs
EvalOpSpecialized) assumes that BSWAP16 zero extends into the upper 16
bits. This was not the case, and in fact the behavior of BSWAP16
depended on platform.

Normally this would not be a problem since we always insert normalizing
casts when creating BSWAP16 nodes, however VN was smart enough to remove
this cast in some cases (see the test).

Change the semantics of BSWAP16 nodes to zero extend into the upper 16
bits to match constant folding, and add a small peephole to avoid
inserting this normalization in the common case where it is not
necessary.

Fixes #67723
Subsumes #67726

Currently the JIT's constant folding (gtFoldExprConst and VNs
EvalOpSpecialized) assumes that BSWAP16 zero extends into the upper 16
bits. This was not the case, and in fact the behavior of BSWAP16
depended on platform.

Normally this would not be a problem since we always insert normalizing
casts when creating BSWAP16 nodes, however VN was smart enough to remove
this cast in some cases (see the test).

Change the semantics of BSWAP16 nodes to zero extend into the upper 16
bits to match constant folding, and add a small peephole to avoid
inserting this normalization in the common case where it is not
necessary.

Fixes dotnet#67723
Subsumes dotnet#67726
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 12, 2022
@ghost ghost assigned jakobbotsch Apr 12, 2022
@ghost
Copy link

ghost commented Apr 12, 2022

Tagging subscribers to this area: @JulieLeeMSFT
See info in area-owners.md if you want to be subscribed.

Issue Details

Currently the JIT's constant folding (gtFoldExprConst and VNs
EvalOpSpecialized) assumes that BSWAP16 zero extends into the upper 16
bits. This was not the case, and in fact the behavior of BSWAP16
depended on platform.

Normally this would not be a problem since we always insert normalizing
casts when creating BSWAP16 nodes, however VN was smart enough to remove
this cast in some cases (see the test).

Change the semantics of BSWAP16 nodes to zero extend into the upper 16
bits to match constant folding, and add a small peephole to avoid
inserting this normalization in the common case where it is not
necessary.

Fixes #67723
Subsumes #67726

Author: jakobbotsch
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@jakobbotsch
Copy link
Member Author

Small diffs. These are when we previously removed the cast because we are storing the result directly to an indir. That forces the zero-normalization to appear as the peephole does not recognize this:

 G_M551_IG05:        ; gcrefRegs=00000000 {}, byrefRegs=00000004 {rdx}, byref
        ror      cx, 8
+       movzx    rcx, cx
        mov      word  ptr [rdx], cx
-                                               ;; size=7 bbWeight=1    PerfScore 1.50
+                                               ;; size=10 bbWeight=1    PerfScore 1.75
 G_M551_IG06:        ; , epilog, nogc, extend
        ret

Since #66965 improves this and the diffs seem small I don't think it warrants expanding the peephole in this PR to recognize it (let me know if you disagree).

cc @dotnet/jit-contrib PTAL @EgorBo

@jakobbotsch jakobbotsch requested a review from EgorBo April 12, 2022 16:41
@jakobbotsch
Copy link
Member Author

ping @EgorBo

@drieseng
Copy link
Contributor

@jakobbotsch Add the test from #67726?

@jakobbotsch
Copy link
Member Author

@jakobbotsch Add the test from #67726?

Whoops, yes, thanks for pointing that out!

@jakobbotsch
Copy link
Member Author

Failures are #66571 and #67761

@jakobbotsch jakobbotsch merged commit 6a728e2 into dotnet:main Apr 20, 2022
@jakobbotsch jakobbotsch deleted the fix-67723-2 branch April 20, 2022 16:06
directhex pushed a commit to directhex/runtime that referenced this pull request Apr 21, 2022
Currently the JIT's constant folding (gtFoldExprConst and VNs
EvalOpSpecialized) assumes that BSWAP16 zero extends into the upper 16
bits. This was not the case, and in fact the behavior of BSWAP16
depended on platform.

Normally this would not be a problem since we always insert normalizing
casts when creating BSWAP16 nodes, however VN was smart enough to remove
this cast in some cases (see the test).

Change the semantics of BSWAP16 nodes to zero extend into the upper 16
bits to match constant folding, and add a small peephole to avoid
inserting this normalization in the common case where it is not
necessary.

Fixes dotnet#67723
Subsumes dotnet#67726
@ghost ghost locked as resolved and limited conversation to collaborators May 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JIT: Missing sign extension for bswap16
3 participants