tvm
constant_utils.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_TOPI_DETAIL_CONSTANT_UTILS_H_
25 #define TVM_TOPI_DETAIL_CONSTANT_UTILS_H_
26 
27 #include <tvm/arith/analyzer.h>
28 #include <tvm/runtime/logging.h>
29 #include <tvm/te/operation.h>
30 #include <tvm/tirx/analysis.h>
31 #include <tvm/tirx/expr.h>
32 
33 #include <string>
34 #include <vector>
35 
36 namespace tvm {
37 namespace topi {
38 namespace detail {
39 
40 using namespace tvm::te;
41 
49 inline bool IsConstInt(PrimExpr expr) { return expr->IsInstance<tvm::tirx::IntImmNode>(); }
50 
59 inline bool IsConstIntArray(ffi::Array<PrimExpr> array) {
60  bool is_const_int = true;
61  for (auto const& elem : array) {
62  is_const_int &= !elem.defined() || elem->IsInstance<tvm::tirx::IntImmNode>();
63  }
64  return is_const_int;
65 }
66 
75 inline int64_t GetConstInt(PrimExpr expr) {
76  if (expr->IsInstance<tvm::IntImmNode>()) {
77  return expr.as<tvm::IntImmNode>()->value;
78  }
79  LOG(ERROR) << "expr must be a constant integer";
80  return -1;
81 }
82 
92 inline std::vector<int> GetConstIntValues(ffi::Array<PrimExpr> exprs, const std::string& var_name) {
93  std::vector<int> result;
94  if (!exprs.defined()) return result;
95  for (auto expr : exprs) {
96  TVM_FFI_ICHECK(IsConstInt(expr))
97  << "All elements of " << var_name << " must be constant integers";
98  result.push_back(GetConstInt(expr));
99  }
100  return result;
101 }
102 
112 inline std::vector<int64_t> GetConstInt64Values(ffi::Array<PrimExpr> exprs,
113  const std::string& var_name) {
114  std::vector<int64_t> result;
115  if (!exprs.defined()) return result;
116  for (auto expr : exprs) {
117  TVM_FFI_ICHECK(IsConstInt(expr))
118  << "All elements of " << var_name << " must be constant integers";
119  result.push_back(GetConstInt(expr));
120  }
121  return result;
122 }
123 
132 inline bool EqualCheck(PrimExpr lhs, PrimExpr rhs) {
133  tvm::tirx::ExprDeepEqual expr_equal;
134  bool result = expr_equal(lhs, rhs);
135  if (!result) {
136  PrimExpr t = tvm::arith::Analyzer().Simplify(lhs - rhs);
137  if (const IntImmNode* i = t.as<IntImmNode>()) {
138  result = i->value == 0;
139  }
140  }
141  return result;
142 }
143 
144 } // namespace detail
145 } // namespace topi
146 } // namespace tvm
147 #endif // TVM_TOPI_DETAIL_CONSTANT_UTILS_H_
Algebra expression simplifications.
Constant integer literals in the program.
Definition: expr.h:494
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:635
PrimExpr Simplify(const PrimExpr &expr, int steps=2)
Simplify expr.
Tensor expression language DSL.
Definition: extracted_task.h:32
bool is_const_int(const PrimExpr &x, int64_t value)
Check whether x is a constant integer expression.
Definition: op.h:965
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
Operation node can generate one or multiple Tensors.
Compare two expressions recursively and check if they are equal to each other without var remapping.
Definition: analysis.h:61
Analysis utilities and passes for TIR.
TIR expressions.