-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JIT: fix bug where a gc struct is not zero initialized (#67825)
This fixes an unexpected interaction between the zero-init optimization and dead stores. We have a local gc struct that is tracked but not promoted (and so on the frame) with an explicit zero init. The zero-init opt determines that in-prolog init is not needed because the local is tracked and has a live explicit initializer. So it marks the local as `lvHasExplicitInit`. But subsequent control flow optimizations end up making the explicit zero init dead, and it is removed by dead stores. Later on when we report GC info for the struct we report it as untracked. This leads to the GC seeing an uninitialized stack slot as a GC ref. The fix is to inhibit dead stores of zero initializers for `lvHasExplicitInit` (restricted to GC locals with multiple references). [some alternative fixes were considered, see notes in the PR]. Addresses #65694.
- Loading branch information
1 parent
73665cc
commit b13513a
Showing
4 changed files
with
116 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/tests/JIT/Regression/JitBlue/Runtime_65694/Runtime_65694.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
public struct Key | ||
{ | ||
public int a; | ||
public string s; | ||
} | ||
|
||
public struct Problem | ||
{ | ||
public int x; | ||
public double d; | ||
public string s0; | ||
public int y; | ||
public double e; | ||
public string s1; | ||
} | ||
|
||
public class Runtime_65694 | ||
{ | ||
public Dictionary<Key, Problem> _d; | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public void D() | ||
{ | ||
Problem p = new Problem { s0 = "hello", s1 = "world", x = 33 }; | ||
Key k = new Key() { a = 0, s = "a" }; | ||
Dictionary<Key, Problem> d = new Dictionary<Key, Problem>(); | ||
d[k] = p; | ||
|
||
_d = d; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static void F() | ||
{ | ||
GC.Collect(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] | ||
public int G(Key k, bool b) | ||
{ | ||
Problem p = default; | ||
|
||
F(); | ||
|
||
if (b) | ||
{ | ||
if (_d?.TryGetValue(k, out p) == true && (p.x == 33)) | ||
{ | ||
return 22; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
public static int Main() | ||
{ | ||
var r = new Runtime_65694(); | ||
r.D(); | ||
int result = 0; | ||
Key k = new Key() { a = 0, s = "a" }; | ||
result += r.G(k, true); | ||
return result + 78; | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
src/tests/JIT/Regression/JitBlue/Runtime_65694/Runtime_65694.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
</Project> |