summaryrefslogtreecommitdiffstats
path: root/mlir/lib/Transforms/Vectorize.cpp
blob: f38fb8a117b25dcac0556562524cceb093389f99 (plain)
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
//===- Vectorize.cpp - Vectorize Pass Impl ----------------------*- C++ -*-===//
//
// Copyright 2019 The MLIR Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================
//
// This file implements vectorization of loops, operations and data types to
// a target-independent, n-D virtual vector abstraction.
//
//===----------------------------------------------------------------------===//

#include "mlir/Analysis/LoopAnalysis.h"
#include "mlir/Analysis/MLFunctionMatcher.h"
#include "mlir/StandardOps/StandardOps.h"
#include "mlir/Transforms/Pass.h"
#include "mlir/Transforms/Passes.h"

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;
using namespace mlir;

static cl::list<unsigned> clVirtualVectorSize(
    "virtual-vector-size",
    cl::desc("Specify n-D virtual vector size for vectorization"),
    cl::ZeroOrMore);

namespace {

struct Vectorize : public FunctionPass {
  PassResult runOnMLFunction(MLFunction *f) override;

  // Thread-safe RAII contexts local to pass, BumpPtrAllocator freed on exit.
  MLFunctionMatcherContext MLContext;
};

} // end anonymous namespace

PassResult Vectorize::runOnMLFunction(MLFunction *f) {
  using matcher::Doall;
  /// TODO(ntv): support at least 4 cases for each load/store:
  ///   1. invariant along the loop index -> 1-D vectorizable with broadcast
  ///   2. contiguous along the fastest varying dimension wrt the loop index
  ///     -> a. 1-D vectorizable via stripmine/sink if loop is not innermost
  ///     -> b. 1-D vectorizable if loop is innermost
  ///   3. contiguous along non-fastest varying dimension wrt the loop index
  ///     -> needs data layout + copy to vectorize 1-D
  ///   4. not contiguous => not vectorizable
  auto pointwiseLike = Doall();
  auto &matches = pointwiseLike.match(f);
  for (auto loop : matches) {
    auto *doall = cast<ForStmt>(loop.first);
    if (!isVectorizableLoop(*doall)) {
      outs() << "\nNon-vectorizable loop: ";
      doall->print(outs());
      continue;
    }
    outs() << "\nVectorizable loop: ";
    doall->print(outs());
  }
  return PassResult::Success;
}

FunctionPass *mlir::createVectorizePass() { return new Vectorize(); }
OpenPOWER on IntegriCloud