1 //===- Utils.cpp - Utilities to support the Tensor dialect ----------------===//
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 // This file implements utilities for the Tensor dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/Tensor/Utils/Utils.h"
14 
15 #include "mlir/Dialect/Affine/IR/AffineOps.h"
16 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
17 
18 using namespace mlir;
19 using namespace mlir::tensor;
20 
21 PadOp mlir::tensor::createPadScalarOp(Type type, Value source, Value pad,
22                                       ArrayRef<OpFoldResult> low,
23                                       ArrayRef<OpFoldResult> high, bool nofold,
24                                       Location loc, OpBuilder &builder) {
25   auto padTensorOp =
26       builder.create<PadOp>(loc, type, source, low, high, nofold);
27   int rank = padTensorOp.getResultType().getRank();
28   SmallVector<Type, 4> blockArgTypes(rank, builder.getIndexType());
29   SmallVector<Location, 4> blockArgLocs(rank, loc);
30   auto &region = padTensorOp.region();
31   // `builder.createBlock` changes the insertion point within the block. Create
32   // a guard to reset the insertion point of the builder after it is destroyed.
33   OpBuilder::InsertionGuard guard(builder);
34   builder.createBlock(&region, region.end(), blockArgTypes, blockArgLocs);
35   builder.create<YieldOp>(loc, pad);
36   return padTensorOp;
37 }
38 
39 PadOp mlir::tensor::createPadHighOp(Type type, Value source, Value pad,
40                                     bool nofold, Location loc, OpBuilder &b) {
41   SmallVector<OpFoldResult, 4> low, high;
42   auto rankedTensorType = type.cast<RankedTensorType>();
43   assert(rankedTensorType.hasStaticShape());
44   for (const auto &en : enumerate(rankedTensorType.getShape())) {
45     AffineExpr d0;
46     bindDims(b.getContext(), d0);
47     auto dimOp = b.createOrFold<tensor::DimOp>(loc, source, en.index());
48     Value paddingWidth =
49         makeComposedAffineApply(b, loc, en.value() - d0, {dimOp});
50     high.push_back(paddingWidth);
51     low.push_back(b.createOrFold<arith::ConstantIndexOp>(loc, 0));
52   }
53   return createPadScalarOp(type, source, pad, low, high, nofold, loc, b);
54 }
55