Skip to content

[spirv-opt] Reassociation Pass #6407

@alister-chowdhury

Description

@alister-chowdhury

Hey,

Currently folding rules act on a instruction-by-instruction basis, which works for a lot of cases, but struggles to deal with temporaries in the middle of an expression.

e.g:

10 + a + 10       => a + 20
10 + b + a + 10   => no fold

(3 * a) + (-3 *a)     => 0
(3 * a) + 1 + (-3 *a) => no fold

(3 * a) + (3 * b)     => 3 * (a + b)
(3 * a) + c + (3 * b) => no fold

Because each rule would need to account for every possibility and combination, it's not very practical to
make rules for these sorts of situations.

Locally I've been working on a reassociation pass (for FP arithmetic) and was wondering if it is something that you would want (since it's not simply adding extra folding rules).

The general premise is to reassociate independant chains of: OpFAdd, OpFSub, OpFNegate, OpFMul, OpFDiv, OpVectorTimesScalar.

Constants are moved closer together e.g:

  (A + 5 + B - C - 7) => (5 - 7 + A + B - C) => (-2 + A + B - C)
  (A * 5 * B * C * 7) => (5 * 7 * A * B * C) => (35 * A * B * C)

Variables far apart can cancel each other out e.g:

 (A + B + C - A - B)   => C
 (A * B * C / (A * B)) => C

Variables can be factored e.g:

  (A * x * x + B * x + C) => x * (A * x + B) + C
  A * x + B * x + C * x   => x * (A + B + C)

With a few extra folding rules that operate on the chains directly.

Many thanks,
Alister

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions