Skip to content

JIT: Mark memset/memcpy and monitor helpers as heap-mutating - #136734

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:egorbo/fix-memset-helper-heap
Oct 30, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:egorbo/fix-memset-helper-heap

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

CORINFO_HELP_MEMCPY, MEMZERO, MEMSET and NATIVE_MEMSET write memory, but HelperCallProperties didn't mark them as mutating the heap. Value numbering, memory liveness and loop side-effect analysis therefore treated them as no-ops for memory, which allowed loads around them to be CSE'd or hoisted out of loops:

int a = *p;
Unsafe.InitBlockUnaligned(p, 0, n); // non-constant size -> CORINFO_HELP_MEMSET call
return a + *p;                      // *p gets the same VN as 'a', CSE returns 2*a instead of a

for (...)
{
    sum += *p;                          // hoisted out of the loop
    Unsafe.InitBlockUnaligned(p, 0, n);
}

This PR marks these helpers as heap-mutating. MON_ENTER/MON_EXIT get the same treatment since they are full memory barriers and also write *lockTaken.

A couple of diffs

CORINFO_HELP_MEMCPY/MEMZERO/MEMSET/NATIVE_MEMSET write memory but were not
marked as mutating the heap, so value numbering, memory liveness and loop
side-effect analysis ignored their writes. Loads around them could be CSE'd
or hoisted out of loops. Do the same for MON_ENTER/MON_EXIT, which are full
memory barriers and write *lockTaken.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 6941a5c5-0a5f-4856-a810-37536d3aab5b
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 30, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@EgorBo

EgorBo commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib PTAL, simple correctness fix.

@EgorBo
EgorBo requested a review from a team October 30, 2026 21:56
@EgorBo
EgorBo merged commit 8a389ed into dotnet:main Oct 30, 2026
132 checks passed
@EgorBo
EgorBo deleted the egorbo/fix-memset-helper-heap branch October 30, 2026 23:45
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants