Utilities for converting nested parallel loops (affine or scf) to multi-dimensional loops

I was wondering if there are any utilities laying around that converted from affine or scf loops like this:

parallel for i = 0 to n {
  parallel for j = 0 to n {
    }
}

to the multi-dimensional variants in affine / scf that look like:

parallel for (i, j) = (0, 0) to (n, n) {
}

I was looking around and didn’t see any. Some transformations i’m interested in (like scf::collapseParallelLoops) operate on the multi-dimensional loop variants.

At least for scf.parallel, canonicalization should be able to do this: llvm-project/SCF.cpp at main · llvm/llvm-project · GitHub

1 Like

Perfect, thank you!

Do you know what is the difference between generalize canonicalization and the -scf-for-loop-canonicalization pass?

According to comment, “scf-for-loop-canonicalization” wasn’t added to general canonicalizations to avoid introducing dependency between scf and Affine/Tensor/MemRef dialects.

But how to convert multi-dimensional loops to nested parallel loops?