Skip to content

Commit ddbce91

Browse files
authored
JIT: Mark replacements as dirty after setting GTF_VAR_DEATH (#88669)
Physically promoted struct locals are marked as dying when their remainder dies since all other state is stored in other locals. However, when we do this we must also mark all replacements as stale; otherwise a future struct use could skip writebacks and effectively introduce new uses, invalidating the previously marked last use bit. Fix #88616
1 parent 31b1fd4 commit ddbce91

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/coreclr/jit/promotion.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,14 @@ void ReplaceVisitor::ReplaceLocal(GenTree** use, GenTree* user)
24042404
{
24052405
lcl->gtFlags |= GTF_VAR_DEATH;
24062406
CheckForwardSubForLastUse(lclNum);
2407+
2408+
// Relying on the values in the struct local after this struct use
2409+
// would effectively introduce another use of the struct, so
2410+
// indicate that no replacements are up to date.
2411+
for (Replacement& rep : replacements)
2412+
{
2413+
SetNeedsWriteBack(rep);
2414+
}
24072415
}
24082416
return;
24092417
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using Xunit;
7+
8+
public class Runtime_88616
9+
{
10+
[Fact]
11+
public static int TestEntryPoint()
12+
{
13+
S foo;
14+
foo.A = 10;
15+
foo.B = 20;
16+
foo.C = 30;
17+
foo.D = 40;
18+
foo.E = 50;
19+
foo.A += foo.A += foo.A += foo.A += foo.A;
20+
foo.B += foo.B += foo.B += foo.B += foo.B;
21+
foo.C += foo.C += foo.C += foo.C += foo.C;
22+
foo.D += foo.D += foo.D += foo.D += foo.D;
23+
foo.E += foo.E += foo.E += foo.E += foo.E;
24+
// 'foo' is a last use here (it is fully promoted so all of its state
25+
// is stored in separate field locals), so physical promotions marks
26+
// this occurence as GTF_VAR_DEATH.
27+
Mutate(foo);
28+
// However, we cannot use the fact that we wrote the fields back into
29+
// the struct local above to skip those same write backs here; if we
30+
// skip those write backs then we effectively introduce new uses of the
31+
// struct local.
32+
return Check(foo);
33+
}
34+
35+
[MethodImpl(MethodImplOptions.NoInlining)]
36+
private static void Mutate(S s)
37+
{
38+
s.A = -42;
39+
Consume(ref s);
40+
}
41+
42+
[MethodImpl(MethodImplOptions.NoInlining)]
43+
private static void Consume(ref S s)
44+
{
45+
}
46+
47+
[MethodImpl(MethodImplOptions.NoInlining)]
48+
private static int Check(S s)
49+
{
50+
if (s.A != 50)
51+
{
52+
Console.WriteLine("FAIL: s.A == {0}", s.A);
53+
return -1;
54+
}
55+
56+
return 100;
57+
}
58+
59+
private struct S
60+
{
61+
public long A, B, C, D, E;
62+
}
63+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Optimize>True</Optimize>
4+
<DebugType>None</DebugType>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="$(MSBuildProjectName).cs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)