CPU/CodeCache: Purge JitCodeBuffer

This commit is contained in:
Stenzek 2024-06-30 14:05:31 +10:00
parent be8fbafd71
commit 9b42ad3859
No known key found for this signature in database
12 changed files with 160 additions and 220 deletions

View file

@ -97,6 +97,7 @@ static BlockInstructionList s_block_instructions;
static void BacklinkBlocks(u32 pc, const void* dst); static void BacklinkBlocks(u32 pc, const void* dst);
static void UnlinkBlockExits(Block* block); static void UnlinkBlockExits(Block* block);
static void ResetCodeBuffer();
static void ClearASMFunctions(); static void ClearASMFunctions();
static void CompileASMFunctions(); static void CompileASMFunctions();
@ -146,7 +147,15 @@ __attribute__((aligned(HOST_PAGE_SIZE))) static u8 s_code_buffer_ptr[RECOMPILER_
static u8* s_code_buffer_ptr = nullptr; static u8* s_code_buffer_ptr = nullptr;
#endif #endif
static JitCodeBuffer s_code_buffer; static u8* s_code_ptr = nullptr;
static u8* s_free_code_ptr = nullptr;
static u32 s_code_size = 0;
static u32 s_code_used = 0;
static u8* s_far_code_ptr = nullptr;
static u8* s_free_far_code_ptr = nullptr;
static u32 s_far_code_size = 0;
static u32 s_far_code_used = 0;
#ifdef _DEBUG #ifdef _DEBUG
static u32 s_total_instructions_compiled = 0; static u32 s_total_instructions_compiled = 0;
@ -206,10 +215,9 @@ void CPU::CodeCache::Initialize()
{ {
Assert(s_blocks.empty()); Assert(s_blocks.empty());
// TODO: Reduce far code size when not using memory exceptions.
if (IsUsingAnyRecompiler()) if (IsUsingAnyRecompiler())
{ {
s_code_buffer.Reset(s_code_buffer_ptr, RECOMPILER_CODE_CACHE_SIZE, RECOMPILER_FAR_CODE_CACHE_SIZE); ResetCodeBuffer();
CompileASMFunctions(); CompileASMFunctions();
ResetCodeLUT(); ResetCodeLUT();
} }
@ -234,7 +242,7 @@ void CPU::CodeCache::Reset()
if (IsUsingAnyRecompiler()) if (IsUsingAnyRecompiler())
{ {
ClearASMFunctions(); ClearASMFunctions();
s_code_buffer.Reset(s_code_buffer_ptr, RECOMPILER_CODE_CACHE_SIZE, RECOMPILER_FAR_CODE_CACHE_SIZE); ResetCodeBuffer();
CompileASMFunctions(); CompileASMFunctions();
ResetCodeLUT(); ResetCodeLUT();
} }
@ -1331,8 +1339,8 @@ void CPU::CodeCache::CompileOrRevalidateBlock(u32 start_pc)
// Ensure we're not going to run out of space while compiling this block. // Ensure we're not going to run out of space while compiling this block.
// We could definitely do better here... TODO: far code is no longer needed for newrec // We could definitely do better here... TODO: far code is no longer needed for newrec
const u32 block_size = static_cast<u32>(s_block_instructions.size()); const u32 block_size = static_cast<u32>(s_block_instructions.size());
if (s_code_buffer.GetFreeCodeSpace() < (block_size * Recompiler::MAX_NEAR_HOST_BYTES_PER_INSTRUCTION) || if (GetFreeCodeSpace() < (block_size * Recompiler::MAX_NEAR_HOST_BYTES_PER_INSTRUCTION) ||
s_code_buffer.GetFreeFarCodeSpace() < (block_size * Recompiler::MAX_FAR_HOST_BYTES_PER_INSTRUCTION)) GetFreeFarCodeSpace() < (block_size * Recompiler::MAX_FAR_HOST_BYTES_PER_INSTRUCTION))
{ {
ERROR_LOG("Out of code space while compiling {:08X}. Resetting code cache.", start_pc); ERROR_LOG("Out of code space while compiling {:08X}. Resetting code cache.", start_pc);
CodeCache::Reset(); CodeCache::Reset();
@ -1420,9 +1428,86 @@ void CPU::CodeCache::UnlinkBlockExits(Block* block)
block->num_exit_links = 0; block->num_exit_links = 0;
} }
JitCodeBuffer& CPU::CodeCache::GetCodeBuffer() void CPU::CodeCache::ResetCodeBuffer()
{ {
return s_code_buffer; s_code_ptr = static_cast<u8*>(s_code_buffer_ptr);
s_free_code_ptr = s_code_ptr;
s_code_size = RECOMPILER_CODE_CACHE_SIZE - RECOMPILER_FAR_CODE_CACHE_SIZE;
s_code_used = 0;
s_far_code_size = RECOMPILER_FAR_CODE_CACHE_SIZE;
s_far_code_ptr = (s_far_code_size > 0) ? (static_cast<u8*>(s_code_ptr) + s_code_size) : nullptr;
s_free_far_code_ptr = s_far_code_ptr;
s_far_code_used = 0;
MemMap::BeginCodeWrite();
std::memset(s_code_ptr, 0, RECOMPILER_CODE_CACHE_SIZE);
MemMap::FlushInstructionCache(s_code_ptr, RECOMPILER_CODE_CACHE_SIZE);
MemMap::EndCodeWrite();
}
u8* CPU::CodeCache::GetFreeCodePointer()
{
return s_free_code_ptr;
}
u32 CPU::CodeCache::GetFreeCodeSpace()
{
return s_code_size - s_code_used;
}
void CPU::CodeCache::CommitCode(u32 length)
{
if (length == 0) [[unlikely]]
return;
MemMap::FlushInstructionCache(s_free_code_ptr, length);
Assert(length <= (s_code_size - s_code_used));
s_free_code_ptr += length;
s_code_used += length;
}
u8* CPU::CodeCache::GetFreeFarCodePointer()
{
return s_free_far_code_ptr;
}
u32 CPU::CodeCache::GetFreeFarCodeSpace()
{
return s_far_code_size - s_far_code_used;
}
void CPU::CodeCache::CommitFarCode(u32 length)
{
if (length == 0) [[unlikely]]
return;
MemMap::FlushInstructionCache(s_free_far_code_ptr, length);
Assert(length <= (s_far_code_size - s_far_code_used));
s_free_far_code_ptr += length;
s_far_code_used += length;
}
void CPU::CodeCache::AlignCode(u32 alignment)
{
#if defined(CPU_ARCH_X64)
constexpr u8 padding_value = 0xcc; // int3
#else
constexpr u8 padding_value = 0x00;
#endif
DebugAssert(Common::IsPow2(alignment));
const u32 num_padding_bytes =
std::min(static_cast<u32>(Common::AlignUpPow2(reinterpret_cast<uintptr_t>(s_free_code_ptr), alignment) -
reinterpret_cast<uintptr_t>(s_free_code_ptr)),
GetFreeCodeSpace());
std::memset(s_free_code_ptr, padding_value, num_padding_bytes);
s_free_code_ptr += num_padding_bytes;
s_code_used += num_padding_bytes;
} }
const void* CPU::CodeCache::GetInterpretUncachedBlockFunction() const void* CPU::CodeCache::GetInterpretUncachedBlockFunction()
@ -1460,13 +1545,13 @@ void CPU::CodeCache::CompileASMFunctions()
{ {
MemMap::BeginCodeWrite(); MemMap::BeginCodeWrite();
const u32 asm_size = EmitASMFunctions(s_code_buffer.GetFreeCodePointer(), s_code_buffer.GetFreeCodeSpace()); const u32 asm_size = EmitASMFunctions(GetFreeCodePointer(), GetFreeCodeSpace());
#ifdef ENABLE_RECOMPILER_PROFILING #ifdef ENABLE_RECOMPILER_PROFILING
MIPSPerfScope.Register(s_code_buffer.GetFreeCodePointer(), asm_size, "ASMFunctions"); MIPSPerfScope.Register(GetFreeCodePointer(), asm_size, "ASMFunctions");
#endif #endif
s_code_buffer.CommitCode(asm_size); CommitCode(asm_size);
MemMap::EndCodeWrite(); MemMap::EndCodeWrite();
} }
@ -1479,7 +1564,7 @@ bool CPU::CodeCache::CompileBlock(Block* block)
#ifdef ENABLE_RECOMPILER #ifdef ENABLE_RECOMPILER
if (g_settings.cpu_execution_mode == CPUExecutionMode::Recompiler) if (g_settings.cpu_execution_mode == CPUExecutionMode::Recompiler)
{ {
Recompiler::CodeGenerator codegen(&s_code_buffer); Recompiler::CodeGenerator codegen;
host_code = codegen.CompileBlock(block, &host_code_size, &host_far_code_size); host_code = codegen.CompileBlock(block, &host_code_size, &host_far_code_size);
} }
#endif #endif
@ -1503,12 +1588,13 @@ bool CPU::CodeCache::CompileBlock(Block* block)
s_total_instructions_compiled += block->size; s_total_instructions_compiled += block->size;
s_total_host_instructions_emitted += host_instructions; s_total_host_instructions_emitted += host_instructions;
Log_ProfileFmt("0x{:08X}: {}/{}b for {}b ({}i), blowup: {:.2f}x, cache: {:.2f}%/{:.2f}%, ipi: {:.2f}/{:.2f}", DEV_LOG("0x{:08X}: {}/{}b for {}b ({}i), blowup: {:.2f}x, cache: {:.2f}%/{:.2f}%, ipi: {:.2f}/{:.2f}", block->pc,
block->pc, host_code_size, host_far_code_size, block->size * 4, block->size, host_code_size, host_far_code_size, block->size * 4, block->size,
static_cast<float>(host_code_size) / static_cast<float>(block->size * 4), s_code_buffer.GetUsedPct(), static_cast<float>(host_code_size) / static_cast<float>(block->size * 4),
s_code_buffer.GetFarUsedPct(), static_cast<float>(host_instructions) / static_cast<float>(block->size), (static_cast<float>(s_code_used) / static_cast<float>(s_code_size)) * 100.0f,
static_cast<float>(s_total_host_instructions_emitted) / (static_cast<float>(s_far_code_used) / static_cast<float>(s_far_code_size)) * 100.0f,
static_cast<float>(s_total_instructions_compiled)); static_cast<float>(host_instructions) / static_cast<float>(block->size),
static_cast<float>(s_total_host_instructions_emitted) / static_cast<float>(s_total_instructions_compiled));
#endif #endif
#if 0 #if 0

View file

@ -10,7 +10,6 @@
#include "cpu_core_private.h" #include "cpu_core_private.h"
#include "cpu_types.h" #include "cpu_types.h"
#include "util/jit_code_buffer.h"
#include "util/page_fault_handler.h" #include "util/page_fault_handler.h"
#include <array> #include <array>
@ -234,7 +233,20 @@ void LogCurrentState();
#define ENABLE_HOST_DISASSEMBLY 1 #define ENABLE_HOST_DISASSEMBLY 1
#endif #endif
JitCodeBuffer& GetCodeBuffer(); /// Access to normal code allocator.
u8* GetFreeCodePointer();
u32 GetFreeCodeSpace();
void CommitCode(u32 length);
/// Access to far code allocator.
u8* GetFreeFarCodePointer();
u32 GetFreeFarCodeSpace();
void CommitFarCode(u32 length);
/// Adjusts the free code pointer to the specified alignment, padding with bytes.
/// Assumes alignment is a power-of-two.
void AlignCode(u32 alignment);
const void* GetInterpretUncachedBlockFunction(); const void* GetInterpretUncachedBlockFunction();
void CompileOrRevalidateBlock(u32 start_pc); void CompileOrRevalidateBlock(u32 start_pc);

View file

@ -99,9 +99,8 @@ void CPU::NewRec::Compiler::BeginBlock()
const void* CPU::NewRec::Compiler::CompileBlock(CodeCache::Block* block, u32* host_code_size, u32* host_far_code_size) const void* CPU::NewRec::Compiler::CompileBlock(CodeCache::Block* block, u32* host_code_size, u32* host_far_code_size)
{ {
JitCodeBuffer& buffer = CodeCache::GetCodeBuffer(); Reset(block, CPU::CodeCache::GetFreeCodePointer(), CPU::CodeCache::GetFreeCodeSpace(),
Reset(block, buffer.GetFreeCodePointer(), buffer.GetFreeCodeSpace(), buffer.GetFreeFarCodePointer(), CPU::CodeCache::GetFreeFarCodePointer(), CPU::CodeCache::GetFreeFarCodeSpace());
buffer.GetFreeFarCodeSpace());
DEBUG_LOG("Block range: {:08X} -> {:08X}", block->pc, block->pc + block->size * 4); DEBUG_LOG("Block range: {:08X} -> {:08X}", block->pc, block->pc + block->size * 4);
@ -141,8 +140,8 @@ const void* CPU::NewRec::Compiler::CompileBlock(CodeCache::Block* block, u32* ho
const void* code = EndCompile(&code_size, &far_code_size); const void* code = EndCompile(&code_size, &far_code_size);
*host_code_size = code_size; *host_code_size = code_size;
*host_far_code_size = far_code_size; *host_far_code_size = far_code_size;
buffer.CommitCode(code_size); CPU::CodeCache::CommitCode(code_size);
buffer.CommitFarCode(far_code_size); CPU::CodeCache::CommitFarCode(far_code_size);
return code; return code;
} }
@ -2341,21 +2340,20 @@ void CPU::NewRec::BackpatchLoadStore(void* exception_pc, const CodeCache::Loadst
static_cast<TickCount>(static_cast<u32>(info.cycles)) - (info.is_load ? Bus::RAM_READ_TICKS : 0); static_cast<TickCount>(static_cast<u32>(info.cycles)) - (info.is_load ? Bus::RAM_READ_TICKS : 0);
const TickCount cycles_to_remove = static_cast<TickCount>(static_cast<u32>(info.cycles)); const TickCount cycles_to_remove = static_cast<TickCount>(static_cast<u32>(info.cycles));
JitCodeBuffer& buffer = CodeCache::GetCodeBuffer(); void* thunk_address = CPU::CodeCache::GetFreeFarCodePointer();
void* thunk_address = buffer.GetFreeFarCodePointer();
const u32 thunk_size = CompileLoadStoreThunk( const u32 thunk_size = CompileLoadStoreThunk(
thunk_address, buffer.GetFreeFarCodeSpace(), exception_pc, info.code_size, cycles_to_add, cycles_to_remove, thunk_address, CPU::CodeCache::GetFreeFarCodeSpace(), exception_pc, info.code_size, cycles_to_add, cycles_to_remove,
info.gpr_bitmask, info.address_register, info.data_register, info.AccessSize(), info.is_signed, info.is_load); info.gpr_bitmask, info.address_register, info.data_register, info.AccessSize(), info.is_signed, info.is_load);
#if 0 #if 0
Log_DebugPrint("**Backpatch Thunk**"); Log_DebugPrint("**Backpatch Thunk**");
CodeCache::DisassembleAndLogHostCode(thunk_address, thunk_size); CPU::CodeCache::DisassembleAndLogHostCode(thunk_address, thunk_size);
#endif #endif
// backpatch to a jump to the slowmem handler // backpatch to a jump to the slowmem handler
CodeCache::EmitJump(exception_pc, thunk_address, true); CPU::CodeCache::EmitJump(exception_pc, thunk_address, true);
buffer.CommitFarCode(thunk_size); CPU::CodeCache::CommitFarCode(thunk_size);
} }
void CPU::NewRec::Compiler::InitSpeculativeRegs() void CPU::NewRec::Compiler::InitSpeculativeRegs()

View file

@ -7,8 +7,6 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "util/jit_code_buffer.h"
#include "cpu_code_cache_private.h" #include "cpu_code_cache_private.h"
#include "cpu_recompiler_register_cache.h" #include "cpu_recompiler_register_cache.h"
#include "cpu_recompiler_thunks.h" #include "cpu_recompiler_thunks.h"
@ -49,11 +47,10 @@ public:
const CodeCache::InstructionInfo* info; const CodeCache::InstructionInfo* info;
}; };
CodeGenerator(JitCodeBuffer* code_buffer); CodeGenerator();
~CodeGenerator(); ~CodeGenerator();
static const char* GetHostRegName(HostReg reg, RegSize size = HostPointerSize); static const char* GetHostRegName(HostReg reg, RegSize size = HostPointerSize);
static void AlignCodeBuffer(JitCodeBuffer* code_buffer);
static void BackpatchLoadStore(void* host_pc, const CodeCache::LoadstoreBackpatchInfo& lbi); static void BackpatchLoadStore(void* host_pc, const CodeCache::LoadstoreBackpatchInfo& lbi);
@ -267,8 +264,6 @@ private:
bool Compile_cop0(Instruction instruction, const CodeCache::InstructionInfo& info); bool Compile_cop0(Instruction instruction, const CodeCache::InstructionInfo& info);
bool Compile_cop2(Instruction instruction, const CodeCache::InstructionInfo& info); bool Compile_cop2(Instruction instruction, const CodeCache::InstructionInfo& info);
JitCodeBuffer* m_code_buffer;
CodeCache::Block* m_block = nullptr; CodeCache::Block* m_block = nullptr;
CodeBlockInstruction m_block_start = {}; CodeBlockInstruction m_block_start = {};
CodeBlockInstruction m_block_end = {}; CodeBlockInstruction m_block_end = {};

View file

@ -358,12 +358,11 @@ static const a32::Register GetFastmemBasePtrReg()
return GetHostReg32(RMEMBASEPTR); return GetHostReg32(RMEMBASEPTR);
} }
CodeGenerator::CodeGenerator(JitCodeBuffer* code_buffer) CodeGenerator::CodeGenerator()
: m_code_buffer(code_buffer), m_register_cache(*this), : m_register_cache(*this), m_near_emitter(static_cast<vixl::byte*>(CPU::CodeCache::GetFreeCodePointer()),
m_near_emitter(static_cast<vixl::byte*>(code_buffer->GetFreeCodePointer()), code_buffer->GetFreeCodeSpace(), CPU::CodeCache::GetFreeCodeSpace(), a32::A32),
a32::A32), m_far_emitter(static_cast<vixl::byte*>(CPU::CodeCache::GetFreeFarCodePointer()),
m_far_emitter(static_cast<vixl::byte*>(code_buffer->GetFreeFarCodePointer()), code_buffer->GetFreeFarCodeSpace(), CPU::CodeCache::GetFreeFarCodeSpace(), a32::A32),
a32::A32),
m_emit(&m_near_emitter) m_emit(&m_near_emitter)
{ {
InitHostRegs(); InitHostRegs();
@ -387,11 +386,6 @@ const char* CodeGenerator::GetHostRegName(HostReg reg, RegSize size /*= HostPoin
} }
} }
void CodeGenerator::AlignCodeBuffer(JitCodeBuffer* code_buffer)
{
code_buffer->Align(16, 0x90);
}
void CodeGenerator::InitHostRegs() void CodeGenerator::InitHostRegs()
{ {
// allocate nonvolatile before volatile // allocate nonvolatile before volatile
@ -414,17 +408,17 @@ void CodeGenerator::SwitchToNearCode()
void* CodeGenerator::GetStartNearCodePointer() const void* CodeGenerator::GetStartNearCodePointer() const
{ {
return static_cast<u8*>(m_code_buffer->GetFreeCodePointer()); return static_cast<u8*>(CPU::CodeCache::GetFreeCodePointer());
} }
void* CodeGenerator::GetCurrentNearCodePointer() const void* CodeGenerator::GetCurrentNearCodePointer() const
{ {
return static_cast<u8*>(m_code_buffer->GetFreeCodePointer()) + m_near_emitter.GetCursorOffset(); return static_cast<u8*>(CPU::CodeCache::GetFreeCodePointer()) + m_near_emitter.GetCursorOffset();
} }
void* CodeGenerator::GetCurrentFarCodePointer() const void* CodeGenerator::GetCurrentFarCodePointer() const
{ {
return static_cast<u8*>(m_code_buffer->GetFreeFarCodePointer()) + m_far_emitter.GetCursorOffset(); return static_cast<u8*>(CPU::CodeCache::GetFreeFarCodePointer()) + m_far_emitter.GetCursorOffset();
} }
Value CodeGenerator::GetValueInHostRegister(const Value& value, bool allow_zero_register /* = true */) Value CodeGenerator::GetValueInHostRegister(const Value& value, bool allow_zero_register /* = true */)
@ -517,17 +511,17 @@ const void* CodeGenerator::FinalizeBlock(u32* out_host_code_size, u32* out_host_
m_near_emitter.FinalizeCode(); m_near_emitter.FinalizeCode();
m_far_emitter.FinalizeCode(); m_far_emitter.FinalizeCode();
const void* code = m_code_buffer->GetFreeCodePointer(); const void* code = CPU::CodeCache::GetFreeCodePointer();
*out_host_code_size = static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated()); *out_host_code_size = static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated());
*out_host_far_code_size = static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated()); *out_host_far_code_size = static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated());
m_code_buffer->CommitCode(static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated())); CPU::CodeCache::CommitCode(static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated()));
m_code_buffer->CommitFarCode(static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated())); CPU::CodeCache::CommitFarCode(static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated()));
m_near_emitter = CodeEmitter(static_cast<vixl::byte*>(m_code_buffer->GetFreeCodePointer()), m_near_emitter = CodeEmitter(static_cast<vixl::byte*>(CPU::CodeCache::GetFreeCodePointer()),
m_code_buffer->GetFreeCodeSpace(), a32::A32); CPU::CodeCache::GetFreeCodeSpace(), a32::A32);
m_far_emitter = CodeEmitter(static_cast<vixl::byte*>(m_code_buffer->GetFreeFarCodePointer()), m_far_emitter = CodeEmitter(static_cast<vixl::byte*>(CPU::CodeCache::GetFreeFarCodePointer()),
m_code_buffer->GetFreeFarCodeSpace(), a32::A32); CPU::CodeCache::GetFreeFarCodeSpace(), a32::A32);
return code; return code;
} }

