Skip to content

Commit

Permalink
[RISC-V] Fix JitDisasm in release build (dotnet#95502)
Browse files Browse the repository at this point in the history
* Implemented emitDispIns for riscv

* Modified emitDispIns name

* Fixed missed case

* Added assert

* Fixed todo

* Added int to jitprintf

* Added prototype of the emit disp ins

* Fixes in emit dis ins name

* Reinforced types

* Removed useless ifdef statement from emit

* Fixed bug in emit disp ins

* Added release mode emit disp

* Formatted riscv64

* [RISC-V] Added todo comment

* [RISC-V] Applied format patch

* [RISC-V] Undo the emit.cpp dispIns changes

* [RISC-V] Fixed formatting

* Removed dead code

* [RISC-V] Changes after review

---------

Co-authored-by: Grzegorz Czarnecki <g.czarnecki@samsung.com>
  • Loading branch information
Bajtazar and Grzegorz Czarnecki committed Dec 13, 2023
1 parent 1d4897d commit 486142a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 50 deletions.
5 changes: 3 additions & 2 deletions src/coreclr/jit/ee_il_dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@ FILE* jitstdout()
}

// Like printf/logf, but only outputs to jitstdout -- skips call back into EE.
void jitprintf(const char* fmt, ...)
int jitprintf(const char* fmt, ...)
{
va_list vl;
va_start(vl, fmt);
vfprintf(jitstdout(), fmt, vl);
int status = vfprintf(jitstdout(), fmt, vl);
va_end(vl);
return status;
}

void jitShutdown(bool processIsTerminating)
Expand Down
5 changes: 1 addition & 4 deletions src/coreclr/jit/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ void emitter::appendToCurIG(instrDesc* id)
* Display (optionally) an instruction offset.
*/

void emitter::emitDispInsAddr(BYTE* code)
void emitter::emitDispInsAddr(const BYTE* code)
{
#ifdef DEBUG
if (emitComp->opts.disAddr)
Expand Down Expand Up @@ -4194,8 +4194,6 @@ void emitter::emitDispIG(insGroup* ig, bool displayFunc, bool displayInstruction

printf("\n");

#if !defined(TARGET_RISCV64)
// TODO-RISCV64-Bug: When JitDump is on, it asserts in emitDispIns which is not implemented.
if (displayInstructions)
{
instrDesc* id = emitFirstInstrDesc(ig->igData);
Expand Down Expand Up @@ -4228,7 +4226,6 @@ void emitter::emitDispIG(insGroup* ig, bool displayFunc, bool displayInstruction
printf("\n");
}
}
#endif // !TARGET_RISCV64
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/emit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2122,7 +2122,7 @@ class emitter
void emitDispJumpList();
void emitDispClsVar(CORINFO_FIELD_HANDLE fldHnd, ssize_t offs, bool reloc = false);
void emitDispFrameRef(int varx, int disp, int offs, bool asmfm);
void emitDispInsAddr(BYTE* code);
void emitDispInsAddr(const BYTE* code);
void emitDispInsOffs(unsigned offs, bool doffs);
void emitDispInsHex(instrDesc* id, BYTE* code, size_t sz);
void emitDispEmbBroadcastCount(instrDesc* id);
Expand Down
93 changes: 53 additions & 40 deletions src/coreclr/jit/emitriscv64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2138,19 +2138,13 @@ void emitter::emitJumpDistBind()

size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
{
BYTE* dstRW = *dp + writeableOffset;
BYTE* dstRW2 = dstRW + 4; // addr for updating gc info if needed.
code_t code = 0;
instruction ins;
size_t sz; // = emitSizeOfInsDsc(id);

#ifdef DEBUG
#if DUMP_GC_TABLES
bool dspOffs = emitComp->opts.dspGCtbls;
#else
bool dspOffs = !emitComp->opts.disDiffable;
#endif
#endif // DEBUG
BYTE* dstRW = *dp + writeableOffset;
BYTE* dstRW2 = dstRW + 4; // addr for updating gc info if needed.
const BYTE* const odstRW = dstRW;
const BYTE* const odst = *dp;
code_t code = 0;
instruction ins;
size_t sz; // = emitSizeOfInsDsc(id);

assert(REG_NA == (int)REG_NA);

Expand Down Expand Up @@ -2879,12 +2873,12 @@ size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)

if (emitComp->opts.disAsm || emitComp->verbose)
{
code_t* cp = (code_t*)(*dp + writeableOffset);
while ((BYTE*)cp != dstRW)
{
emitDisInsName(*cp, (BYTE*)cp, id);
cp++;
}
#if DUMP_GC_TABLES
bool dspOffs = emitComp->opts.dspGCtbls;
#else // !DUMP_GC_TABLES
bool dspOffs = !emitComp->opts.disDiffable;
#endif // !DUMP_GC_TABLES
emitDispIns(id, false, dspOffs, true, emitCurCodeOffs(odst), *dp, (dstRW - odstRW), ig);
}

if (emitComp->compDebugBreak)
Expand All @@ -2896,7 +2890,12 @@ size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
assert(!"JitBreakEmitOutputInstr reached");
}
}
#endif
#else // !DEBUG
if (emitComp->opts.disAsm)
{
emitDispIns(id, false, false, true, emitCurCodeOffs(odst), *dp, (dstRW - odstRW), ig);
}
#endif // !DEBUG

