dotnet / dotnet/runtime

JIT clears the entire struct under SkipLocalsInit if it has gc fields, but doesn't have to.

Open
#129,174 0 comments 0 reactions 1 assignee Claimed by @EgorBo View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

From https://github.com/dotnet/runtime/pull/129157#discussion_r3379342760

```cs
using System;
using System.Runtime.CompilerServices;

[module: SkipLocalsInit]

public class Prog
{
static void Test(string str)
{
if (TryParse(str, out var ms))
Console.WriteLine(ms.A);
}

struct MyStruct
{
public string X; // replace with long
public long A;
public long B;
public long C;
public long D;
public long E;
public long F;
public long G;
public long H;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool TryParse(string str, out MyStruct ms) => throw null;
}
```
Normally, under SkipLocalsInit, the JIT can omit zeroing for out parameters and it does that if it has no GC fields, once at least one GC field is there it clears the entire struct while it could only clear individual GC slots (e.g. when there are just a few of them in a large struct)

Godbolt link: https://godbolt.org/z/xMTcqn8EW

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.