My Project
AffineMap.h
Go to the documentation of this file.
1 //===- AffineMap.h - MLIR Affine Map Class ----------------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 // Affine maps are mathematical functions which map a list of dimension
10 // identifiers and symbols, to multidimensional affine expressions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_IR_AFFINE_MAP_H
15 #define MLIR_IR_AFFINE_MAP_H
16 
17 #include "mlir/Support/LLVM.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMapInfo.h"
20 
21 namespace mlir {
22 
23 namespace detail {
24 struct AffineMapStorage;
25 } // end namespace detail
26 
27 class AffineExpr;
28 class Attribute;
29 struct LogicalResult;
30 class MLIRContext;
31 
37 class AffineMap {
38 public:
40 
41  AffineMap() : map(nullptr) {}
42  explicit AffineMap(ImplType *map) : map(map) {}
43  AffineMap(const AffineMap &other) : map(other.map) {}
44  AffineMap &operator=(const AffineMap &other) = default;
45 
47  static AffineMap get(MLIRContext *context);
48 
49  static AffineMap get(unsigned dimCount, unsigned symbolCount,
50  ArrayRef<AffineExpr> results);
51 
53  static AffineMap getConstantMap(int64_t val, MLIRContext *context);
54 
56  static AffineMap getMultiDimIdentityMap(unsigned numDims,
57  MLIRContext *context);
58 
65  static AffineMap getPermutationMap(ArrayRef<unsigned> permutation,
66  MLIRContext *context);
67 
68  MLIRContext *getContext() const;
69 
70  explicit operator bool() { return map != nullptr; }
71  bool operator==(AffineMap other) const { return other.map == map; }
72  bool operator!=(AffineMap other) const { return !(other.map == map); }
73 
77  bool isIdentity() const;
78 
80  bool isEmpty() const;
81 
83  bool isSingleConstant() const;
84 
87  int64_t getSingleConstantResult() const;
88 
89  // Prints affine map to 'os'.
90  void print(raw_ostream &os) const;
91  void dump() const;
92 
93  unsigned getNumDims() const;
94  unsigned getNumSymbols() const;
95  unsigned getNumResults() const;
96  unsigned getNumInputs() const;
97 
98  ArrayRef<AffineExpr> getResults() const;
99  AffineExpr getResult(unsigned idx) const;
100 
103  void walkExprs(std::function<void(AffineExpr)> callback) const;
104 
110  AffineMap replaceDimsAndSymbols(ArrayRef<AffineExpr> dimReplacements,
111  ArrayRef<AffineExpr> symReplacements,
112  unsigned numResultDims,
113  unsigned numResultSyms);
114 
117  LogicalResult constantFold(ArrayRef<Attribute> operandConstants,
118  SmallVectorImpl<Attribute> &results) const;
119 
134  AffineMap compose(AffineMap map);
135 
138  bool isProjectedPermutation();
139 
141  bool isPermutation();
142 
144  AffineMap getSubMap(ArrayRef<unsigned> resultPos);
145 
146  friend ::llvm::hash_code hash_value(AffineMap arg);
147 
148 private:
149  ImplType *map;
150 
151  static AffineMap getImpl(unsigned dimCount, unsigned symbolCount,
152  ArrayRef<AffineExpr> results, MLIRContext *context);
153 };
154 
155 // Make AffineExpr hashable.
156 inline ::llvm::hash_code hash_value(AffineMap arg) {
157  return ::llvm::hash_value(arg.map);
158 }
159 
162 
198 
222 
223 inline raw_ostream &operator<<(raw_ostream &os, AffineMap map) {
224  map.print(os);
225  return os;
226 }
227 } // end namespace mlir
228 
229 namespace llvm {
230 
231 // AffineExpr hash just like pointers
232 template <> struct DenseMapInfo<mlir::AffineMap> {
235  return mlir::AffineMap(static_cast<mlir::AffineMap::ImplType *>(pointer));
236  }
239  return mlir::AffineMap(static_cast<mlir::AffineMap::ImplType *>(pointer));
240  }
241  static unsigned getHashValue(mlir::AffineMap val) {
242  return mlir::hash_value(val);
243  }
244  static bool isEqual(mlir::AffineMap LHS, mlir::AffineMap RHS) {
245  return LHS == RHS;
246  }
247 };
248 
249 } // namespace llvm
250 
251 #endif // MLIR_IR_AFFINE_MAP_H
Definition: InferTypeOpInterface.cpp:20
AffineMap inversePermutation(AffineMap map)
Definition: AffineMap.cpp:289
static bool isEqual(mlir::AffineMap LHS, mlir::AffineMap RHS)
Definition: AffineMap.h:244
Definition: PassRegistry.cpp:413
Definition: Attributes.h:129
Definition: LLVM.h:45
AffineMap()
Definition: AffineMap.h:41
bool operator!=(AffineMap other) const
Definition: AffineMap.h:72
Definition: LLVM.h:34
static mlir::AffineMap getEmptyKey()
Definition: AffineMap.h:233
Definition: LogicalResult.h:18
bool operator==(AffineMap other) const
Definition: AffineMap.h:71
Definition: LLVM.h:37
auto map(Fn fun, IterType begin, IterType end) -> SmallVector< typename std::result_of< Fn(decltype(*begin))>::type, 8 >
Map with iterators.
Definition: Functional.h:28
AffineMap(const AffineMap &other)
Definition: AffineMap.h:43
AffineMap concatAffineMaps(ArrayRef< AffineMap > maps)
Definition: AffineMap.cpp:313
inline ::llvm::hash_code hash_value(AffineMap arg)
Definition: AffineMap.h:156
Definition: AffineExpr.h:66
static mlir::AffineMap getTombstoneKey()
Definition: AffineMap.h:237
Definition: AffineMap.h:37
static unsigned getHashValue(mlir::AffineMap val)
Definition: AffineMap.h:241
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:201
void print(raw_ostream &os) const
Definition: AsmPrinter.cpp:2097
AffineMap simplifyAffineMap(AffineMap map)
Simplify an affine map by simplifying its underlying AffineExpr results.
Definition: AffineMap.cpp:280
Definition: MLIRContext.h:34
void print(OpAsmPrinter &p, AffineIfOp op)
Definition: AffineOps.cpp:1671
Definition: AffineMapDetail.h:23
raw_ostream & operator<<(raw_ostream &os, SubViewOp::Range &range)
Definition: Ops.cpp:2759
AffineMap(ImplType *map)
Definition: AffineMap.h:42