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
impl OpDef
Sourcepub fn new(name: &str, doc: &str) -> Self
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.
Sourcepub fn op(self) -> Result<Op>
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.
Sourcepub fn signature<S: Specs>(self, specs: S) -> Self
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.
Sourcepub fn add_arg(self, name: &str, doc: &str) -> Self
pub fn add_arg(self, name: &str, doc: &str) -> Self
Append the description of the value argument name, without changing what the validator
checks.
Sourcepub fn add_ty_arg(self, name: &str, doc: &str) -> Self
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.
Sourcepub fn attrs_type_key(self, key: &str) -> Self
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.
Sourcepub fn set_attr<T: Into<Any>>(
self,
key: AttrKey<T>,
value: impl Into<T>,
) -> Self
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.
Sourcepub fn replace_attr<T: Into<Any>>(
self,
key: AttrKey<T>,
value: impl Into<T>,
) -> Self
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.
Sourcepub fn reset_attr<T>(self, key: AttrKey<T>) -> Self
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.
Sourcepub fn set_infer_type<F>(self, infer: F) -> Self
pub fn set_infer_type<F>(self, infer: F) -> Self
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.
Sourcepub fn set_validator<F>(self, validate: F) -> Self
pub fn set_validator<F>(self, validate: F) -> Self
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.
Sourcepub fn set_region_body_params<F>(self, body_params: F) -> Self
pub fn set_region_body_params<F>(self, body_params: F) -> Self
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.
Sourcepub fn set_region_validate<F>(self, validate: F) -> Self
pub fn set_region_validate<F>(self, validate: F) -> Self
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.