View file

@ -482,12 +482,11 @@ static const a64::XRegister GetFastmemBasePtrReg()
return GetHostReg64(RMEMBASEPTR); return GetHostReg64(RMEMBASEPTR);
} }
CodeGenerator::CodeGenerator(JitCodeBuffer* code_buffer) CodeGenerator::CodeGenerator()
: m_code_buffer(code_buffer), m_register_cache(*this), : m_register_cache(*this), m_near_emitter(static_cast<vixl::byte*>(CPU::CodeCache::GetFreeCodePointer()),
m_near_emitter(static_cast<vixl::byte*>(code_buffer->GetFreeCodePointer()), code_buffer->GetFreeCodeSpace(), CPU::CodeCache::GetFreeCodeSpace(), a64::PositionDependentCode),
a64::PositionDependentCode), m_far_emitter(static_cast<vixl::byte*>(CPU::CodeCache::GetFreeFarCodePointer()),
m_far_emitter(static_cast<vixl::byte*>(code_buffer->GetFreeFarCodePointer()), code_buffer->GetFreeFarCodeSpace(), CPU::CodeCache::GetFreeFarCodeSpace(), a64::PositionDependentCode),
a64::PositionDependentCode),
m_emit(&m_near_emitter) m_emit(&m_near_emitter)
{ {
// remove the temporaries from vixl's list to prevent it from using them. // remove the temporaries from vixl's list to prevent it from using them.
@ -523,11 +522,6 @@ const char* CodeGenerator::GetHostRegName(HostReg reg, RegSize size /*= HostPoin
} }
} }
void CodeGenerator::AlignCodeBuffer(JitCodeBuffer* code_buffer)
{
code_buffer->Align(16, 0x90);
}
void CodeGenerator::InitHostRegs() void CodeGenerator::InitHostRegs()
{ {
// TODO: function calls mess up the parameter registers if we use them.. fix it // TODO: function calls mess up the parameter registers if we use them.. fix it
@ -551,17 +545,17 @@ void CodeGenerator::SwitchToNearCode()
void* CodeGenerator::GetStartNearCodePointer() const void* CodeGenerator::GetStartNearCodePointer() const
{ {
return static_cast<u8*>(m_code_buffer->GetFreeCodePointer()); return static_cast<u8*>(CPU::CodeCache::GetFreeCodePointer());
} }
void* CodeGenerator::GetCurrentNearCodePointer() const void* CodeGenerator::GetCurrentNearCodePointer() const
{ {
return static_cast<u8*>(m_code_buffer->GetFreeCodePointer()) + m_near_emitter.GetCursorOffset(); return static_cast<u8*>(CPU::CodeCache::GetFreeCodePointer()) + m_near_emitter.GetCursorOffset();
} }
void* CodeGenerator::GetCurrentFarCodePointer() const void* CodeGenerator::GetCurrentFarCodePointer() const
{ {
return static_cast<u8*>(m_code_buffer->GetFreeFarCodePointer()) + m_far_emitter.GetCursorOffset(); return static_cast<u8*>(CPU::CodeCache::GetFreeFarCodePointer()) + m_far_emitter.GetCursorOffset();
} }
Value CodeGenerator::GetValueInHostRegister(const Value& value, bool allow_zero_register /* = true */) Value CodeGenerator::GetValueInHostRegister(const Value& value, bool allow_zero_register /* = true */)
@ -671,12 +665,12 @@ const void* CodeGenerator::FinalizeBlock(u32* out_host_code_size, u32* out_host_
m_near_emitter.FinalizeCode(); m_near_emitter.FinalizeCode();
m_far_emitter.FinalizeCode(); m_far_emitter.FinalizeCode();
const void* code = m_code_buffer->GetFreeCodePointer(); const void* code = CPU::CodeCache::GetFreeCodePointer();
*out_host_code_size = static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated()); *out_host_code_size = static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated());
*out_host_far_code_size = static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated()); *out_host_far_code_size = static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated());
m_code_buffer->CommitCode(static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated())); CPU::CodeCache::CommitCode(static_cast<u32>(m_near_emitter.GetSizeOfCodeGenerated()));
m_code_buffer->CommitFarCode(static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated())); CPU::CodeCache::CommitFarCode(static_cast<u32>(m_far_emitter.GetSizeOfCodeGenerated()));
m_near_emitter.Reset(); m_near_emitter.Reset();
m_far_emitter.Reset(); m_far_emitter.Reset();

