Skip to content

[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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Jul 15, 2025

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

@Copilot Copilot AI review requested due to automatic review settings July 15, 2025 19:44
@BrzVlad BrzVlad requested a review from steveisok as a code owner July 15, 2025 19:44
@BrzVlad BrzVlad requested a review from vitek-karas as a code owner July 15, 2025 19:44
Copy link
Contributor

@Copilot 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 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);

@BrzVlad BrzVlad requested a review from lateralusX July 15, 2025 19:45
@BrzVlad
Copy link
Member Author

BrzVlad commented Jul 15, 2025

cc @kotlarmilos

@vitek-karas
Copy link
Member

Just curious - do we know why this started to happen only recently?

@kotlarmilos
Copy link
Member

Changes in #116538 made the error visible on iOS/tvOS.

Copy link
Member

@lateralusX lateralusX left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@kotlarmilos kotlarmilos left a comment

Choose a reason for hiding this comment

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

LGTM!

BrzVlad added 3 commits July 22, 2025 17:04
…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.
@BrzVlad BrzVlad force-pushed the fix-aot-constrained-gsharedvt branch from dbe9b64 to d385aec Compare July 22, 2025 14:04
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.

5 participants