You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: (bug) impImportAndPushBoxForNullable drops GTF_IND_INITCLASS, allowing Nullable<T> loads to be hoisted before static constructor #135817
When boxing a Nullable<T> loaded from a static field, the JIT drops GTF_IND_INITCLASS on the decomposed hasValue and value loads, allowing loop hoisting to move the field load ahead of the static constructor check.
Minimal Repro
usingSystem;usingSystem.Runtime.CompilerServices;classHolder{publicstaticreadonlyint?Value=Init();staticint?Init()=>42;}classProgram{[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.AggressiveOptimization)]staticintTest(intn,intdenom){intsum=0;for(inti=0;i<n;i++){sum+=i/denom;object?res=Holder.Value;if(resis not null)sum+=42;}returnsum;}staticvoidMain(){Console.WriteLine(Test(1,1));}}
Expected
42
Actual
0
Regression?
Yes, since .NET 8.0:
.NET 6.0.36: Does not reproduce (42)
.NET 7.0.20: Does not reproduce (42)
.NET 8.0.32: Reproduces (0)
.NET 9.0.21: Reproduces (0)
.NET 10.0.13: Reproduces (0)
.NET 11.0.0-rc.1.26425.128: Reproduces (0)
Notes
Compiler::impImportAndPushBoxForNullable calls impGetNodeAddr(nullableObj, CHECK_SPILL_ALL, GTF_IND_INITCLASS, &indirFlags), which splits the class-init helper into a separate statement and returns GTF_IND_INITCLASS in indirFlags, but never passes indirFlags to the gtNewLoadValueNode calls for value and hasValue (similarly, gtFoldNullCheck drops GTF_IND_INITCLASS when folding BOX_NULLABLE(&x) != null to x.hasValue).
Consequently, optHoistLoopCode does not recognize the load as treeIsCctorDependent and hoists Holder.Value._hasValue into the loop preheader ahead of CORINFO_HELP_GET_GCSTATIC_BASE.
Same class of bug as #134897 (CopyFieldByField in morphblock.cpp), but in a different code path.
Passing DOTNET_JitMinOpts=1 produces the expected 42.
Activity
dotnet-policy-service commentedon Oct 14, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: preserve GTF_IND_INITCLASS when inlining BOX of Nullable<T> (#13…