View file

@ -369,10 +369,10 @@ static const Xbyak::Reg64 GetFastmemBasePtrReg()
return GetHostReg64(RMEMBASEPTR); return GetHostReg64(RMEMBASEPTR);
} }
CodeGenerator::CodeGenerator(JitCodeBuffer* code_buffer) CodeGenerator::CodeGenerator()
: m_code_buffer(code_buffer), m_register_cache(*this), : m_register_cache(*this), m_near_emitter(CPU::CodeCache::GetFreeCodeSpace(), CPU::CodeCache::GetFreeCodePointer()),
m_near_emitter(code_buffer->GetFreeCodeSpace(), code_buffer->GetFreeCodePointer()), m_far_emitter(CPU::CodeCache::GetFreeFarCodeSpace(), CPU::CodeCache::GetFreeFarCodePointer()),
m_far_emitter(code_buffer->GetFreeFarCodeSpace(), code_buffer->GetFreeFarCodePointer()), m_emit(&m_near_emitter) m_emit(&m_near_emitter)
{ {
InitHostRegs(); InitHostRegs();
} }
@ -408,11 +408,6 @@ const char* CodeGenerator::GetHostRegName(HostReg reg, RegSize size /*= HostPoin
} }
} }
void CodeGenerator::AlignCodeBuffer(JitCodeBuffer* code_buffer)
{
code_buffer->Align(16, 0x90);
}
void CodeGenerator::InitHostRegs() void CodeGenerator::InitHostRegs()
{ {
#if defined(ABI_WIN64) #if defined(ABI_WIN64)
@ -571,8 +566,8 @@ const void* CodeGenerator::FinalizeBlock(u32* out_host_code_size, u32* out_host_
const void* code = m_near_emitter.getCode<const void*>(); const void* code = m_near_emitter.getCode<const void*>();
*out_host_code_size = near_size; *out_host_code_size = near_size;
*out_host_far_code_size = far_size; *out_host_far_code_size = far_size;
m_code_buffer->CommitCode(near_size); CPU::CodeCache::CommitCode(near_size);
m_code_buffer->CommitFarCode(far_size); CPU::CodeCache::CommitFarCode(far_size);
m_near_emitter.reset(); m_near_emitter.reset();
m_far_emitter.reset(); m_far_emitter.reset();

View file

@ -46,8 +46,6 @@ add_library(util
input_source.h input_source.h
iso_reader.cpp iso_reader.cpp
iso_reader.h iso_reader.h
jit_code_buffer.cpp
jit_code_buffer.h
page_fault_handler.cpp page_fault_handler.cpp
page_fault_handler.h page_fault_handler.h
platform_misc.h platform_misc.h

View file

@ -1,74 +0,0 @@
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "jit_code_buffer.h"
#include "common/align.h"
#include "common/assert.h"
#include "common/memmap.h"
#include <algorithm>
#include <cstring>
JitCodeBuffer::JitCodeBuffer() = default;
JitCodeBuffer::~JitCodeBuffer() = default;
void JitCodeBuffer::Reset(void* ptr, u32 size, u32 far_code_size /* = 0 */)
{
Assert(far_code_size < size);
m_total_size = size;
m_code_ptr = static_cast<u8*>(ptr);
m_free_code_ptr = m_code_ptr;
m_code_size = size - far_code_size;
m_code_used = 0;
m_far_code_size = far_code_size;
m_far_code_ptr = (far_code_size > 0) ? (static_cast<u8*>(m_code_ptr) + m_code_size) : nullptr;
m_free_far_code_ptr = m_far_code_ptr;
m_far_code_used = 0;
MemMap::BeginCodeWrite();
std::memset(m_code_ptr, 0, m_total_size);
MemMap::FlushInstructionCache(m_code_ptr, m_total_size);
MemMap::EndCodeWrite();
}
void JitCodeBuffer::CommitCode(u32 length)
{
if (length == 0)
return;
MemMap::FlushInstructionCache(m_free_code_ptr, length);
Assert(length <= (m_code_size - m_code_used));
m_free_code_ptr += length;
m_code_used += length;
}
void JitCodeBuffer::CommitFarCode(u32 length)
{
if (length == 0)
return;
MemMap::FlushInstructionCache(m_free_far_code_ptr, length);
Assert(length <= (m_far_code_size - m_far_code_used));
m_free_far_code_ptr += length;
m_far_code_used += length;
}
void JitCodeBuffer::Align(u32 alignment, u8 padding_value)
{
DebugAssert(Common::IsPow2(alignment));
const u32 num_padding_bytes =
std::min(static_cast<u32>(Common::AlignUpPow2(reinterpret_cast<uintptr_t>(m_free_code_ptr), alignment) -
reinterpret_cast<uintptr_t>(m_free_code_ptr)),
GetFreeCodeSpace());
std::memset(m_free_code_ptr, padding_value, num_padding_bytes);
m_free_code_ptr += num_padding_bytes;
m_code_used += num_padding_bytes;
}

