summaryrefslogtreecommitdiffstats
path: root/mlir/lib/Dialect/Tensor/Transforms/IndependenceTransforms.cpp
blob: 721730862d49b3786a3cb5be67ab69d4c7796102 (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
//===- IndependenceTransforms.cpp - Make ops independent of values --------===//
//
// 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/Tensor/Transforms/Transforms.h"

#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Affine/Transforms/Transforms.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Utils/StaticValueUtils.h"
#include "mlir/Interfaces/ValueBoundsOpInterface.h"

using namespace mlir;
using namespace mlir::tensor;

/// Make the given OpFoldResult independent of all independencies.
static FailureOr<OpFoldResult> makeIndependent(OpBuilder &b, Location loc,
                                               OpFoldResult ofr,
                                               ValueRange independencies) {
  if (ofr.is<Attribute>())
    return ofr;
  Value value = ofr.get<Value>();
  AffineMap boundMap;
  ValueDimList mapOperands;
  if (failed(ValueBoundsConstraintSet::computeIndependentBound(
          boundMap, mapOperands, presburger::BoundType::UB, value,
          /*dim=*/std::nullopt, independencies, /*closedUB=*/true)))
    return failure();
  return mlir::affine::materializeComputedBound(b, loc, boundMap, mapOperands);
}

FailureOr<Value> tensor::buildIndependentOp(OpBuilder &b, tensor::PadOp padOp,
                                            ValueRange independencies) {
  OpBuilder::InsertionGuard g(b);
  b.setInsertionPoint(padOp);
  Location loc = padOp.getLoc();

  // Non-constant padding not supported.
  Value constantPadding = padOp.getConstantPaddingValue();
  if (!constantPadding)
    return failure();

  SmallVector<OpFoldResult> newMixedLow, newMixedHigh;
  for (OpFoldResult ofr : padOp.getMixedLowPad()) {
    auto ub = makeIndependent(b, loc, ofr, independencies);
    if (failed(ub))
      return failure();
    newMixedLow.push_back(*ub);
  }
  for (OpFoldResult ofr : padOp.getMixedHighPad()) {
    auto ub = makeIndependent(b, loc, ofr, independencies);
    if (failed(ub))
      return failure();
    newMixedHigh.push_back(*ub);
  }

  // Return existing tensor::PadOp if nothing has changed.
  if (llvm::equal(padOp.getMixedLowPad(), newMixedLow) &&
      llvm::equal(padOp.getMixedHighPad(), newMixedHigh))
    return padOp.getResult();

  // Create a new tensor::PadOp.
  auto newPadOp = b.create<PadOp>(
      loc, padOp.getResultType(), padOp.getSource(), newMixedLow, newMixedHigh,
      constantPadding, padOp.getNofold(), /*attrs=*/ArrayRef<NamedAttribute>{});

  // Create a tensor::ExtractSliceOp.
  // Reify the result sizes of the old tensor::PadOp.
  ReifiedRankedShapedTypeDims reifiedSizes;
  ReifyRankedShapedTypeOpInterface reifyShapedTypeInterface =
      dyn_cast<ReifyRankedShapedTypeOpInterface>(padOp.getOperation());
  if (failed(reifyShapedTypeInterface.reifyResultShapes(b, reifiedSizes)))
    return failure();
  SmallVector<OpFoldResult> offsets, sizes, strides;
  for (int64_t i = 0, e = padOp.getResultType().getRank(); i < e; ++i) {
    // offset = ub(low_padding) - low_padding
    OpFoldResult prevLow = padOp.getMixedLowPad()[i];
    if (prevLow.is<Attribute>()) {
      offsets.push_back(b.getIndexAttr(0));
    } else {
      offsets.push_back(
          b.create<affine::AffineApplyOp>(
               loc, b.getAffineDimExpr(0) - b.getAffineDimExpr(1),
               std::initializer_list<Value>{newMixedLow[i].get<Value>(),
                                            prevLow.get<Value>()})
              .getResult());
    }
    // size = reified result size
    if (!padOp.getResultType().isDynamicDim(i)) {
      sizes.push_back(b.getIndexAttr(padOp.getResultType().getDimSize(i)));
    } else {
      sizes.push_back(reifiedSizes[0][i]);
    }
    // stride = 1
    strides.push_back(b.getIndexAttr(1));
  }

  return b.create<ExtractSliceOp>(loc, newPadOp, offsets, sizes, strides)
      .getResult();
}

FailureOr<Value> tensor::buildIndependentOp(OpBuilder &b,
                                            tensor::EmptyOp emptyOp,
                                            ValueRange independencies) {
  OpBuilder::InsertionGuard g(b);
  b.setInsertionPoint(emptyOp);
  Location loc = emptyOp.getLoc();

  SmallVector<OpFoldResult> newSizes;
  for (OpFoldResult ofr : emptyOp.getMixedSizes()) {
    auto ub = makeIndependent(b, loc, ofr, independencies);
    if (failed(ub))
      return failure();
    newSizes.push_back(*ub);
  }

  // Return existing tensor::EmptyOp if nothing has changed.
  if (llvm::equal(emptyOp.getMixedSizes(), newSizes))
    return emptyOp.getResult();

  // Create a new tensor::EmptyOp.
  Value newEmptyOp =
      b.create<EmptyOp>(loc, newSizes, emptyOp.getType().getElementType());

  // Create a tensor::ExtractSliceOp.
  SmallVector<OpFoldResult> offsets(newSizes.size(), b.getIndexAttr(0));
  SmallVector<OpFoldResult> strides(newSizes.size(), b.getIndexAttr(1));
  return b
      .create<ExtractSliceOp>(loc, newEmptyOp, offsets, emptyOp.getMixedSizes(),
                              strides)
      .getResult();
}