runtime icon indicating copy to clipboard operation
runtime copied to clipboard

JIT: Optimize "X ^ 0" to "X"

Open SkiFoD opened this issue 4 years ago • 8 comments

Current implementation emits:

8B4510               mov      eax, dword ptr [rbp+10H]
83F000               xor      eax, 0

I suppose it can be simplified to

8B4510               mov      eax, dword ptr [rbp+10H]

https://godbolt.org/z/57fh1v6PK

I made a quick research and folded it in the lowering phase. The diffs are quite small, but the folding may be useful.

before.txt after.txt diff_summary.6.md

@EgorBo, @SingleAccretion I would be happy to know your opinion if it is worth it to implement this optimization.

SkiFoD avatar Feb 09 '22 12:02 SkiFoD

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

Issue Details

Current implementation emits:

8B4510               mov      eax, dword ptr [rbp+10H]
83F000               xor      eax, 0

I suppose it can be simplified to

8B4510               mov      eax, dword ptr [rbp+10H]

https://godbolt.org/z/57fh1v6PK

I made a quick research and folded it in the lowering phase. The diffs are quite small, but the folding may be useful.

before.txt after.txt diff_summary.6.md

@EgorBo, @SingleAccretion I would be happy to know your opinion if it is worth it to implement this optimization.

Author: SkiFoD
Assignees: -
Labels:

area-CodeGen-coreclr, untriaged

Milestone: -

ghost avatar Feb 09 '22 12:02 ghost

https://godbolt.org/z/57fh1v6PK

nit: here is the updated link with optimization flags: https://godbolt.org/z/rG3cMKr5q

am11 avatar Feb 09 '22 12:02 am11

@EgorBo, @SingleAccretion I would be happy to know your opinion if it is worth it to implement this optimization.

why not if it produces diffs 🙂

Have you measured the diffs if it's done in morph instead? I guess it should be a pretty straightforward change in morph. E.g. I believe we already have x * 0 there

EgorBo avatar Feb 09 '22 12:02 EgorBo

@EgorBo, @SingleAccretion I would be

happy to know your opinion if it is worth it to implement this optimization.

why not if it produces diffs 🙂

Have you measured the diffs if it's done in morph instead? I guess it should be a pretty straightforward change in morph. E.g. I believe we already have x * 0 there

I tried to implement it in morph at first, it produced these regressions (diff_summary.3.md)

However, I think it could happen because I missed something.

SkiFoD avatar Feb 09 '22 12:02 SkiFoD

I moved the optimization to morph phase. Here are asmdiffs for x64 diff_summary.10.md I couldn't get rid of the regressions. There are 190 of them, which doesn't feel right to me.

SkiFoD avatar Feb 09 '22 16:02 SkiFoD

@SkiFoD, the code complete for .NET 7 is July 11th. Do you think you can complete this before that? If not, we can move this to .NET 8.

JulieLeeMSFT avatar Jun 10 '22 19:06 JulieLeeMSFT

I couldn't get rid of the regressions. There are 190 of them, which doesn't feel right to me

Examples of the regressions would be beneficial (that is the actual assembly before/after). Often times regressions may simply be the JIT being able to kick in additional optimizations once the IR has simplified.

tannergooding avatar Jun 11 '22 04:06 tannergooding

@JulieLeeMSFT @tannergooding Hey everyone. The code has been already merged here https://github.com/dotnet/runtime/pull/65072

SkiFoD avatar Jun 11 '22 07:06 SkiFoD