This is an archive of the discontinued LLVM Phabricator instance.

[mlir][SCF][Transform] Add loop.coalesce_parallel Op in transform dialect
Needs RevisionPublic

Authored by tavakkoliamirmohammad on Apr 7 2023, 6:17 PM.

Details

Summary

loop.coalesce_parallel enable the coalescing of arbitrary index variables in an scf.forall. This is done by calculating the old index variables based on the new index variables.

Diff Detail

Event Timeline

tavakkoliamirmohammad requested review of this revision.Apr 7 2023, 6:17 PM

Fix missing import after applying patch

Hi @ftynse @nicolasvasilache. Could you kindly review my submitted patch? Thanks!

ftynse requested changes to this revision.Apr 14 2023, 3:05 PM
ftynse added inline comments.
mlir/include/mlir/Dialect/SCF/TransformOps/SCFTransformOps.td
221

Can't we rather extend loop.coalesce to dispatch to one of the two functions based on the kind of op it sees?

226

Please provide more documentation tha one line.

234–236

What are these? Please document. It's unclear to me why do we need to hardcode exactly three of these.

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

Coalescing normally requires two nested loops. I suppose this attempts to collapse iterators of a single op, but this is not documented, and not really exercised in the test, which doesn't test for the trip count of the new dimensions or for how the access indices are updated.

This revision now requires changes to proceed.Apr 14 2023, 3:05 PM
tavakkoliamirmohammad marked an inline comment as done.

Updated the op documentation and addressed the comments.

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

Since we need additional collapsed dimensions input specifying the position of the newly coalesced dim. Also, I added another input for the mapping attribute which is not necessary in the case of scf.for loop.

226

Added

234–236

collapsed_dim0 means that whatever is specified in that array will be dim0 in new scf.forall. I added an example in op document.
The number three comes from the mapping attribute having at most three dimensions.
Also, I don't know if we can have a two-dimensional array in MLIR. If this is supported, we can change this code to be a two-dimensional array, collapsed_dims.

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

Could you please help me understand your comment better? I apologize if I didn't fully comprehend what you were trying to convey.
The access indices won't change inside the computation. The access indices are computed based on arith.remsi and arith.divsi. These checks are in the test file.

ftynse requested changes to this revision.Apr 27 2023, 11:32 AM

Apologies for the delay. Some more comments, but we are getting there.

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

Okay, then let's at least rename this to coalesce_forall because "scf.parallel" is a different construct that is not being handled here.

226

Nit: let's make sure it fits into 80 cols.
Also: "when using" is not clear in this context, say something like "scf.forall ops associated with the operand handle have their dimensions coalesced, other ops are not modified".

227

Nit: numbner/number

232

This can use markdown-style three backticks to delimit the code here (it will also render properly on the website).

234–236

Also, I don't know if we can have a two-dimensional array in MLIR. If this is supported, we can change this code to be a two-dimensional array, collapsed_dims.

We can have an ArrayAttr of DenseArrayAttr (or any other attribute as a matter of fact). Let's do this instead. Hardcoding assumptions about hardware isn't desirable at this level.

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

Ultra-nit: no need to prefix SmallVector` with llvm::, it is re-exported in the mlir namespace. Also, you may want to consider RaggedArray from mlir/Dialect/Transform/Utils/.

259

Nit: please expand auto unless the type is obvious from context (e.g., there's a cast on the RHS).

279

It would be nice to add a diagnostic note pointing to the specific loop that failed to coalesce.

mlir/lib/Dialect/SCF/Utils/Utils.cpp
713–714

Let's rather make this function accept a RewriterBase so we can connect that properly from elsewhere in the codebase.

797–799

If this uses RewriterBase, all of these becomes much simpler and cheaper. The body block of the original loop can be inlined, which also handles the update of block arguments. Using RAUW makes this function incompatible with rewrites and generally difficult to debug.

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

Now that there's more documentation, I suppose the only missing thing here is the check that the new loop iterates in (1824, 3648).

126

Nit: please add the newline.

This revision now requires changes to proceed.Apr 27 2023, 11:32 AM