File: ParallelLoopCollapsing.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (108 lines) | stat: -rw-r--r-- 3,775 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===- ParallelLoopCollapsing.cpp - Pass collapsing parallel loop indices -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/SCF/Transforms/Passes.h"

#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/SCF/Utils/Utils.h"
#include "mlir/Transforms/RegionUtils.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"

namespace mlir {
#define GEN_PASS_DEF_TESTSCFPARALLELLOOPCOLLAPSING
#include "mlir/Dialect/SCF/Transforms/Passes.h.inc"
} // namespace mlir

#define DEBUG_TYPE "parallel-loop-collapsing"

using namespace mlir;

namespace {
struct TestSCFParallelLoopCollapsing
    : public impl::TestSCFParallelLoopCollapsingBase<
          TestSCFParallelLoopCollapsing> {
  void runOnOperation() override {
    Operation *module = getOperation();

    // The common case for GPU dialect will be simplifying the ParallelOp to 3
    // arguments, so we do that here to simplify things.
    llvm::SmallVector<std::vector<unsigned>, 3> combinedLoops;

    // Gather the input args into the format required by
    // `collapseParallelLoops`.
    if (!clCollapsedIndices0.empty())
      combinedLoops.push_back(clCollapsedIndices0);
    if (!clCollapsedIndices1.empty()) {
      if (clCollapsedIndices0.empty()) {
        llvm::errs()
            << "collapsed-indices-1 specified but not collapsed-indices-0";
        signalPassFailure();
        return;
      }
      combinedLoops.push_back(clCollapsedIndices1);
    }
    if (!clCollapsedIndices2.empty()) {
      if (clCollapsedIndices1.empty()) {
        llvm::errs()
            << "collapsed-indices-2 specified but not collapsed-indices-1";
        signalPassFailure();
        return;
      }
      combinedLoops.push_back(clCollapsedIndices2);
    }

    if (combinedLoops.empty()) {
      llvm::errs() << "No collapsed-indices were specified. This pass is only "
                      "for testing and does not automatically collapse all "
                      "parallel loops or similar.";
      signalPassFailure();
      return;
    }

    // Confirm that the specified loops are [0,N) by testing that N values exist
    // with the maximum value being N-1.
    llvm::SmallSet<unsigned, 8> flattenedCombinedLoops;
    unsigned maxCollapsedIndex = 0;
    for (auto &loops : combinedLoops) {
      for (auto &loop : loops) {
        flattenedCombinedLoops.insert(loop);
        maxCollapsedIndex = std::max(maxCollapsedIndex, loop);
      }
    }

    if (maxCollapsedIndex != flattenedCombinedLoops.size() - 1 ||
        !flattenedCombinedLoops.contains(maxCollapsedIndex)) {
      llvm::errs()
          << "collapsed-indices arguments must include all values [0,N).";
      signalPassFailure();
      return;
    }

    // Only apply the transformation on parallel loops where the specified
    // transformation is valid, but do NOT early abort in the case of invalid
    // loops.
    module->walk([&](scf::ParallelOp op) {
      if (flattenedCombinedLoops.size() != op.getNumLoops()) {
        op.emitOpError("has ")
            << op.getNumLoops()
            << " iter args while this limited functionality testing pass was "
               "configured only for loops with exactly "
            << flattenedCombinedLoops.size() << " iter args.";
        return;
      }
      collapseParallelLoops(op, combinedLoops);
    });
  }
};
} // namespace

std::unique_ptr<Pass> mlir::createTestSCFParallelLoopCollapsingPass() {
  return std::make_unique<TestSCFParallelLoopCollapsing>();
}