MLIR  15.0.0git
IntegerSet.h
Go to the documentation of this file.
1 //===- IntegerSet.h - MLIR Integer Set Class --------------------*- C++ -*-===//
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 // Integer sets are sets of points from the integer lattice constrained by
10 // affine equality/inequality constraints. This class is meant to represent
11 // integer sets in the IR - for 'affine.if' operations and as attributes of
12 // other operations. It is typically expected to contain only a handful of
13 // affine constraints, and is immutable like an affine map. Integer sets are not
14 // unique'd unless the number of constraints they contain are below a certain
15 // threshold - although affine expressions that make up its equalities and
16 // inequalities are themselves unique.
17 
18 // This class is not meant for affine analysis and operations like set
19 // operations, emptiness checks, or other math operations for analysis and
20 // transformation. For the latter, use FlatAffineConstraints.
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #ifndef MLIR_IR_INTEGERSET_H
25 #define MLIR_IR_INTEGERSET_H
26 
27 #include "mlir/IR/AffineExpr.h"
28 #include "llvm/ADT/ArrayRef.h"
29 
30 namespace mlir {
31 
32 namespace detail {
33 struct IntegerSetStorage;
34 } // namespace detail
35 
36 class MLIRContext;
37 
38 /// An integer set representing a conjunction of one or more affine equalities
39 /// and inequalities. An integer set in the IR is immutable like the affine map,
40 /// but integer sets are not unique'd unless the number of constraints in them
41 /// is below `kUniquingThreshold`. The affine expressions that make up the
42 /// equalities and inequalities of an integer set are themselves unique and are
43 /// allocated by the bump pointer allocator.
44 class IntegerSet {
45 public:
47 
48  constexpr IntegerSet() {}
49  explicit IntegerSet(ImplType *set) : set(set) {}
50 
51  static IntegerSet get(unsigned dimCount, unsigned symbolCount,
52  ArrayRef<AffineExpr> constraints,
53  ArrayRef<bool> eqFlags);
54 
55  // Returns the canonical empty IntegerSet (i.e. a set with no integer points).
56  static IntegerSet getEmptySet(unsigned numDims, unsigned numSymbols,
57  MLIRContext *context) {
58  auto one = getAffineConstantExpr(1, context);
59  // 1 == 0.
60  return get(numDims, numSymbols, one, true);
61  }
62 
63  /// Returns true if this is the canonical integer set.
64  bool isEmptyIntegerSet() const;
65 
66  /// This method substitutes any uses of dimensions and symbols (e.g.
67  /// dim#0 with dimReplacements[0]) in subexpressions and returns the modified
68  /// integer set. Because this can be used to eliminate dims and
69  /// symbols, the client needs to specify the number of dims and symbols in
70  /// the result. The returned map always has the same number of results.
71  IntegerSet replaceDimsAndSymbols(ArrayRef<AffineExpr> dimReplacements,
72  ArrayRef<AffineExpr> symReplacements,
73  unsigned numResultDims,
74  unsigned numResultSyms);
75 
76  explicit operator bool() { return set; }
77  bool operator==(IntegerSet other) const { return set == other.set; }
78 
79  unsigned getNumDims() const;
80  unsigned getNumSymbols() const;
81  unsigned getNumInputs() const;
82  unsigned getNumConstraints() const;
83  unsigned getNumEqualities() const;
84  unsigned getNumInequalities() const;
85 
86  ArrayRef<AffineExpr> getConstraints() const;
87 
88  AffineExpr getConstraint(unsigned idx) const;
89 
90  /// Returns the equality bits, which specify whether each of the constraints
91  /// is an equality or inequality.
92  ArrayRef<bool> getEqFlags() const;
93 
94  /// Returns true if the idx^th constraint is an equality, false if it is an
95  /// inequality.
96  bool isEq(unsigned idx) const;
97 
98  MLIRContext *getContext() const;
99 
100  /// Walk all of the AffineExpr's in this set's constraints. Each node in an
101  /// expression tree is visited in postorder.
102  void walkExprs(function_ref<void(AffineExpr)> callback) const;
103 
104  void print(raw_ostream &os) const;
105  void dump() const;
106 
107  friend ::llvm::hash_code hash_value(IntegerSet arg);
108 
109  /// Methods supporting C API.
110  const void *getAsOpaquePointer() const {
111  return static_cast<const void *>(set);
112  }
113  static IntegerSet getFromOpaquePointer(const void *pointer) {
114  return IntegerSet(
115  reinterpret_cast<ImplType *>(const_cast<void *>(pointer)));
116  }
117 
118 private:
119  ImplType *set{nullptr};
120 };
121 
122 // Make AffineExpr hashable.
123 inline ::llvm::hash_code hash_value(IntegerSet arg) {
124  return ::llvm::hash_value(arg.set);
125 }
126 
127 } // namespace mlir
128 namespace llvm {
129 
130 // IntegerSet hash just like pointers.
131 template <> struct DenseMapInfo<mlir::IntegerSet> {
133  auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
134  return mlir::IntegerSet(static_cast<mlir::IntegerSet::ImplType *>(pointer));
135  }
138  return mlir::IntegerSet(static_cast<mlir::IntegerSet::ImplType *>(pointer));
139  }
140  static unsigned getHashValue(mlir::IntegerSet val) {
141  return mlir::hash_value(val);
142  }
143  static bool isEqual(mlir::IntegerSet LHS, mlir::IntegerSet RHS) {
144  return LHS == RHS;
145  }
146 };
147 
148 } // namespace llvm
149 #endif // MLIR_IR_INTEGERSET_H
Include the generated interface declarations.
const void * getAsOpaquePointer() const
Methods supporting C API.
Definition: IntegerSet.h:110
static unsigned getHashValue(mlir::IntegerSet val)
Definition: IntegerSet.h:140
Explicitly register a set of "builtin" types.
Definition: CallGraph.h:221
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
Definition: AffineExpr.cpp:514
bool operator==(IntegerSet other) const
Definition: IntegerSet.h:77
constexpr IntegerSet()
Definition: IntegerSet.h:48
IntegerSet(ImplType *set)
Definition: IntegerSet.h:49
static bool isEqual(mlir::IntegerSet LHS, mlir::IntegerSet RHS)
Definition: IntegerSet.h:143
static mlir::IntegerSet getEmptyKey()
Definition: IntegerSet.h:132
Base type for affine expression.
Definition: AffineExpr.h:68
static IntegerSet getFromOpaquePointer(const void *pointer)
Definition: IntegerSet.h:113
static void print(ArrayType type, DialectAsmPrinter &os)
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:240
static mlir::IntegerSet getTombstoneKey()
Definition: IntegerSet.h:136
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
static IntegerSet getEmptySet(unsigned numDims, unsigned numSymbols, MLIRContext *context)
Definition: IntegerSet.h:56
inline ::llvm::hash_code hash_value(IntegerSet arg)
Definition: IntegerSet.h:123
An integer set representing a conjunction of one or more affine equalities and inequalities.
Definition: IntegerSet.h:44