Fix #74020: Optimize consecutive shifts in JIT Morph#122533
Fix #74020: Optimize consecutive shifts in JIT Morph#122533csa7mdm wants to merge 3 commits intodotnet:mainfrom
Conversation
0b10860 to
cca8115
Compare
tannergooding
left a comment
There was a problem hiding this comment.
LGTM. I do still think this would be better to handle in morph and that it would be good to track RSH(x, n) for 0 < n < bitWidth as being "never negative", but will defer to other input.
CC. @dotnet/jit-contrib for secondary review
@csa7mdm, I also think it would be better to handle it in morph. Do you want to try that? |
|
n workflow(s) awaiting approva |
|
n workflow(s) awaiting approval |
|
Approved workflow to run. |
|
@csa7mdm it looks like the latest CI run failed if you want to take a look? |
b9cbf68 to
fc342c4
Compare
|
The two CI failures are unrelated to this change and are a known flaky infrastructure issue:
The Build Analysis check (merge gate) passed with |
|
Also, the PR seems to have 0 diffs |
Add peephole optimization in fgMorphSmpOpOptional to fold consecutive same-operation right shifts with constant amounts: (x shift c1) shift c2 -> x shift (c1 + c2) Handles overshift: RSH saturates to bitWidth-1, RSZ folds to zero. Guarded by fgGlobalMorph, side-effect checks, and multi-reg checks. Includes comprehensive tests in ShiftCombining.cs covering RSH/RSZ for int32/int64, overshift saturation, boundary cases, and the original division pattern from issue dotnet#74020.
4d4423c to
b90ca38
Compare
|
Thanks @EgorBo for the review! Addressed both concerns:
Justification: The pattern appears when C# code does repeated integer division by powers of two ( |
|
Thanks @EgorBo for the review! Addressed both concerns:
Justification: The pattern appears when C# code does repeated integer division by powers of two ( |
|
Thanks @EgorBo! Based on your detailed review and looking at this from a broader architectural perspective, I've implemented the following refactoring: 1. Stripped Overshift PathUpon deeper root cause analysis, I agree with your assessment - handling over-shifts (shifting beyond 2. Side Effects HandlingGiven that we are removing the over-shift transformation (which previously would aggressively eliminate the inner shift's operand entirely and drop its side-effects), we no longer need the complex 3. JIT API Correctness & ConsistencyAddressed all specific architectural nits to adhere strictly to JIT engineering guidelines:
4. Semantic Testing VerificationRewrote the test coverage. Using |
Description
This PR addresses issue #74020 by adding a peephole optimization in the JIT Morph phase (
fgMorphSmpOpOptional) to combine consecutive right shift operations (GT_RSH/GT_RSZ) with constant shift amounts.Per reviewer feedback from @tannergooding and @JulieLeeMSFT, the optimization was moved from Lowering to Morph so downstream passes (VN, CSE, LSRA) benefit from the simplified IR.
Changes
src/coreclr/jit/morph.cpp- Addedcase GT_RSH/case GT_RSZtofgMorphSmpOpOptional:(x shift c1) shift c2intox shift (c1+c2)for same-operation shiftsbitWidth - 1fgGlobalMorphVerification
x / c1 / c2generates redundantshrinstructions