This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][Transform] Introduce loop.coalesce transform op.
ClosedPublic

Authored by kaitingwang on Jan 7 2023, 10:25 AM.

Details

Summary

This patch made a minor refactor of LoopCoalescing.cpp's walkLoops templated method and placed it in Affine's LoopUtils.cpp/h.
This method is also renamed as coalescePerfectlyNestedLoops method. This minor change enables this method to be invoked
by both the original LoopCoalescing pass as well as the newly introduced loop.coalesce transform op.

The loop.coalesce transform op has the ability to coalesce affine, and scf loop nests, leveraging existing LoopCoalescing
mechanism. I have created it inside the SCFTransformOps.td instead of AffineTransformOps.td as it feels to be similar
in spirit as the loop.unroll op that can handle both scf and affine loops. Please let me know if you feel that this op
should be moved into AffineTransformOps.td instead.

The testcase added illustrates loop.coalesce transform op working for scf, affine loops (inner, outer) as well as
coalesced loop can be further unrolled (achieving composibility).

Diff Detail

Event Timeline

kaitingwang created this revision.Jan 7 2023, 10:25 AM
kaitingwang requested review of this revision.Jan 7 2023, 10:25 AM
Herald added a project: Restricted Project. · View Herald Transcript
ftynse requested changes to this revision.Jan 10 2023, 2:33 AM
ftynse added inline comments.
mlir/include/mlir/Dialect/Affine/LoopUtils.h
46–48

This won't work without explicitly listing available specializations as extern. Or moving the body of the function template to the header.

mlir/include/mlir/Dialect/SCF/TransformOps/SCFTransformOps.h
12 ↗(On Diff #487110)

Please don't add back the dependency on PDL, we don't want it in most cases.

mlir/include/mlir/Dialect/SCF/TransformOps/SCFTransformOps.td
12

Ditto.

195
197–198
202–203

Does the transform fail if the coalescing didn't happen? It can still return the original loop so the transformation continues if the silenceable failures are suppressed.

206

There is absolutely no reason why this should be a PDL_Operation and not a TransformHandleTypeInterface.

209

Nit: can we just use functional-type for a more homogeneous syntax?

mlir/lib/Dialect/Affine/Transforms/LoopCoalescing.cpp
37–42

You can request a PreOrder walk via a template parameter, and skip descending into nested operations once the first loop is found.

mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
2848

Nit: please don't specify the explicit number of stack elements unless you have a strong reason to pick a specific number. Here and below.

2899–2900

This should be in the header.

mlir/lib/Dialect/SCF/TransformOps/SCFTransformOps.cpp
264

It would be better if this emitted a silenceable failure when the input was not a supported loop.

mlir/test/Dialect/SCF/transform-op-coalesce.mlir
2

There doesn't seem to be a need to allow unregistered dialects in this test.

8

Please use sigils (%, #, etc.) outside the pattern match. Some very old tests may be doing otherwise, but they are not the example to follow.

This revision now requires changes to proceed.Jan 10 2023, 2:33 AM

Much appreciated the detail review comments from @ftynse ! I went over each of them carefully and hopefully not missing anything. Please let me know if further improvements are needed! Thank you once again.

Thanks for iterating! I have another batch of comments and this should be good to go.

mlir/include/mlir/Dialect/SCF/TransformOps/SCFTransformOps.td
195

This doesn't seem to have been addressed. If you are looking at reviews in the email, consider opening the web version. It contains suggested changes to the code that are not being sent over email.

197–198

Ditto.

mlir/lib/Dialect/Affine/Transforms/LoopCoalescing.cpp
39–41

Could this check the result of coalescePerfectlyNestedLoops and, if it is a failure, emit an error and signal pass failure? I see we were not doing this before because coalescePerfectlyNestedLoops was not propagating errors, but now it does, so sounds like a usability improvement. Good for a separate diff, too.

mlir/lib/Dialect/SCF/TransformOps/SCFTransformOps.cpp
268

It is invalid to create a freestanding Note diagnostic, it must always be attached to a diagnostic with larger severity. This code can use emitSilenceableError() instead of manually creating the diagnostic.

Please add a test exercising this failure mode. We usually have tests for all user-visible error messages, which let us catch situations like the one described above.

269

Nit: start diagnostic messages from a lower-case letter.

mlir/test/Dialect/SCF/transform-op-coalesce.mlir
8

Sorry, I wasn't clear here. I mean something like %[[IV0:.*]] so the % symbol is present in the CHECK line, but is not part of the regular expression.

62–68

There are still some % inside the regular expression here, they should go outside, like in %[[IV2:[a-zA-Z0-9]+]].

ftynse requested changes to this revision.Jan 16 2023, 8:11 AM
This revision now requires changes to proceed.Jan 16 2023, 8:11 AM
kaitingwang marked 7 inline comments as done.

Thanks @ftynse ! Indeed, I didn't realize the review e-mail doesn't cover all the comments. I also managed to spectacularly misunderstand your comments on the sigils being 'outside' the pattern match. Hopefully, this time catches it all.

I didn't make the runOnOperation for LoopCoalescing.cpp return signalPassFailure() as you suggested because test/Dialect/Affine/loop-coalescing.mlir has a case (the last case in the test) where no coalescing takes place yet expecting the pass to return 0. I wasn't sure if I should modified this assumed behavior and this testcase. Let me know what you think and I'd be happy to modify it further.

kaitingwang marked 12 inline comments as done.Jan 16 2023, 9:14 PM
kaitingwang added inline comments.
mlir/include/mlir/Dialect/SCF/TransformOps/SCFTransformOps.td
195

Indeed I missed it. Your wording sounds much better!

mlir/test/Dialect/SCF/transform-op-coalesce.mlir
8

Your comment was actually quite clear...Not sure what I was thinking the first time. :) Thanks for following up.

ftynse accepted this revision.Jan 17 2023, 12:58 AM

Thanks!

I didn't make the runOnOperation for LoopCoalescing.cpp return signalPassFailure() as you suggested because test/Dialect/Affine/loop-coalescing.mlir has a case (the last case in the test) where no coalescing takes place yet expecting the pass to return 0. I wasn't sure if I should modified this assumed behavior and this testcase. Let me know what you think and I'd be happy to modify it further.

I see. A comment above the error-suppressing line along the lines of "this pass should succeed even if no coalescing happened" would be helpful if somebody looks at this later.

This revision is now accepted and ready to land.Jan 17 2023, 12:58 AM
This revision was automatically updated to reflect the committed changes.