diff --git a/src/coreclr/jit/compiler.h b/src/coreclr/jit/compiler.h index 19c9793935679a..17b92c3ba1d9de 100644 --- a/src/coreclr/jit/compiler.h +++ b/src/coreclr/jit/compiler.h @@ -6357,6 +6357,7 @@ class Compiler class ThreeOptLayout { static bool EdgeCmp(const FlowEdge* left, const FlowEdge* right); + static constexpr unsigned maxSwaps = 1000; Compiler* compiler; PriorityQueue cutPoints; diff --git a/src/coreclr/jit/fgopt.cpp b/src/coreclr/jit/fgopt.cpp index b7118ffd719e3a..7ee0d80aa9dc49 100644 --- a/src/coreclr/jit/fgopt.cpp +++ b/src/coreclr/jit/fgopt.cpp @@ -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(); @@ -5498,8 +5499,10 @@ bool Compiler::ThreeOptLayout::RunGreedyThreeOptPass(unsigned startPos, unsigned } modified = true; + numSwaps++; } + cutPoints.Clear(); return modified; } diff --git a/src/coreclr/jit/priorityqueue.h b/src/coreclr/jit/priorityqueue.h index dc5c62877fc16b..ea26a5c8c1fd38 100644 --- a/src/coreclr/jit/priorityqueue.h +++ b/src/coreclr/jit/priorityqueue.h @@ -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)