Expand description
The primitive operators: arithmetic, comparisons, logic, casts and math functions, written in
Rust after src/ir/prim/op.cc, src/ir/prim/math.cc and include/tvm/ir/prim/op.h.
The functions python/tvm_tirx/ir/prim/op.py defines, with the operators that
python/tvm_tirx/ir/_prim_expr_overload.py gives expressions (a + b is add). Unlike the
node constructors such as Add::new, which take operands of one type and
never fold, these match their operand types (BinaryOpMatchTypes), fold constants
(TryConstFold) and may return any Expr: a node, a constant or an intrinsic call, with the
checks and errors of the C++ functions.
Each function builds at the unknown location, as the C++ default argument does; the _at
variant takes a location. An operand is an expression, an expression under construction (an
ExprResult, see below) or a literal, typed as C++ types it: an integer next to an expression is
MakeConst(other.ty(), v), an f32 a float32 constant, an f64 a float64 one, and a literal
on its own the constant From makes of it (an i32 is an int32):
use tvm_tirx::ir::prim::op::{add, floordiv};
use tvm_tirx::ir::{PrimType, Var, UNKNOWN_LOC};
let x = Var::new("x", PrimType::int(32), UNKNOWN_LOC);
let index = floordiv(&x + 1, 4)?; // C++ `floordiv(x + 1, 4)`
let shifted = add(&x, 1)?;&x + 1 builds an ExprResult: the C++ operators on expression handles, with literals
typed the same way. An ExprResult holds the expression under construction or the
first error any step raised; it passes into any function or constructor that takes
impl IntoExpr, whose ? then reports that error, and Expr::try_from takes the expression out.
The operations Rust has no operator for are the PrimOps methods (x.floordiv(4),
x.less(y), x.cast(ty)).
Structs§
- An expression being built by operators: either the expression, a
PrimExpr, or the first error any step raised. Later steps pass an error through untouched. AnExprResultis anIntoExpr, so it passes into any function or constructor that takes one, whose?then reports the error;PrimExpr::try_fromtakes the expression out without a further check, andExpr::try_fromtakes it out as anExpr.
Enums§
- The value of a constant, as C++
MakeConsttakes it: an integer or a double.
Traits§
- An operand of an operator: an expression, an expression under construction (
ExprResult) or a literal, which is typed after the expression it meets (C++TVM_DEFINE_BINOP_CONST_VAL_OVERLOADand friends): ani32ori64isMakeConst(other.ty(), v), anf32a float32 constant, anf64a float64 one. A literal that meets no expression is the constantFrommakes of it. - The C++
PrimExproperations that are functions rather than Rust operators, as methods of anExprResultor an expression handle. Each takes an expression or a literal on the right, typed as for the operators, and leavesselfusable; the comparisons are named after the C++ functions (less,equal), since Rust’seqandltreturn abool.
Functions§
- The absolute value of
x:select(x >= 0, x, -x)for a signed integer, a call ofprim.fabsfor a float, and an unsigned integer itself; a constant folds (C++abs,prim.abs). absatloc.- The arc cosine of the float
x(C++acos,prim.acos). acosatloc.- The inverse hyperbolic cosine of the float
x(C++acosh,prim.acosh). acoshatloc.a + b(C++add,prim._OpAdd).addatloc.- The conjunction of
conds, which must not be empty (prim.all). allatloc.- The disjunction of
conds, which must not be empty (prim.any). anyatloc.- The arc sine of the float
x(C++asin,prim.asin). asinatloc.- The inverse hyperbolic sine of the float
x(C++asinh,prim.asinh). asinhatloc.- A statement that
condholds, for simplifications (C++assume,prim.assume). assumeatloc.- The arc tangent of the float
x(C++atan,prim.atan). - The arc tangent of
x / y, by the signs of both (C++atan2,prim.atan2). atan2atloc.atanatloc.- The inverse hyperbolic tangent of the float
x(C++atanh,prim.atanh). atanhatloc.a & b(C++bitwise_and,prim.bitwise_and).bitwise_andatloc.- The node
~aof an integer or boolean, which, unlike the other operators, neither promotes nor folds (C++bitwise_neg, which checks the operand beforeBitwiseNot::newdoes). bitwise_notatloc.a | b(C++bitwise_or,prim.bitwise_or).bitwise_oratloc.a ^ b(C++bitwise_xor,prim.bitwise_xor).bitwise_xoratloc.valueconverted toty(C++prim::cast(value, t),prim._cast): a value already of that type is itself, a constant is folded when the result is the same value inty, a scalar cast to a vector type is broadcast, a broadcast is cast element by element, and so is a constant ramp cast to an integer type when every lane fits both types; anything else is aCastnode, whichCast::newbuilds in every case.castatloc.xrounded up (C++ceil,prim.ceil).ceilatloc.a / b, rounding up, of two integers:floordiv(a + b - 1, b)(C++ceildiv,prim._OpCeilDiv).ceildivatloc.- The number of leading zero bits of
x, a call typed asx(C++clz,prim.clz). clzatloc.valueas an expression: a Rustbool, integer or float becomes a constant of the default type, and an expression is returned as is (prim.convert).xwith the sign ofy(C++copysign,prim.copysign).copysignatloc.- The cosine of the float
x(C++cos,prim.cos). cosatloc.- The hyperbolic cosine of the float
x(C++cosh,prim.cosh). coshatloc.a / b; integer division rounds towards zero, as in C (C++div,prim._OpDiv).divatloc.- The boolean
a == b; twovscale()calls are equal (C++equal,prim._OpEQ). eqatloc.- The error function of
x(C++erf,prim.erf). erfatloc.eto the powerx(C++exp,prim.exp).2to the powerx(C++exp2,prim.exp2).exp2atloc.10to the powerx(C++exp10,prim.exp10).exp10atloc.expatloc.xrounded down (C++floor,prim.floor).flooratloc.a / b, rounding down, of two integers (C++floordiv,prim._OpFloorDiv).floordivatloc.floormodatloc.x * y + zin one rounding, a call typed asx(C++fma,prim.fma).fmaatloc.- The floating-point remainder of
x / y, of two matched floats (C++fmod). fmodatloc.- The boolean
a >= b(C++greater_equal,prim._OpGE). geatloc.- The boolean
a > b(C++greater,prim._OpGT). gtatloc.sqrt(x * x + y * y)(C++hypot,prim.hypot).hypotatloc.true_valuewherecondholds andfalse_valueelsewhere, evaluating only the chosen one; a constant condition folds (C++if_then_else,prim._OpIfThenElse).if_then_elseatloc.indexdivatloc.indexmodatloc.- The positive infinity of the scalar float type
ty(C++infinity,prim.infinity). infinityatloc.- Whether
xis finite:!isinf(x) && !isnan(x)(C++isfinite,prim.isfinite). isfiniteatloc.- Whether
xis infinite: false for an integer,abs(x) == inf && !isnan(x)for a float (C++isinf,prim.isinf). isinfatloc.- Whether
xis NaN, a call typed by itsFInferType(C++isnan,prim.isnan). isnanatloc.x * 2 ^ y(C++ldexp,prim.ldexp).ldexpatloc.- The boolean
a <= b(C++less_equal,prim._OpLE). leatloc.cond, marked as likely to hold; an integer constant is itself (C++likely,prim.likely).likelyatloc.- The natural logarithm of
x(C++log,prim.log). - The base-2 logarithm of
x(C++log2,prim.log2). log(1 + x)(C++log1p,prim.log1p).log1patloc.log2atloc.- The base-10 logarithm of
x(C++log10,prim.log10). log10atloc.logatloc.a && bof two booleans, which are not matched; a constant side folds (C++logical_and,prim._OpAnd).logical_andatloc.!aof a boolean; a constant folds (C++logical_not).logical_notatloc.a || bof two booleans, which are not matched; a constant side folds (C++logical_or,prim._OpOr).logical_oratloc.- The boolean
a < b(C++less,prim._OpLT). ltatloc.- The constant
valueof typety: anIntImmof an integer or boolean type, aFloatImmof a floating-point type, and the scalar constant broadcast over the lanes of a vector type (C++prim::MakeConst(ty, value), which has no global function). An unsigned type takes no negative value. PreferIntImm::from_dtypeorFloatImm::from_dtypewhen the kind of the type is known. make_constatloc.aandbbrought to one type, as every binary operator does first (C++BinaryOpMatchTypes(lhs, rhs)): a scalar next to a vector is broadcast to its lanes, and both sides are cast to the typePromoteBinaryOpTypepicks (the wider float, a float over an integer, the wider integer, unsigned over signed of the same width).match_typeswith the casts atloc.- The larger of
aandb; a symbolic infinity on either side decides it (C++max,prim._OpMax). maxatloc.- The largest value of the scalar type
ty(C++max_value,prim.max_value). max_valueatloc.- The smaller of
aandb; a symbolic infinity on either side decides it (C++min,prim._OpMin). minatloc.- The smallest value of the scalar type
ty(C++min_value,prim.min_value). min_valueatloc.a * b(C++mul,prim._OpMul).mulatloc.- The boolean
a != b(C++not_equal,prim._OpNE). neatloc.xrounded to the nearest integer under the current rounding mode, a tie to even (C++nearbyint,prim.nearbyint).nearbyintatloc.-a: a constant is negated in place when the result is a value of its type, anything else isMakeConst(a.ty(), 0) - a(C++neg).negatloc.- The next representable value after
xtowardsy(C++nextafter,prim.nextafter). nextafteratloc.- The number of set bits of
x(C++popcount,prim.popcount). popcountatloc.xto the powery, of two matched floats (C++pow,prim._OpPow). C++ also logs a warning for a constant exponent of 3 or more, which is not repeated here.powatloc.xrounded to the nearest integer; a constant folds a tie to even, asstd::nearbyintdoes (C++round,prim.round).roundatloc.- The reciprocal square root of
x(C++rsqrt,prim.rsqrt). rsqrtatloc.a << bof two integers; two constants fold when the result is a value of their type (C++left_shift,prim.left_shift).shift_leftatloc.a >> bof two integers, sign-extending (C++right_shift,prim.right_shift).shift_rightatloc.- The sigmoid of
x(C++sigmoid,prim.sigmoid). sigmoidatloc.- The sine of the float
x(C++sin,prim.sin). sinatloc.- The hyperbolic sine of the float
x(C++sinh,prim.sinh). sinhatloc.- The square root of
x(C++sqrt,prim.sqrt). sqrtatloc.a - b(C++sub,prim._OpSub).subatloc.- The tangent of the float
x(C++tan,prim.tan). tanatloc.- The hyperbolic tangent of the float
x(C++tanh,prim.tanh). tanhatloc.xrounded towards zero (C++trunc,prim.trunc).truncatloc.a / b, rounding towards zero, of two integers (C++truncdiv,prim._OpTruncDiv).truncdivatloc.truncmodatloc.- The call of
prim.vscale, the number of lanes a scalable vector has pervscale, anint32(C++Call(PrimType::Int(32), vscale_op(), {})). vscaleatloc.