Skip to content

Conversation

@janvorli
Copy link
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
Contributor

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

Copilot finished reviewing on behalf of janvorli November 18, 2025 22:54
Copy link
Contributor

Copilot AI left a comment

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

@janvorli janvorli closed this Nov 20, 2025
@janvorli janvorli reopened this Nov 20, 2025
if (helpFunc == CORINFO_HELP_UNBOX)
{
EmitPushHelperCall_2(helpFunc, embedInfo, m_pStackPointer[0].var, StackTypeO, (CORINFO_CLASS_HANDLE)embedInfo.compileTimeHandle);
EmitPushHelperCall_2(helpFunc, embedInfo, m_pStackPointer[0].var, StackTypeI, (CORINFO_CLASS_HANDLE)embedInfo.compileTimeHandle);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a pointer onto the GC heap? Doesn't it need to be StackTypeByRef?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants