Skip to content

[clr-interp] Add support for synchronized methods - #120006

Merged
davidwrighton merged 4 commits into
dotnet:mainfrom
davidwrighton:add_synchronized_support
Sep 24, 2025
Merged

[clr-interp] Add support for synchronized methods#120006
davidwrighton merged 4 commits into
dotnet:mainfrom
davidwrighton:add_synchronized_support

Conversation

@davidwrighton

Copy link
Copy Markdown
Member
  • The general approach is to wrap the method is a try/finally block
    • This is done by appending some IL to the method, and adding an EH clause
    • This required implementing a few abstraction boundaries so that the code/eh clauses could be rewritten within the interpreter
  • Synchronized methods also re-use the shadow this pointer logic
  • existing IL which is encoded as a RET needs to actually have the semantics of a LEAVE to to the actual return from the method
  • We also have a couple of "intrinsic" tokens which the our implementation of CALL now recognizes.

- The general approach is to wrap the method is a try/finally block
- This is done by appending some IL to the method, and adding an EH clause
- This required implementing a few abstraction boundaries so that the code/eh clauses could be rewritten within the interpreter
- Synchronized methods also re-use the shadow this pointer logic
- existing IL which is encoded as a RET needs to actually have the semantics of a LEAVE to to the actual return from the method
- We also have a couple of "intrinsic" tokens which the our implementation of CALL now recognizes.
CopilotAI review requested due to automatic review settings September 23, 2025 16:56
@github-actionsgithub-actionsBot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 23, 2025

CopilotAI 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 adds support for synchronized methods in the CLR interpreter by implementing a try/finally pattern to handle monitor enter/exit operations.

Key changes:

  • Wraps synchronized methods in try/finally blocks with additional IL opcodes for monitor operations
  • Implements shadow this pointer logic for synchronized instance methods
  • Converts RET instructions to LEAVE operations in synchronized methods to ensure proper cleanup

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
FileDescription
src/coreclr/vm/interpexec.cppAdds INTOP_CALL_HELPER_V_SA opcode handler and HELPER_FTN_V_PP typedef
src/coreclr/interpreter/intops.hDefines intrinsic tokens for synchronized method implementation
src/coreclr/interpreter/inc/intops.defAdds INTOP_CALL_HELPER_V_SA opcode definition
src/coreclr/interpreter/compiler.hAdds synchronized method state tracking variables and helper methods
src/coreclr/interpreter/compiler.cppImplements synchronized method compilation logic, IL rewriting, and monitor operations

@davidwrightondavidwrighton added area-CodeGen-Interpreter-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Sep 23, 2025
@kg

kg commented Sep 23, 2025

Copy link
Copy Markdown
Contributor

(Not a complaint about this implementation) Is there a reason we couldn't have the interpreter itself do the synchronization?

Comment threadsrc/coreclr/interpreter/compiler.cpp
Comment threadsrc/coreclr/interpreter/compiler.cpp
Comment threadsrc/coreclr/interpreter/compiler.cpp
Comment threadsrc/coreclr/interpreter/compiler.cpp
Comment threadsrc/coreclr/interpreter/intops.h
kg
kg approved these changes Sep 23, 2025

@kgkg 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.

The complexity bothers me but I assume there's a good reason why it can't be simpler. Other than the comments I left it LGTM

Comment threadsrc/coreclr/interpreter/compiler.cpp Outdated
Comment threadsrc/coreclr/interpreter/compiler.cpp
@davidwrighton
davidwrighton merged commit 12ca85b into dotnet:mainSep 24, 2025
96 of 98 checks passed
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Oct 25, 2025
Sign up for freeto 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.

4 participants

@davidwrighton@kg@janvorli