1//===- IndependenceTransforms.cpp - Make ops independent of values --------===//
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#include "mlir/Dialect/Tensor/Transforms/Transforms.h"
10
11#include "mlir/Dialect/Affine/IR/AffineOps.h"
12#include "mlir/Dialect/Affine/Transforms/Transforms.h"
13#include "mlir/Dialect/Tensor/IR/Tensor.h"
14#include "mlir/Interfaces/ValueBoundsOpInterface.h"
15
16using namespace mlir;
17using namespace mlir::tensor;
18
19/// Make the given OpFoldResult independent of all independencies.
20static FailureOr<OpFoldResult> makeIndependent(OpBuilder &b, Location loc,
21 OpFoldResult ofr,
22 ValueRange independencies) {
23 if (isa<Attribute>(Val: ofr))
24 return ofr;
25 Value value = cast<Value>(Val&: ofr);
26 AffineMap boundMap;
27 ValueDimList mapOperands;
28 if (failed(Result: ValueBoundsConstraintSet::computeIndependentBound(
29 resultMap&: boundMap, mapOperands, type: presburger::BoundType::UB, var: value,
30 independencies, options: ValueBoundsOptions{/*closedUB=*/true})))
31 return failure();
32 return mlir::affine::materializeComputedBound(b, loc, boundMap, mapOperands);
33}
34
35FailureOr<Value> tensor::buildIndependentOp(OpBuilder &b, tensor::PadOp padOp,
36 ValueRange independencies) {
37 OpBuilder::InsertionGuard g(b);
38 b.setInsertionPoint(padOp);
39 Location loc = padOp.getLoc();
40
41 // Non-constant padding not supported.
42 Value constantPadding = padOp.getConstantPaddingValue();
43 if (!constantPadding)
44 return failure();
45
46 SmallVector<OpFoldResult> newMixedLow, newMixedHigh;
47 for (OpFoldResult ofr : padOp.getMixedLowPad()) {
48 auto ub = makeIndependent(b, loc, ofr, independencies);
49 if (failed(Result: ub))
50 return failure();
51 newMixedLow.push_back(Elt: *ub);
52 }
53 for (OpFoldResult ofr : padOp.getMixedHighPad()) {
54 auto ub = makeIndependent(b, loc, ofr, independencies);
55 if (failed(Result: ub))
56 return failure();
57 newMixedHigh.push_back(Elt: *ub);
58 }
59
60 // Return existing tensor::PadOp if nothing has changed.
61 if (llvm::equal(LRange: padOp.getMixedLowPad(), RRange&: newMixedLow) &&
62 llvm::equal(LRange: padOp.getMixedHighPad(), RRange&: newMixedHigh))
63 return padOp.getResult();
64
65 // Create a new tensor::PadOp.
66 auto newPadOp =
67 PadOp::create(builder&: b, location: loc, resultType: padOp.getResultType(), source: padOp.getSource(),
68 low: newMixedLow, high: newMixedHigh, constantPadValue: constantPadding,
69 nofold: padOp.getNofold(), /*attrs=*/ArrayRef<NamedAttribute>{});
70
71 // Create a tensor::ExtractSliceOp.
72 // Reify the result sizes of the old tensor::PadOp.
73 ReifiedRankedShapedTypeDims reifiedSizes;
74 ReifyRankedShapedTypeOpInterface reifyShapedTypeInterface =
75 dyn_cast<ReifyRankedShapedTypeOpInterface>(Val: padOp.getOperation());
76 if (failed(Result: reifyShapedTypeInterface.reifyResultShapes(builder&: b, reifiedReturnShapes&: reifiedSizes)))
77 return failure();
78 SmallVector<OpFoldResult> offsets, sizes, strides;
79 for (int64_t i = 0, e = padOp.getResultType().getRank(); i < e; ++i) {
80 // offset = ub(low_padding) - low_padding
81 OpFoldResult prevLow = padOp.getMixedLowPad()[i];
82 if (isa<Attribute>(Val: prevLow)) {
83 offsets.push_back(Elt: b.getIndexAttr(value: 0));
84 } else {
85 offsets.push_back(
86 Elt: affine::AffineApplyOp::create(
87 builder&: b, location: loc, exprList: b.getAffineDimExpr(position: 0) - b.getAffineDimExpr(position: 1),
88 mapOperands: std::initializer_list<Value>{cast<Value>(Val&: newMixedLow[i]),
89 cast<Value>(Val&: prevLow)})
90 .getResult());
91 }
92 // size = reified result size
93 if (!padOp.getResultType().isDynamicDim(idx: i)) {
94 sizes.push_back(Elt: b.getIndexAttr(value: padOp.getResultType().getDimSize(idx: i)));
95 } else {
96 sizes.push_back(Elt: reifiedSizes[0][i]);
97 }
98 // stride = 1
99 strides.push_back(Elt: b.getIndexAttr(value: 1));
100 }
101
102 return ExtractSliceOp::create(builder&: b, location: loc, source: newPadOp, offsets, sizes, strides)
103 .getResult();
104}
105
106FailureOr<Value> tensor::buildIndependentOp(OpBuilder &b,
107 tensor::EmptyOp emptyOp,
108 ValueRange independencies) {
109 OpBuilder::InsertionGuard g(b);
110 b.setInsertionPoint(emptyOp);
111 Location loc = emptyOp.getLoc();
112
113 SmallVector<OpFoldResult> newSizes;
114 for (OpFoldResult ofr : emptyOp.getMixedSizes()) {
115 auto ub = makeIndependent(b, loc, ofr, independencies);
116 if (failed(Result: ub))
117 return failure();
118 newSizes.push_back(Elt: *ub);
119 }
120
121 // Return existing tensor::EmptyOp if nothing has changed.
122 if (llvm::equal(LRange: emptyOp.getMixedSizes(), RRange&: newSizes))
123 return emptyOp.getResult();
124
125 // Create a new tensor::EmptyOp.
126 Value newEmptyOp =
127 EmptyOp::create(builder&: b, location: loc, sizes: newSizes, elementType: emptyOp.getType().getElementType());
128
129 // Create a tensor::ExtractSliceOp.
130 SmallVector<OpFoldResult> offsets(newSizes.size(), b.getIndexAttr(value: 0));
131 SmallVector<OpFoldResult> strides(newSizes.size(), b.getIndexAttr(value: 1));
132 return ExtractSliceOp::create(builder&: b, location: loc, source: newEmptyOp, offsets,
133 sizes: emptyOp.getMixedSizes(), strides)
134 .getResult();
135}
136