Skip to content

Fix an SSA bug from the ASG refactoring #86108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ struct GenTree

bool OperIsSsaDef() const
{
return OperIs(GT_CALL) || OperIsLocalStore();
return OperIsLocalStore() || OperIs(GT_CALL);
}

static bool OperIsHWIntrinsic(genTreeOps gtOper)
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/jit/ssabuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ void SsaBuilder::InsertPhiFunctions(BasicBlock** postOrder, int count)
//
void SsaBuilder::RenameDef(GenTree* defNode, BasicBlock* block)
{
assert(defNode->OperIsSsaDef());
assert(defNode->OperIsStore() || defNode->OperIs(GT_CALL));

GenTreeLclVarCommon* lclNode;
bool isFullDef = false;
Expand Down Expand Up @@ -1114,7 +1114,7 @@ void SsaBuilder::BlockRenameVariables(BasicBlock* block)
{
for (GenTree* const tree : stmt->TreeList())
{
if (tree->OperIsSsaDef())
if (tree->OperIsStore() || tree->OperIs(GT_CALL))
Copy link
Contributor Author

@SingleAccretion SingleAccretion May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In actuality the same problem exists for un-analyzable stores as well. But that is a pre-existing issue.

Reproduction
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static bool Problem()
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Throw()
    {
        _intStatic = 1;
        throw new Exception();
    }

    _intStatic = 2;

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

    return false;
}

Filed #86112 for this.

{
RenameDef(tree, block);
}
Expand Down
39 changes: 39 additions & 0 deletions src/tests/JIT/opt/SSA/MemorySsa.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;

public class MemorySsaTests
{
private static int _intStatic;

public static int Main()
{
return ProblemWithHandlerPhis(new int[0]) ? 101 : 100;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool ProblemWithHandlerPhis(int[] x)
{
_intStatic = 2;

try
{
_intStatic = 1;
_ = x[0];
_intStatic = 2;
}
catch (Exception)
{
// Memory PHIs in handlers must consider all intermediate states
// that might arise from stores between throwing expressions.
if (_intStatic == 2)
{
return true;
}
}

return false;
}
}
12 changes: 12 additions & 0 deletions src/tests/JIT/opt/SSA/MemorySsa.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>