View file

@ -1,54 +0,0 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once
#include "common/types.h"
class JitCodeBuffer
{
public:
JitCodeBuffer();
~JitCodeBuffer();
bool IsValid() const { return (m_code_ptr != nullptr); }
void Reset(void* ptr, u32 size, u32 far_code_size = 0);
ALWAYS_INLINE u8* GetCodePointer() const { return m_code_ptr; }
ALWAYS_INLINE u32 GetTotalSize() const { return m_total_size; }
ALWAYS_INLINE float GetUsedPct() const
{
return (static_cast<float>(m_code_used) / static_cast<float>(m_code_size)) * 100.0f;
}
ALWAYS_INLINE float GetFarUsedPct() const
{
return (static_cast<float>(m_far_code_used) / static_cast<float>(m_far_code_size)) * 100.0f;
}
ALWAYS_INLINE u32 GetTotalUsed() const { return m_code_used + m_far_code_used; }
ALWAYS_INLINE u8* GetFreeCodePointer() const { return m_free_code_ptr; }
ALWAYS_INLINE u32 GetFreeCodeSpace() const { return static_cast<u32>(m_code_size - m_code_used); }
void CommitCode(u32 length);
ALWAYS_INLINE u8* GetFreeFarCodePointer() const { return m_free_far_code_ptr; }
ALWAYS_INLINE u32 GetFreeFarCodeSpace() const { return static_cast<u32>(m_far_code_size - m_far_code_used); }
void CommitFarCode(u32 length);
/// Adjusts the free code pointer to the specified alignment, padding with bytes.
/// Assumes alignment is a power-of-two.
void Align(u32 alignment, u8 padding_value);
private:
u8* m_code_ptr = nullptr;
u8* m_free_code_ptr = nullptr;
u32 m_code_size = 0;
u32 m_code_reserve_size = 0;
u32 m_code_used = 0;
u8* m_far_code_ptr = nullptr;
u8* m_free_far_code_ptr = nullptr;
u32 m_far_code_size = 0;
u32 m_far_code_used = 0;
u32 m_total_size = 0;
};

