Skip to content

Commit

Permalink
JIT: Handle memory conservatively in handler blocks in VN (#106274)
Browse files Browse the repository at this point in the history
SSA does not model memory SSA faithfully inside try clauses. To model it
faithfully we would need to make all intermediate memory states in the
try block visible to the handler block. Doing so would require teaching
SSA about all nodes that define memory, and introduce a bunch of new
memory SSA states for EH constructs.

Instead of doing that this PR makes VN treat any handler block with
multiple incoming memory states (meaning that memory changed during the
try) conservatively. The diffs seem small enough; we can consider a more
elaborate fix in .NET 10.

Fix #86112
  • Loading branch information
jakobbotsch authored Aug 12, 2024
1 parent 2ba659b commit f18294b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11007,7 +11007,13 @@ void Compiler::fgValueNumberBlock(BasicBlock* blk)

ValueNum newMemoryVN;
FlowGraphNaturalLoop* loop = m_blockToLoop->GetLoop(blk);
if ((loop != nullptr) && (loop->GetHeader() == blk))
if (bbIsHandlerBeg(blk))
{
// We do not model memory SSA faithfully for handling (in particular, we do not model that
// the handler may see memory states from intermediate points in the enclosed blocks)
newMemoryVN = vnStore->VNForExpr(blk, TYP_HEAP);
}
else if ((loop != nullptr) && (loop->GetHeader() == blk))
{
newMemoryVN = fgMemoryVNForLoopSideEffects(memoryKind, blk, loop);
}
Expand Down
39 changes: 39 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_86112/Runtime_86112.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.Runtime.CompilerServices;
using Xunit;

public class Runtime_86112
{
static int _intStatic;

[Fact]
public static int Problem()
{
[MethodImpl(MethodImplOptions.NoInlining)]
static void Throw()
{
_intStatic = 1;
throw new Exception();
}

_intStatic = 2;

try
{
Throw();
_intStatic = 2;
}
catch (Exception)
{
if (_intStatic == 2)
{
return 101;
}
}

return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit f18294b

Please sign in to comment.