tvm_tirx::ir::prim

Module op

Source
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. An ExprResult is an IntoExpr, so it passes into any function or constructor that takes one, whose ? then reports the error; PrimExpr::try_from takes the expression out without a further check, and Expr::try_from takes it out as an Expr.

Enums§

  • The value of a constant, as C++ MakeConst takes 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_OVERLOAD and friends): an i32 or i64 is MakeConst(other.ty(), v), an f32 a float32 constant, an f64 a float64 one. A literal that meets no expression is the constant From makes of it.
  • The C++ PrimExpr operations that are functions rather than Rust operators, as methods of an ExprResult or an expression handle. Each takes an expression or a literal on the right, typed as for the operators, and leaves self usable; the comparisons are named after the C++ functions (less, equal), since Rust’s eq and lt return a bool.

Functions§

  • The absolute value of x: select(x >= 0, x, -x) for a signed integer, a call of prim.fabs for a float, and an unsigned integer itself; a constant folds (C++ abs, prim.abs).
  • abs at loc.
  • The arc cosine of the float x (C++ acos, prim.acos).
  • acos at loc.
  • The inverse hyperbolic cosine of the float x (C++ acosh, prim.acosh).
  • acosh at loc.
  • a + b (C++ add, prim._OpAdd).
  • add at loc.
  • The conjunction of conds, which must not be empty (prim.all).
  • all at loc.
  • The disjunction of conds, which must not be empty (prim.any).
  • any at loc.
  • The arc sine of the float x (C++ asin, prim.asin).
  • asin at loc.
  • The inverse hyperbolic sine of the float x (C++ asinh, prim.asinh).
  • asinh at loc.
  • A statement that cond holds, for simplifications (C++ assume, prim.assume).
  • assume at loc.
  • 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).
  • atan2 at loc.
  • atan at loc.
  • The inverse hyperbolic tangent of the float x (C++ atanh, prim.atanh).
  • atanh at loc.
  • a & b (C++ bitwise_and, prim.bitwise_and).
  • bitwise_and at loc.
  • The node ~a of an integer or boolean, which, unlike the other operators, neither promotes nor folds (C++ bitwise_neg, which checks the operand before BitwiseNot::new does).
  • bitwise_not at loc.
  • a | b (C++ bitwise_or, prim.bitwise_or).
  • bitwise_or at loc.
  • a ^ b (C++ bitwise_xor, prim.bitwise_xor).
  • bitwise_xor at loc.
  • value converted to ty (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 in ty, 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 a Cast node, which Cast::new builds in every case.
  • cast at loc.
  • x rounded up (C++ ceil, prim.ceil).
  • ceil at loc.
  • a / b, rounding up, of two integers: floordiv(a + b - 1, b) (C++ ceildiv, prim._OpCeilDiv).
  • ceildiv at loc.
  • The number of leading zero bits of x, a call typed as x (C++ clz, prim.clz).
  • clz at loc.
  • value as an expression: a Rust bool, integer or float becomes a constant of the default type, and an expression is returned as is (prim.convert).
  • x with the sign of y (C++ copysign, prim.copysign).
  • copysign at loc.
  • The cosine of the float x (C++ cos, prim.cos).
  • cos at loc.
  • The hyperbolic cosine of the float x (C++ cosh, prim.cosh).
  • cosh at loc.
  • a / b; integer division rounds towards zero, as in C (C++ div, prim._OpDiv).
  • div at loc.
  • The boolean a == b; two vscale() calls are equal (C++ equal, prim._OpEQ).
  • eq at loc.
  • The error function of x (C++ erf, prim.erf).
  • erf at loc.
  • e to the power x (C++ exp, prim.exp).
  • 2 to the power x (C++ exp2, prim.exp2).
  • exp2 at loc.
  • 10 to the power x (C++ exp10, prim.exp10).
  • exp10 at loc.
  • exp at loc.
  • x rounded down (C++ floor, prim.floor).
  • floor at loc.
  • a / b, rounding down, of two integers (C++ floordiv, prim._OpFloorDiv).
  • floordiv at loc.
  • The remainder of floordiv, with the sign of b (C++ floormod, prim._OpFloorMod).
  • floormod at loc.
  • x * y + z in one rounding, a call typed as x (C++ fma, prim.fma).
  • fma at loc.
  • The floating-point remainder of x / y, of two matched floats (C++ fmod).
  • fmod at loc.
  • The boolean a >= b (C++ greater_equal, prim._OpGE).
  • ge at loc.
  • The boolean a > b (C++ greater, prim._OpGT).
  • gt at loc.
  • sqrt(x * x + y * y) (C++ hypot, prim.hypot).
  • hypot at loc.
  • true_value where cond holds and false_value elsewhere, evaluating only the chosen one; a constant condition folds (C++ if_then_else, prim._OpIfThenElse).
  • if_then_else at loc.
  • a / b over non-negative indices, which is floordiv (C++ indexdiv, prim._OpIndexDiv).
  • indexdiv at loc.
  • a % b over non-negative indices, which is floormod (C++ indexmod, prim._OpIndexMod).
  • indexmod at loc.
  • The positive infinity of the scalar float type ty (C++ infinity, prim.infinity).
  • infinity at loc.
  • Whether x is finite: !isinf(x) && !isnan(x) (C++ isfinite, prim.isfinite).
  • isfinite at loc.
  • Whether x is infinite: false for an integer, abs(x) == inf && !isnan(x) for a float (C++ isinf, prim.isinf).
  • isinf at loc.
  • Whether x is NaN, a call typed by its FInferType (C++ isnan, prim.isnan).
  • isnan at loc.
  • x * 2 ^ y (C++ ldexp, prim.ldexp).
  • ldexp at loc.
  • The boolean a <= b (C++ less_equal, prim._OpLE).
  • le at loc.
  • cond, marked as likely to hold; an integer constant is itself (C++ likely, prim.likely).
  • likely at loc.
  • 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).
  • log1p at loc.
  • log2 at loc.
  • The base-10 logarithm of x (C++ log10, prim.log10).
  • log10 at loc.
  • log at loc.
  • a && b of two booleans, which are not matched; a constant side folds (C++ logical_and, prim._OpAnd).
  • logical_and at loc.
  • !a of a boolean; a constant folds (C++ logical_not).
  • logical_not at loc.
  • a || b of two booleans, which are not matched; a constant side folds (C++ logical_or, prim._OpOr).
  • logical_or at loc.
  • The boolean a < b (C++ less, prim._OpLT).
  • lt at loc.
  • The constant value of type ty: an IntImm of an integer or boolean type, a FloatImm of 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. Prefer IntImm::from_dtype or FloatImm::from_dtype when the kind of the type is known.
  • make_const at loc.
  • a and b brought 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 type PromoteBinaryOpType picks (the wider float, a float over an integer, the wider integer, unsigned over signed of the same width).
  • match_types with the casts at loc.
  • The larger of a and b; a symbolic infinity on either side decides it (C++ max, prim._OpMax).
  • max at loc.
  • The largest value of the scalar type ty (C++ max_value, prim.max_value).
  • max_value at loc.
  • The smaller of a and b; a symbolic infinity on either side decides it (C++ min, prim._OpMin).
  • min at loc.
  • The smallest value of the scalar type ty (C++ min_value, prim.min_value).
  • min_value at loc.
  • a * b (C++ mul, prim._OpMul).
  • mul at loc.
  • The boolean a != b (C++ not_equal, prim._OpNE).
  • ne at loc.
  • x rounded to the nearest integer under the current rounding mode, a tie to even (C++ nearbyint, prim.nearbyint).
  • nearbyint at loc.
  • -a: a constant is negated in place when the result is a value of its type, anything else is MakeConst(a.ty(), 0) - a (C++ neg).
  • neg at loc.
  • The next representable value after x towards y (C++ nextafter, prim.nextafter).
  • nextafter at loc.
  • The number of set bits of x (C++ popcount, prim.popcount).
  • popcount at loc.
  • x to the power y, 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.
  • pow at loc.
  • x rounded to the nearest integer; a constant folds a tie to even, as std::nearbyint does (C++ round, prim.round).
  • round at loc.
  • The reciprocal square root of x (C++ rsqrt, prim.rsqrt).
  • rsqrt at loc.
  • a << b of two integers; two constants fold when the result is a value of their type (C++ left_shift, prim.left_shift).
  • shift_left at loc.
  • a >> b of two integers, sign-extending (C++ right_shift, prim.right_shift).
  • shift_right at loc.
  • The sigmoid of x (C++ sigmoid, prim.sigmoid).
  • sigmoid at loc.
  • The sine of the float x (C++ sin, prim.sin).
  • sin at loc.
  • The hyperbolic sine of the float x (C++ sinh, prim.sinh).
  • sinh at loc.
  • The square root of x (C++ sqrt, prim.sqrt).
  • sqrt at loc.
  • a - b (C++ sub, prim._OpSub).
  • sub at loc.
  • The tangent of the float x (C++ tan, prim.tan).
  • tan at loc.
  • The hyperbolic tangent of the float x (C++ tanh, prim.tanh).
  • tanh at loc.
  • x rounded towards zero (C++ trunc, prim.trunc).
  • trunc at loc.
  • a / b, rounding towards zero, of two integers (C++ truncdiv, prim._OpTruncDiv).
  • truncdiv at loc.
  • The remainder of truncdiv, with the sign of a (C++ truncmod, prim._OpTruncMod).
  • truncmod at loc.
  • The call of prim.vscale, the number of lanes a scalable vector has per vscale, an int32 (C++ Call(PrimType::Int(32), vscale_op(), {})).
  • vscale at loc.