diff options
| author | Martino Fontana <tinozzo123@gmail.com> | 2026-08-08 17:48:30 +0200 |
|---|---|---|
| committer | Martino Fontana <tinozzo123@gmail.com> | 2026-11-22 17:17:04 +0100 |
| commit | 1704baf53b3ca02a29537a9b3d620e20fbaa6705 (patch) | |
| tree | 615caeb106d5c47637b7c91a5ae714a9946e6e83 /Source/Core | |
| parent | b23f8d91b0568c886fb503ded8802bd4e47d170b (diff) | |
JitRegCache: Also make the constraints data oriented
`RCConstraints::IsAnyConstraintActive()` was extremely inefficient.
It could be argued that, since it's only used in ASSERTs, they could have been changed to DEBUG_ASSERTs.
But with this, we can have them in release builds with little performance impact.
Also, the implementations of `RCConstraints` are moved to the header files, since they tend to be short and thus inline well.
All the low-hanging performance improvements for the register cache are now fixed, with profiling percentages being at expected values.
(Though more micro-optimizations are possible.)
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.cpp | 62 | ||||
| -rw-r--r-- | Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.h | 162 |
2 files changed, 117 insertions, 107 deletions
diff --git a/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.cpp b/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.cpp index 7b74ecd1a7..73e5efc107 100644 --- a/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.cpp +++ b/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.cpp @@ -15,7 +15,6 @@ #include "Common/x64Emitter.h" #include "Common/x64Reg.h" #include "Core/PowerPC/Jit64/Jit.h" -#include "Core/PowerPC/Jit64/RegCache/RCMode.h" using namespace Gen; using namespace PowerPC; @@ -257,36 +256,6 @@ bool RegCache::SanityCheck() const return true; } -RCOpArg RegCache::Use(preg_t preg, RCMode mode) -{ - m_guests_constraints[preg].AddUse(mode); - return RCOpArg{this, preg}; -} - -RCOpArg RegCache::UseNoImm(preg_t preg, RCMode mode) -{ - m_guests_constraints[preg].AddUseNoImm(mode); - return RCOpArg{this, preg}; -} - -RCOpArg RegCache::BindOrImm(preg_t preg, RCMode mode) -{ - m_guests_constraints[preg].AddBindOrImm(mode); - return RCOpArg{this, preg}; -} - -RCX64Reg RegCache::Bind(preg_t preg, RCMode mode) -{ - m_guests_constraints[preg].AddBind(mode); - return RCX64Reg{this, preg}; -} - -RCX64Reg RegCache::RevertableBind(preg_t preg, RCMode mode) -{ - m_guests_constraints[preg].AddRevertableBind(mode); - return RCX64Reg{this, preg}; -} - RCX64Reg RegCache::Scratch() { return Scratch(GetFreeXReg()); @@ -575,8 +544,7 @@ void RegCache::Unlock(preg_t preg) if (m_state.m_guests_n_locked[preg] == 0) { m_state.m_guests_is_locked[preg] = false; - // Fully unlocked, reset realization state. - m_guests_constraints[preg] = {}; + m_guests_constraints.Reset(preg); } } @@ -592,27 +560,22 @@ void RegCache::UnlockX(X64Reg xr) m_state.m_hosts_is_locked[xr] = m_state.m_hosts_n_locked[xr] > 0; } -bool RegCache::IsRealized(preg_t preg) const -{ - return m_guests_constraints[preg].IsRealized(); -} - void RegCache::Realize(preg_t preg) { - if (m_guests_constraints[preg].IsRealized()) + if (m_guests_constraints.IsRealized(preg)) return; - const bool load = m_guests_constraints[preg].ShouldLoad(); - const bool dirty = m_guests_constraints[preg].ShouldDirty(); - const bool kill_imm = m_guests_constraints[preg].ShouldKillImmediate(); - const bool kill_mem = m_guests_constraints[preg].ShouldKillMemory(); + const bool load = m_guests_constraints.ShouldLoad(preg); + const bool dirty = m_guests_constraints.ShouldDirty(preg); + const bool kill_imm = m_guests_constraints.ShouldKillImmediate(preg); + const bool kill_mem = m_guests_constraints.ShouldKillMemory(preg); const auto do_bind = [&] { BindToRegister(preg, load, dirty); - m_guests_constraints[preg].Realized(RCConstraint::RealizedLoc::Bound); + m_guests_constraints.Realized(preg, RCConstraints::RealizedLoc::Bound); }; - if (m_guests_constraints[preg].ShouldBeRevertable()) + if (m_guests_constraints.ShouldBeRevertable(preg)) { StoreFromRegister(preg, FlushMode::Undirty); do_bind(); @@ -625,22 +588,17 @@ void RegCache::Realize(preg_t preg) if (dirty || kill_imm) do_bind(); else - m_guests_constraints[preg].Realized(RCConstraint::RealizedLoc::Imm); + m_guests_constraints.Realized(preg, RCConstraints::RealizedLoc::Imm); } else if (!m_state.m_guests_in_host_register[preg]) { if (kill_mem) do_bind(); else - m_guests_constraints[preg].Realized(RCConstraint::RealizedLoc::Mem); + m_guests_constraints.Realized(preg, RCConstraints::RealizedLoc::Mem); } else { do_bind(); } } - -bool RegCache::IsAnyConstraintActive() const -{ - return std::ranges::any_of(m_guests_constraints, &RCConstraint::IsActive); -} diff --git a/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.h b/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.h index a859390370..0312db2f1a 100644 --- a/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.h +++ b/Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.h @@ -8,6 +8,7 @@ #include <optional> #include <span> #include <type_traits> +#include <utility> #include <variant> #include "Common/x64Emitter.h" @@ -143,22 +144,30 @@ private: RegsState m_state; }; -class RCConstraint +class RCConstraints { public: - bool IsRealized() const { return realized != RealizedLoc::Invalid; } - bool IsActive() const + bool IsRealized(preg_t preg) const { return m_realized[preg] != RealizedLoc::Invalid; } + bool IsAnyConstraintActive() const { - return IsRealized() || write || read || kill_imm || kill_mem || revertable; + // Check whether any member of m_realized is different from RealizedLoc::Invalid. + // This is ugly, but compilers refuse to emit optimized code otherwise, which is needed + // since this function is called very often. + // (memcmp with a zeroes array seems like a sane alterative, but MSVC always refuses to inline + // it with constant size 32, while GCC refuses to inline it if there are other comparisons.) + static_assert(std::to_underlying(RealizedLoc::Invalid) == 0); + const auto chunks = std::bit_cast<std::array<std::uint64_t, 4>>(m_realized); + return (chunks[0] | chunks[1] | chunks[2] | chunks[3]) != 0 || + (m_write | m_read | m_kill_imm | m_kill_mem | m_revertable); } - bool ShouldLoad() const { return read; } - bool ShouldDirty() const { return write; } - bool ShouldBeRevertable() const { return revertable; } - bool ShouldKillImmediate() const { return kill_imm; } - bool ShouldKillMemory() const { return kill_mem; } + bool ShouldLoad(preg_t preg) const { return m_read[preg]; } + bool ShouldDirty(preg_t preg) const { return m_write[preg]; } + bool ShouldBeRevertable(preg_t preg) const { return m_revertable[preg]; } + bool ShouldKillImmediate(preg_t preg) const { return m_kill_imm[preg]; } + bool ShouldKillMemory(preg_t preg) const { return m_kill_mem[preg]; } - enum class RealizedLoc + enum class RealizedLoc : u8 { Invalid, Bound, @@ -166,13 +175,13 @@ public: Mem, }; - void Realized(RealizedLoc loc) + void Realized(preg_t preg, RealizedLoc loc) { - realized = loc; - ASSERT(IsRealized()); + m_realized[preg] = loc; + ASSERT(IsRealized(preg)); } - enum class ConstraintLoc + enum class ConstraintLoc : u8 { Bound, BoundOrImm, @@ -180,35 +189,54 @@ public: Any, }; - void AddUse(RCMode mode) { AddConstraint(mode, ConstraintLoc::Any, false); } - void AddUseNoImm(RCMode mode) { AddConstraint(mode, ConstraintLoc::BoundOrMem, false); } - void AddBindOrImm(RCMode mode) { AddConstraint(mode, ConstraintLoc::BoundOrImm, false); } - void AddBind(RCMode mode) { AddConstraint(mode, ConstraintLoc::Bound, false); } - void AddRevertableBind(RCMode mode) { AddConstraint(mode, ConstraintLoc::Bound, true); } + void AddUse(preg_t preg, RCMode mode) { AddConstraint(preg, mode, ConstraintLoc::Any, false); } + void AddUseNoImm(preg_t preg, RCMode mode) + { + AddConstraint(preg, mode, ConstraintLoc::BoundOrMem, false); + } + void AddBindOrImm(preg_t preg, RCMode mode) + { + AddConstraint(preg, mode, ConstraintLoc::BoundOrImm, false); + } + void AddBind(preg_t preg, RCMode mode) { AddConstraint(preg, mode, ConstraintLoc::Bound, false); } + void AddRevertableBind(preg_t preg, RCMode mode) + { + AddConstraint(preg, mode, ConstraintLoc::Bound, true); + } + + void Reset(preg_t preg) + { + m_realized[preg] = {}; + m_write[preg] = false; + m_read[preg] = false; + m_kill_imm[preg] = false; + m_kill_mem[preg] = false; + m_revertable[preg] = false; + } private: - void AddConstraint(RCMode mode, ConstraintLoc loc, bool should_revertable) + void AddConstraint(preg_t preg, RCMode mode, ConstraintLoc loc, bool should_revertable) { - if (IsRealized()) + if (IsRealized(preg)) { - ASSERT(IsCompatible(mode, loc, should_revertable)); + ASSERT(IsCompatible(preg, mode, loc, should_revertable)); return; } if (should_revertable) - revertable = true; + m_revertable[preg] = true; switch (loc) { case ConstraintLoc::Bound: - kill_imm = true; - kill_mem = true; + m_kill_imm[preg] = true; + m_kill_mem[preg] = true; break; case ConstraintLoc::BoundOrImm: - kill_mem = true; + m_kill_mem[preg] = true; break; case ConstraintLoc::BoundOrMem: - kill_imm = true; + m_kill_imm[preg] = true; break; case ConstraintLoc::Any: break; @@ -217,21 +245,21 @@ private: switch (mode) { case RCMode::Read: - read = true; + m_read[preg] = true; break; case RCMode::Write: - write = true; + m_write[preg] = true; break; case RCMode::ReadWrite: - read = true; - write = true; + m_read[preg] = true; + m_write[preg] = true; break; } } - bool IsCompatible(RCMode mode, ConstraintLoc loc, bool should_revertable) const + bool IsCompatible(preg_t preg, RCMode mode, ConstraintLoc loc, bool should_revertable) const { - if (should_revertable && !revertable) + if (should_revertable && !m_revertable[preg]) { return false; } @@ -240,11 +268,11 @@ private: switch (loc) { case ConstraintLoc::Bound: - return realized == RealizedLoc::Bound; + return m_realized[preg] == RealizedLoc::Bound; case ConstraintLoc::BoundOrImm: - return realized == RealizedLoc::Bound || realized == RealizedLoc::Imm; + return m_realized[preg] == RealizedLoc::Bound || m_realized[preg] == RealizedLoc::Imm; case ConstraintLoc::BoundOrMem: - return realized == RealizedLoc::Bound || realized == RealizedLoc::Mem; + return m_realized[preg] == RealizedLoc::Bound || m_realized[preg] == RealizedLoc::Mem; case ConstraintLoc::Any: return true; } @@ -252,15 +280,15 @@ private: return false; }(); - const bool is_mode_compatible = [&] { + const bool is_mode_compatible = [&] -> bool { switch (mode) { case RCMode::Read: - return read; + return m_read[preg]; case RCMode::Write: - return write; + return m_write[preg]; case RCMode::ReadWrite: - return read && write; + return m_read[preg] && m_write[preg]; } ASSERT(false); return false; @@ -269,12 +297,13 @@ private: return is_loc_compatible && is_mode_compatible; } - RealizedLoc realized = RealizedLoc::Invalid; - bool write = false; - bool read = false; - bool kill_imm = false; - bool kill_mem = false; - bool revertable = false; + // Alignment is important since IsAnyConstraintActive is very hot + alignas(NUM_GUEST_REGS) std::array<RealizedLoc, NUM_GUEST_REGS> m_realized{}; + BitSetGuest m_write; + BitSetGuest m_read; + BitSetGuest m_kill_imm; + BitSetGuest m_kill_mem; + BitSetGuest m_revertable; }; class RegCache @@ -335,11 +364,35 @@ public: return static_cast<Gen::X64Reg>(m_state.m_guests_host_register[preg]); } - RCOpArg Use(preg_t preg, RCMode mode); - RCOpArg UseNoImm(preg_t preg, RCMode mode); - RCOpArg BindOrImm(preg_t preg, RCMode mode); - RCX64Reg Bind(preg_t preg, RCMode mode); - RCX64Reg RevertableBind(preg_t preg, RCMode mode); + RCOpArg Use(preg_t preg, RCMode mode) + { + m_guests_constraints.AddUse(preg, mode); + return RCOpArg{this, preg}; + } + + RCOpArg UseNoImm(preg_t preg, RCMode mode) + { + m_guests_constraints.AddUseNoImm(preg, mode); + return RCOpArg{this, preg}; + } + + RCOpArg BindOrImm(preg_t preg, RCMode mode) + { + m_guests_constraints.AddBindOrImm(preg, mode); + return RCOpArg{this, preg}; + } + + RCX64Reg Bind(preg_t preg, RCMode mode) + { + m_guests_constraints.AddBind(preg, mode); + return RCX64Reg{this, preg}; + } + + RCX64Reg RevertableBind(preg_t preg, RCMode mode) + { + m_guests_constraints.AddRevertableBind(preg, mode); + return RCX64Reg{this, preg}; + } RCX64Reg Scratch(); RCX64Reg Scratch(Gen::X64Reg xr); @@ -391,14 +444,13 @@ protected: void Unlock(preg_t preg); void LockX(Gen::X64Reg xr); void UnlockX(Gen::X64Reg xr); - bool IsRealized(preg_t preg) const; + bool IsRealized(preg_t preg) const { return m_guests_constraints.IsRealized(preg); } + bool IsAnyConstraintActive() const { return m_guests_constraints.IsAnyConstraintActive(); } void Realize(preg_t preg); - bool IsAnyConstraintActive() const; - Jit64& m_jit; Gen::XEmitter& m_emitter; RegsState m_state{}; - std::array<RCConstraint, NUM_GUEST_REGS> m_guests_constraints; + RCConstraints m_guests_constraints{}; }; |
