llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir][vector][linalg] `fast-math` flags in the `Vector` and `Linalg` dialects

Open FedericoBruzzone opened this issue 4 weeks ago • 2 comments

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

Description PRs Status
Previous PRs #233546, #233548, #236855 ✅
Write the RFC on the meaning and propagation of fast-math flags in Vector and Linalg https://discourse.llvm.org/t/rfc-mlir-linalg-vector-fast-math-flags-on-linalg-and-vector-ops/92764 ✅
Expose allow_fast_math_flag_loss in the transform ops, if the RFC keeps the option TBD WIP
Add flags to, or document the absence of flags on, vector.outerproduct and vector.fma TBD WIP
Keep the flags in the unrolling and the warp distribution of reductions TBD WIP
Restrict the combining kinds accepted by the vector.contract rewrites (tracked in #234238) TBD WIP

More tasks will be added once the RFC settles.

PRs

  • #233546
  • #233548
  • #236855
  • #236856 (paused)

Cheers, -Fede

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

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

FedericoBruzzone avatar Nov 08 '26 09:11 FedericoBruzzone

@llvm/issue-subscribers-mlir

Author: Federico Bruzzone (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

Thanks, @FedericoBruzzone.

The RFC is live @banach-space, @rengolin.

mshahneo avatar Nov 09 '26 07:11 mshahneo