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

More IR interpreter profiler work #19260

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 13 additions & 1 deletion Core/MIPS/IR/IRInst.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Common/CommonFuncs.h"
#include "Core/MIPS/IR/IRInst.h"
#include "Core/MIPS/MIPSDebugInterface.h"
#include "Core/HLE/ReplaceTables.h"

// Legend
// ======================
Expand All @@ -13,6 +14,7 @@
// 2 = FPR register, Vec2 (uncommon)
// v = Vec4Init constant, chosen by immediate
// s = Shuffle immediate (4 2-bit fields, choosing a xyzw shuffle)
// r = Replacement function (in constant field)

static const IRMeta irMeta[] = {
{ IROp::Nop, "Nop", "" },
Expand Down Expand Up @@ -165,7 +167,7 @@ static const IRMeta irMeta[] = {
{ IROp::Break, "Break", "", IRFLAG_EXIT },
{ IROp::SetPC, "SetPC", "_G" },
{ IROp::SetPCConst, "SetPC", "_C" },
{ IROp::CallReplacement, "CallRepl", "GC", IRFLAG_BARRIER },
{ IROp::CallReplacement, "CallRepl", "Gr", IRFLAG_BARRIER },
{ IROp::Breakpoint, "Breakpoint", "_C", IRFLAG_BARRIER },
{ IROp::MemoryCheck, "MemoryCheck", "IGC", IRFLAG_BARRIER },

Expand Down Expand Up @@ -306,6 +308,16 @@ void DisassembleParam(char *buf, int bufSize, u8 param, char type, u32 constant)
case 's':
snprintf(buf, bufSize, "%c%c%c%c", xyzw[param & 3], xyzw[(param >> 2) & 3], xyzw[(param >> 4) & 3], xyzw[(param >> 6) & 3]);
break;
case 'r':
{
const ReplacementTableEntry *entry = GetReplacementFunc(constant);
if (entry) {
snprintf(buf, bufSize, "%s", entry->name);
} else {
snprintf(buf, bufSize, "(unkn. repl %d)", constant);
}
break;
}
case '_':
case '\0':
buf[0] = 0;
Expand Down
1 change: 0 additions & 1 deletion Core/MIPS/IR/IRJit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ void IRBlockCache::ComputeStats(BlockCacheStats &bcStats) const {
bcStats.maxBloatBlock = origAddr;
}
totalBloat += bloat;
bcStats.bloatMap[bloat] = origAddr;
}
bcStats.numBlocks = (int)blocks_.size();
bcStats.minBloat = minBloat;
Expand Down
1 change: 0 additions & 1 deletion Core/MIPS/IR/IRNativeCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,6 @@ void IRNativeBlockCacheDebugInterface::ComputeStats(BlockCacheStats &bcStats) co
bcStats.maxBloatBlock = origAddr;
}
totalBloat += bloat;
bcStats.bloatMap[(float)bloat] = origAddr;
}
bcStats.numBlocks = numBlocks;
bcStats.minBloat = (float)minBloat;
Expand Down
2 changes: 0 additions & 2 deletions Core/MIPS/JitCommon/JitBlockCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,6 @@ void JitBlockCache::ComputeStats(BlockCacheStats &bcStats) const {
bcStats.maxBloatBlock = b->originalAddress;
}
totalBloat += bloat;
bcStats.bloatMap[(float)bloat] = b->originalAddress;
}
bcStats.numBlocks = num_blocks_;
bcStats.minBloat = (float)minBloat;
Expand Down Expand Up @@ -710,6 +709,5 @@ JitBlockDebugInfo JitBlockCache::GetBlockDebugInfo(int blockNum) const {
#elif PPSSPP_ARCH(RISCV64)
debugInfo.targetDisasm = DisassembleRV64(block->normalEntry, block->codeSize);
#endif

return debugInfo;
}
1 change: 0 additions & 1 deletion Core/MIPS/JitCommon/JitBlockCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ struct BlockCacheStats {
u32 minBloatBlock;
float maxBloat;
u32 maxBloatBlock;
std::map<float, u32> bloatMap;
};

