Skip to content

Implement additional CEE_BOX peepholes - #121763

Merged
janvorli merged 2 commits into
dotnet:mainfrom
janvorli:implement-additional-box-peeps
Nov 24, 2025
Merged

janvorli merged 2 commits into
dotnet:mainfrom
janvorli:implement-additional-box-peeps

Conversation

@janvorli

Copy link
Copy Markdown
Member

This change implements CEE_BOX peepholes like JIT does in Compiler::impBoxPatternMatch, mostly to fix issues with sequences starting with CEE_BOX applied to ByRefLike types. While it is illegal to box a ByRefLike types, some sequences can be replaced by constants or nop.

There is also one extra fix for the destination type of an unbox. It was incorrectly changed to StackTypeO by a large change in the past from the correct StackTypeI.

This fixes a relatively large number of failing libraries tests.

This change implements CEE_BOX peepholes like JIT does, mostly to fix
issues with sequences starting with CEE_BOX applied to ByRefLike types.
While it is illegal to box a ByRefLike types, some sequences can be
replaced by constants or nop.

There is also one extra fix for the destination type of an unbox. It was
incorrectly changed to StackTypeO by a large change in the past from the
correct StackTypeI.

This fixes a relatively large number of failing libraries tests.
@janvorli janvorli added this to the 11.0.0 milestone Nov 18, 2025
@janvorli janvorli self-assigned this Nov 18, 2025
@janvorli
janvorli requested review from BrzVlad and kg as code owners November 18, 2025 22:52
Copilot AI review requested due to automatic review settings November 18, 2025 22:52
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements additional CEE_BOX peephole optimizations in the CoreCLR interpreter, mirroring the JIT's Compiler::impBoxPatternMatch functionality. The primary goal is to handle sequences involving BOX operations on ByRefLike types by replacing them with constants or no-ops, as boxing ByRefLike types is illegal but certain code patterns can be safely optimized away.

Key changes:

  • Changed ApplyPeepFunc_t return type from void to int to support flexible skip distances
  • Added 6 new peephole optimization patterns for BOX-related sequences (BOX+BRTRUE/BRFALSE, BOX+ISINST, BOX+ISINST+BRTRUE/BRFALSE, etc.)
  • Fixed UNBOX destination type from StackTypeO to StackTypeI (correcting a previous regression)

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/interpreter/compiler.h Updated ApplyPeepFunc_t return type and added declarations for 6 new BOX-related peephole check/apply function pairs
src/coreclr/interpreter/compiler.cpp Implemented new BOX peephole patterns, updated existing Apply functions to return skip distance, fixed UNBOX type, and registered new peepholes in the optimization pipeline

Comment thread src/coreclr/interpreter/compiler.cpp Resolved
Comment thread src/coreclr/interpreter/compiler.cpp Resolved
Comment thread src/coreclr/interpreter/compiler.cpp Resolved
Comment thread src/coreclr/interpreter/compiler.cpp Resolved
@janvorli janvorli closed this Nov 20, 2025
@janvorli janvorli reopened this Nov 20, 2025
Comment thread src/coreclr/interpreter/compiler.cpp Resolved
Change unbox result stack type to StackTypeByRef
@janvorli
janvorli merged commit 7d37868 into dotnet:main Nov 24, 2025
@janvorli
janvorli deleted the implement-additional-box-peeps branch November 24, 2025 17:58
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants