Skip to content

Commit

Permalink
jit: Use a hash for invalidate/clear all.
Browse files Browse the repository at this point in the history
This should make sceKernelICacheClearAll() more useful.
  • Loading branch information
unknownbrackets committed Dec 19, 2021
1 parent 293b5c0 commit 00f9707
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Core/MIPS/JitCommon/JitBlockCache.cpp
Expand Up @@ -19,7 +19,9 @@
#include <cstddef>
#include <algorithm>

#include "ext/xxhash.h"
#include "Common.h"
#include "Common/Profiler/Profiler.h"

#ifdef _WIN32
#include "Common/CommonWindows.h"
Expand Down Expand Up @@ -59,6 +61,15 @@ op_agent_t agent;

const u32 INVALID_EXIT = 0xFFFFFFFF;

static uint64_t HashJitBlock(const JitBlock &b) {
PROFILE_THIS_SCOPE("jithash");
if (JIT_USE_COMPILEDHASH) {
// Includes the emuhack (or emuhacks) in memory.
return XXH3_64bits(Memory::GetPointer(b.originalAddress), b.originalSize * 4);
}
return 0;
}

JitBlockCache::JitBlockCache(MIPSState *mipsState, CodeBlockCommon *codeBlock) :
codeBlock_(codeBlock), blocks_(nullptr), num_blocks_(0) {
}
Expand Down Expand Up @@ -234,6 +245,9 @@ void JitBlockCache::FinalizeBlock(int block_num, bool block_link) {
MIPSOpcode opcode = GetEmuHackOpForBlock(block_num);
Memory::Write_Opcode_JIT(b.originalAddress, opcode);

// Note that this hashes the emuhack too, which is intentional.
b.compiledHash = HashJitBlock(b);

AddBlockMap(block_num);

if (block_link) {
Expand Down Expand Up @@ -592,8 +606,15 @@ void JitBlockCache::InvalidateChangedBlocks() {
if (b.invalid || b.IsPureProxy())
continue;

const u32 emuhack = GetEmuHackOpForBlock(block_num).encoding;
if (Memory::ReadUnchecked_U32(b.originalAddress) != emuhack) {
bool changed = false;
if (JIT_USE_COMPILEDHASH) {
changed = b.compiledHash != HashJitBlock(b);
} else {
const u32 emuhack = GetEmuHackOpForBlock(block_num).encoding;
changed = Memory::ReadUnchecked_U32(b.originalAddress) != emuhack;
}

if (changed) {
DEBUG_LOG(JIT, "Invalidating changed block at %08x", b.originalAddress);
DestroyBlock(block_num, DestroyType::INVALIDATE);
}
Expand Down
3 changes: 3 additions & 0 deletions Core/MIPS/JitCommon/JitBlockCache.h
Expand Up @@ -17,6 +17,7 @@

#pragma once

#include <cstdint>
#include <map>
#include <unordered_map>
#include <vector>
Expand All @@ -33,6 +34,7 @@ const int MAX_JIT_BLOCK_EXITS = 2;
#else
const int MAX_JIT_BLOCK_EXITS = 8;
#endif
constexpr bool JIT_USE_COMPILEDHASH = true;

struct BlockCacheStats {
int numBlocks;
Expand Down Expand Up @@ -68,6 +70,7 @@ struct JitBlock {

u32 originalAddress;
MIPSOpcode originalFirstOpcode; //to be able to restore
uint64_t compiledHash;
u16 codeSize;
u16 originalSize;
u16 blockNum;
Expand Down

0 comments on commit 00f9707

Please sign in to comment.