1 //===- Utils.cpp - Utilities to support the Linalg 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 Linalg dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/Arithmetic/Utils/Utils.h"
14 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
15 #include "llvm/ADT/SmallBitVector.h"
16 
17 using namespace mlir;
18 
19 /// Matches a ConstantIndexOp.
20 /// TODO: This should probably just be a general matcher that uses matchConstant
21 /// and checks the operation for an index type.
22 detail::op_matcher<arith::ConstantIndexOp> mlir::matchConstantIndex() {
23   return detail::op_matcher<arith::ConstantIndexOp>();
24 }
25 
26 /// Detects the `values` produced by a ConstantIndexOp and places the new
27 /// constant in place of the corresponding sentinel value.
28 void mlir::canonicalizeSubViewPart(
29     SmallVectorImpl<OpFoldResult> &values,
30     llvm::function_ref<bool(int64_t)> isDynamic) {
31   for (OpFoldResult &ofr : values) {
32     if (ofr.is<Attribute>())
33       continue;
34     // Newly static, move from Value to constant.
35     if (auto cstOp =
36             ofr.dyn_cast<Value>().getDefiningOp<arith::ConstantIndexOp>())
37       ofr = OpBuilder(cstOp).getIndexAttr(cstOp.value());
38   }
39 }
40 
41 llvm::SmallBitVector mlir::getPositionsOfShapeOne(unsigned rank,
42                                                   ArrayRef<int64_t> shape) {
43   llvm::SmallBitVector dimsToProject(shape.size());
44   for (unsigned pos = 0, e = shape.size(); pos < e && rank > 0; ++pos) {
45     if (shape[pos] == 1) {
46       dimsToProject.set(pos);
47       --rank;
48     }
49   }
50   return dimsToProject;
51 }
52 
53 Value mlir::getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc,
54                                             OpFoldResult ofr) {
55   if (auto value = ofr.dyn_cast<Value>())
56     return value;
57   auto attr = ofr.dyn_cast<Attribute>().dyn_cast<IntegerAttr>();
58   assert(attr && "expect the op fold result casts to an integer attribute");
59   return b.create<arith::ConstantIndexOp>(loc, attr.getValue().getSExtValue());
60 }
61 
62 SmallVector<Value>
63 mlir::getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc,
64                                       ArrayRef<OpFoldResult> valueOrAttrVec) {
65   return llvm::to_vector<4>(
66       llvm::map_range(valueOrAttrVec, [&](OpFoldResult value) -> Value {
67         return getValueOrCreateConstantIndexOp(b, loc, value);
68       }));
69 }
70 
71 Value ArithBuilder::_and(Value lhs, Value rhs) {
72   return b.create<arith::AndIOp>(loc, lhs, rhs);
73 }
74 Value ArithBuilder::add(Value lhs, Value rhs) {
75   if (lhs.getType().isa<IntegerType>())
76     return b.create<arith::AddIOp>(loc, lhs, rhs);
77   return b.create<arith::AddFOp>(loc, lhs, rhs);
78 }
79 Value ArithBuilder::mul(Value lhs, Value rhs) {
80   if (lhs.getType().isa<IntegerType>())
81     return b.create<arith::MulIOp>(loc, lhs, rhs);
82   return b.create<arith::MulFOp>(loc, lhs, rhs);
83 }
84 Value ArithBuilder::sgt(Value lhs, Value rhs) {
85   if (lhs.getType().isa<IndexType, IntegerType>())
86     return b.create<arith::CmpIOp>(loc, arith::CmpIPredicate::sgt, lhs, rhs);
87   return b.create<arith::CmpFOp>(loc, arith::CmpFPredicate::OGT, lhs, rhs);
88 }
89 Value ArithBuilder::slt(Value lhs, Value rhs) {
90   if (lhs.getType().isa<IndexType, IntegerType>())
91     return b.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt, lhs, rhs);
92   return b.create<arith::CmpFOp>(loc, arith::CmpFPredicate::OLT, lhs, rhs);
93 }
94 Value ArithBuilder::select(Value cmp, Value lhs, Value rhs) {
95   return b.create<arith::SelectOp>(loc, cmp, lhs, rhs);
96 }
97