tvm
int_set.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_ARITH_INT_SET_H_
25 #define TVM_ARITH_INT_SET_H_
26 
27 #include <tvm/ir/expr.h>
28 #include <tvm/tir/expr.h>
29 
30 #include <unordered_map>
31 
32 namespace tvm {
33 namespace arith {
34 
35 using tir::IterVar;
36 using tir::Var;
37 using tir::VarNode;
38 
39 class Analyzer;
40 
41 //-----------------------------------------------
42 // Integer set data structure.
43 //
44 // This is a API build on top of the base
45 // integer analysis API to provide set analysis.
46 //------------------------------------------------
51 
57 class IntSetNode : public Object {
58  public:
59  static constexpr const char* _type_key = "IntSet";
60  static constexpr bool _type_has_method_sequal_reduce = false;
62 };
63 
68 class IntSet : public ObjectRef {
69  public:
75  Range CoverRange(Range max_range) const;
77  PrimExpr min() const;
79  PrimExpr max() const;
83  bool IsNothing() const;
85  bool IsEverything() const;
87  bool IsSinglePoint() const;
101  bool CanProveSinglePoint(Analyzer* ana) const;
102  // TODO(tvm-team): update all CanProve to explicitly take
103  // analyzer to encourage more analyzer reuse
105  bool CanProvePositive() const;
107  bool CanProveNegative() const;
109  bool CanProveNonPositive() const;
111  bool CanProveNonNegative() const;
113  bool HasUpperBound() const;
115  bool HasLowerBound() const;
116 
128  bool MatchRange(const tvm::Range& r) const;
130  static IntSet Nothing();
132  static IntSet Everything();
138  static IntSet SinglePoint(PrimExpr point);
144  static IntSet Vector(PrimExpr vec);
165 
167 };
168 
169 //-----------------------------------------------
170 // Integer set legacy API.
171 //------------------------------------------------
178 Map<Var, IntSet> ConvertDomMap(const std::unordered_map<const VarNode*, IntSet>& dom_map);
204 IntSet EvalSet(PrimExpr e, const std::unordered_map<const tir::VarNode*, IntSet>& dom_map);
214 
223 IntSet EvalSet(IntSet s, const std::unordered_map<const VarNode*, IntSet>& dom_map);
231 IntSet EvalSet(Range r, const std::unordered_map<const VarNode*, IntSet>& dom_map);
239 Array<IntSet> EvalSet(const Array<Range>& region, const Map<Var, IntSet>& dom_map);
241 using ExprIntSetMap = std::unordered_map<PrimExpr, IntSet, ObjectPtrHash, ObjectPtrEqual>;
251  const std::unordered_map<const VarNode*, IntSet>& dom_map);
252 
259 
266 
273 
280 
287 
294 
305  const Map<Var, Range>& var_dom,
306  const PrimExpr& predicate,
307  arith::Analyzer* analyzer);
308 
319  const Map<Var, Range>& var_dom,
320  const PrimExpr& predicate,
321  arith::Analyzer* analyzer);
322 
334  const Map<Var, Range>& var_dom,
335  const PrimExpr& predicate,
336  arith::Analyzer* analyzer);
337 
338 } // namespace arith
339 } // namespace tvm
340 #endif // TVM_ARITH_INT_SET_H_
Reference to PrimExprNode.
Definition: expr.h:115
Range container
Definition: expr.h:725
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:629
Base class of all Integer set containers. represent a set of integers in one dimension.
Definition: int_set.h:57
TVM_DECLARE_BASE_OBJECT_INFO(IntSetNode, Object)
static constexpr const char * _type_key
Definition: int_set.h:59
static constexpr bool _type_has_method_sequal_reduce
Definition: int_set.h:60
Managed reference to IntSetNode.
Definition: int_set.h:68
static IntSet Vector(PrimExpr vec)
construct a integer set from vector expression.
bool MatchRange(const tvm::Range &r) const
Try to match IntSet with range r.
bool IsNothing() const
TVM_DEFINE_OBJECT_REF_METHODS(IntSet, ObjectRef, IntSetNode)
Range CoverRange(Range max_range) const
Find a range that covers the region.
bool HasLowerBound() const
static IntSet SinglePoint(PrimExpr point)
construct a point set.
static IntSet FromMinExtent(PrimExpr min, PrimExpr extent)
Construct a set representing a range [min, min + extent).
bool CanProveNonPositive() const
bool IsSinglePoint() const
bool HasUpperBound() const
bool CanProveSinglePoint(Analyzer *ana) const
Check if we can prove it is a single point.
bool IsEverything() const
bool CanProveNonNegative() const
SignType GetSignType() const
static IntSet Interval(PrimExpr min, PrimExpr max)
Construct a set representing a interval.
static IntSet Nothing()
bool CanProveNegative() const
bool CanProvePositive() const
PrimExpr max() const
static IntSet FromRange(tvm::Range r)
Construct a set representing a range.
static IntSet Everything()
PrimExpr min() const
PrimExpr PointValue() const
The single point value, call only if IsSinglePoint is true.
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Base expr nodes in TVM.
std::unordered_map< PrimExpr, IntSet, ObjectPtrHash, ObjectPtrEqual > ExprIntSetMap
Map from Expr to IntSet.
Definition: int_set.h:241
Optional< Array< IntSet > > EstimateRegionLowerBound(const Array< Range > &region, const Map< Var, Range > &var_dom, const PrimExpr &predicate, arith::Analyzer *analyzer)
Analyze the region with affine map, given the domain of variables and their predicate....
Map< Var, IntSet > ConvertDomMap(const std::unordered_map< const VarNode *, IntSet > &dom_map)
Convert std::unordered_map<const VarNode*, IntSet> to Map<Var, IntSet>
Array< IntSet > UnionRegionLowerBound(const Array< Array< IntSet >> &nd_int_sets)
The union of N-dimensional integer sets.
IntSet Union(const Array< IntSet > &sets)
Create a union set of all sets, possibly relaxed.
IntSet EvalSet(PrimExpr e, const Map< IterVar, IntSet > &dom_map)
Find an symbolic integer set that contains all possible values of e given the domain of each iteratio...
Map< Var, arith::IntSet > AsIntSet(const Map< Var, Range > &var_dom)
Converts the Ranges to IntSets.
Optional< Array< IntSet > > EstimateRegionStrictBound(const Array< Range > &region, const Map< Var, Range > &var_dom, const PrimExpr &predicate, arith::Analyzer *analyzer)
Analyze the region with affine map, given the domain of variables and their predicate....
ExprIntSetMap EvalSetForEachSubExpr(PrimExpr e, const std::unordered_map< const VarNode *, IntSet > &dom_map)
Find the integer set of every sub-expression, given the domain of each iteration variables.
IntSet UnionLowerBound(const Array< IntSet > &sets)
Create a lower-bound of union set, where some of the segments may be dropped.
SignType
Sign type of an integer expression.
Definition: int_set.h:50
@ kNegative
Definition: int_set.h:50
@ kPositive
Definition: int_set.h:50
@ kUnknown
Definition: int_set.h:50
@ kZero
Definition: int_set.h:50
Array< IntSet > UnionRegion(const Array< Array< IntSet >> &nd_int_sets)
The union of N-dimensional integer sets.
IntSet Intersect(const Array< IntSet > &sets)
Create an intersected set of all sets.
Array< IntSet > EstimateRegionUpperBound(const Array< Range > &region, const Map< Var, Range > &var_dom, const PrimExpr &predicate, arith::Analyzer *analyzer)
Analyze the region with affine map, given the domain of variables and their predicate Relaxation of t...
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
TIR expressions.