Skip to content

Commit 486142a

Browse files
BajtazarGrzegorz Czarnecki
andauthored
[RISC-V] Fix JitDisasm in release build (#95502)
* 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 <[email protected]>
1 parent 1d4897d commit 486142a

File tree

6 files changed

+63
-50
lines changed

6 files changed

+63
-50
lines changed

src/coreclr/jit/ee_il_dll.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,13 @@ FILE* jitstdout()
144144
}
145145

146146
// Like printf/logf, but only outputs to jitstdout -- skips call back into EE.
147-
void jitprintf(const char* fmt, ...)
147+
int jitprintf(const char* fmt, ...)
148148
{
149149
va_list vl;
150150
va_start(vl, fmt);
151-
vfprintf(jitstdout(), fmt, vl);
151+
int status = vfprintf(jitstdout(), fmt, vl);
152152
va_end(vl);
153+
return status;
153154
}
154155

155156
void jitShutdown(bool processIsTerminating)

src/coreclr/jit/emit.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ void emitter::appendToCurIG(instrDesc* id)
15421542
* Display (optionally) an instruction offset.
15431543
*/
15441544

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

41954195
printf("\n");
41964196

4197-
#if !defined(TARGET_RISCV64)
4198-
// TODO-RISCV64-Bug: When JitDump is on, it asserts in emitDispIns which is not implemented.
41994197
if (displayInstructions)
42004198
{
42014199
instrDesc* id = emitFirstInstrDesc(ig->igData);
@@ -4228,7 +4226,6 @@ void emitter::emitDispIG(insGroup* ig, bool displayFunc, bool displayInstruction
42284226
printf("\n");
42294227
}
42304228
}
4231-
#endif // !TARGET_RISCV64
42324229
}
42334230
}
42344231

src/coreclr/jit/emit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,7 @@ class emitter
21222122
void emitDispJumpList();
21232123
void emitDispClsVar(CORINFO_FIELD_HANDLE fldHnd, ssize_t offs, bool reloc = false);
21242124
void emitDispFrameRef(int varx, int disp, int offs, bool asmfm);
2125-
void emitDispInsAddr(BYTE* code);
2125+
void emitDispInsAddr(const BYTE* code);
21262126
void emitDispInsOffs(unsigned offs, bool doffs);
21272127
void emitDispInsHex(instrDesc* id, BYTE* code, size_t sz);
21282128
void emitDispEmbBroadcastCount(instrDesc* id);

src/coreclr/jit/emitriscv64.cpp

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,19 +2138,13 @@ void emitter::emitJumpDistBind()
21382138

21392139
size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
21402140
{
2141-
BYTE* dstRW = *dp + writeableOffset;
2142-
BYTE* dstRW2 = dstRW + 4; // addr for updating gc info if needed.
2143-
code_t code = 0;
2144-
instruction ins;
2145-
size_t sz; // = emitSizeOfInsDsc(id);
2146-
2147-
#ifdef DEBUG
2148-
#if DUMP_GC_TABLES
2149-
bool dspOffs = emitComp->opts.dspGCtbls;
2150-
#else
2151-
bool dspOffs = !emitComp->opts.disDiffable;
2152-
#endif
2153-
#endif // DEBUG
2141+
BYTE* dstRW = *dp + writeableOffset;
2142+
BYTE* dstRW2 = dstRW + 4; // addr for updating gc info if needed.
2143+
const BYTE* const odstRW = dstRW;
2144+
const BYTE* const odst = *dp;
2145+
code_t code = 0;
2146+
instruction ins;
2147+
size_t sz; // = emitSizeOfInsDsc(id);
21542148

21552149
assert(REG_NA == (int)REG_NA);
21562150

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

28802874
if (emitComp->opts.disAsm || emitComp->verbose)
28812875
{
2882-
code_t* cp = (code_t*)(*dp + writeableOffset);
2883-
while ((BYTE*)cp != dstRW)
2884-
{
2885-
emitDisInsName(*cp, (BYTE*)cp, id);
2886-
cp++;
2887-
}
2876+
#if DUMP_GC_TABLES
2877+
bool dspOffs = emitComp->opts.dspGCtbls;
2878+
#else // !DUMP_GC_TABLES
2879+
bool dspOffs = !emitComp->opts.disDiffable;
2880+
#endif // !DUMP_GC_TABLES
2881+
emitDispIns(id, false, dspOffs, true, emitCurCodeOffs(odst), *dp, (dstRW - odstRW), ig);
28882882
}
28892883

28902884
if (emitComp->compDebugBreak)
@@ -2896,7 +2890,12 @@ size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
28962890
assert(!"JitBreakEmitOutputInstr reached");
28972891
}
28982892
}
2899-
#endif
2893+
#else // !DEBUG
2894+
if (emitComp->opts.disAsm)
2895+
{
2896+
emitDispIns(id, false, false, true, emitCurCodeOffs(odst), *dp, (dstRW - odstRW), ig);
2897+
}
2898+
#endif // !DEBUG
29002899

