-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[mono][aot] Prevent localloc in a loop during constrained gsharedvt calls #117679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 a caching mechanism for localloc operations in Mono's AOT compiler to prevent memory allocation in loops during constrained gsharedvt calls. The optimization defers and consolidates localloc operations by caching their addresses and later patching them with the maximum required size.
Key changes:
- Introduces a localloc caching system with two cache slots for temporary buffers
- Replaces direct localloc calls with cached versions in constrained gsharedvt call handling
- Adds post-processing to patch cached locallocs with maximum allocation sizes
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/mono/mono/mini/mini.h | Defines MonoCachedLocallocInfo struct and adds localloc_cache array to MonoCompile |
src/mono/mono/mini/method-to-ir.c | Implements mono_emit_cached_localloc function and integrates caching into gsharedvt calls |
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs | Removes test platform exclusion for fixed issue |
Comments suppressed due to low confidence (1)
src/mono/mono/mini/method-to-ir.c:3381
- The GSList should be reset to NULL after freeing to prevent potential use-after-free issues in case the cache is accessed again. Add 'info->localloc_ins = NULL;' after this line.
MonoVTable *vtable = mono_class_vtable_checked (klass, cfg->error);
cc @kotlarmilos |
9da62a4
to
0423169
Compare
Just curious - do we know why this started to happen only recently? |
Changes in #116538 made the error visible on iOS/tvOS. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
…alls We create a var that stores the address of some localloc memory. This var is nulled at method entry. In every place where we need to obtain temporary localloc memory, we check if the cache var was initialized, if not we do a localloc, otherwise we use the cached ptr as the temporary memory buffer. This commit adds 2 such caches because the constrained gsharedvt call can require 2 separate temporary buffers.
dbe9b64
to
d385aec
Compare
We create a var that stores the address of some localloc-ed memory. This var is nulled at method entry. In every place where we need to obtain temporary localloc memory, we check if the cache var was initialized. If not we do a localloc and save it into the cache, otherwise we use the cached ptr as the temporary memory buffer. Once we imported all IL, we patch the locallocs using the cache to allocate the maximum sized used between different call sites. This commit adds 2 such caches because the constrained gsharedvt call can require 2 separate temporary buffers.
#116714
#116815