tvm_tirx/sym/
constraint.rs1#![allow(clippy::new_without_default)]
21
22use std::cell::UnsafeCell;
32use std::mem::{align_of, offset_of, size_of};
33use tvm_ffi::{AnyView, Array, Error, Result, VALUE_ERROR};
34use crate::ir::expr::PrimExpr;
42use crate::ir::expr::Range;
43use crate::ir::expr::Var;
44use std::borrow::Borrow;
45use std::ops::Deref;
46use tvm_ffi::IntoArg;
47use tvm_ffi::Object;
48use tvm_ffi::ObjectArc;
49#[allow(missing_docs)]
54#[repr(C)]
55#[derive(tvm_ffi::derive::ObjectRef, Clone)]
56pub struct Constraint {
57 data_: ObjectArc<ConstraintObj>,
58}
59
60#[allow(missing_docs)]
63#[repr(C)]
64#[derive(tvm_ffi::derive::Object)]
65#[object(type_key = "sym.Constraint", immutable, rs_parent)]
66pub struct ConstraintObj {
67 base_: Object,
68}
69
70const _: () = {
71 assert!(::core::mem::size_of::<ConstraintObj>() == 24);
72 assert!(::core::mem::align_of::<ConstraintObj>() == 8);
73};
74
75#[allow(missing_docs)]
76impl ConstraintObj {
77 pub fn new() -> Self {
78 let base_ = Object::new();
79 Self { base_ }
80 }
81}
82#[allow(missing_docs)]
88#[repr(C)]
89#[derive(tvm_ffi::derive::ObjectRef, Clone)]
90pub struct BindingConstraint {
91 data_: ObjectArc<BindingConstraintObj>,
92}
93
94#[allow(missing_docs)]
97#[repr(C)]
98#[derive(tvm_ffi::derive::Object)]
99#[object(type_key = "sym.BindingConstraint", final, immutable)]
100pub struct BindingConstraintObj {
101 base_: ConstraintObj,
102 pub var: Var,
103 pub value: PrimExpr,
104}
105
106const _: () = {
107 assert!(::core::mem::size_of::<BindingConstraintObj>() == 40);
108 assert!(::core::mem::align_of::<BindingConstraintObj>() == 8);
109};
110
111impl Deref for BindingConstraintObj {
112 type Target = ConstraintObj;
113 fn deref(&self) -> &ConstraintObj {
114 &self.base_
115 }
116}
117
118#[allow(missing_docs)]
119impl BindingConstraintObj {
120 pub(crate) fn new(var: Var, value: PrimExpr) -> Self {
121 let base_ = ConstraintObj::new();
122 Self { base_, var, value }
123 }
124}
125
126#[allow(missing_docs)]
127impl BindingConstraint {
128 pub fn new(var: impl IntoArg<Var>, value: impl IntoArg<PrimExpr>) -> Self {
130 let obj = BindingConstraintObj::new(var.into_owned(), value.into_owned());
131 Self { data_: ObjectArc::new(obj) }
132 }
133}
134
135tvm_ffi::impl_object_ref_upcast!(BindingConstraint => Constraint);
136#[allow(missing_docs)]
142#[repr(C)]
143#[derive(tvm_ffi::derive::ObjectRef, Clone)]
144pub struct PredicateConstraint {
145 data_: ObjectArc<PredicateConstraintObj>,
146}
147
148#[allow(missing_docs)]
151#[repr(C)]
152#[derive(tvm_ffi::derive::Object)]
153#[object(type_key = "sym.PredicateConstraint", final, immutable)]
154pub struct PredicateConstraintObj {
155 base_: ConstraintObj,
156 pub condition: PrimExpr,
157 pub assume_always: bool,
158}
159
160const _: () = {
161 assert!(::core::mem::size_of::<PredicateConstraintObj>() == 40);
162 assert!(::core::mem::align_of::<PredicateConstraintObj>() == 8);
163};
164
165impl Deref for PredicateConstraintObj {
166 type Target = ConstraintObj;
167 fn deref(&self) -> &ConstraintObj {
168 &self.base_
169 }
170}
171
172#[allow(missing_docs)]
173impl PredicateConstraintObj {
174 pub(crate) fn new(condition: PrimExpr, assume_always: bool) -> Self {
175 let base_ = ConstraintObj::new();
176 Self { base_, condition, assume_always }
177 }
178}
179
180#[allow(missing_docs)]
181impl PredicateConstraint {
182 pub fn new(condition: impl IntoArg<PrimExpr>, assume_always: bool) -> Self {
184 let obj = PredicateConstraintObj::new(condition.into_owned(), assume_always);
185 Self { data_: ObjectArc::new(obj) }
186 }
187}
188
189tvm_ffi::impl_object_ref_upcast!(PredicateConstraint => Constraint);
190#[allow(missing_docs)]
196#[repr(C)]
197#[derive(tvm_ffi::derive::ObjectRef, Clone)]
198pub struct RangeConstraint {
199 data_: ObjectArc<RangeConstraintObj>,
200}
201
202#[allow(missing_docs)]
205#[repr(C)]
206#[derive(tvm_ffi::derive::Object)]
207#[object(type_key = "sym.RangeConstraint", final, immutable)]
208pub struct RangeConstraintObj {
209 base_: ConstraintObj,
210 pub var: Var,
211 pub range: Range,
212}
213
214const _: () = {
215 assert!(::core::mem::size_of::<RangeConstraintObj>() == 40);
216 assert!(::core::mem::align_of::<RangeConstraintObj>() == 8);
217};
218
219impl Deref for RangeConstraintObj {
220 type Target = ConstraintObj;
221 fn deref(&self) -> &ConstraintObj {
222 &self.base_
223 }
224}
225
226#[allow(missing_docs)]
227impl RangeConstraintObj {
228 pub(crate) fn new(var: Var, range: Range) -> Self {
229 let base_ = ConstraintObj::new();
230 Self { base_, var, range }
231 }
232}
233
234#[allow(missing_docs)]
235impl RangeConstraint {
236 pub fn new(var: impl IntoArg<Var>, range: impl IntoArg<Range>) -> Self {
238 let obj = RangeConstraintObj::new(var.into_owned(), range.into_owned());
239 Self { data_: ObjectArc::new(obj) }
240 }
241}
242
243tvm_ffi::impl_object_ref_upcast!(RangeConstraint => Constraint);
244#[repr(C)]
252#[derive(tvm_ffi::derive::Object)]
253#[object(type_key = "sym.ConstraintContext", final)]
254pub struct ConstraintContextObj {
255 base_: Object,
256 parent: Option<ObjectArc<ConstraintContextObj>>,
257 parent_prefix_size: i64,
258 constraints: UnsafeCell<Array<Constraint>>,
259}
260
261const _: () = {
264 assert!(size_of::<ConstraintContextObj>() == 48);
265 assert!(align_of::<ConstraintContextObj>() == 8);
266 assert!(offset_of!(ConstraintContextObj, parent) == 24);
267 assert!(offset_of!(ConstraintContextObj, parent_prefix_size) == 32);
268 assert!(offset_of!(ConstraintContextObj, constraints) == 40);
269};
270
271#[repr(C)]
298#[derive(tvm_ffi::derive::ObjectRef, Clone)]
299pub struct ConstraintContext {
300 data_: ObjectArc<ConstraintContextObj>,
301}
302
303impl ConstraintContextObj {
304 pub fn append(&self, constraint: impl IntoArg<Constraint>) -> Result<()> {
308 tvm_ffi::cached_global_func!("sym.ConstraintContextAppend")
309 .call_packed(&[AnyView::from(self), AnyView::from(constraint.into_arg().borrow())])?;
310 Ok(())
311 }
312
313 pub fn local_size(&self) -> i64 {
315 unsafe { (&*self.constraints.get()).len() as i64 }
317 }
318
319 pub fn snapshot(&self) -> ConstraintContext {
321 let parent = ConstraintContext::try_from(AnyView::from(self))
322 .expect("context has its registered native type");
323 ConstraintContext::allocate(Some(parent.data_), self.local_size())
324 }
325
326 fn local_array(&self) -> Array<Constraint> {
327 unsafe { (&*self.constraints.get()).clone() }
330 }
331
332 pub fn for_each(&self, mut callback: impl FnMut(&Constraint)) {
337 let retained = ConstraintContext::try_from(AnyView::from(self))
338 .expect("context has its registered native type");
339 retained.for_each_prefix(retained.local_size(), &mut callback);
340 }
341
342 fn for_each_prefix(&self, prefix: i64, callback: &mut impl FnMut(&Constraint)) {
343 let local = self.local_array();
344 if let Some(parent) = &self.parent {
345 parent.for_each_prefix(self.parent_prefix_size, callback);
346 }
347 for constraint in local.iter().take(prefix as usize) {
348 callback(constraint);
349 }
350 }
351
352 pub fn to_array(&self) -> Array<Constraint> {
356 if self.parent.is_none() {
357 return self.local_array();
358 }
359 let mut facts = Vec::new();
360 self.for_each(|fact| facts.push(fact.clone()));
361 Array::from_vec(facts)
362 }
363}
364
365impl ConstraintContext {
366 pub fn new() -> Self {
368 Self::allocate(None, 0)
369 }
370
371 pub fn with_parent_prefix_size(
380 parent: impl IntoArg<ConstraintContext>,
381 parent_prefix_size: i64,
382 ) -> Result<Self> {
383 let parent = parent.into_owned();
384 if parent_prefix_size < 0 || parent_prefix_size > parent.local_size() {
385 return Err(Error::new(
386 VALUE_ERROR,
387 "Constraint context parent local prefix is out of bounds",
388 ));
389 }
390 Ok(Self::allocate(Some(parent.data_), parent_prefix_size))
391 }
392
393 fn allocate(parent: Option<ObjectArc<ConstraintContextObj>>, prefix: i64) -> Self {
394 Self {
395 data_: ObjectArc::new(ConstraintContextObj {
396 base_: Object::new(),
397 parent,
398 parent_prefix_size: prefix,
399 constraints: UnsafeCell::new(Array::default()),
402 }),
403 }
404 }
405}