enum class DestroyType {
Expand Down
2 changes: 1 addition & 1 deletion Core/MIPS/MIPSTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static const MIPSInstruction tableSpecial[64] = // 000000 ..... ..... ..... ....
INSTR("jalr", JITFUNC(Comp_JumpReg), Dis_JumpRegType, Int_JumpRegType, IS_JUMP|IN_RS|OUT_RD|DELAYSLOT),
INSTR("movz", JITFUNC(Comp_RType3), Dis_RType3, Int_RType3, OUT_RD|IN_RS|IN_RT|IS_CONDMOVE|CONDTYPE_EQ),
INSTR("movn", JITFUNC(Comp_RType3), Dis_RType3, Int_RType3, OUT_RD|IN_RS|IN_RT|IS_CONDMOVE|CONDTYPE_NE),
INSTR("syscall", JITFUNC(Comp_Syscall), Dis_Syscall, Int_Syscall, IN_MEM|IN_OTHER|OUT_MEM|OUT_OTHER),
INSTR("syscall", JITFUNC(Comp_Syscall), Dis_Syscall, Int_Syscall, IN_MEM|IN_OTHER|OUT_MEM|OUT_OTHER|IS_SYSCALL),
INSTR("break", JITFUNC(Comp_Break), Dis_Generic, Int_Break, 0),
INVALID,
INSTR("sync", JITFUNC(Comp_DoNothing), Dis_Generic, Int_Sync, 0),
Expand Down
2 changes: 2 additions & 0 deletions Core/MIPS/MIPSTables.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@

#define OUT_VD 0x100000000000ULL

#define IS_SYSCALL 0x200000000000ULL

#ifndef CDECL
#define CDECL
#endif
Expand Down
102 changes: 75 additions & 27 deletions UI/JitCompareScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,34 @@
#include "Core/MIPS/JitCommon/JitState.h"

JitCompareScreen::JitCompareScreen() : UIDialogScreenWithBackground() {
JitBlockCacheDebugInterface *blockCacheDebug = MIPSComp::jit->GetBlockCacheDebugInterface();
// The only defaults that make sense.
if (blockCacheDebug->SupportsProfiling()) {
listSort_ = ListSort::TIME_SPENT;
} else {
listSort_ = ListSort::BLOCK_LENGTH_DESC;
}
FillBlockList();
}

void JitCompareScreen::Flip() {
using namespace UI;
// If we add more, let's convert to a for loop.
switch (viewMode_) {
case ViewMode::DISASM:
comparisonView_->SetVisibility(V_VISIBLE);
blockListView_->SetVisibility(V_GONE);
statsView_->SetVisibility(V_GONE);
break;
case ViewMode::BLOCK_LIST:
comparisonView_->SetVisibility(V_GONE);
blockListView_->SetVisibility(V_VISIBLE);
statsView_->SetVisibility(V_GONE);
break;
case ViewMode::STATS:
comparisonView_->SetVisibility(V_GONE);
blockListView_->SetVisibility(V_GONE);
statsView_->SetVisibility(V_VISIBLE);
break;
}
}
Expand Down Expand Up @@ -123,6 +138,13 @@ void JitCompareScreen::CreateViews() {
ScrollView *blockScroll = blockListView_->Add(new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0f)));
blockListContainer_ = blockScroll->Add(new LinearLayout(ORIENT_VERTICAL));

statsView_ = root_->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0f)));
statsView_->SetVisibility(V_GONE);

LinearLayout *statsTopBar = statsView_->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
ScrollView *statsScroll = statsView_->Add(new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0f)));
statsContainer_ = statsScroll->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));

