Skip to content

Commit

Permalink
CPU/CodeCache: Fall back constantly-modified blocks to interpreter
Browse files Browse the repository at this point in the history
Partial fix for slowdown in Colin McRae Rally 2.0.
  • Loading branch information
stenzek committed Apr 26, 2021
1 parent b84827b commit f34a048
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/core/cpu_code_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ static constexpr u32 RECOMPILER_FAR_CODE_CACHE_SIZE = 16 * 1024 * 1024;
#endif
static constexpr u32 CODE_WRITE_FAULT_THRESHOLD_FOR_SLOWMEM = 10;

// Fall blocks back to interpreter if we recompile more than 20 times within 100 frames.
static constexpr u32 RECOMPILE_FRAMES_TO_FALL_BACK_TO_INTERPRETER = 100;
static constexpr u32 RECOMPILE_COUNT_TO_FALL_BACK_TO_INTERPRETER = 20;

#ifdef USE_STATIC_CODE_BUFFER
static constexpr u32 RECOMPILER_GUARD_SIZE = 4096;
alignas(Recompiler::CODE_STORAGE_ALIGNMENT) static u8
Expand Down Expand Up @@ -409,18 +413,34 @@ CodeBlockKey GetNextBlockKey()
return key;
}

// assumes it has already been unlinked
static void FallbackExistingBlockToInterpreter(CodeBlock* block)
{
// Replace with null so we don't try to compile it again.
s_blocks.emplace(block->key.bits, nullptr);
delete block;
}

CodeBlock* LookupBlock(CodeBlockKey key)
{
BlockMap::iterator iter = s_blocks.find(key.bits);
if (iter != s_blocks.end())
{
// ensure it hasn't been invalidated
CodeBlock* existing_block = iter->second;
if (!existing_block || !existing_block->invalidated || RevalidateBlock(existing_block))
if (!existing_block || !existing_block->invalidated)
return existing_block;

// if compilation fails or we're forced back to the interpreter, bail out
if (RevalidateBlock(existing_block))
return existing_block;
else
return nullptr;
}

CodeBlock* block = new CodeBlock(key);
block->recompile_frame_number = System::GetFrameNumber();

if (CompileBlock(block))
{
// add it to the page map if it's in ram
Expand Down Expand Up @@ -474,11 +494,34 @@ bool RevalidateBlock(CodeBlock* block)
RemoveBlockFromHostCodeMap(block);
#endif

const u32 frame_number = System::GetFrameNumber();
const u32 frame_diff = frame_number - block->recompile_frame_number;
if (frame_diff <= RECOMPILE_FRAMES_TO_FALL_BACK_TO_INTERPRETER)
{
block->recompile_count++;

if (block->recompile_count >= RECOMPILE_COUNT_TO_FALL_BACK_TO_INTERPRETER)
{
Log_PerfPrintf("Block 0x%08X has been recompiled %u times in %u frames, falling back to interpreter",
block->GetPC(), block->recompile_count, frame_diff);

FallbackExistingBlockToInterpreter(block);
return false;
}
}
else
{
// It's been a while since this block was modified, so it's all good.
block->recompile_frame_number = frame_number;
block->recompile_count = 0;
}

block->instructions.clear();

if (!CompileBlock(block))
{
Log_WarningPrintf("Failed to recompile block 0x%08X - flushing.", block->GetPC());
delete block;
Log_PerfPrintf("Failed to recompile block 0x%08X, falling back to interpreter.", block->GetPC());
FallbackExistingBlockToInterpreter(block);
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/cpu_code_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ struct CodeBlock
bool contains_double_branches = false;
bool invalidated = false;

u32 recompile_frame_number = 0;
u32 recompile_count = 0;

const u32 GetPC() const { return key.GetPC(); }
const u32 GetSizeInBytes() const { return static_cast<u32>(instructions.size()) * sizeof(Instruction); }
const u32 GetStartPageIndex() const { return (key.GetPCPhysicalAddress() / HOST_PAGE_SIZE); }
Expand Down

0 comments on commit f34a048

Please sign in to comment.