/* All instructions are expected to generate code */

Expand All @@ -2910,8 +2909,6 @@ size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
/*****************************************************************************/
/*****************************************************************************/

#ifdef DEBUG

// clang-format off
static const char* const RegNames[] =
{
Expand All @@ -2922,39 +2919,32 @@ static const char* const RegNames[] =

//----------------------------------------------------------------------------------------
// Disassemble the given instruction.
// The `emitter::emitDisInsName` is focused on the most important for debugging.
// The `emitter::emitDispInsName` is focused on the most important for debugging.
// So it implemented as far as simply and independently which is very useful for
// porting easily to the release mode.
//
// Arguments:
// code - The instruction's encoding.
// addr - The address of the code.
// doffs - Flag informing whether the instruction's offset should be displayed.
// insOffset - The instruction's offset.
// id - The instrDesc of the code if needed.
//
// Note:
// The length of the instruction's name include aligned space is 15.
//

void emitter::emitDisInsName(code_t code, const BYTE* addr, instrDesc* id)
void emitter::emitDispInsName(code_t code, const BYTE* addr, bool doffs, unsigned insOffset, instrDesc* id)
{
const BYTE* insAdr = addr - writeableOffset;

unsigned int opcode = code & 0x7f;
assert((opcode & 0x3) == 0x3);

bool disOpcode = !emitComp->opts.disDiffable;
bool disAddr = emitComp->opts.disAddr;
if (disAddr)
{
printf(" 0x%llx", insAdr);
}
emitDispInsAddr(insAdr);
emitDispInsOffs(insOffset, doffs);

printf(" ");

if (disOpcode)
{
printf("%08X ", code);
}
printf(" ");

switch (opcode)
{
Expand Down Expand Up @@ -3915,15 +3905,38 @@ void emitter::emitDispInsHex(instrDesc* id, BYTE* code, size_t sz)
}
}

void emitter::emitDispInsInstrNum(const instrDesc* id) const
{
#ifdef DEBUG
if (!emitComp->verbose)
return;

printf("IN%04x: ", id->idDebugOnlyInfo()->idNum);
#endif // DEBUG
}

void emitter::emitDispIns(
instrDesc* id, bool isNew, bool doffs, bool asmfm, unsigned offset, BYTE* pCode, size_t sz, insGroup* ig)
{
// RISCV64 implements this similar by `emitter::emitDisInsName`.
// For RISCV64 maybe the `emitDispIns` is over complicate.
// The `emitter::emitDisInsName` is focused on the most important for debugging.
NYI_RISCV64("RISCV64 not used the emitter::emitDispIns");
if (pCode == nullptr)
return;

emitDispInsInstrNum(id);

const BYTE* instr = pCode + writeableOffset;
size_t instrSize;
for (size_t i = 0; i < sz; instr += instrSize, i += instrSize, offset += instrSize)
{
// TODO-RISCV64: support different size instructions
instrSize = sizeof(code_t);
code_t instruction;
memcpy(&instruction, instr, instrSize);
emitDispInsName(instruction, instr, doffs, offset, id);
}
}

#ifdef DEBUG

/*****************************************************************************
*
* Display a stack frame reference.
Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/jit/emitriscv64.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ struct CnsVal

const char* emitFPregName(unsigned reg, bool varName = true);
const char* emitVectorRegName(regNumber reg);

void emitDisInsName(code_t code, const BYTE* addr, instrDesc* id);
#endif // DEBUG

void emitIns_J_cond_la(instruction ins, BasicBlock* dst, regNumber reg1 = REG_R0, regNumber reg2 = REG_R0);
Expand Down Expand Up @@ -62,6 +60,10 @@ bool emitInsIsLoad(instruction ins);
bool emitInsIsStore(instruction ins);
bool emitInsIsLoadOrStore(instruction ins);

void emitDispInsName(code_t code, const BYTE* addr, bool doffs, unsigned insOffset, instrDesc* id);

void emitDispInsInstrNum(const instrDesc* id) const;

emitter::code_t emitInsCode(instruction ins /*, insFormat fmt*/);

// Generate code for a load or store operation and handle the case of contained GT_LEA op1 with [base + offset]
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*****************************************************************************/

void jitprintf(const char* fmt, ...);
int jitprintf(const char* fmt, ...);

#ifdef DEBUG

Expand Down

0 comments on commit 486142a

Please sign in to comment.