Skip to content

Commit

Permalink
SPU: Make recompilers lock-free.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Oct 26, 2019
1 parent 9ac6ef6 commit 224bc09
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 252 deletions.
36 changes: 22 additions & 14 deletions rpcs3/Emu/Cell/SPUASMJITRecompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,23 @@ void spu_recompiler::init()
}
}

spu_function_t spu_recompiler::compile(const std::vector<u32>& func, void* fn_location)
spu_function_t spu_recompiler::compile(std::vector<u32>&& _func)
{
if (!fn_location)
{
fn_location = m_spurt->find(func);
}
const auto add_loc = m_spurt->add_empty(std::move(_func));

if (fn_location == spu_runtime::g_dispatcher)
if (!add_loc)
{
return &dispatch;
return nullptr;
}

if (!fn_location)
if (add_loc->compiled)
{
return nullptr;
return add_loc->compiled;
}

if (auto cache = g_fxo->get<spu_cache>(); cache && g_cfg.core.spu_cache)
const std::vector<u32>& func = add_loc->data;

if (auto cache = g_fxo->get<spu_cache>(); cache && g_cfg.core.spu_cache && !add_loc->cached.exchange(1))
{
cache->add(func);
}
Expand Down Expand Up @@ -94,10 +93,10 @@ spu_function_t spu_recompiler::compile(const std::vector<u32>& func, void* fn_lo
X86Assembler compiler(&code);
this->c = &compiler;

if (g_cfg.core.spu_debug)
if (g_cfg.core.spu_debug && !add_loc->logged.exchange(1))
{
// Dump analyser data
this->dump(log);
this->dump(func, log);
fs::file(m_spurt->get_cache_path() + "spu.log", fs::write + fs::append).write(log);

// Set logger
Expand Down Expand Up @@ -892,12 +891,21 @@ spu_function_t spu_recompiler::compile(const std::vector<u32>& func, void* fn_lo
LOG_FATAL(SPU, "Failed to build a function");
}

if (!m_spurt->add(fn_location, fn))
// Install compiled function pointer
const bool added = !add_loc->compiled && add_loc->compiled.compare_and_swap_test(nullptr, fn);

// Rebuild trampoline if necessary
if (!m_spurt->rebuild_ubertrampoline(func[1]))
{
return nullptr;
}

if (g_cfg.core.spu_debug)
if (added)
{
add_loc->compiled.notify_all();
}

if (g_cfg.core.spu_debug && added)
{
// Add ASMJIT logs
fmt::append(log, "Address: %p\n\n", fn);
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/Cell/SPUASMJITRecompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class spu_recompiler : public spu_recompiler_base

virtual void init() override;

virtual spu_function_t compile(const std::vector<u32>&, void*) override;
virtual spu_function_t compile(std::vector<u32>&&) override;

private:
// ASMJIT runtime
Expand Down

0 comments on commit 224bc09

Please sign in to comment.