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
10 changes: 9 additions & 1 deletion src/coreclr/jit/inductionvariableopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,15 @@ 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 that block is still reachable. Earlier phases
// (e.g. redundant branch opts jump threading) can leave the def in a block
// that is now unreachable while its SSA def still reaches the loop. Emitting
// the initialization there would place it in dead code that is subsequently
// removed, leaving the widened IV uninitialized. Fall back to the preheader
// in that case.
if ((startSsaDsc->GetBlock() != nullptr) && (startSsaDsc->GetDefNode() != nullptr) &&
m_dfsTree->Contains(startSsaDsc->GetBlock()))
{
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());
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="JitBlue\Runtime_129970\Runtime_129970.cs" />
<Compile Include="JitBlue\Runtime_129971\Runtime_129971.cs" />
<Compile Include="JitBlue\Runtime_129972\Runtime_129972.cs" />
<Compile Include="JitBlue\Runtime_130189\Runtime_130189.cs" />
<Compile Include="JitBlue\Runtime_10337\Runtime_10337.cs" />
</ItemGroup>
<Import Project="$(TestSourceDir)MergedTestRunner.targets" />
Expand Down
Loading