Skip to content

Commit

Permalink
JIT: fix OSR handling for pinned locals (#67680)
Browse files Browse the repository at this point in the history
The OSR method may not see any references to the pinned local, but must
still report it in the GC info. So under OSR, mark (root method) pinned
locals as implicitly referenced.

This should address the issue seen in #67688.
  • Loading branch information
AndyAyersMS authored Apr 7, 2022
1 parent 32a6620 commit c288acd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ void Compiler::lvaInitTypeRef()
{
JITDUMP("Setting lvPinned for V%02u\n", varNum);
varDsc->lvPinned = 1;

if (opts.IsOSR())
{
// OSR method may not see any references to the pinned local,
// but must still report it in GC info.
//
varDsc->lvImplicitlyReferenced = 1;
}
}
else
{
Expand Down
37 changes: 37 additions & 0 deletions src/tests/JIT/opt/OSR/pinnedlocal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

// Run under COMPlus_GCStress=3

class PinnedLocal
{
static int F(char c)
{
return (int) c;
}

public static unsafe int Main()
{
string ss = "goodbye, world\n";
string s = "hello, world\n";
int r = 0;
fixed(char* p = s)
{
for (int i = 0; i < 100_000; i++)
{
r += F(p[i % s.Length]);

if ((i % 10_000) == 0)
{
GC.Collect(2);
ss = new String('a', 100);
}
}

Console.WriteLine($"r is {r}");
return r - 9000061 + 100;
}
}
}
21 changes: 21 additions & 0 deletions src/tests/JIT/opt/OSR/pinnedlocal.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<DebugType />
<Optimize>True</Optimize>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<PropertyGroup>
<CLRTestBatchPreCommands><![CDATA[
$(CLRTestBatchPreCommands)
set COMPlus_GCStress=3
]]></CLRTestBatchPreCommands>
<BashCLRTestPreCommands><![CDATA[
$(BashCLRTestPreCommands)
export COMPlus_GCStress=3
]]></BashCLRTestPreCommands>
</PropertyGroup>
</Project>

0 comments on commit c288acd

Please sign in to comment.