// leftColumn->Add(new Choice(dev->T("By Address")))->OnClick.Handle(this, &JitCompareScreen::OnSelectBlock);
leftColumn->Add(new Choice(dev->T("All")))->OnClick.Add([=](UI::EventParams &e) {
listType_ = ListType::ALL_BLOCKS;
Expand Down Expand Up @@ -151,6 +173,9 @@ void JitCompareScreen::CreateViews() {
void JitCompareScreen::FillBlockList() {
JitBlockCacheDebugInterface *blockCacheDebug = MIPSComp::jit->GetBlockCacheDebugInterface();
blockList_.clear();
int64_t sumTotalNanos = 0;
int64_t sumExecutions = 0;
bool profiling = blockCacheDebug->SupportsProfiling();
for (int i = 0; i < blockCacheDebug->GetNumBlocks(); i++) {
if (!blockCacheDebug->IsValidBlock(i)) {
continue;
Expand All @@ -163,12 +188,15 @@ void JitCompareScreen::FillBlockList() {
case ListType::FPU_BLOCKS:
case ListType::VFPU_BLOCKS:
{
const int flags = listType_ == ListType::FPU_BLOCKS ? IS_FPU : IS_VFPU;
const uint64_t flags = listType_ == ListType::FPU_BLOCKS ? IS_FPU : IS_VFPU;
// const uint64_t antiFlags = IS_SYSCALL;
const uint64_t antiFlags = 0;
JitBlockMeta meta = blockCacheDebug->GetBlockMeta(i);
if (meta.valid) {
for (u32 addr = meta.addr; addr < meta.addr + meta.sizeInBytes; addr += 4) {
MIPSOpcode opcode = Memory::Read_Instruction(addr);
if (MIPSGetInfo(opcode) & flags) {
MIPSInfo info = MIPSGetInfo(opcode);
if ((info & flags) && !(info & antiFlags)) {
blockList_.push_back(i);
break;
}
Expand All @@ -178,8 +206,17 @@ void JitCompareScreen::FillBlockList() {
default:
break;
}

if (profiling) {
JitBlockProfileStats stats = blockCacheDebug->GetBlockProfileStats(i);
sumTotalNanos += stats.totalNanos;
sumExecutions += stats.executions;
}
}

sumTotalNanos_ = sumTotalNanos;
sumExecutions_ = sumExecutions;

if (listSort_ == ListSort::BLOCK_NUM) {
// Already sorted, effectively.
return;
Expand Down Expand Up @@ -273,26 +310,55 @@ void JitCompareScreen::UpdateDisasm() {

int numMips = leftDisasm_->GetNumSubviews();
int numHost = rightDisasm_->GetNumSubviews();
snprintf(temp, sizeof(temp), "%d to %d : %d%%", numMips, numHost, 100 * numHost / numMips);
double bloat = 100.0 * numHost / numMips;
if (blockCacheDebug->SupportsProfiling()) {
JitBlockProfileStats stats = blockCacheDebug->GetBlockProfileStats(blockNum);
int execs = (int)stats.executions;
double us = (double)stats.totalNanos / 1000000.0;
double percentage = 100.0 * (double)stats.totalNanos / (double)sumTotalNanos_;
snprintf(temp, sizeof(temp), "%d runs, %0.2f ms, %0.2f%%, bloat: %0.1f%%", execs, us, percentage, bloat);
} else {
snprintf(temp, sizeof(temp), "bloat: %0.1f%%", bloat);
}
blockStats_->SetText(temp);
} else {
} else if (viewMode_ == ViewMode::BLOCK_LIST) {
blockListContainer_->Clear();
bool profiling = blockCacheDebug->SupportsProfiling();
for (int i = 0; i < std::min(100, (int)blockList_.size()); i++) {
int blockNum = blockList_[i];
JitBlockMeta meta = blockCacheDebug->GetBlockMeta(blockNum);
char temp[512], small[512];
if (blockCacheDebug->SupportsProfiling()) {
if (profiling) {
JitBlockProfileStats stats = blockCacheDebug->GetBlockProfileStats(blockNum);
int execs = (int)stats.executions;
double us = (double)stats.totalNanos / 1000.0;
snprintf(temp, sizeof(temp), "%08x: %d instrs (%d exec, %0.2f us)", meta.addr, meta.sizeInBytes / 4, execs, us);
double us = (double)stats.totalNanos / 1000000.0;
double percentage = 100.0 * (double)stats.totalNanos / (double)sumTotalNanos_;
snprintf(temp, sizeof(temp), "%08x: %d instrs (%d runs, %0.2f ms, %0.2f%%)", meta.addr, meta.sizeInBytes / 4, execs, us, percentage);
} else {
snprintf(temp, sizeof(temp), "%08x: %d instrs", meta.addr, meta.sizeInBytes / 4);
}
snprintf(small, sizeof(small), "Small text");
Choice *blockChoice = blockListContainer_->Add(new Choice(temp, small));
blockChoice->OnClick.Handle(this, &JitCompareScreen::OnBlockClick);
}
} else { // viewMode_ == ViewMode::STATS
statsContainer_->Clear();

BlockCacheStats bcStats;
blockCacheDebug->ComputeStats(bcStats);

char stats[1024];
snprintf(stats, sizeof(stats),
"Num blocks: %d\n"
"Average Bloat: %0.2f%%\n"
"Min Bloat: %0.2f%% (%08x)\n"
"Max Bloat: %0.2f%% (%08x)\n",
blockCacheDebug->GetNumBlocks(),
100.0 * bcStats.avgBloat,
100.0 * bcStats.minBloat, bcStats.minBloatBlock,
100.0 * bcStats.maxBloat, bcStats.maxBloatBlock);

statsContainer_->Add(new TextView(stats));
}
}

Expand Down Expand Up @@ -332,26 +398,8 @@ UI::EventReturn JitCompareScreen::OnShowStats(UI::EventParams &e) {
return UI::EVENT_DONE;
}

JitBlockCacheDebugInterface *blockCache = MIPSComp::jit->GetBlockCacheDebugInterface();
if (!blockCache)
return UI::EVENT_DONE;

BlockCacheStats bcStats;
blockCache->ComputeStats(bcStats);
NOTICE_LOG(JIT, "Num blocks: %i", bcStats.numBlocks);
NOTICE_LOG(JIT, "Average Bloat: %0.2f%%", 100 * bcStats.avgBloat);
NOTICE_LOG(JIT, "Min Bloat: %0.2f%% (%08x)", 100 * bcStats.minBloat, bcStats.minBloatBlock);
NOTICE_LOG(JIT, "Max Bloat: %0.2f%% (%08x)", 100 * bcStats.maxBloat, bcStats.maxBloatBlock);

int ctr = 0, sz = (int)bcStats.bloatMap.size();
for (auto iter : bcStats.bloatMap) {
if (ctr < 10 || ctr > sz - 10) {
NOTICE_LOG(JIT, "%08x: %f", iter.second, iter.first);
} else if (ctr == 10) {
NOTICE_LOG(JIT, "...");
}
ctr++;
}
viewMode_ = ViewMode::STATS;
UpdateDisasm();
return UI::EVENT_DONE;
}

Expand Down
14 changes: 10 additions & 4 deletions UI/JitCompareScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class JitCompareScreen : public UIDialogScreenWithBackground {
void FillBlockList();

UI::LinearLayout *comparisonView_;
UI::LinearLayout *leftDisasm_;
UI::LinearLayout *rightDisasm_;

UI::LinearLayout *blockListView_;
UI::LinearLayout *blockListContainer_;

UI::LinearLayout *statsView_;
UI::LinearLayout *statsContainer_;

UI::EventReturn OnSelectBlock(UI::EventParams &e);
UI::EventReturn OnBlockAddress(UI::EventParams &e);
UI::EventReturn OnAddressChange(UI::EventParams &e);
Expand All @@ -30,6 +36,7 @@ class JitCompareScreen : public UIDialogScreenWithBackground {
enum class ViewMode {
BLOCK_LIST,
DISASM,
STATS,
};
enum class ListType {
ALL_BLOCKS,
Expand All @@ -46,17 +53,16 @@ class JitCompareScreen : public UIDialogScreenWithBackground {
};
ViewMode viewMode_ = ViewMode::BLOCK_LIST;
ListType listType_ = ListType::ALL_BLOCKS;
ListSort listSort_ = ListSort::BLOCK_LENGTH_DESC;
ListSort listSort_ = ListSort::TIME_SPENT;

int currentBlock_ = -1; // For DISASM mode
int64_t sumTotalNanos_ = 0;
int64_t sumExecutions_ = 0;
std::vector<int> blockList_; // for BLOCK_LIST mode

UI::TextView *blockName_;
UI::TextEdit *blockAddr_;
UI::TextView *blockStats_;

UI::LinearLayout *leftDisasm_;
UI::LinearLayout *rightDisasm_;
};

class AddressPromptScreen : public PopupScreen {
Expand Down
Loading