Skip to content

[mono] Optimize thread state transition - #58992

Closed
filipnavara wants to merge 11 commits into
dotnet:mainfrom
filipnavara:mono_faster_pinvoke
Closed

filipnavara wants to merge 11 commits into
dotnet:mainfrom
filipnavara:mono_faster_pinvoke

Conversation

@filipnavara

@filipnavara filipnavara commented Sep 11, 2021 •

Copy link
Copy Markdown
Member

Ignore for reviews, just want to see the CI.

This revamps the logic in how transitions from managed to native code happen with regards to GC. The core idea is to ensure that every possible reference is spilled to the stack before the transition to icall or P/Invoke.

The wrappers for P/Invoke (w/o SuppressGCTransition) ensure the spilling in the following way:

  • Caller saved registers are saved by the caller of the P/Invoke wrapper
  • Callee saved registers are saved by the LMF (Last Managed Frame) logic into the MonoLMF structure on the stack
  • Argument registers for reference-type arguments are explicitly flushed to stack by generating IL that moves them to volatile local variables

The wrappers for P/Invoke with SuppressGCTransition don't get any special treatment since GC safe mode is not entered.

The wrappers for icalls don't enter GC safe mode themselves. In that regard they behave like P/Invoke with SuppressGCTransition and skip the explicit argument spilling. This should be safe if the icalls use HANDLES logic which should ensure proper tracking by GC if the icall eventually enters the GC safe mode.

Additionally, the following changes are present:

  • thread_state_init captures only stack boundaries in the context
  • General registers are not captured on transition to GC safe mode since all references are already spilled to stack
  • SGen does not check the general registers in the thread context since it already checks the stack
  • copy_stack is no longer necessary and it's removed; since all the references are spilled to the stack before the GC safe transition they stay on the stack for the duration of the native call
  • State checks are disable for non-checked builds because of significant overhead

TODO:

  • Update LLVM to handle save_lmf, add emiting of llvm.eh.unwind.init intrinsic to spill the callee saved registers
  • Investigate preserveall cconv in LLVM
  • Check JIT icalls
  • Make sure that savepoints don't have GC hole
  • Make sure other uses of mono_threads_enter_gc_safe_region are safe
  • Verify that tests still pass

@ghost ghost added community-contribution Indicates that the PR has been added by a community member area-VM-meta-mono labels Sep 11, 2021
@ghost

ghost commented Sep 11, 2021

Copy link
Copy Markdown

Tagging subscribers to this area:
See info in area-owners.md if you want to be subscribed.

Issue Details

Ignore, just want to see the CI.

Author: filipnavara
Assignees: -
Labels:

area-VM-meta-mono, community-contribution

Milestone: -

@filipnavara filipnavara reopened this Sep 12, 2021
@ghost

ghost commented Nov 3, 2021

Copy link
Copy Markdown

Draft Pull Request was automatically closed for inactivity. Please let us know if you'd like to reopen it.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2021
@filipnavara
filipnavara deleted the mono_faster_pinvoke branch June 5, 2025 07:45
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-VM-meta-mono community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant