Skip to content

Commit

Permalink
JIT: Limit 3-opt to 1000 swaps per run (#112259)
Browse files Browse the repository at this point in the history
Fixes #111988.
  • Loading branch information
amanasifkhalid authored Feb 7, 2025
1 parent 7c8d89c commit 5f652c2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6352,6 +6352,7 @@ class Compiler
class ThreeOptLayout
{
static bool EdgeCmp(const FlowEdge* left, const FlowEdge* right);
static constexpr unsigned maxSwaps = 1000;

Compiler* compiler;
PriorityQueue<FlowEdge*, decltype(&ThreeOptLayout::EdgeCmp)> cutPoints;
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5343,7 +5343,8 @@ bool Compiler::ThreeOptLayout::RunGreedyThreeOptPass(unsigned startPos, unsigned
// and before the destination block, and swap the partitions to create fallthrough.
// If it is, do the swap, and for the blocks before/after each cut point that lost fallthrough,
// consider adding their successors/predecessors to 'cutPoints'.
while (!cutPoints.Empty())
unsigned numSwaps = 0;
while (!cutPoints.Empty() && (numSwaps < maxSwaps))
{
FlowEdge* const candidateEdge = cutPoints.Pop();
candidateEdge->markUnvisited();
Expand Down Expand Up @@ -5498,8 +5499,10 @@ bool Compiler::ThreeOptLayout::RunGreedyThreeOptPass(unsigned startPos, unsigned
}

modified = true;
numSwaps++;
}

cutPoints.Clear();
return modified;
}

Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/priorityqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class PriorityQueue
return data.empty();
}

void Clear()
{
data.clear();
}

// Insert new element at the back of the vector.
// Then, while the new element has a higher priority than its parent, move the element up.
void Push(const T& value)
Expand Down

0 comments on commit 5f652c2

Please sign in to comment.