View file

@ -36,7 +36,6 @@
<ClInclude Include="input_manager.h" /> <ClInclude Include="input_manager.h" />
<ClInclude Include="input_source.h" /> <ClInclude Include="input_source.h" />
<ClInclude Include="iso_reader.h" /> <ClInclude Include="iso_reader.h" />
<ClInclude Include="jit_code_buffer.h" />
<ClInclude Include="metal_device.h"> <ClInclude Include="metal_device.h">
<ExcludedFromBuild>true</ExcludedFromBuild> <ExcludedFromBuild>true</ExcludedFromBuild>
</ClInclude> </ClInclude>
@ -147,7 +146,6 @@
<ClCompile Include="input_manager.cpp" /> <ClCompile Include="input_manager.cpp" />
<ClCompile Include="input_source.cpp" /> <ClCompile Include="input_source.cpp" />
<ClCompile Include="iso_reader.cpp" /> <ClCompile Include="iso_reader.cpp" />
<ClCompile Include="jit_code_buffer.cpp" />
<ClCompile Include="cd_subchannel_replacement.cpp" /> <ClCompile Include="cd_subchannel_replacement.cpp" />
<ClCompile Include="opengl_context.cpp"> <ClCompile Include="opengl_context.cpp">
<ExcludedFromBuild Condition="'$(Platform)'=='ARM64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Platform)'=='ARM64'">true</ExcludedFromBuild>

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <ItemGroup>
<ClInclude Include="jit_code_buffer.h" />
<ClInclude Include="state_wrapper.h" /> <ClInclude Include="state_wrapper.h" />
<ClInclude Include="audio_stream.h" /> <ClInclude Include="audio_stream.h" />
<ClInclude Include="cd_xa.h" /> <ClInclude Include="cd_xa.h" />
@ -75,7 +74,6 @@
<ClInclude Include="sockets.h" /> <ClInclude Include="sockets.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="jit_code_buffer.cpp" />
<ClCompile Include="state_wrapper.cpp" /> <ClCompile Include="state_wrapper.cpp" />
<ClCompile Include="cd_image.cpp" /> <ClCompile Include="cd_image.cpp" />
<ClCompile Include="audio_stream.cpp" /> <ClCompile Include="audio_stream.cpp" />