Issue 240110
Summary [mlir][vector][linalg] `fast-math` flags in the `Vector` and `Linalg` dialects
Labels mlir
Assignees
Reporter FedericoBruzzone
    This is a **preliminary** umbrella ticket to track the flow related to `fast-math` flags in the Linalg and Vector dialects.

This ticket is tracked under the Vector index of meta-issues: #235708 :D

> [!NOTE]
> **Development is paused.** 
> The discussion in #236855 concluded that Linalg and Vector have no documented design for how these flags compose and propagate, and that this needs an RFC before more patches land. The RFC is being drafted by @mshahneo and will be linked here once it is up. Until then, only fixes that stop a rewrite from silently changing the semantics of an op should land.

### Motivation

`vector.reduction` (#66905) and `vector.contract` (#192788) can carry `arith` fast-math flags. `vector.multi_reduction` could not, so the Linalg vectorizer dropped the flags of the combiner op, and several rewrites rebuilt a `vector.contract` without its flags (and, for some patterns, without its combining kind). Dropping flags is always correct, because they only grant permission, but it costs performance, since later lowerings (`arith` folds, the Math FMA uplift, VectorToLLVM, XeGPU, LLVM itself) are the ones that use them.

The patches so far grew organically, one op at a time. The open questions are about the design, not about a single op.

### Goal

One documented meaning for the flags on Vector and Linalg ops, and clear rules for how rewrites keep them. The RFC is expected to answer:

* Where do Linalg and Vector sit between per-op control (the LLVM model) and per-function or per-TU control (the Clang model)?
* How do flags compose when a rewrite decomposes, combines or drops ops, or makes up a value (pad, neutral, init)? The current convention is: decompose gives each part the flags of the op, combine gives the intersection, drop is always correct, and a made-up value must be one the flags allow.
* Can the flags be propagated by a mechanism instead of by hand in every pattern?

### Current state

Done:
* `vector.multi_reduction` carries an optional `fastmath` attribute, kept by the Linalg vectorizer and by the unit-dim, lowering and reduction-to-contract patterns (#233546)
* XeGPU keeps the flags when it lowers vector reductions (#233548)
* The contract rebuilds keep the combining kind and the flags (#236855). This was merged as an interim fix.

Paused:
* `allow_fast_math_flag_loss` exposed in the transform ops (#236856)

Known places that still lose the flags (found in the survey on #236855):
* `vector.outerproduct` and `vector.fma`, which have no flags
* The unrolling of reductions
* The warp distribution of reductions

### Tasks

1. Write the RFC on the meaning and propagation of fast-math flags in Vector and Linalg
2. Expose `allow_fast_math_flag_loss` in the transform ops, if the RFC keeps the option
3. Add flags to, or document the absence of flags on, `vector.outerproduct` and `vector.fma`
4. Keep the flags in the unrolling and the warp distribution of reductions
5. Restrict the combining kinds accepted by the `vector.contract` rewrites (tracked in #234238)

More tasks will be added once the RFC settles.

### PRs

- [x] #233546
- [x] #233548
- [x] #236855
- [ ] 1. RFC, TBD
- [ ] 2. #236856 (paused)
- [ ] 3. TBD
- [ ] 4. TBD
- [ ] 5. see #234238

Cheers,
-Fede

P.S. If anything is unclear, please do not hesitate to point it out.

CC: @mshahneo @banach-space @rengolin @Jianhui-Li

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to