29012900
/* All instructions are expected to generate code */
29022901

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

2913-
#ifdef DEBUG
2914-
29152912
// clang-format off
29162913
static const char* const RegNames[] =
29172914
{
@@ -2922,39 +2919,32 @@ static const char* const RegNames[] =
29222919

29232920
//----------------------------------------------------------------------------------------
29242921
// Disassemble the given instruction.
2925-
// The `emitter::emitDisInsName` is focused on the most important for debugging.
2922+
// The `emitter::emitDispInsName` is focused on the most important for debugging.
29262923
// So it implemented as far as simply and independently which is very useful for
29272924
// porting easily to the release mode.
29282925
//
29292926
// Arguments:
29302927
// code - The instruction's encoding.
29312928
// addr - The address of the code.
2929+
// doffs - Flag informing whether the instruction's offset should be displayed.
2930+
// insOffset - The instruction's offset.
29322931
// id - The instrDesc of the code if needed.
29332932
//
29342933
// Note:
29352934
// The length of the instruction's name include aligned space is 15.
29362935
//
29372936

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

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

2945-
bool disOpcode = !emitComp->opts.disDiffable;
2946-
bool disAddr = emitComp->opts.disAddr;
2947-
if (disAddr)
2948-
{
2949-
printf(" 0x%llx", insAdr);
2950-
}
2944+
emitDispInsAddr(insAdr);
2945+
emitDispInsOffs(insOffset, doffs);
29512946

2952-
printf(" ");
2953-
2954-
if (disOpcode)
2955-
{
2956-
printf("%08X ", code);
2957-
}
2947+
printf(" ");
29582948

29592949
switch (opcode)
29602950
{
@@ -3915,15 +3905,38 @@ void emitter::emitDispInsHex(instrDesc* id, BYTE* code, size_t sz)
39153905
}
39163906
}
39173907

3908+
void emitter::emitDispInsInstrNum(const instrDesc* id) const
3909+
{
3910+
#ifdef DEBUG
3911+
if (!emitComp->verbose)
3912+
return;
3913+
3914+
printf("IN%04x: ", id->idDebugOnlyInfo()->idNum);
3915+
#endif // DEBUG
3916+
}
3917+
39183918
void emitter::emitDispIns(
39193919
instrDesc* id, bool isNew, bool doffs, bool asmfm, unsigned offset, BYTE* pCode, size_t sz, insGroup* ig)
39203920
{
3921-
// RISCV64 implements this similar by `emitter::emitDisInsName`.
3922-
// For RISCV64 maybe the `emitDispIns` is over complicate.
3923-
// The `emitter::emitDisInsName` is focused on the most important for debugging.
3924-
NYI_RISCV64("RISCV64 not used the emitter::emitDispIns");
3921+
if (pCode == nullptr)
3922+
return;
3923+
3924+
emitDispInsInstrNum(id);
3925+
3926+
const BYTE* instr = pCode + writeableOffset;
3927+
size_t instrSize;
3928+
for (size_t i = 0; i < sz; instr += instrSize, i += instrSize, offset += instrSize)
3929+
{
3930+
// TODO-RISCV64: support different size instructions
3931+
instrSize = sizeof(code_t);
3932+
code_t instruction;
3933+
memcpy(&instruction, instr, instrSize);
3934+
emitDispInsName(instruction, instr, doffs, offset, id);
3935+
}
39253936
}
39263937

3938+
#ifdef DEBUG
3939+
39273940
/*****************************************************************************
39283941
*
39293942
* Display a stack frame reference.

src/coreclr/jit/emitriscv64.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ struct CnsVal
2727

2828
const char* emitFPregName(unsigned reg, bool varName = true);
2929
const char* emitVectorRegName(regNumber reg);
30-
31-
void emitDisInsName(code_t code, const BYTE* addr, instrDesc* id);
3230
#endif // DEBUG
3331

3432
void emitIns_J_cond_la(instruction ins, BasicBlock* dst, regNumber reg1 = REG_R0, regNumber reg2 = REG_R0);
@@ -62,6 +60,10 @@ bool emitInsIsLoad(instruction ins);
6260
bool emitInsIsStore(instruction ins);
6361
bool emitInsIsLoadOrStore(instruction ins);
6462

63+
void emitDispInsName(code_t code, const BYTE* addr, bool doffs, unsigned insOffset, instrDesc* id);
64+
65+
void emitDispInsInstrNum(const instrDesc* id) const;
66+
6567
emitter::code_t emitInsCode(instruction ins /*, insFormat fmt*/);
6668

6769
// Generate code for a load or store operation and handle the case of contained GT_LEA op1 with [base + offset]

src/coreclr/jit/host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/*****************************************************************************/
55

6-
void jitprintf(const char* fmt, ...);
6+
int jitprintf(const char* fmt, ...);
77

88
#ifdef DEBUG
99

0 commit comments

Comments
 (0)