Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/coreclr/jit/inductionvariableopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,11 @@ bool Compiler::optWidenPrimaryIV(FlowGraphNaturalLoop* loop, unsigned lclNum, Sc

BasicBlock* preheader = loop->EntryEdge(0)->getSourceBlock();
BasicBlock* initBlock = preheader;
if ((startSsaDsc->GetBlock() != nullptr) && (startSsaDsc->GetDefNode() != nullptr))
// Prefer to initialize the widened IV in the same block as the reaching def
// of the narrow IV, but only if the reaching def is not a phi. RBO's jump threading
// can leave stale SSA with the once-containing block being unreachable.
if ((startSsaDsc->GetBlock() != nullptr) && (startSsaDsc->GetDefNode() != nullptr) &&
!startSsaDsc->GetDefNode()->IsPhiDefn())
{
initBlock = startSsaDsc->GetBlock();
}
Expand Down
52 changes: 52 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_130189/Runtime_130189.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using Xunit;

// Regression test for https://github.com/dotnet/runtime/issues/130189
//
// Redundant branch opts' dominator-based jump threading used to bypass a block
// that contained a globally-used PHI def without updating SSA. That left the
// loop reading a stale (soon to be dead) PHI value, which subsequent induction
// variable widening then miscompiled, producing a wrong result.

public sealed class Runtime_130189
{
ushort[] d = [0, 0, 1, 65535, 1, 0, 1, 65535, 65535];
int[] a = [0, 4];
int i;

bool Next(out int p)
{
bool r;
if (r = i < a.Length)
p = a[i++];
else
p = -1;
return r;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
internal long Sum()
{
i = 0;
long s = 0;
int x, p;
while (Next(out p))
{
while ((x = d[p]) != 65535)
{
s += x;
p++;
}
}
return s;
}

[Fact]
public static void TestEntryPoint()
{
Assert.Equal(3, new Runtime_130189().Sum());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading