Skip to content

Commit

Permalink
JIT: Dump BB in/out memory SSAs inline when SSA is valid (#95017)
Browse files Browse the repository at this point in the history
When SSA is considered to be valid, write in and out SSA numbers for
memory states when dumping the blocks.

For example:

```csharp
public static void Foo(C c)
{
    if (c.V % 2 == 0)
    {
        c.V = 123;
    }
    else
    {
        c.V = 456;
    }

    Console.WriteLine(c.V);
}

public class C
{
    public int V;
}
```
dumps

```
------------ BB01 [000..00A) -> BB03 (cond), preds={} succs={BB02,BB03}
ByrefExposed, GcHeap = m:1

***** BB01
STMT00000 ( 0x000[E-] ... 0x008 )
N009 ( 10, 10) [000007] ---XG------                         ▌  JTRUE     void
N008 (  8,  8) [000006] J--XG--N---                         └──▌  NE        int
N006 (  6,  6) [000004] ---XG------                            ├──▌  AND       int
N004 (  4,  4) [000002] ---XG------                            │  ├──▌  IND       int
N003 (  2,  2) [000024] -------N---                            │  │  └──▌  ADD       byref
N001 (  1,  1) [000000] -----------                            │  │     ├──▌  LCL_VAR   ref    V00 arg0         u:1
N002 (  1,  1) [000023] -----------                            │  │     └──▌  CNS_INT   long   8 Fseq[V]
N005 (  1,  1) [000003] -----------                            │  └──▌  CNS_INT   int    1
N007 (  1,  1) [000005] -----------                            └──▌  CNS_INT   int    0

ByrefExposed, GcHeap = m:1

------------ BB02 [00A..014) -> BB04 (always), preds={BB01} succs={BB04}
ByrefExposed, GcHeap = m:1

***** BB02
STMT00004 ( 0x00A[E-] ... 0x00D )
N005 (  6,  6) [000020] -A-XG------                         ▌  STOREIND  int
N003 (  2,  2) [000028] -------N---                         ├──▌  ADD       byref
N001 (  1,  1) [000017] -----------                         │  ├──▌  LCL_VAR   ref    V00 arg0         u:1
N002 (  1,  1) [000027] -----------                         │  └──▌  CNS_INT   long   8 Fseq[V]
N004 (  1,  1) [000018] -----------                         └──▌  CNS_INT   int    123

ByrefExposed, GcHeap = m:5

------------ BB03 [014..01F), preds={BB01} succs={BB04}
ByrefExposed, GcHeap = m:1

***** BB03
STMT00001 ( 0x014[E-] ... 0x01A )
N005 (  6,  9) [000011] -A-XG------                         ▌  STOREIND  int
N003 (  2,  2) [000026] -------N---                         ├──▌  ADD       byref
N001 (  1,  1) [000008] -----------                         │  ├──▌  LCL_VAR   ref    V00 arg0         u:1
N002 (  1,  1) [000025] -----------                         │  └──▌  CNS_INT   long   8 Fseq[V]
N004 (  1,  4) [000009] -----------                         └──▌  CNS_INT   int    456

ByrefExposed, GcHeap = m:4

------------ BB04 [01F..02B) (return), preds={BB02,BB03} succs={}
ByrefExposed, GcHeap = phi(m:5, m:4)

***** BB04
STMT00002 ( 0x01F[E-] ... 0x02A )
N005 ( 18, 10) [000015] --CXG------                         ▌  CALL      void   System.Console:WriteLine(int)
N004 (  4,  4) [000014] ---XG------ arg0 in rcx             └──▌  IND       int
N003 (  2,  2) [000030] -------N---                            └──▌  ADD       byref
N001 (  1,  1) [000012] -----------                               ├──▌  LCL_VAR   ref    V00 arg0         u:1 (last use)
N002 (  1,  1) [000029] -----------                               └──▌  CNS_INT   long   8 Fseq[V]

***** BB04
STMT00003 ( 0x02A[E-] ... ??? )
N001 (  0,  0) [000016] -----------                         ▌  RETURN    void

ByrefExposed, GcHeap = m:3

```

after SSA.
  • Loading branch information
jakobbotsch authored Nov 21, 2023
1 parent aaa5347 commit 186cf81
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,7 @@ void Compiler::compInit(ArenaAllocator* pAlloc,
m_nodeToLoopMemoryBlockMap = nullptr;
m_signatureToLookupInfoMap = nullptr;
fgSsaPassesCompleted = 0;
fgSsaChecksEnabled = false;
fgSsaValid = false;
fgVNPassesCompleted = 0;

// check that HelperCallProperties are initialized
Expand Down Expand Up @@ -4960,10 +4960,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
// So, disable the ssa checks.
//
if (fgSsaChecksEnabled)
if (fgSsaValid)
{
JITDUMP("Disabling SSA checking before assertion prop\n");
fgSsaChecksEnabled = false;
JITDUMP("Marking SSA as invalid before assertion prop\n");
fgSsaValid = false;
}

if (doAssertionProp)
Expand Down Expand Up @@ -5742,7 +5742,7 @@ void Compiler::ResetOptAnnotations()
m_dominancePreds = nullptr;
fgSsaPassesCompleted = 0;
fgVNPassesCompleted = 0;
fgSsaChecksEnabled = false;
fgSsaValid = false;

for (BasicBlock* const block : Blocks())
{
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5051,7 +5051,7 @@ class Compiler
void fgResetForSsa();

unsigned fgSsaPassesCompleted; // Number of times fgSsaBuild has been run.
bool fgSsaChecksEnabled; // True if SSA info can be cross-checked versus IR
bool fgSsaValid; // True if SSA info is valid and can be cross-checked versus IR

#ifdef DEBUG
void DumpSsaSummary();
Expand Down Expand Up @@ -5655,6 +5655,9 @@ class Compiler
void fgDumpBlock(BasicBlock* block);
void fgDumpTrees(BasicBlock* firstBlock, BasicBlock* lastBlock);

void fgDumpBlockMemorySsaIn(BasicBlock* block);
void fgDumpBlockMemorySsaOut(BasicBlock* block);

static fgWalkPreFn fgStress64RsltMulCB;
void fgStress64RsltMul();
void fgDebugCheckUpdate();
Expand Down
85 changes: 84 additions & 1 deletion src/coreclr/jit/fgdiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,11 @@ void Compiler::fgDumpBlock(BasicBlock* block)
printf("\n------------ ");
block->dspBlockHeader(this);

if (fgSsaValid)
{
fgDumpBlockMemorySsaIn(block);
}

if (!block->IsLIR())
{
for (Statement* const stmt : block->Statements())
Expand All @@ -2472,6 +2477,12 @@ void Compiler::fgDumpBlock(BasicBlock* block)
{
gtDispRange(LIR::AsRange(block));
}

if (fgSsaValid)
{
printf("\n");
fgDumpBlockMemorySsaOut(block);
}
}

//------------------------------------------------------------------------
Expand All @@ -2498,6 +2509,78 @@ void Compiler::fgDumpTrees(BasicBlock* firstBlock, BasicBlock* lastBlock)
"----------\n");
}

//------------------------------------------------------------------------
// fgDumpBlockMemorySsaIn: Dump memory state SSAs incoming to a block.
//
// Arguments:
// block - The block
//
void Compiler::fgDumpBlockMemorySsaIn(BasicBlock* block)
{
for (MemoryKind memoryKind : allMemoryKinds())
{
if (byrefStatesMatchGcHeapStates)
{
printf("SSA MEM: %s, %s", memoryKindNames[ByrefExposed], memoryKindNames[GcHeap]);
}
else
{
printf("SSA MEM: %s", memoryKindNames[memoryKind]);
}

if (block->bbMemorySsaPhiFunc[memoryKind] == nullptr)
{
printf(" = m:%u\n", block->bbMemorySsaNumIn[memoryKind]);
}
else
{
printf(" = phi(");
BasicBlock::MemoryPhiArg* phiArgs = block->bbMemorySsaPhiFunc[memoryKind];
const char* sep = "";
for (BasicBlock::MemoryPhiArg* arg = block->bbMemorySsaPhiFunc[memoryKind]; arg != nullptr;
arg = arg->m_nextArg)
{
printf("%sm:%u", sep, arg->GetSsaNum());
sep = ", ";
}
printf(")\n");
}

if (byrefStatesMatchGcHeapStates)
{
break;
}
}
}

//------------------------------------------------------------------------
// fgDumpBlockMemorySsaOut: Dump memory state SSAs outgoing from a block.
//
// Arguments:
// block - The block
//
void Compiler::fgDumpBlockMemorySsaOut(BasicBlock* block)
{
for (MemoryKind memoryKind : allMemoryKinds())
{
if (byrefStatesMatchGcHeapStates)
{
printf("SSA MEM: %s, %s", memoryKindNames[ByrefExposed], memoryKindNames[GcHeap]);
}
else
{
printf("SSA MEM: %s", memoryKindNames[memoryKind]);
}

printf(" = m:%u\n", block->bbMemorySsaNumOut[memoryKind]);

if (byrefStatesMatchGcHeapStates)
{
break;
}
}
}

/*****************************************************************************
* Try to create as many candidates for GTF_MUL_64RSLT as possible.
* We convert 'intOp1*intOp2' into 'int(long(nop(intOp1))*long(intOp2))'.
Expand Down Expand Up @@ -4525,7 +4608,7 @@ class SsaCheckVisitor : public GenTreeVisitor<SsaCheckVisitor>
//
void Compiler::fgDebugCheckSsa()
{
if (!fgSsaChecksEnabled)
if (!fgSsaValid)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/ssabuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ PhaseStatus Compiler::fgSsaBuild()
SsaBuilder builder(this);
builder.Build();
fgSsaPassesCompleted++;
fgSsaChecksEnabled = true;
fgSsaValid = true;
#ifdef DEBUG
JitTestCheckSSA();
#endif // DEBUG
Expand Down

0 comments on commit 186cf81

Please sign in to comment.