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
Unexpected reordering of memory allocation when using GC.GetAllocatedBytesForCurrentThread() when running optimized #96836
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIquestionAnswer questions and provide assistance, not an issue with source code or documentation.
I'm using GC.GetAllocatedBytesForCurrentThread() in a unit test to verify zero-allocation behavior. My test is like this:
var before = GC.GetAllocatedBytesForCurrentThread();
// do zero-allocation thing
var delta = GC.GetAllocatedBytesForCurrentThread() - before;
Assert.That(delta, Is.EqualTo(0));
I found that this was failing in release mode, where delta as 24 rather than zero. This happened even when I commented out all lines between the 2 GC checks.
Reproduction Steps
Here is a simple program which repros the issue for me in release mode but not in debug mode:
var before = GC.GetAllocatedBytesForCurrentThread();
var delta = GC.GetAllocatedBytesForCurrentThread() - before;
//Thread.MemoryBarrier(); // uncommenting this fixes the problem
Console.WriteLine((object)delta); // 0 in debug mode, 24 in release mode
The JIT is reordering the allocation of the Int32 box to be in front of GetAllocatedBytesForCurrentThread call as a side-effect of various optimizations. @dotnet/jit-contrib This reordering does not seem to be profitable. Do we want to avoid it?
However, small allocations are considered to be side-effect free and the JIT can reorder or remove them as it sees a fit in general. Issues like this are expected if you try to use GetAllocatedBytesForCurrentThread. Also, allocations can happen as side-effects of JITing.
looks like it's an intentional behaviour of impImportAndPushBox - we try to write boxed tree directly to a box instead of saving it to a local first (e.g. for structs that will be a redundant copy I guess)
So my initial conclusion that this behavior helps us to optimize boxings in certain cases (e.g. from GDVs?)
it can be "fixed" like this: EgorBo@9a4772d (fixes the issue) but brings a lot of size regressions. Since, as Jan noted, allocation is side-effect free (we ignore OOM as a side-effect) it's legal to keep it as is... we can intrinsify the GC call if needed.
To basically always spill GC.GetAllocatedBytesForCurrentThread() if we see it. That will fix this specific issue but other phases might still do something similar. We can also mark all allocations as "have side-effects" in the presense of GetAllocatedBytesForCurrentThread but that probably a bad idea (observing impacts results).
To basically always spill GC.GetAllocatedBytesForCurrentThread() if we see it. That will fix this specific issue but other phases might still do something similar.
Right. I do not think special casing GetAllocatedBytesForCurrentThread in the JIT makes sense.
The JIT is reordering the allocation of the Int32 box to be in front of GetAllocatedBytesForCurrentThread call as a side-effect of various optimizations. @dotnet/jit-contrib This reordering does not seem to be profitable. Do we want to avoid it?
As Egor mentioned, the benefit (more apparent with larger value types that are returned by ref) is that the code does not need to copy the value from the call's return buffer into the box payload; instead the call writes the value into the box directly.
Would adding something like a memory barrier inside the implementation of GetAllocatedBytesForCurrentThread be sufficient to prevent this reordering without any JIT changes?
My (possibly mistaken) assumption is that this methods only real purpose is measurements like what I’ve shown; it would be nice if those behaved as expected.
Would adding something like a memory barrier inside the implementation of GetAllocatedBytesForCurrentThread be sufficient to prevent this reordering without any JIT changes?
It would not help.
To avoid unexpected interferences between the measured code and the surrounding code, you can wrap the measured code in no-inline method. Wrappers like this are common for perf microbenchmark. For example, Benchmark.NET does something similar.
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIquestionAnswer questions and provide assistance, not an issue with source code or documentation.
Activity
jkotas commentedon Jan 11, 2024
The JIT is reordering the allocation of the
Int32box to be in front ofGetAllocatedBytesForCurrentThreadcall as a side-effect of various optimizations. @dotnet/jit-contrib This reordering does not seem to be profitable. Do we want to avoid it?However, small allocations are considered to be side-effect free and the JIT can reorder or remove them as it sees a fit in general. Issues like this are expected if you try to use GetAllocatedBytesForCurrentThread. Also, allocations can happen as side-effects of JITing.
EgorBo commentedon Jan 11, 2024
Minimal repro:
looks like it's an intentional behaviour of
impImportAndPushBox- we try to write boxed tree directly to a box instead of saving it to a local first (e.g. for structs that will be a redundant copy I guess)EgorBo commentedon Jan 11, 2024
So my initial conclusion that this behavior helps us to optimize boxings in certain cases (e.g. from GDVs?)
it can be "fixed" like this: EgorBo@9a4772d (fixes the issue) but brings a lot of size regressions. Since, as Jan noted, allocation is side-effect free (we ignore OOM as a side-effect) it's legal to keep it as is... we can intrinsify the GC call if needed.
cc @dotnet/jit-contrib
jkotas commentedon Jan 11, 2024
What do you mean?
EgorBo commentedon Jan 11, 2024
To basically always spill
GC.GetAllocatedBytesForCurrentThread()if we see it. That will fix this specific issue but other phases might still do something similar. We can also mark all allocations as "have side-effects" in the presense ofGetAllocatedBytesForCurrentThreadbut that probably a bad idea (observing impacts results).jkotas commentedon Jan 11, 2024
Right. I do not think special casing GetAllocatedBytesForCurrentThread in the JIT makes sense.
AndyAyersMS commentedon Jan 12, 2024
As Egor mentioned, the benefit (more apparent with larger value types that are returned by ref) is that the code does not need to copy the value from the call's return buffer into the box payload; instead the call writes the value into the box directly.
madelson commentedon Jan 12, 2024
Would adding something like a memory barrier inside the implementation of GetAllocatedBytesForCurrentThread be sufficient to prevent this reordering without any JIT changes?
My (possibly mistaken) assumption is that this methods only real purpose is measurements like what I’ve shown; it would be nice if those behaved as expected.
jkotas commentedon Jan 12, 2024
It would not help.
To avoid unexpected interferences between the measured code and the surrounding code, you can wrap the measured code in no-inline method. Wrappers like this are common for perf microbenchmark. For example, Benchmark.NET does something similar.