Skip to content
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

JIT: Add 3-opt implementation for improving upon RPO-based layout #103450

Merged
merged 48 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
5813e1a
Implement k-opt for non-EH methods
amanasifkhalid Jun 13, 2024
3f4e749
Enable for methods with EH
amanasifkhalid Jun 13, 2024
da291af
Merge branch 'main' into k-opt-layout
amanasifkhalid Jun 13, 2024
699714b
Add comments
amanasifkhalid Jun 13, 2024
0849f76
Style
amanasifkhalid Jun 13, 2024
e223927
Merge branch 'main' into k-opt-layout
amanasifkhalid Jun 13, 2024
044b332
Only one iteration for now; try to reduce TP cost
amanasifkhalid Jun 13, 2024
f0e7f6b
Remove initial layout cost calculation
amanasifkhalid Jun 13, 2024
41efb9b
Conditionalize EH checks
amanasifkhalid Jun 13, 2024
5b7a85e
Merge from main
amanasifkhalid Sep 18, 2024
94c2272
Add priority queue impl
amanasifkhalid Sep 18, 2024
0081d9b
wip
amanasifkhalid Sep 20, 2024
4700e65
Fix lambda capture
amanasifkhalid Sep 20, 2024
cabacf9
Merge branch 'main' into k-opt-layout
amanasifkhalid Sep 23, 2024
ebb7e6a
Consider forward conditional jumps
amanasifkhalid Sep 24, 2024
4eb4471
Remove debug print
amanasifkhalid Sep 24, 2024
0175b18
Consider backward jumps; find more initial candidates
amanasifkhalid Sep 25, 2024
bbc28df
Revert irrelevant changes
amanasifkhalid Sep 25, 2024
2e507be
Missed a few
amanasifkhalid Sep 25, 2024
9ed6452
Add JitDump check
amanasifkhalid Sep 25, 2024
40fc6bc
Add more candidate edges when reordering
amanasifkhalid Sep 25, 2024
0b8e830
Merge branch 'main' into k-opt-layout
amanasifkhalid Sep 25, 2024
a3b7392
Don't add duplicate edges to cutPoints
amanasifkhalid Sep 26, 2024
d468a8a
Consider each candidate edge at most once
amanasifkhalid Sep 27, 2024
9cc1860
Merge from main
amanasifkhalid Oct 17, 2024
3ee5c42
Don't factor cold branches into cost calculatioN
amanasifkhalid Oct 17, 2024
5c134b5
Merge from main
amanasifkhalid Oct 21, 2024
d6fea98
Remove used candidates set
amanasifkhalid Oct 21, 2024
872b431
Revert "Remove used candidates set"
amanasifkhalid Oct 21, 2024
fadaeba
Tweak cold block finding
amanasifkhalid Oct 21, 2024
e17b169
Refactor into ThreeOptLayout class
amanasifkhalid Oct 25, 2024
eea003f
Reorder EH regions
amanasifkhalid Oct 28, 2024
96ef576
Fix completely cold try regions (clean replay)
amanasifkhalid Oct 28, 2024
8d95f49
Small cleanup
amanasifkhalid Oct 29, 2024
5388e9e
Add currEHRegion member
amanasifkhalid Oct 29, 2024
5e90c89
Only move blocks within same region after 3-opt
amanasifkhalid Oct 29, 2024
1cecc72
Merge from main
amanasifkhalid Oct 29, 2024
b7a0a03
Cleanup
amanasifkhalid Oct 29, 2024
6ad7541
Comments
amanasifkhalid Oct 29, 2024
a26be54
EdgeCmp tie-breaker
amanasifkhalid Oct 30, 2024
a1b8661
Comment feedback; replace 'usedCandidates' set with FlowEdge flag
amanasifkhalid Oct 30, 2024
ebbb973
Reframe cost model as maximal score problem
amanasifkhalid Oct 31, 2024
7d9734f
Small refactor
amanasifkhalid Oct 31, 2024
a2b3e16
Simplify adding new candidate edges
amanasifkhalid Oct 31, 2024
88d5389
Guess number of hot blocks when allocating arrays
amanasifkhalid Nov 4, 2024
4ab9f17
Skip reordering if too few blocks
amanasifkhalid Nov 4, 2024
ed39074
Add another check for too few blocks
amanasifkhalid Nov 4, 2024
f2eb773
JITDUMP msg
amanasifkhalid Nov 4, 2024
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
21 changes: 21 additions & 0 deletions src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,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 @@ -604,6 +607,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 @@ -688,6 +692,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 @@ -5257,6 +5257,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 @@ -6251,6 +6252,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
Loading