Skip to content

Comments

Fix #74020: Optimize consecutive shifts in JIT Morph#122533

Open
csa7mdm wants to merge 1 commit intodotnet:mainfrom
csa7mdm:fix/74020-jit-shift-opt
Open

Fix #74020: Optimize consecutive shifts in JIT Morph#122533
csa7mdm wants to merge 1 commit intodotnet:mainfrom
csa7mdm:fix/74020-jit-shift-opt

Conversation

@csa7mdm
Copy link

@csa7mdm csa7mdm commented Dec 14, 2025

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 - Added case GT_RSH / case GT_RSZ to fgMorphSmpOpOptional:

  • Fold (x shift c1) shift c2 into x shift (c1+c2) for same-operation shifts
  • Handle RSH overshift with saturation to bitWidth - 1
  • Handle RSZ overshift by folding to zero constant
  • Guarded by fgGlobalMorph

Verification

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Dec 14, 2025
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 14, 2025
@csa7mdm csa7mdm force-pushed the fix/74020-jit-shift-opt branch 2 times, most recently from 0b10860 to cca8115 Compare December 15, 2025 12:43
@vcsjones vcsjones added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 17, 2025
Copy link
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@JulieLeeMSFT
Copy link
Member

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.

@csa7mdm, I also think it would be better to handle it in morph. Do you want to try that?

@JulieLeeMSFT JulieLeeMSFT added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 26, 2026
@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 26, 2026
@csa7mdm
Copy link
Author

csa7mdm commented Jan 27, 2026

n workflow(s) awaiting approva

@csa7mdm
Copy link
Author

csa7mdm commented Jan 27, 2026

n workflow(s) awaiting approval

@JulieLeeMSFT
Copy link
Member

Approved workflow to run.

@adamperlin
Copy link
Contributor

@csa7mdm it looks like the latest CI run failed if you want to take a look?

@JulieLeeMSFT JulieLeeMSFT added the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 23, 2026
@JulieLeeMSFT
Copy link
Member

@csa7mdm it looks like the latest CI run failed if you want to take a look?

@csa7mdm, please check the CI test failures.

@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 23, 2026
@csa7mdm csa7mdm force-pushed the fix/74020-jit-shift-opt branch from 968d96a to b9cbf68 Compare February 24, 2026 00:55
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Feb 24, 2026
Add consecutive shift folding to fgMorphSmpOpOptional (morph.cpp):
- Fold (x shift c1) shift c2 into x shift (c1+c2) for same-op shifts
- Handle RSH overshift with saturation to bitWidth-1
- Handle RSZ/LSH overshift by folding to zero constant
- Guarded by fgGlobalMorph to run only during morphing phase

Per reviewer feedback, this algebraic simplification belongs in Morph
(not Lowering) so downstream passes (VN, CSE, LSRA) benefit from
the simplified IR.

Fixes dotnet#74020
@csa7mdm csa7mdm force-pushed the fix/74020-jit-shift-opt branch from b9cbf68 to fc342c4 Compare February 24, 2026 00:59
@csa7mdm csa7mdm changed the title Fix #74020: Optimize consecutive shifts in JIT Lowering Fix #74020: Optimize consecutive shifts in JIT Morph Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants