tvm_tirx/ir/base.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20//! Registry functions held by name. An [`EnvFunc`] keeps a registered global function with its
21//! name, so IR that refers to it serializes the name and resolves it again when loaded.
22
23// tvm-ffi-stubgen(file): mod(missing_docs = allow)
24// Names the hand-written wrappers use: the import section keeps them even when no generated block
25// does.
26// tvm-ffi-stubgen(import): tvm_ffi::Result
27
28// tvm-ffi-stubgen(begin): import-section
29use tvm_ffi::Function;
30use tvm_ffi::IntoArg;
31use tvm_ffi::Object;
32use tvm_ffi::ObjectArc;
33use tvm_ffi::Result;
34use tvm_ffi::String;
35// tvm-ffi-stubgen(end)
36
37/// A registered global function `func` kept with its registry name `name`.
38///
39/// The name identifies it: it is serialized by name, loading looks the name up again, and
40/// structural equality compares the name and ignores `func`.
41///
42/// # Examples
43///
44/// ```
45/// use tvm_tirx::ir::EnvFunc;
46/// use tvm_tirx::tvm_ffi::{Function, Result};
47///
48/// # let double = Function::from_typed(|x: i64| -> Result<i64> { Ok(x * 2) });
49/// # Function::register_global("my_ext.double", double)?;
50/// let double = EnvFunc::get("my_ext.double")?;
51///
52/// assert_eq!(i64::try_from(double.func.call_tuple((21_i64,))?)?, 42);
53/// # Ok::<(), tvm_tirx::tvm_ffi::Error>(())
54/// ```
55// tvm-ffi-stubgen(begin): object/ir.EnvFunc
56#[allow(missing_docs)]
57#[repr(C)]
58#[derive(tvm_ffi::derive::ObjectRef, Clone)]
59pub struct EnvFunc {
60 data_: ObjectArc<EnvFuncObj>,
61}
62
63/// The object behind [`EnvFunc`].
64// Complete: reflected fields fill [24, 48) exactly.
65#[allow(missing_docs)]
66#[repr(C)]
67#[derive(tvm_ffi::derive::Object)]
68#[object(type_key = "ir.EnvFunc", final, immutable)]
69pub struct EnvFuncObj {
70 base_: Object,
71 pub name: String,
72 pub func: Function,
73}
74
75const _: () = {
76 assert!(::core::mem::size_of::<EnvFuncObj>() == 48);
77 assert!(::core::mem::align_of::<EnvFuncObj>() == 8);
78};
79
80#[allow(missing_docs)]
81impl EnvFuncObj {
82 pub(crate) fn new(name: String, func: Function) -> Self {
83 let base_ = Object::new();
84 Self { base_, name, func }
85 }
86}
87
88#[allow(missing_docs)]
89impl EnvFunc {
90 /// Create a `EnvFunc` from its field values.
91 pub fn new(name: impl Into<String>, func: impl IntoArg<Function>) -> Self {
92 let obj = EnvFuncObj::new(name.into(), func.into_owned());
93 Self { data_: ObjectArc::new(obj) }
94 }
95}
96// tvm-ffi-stubgen(end)
97
98impl EnvFunc {
99 /// Return the global function `name` kept with its name (`ir.EnvFuncGet`).
100 ///
101 /// # Errors
102 ///
103 /// Returns an error if no global function is registered as `name`.
104 pub fn get(name: &str) -> Result<Self> {
105 tvm_ffi::cached_global_func!("ir.EnvFuncGet").call_tuple((String::from(name),))?.try_into()
106 }
107}