- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Fix #121066: Mark INITCLASS helpers as mutating heap to respect static constructor side-effects #121069
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
Conversation
…est case Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
| 5 methods affected in the diffs, looks like we no longer CSE something over the CLASS_INIT call, presumably, for a good reason. | 
| PTAL @AndyAyersMS @dotnet/jit-contrib | 
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.
Can you open a follow up issue?
We should model these helpers the same way we model all the others and fix the logic in VN to add heap mutation when appropriate.
| @jkotas @AndyAyersMS any ideas if it meets the bar for backport to 9.0? It seems like explicit initclass calls are rare, normally, we trigger cctor via one of the STATICBASE helper calls | 
| 
 Can this bug manifest as a tiered compilation behavior difference? (ie it works when called once, it breaks if when called hundred times). It would be my primary justification of the backport. Reliability bugs like that are always difficult to diagnose. I would do proactive 10.0 backport only. Backport to 8.0/9.0 reactively only if we get customer report. | 
| Seems like it could show up latently. Say a method avoids touching a class the first N times it is called, and then uses it, and by then the method is tiered up. Or it is called once and loops M times before touching the class, and by then it is running an OSR version. | 
Summary
This PR fixes issue #121066 where JIT optimizations (inlining, CSE, and redundant branch removal) were not respecting the side-effects of precise static constructors, leading to incorrect program behavior.
Problem
The
CORINFO_HELP_INITCLASSandCORINFO_HELP_INITINSTCLASSJIT helpers were incorrectly marked asisPure = trueinHelperCallProperties::init(). This caused the JIT to treat these helpers as having no side effects, allowing optimizations to eliminate or reorder static constructor calls. However, static constructors can have observable side effects (modifying static fields, I/O operations, etc.) that must be preserved.Consider this reproduction case:
Changes
src/coreclr/jit/utils.cpp
isPure = trueforCORINFO_HELP_INITCLASSandCORINFO_HELP_INITINSTCLASScasesmutatesHeap = trueto properly indicate these helpers modify program statesrc/tests/JIT/Regression/JitBlue/Runtime_121066/
Impact
This change ensures that the JIT compiler treats static constructor initialization helpers as having side effects, preventing incorrect optimizations that could eliminate or reorder these critical operations. The performance impact should be minimal since:
mayRunCctorflag was already set (indicating potential side effects)Fixes #121066
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
https://api.github.com/repos/dotnet/runtime/issues/121066curl -s REDACTED(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.