Skip to content

Commit

Permalink
JIT: Add 3-opt implementation for improving upon RPO-based block layo…
Browse files Browse the repository at this point in the history
  • Loading branch information
amanasifkhalid authored Nov 4, 2024
1 parent 2bb4bb7 commit 1c10cee
Show file tree
Hide file tree
Showing 4 changed files with 537 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ struct FlowEdge
// The count of duplicate "edges" (used for switch stmts or degenerate branches)
unsigned m_dupCount;

// Convenience flag for phases that need to track edge visitation
bool m_visited;

// True if likelihood has been set
INDEBUG(bool m_likelihoodSet);

Expand All @@ -603,6 +606,7 @@ struct FlowEdge
, m_destBlock(destBlock)
, m_likelihood(0)
, m_dupCount(0)
, m_visited(false)
#ifdef DEBUG
, m_likelihoodSet(false)
#endif // DEBUG
Expand Down Expand Up @@ -687,6 +691,23 @@ struct FlowEdge
assert(m_dupCount >= 1);
m_dupCount--;
}

bool visited() const
{
return m_visited;
}

void markVisited()
{
assert(!visited());
m_visited = true;
}

void markUnvisited()
{
assert(visited());
m_visited = false;
}
};

//------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5163,6 +5163,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
auto lateLayoutPhase = [this] {
fgDoReversePostOrderLayout();
fgMoveColdBlocks();
fgSearchImprovedLayout();

if (compHndBBtabCount != 0)
{
Expand Down
24 changes: 24 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include "cycletimer.h"
#include "blockset.h"
#include "arraystack.h"
#include "priorityqueue.h"
#include "hashbv.h"
#include "jitexpandarray.h"
#include "valuenum.h"
Expand Down Expand Up @@ -6213,6 +6214,29 @@ class Compiler
bool fgReorderBlocks(bool useProfile);
void fgDoReversePostOrderLayout();
void fgMoveColdBlocks();
void fgSearchImprovedLayout();

class ThreeOptLayout
{
static bool EdgeCmp(const FlowEdge* left, const FlowEdge* right);

Compiler* compiler;
PriorityQueue<FlowEdge*, decltype(&ThreeOptLayout::EdgeCmp)> cutPoints;
unsigned* ordinals;
BasicBlock** blockOrder;
BasicBlock** tempOrder;
unsigned numCandidateBlocks;
unsigned currEHRegion;

void ConsiderEdge(FlowEdge* edge);
void AddNonFallthroughSuccs(unsigned blockPos);
void AddNonFallthroughPreds(unsigned blockPos);
bool RunThreeOptPass(BasicBlock* startBlock, BasicBlock* endBlock);

public:
ThreeOptLayout(Compiler* comp);
void Run();
};

template <bool hasEH>
void fgMoveHotJumps();
Expand Down
Loading

0 comments on commit 1c10cee

Please sign in to comment.