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

PPU LLVM: Memory Consumption Enhancements #16537

Merged
merged 20 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8d03cad
LLVM: Install error reporting handler
elad335 Jan 10, 2025
776609b
Thread.cpp: Report VM addresses on VM segfault
elad335 Jan 10, 2025
e6930f9
LLVM: Slice PPU executable memory
elad335 Jan 10, 2025
143afb9
Remove ppu_attr::known_addr
elad335 Jan 15, 2025
f0aab21
PPU Analyzer: Remove ppu_function::name to lower sizeof(ppu_function)
elad335 Jan 15, 2025
9270103
PPU Analyzer: Use std::map::extract to reduce peak memory consumption
elad335 Jan 16, 2025
42dcbc9
PPU Analyzer: Remove ppu_function::calls to lower sizeof(ppu_function)
elad335 Jan 16, 2025
6184cf0
PPU Analyzer: Move ppu_function::callers to lower sizeof(ppu_function)
elad335 Jan 16, 2025
14abb96
PPU Analyzer: Clean addr_heap from non-relocations (PRX)
elad335 Jan 18, 2025
d75f024
PPU Analyzer: Report invalid functions
elad335 Jan 18, 2025
a4bc25d
PPU LLVM: Simplify module progress
elad335 Jan 21, 2025
d9047a8
PPU LLVM: Disable an experimental limit
elad335 Jan 21, 2025
faf45e6
LLVM: Add explicit resource-freeing at emulation stop
elad335 Jan 21, 2025
c5a06f4
JIT.h: Use kOptimizeForSize
elad335 Jan 22, 2025
e74ae04
JIT.h: Add option for lowered function size
elad335 Jan 22, 2025
4dfed5f
JITASM.cpp: add assert for code building
elad335 Jan 22, 2025
59993e7
PPU LLVM: Reduce size of JIT-transition treampolines to 16
elad335 Jan 22, 2025
17465f4
Fix Emulator::IsPaused() to allow measurements during module compilation
elad335 Jan 23, 2025
321604f
Protect RPCS3 from built-in PS2 emulator
elad335 Jan 23, 2025
f94015e
Disable ARM trampolines
elad335 Jan 25, 2025
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
18 changes: 13 additions & 5 deletions Utilities/JIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct jit_runtime_base
jit_runtime_base& operator=(const jit_runtime_base&) = delete;

const asmjit::Environment& environment() const noexcept;
void* _add(asmjit::CodeHolder* code) noexcept;
void* _add(asmjit::CodeHolder* code, usz align = 64) noexcept;
virtual uchar* _alloc(usz size, usz align) noexcept = 0;
};

Expand All @@ -93,6 +93,10 @@ struct jit_runtime final : jit_runtime_base
// Allocate memory
static u8* alloc(usz size, usz align, bool exec = true) noexcept;

// Allocate 0 bytes, observe memory location
// Same as alloc(0, 1, exec)
static u8* peek(bool exec = true) noexcept;

// Should be called at least once after global initialization
static void initialize();

Expand Down Expand Up @@ -432,7 +436,7 @@ namespace asmjit

// Build runtime function with asmjit::X86Assembler
template <typename FT, typename Asm = native_asm, typename F>
inline FT build_function_asm(std::string_view name, F&& builder, ::jit_runtime* custom_runtime = nullptr)
inline FT build_function_asm(std::string_view name, F&& builder, ::jit_runtime* custom_runtime = nullptr, bool reduced_size = false)
{
#ifdef __APPLE__
pthread_jit_write_protect_np(false);
Expand Down Expand Up @@ -467,6 +471,7 @@ inline FT build_function_asm(std::string_view name, F&& builder, ::jit_runtime*

Asm compiler(&code);
compiler.addEncodingOptions(EncodingOptions::kOptimizedAlign);
compiler.addEncodingOptions(EncodingOptions::kOptimizeForSize);
if constexpr (std::is_invocable_r_v<bool, F, Asm&, native_args&>)
{
if (!builder(compiler, args))
Expand All @@ -483,7 +488,7 @@ inline FT build_function_asm(std::string_view name, F&& builder, ::jit_runtime*
compiler();
}

const auto result = rt._add(&code);
const auto result = rt._add(&code, reduced_size ? 16 : 64);
jit_announce(result, code.codeSize(), name);
return reinterpret_cast<FT>(uptr(result));
}
Expand All @@ -498,6 +503,8 @@ namespace llvm
class StringRef;
}

enum class thread_state : u32;

// Temporary compiler interface
class jit_compiler final
{
Expand All @@ -514,8 +521,9 @@ class jit_compiler final
atomic_t<usz> m_disk_space = umax;

public:
jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags = 0);
~jit_compiler();
jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags = 0, std::function<u64(const std::string&)> symbols_cement = {}) noexcept;
jit_compiler& operator=(thread_state) noexcept;
~jit_compiler() noexcept;

// Get LLVM context
auto& get_context()
Expand Down
27 changes: 25 additions & 2 deletions Utilities/JITASM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ static u8* add_jit_memory(usz size, usz align)
return pointer;
}

if (!size && align == 1)
{
// Return memory top address
return pointer + (Ctr.load() & 0xffff'ffff);
}

u64 olda, newa;

// Simple allocation by incrementing pointer to the next free data
Expand Down Expand Up @@ -211,15 +217,15 @@ const asmjit::Environment& jit_runtime_base::environment() const noexcept
return g_env;
}

void* jit_runtime_base::_add(asmjit::CodeHolder* code) noexcept
void* jit_runtime_base::_add(asmjit::CodeHolder* code, usz align) noexcept
{
ensure(!code->flatten());
ensure(!code->resolveUnresolvedLinks());
usz codeSize = code->codeSize();
if (!codeSize)
return nullptr;

auto p = ensure(this->_alloc(codeSize, 64));
auto p = ensure(this->_alloc(codeSize, align));
ensure(!code->relocateToBase(uptr(p)));

{
Expand All @@ -231,6 +237,11 @@ void* jit_runtime_base::_add(asmjit::CodeHolder* code) noexcept

for (asmjit::Section* section : code->_sections)
{
if (section->offset() + section->bufferSize() > utils::align<usz>(codeSize, align))
{
fmt::throw_exception("CodeHolder section exceeds range: Section->offset: 0x%x, Section->bufferSize: 0x%x, alloted-memory=0x%x", section->offset(), section->bufferSize(), utils::align<usz>(codeSize, align));
}

std::memcpy(p + section->offset(), section->data(), section->bufferSize());
}
}
Expand Down Expand Up @@ -268,6 +279,18 @@ u8* jit_runtime::alloc(usz size, usz align, bool exec) noexcept
}
}

u8* jit_runtime::peek(bool exec) noexcept
{
if (exec)
{
return add_jit_memory<s_code_pos, 0x0, utils::protection::wx>(0, 1);
}
else
{
return add_jit_memory<s_data_pos, 0x40000000, utils::protection::rw>(0, 1);
}
}

void jit_runtime::initialize()
{
if (!s_code_init.empty() || !s_data_init.empty())
Expand Down
Loading