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/tirx/expr.h>
29 
30 #include <unordered_map>
31 
32 namespace tvm {
33 namespace arith {
34 
35 using tirx::IterVar;
36 using tirx::Var;
37 using tirx::VarNode;
38 
39 class AnalyzerObj;
40 class Analyzer;
41 
42 //-----------------------------------------------
43 // Integer set data structure.
44 //
45 // This is a API build on top of the base
46 // integer analysis API to provide set analysis.
47 //------------------------------------------------
52 
58 class IntSetNode : public ffi::Object {
59  public:
60  TVM_FFI_DECLARE_OBJECT_INFO("ir.IntSet", IntSetNode, ffi::Object);
61 };
62 
67 class IntSet : public ffi::ObjectRef {
68  public:
74  Range CoverRange(Range max_range) const;
76  PrimExpr min() const;
78  PrimExpr max() const;
82  bool IsNothing() const;
84  bool IsEverything() const;
86  bool IsSinglePoint() const;
100  bool CanProveSinglePoint(const Analyzer& ana) const;
101  // TODO(tvm-team): update all CanProve to explicitly take
102  // analyzer to encourage more analyzer reuse
104  bool CanProvePositive() const;
106  bool CanProveNegative() const;
108  bool CanProveNonPositive() const;
110  bool CanProveNonNegative() const;
112  bool HasUpperBound() const;
114  bool HasLowerBound() const;
115 
127  bool MatchRange(const tvm::Range& r) const;
129  static IntSet Nothing();
131  static IntSet Everything();
137  static IntSet SinglePoint(PrimExpr point);
143  static IntSet Vector(PrimExpr vec);
164 
166 };
167 
168 //-----------------------------------------------
169 // Integer set legacy API.
170 //------------------------------------------------
177 ffi::Map<Var, IntSet> ConvertDomMap(const std::unordered_map<const VarNode*, IntSet>& dom_map);
186 IntSet EvalSet(PrimExpr e, const ffi::Map<IterVar, IntSet>& dom_map);
195 IntSet EvalSet(PrimExpr e, const ffi::Map<Var, IntSet>& dom_map);
203 IntSet EvalSet(PrimExpr e, const std::unordered_map<const tirx::VarNode*, IntSet>& dom_map);
212 IntSet EvalSet(Range r, const ffi::Map<IterVar, IntSet>& dom_map);
213 
222 IntSet EvalSet(IntSet s, const std::unordered_map<const VarNode*, IntSet>& dom_map);
230 IntSet EvalSet(Range r, const std::unordered_map<const VarNode*, IntSet>& dom_map);
238 ffi::Array<IntSet> EvalSet(const ffi::Array<Range>& region, const ffi::Map<Var, IntSet>& dom_map);
240 using ExprIntSetMap = std::unordered_map<PrimExpr, IntSet, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>;
250  const std::unordered_map<const VarNode*, IntSet>& dom_map);
251 
257 IntSet Union(const ffi::Array<IntSet>& sets);
258 
264 ffi::Array<IntSet> UnionRegion(const ffi::Array<ffi::Array<IntSet>>& nd_int_sets);
265 
271 IntSet UnionLowerBound(const ffi::Array<IntSet>& sets);
272 
278 ffi::Array<IntSet> UnionRegionLowerBound(const ffi::Array<ffi::Array<IntSet>>& nd_int_sets);
279 
285 IntSet Intersect(const ffi::Array<IntSet>& sets);
286 
292 ffi::Map<Var, arith::IntSet> AsIntSet(const ffi::Map<Var, Range>& var_dom);
293 
304 TVM_DLL ffi::Optional<ffi::Array<IntSet>> EstimateRegionStrictBound(
305  const ffi::Array<Range>& region, const ffi::Map<Var, Range>& var_dom, const PrimExpr& predicate,
306  const arith::Analyzer& analyzer);
307 
318 TVM_DLL ffi::Optional<ffi::Array<IntSet>> EstimateRegionLowerBound(
319  const ffi::Array<Range>& region, const ffi::Map<Var, Range>& var_dom, const PrimExpr& predicate,
320  const arith::Analyzer& analyzer);
321 
332 TVM_DLL ffi::Array<IntSet> EstimateRegionUpperBound(const ffi::Array<Range>& region,
333  const ffi::Map<Var, Range>& var_dom,
334  const PrimExpr& predicate,
335  const arith::Analyzer& analyzer);
336 
337 } // namespace arith
338 } // namespace tvm
339 #endif // TVM_ARITH_INT_SET_H_
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Range container
Definition: expr.h:484
Managed reference to AnalyzerObj.
Definition: analyzer.h:913
Base class of all Integer set containers. represent a set of integers in one dimension.
Definition: int_set.h:58
TVM_FFI_DECLARE_OBJECT_INFO("ir.IntSet", IntSetNode, ffi::Object)
Managed reference to IntSetNode.
Definition: int_set.h:67
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
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).
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IntSet, ffi::ObjectRef, IntSetNode)
bool CanProveNonPositive() const
bool IsSinglePoint() const
bool HasUpperBound() const
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
bool CanProveSinglePoint(const Analyzer &ana) const
Check if we can prove it is a single point.
PrimExpr PointValue() const
The single point value, call only if IsSinglePoint is true.
Base expr nodes in TVM.
std::unordered_map< PrimExpr, IntSet, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > ExprIntSetMap
Map from Expr to IntSet.
Definition: int_set.h:240
ffi::Map< Var, arith::IntSet > AsIntSet(const ffi::Map< Var, Range > &var_dom)
Converts the Ranges to IntSets.
ffi::Array< IntSet > EstimateRegionUpperBound(const ffi::Array< Range > &region, const ffi::Map< Var, Range > &var_dom, const PrimExpr &predicate, const arith::Analyzer &analyzer)
Analyze the region with affine map, given the domain of variables and their predicate Relaxation of t...
IntSet UnionLowerBound(const ffi::Array< IntSet > &sets)
Create a lower-bound of union set, where some of the segments may be dropped.
ffi::Array< IntSet > UnionRegionLowerBound(const ffi::Array< ffi::Array< IntSet >> &nd_int_sets)
The union of N-dimensional integer sets.
ffi::Optional< ffi::Array< IntSet > > EstimateRegionLowerBound(const ffi::Array< Range > &region, const ffi::Map< Var, Range > &var_dom, const PrimExpr &predicate, const 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.
ffi::Optional< ffi::Array< IntSet > > EstimateRegionStrictBound(const ffi::Array< Range > &region, const ffi::Map< Var, Range > &var_dom, const PrimExpr &predicate, const arith::Analyzer &analyzer)
Analyze the region with affine map, given the domain of variables and their predicate....
IntSet Union(const ffi::Array< IntSet > &sets)
Create a union set of all sets, possibly relaxed.
IntSet Intersect(const ffi::Array< IntSet > &sets)
Create an intersected set of all sets.
SignType
Sign type of an integer expression.
Definition: int_set.h:51
@ kNegative
Definition: int_set.h:51
@ kPositive
Definition: int_set.h:51
@ kUnknown
Definition: int_set.h:51
@ kZero
Definition: int_set.h:51
ffi::Map< Var, IntSet > ConvertDomMap(const std::unordered_map< const VarNode *, IntSet > &dom_map)
Convert std::unordered_map<const VarNode*, IntSet> to ffi::Map<Var, IntSet>
ffi::Array< IntSet > UnionRegion(const ffi::Array< ffi::Array< IntSet >> &nd_int_sets)
The union of N-dimensional integer sets.
IntSet EvalSet(PrimExpr e, const ffi::Map< IterVar, IntSet > &dom_map)
Find an symbolic integer set that contains all possible values of e given the domain of each iteratio...
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
TIR expressions.