summaryrefslogtreecommitdiffstats
path: root/mlir/examples/Linalg/Linalg3/lib/Transforms.cpp
blob: 8731138bba575a5d4c60501af81cfa1e263544a3 (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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//===- Transforms.cpp - Implementation of the linalg Transformations ------===//
//
// 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 analyses and transformations for the linalg dialect.
//
//===----------------------------------------------------------------------===//

#include "linalg3/Transforms.h"
#include "linalg2/Intrinsics.h"
#include "linalg3/Ops.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/OperationSupport.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/StandardTypes.h"

using namespace mlir;
using namespace mlir::edsc;
using namespace mlir::edsc::intrinsics;
using namespace linalg;
using namespace linalg::intrinsics;

void linalg::composeSliceOps(mlir::FuncOp f) {
  f.walk<SliceOp>([](SliceOp sliceOp) {
    auto *sliceResult = sliceOp.getResult();
    auto viewOp = emitAndReturnFullyComposedView(sliceResult);
    sliceResult->replaceAllUsesWith(viewOp.getResult());
    sliceOp.erase();
  });
}

void linalg::lowerToFinerGrainedTensorContraction(mlir::FuncOp f) {
  f.walk([](Operation *op) {
    if (auto matmulOp = dyn_cast<linalg::MatmulOp>(op)) {
      matmulOp.writeAsFinerGrainTensorContraction();
    } else if (auto matvecOp = dyn_cast<linalg::MatvecOp>(op)) {
      matvecOp.writeAsFinerGrainTensorContraction();
    } else {
      return;
    }
    op->erase();
  });
}

// Folding eagerly is necessary to abide by affine.for static step requirement.
// Returns nullptr if folding is not trivially feasible.
static Value *tryFold(AffineMap map, SmallVector<Value *, 4> operands) {
  assert(map.getNumResults() == 1 && "single result map expected");
  auto expr = map.getResult(0);
  if (auto dim = expr.dyn_cast<AffineDimExpr>())
    return operands[dim.getPosition()];
  if (auto sym = expr.dyn_cast<AffineSymbolExpr>())
    return operands[map.getNumDims() + sym.getPosition()];
  if (auto cst = expr.dyn_cast<AffineConstantExpr>())
    return constant_index(cst.getValue());
  return nullptr;
}

Value *linalg::makeFoldedComposedAffineApply(AffineMap map,
                                             ArrayRef<Value *> operandsRef) {
  SmallVector<Value *, 4> operands(operandsRef.begin(), operandsRef.end());
  fullyComposeAffineMapAndOperands(&map, &operands);
  if (auto *v = tryFold(map, operands)) {
    return v;
  }
  auto &b = ScopedContext::getBuilder();
  auto loc = ScopedContext::getLocation();
  return b.create<AffineApplyOp>(loc, map, operands).getResult();
}

linalg::RangeParts::RangeParts(unsigned reserved) {
  mins.reserve(reserved);
  maxes.reserve(reserved);
  steps.reserve(reserved);
}

static SmallVector<Value *, 4>
extractFromRanges(ArrayRef<Value *> ranges,
                  std::function<Value *(RangeOp)> extract) {
  SmallVector<Value *, 4> res;
  res.reserve(ranges.size());
  for (auto *v : ranges) {
    auto r = cast<RangeOp>(v->getDefiningOp());
    res.push_back(extract(r));
  }
  return res;
}

linalg::RangeParts::RangeParts(ArrayRef<Value *> ranges)
    : mins(extractFromRanges(ranges, [](RangeOp r) { return r.getMin(); })),
      maxes(extractFromRanges(ranges, [](RangeOp r) { return r.getMax(); })),
      steps(extractFromRanges(ranges, [](RangeOp r) { return r.getStep(); })) {}

SmallVector<Value *, 4> linalg::RangeParts::makeRanges() {
  SmallVector<Value *, 4> res;
  res.reserve(mins.size());
  for (auto z : llvm::zip(mins, maxes, steps)) {
    res.push_back(range(std::get<0>(z), std::get<1>(z), std::get<2>(z)));
  }
  return res;
}

static RangeParts makeGenericRangeParts(AffineMap map,
                                        ArrayRef<Value *> ranges) {
  assert(map.getNumInputs() == ranges.size());
  unsigned numDims = map.getNumDims();
  assert(map.getNumSymbols() == 0);

  RangeParts res(map.getNumResults());
  RangeParts rangeParts(ranges);
  for (auto expr : map.getResults()) {
    AffineMap map = AffineMap::get(numDims, 0, expr);
    res.mins.push_back(makeFoldedComposedAffineApply(map, rangeParts.mins));
    res.maxes.push_back(makeFoldedComposedAffineApply(map, rangeParts.maxes));
    res.steps.push_back(makeFoldedComposedAffineApply(map, rangeParts.steps));
  }
  return res;
}

SmallVector<Value *, 4> makeGenericRanges(AffineMap map,
                                          ArrayRef<Value *> ranges) {
  return makeGenericRangeParts(map, ranges).makeRanges();
}

SmallVector<Value *, 4>
linalg::makeGenericLoopRanges(AffineMap operandRangesToLoopMaps,
                              ArrayRef<Value *> ranges,
                              ArrayRef<Value *> tileSizes) {
  RangeParts res = makeGenericRangeParts(operandRangesToLoopMaps, ranges);
  if (tileSizes.empty())
    return res.makeRanges();
  SmallVector<Value *, 4> tiledSteps;
  for (auto z : llvm::zip(res.steps, tileSizes)) {
    auto *step = std::get<0>(z);
    auto tileSize = std::get<1>(z);
    auto stepValue = cast<ConstantIndexOp>(step->getDefiningOp()).getValue();
    auto tileSizeValue =
        cast<ConstantIndexOp>(tileSize->getDefiningOp()).getValue();
    assert(stepValue > 0);
    tiledSteps.push_back(constant_index(stepValue * tileSizeValue));
  }
  res.steps = tiledSteps;
  return res.makeRanges();
}

template <class ContractionOp>
static SmallVector<mlir::AffineForOp, 4>
writeContractionAsLoops(ContractionOp contraction) {
  OpBuilder builder(contraction.getOperation());
  ScopedContext scope(builder, contraction.getLoc());
  auto allRanges = getRanges(contraction);
  auto loopRanges =
      makeGenericLoopRanges(operandRangesToLoopsMap(contraction), allRanges);

  SmallVector<IndexHandle, 4> parallelIvs(contraction.getNumParallelDims());
  SmallVector<IndexHandle, 4> reductionIvs(contraction.getNumReductionDims());
  auto pivs = makeIndexHandlePointers(parallelIvs);
  auto rivs = makeIndexHandlePointers(reductionIvs);
  assert(loopRanges.size() == pivs.size() + rivs.size());

  // clang-format off
  using linalg::common::LoopNestRangeBuilder;
  ArrayRef<Value *> ranges(loopRanges);
  LoopNestRangeBuilder(pivs, ranges.take_front(pivs.size()))([&]{
    LoopNestRangeBuilder(rivs, ranges.take_back(rivs.size()))(
      [&contraction, &parallelIvs, &reductionIvs] {
        SmallVector<mlir::Value *, 4> parallel(
            parallelIvs.begin(), parallelIvs.end());
        SmallVector<mlir::Value *, 4> reduction(
            reductionIvs.begin(), reductionIvs.end());
        contraction.emitScalarImplementation(parallel, reduction);
      });
  });
  // clang-format on

  // Return the AffineForOp for better compositionality (e.g. tiling).
  SmallVector<mlir::AffineForOp, 4> loops;
  loops.reserve(pivs.size() + rivs.size());
  for (auto iv : parallelIvs)
    loops.push_back(getForInductionVarOwner(iv.getValue()));
  for (auto iv : reductionIvs)
    loops.push_back(getForInductionVarOwner(iv.getValue()));

  return loops;
}

llvm::Optional<SmallVector<mlir::AffineForOp, 4>>
linalg::writeAsLoops(Operation *op) {
  if (auto matmulOp = dyn_cast<linalg::MatmulOp>(op)) {
    return writeContractionAsLoops(matmulOp);
  } else if (auto matvecOp = dyn_cast<linalg::MatvecOp>(op)) {
    return writeContractionAsLoops(matvecOp);
  } else if (auto dotOp = dyn_cast<linalg::DotOp>(op)) {
    return writeContractionAsLoops(dotOp);
  }
  return llvm::None;
}

void linalg::lowerToLoops(mlir::FuncOp f) {
  f.walk([](Operation *op) {
    if (writeAsLoops(op))
      op->erase();
  });
}

/// Emits and returns the standard load and store ops from the view indexings.
/// If the indexing is of index type, use it as an index to the load/store.
/// If the indexing is a range, use range.min + indexing as an index to the
/// load/store.
template <typename LoadOrStoreOp>
static SmallVector<Value *, 8>
emitAndReturnLoadStoreOperands(LoadOrStoreOp loadOrStoreOp, ViewOp viewOp) {
  unsigned storeDim = 0;
  SmallVector<Value *, 8> operands;
  for (auto *indexing : viewOp.getIndexings()) {
    if (indexing->getType().isa<IndexType>()) {
      operands.push_back(indexing);
      continue;
    }
    RangeOp range = cast<RangeOp>(indexing->getDefiningOp());
    ValueHandle min(range.getMin());
    Value *storeIndex = *(loadOrStoreOp.getIndices().begin() + storeDim++);
    using edsc::op::operator+;
    operands.push_back(min + ValueHandle(storeIndex));
  }
  return operands;
}

namespace {

/// Rewriting linalg::LoadOp and linalg::StoreOp to mlir::LoadOp and
/// mlir::StoreOp requires finding the proper indexing in the supporting MemRef.
/// This is most easily achieved by calling emitAndReturnFullyComposedView to
/// fold away all the SliceOp.
template <typename LoadOrStoreOpTy>
struct Rewriter : public OpRewritePattern<LoadOrStoreOpTy> {
  using OpRewritePattern<LoadOrStoreOpTy>::OpRewritePattern;

  /// Performs the rewrite.
  PatternMatchResult matchAndRewrite(LoadOrStoreOpTy op,
                                     PatternRewriter &rewriter) const override;
};

struct LowerLinalgLoadStorePass
    : public FunctionPass<LowerLinalgLoadStorePass> {
  void runOnFunction() {
    OwningRewritePatternList patterns;
    auto *context = &getContext();
    patterns.insert<Rewriter<linalg::LoadOp>, Rewriter<linalg::StoreOp>>(
        context);
    applyPatternsGreedily(getFunction(), patterns);
  }
};

template <>
PatternMatchResult
Rewriter<linalg::LoadOp>::matchAndRewrite(linalg::LoadOp load,
                                          PatternRewriter &rewriter) const {
  SliceOp slice = dyn_cast<SliceOp>(load.getView()->getDefiningOp());
  ViewOp view = slice ? emitAndReturnFullyComposedView(slice.getResult())
                      : cast<ViewOp>(load.getView()->getDefiningOp());
  OpBuilder builder(load);
  ScopedContext scope(builder, load.getLoc());
  auto *memRef = view.getSupportingMemRef();
  auto operands = emitAndReturnLoadStoreOperands(load, view);
  rewriter.replaceOpWithNewOp<mlir::LoadOp>(load, memRef, operands);
  return matchSuccess();
}

template <>
PatternMatchResult
Rewriter<linalg::StoreOp>::matchAndRewrite(linalg::StoreOp store,
                                           PatternRewriter &rewriter) const {
  SliceOp slice = dyn_cast<SliceOp>(store.getView()->getDefiningOp());
  ViewOp view = slice ? emitAndReturnFullyComposedView(slice.getResult())
                      : cast<ViewOp>(store.getView()->getDefiningOp());
  OpBuilder builder(store);
  ScopedContext scope(builder, store.getLoc());
  auto *valueToStore = store.getValueToStore();
  auto *memRef = view.getSupportingMemRef();
  auto operands = emitAndReturnLoadStoreOperands(store, view);
  rewriter.replaceOpWithNewOp<mlir::StoreOp>(store, valueToStore, memRef,
                                             operands);
  return matchSuccess();
}
} // namespace

std::unique_ptr<FunctionPassBase> linalg::createLowerLinalgLoadStorePass() {
  return std::make_unique<LowerLinalgLoadStorePass>();
}
OpenPOWER on IntegriCloud