Skip to content

JIT: fix issue in cloning loops with trys in handlers #113586

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 1 commit into from
Mar 16, 2025
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
14 changes: 10 additions & 4 deletions src/coreclr/jit/fgehopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2680,8 +2680,8 @@ BasicBlock* Compiler::fgCloneTryRegion(BasicBlock* tryEntry, CloneTryInfo& info,
if (bbIsTryBeg(block))
{
assert(added);
JITDUMP("==> found try entry for EH#%02u nested in handler at " FMT_BB "\n", block->bbNum,
block->getTryIndex());
JITDUMP("==> found try entry for EH#%02u nested in handler at " FMT_BB "\n", block->getTryIndex(),
block->bbNum);
regionsToProcess.Push(block->getTryIndex());
}
}
Expand Down Expand Up @@ -2761,6 +2761,12 @@ BasicBlock* Compiler::fgCloneTryRegion(BasicBlock* tryEntry, CloneTryInfo& info,
assert(insertBeforeIndex == enclosingTryIndex);
}

if (insertBeforeIndex != compHndBBtabCount)
{
JITDUMP("Existing EH region(s) EH#%02u...EH#%02u will become EH#%02u...EH#%02u\n", insertBeforeIndex,
compHndBBtabCount - 1, insertBeforeIndex + regionCount, compHndBBtabCount + regionCount - 1);
}

// Once we call fgTryAddEHTableEntries with deferCloning = false,
// all the EH indicies at or above insertBeforeIndex will shift,
// and the EH table may reallocate.
Expand Down Expand Up @@ -2860,7 +2866,7 @@ BasicBlock* Compiler::fgCloneTryRegion(BasicBlock* tryEntry, CloneTryInfo& info,
//
if (ebd->ebdEnclosingTryIndex != EHblkDsc::NO_ENCLOSING_INDEX)
{
if (XTnum < clonedOutermostRegionIndex)
if (ebd->ebdEnclosingTryIndex < clonedOutermostRegionIndex)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the actual fix, we only want to adjust indices within the span of regions we've cloned.

{
ebd->ebdEnclosingTryIndex += (unsigned short)indexShift;
}
Expand All @@ -2873,7 +2879,7 @@ BasicBlock* Compiler::fgCloneTryRegion(BasicBlock* tryEntry, CloneTryInfo& info,

if (ebd->ebdEnclosingHndIndex != EHblkDsc::NO_ENCLOSING_INDEX)
{
if (XTnum < clonedOutermostRegionIndex)
if (ebd->ebdEnclosingHndIndex < clonedOutermostRegionIndex)
Copy link
Member Author

Choose a reason for hiding this comment

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

Second part of fix.

{
ebd->ebdEnclosingHndIndex += (unsigned short)indexShift;
}
Expand Down
28 changes: 22 additions & 6 deletions src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6140,14 +6140,30 @@ bool FlowGraphNaturalLoop::CanDuplicateWithEH(INDEBUG(const char** reason))
// Check if this is an "outermost" try within the loop.
// If so, we have more checking to do later on.
//
const bool headerInTry = header->hasTryIndex();
unsigned blockIndex = block->getTryIndex();
unsigned outermostBlockIndex = comp->ehTrueEnclosingTryIndex(blockIndex);
bool const headerIsInTry = header->hasTryIndex();
unsigned const blockTryIndex = block->getTryIndex();
unsigned const enclosingTryIndex = comp->ehTrueEnclosingTryIndex(blockTryIndex);

if ((headerInTry && (outermostBlockIndex == header->getTryIndex())) ||
(!headerInTry && (outermostBlockIndex == EHblkDsc::NO_ENCLOSING_INDEX)))
if ((headerIsInTry && (enclosingTryIndex == header->getTryIndex())) ||
(!headerIsInTry && (enclosingTryIndex == EHblkDsc::NO_ENCLOSING_INDEX)))
{
tryRegionsToClone.Push(block);
// When we clone a try we also clone its handler.
//
// This try may be enclosed in a handler whose try begin is in the loop.
// If so we'll clone this try when we clone (the handler of) that try.
//
bool isInHandlerOfInLoopTry = false;
if (block->hasHndIndex())
{
unsigned const enclosingHndIndex = block->getHndIndex();
BasicBlock* const associatedTryBeg = comp->ehGetDsc(enclosingHndIndex)->ebdTryBeg;
isInHandlerOfInLoopTry = this->ContainsBlock(associatedTryBeg);
}

if (!isInHandlerOfInLoopTry)
{
tryRegionsToClone.Push(block);
}
}
}

Expand Down
138 changes: 138 additions & 0 deletions src/tests/JIT/opt/Cloning/loops_with_eh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// g - giant finally (TF will remain try finally)
// p - regions are serial, not nested
// TFi - try finally with what follows in the finally
// I - inlining
//
// x: we currently cannot clone loops where the try is the first thing
// as the header and preheader are different regions
Expand Down Expand Up @@ -1127,5 +1128,142 @@ public static int Sum_TFiTFxL(int[] data, int n)

return sum;
}

[Fact]
public static int Test_LTFiTF() => Sum_LTFiTF(data, n) - 130;

public static int Sum_LTFiTF(int[] data, int n)
{
int sum = 0;

for (int i = 0; i < n; i++)
{
sum += data[i];
try
{
SideEffect();
}
finally
{
try
{
SideEffect();
}
finally
{
sum += 1;
}

sum += 1;
}
}

return sum;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int SomeEH()
{
int sum = 0;

try
{
SideEffect();
}
finally
{
try
{
SideEffect();
}
finally
{
sum += 1;
}

sum += 1;
}

return sum;
}

[Fact]
public static int Test_LITFiTF() => Sum_LITFiTF(data, n) - 130;

public static int Sum_LITFiTF(int[] data, int n)
{
int sum = 0;

for (int i = 0; i < n; i++)
{
sum += data[i];
sum += SomeEH();
}

return sum;
}

[Fact]
public static int Test_TFLTFiTF() => Sum_TFLTFiTF(data, n) - 131;

public static int Sum_TFLTFiTF(int[] data, int n)
Copy link
Member Author

Choose a reason for hiding this comment

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

This test case has the EH arrangement that triggers the bug.

A try with a loop with a try / finally with a try in the finally.

{
int sum = 0;

try
{
for (int i = 0; i < n; i++)
{
sum += data[i];
try
{
SideEffect();
}
finally
{
try
{
SideEffect();
}
finally
{
sum += 1;
}

sum += 1;
}
}
}
finally
{
SideEffect();
sum += 1;
}
return sum;
}

// [Fact]
public static int Test_TFLITFiTF() => Sum_TFLITFiTF(data, n) - 131;

public static int Sum_TFLITFiTF(int[] data, int n)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a case like the above but requires inlining to create the proper loop/EH structure; it's what I ran into testing #112998.

{
int sum = 0;

try
{
for (int i = 0; i < n; i++)
{
sum += data[i];
sum += SomeEH();
}
}
finally
{
SideEffect();
sum += 1;
}

return sum;
}
}

Loading