MLIR 24.0.0git
ACCLoopCollapse.cpp
Go to the documentation of this file.
1//===- ACCLoopCollapse.cpp - Collapse multi-dimensional parallel loops ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass collapses each multi-dimensional scf.parallel loop inside an
10// acc.compute_region into a one-dimensional scf.parallel loop. The original
11// induction variables are recovered from the collapsed one with remainder and
12// division operations. Loops outside an acc.compute_region are left alone.
13//
14// The collapsed loop keeps every discardable attribute of the original loop
15// (for example acc.par_dims, acc.gpu_block_redundant and acc.chunk_size), and
16// reductions are carried over by the collapsing utility. The pass also sets
17// acc.collapse_count to the number of collapsed dimensions, which remark
18// emission reads to report the collapse depth.
19//
20// Example:
21//
22// scf.parallel (%i, %j) = (%c0, %c0) to (%c10, %c20) step (%c1, %c1) {
23// ...
24// } {acc.par_dims = #acc<par_dims[block_x]>}
25//
26// becomes
27//
28// scf.parallel (%ij) = (%c0) to (%c200) step (%c1) {
29// %j = arith.remsi %ij, %c20
30// %i = arith.divsi %ij, %c20
31// ...
32// } {acc.collapse_count = 2 : i64, acc.par_dims = #acc<par_dims[block_x]>}
33//
34//===----------------------------------------------------------------------===//
35
43#include "llvm/Support/Debug.h"
44#include <vector>
45
46namespace mlir {
47namespace acc {
48#define GEN_PASS_DEF_ACCLOOPCOLLAPSE
49#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
50} // namespace acc
51} // namespace mlir
52
53#define DEBUG_TYPE "acc-loop-collapse"
54
55using namespace mlir;
56
57namespace {
58/// Collapse every dimension of \p parallelOp into a single dimension and carry
59/// the attributes of the original loop over to the collapsed loop.
60void collapseParallelOp(IRRewriter &rewriter, scf::ParallelOp parallelOp) {
61 unsigned collapseDepth = parallelOp.getNumLoops();
62
63 LLVM_DEBUG(llvm::dbgs() << "Collapsing loop with " << collapseDepth
64 << " dimensions\n");
65
66 // A single group containing every dimension of the loop.
67 std::vector<unsigned> combinedDims;
68 combinedDims.reserve(collapseDepth);
69 for (unsigned i = 0; i < collapseDepth; ++i)
70 combinedDims.push_back(i);
71
72 // The collapsing utility builds a new loop and drops the attributes of the
73 // original one, so save them here.
74 SmallVector<NamedAttribute> attrs(parallelOp->getDiscardableAttrs().begin(),
75 parallelOp->getDiscardableAttrs().end());
76
77 scf::ParallelOp collapsedOp =
78 collapseParallelLoops(rewriter, parallelOp, combinedDims);
79
80 for (NamedAttribute attr : attrs)
81 collapsedOp->setDiscardableAttr(attr.getName(), attr.getValue());
82
83 // Record the collapse depth so that remarks can report it.
84 acc::setCollapseCountAttr(collapsedOp, collapseDepth);
85}
86
87class ACCLoopCollapse : public acc::impl::ACCLoopCollapseBase<ACCLoopCollapse> {
88public:
89 using ACCLoopCollapseBase<ACCLoopCollapse>::ACCLoopCollapseBase;
90
91 void runOnOperation() override {
92 LLVM_DEBUG(llvm::dbgs() << "Enter ACCLoopCollapse()\n");
93
94 IRRewriter rewriter(&getContext());
95
96 // Only loops inside a compute region are collapsed. The inner walk is
97 // post-order, so a nested loop is collapsed before its parent. Replacing
98 // the visited loop is safe because the walk has already moved past it.
99 getOperation().walk([&](acc::ComputeRegionOp computeRegion) {
100 computeRegion.walk([&](scf::ParallelOp op) {
101 if (op.getNumLoops() > 1)
102 collapseParallelOp(rewriter, op);
103 });
104 });
105
106 LLVM_DEBUG(llvm::dbgs() << "Exit ACCLoopCollapse()\n");
107 }
108};
109} // namespace
b getContext())
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:166
void setCollapseCountAttr(Operation *op, uint64_t count)
Record on a collapsed loop how many original loops were folded into it.
Include the generated interface declarations.
scf::ParallelOp collapseParallelLoops(RewriterBase &rewriter, scf::ParallelOp loops, ArrayRef< std::vector< unsigned > > combinedDimensions)
Take the ParallelLoop and for each set of dimension indices, combine them into a single dimension.
Definition Utils.cpp:1179