24#ifndef TVM_IR_PRIM_CONST_FOLD_H_
25#define TVM_IR_PRIM_CONST_FOLD_H_
27#include <tvm/ffi/optional.h>
29#include <tvm/runtime/logging.h>
75 return type.code ==
static_cast<uint8_t
>(DLDataTypeCode::kDLInt) &&
76 (type.bits == 32 || type.bits == 64) && type.lanes == 1;
80 TVM_FFI_DCHECK(expr !=
nullptr);
81 TVM_FFI_DCHECK(!expr->ExprObj::ty.as<
MissingType>().has_value());
82 const auto* prim_ty = expr->ExprObj::ty.as<
PrimTypeObj>();
83 TVM_FFI_DCHECK(prim_ty !=
nullptr);
91 if (!dtype.
IsScalar() || dtype.
bits() <= 0)
return false;
92 if (
auto small = value.as<int64_t>()) {
95 (dtype.
bits() >= 63 ||
static_cast<uint64_t
>(*small) < (uint64_t{1} << dtype.
bits()));
99 return *small == 0 || *small == 1;
102 if (dtype.
bits() >= 64)
return true;
103 int64_t limit = int64_t{1} << (dtype.
bits() - 1);
104 return *small >= -limit && *small < limit;
109 return value >= 0 && value < (ffi::BigInt(1) << dtype.
bits());
112 ffi::BigInt limit = ffi::BigInt(1) << (dtype.
bits() - 1);
113 return value >= -limit && value < limit;
120 return !std::isfinite(value) || (value >= std::numeric_limits<float>::lowest() &&
121 value <= std::numeric_limits<float>::max());
126#define TVM_PRIM_CONST_PROPAGATION(BODY) \
127 using tvm::FloatImmObj; \
128 const IntImmObj* pa = a.as<IntImmObj>(); \
129 const IntImmObj* pb = b.as<IntImmObj>(); \
130 const FloatImmObj* fa = a.as<FloatImmObj>(); \
131 const FloatImmObj* fb = b.as<FloatImmObj>(); \
134#define TVM_PRIM_INDEX_CONST_PROPAGATION(BODY) \
135 const IntImmObj* pa = a.as<IntImmObj>(); \
136 const IntImmObj* pb = b.as<IntImmObj>(); \
137 if (::tvm::prim::detail::IsIndexTypedExpr(a) && ::tvm::prim::detail::IsIndexTypedExpr(b)) { \
145 PrimType result_ty = a.ty();
147 ffi::BigInt res = pa->value + pb->value;
149 return IntImm(result_ty, std::move(res));
151 if (pa && pa->value == 0)
return b;
152 if (pb && pb->value == 0)
return a;
154 if (result_ty.bits() == 32) {
155 float lhs =
static_cast<float>(fa->value);
156 float rhs =
static_cast<float>(fb->value);
159 return FloatImm(result_ty,
static_cast<float>(lhs + rhs));
160 }
else if (result_ty.bits() == 64) {
161 return FloatImm(result_ty, fa->value + fb->value);
164 if (fa && fa->value == 0)
return b;
165 if (fb && fb->value == 0)
return a;
173 PrimType result_ty = a.ty();
175 ffi::BigInt res = pa->value - pb->value;
177 return IntImm(result_ty, std::move(res));
179 if (pb && pb->value == 0)
return a;
181 if (result_ty.bits() == 32) {
182 float lhs =
static_cast<float>(fa->value);
183 float rhs =
static_cast<float>(fb->value);
186 return FloatImm(result_ty,
static_cast<float>(lhs - rhs));
187 }
else if (result_ty.bits() == 64) {
188 return FloatImm(result_ty, fa->value - fb->value);
191 if (fb && fb->value == 0)
return a;
199 PrimType result_ty = a.ty();
201 ffi::BigInt res = pa->value * pb->value;
203 return IntImm(result_ty, std::move(res));
206 if (pa->value == 1)
return b;
207 if (pa->value == 0)
return a;
210 if (pb->value == 1)
return a;
211 if (pb->value == 0)
return b;
214 if (result_ty.bits() == 32) {
215 float lhs =
static_cast<float>(fa->value);
216 float rhs =
static_cast<float>(fb->value);
219 return FloatImm(result_ty,
static_cast<float>(lhs * rhs));
220 }
else if (result_ty.bits() == 64) {
221 return FloatImm(result_ty, fa->value * fb->value);
225 if (fa->value == 1)
return b;
226 if (fa->value == 0)
return a;
229 if (fb->value == 1)
return a;
230 if (fb->value == 0)
return b;
239 PrimType result_ty = a.ty();
243 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
244 ffi::BigInt res = pa->value / pb->value;
246 return IntImm(result_ty, std::move(res));
249 if (pa->value == 0)
return a;
252 if (pb->value == 1)
return a;
253 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
256 TVM_FFI_ICHECK_NE(fb->value, 0) <<
"Divide by zero";
257 if (result_ty.bits() == 32) {
258 float lhs =
static_cast<float>(fa->value);
259 float rhs =
static_cast<float>(fb->value);
262 return FloatImm(result_ty,
static_cast<float>(lhs / rhs));
263 }
else if (result_ty.bits() == 64) {
264 return FloatImm(result_ty, fa->value / fb->value);
267 if (fa && fa->value == 0)
return a;
269 if (fb->value == 1)
return a;
270 TVM_FFI_ICHECK_NE(fb->value, 0) <<
"Divide by zero";
279 PrimType result_ty = a.ty();
281 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
282 ffi::BigInt res = pa->value % pb->value;
284 return IntImm(result_ty, std::move(res));
287 if (pa->value == 0)
return a;
290 if (pb->value == 1)
return IntImm(result_ty, 0);
291 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
295 if (pa && pb && a.ty().bits() >= 64) {
296 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
297 ffi::BigInt res = pa->value % pb->value;
299 return IntImm(a.ty(), std::move(res));
307 PrimType result_ty = a.ty();
309 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
310 ffi::BigInt res = ffi::floordiv(pa->value, pb->value);
312 return IntImm(result_ty, std::move(res));
315 if (pa->value == 0)
return a;
318 if (pb->value == 1)
return a;
319 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
321 if (fa && fb && fb->value != 0) {
322 if (result_ty.bits() == 32) {
323 float lhs =
static_cast<float>(fa->value);
324 float rhs =
static_cast<float>(fb->value);
326 float quotient = lhs / rhs;
327 return FloatImm(result_ty, std::floor(quotient));
328 }
else if (result_ty.bits() == 64) {
329 return FloatImm(result_ty, std::floor(fa->value / fb->value));
334 if (fa && fa->value == 0)
return a;
336 if (fb->value == 1)
return a;
337 TVM_FFI_ICHECK_NE(fb->value, 0) <<
"Divide by zero";
346 PrimType result_ty = a.ty();
348 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
349 ffi::BigInt res = ffi::floormod(pa->value, pb->value);
351 return IntImm(result_ty, std::move(res));
354 if (pa->value == 0)
return a;
357 if (pb->value == 1)
return IntImm(result_ty, 0);
358 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
362 if (pa && pb && (a.ty().bits() >= 64 || a.ty().MatchesCode(DLDataTypeCode::kDLUInt))) {
363 TVM_FFI_ICHECK_NE(pb->value, 0) <<
"Divide by zero";
364 ffi::BigInt res = ffi::floormod(pa->value, pb->value);
366 return IntImm(a.ty(), std::move(res));
374 PrimType result_ty = a.ty();
375 if (pa && pb)
return IntImm(result_ty, std::min(pa->value, pb->value));
376 if (fa && fb)
return std::isnan(fa->value) || fa->value < fb->value ? a : b;
378 if (a.same_as(b))
return a;
385 PrimType result_ty = a.ty();
386 if (pa && pb)
return IntImm(result_ty, std::max(pa->value, pb->value));
387 if (fa && fb)
return std::isnan(fa->value) || fa->value > fb->value ? a : b;
389 if (a.same_as(b))
return a;
396 if (pa && pb)
return IntImm::Bool(pa->value > pb->value);
397 if (fa && fb)
return IntImm::Bool(fa->value > fb->value);
405 if (pa && pb)
return IntImm::Bool(pa->value >= pb->value);
406 if (fa && fb)
return IntImm::Bool(fa->value >= fb->value);
414 if (pa && pb)
return IntImm::Bool(pa->value < pb->value);
415 if (fa && fb)
return IntImm::Bool(fa->value < fb->value);
423 if (pa && pb)
return IntImm::Bool(pa->value <= pb->value);
424 if (fa && fb)
return IntImm::Bool(fa->value <= fb->value);
432 if (pa && pb)
return IntImm::Bool(pa->value == pb->value);
433 if (fa && fb)
return IntImm::Bool(fa->value == fb->value);
441 if (pa && pb)
return IntImm::Bool(pa->value != pb->value);
442 if (fa && fb)
return IntImm::Bool(fa->value != fb->value);
451 if (pa && pa->value)
return b;
452 if (pa && !pa->value)
return a;
453 if (pb && pb->value)
return a;
454 if (pb && !pb->value)
return b;
462 if (pa && pa->value)
return a;
463 if (pa && !pa->value)
return b;
464 if (pb && pb->value)
return b;
465 if (pb && !pb->value)
return a;
Base type of all the expressions.
Definition expr.h:66
static IntImm Bool(bool value, Location loc=UnknownLoc())
Construct a scalar boolean constant.
Definition expr.h:351
Managed reference to the MissingTypeObj singleton.
Definition type.h:126
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Primitive data types used in the low-level IR.
Definition type.h:164
TVM_FFI_INLINE int32_t bits() const
Definition type.h:224
TVM_FFI_INLINE bool IsScalar() const
Whether this type is a scalar, excluding fixed and scalable vectors.
Definition type.h:259
TVM_FFI_INLINE bool MatchesCode(Codes... codes) const
Check whether the dtype code matches any of the provided DLPack codes.
Definition type.h:253
#define TVM_PRIM_CONST_PROPAGATION(BODY)
Definition const_fold.h:126
#define TVM_PRIM_INDEX_CONST_PROPAGATION(BODY)
Definition const_fold.h:134
bool IsIndexTypedExpr(const ExprObj *expr)
Definition const_fold.h:79
bool IsIndexType(DLDataType type)
Check whether type is used to represent index.
Definition const_fold.h:74
bool IsInFloat32Range(double value)
Check float32 range, allowing rounding and supported special values.
Definition const_fold.h:119
bool IsRepresentable(const ffi::BigInt &value, const PrimType &dtype)
Whether an exact integer is a valid literal of the given scalar type.
Definition const_fold.h:90
ffi::Optional< PrimExpr > TryConstFold< prim::Sub >(PrimExpr a, PrimExpr b)
Definition const_fold.h:171
ffi::Optional< PrimExpr > TryConstFold< prim::Not >(PrimExpr a)
Definition const_fold.h:470
ffi::Optional< PrimExpr > TryConstFold< prim::EQ >(PrimExpr a, PrimExpr b)
Definition const_fold.h:430
ffi::Optional< PrimExpr > TryConstFold< prim::Max >(PrimExpr a, PrimExpr b)
Definition const_fold.h:383
ffi::Optional< PrimExpr > TryConstFold< prim::GT >(PrimExpr a, PrimExpr b)
Definition const_fold.h:394
ffi::Optional< PrimExpr > TryConstFold< prim::LT >(PrimExpr a, PrimExpr b)
Definition const_fold.h:412
ffi::Optional< PrimExpr > TryConstFold(PrimExpr a, PrimExpr b)
Try to run binary compute with constant folding.
ffi::Optional< PrimExpr > TryConstFold< prim::LE >(PrimExpr a, PrimExpr b)
Definition const_fold.h:421
ffi::Optional< PrimExpr > TryConstFold< prim::Add >(PrimExpr a, PrimExpr b)
Definition const_fold.h:143
ffi::Optional< PrimExpr > TryConstFold< prim::Mul >(PrimExpr a, PrimExpr b)
Definition const_fold.h:197
ffi::Optional< PrimExpr > TryConstFold< prim::NE >(PrimExpr a, PrimExpr b)
Definition const_fold.h:439
ffi::Optional< PrimExpr > TryConstFold< prim::And >(PrimExpr a, PrimExpr b)
Definition const_fold.h:448
ffi::Optional< PrimExpr > TryConstFold< prim::Min >(PrimExpr a, PrimExpr b)
Definition const_fold.h:372
ffi::Optional< PrimExpr > TryConstFold< prim::Mod >(PrimExpr a, PrimExpr b)
Definition const_fold.h:277
ffi::Optional< PrimExpr > TryConstFold< prim::FloorMod >(PrimExpr a, PrimExpr b)
Definition const_fold.h:344
ffi::Optional< PrimExpr > TryConstFold< prim::Div >(PrimExpr a, PrimExpr b)
Definition const_fold.h:237
ffi::Optional< PrimExpr > TryConstFold< prim::GE >(PrimExpr a, PrimExpr b)
Definition const_fold.h:403
ffi::Optional< PrimExpr > TryConstFold< prim::Or >(PrimExpr a, PrimExpr b)
Definition const_fold.h:459
ffi::Optional< PrimExpr > TryConstFold< prim::FloorDiv >(PrimExpr a, PrimExpr b)
Definition const_fold.h:305
tvm::IntImmObj IntImmObj
Definition expr.h:49
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41