Skip to content

Commit 655f414

Browse files
authored
JIT: add ability to order blocks in dump by bbNum or bbID (#70399)
By default the JIT will dump blocks in bbNext order. This adds other ordering options, specified by `JitDumpFgBlockOrder`: * `0 (default) bbNext` * `1 bbNum` * `2 bbID` `bbID` in particular is useful when comparing JIT dumps where blocks have been reordered but the blocks themselves have similar content.
1 parent 77a8d88 commit 655f414

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

src/coreclr/jit/compiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4247,7 +4247,8 @@ class Compiler
42474247
unsigned fgEdgeCount; // # of control flow edges between the BBs
42484248
unsigned fgBBcount; // # of BBs in the method
42494249
#ifdef DEBUG
4250-
unsigned fgBBcountAtCodegen; // # of BBs in the method at the start of codegen
4250+
unsigned fgBBcountAtCodegen; // # of BBs in the method at the start of codegen
4251+
jitstd::vector<BasicBlock*>* fgBBOrder; // ordered vector of BBs
42514252
#endif
42524253
unsigned fgBBNumMax; // The max bbNum that has been assigned to basic blocks
42534254
unsigned fgDomBBcount; // # of BBs for which we have dominator and reachability information

src/coreclr/jit/fgbasic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void Compiler::fgInit()
6161

6262
#ifdef DEBUG
6363
fgBBcountAtCodegen = 0;
64+
fgBBOrder = nullptr;
6465
#endif // DEBUG
6566

6667
fgBBNumMax = 0;

src/coreclr/jit/fgdiagnostic.cpp

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#endif
99

1010
#include "allocacheck.h" // for alloca
11+
#include "jitstd/algorithm.h"
1112

1213
// Flowgraph Check and Dump Support
1314

@@ -2181,25 +2182,67 @@ void Compiler::fgTableDispBasicBlock(BasicBlock* block, int ibcColWidth /* = 0 *
21812182

21822183
void Compiler::fgDispBasicBlocks(BasicBlock* firstBlock, BasicBlock* lastBlock, bool dumpTrees)
21832184
{
2184-
BasicBlock* block;
2185+
// Build vector of blocks in order.
2186+
//
2187+
if (fgBBOrder == nullptr)
2188+
{
2189+
CompAllocator allocator = getAllocator(CMK_DebugOnly);
2190+
fgBBOrder = new (allocator) jitstd::vector<BasicBlock*>(allocator);
2191+
}
2192+
2193+
fgBBOrder->reserve(fgBBcount);
2194+
fgBBOrder->clear();
21852195

2186-
// If any block has IBC data, we add an "IBC weight" column just before the 'IL range' column. This column is as
2187-
// wide as necessary to accommodate all the various IBC weights. It's at least 4 characters wide, to accommodate
2188-
// the "IBC" title and leading space.
21892196
int ibcColWidth = 0;
2190-
for (block = firstBlock; block != nullptr; block = block->bbNext)
2197+
2198+
for (BasicBlock* block = firstBlock; block != nullptr; block = block->bbNext)
21912199
{
21922200
if (block->hasProfileWeight())
21932201
{
21942202
int thisIbcWidth = CountDigits(block->bbWeight);
21952203
ibcColWidth = max(ibcColWidth, thisIbcWidth);
21962204
}
21972205

2206+
fgBBOrder->push_back(block);
2207+
21982208
if (block == lastBlock)
21992209
{
22002210
break;
22012211
}
22022212
}
2213+
2214+
bool inDefaultOrder = true;
2215+
2216+
struct fgBBNumCmp
2217+
{
2218+
bool operator()(const BasicBlock* bb1, const BasicBlock* bb2)
2219+
{
2220+
return bb1->bbNum < bb2->bbNum;
2221+
}
2222+
};
2223+
2224+
struct fgBBIDCmp
2225+
{
2226+
bool operator()(const BasicBlock* bb1, const BasicBlock* bb2)
2227+
{
2228+
return bb1->bbID < bb2->bbID;
2229+
}
2230+
};
2231+
2232+
// Optionally sort
2233+
//
2234+
if (JitConfig.JitDumpFgBlockOrder() == 1)
2235+
{
2236+
jitstd::sort(fgBBOrder->begin(), fgBBOrder->end(), fgBBNumCmp());
2237+
inDefaultOrder = false;
2238+
}
2239+
else if (JitConfig.JitDumpFgBlockOrder() == 2)
2240+
{
2241+
2242+
jitstd::sort(fgBBOrder->begin(), fgBBOrder->end(), fgBBIDCmp());
2243+
inDefaultOrder = false;
2244+
}
2245+
22032246
if (ibcColWidth > 0)
22042247
{
22052248
ibcColWidth = max(ibcColWidth, 3) + 1; // + 1 for the leading space
@@ -2234,7 +2277,7 @@ void Compiler::fgDispBasicBlocks(BasicBlock* firstBlock, BasicBlock* lastBlock,
22342277

22352278
// clang-format on
22362279

2237-
for (block = firstBlock; block; block = block->bbNext)
2280+
for (BasicBlock* block : *fgBBOrder)
22382281
{
22392282
// First, do some checking on the bbPrev links
22402283
if (block->bbPrev)
@@ -2249,7 +2292,7 @@ void Compiler::fgDispBasicBlocks(BasicBlock* firstBlock, BasicBlock* lastBlock,
22492292
printf("bad prev link!\n");
22502293
}
22512294

2252-
if (block == fgFirstColdBlock)
2295+
if (inDefaultOrder && (block == fgFirstColdBlock))
22532296
{
22542297
printf(
22552298
"~~~~~~%*s~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%*s~~~~~~~~~~~~~~~~~~~~~~~~~~%*s~~~~~~~~~~~~~~~~~~~~~~~~"
@@ -2258,7 +2301,7 @@ void Compiler::fgDispBasicBlocks(BasicBlock* firstBlock, BasicBlock* lastBlock,
22582301
}
22592302

22602303
#if defined(FEATURE_EH_FUNCLETS)
2261-
if (block == fgFirstFuncletBB)
2304+
if (inDefaultOrder && (block == fgFirstFuncletBB))
22622305
{
22632306
printf(
22642307
"++++++%*s+++++++++++++++++++++++++++++++++++++%*s++++++++++++++++++++++++++%*s++++++++++++++++++++++++"
@@ -2282,7 +2325,13 @@ void Compiler::fgDispBasicBlocks(BasicBlock* firstBlock, BasicBlock* lastBlock,
22822325

22832326
if (dumpTrees)
22842327
{
2285-
fgDumpTrees(firstBlock, lastBlock);
2328+
for (BasicBlock* block : *fgBBOrder)
2329+
{
2330+
fgDumpBlock(block);
2331+
}
2332+
printf("\n-----------------------------------------------------------------------------------------------------"
2333+
"----"
2334+
"----------\n");
22862335
}
22872336
}
22882337

src/coreclr/jit/jitconfigvalues.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ CONFIG_INTEGER(JitDumpFgBlockID, W("JitDumpFgBlockID"), 0) // 0 == display block
243243
// bbNum and bbID
244244
CONFIG_INTEGER(JitDumpFgBlockFlags, W("JitDumpFgBlockFlags"), 0) // 0 == don't display block flags; 1 == display flags
245245
CONFIG_INTEGER(JitDumpFgLoopFlags, W("JitDumpFgLoopFlags"), 0) // 0 == don't display loop flags; 1 == display flags
246+
CONFIG_INTEGER(JitDumpFgBlockOrder, W("JitDumpFgBlockOrder"), 0) // 0 == bbNext order; 1 == bbNum order; 2 == bbID
247+
// order
246248

247249
CONFIG_STRING(JitDumpPreciseDebugInfoFile, W("JitDumpPreciseDebugInfoFile"))
248250
CONFIG_INTEGER(JitDisasmWithDebugInfo, W("JitDisasmWithDebugInfo"), 0)

0 commit comments

Comments
 (0)