tvm_tirx/ir/attrs.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// The generated `new` of a field-less base needs no `Default`.
20#![allow(clippy::new_without_default)]
21
22//! Attributes. Calls, functions and modules carry metadata as [`Attrs`]: a typed attribute object
23//! of an operator, or a [`DictAttrs`] map of named values.
24
25// tvm-ffi-stubgen(file): mod(missing_docs = allow)
26// `no_new` generates no allocator for the abstract base: a value is one of its subtypes.
27// tvm-ffi-stubgen(object): ir.Attrs(no_new)
28
29// tvm-ffi-stubgen(begin): import-section
30use std::ops::Deref;
31use tvm_ffi::Any;
32use tvm_ffi::Map;
33use tvm_ffi::Object;
34use tvm_ffi::ObjectArc;
35use tvm_ffi::String;
36// tvm-ffi-stubgen(end)
37
38/// The base of every attribute object, the metadata attached to a call, a function or a module.
39// tvm-ffi-stubgen(begin): object/ir.Attrs
40#[allow(missing_docs)]
41#[repr(C)]
42#[derive(tvm_ffi::derive::ObjectRef, Clone)]
43pub struct Attrs {
44 data_: ObjectArc<AttrsObj>,
45}
46
47/// The object behind [`Attrs`].
48// Complete: reflected fields fill [24, 24) exactly.
49#[allow(missing_docs)]
50#[repr(C)]
51#[derive(tvm_ffi::derive::Object)]
52#[object(type_key = "ir.Attrs", immutable, rs_parent)]
53pub struct AttrsObj {
54 base_: Object,
55}
56
57const _: () = {
58 assert!(::core::mem::size_of::<AttrsObj>() == 24);
59 assert!(::core::mem::align_of::<AttrsObj>() == 8);
60};
61
62#[allow(missing_docs)]
63impl AttrsObj {
64 pub fn new() -> Self {
65 let base_ = Object::new();
66 Self { base_ }
67 }
68}
69// tvm-ffi-stubgen(end)
70
71/// Attributes as a map `dict` from names to values, such as the attributes of a function or a
72/// module.
73///
74/// # Examples
75///
76/// ```
77/// use tvm_tirx::ir::{DictAttrs, IRModule};
78/// use tvm_tirx::tvm_ffi::{Any, Map, String};
79///
80/// let dict: Map<String, Any> =
81/// [("system_lib_prefix".into(), Any::from(String::from("my_lib_")))].into_iter().collect();
82/// let module = IRModule::new(Map::new(), DictAttrs::new(dict), Map::new())?;
83///
84/// let prefix = module.attrs.dict.get("system_lib_prefix")?.unwrap();
85/// assert_eq!(String::try_from(prefix)?, "my_lib_");
86/// # Ok::<(), tvm_tirx::tvm_ffi::Error>(())
87/// ```
88// tvm-ffi-stubgen(begin): object/ir.DictAttrs
89#[allow(missing_docs)]
90#[repr(C)]
91#[derive(tvm_ffi::derive::ObjectRef, Clone)]
92pub struct DictAttrs {
93 data_: ObjectArc<DictAttrsObj>,
94}
95
96/// The object behind [`DictAttrs`].
97// Complete: reflected fields fill [24, 32) exactly.
98#[allow(missing_docs)]
99#[repr(C)]
100#[derive(tvm_ffi::derive::Object)]
101#[object(type_key = "ir.DictAttrs", final, immutable)]
102pub struct DictAttrsObj {
103 base_: AttrsObj,
104 pub dict: Map<String, Any>,
105}
106
107const _: () = {
108 assert!(::core::mem::size_of::<DictAttrsObj>() == 32);
109 assert!(::core::mem::align_of::<DictAttrsObj>() == 8);
110};
111
112impl Deref for DictAttrsObj {
113 type Target = AttrsObj;
114 fn deref(&self) -> &AttrsObj {
115 &self.base_
116 }
117}
118
119#[allow(missing_docs)]
120impl DictAttrsObj {
121 pub(crate) fn new(dict: Map<String, Any>) -> Self {
122 let base_ = AttrsObj::new();
123 Self { base_, dict }
124 }
125}
126
127#[allow(missing_docs)]
128impl DictAttrs {
129 /// Create a `DictAttrs` from its field values.
130 pub fn new(dict: impl Into<Map<String, Any>>) -> Self {
131 let obj = DictAttrsObj::new(dict.into());
132 Self { data_: ObjectArc::new(obj) }
133 }
134}
135
136tvm_ffi::impl_object_ref_upcast!(DictAttrs => Attrs);
137// tvm-ffi-stubgen(end)