Skip to main content

OpDef

Struct OpDef 

Source
pub struct OpDef(/* private fields */);
Expand description

A chain of steps that registers a new operator.

Each step updates the registry at once, and a failed step skips the rest. The chain completes when it is dropped, panicking with the error of a failed step, or by OpDef::op, which returns it. Register an operator before building IR with it, as a tvm_ffi::static_init_block! does.

A hook, such as the one OpDef::set_infer_type sets, reports failure through its Result; a panic in it aborts the process, since it would unwind into native code.

§Examples

use tvm_tirx::ir::op_attr::CALL_EFFECT_KIND;
use tvm_tirx::ir::sig::{self, IntExpr};
use tvm_tirx::ir::{
    Call, CallEffectKind, CallObj, Op, OpDef, PrimType, PrimVar, Type, Var, UNKNOWN_LOC,
};
use tvm_tirx::script::script;
use tvm_tirx::tvm_ffi::{self, Result};

fn first_arg_type(call: &CallObj) -> Result<Type> {
    Ok(call.args[0].ty.clone())
}

tvm_ffi::static_init_block! {
    OpDef::new("my_ext.clamp", "Clamp an index to a bound.")
        .signature((sig::arg::<IntExpr>("index", ""), sig::arg::<IntExpr>("bound", "")))
        .set_infer_type(first_arg_type)
        .set_attr(CALL_EFFECT_KIND, CallEffectKind::Pure);
}

let clamp = Op::get("my_ext.clamp")?;
let i = PrimVar::new("i", PrimType::int(32), UNKNOWN_LOC);
let call = Call::new(None, clamp, [&i, &i], None, Vec::<Type>::new(), UNKNOWN_LOC)?;
assert_eq!(script(&call.ty)?, "T.int32");
call.validate()?;

Implementations§

Source§

impl OpDef

Source

pub fn new(name: &str, doc: &str) -> Self

Register a new operator name and start its chain (ir.RegisterOp).

The indentation doc shares is removed. A name already registered fails the chain with an AttributeError.

Source

pub fn op(self) -> Result<Op>

Complete the chain now, instead of when it is dropped, and return the operator.

§Errors

Returns the error of the step that failed, which dropping the chain would panic with, or of the ir.expose_op receiver.

Source

pub fn signature<S: Specs>(self, specs: S) -> Self

Set the signature of the operator to specs, a descriptor of sig or a tuple of them in argument order.

The validator generated from it checks a Call’s argument counts, argument types and attrs, unless a custom validator is set.

Source

pub fn add_arg(self, name: &str, doc: &str) -> Self

Append the description of the value argument name, without changing what the validator checks.

Source

pub fn add_ty_arg(self, name: &str, doc: &str) -> Self

Append the description of the type argument name, without changing what the validator checks.

Source

pub fn attrs_type_key(self, key: &str) -> Self

Set the type of Call.attrs to the type registered as key; an unregistered key fails the chain with an InternalError.

Source

pub fn set_attr<T: Into<Any>>( self, key: AttrKey<T>, value: impl Into<T>, ) -> Self

Register the value of the attribute key for the operator; a value already registered fails the chain with a ValueError, and an opaque pointer with a TypeError.

Source

pub fn replace_attr<T: Into<Any>>( self, key: AttrKey<T>, value: impl Into<T>, ) -> Self

Register the value of the attribute key for the operator, replacing a value already registered; an opaque pointer fails the chain with a TypeError.

Source

pub fn reset_attr<T>(self, key: AttrKey<T>) -> Self

Remove the operator’s value of the attribute key; a missing value is ignored, and live OpAttrMaps see the removal.

Source

pub fn set_infer_type<F>(self, infer: F) -> Self
where F: Fn(&CallObj) -> Result<Type> + Copy + 'static,

Set the type inference of the operator, its FInferType, to infer, a function or a closure that captures nothing.

It gives the result type of a Call that has none; op_attr::FIXED_RETURN_TYPE takes precedence. A second one fails the chain with a ValueError.

Source

pub fn set_validator<F>(self, validate: F) -> Self
where F: Fn(&CallObj) -> Result<()> + Copy + 'static,

Set a custom validator of the operator, validate, a function or a closure that captures nothing.

Call::validate runs it instead of the generated one. A second one fails the chain with a ValueError.

Source

pub fn set_region_body_params<F>(self, body_params: F) -> Self
where F: Fn(&CallObj) -> Result<Array<Var>> + Copy + 'static,

Set the body parameters of the operator’s regions, its FRegionGetBodyParams, to body_params, a function or a closure that captures nothing; this makes the operator a region operation.

The RegionStmt constructor checks a region’s body parameters against it. A second hook fails the chain with a ValueError.

Source

pub fn set_region_validate<F>(self, validate: F) -> Self
where F: Fn(&RegionStmtObj) -> Result<()> + Copy + 'static,

Set the check of the operator’s regions, its FRegionValidate, to validate, a function or a closure that captures nothing.

The RegionStmt constructor runs it on a complete region statement. A second check fails the chain with a ValueError.

Trait Implementations§

Source§

impl Drop for OpDef

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for OpDef

§

impl RefUnwindSafe for OpDef

§

impl Send for OpDef

§

impl Sync for OpDef

§

impl Unpin for OpDef

§

impl UnsafeUnpin for OpDef

§

impl UnwindSafe for OpDef

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeSubsumes<T> for T
where T: ?Sized,