Skip to content

[LLVM] Redesign Straight-Line Strength Reduction (SLSR) #162376

@fiigii

Description

@fiigii

Motivation

The Straight-Line Strength Reduction (SLSR) pass reduces arithmetic redundancy in straight-line code (often emitted by unrolling). The current implementation has several limitations:

  • The representation is fixed to Base (Value), Index (Constant), and Stride (Value). It only reuses work when Base and Stride match and the Index delta is a constant. In practice, reuse opportunities also arise when Base or Stride differ by a constant or even a variable expression.
  • Profitability relies on simple heuristics. For example, isSimplestForm rejects canonical GEPs whose index is 1, even though such forms are common and often profitable after unrolling.
  • Compile-time is impacted by a linear scan over a list of candidates to find a basis, limiting the search scope and effectiveness.
  • The pass primarily targets fully unrolled, uniform loop bodies. Unrolled loops with conditional bodies have similar reuse opportunities but are not well handled today.

Example motivating pattern:

for (int i = 0; i < N; i++) {
  if (cond)
    body;
}

After unrolling, later bodies may reuse computations from earlier bodies even when the earlier ones do not dominate the later ones.

Proposed Changes

This redesign will be contributed as a sequence of PRs:

  1. Extend candidate equivalence beyond Index deltas to also consider Base and Stride deltas. Deltas are computed via ScalarEvolution (getMinusSCEV); they may be constant or variable.
  2. Replace linear basis search with an efficient dictionary keyed by tuples, enabling fast, dominance-aware lookups with broader scope.
  3. Simplify GEP candidate construction by avoiding factorArrayIndex when getMinusSCEV can derive the needed delta, reducing compile-time and brittleness.
  4. Introduce a clearer cost model to replace ad-hoc heuristics, allowing consistent profitability decisions across Add, Mul, and GEP forms.
  5. Add Partial Strength Reduction (PSR) to handle unrolled code with conditionals by hoisting a reusable basis to the nearest common dominator and rewriting users across non-dominating paths.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions