This is an archive of the discontinued LLVM Phabricator instance.

[mlir][VectorOps] Introduce a `vector.fma` op that works on n-D vectors and lowers to `llvm.intrin.fmuladd`
ClosedPublic

Authored by nicolasvasilache on Feb 5 2020, 10:19 AM.

Details

Summary

The vector.fma operation is portable enough across targets that we do not want
to keep it wrapped under vector.outerproduct and llvm.intrin.fmuladd.
This revision lifts the op into the vector dialect and implements the lowering to LLVM by using two patterns:

  1. a pattern that lowers from n-D to (n-1)-D by unrolling when n > 2
  2. a pattern that converts from 1-D to the proper LLVM representation

Diff Detail

Event Timeline

Avoid deleting all the tests.

Unit tests: unknown.

clang-tidy: pass.

clang-format: fail. Please format your changes with clang-format by running git-clang-format HEAD^ or applying this patch.

Build artifacts: clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

Unit tests: unknown.

clang-tidy: pass.

clang-format: fail. Please format your changes with clang-format by running git-clang-format HEAD^ or applying this patch.

Build artifacts: clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

aartbik requested changes to this revision.Feb 5 2020, 2:45 PM
aartbik added inline comments.
mlir/include/mlir/Dialect/VectorOps/VectorOps.td
398

typo: that operate (plural)

but more in general, can you describe the semantics in a bit more detail than this? In particular, the lowering part to llvm could be mentioned at one point as motivation to have this, but it seems a bit strange to mention that in the very first sentence already.

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
645

perhaps use the %[[s0:.*]] = .... to capture the values and make sure they are used where expected?

This revision now requires changes to proceed.Feb 5 2020, 2:45 PM

Thanks, Nicolas! LGTM. Just minor changes.

mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
413

Adding doc about what this pattern is matching and what is not would be great

540

I think adding a small example to the doc would help better understand what this patter is doing.

1061–1064

Rename VectorFMAOpRewritePattern and VectorFMAOpConversion to be more aligned with what they match?
You'll probably find something better but something along the lines of SingleDimVectorFMAOpRewritePattern and MultiDimVectorFMAOpConversion

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
645

+1

nicolasvasilache marked 7 inline comments as done.

ddress review comments.

mlir/include/mlir/Dialect/VectorOps/VectorOps.td
398

Made the description more general.
There is a notion of guaranteed fmuladd in the LLVM case that I kept.

Unit tests: unknown.

clang-tidy: pass.

clang-format: fail. Please format your changes with clang-format by running git-clang-format HEAD^ or applying this patch.

Build artifacts: clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

aartbik requested changes to this revision.Feb 6 2020, 10:18 AM
aartbik added inline comments.
mlir/include/mlir/Dialect/VectorOps/VectorOps.td
399

much better, and the mention of llvm later is fine of course

I would still say something about that all shapes need to match exactly (rank and dimensions), it is implied by the syntax, and of course enforced by the traits, but why not be a bit more explicit in the doc :-)

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
641

note that something like

// CHECK-SAME: %[[A:arg[0-9]+]]:

can be used if you want to match the argument inputs as well, see e.g. insert_strided_slice3 (probably need to rename a/b into arg0/arg1 to be sure, although I think that renaming happens anyway)

This revision now requires changes to proceed.Feb 6 2020, 10:18 AM
aartbik added inline comments.Feb 6 2020, 10:49 AM
mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
548

this is not a check pattern, so why not fill out the %a, %b, %c and ssa vars for readability?

fhahn added a subscriber: fhahn.Feb 6 2020, 11:23 AM
fhahn added inline comments.
mlir/include/mlir/Dialect/VectorOps/VectorOps.td
400

IIRC llvm.fmuladd does not specify the rounding behaviour (mul and add may or may not be fused).

The way I read the description here it seems like fusion is ecpexted for vector.fma unless I am missing something. It might be worth clarifying the rounding behaviour

nicolasvasilache marked 4 inline comments as done.Feb 6 2020, 7:37 PM
nicolasvasilache added inline comments.
mlir/include/mlir/Dialect/VectorOps/VectorOps.td
400

Thanks @fhahn ! I overlooked the semantics section that clearly states to use fma.
What I want is indeed fma, will adapt the revision accordingly.

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
641

I do not see this as necessary, there is a 1-1 mapping that is already captured by the type.

Is there a way to remove your blocking review but without approving yet? I don't see that option here.
Anyway, please look at the other comments that are still open.

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
641

It was of course just a suggestion :-)

Regardless, I find %[[arg0]] easier to parse by eye than {{.*}} and it is the direction we are generally taking with new tests. But I won't insist.

nicolasvasilache marked an inline comment as done.Feb 7 2020, 6:14 AM

Is there a way to remove your blocking review but without approving yet? I don't see that option here.
Anyway, please look at the other comments that are still open.

@aartbik that's fine, I still need to expose the fma intrinsic from LLVM and use it in this revision, fmuladd is not the right thing as @fhahn 's comment made me realize.

nicolasvasilache marked 3 inline comments as done.

Rebase on top of llvm.intr.fma addition.
Address review comments.

nicolasvasilache marked an inline comment as done.Feb 7 2020, 8:02 AM
nicolasvasilache added inline comments.
mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
641

ok, I am sensitive to general simplicity, maybe erring on the side of terseness is counter-productive, I'll just update it :)

aartbik accepted this revision.Feb 7 2020, 11:34 AM
This revision is now accepted and ready to land.Feb 7 2020, 11:34 AM
This revision was automatically updated to reflect the committed changes.