Skip to content

SPU optimizer#18141

Draft
RipleyTom wants to merge 1 commit into
RPCS3:masterfrom
RipleyTom:spu_optimization
Draft

SPU optimizer#18141
RipleyTom wants to merge 1 commit into
RPCS3:masterfrom
RipleyTom:spu_optimization

Conversation

@RipleyTom
Copy link
Copy Markdown
Contributor

Aims to remove the most dubious optimization patterns from the SPULLVMRecompiler and treat them as a separate option(SPU Optimizer in CPU tab) and hopefully with better detection/behaviour.

This takes a spu_program and builds a history going through the instructions one by one, tracking what function result each register is currently tracking(the first 128 history_index values are the registers before the start of execution and can't be part of a pattern as they are out of scope). Then we built a referential history, increasing the ref_count of all the instructions whose results are used. The final instruction of the pattern is replaced by a RPCS3_OPTIMIZER pseudo instruction which has various variants depending on the patterns.

Finally we try to detect the pattern and if we do we eliminate all the instructions which results are no longer used in the current program(hence the ref_count earlier).

<property name="title">
<string>Additional Settings</string>
</property>
<layout class="QVBoxLayout" name="checkboxes_layout">
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the name? Is it duplicated?

std::vector<u32> m_optimized_data;

public:
const std::vector<u32>& get_data() const noexcept { return m_data; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just call this data() as well?

@elad335 elad335 assigned elad335 and unassigned elad335 Feb 3, 2026
@elad335 elad335 self-requested a review February 3, 2026 15:23
// Encoding: rt4 (bits 21-27) = dest, rb (bits 14-20) = src2, ra (bits 7-13) = src1, rc (bits 0-6) = variant
static constexpr u32 make_optimizer_opcode_2src(u32 variant, u32 src1, u32 src2, u32 dest)
{
return (0xa << 28) | ((dest & 0x7f) << 21) | ((src2 & 0x7f) << 14) | ((src1 & 0x7f) << 7) | (variant & 0x7f);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spu_opcode_t fields allow to insert values as well.

{
// Creates history
for (const auto& op : program.get_data())
{
Copy link
Copy Markdown
Contributor

@elad335 elad335 Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it check if there are branches that interfere with the patterns? Like:

  1. Conditional Branch to 2st instruction.
  2. 1st instruction of pattern.
  3. 2nd instruction of pattern.

"Pattern matched" - but if the branch is taken, 1st instruction won't execute and the pattern would be incomplete.

It only appears so in first glance, because it iterates through all instructions.

@Megamouse Megamouse added CPU Optimization Optimizes existing code labels Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CPU Optimization Optimizes existing code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants