CPU/Recompiler: Write exception exits to far code buffer

Keeps the hot path nice and clean.
This commit is contained in:
Connor McLaughlin
2019-11-22 17:57:02 +10:00
parent 7b0978119b
commit 11966e4caf
7 changed files with 345 additions and 286 deletions

View File

@ -9,8 +9,11 @@ Log_SetChannel(CPU::CodeCache);
namespace CPU {
bool USE_CODE_CACHE = true;
bool USE_RECOMPILER = true;
bool USE_CODE_CACHE = false;
bool USE_RECOMPILER = false;
static constexpr size_t RECOMPILER_CODE_CACHE_SIZE = 32 * 1024 * 1024;
static constexpr size_t RECOMPILER_FAR_CODE_CACHE_SIZE = 32 * 1024 * 1024;
CodeCache::CodeCache() = default;
@ -22,7 +25,7 @@ void CodeCache::Initialize(System* system, Core* core, Bus* bus)
m_core = core;
m_bus = bus;
m_code_buffer = std::make_unique<JitCodeBuffer>();
m_code_buffer = std::make_unique<JitCodeBuffer>(RECOMPILER_CODE_CACHE_SIZE, RECOMPILER_FAR_CODE_CACHE_SIZE);
m_asm_functions = std::make_unique<Recompiler::ASMFunctions>();
m_asm_functions->Generate(m_code_buffer.get());
}
@ -50,7 +53,7 @@ void CodeCache::Execute()
#if 0
const u32 tick = m_system->GetGlobalTickCounter() + m_core->GetPendingTicks();
if (tick == 8950812)
if (tick == 58672386)
__debugbreak();
#endif
@ -184,6 +187,11 @@ bool CodeCache::CompileBlock(CodeBlock* block)
bool is_branch_delay_slot = false;
bool is_load_delay_slot = false;
#if 0
if (pc == 0x0005aa90)
__debugbreak();
#endif
for (;;)
{
CodeBlockInstruction cbi = {};
@ -247,7 +255,10 @@ bool CodeCache::CompileBlock(CodeBlock* block)
if (USE_RECOMPILER)
{
// Ensure we're not going to run out of space while compiling this block.
if (m_code_buffer->GetFreeCodeSpace() < (block->instructions.size() * Recompiler::MAX_HOST_BYTES_PER_INSTRUCTION))
if (m_code_buffer->GetFreeCodeSpace() <
(block->instructions.size() * Recompiler::MAX_NEAR_HOST_BYTES_PER_INSTRUCTION) ||
m_code_buffer->GetFreeFarCodeSpace() <
(block->instructions.size() * Recompiler::MAX_FAR_HOST_BYTES_PER_INSTRUCTION))
{
Log_WarningPrintf("Out of code space, flushing all blocks.");
Reset();

View File

@ -7,7 +7,8 @@ namespace CPU::Recompiler {
CodeGenerator::CodeGenerator(Core* cpu, JitCodeBuffer* code_buffer, const ASMFunctions& asm_functions)
: m_cpu(cpu), m_code_buffer(code_buffer), m_asm_functions(asm_functions), m_register_cache(*this),
m_emit(code_buffer->GetFreeCodeSpace(), code_buffer->GetFreeCodePointer())
m_near_emitter(code_buffer->GetFreeCodeSpace(), code_buffer->GetFreeCodePointer()),
m_far_emitter(code_buffer->GetFreeFarCodeSpace(), code_buffer->GetFreeFarCodePointer()), m_emit(&m_near_emitter)
{
InitHostRegs();
}
@ -608,7 +609,7 @@ void CodeGenerator::BlockPrologue()
void CodeGenerator::BlockEpilogue()
{
#if defined(_DEBUG) && defined(Y_CPU_X64)
m_emit.nop();
m_emit->nop();
#endif
m_register_cache.FlushAllGuestRegisters(true, true);
@ -632,7 +633,7 @@ void CodeGenerator::InstructionPrologue(const CodeBlockInstruction& cbi, TickCou
bool force_sync /* = false */)
{
#if defined(_DEBUG) && defined(Y_CPU_X64)
m_emit.nop();
m_emit->nop();
#endif
// reset dirty flags

View File

@ -33,9 +33,6 @@ public:
static const char* GetHostRegName(HostReg reg, RegSize size = HostPointerSize);
static void AlignCodeBuffer(JitCodeBuffer* code_buffer);
RegisterCache& GetRegisterCache() { return m_register_cache; }
CodeEmitter& GetCodeEmitter() { return m_emit; }
bool CompileBlock(const CodeBlock* block, CodeBlock::HostCodePointer* out_host_code, u32* out_host_code_size);
//////////////////////////////////////////////////////////////////////////
@ -146,14 +143,18 @@ private:
Value ConvertValueSize(const Value& value, RegSize size, bool sign_extend);
void ConvertValueSizeInPlace(Value* value, RegSize size, bool sign_extend);
void SwitchToFarCode();
void SwitchToNearCode();
void* GetCurrentNearCodePointer() const;
void* GetCurrentFarCodePointer() const;
//////////////////////////////////////////////////////////////////////////
// Code Generation Helpers
//////////////////////////////////////////////////////////////////////////
// branch target, memory address, etc
void BlockPrologue();
void BlockEpilogue();
void InstructionPrologue(const CodeBlockInstruction& cbi, TickCount cycles,
bool force_sync = false);
void InstructionPrologue(const CodeBlockInstruction& cbi, TickCount cycles, bool force_sync = false);
void InstructionEpilogue(const CodeBlockInstruction& cbi);
void SyncCurrentInstructionPC();
void SyncPC();
@ -182,7 +183,9 @@ private:
const CodeBlockInstruction* m_block_start = nullptr;
const CodeBlockInstruction* m_block_end = nullptr;
RegisterCache m_register_cache;
CodeEmitter m_emit;
CodeEmitter m_near_emitter;
CodeEmitter m_far_emitter;
CodeEmitter* m_emit;
u32 m_delayed_pc_add = 0;
TickCount m_delayed_cycles_add = 0;
@ -197,4 +200,4 @@ private:
bool m_next_load_delay_dirty = false;
};
} // namespace CPU_X86::Recompiler
} // namespace CPU::Recompiler

File diff suppressed because it is too large Load Diff

View File

@ -35,7 +35,8 @@ constexpr HostReg HostReg_Invalid = static_cast<HostReg>(HostReg_Count);
constexpr RegSize HostPointerSize = RegSize_64;
// A reasonable "maximum" number of bytes per instruction.
constexpr u32 MAX_HOST_BYTES_PER_INSTRUCTION = 128;
constexpr u32 MAX_NEAR_HOST_BYTES_PER_INSTRUCTION = 64;
constexpr u32 MAX_FAR_HOST_BYTES_PER_INSTRUCTION = 128;
// Are shifts implicitly masked to 0..31?
constexpr bool SHIFTS_ARE_IMPLICITLY_MASKED = true;