tvm_tirx/ir/
unique_name_supply.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#![allow(dead_code, unused_imports)]

//! A supply of unique names, for building and rewriting IR.
//!
//! The objects `python/tvm_tirx/ir/unique_name_supply.py` defines. A [`UniqueNameSupply`] is
//! opaque: it is used through its methods, which wrap the native `ir.UniqueNameSupply*` functions.

// Names the hand-written wrappers use: the import section keeps them even when no generated block
// does.
// tvm-ffi-stubgen(import-object): tvm_ffi::Result
// tvm-ffi-stubgen(import-object): tvm_ffi::String
// tvm-ffi-stubgen(import-object): crate::ir::support

// tvm-ffi-stubgen(begin): import-section
use crate::ir::support;
use std::ops::Deref;
use tvm_ffi::Object;
use tvm_ffi::ObjectArc;
use tvm_ffi::Result;
use tvm_ffi::String;
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/ir.UniqueNameSupply
/// Opaque: bytes [24, 64) of [24, 64) are not accounted for by reflected fields. Fields are read through the C ABI getters.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "ir.UniqueNameSupply"]
#[type_final]
pub struct UniqueNameSupplyObj {
    base: Object,
}

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct UniqueNameSupply {
    base: ObjectArc<UniqueNameSupplyObj>,
}

impl Deref for UniqueNameSupply {
    type Target = UniqueNameSupplyObj;
    fn deref(&self) -> &UniqueNameSupplyObj {
        &self.base
    }
}
// tvm-ffi-stubgen(end)

impl UniqueNameSupply {
    /// An empty supply whose names start with `prefix` (`ir.UniqueNameSupply`).
    pub fn new(prefix: &str) -> Self {
        support::infallible(
            tvm_ffi::cached_global_func!("ir.UniqueNameSupply")
                .call_tuple((String::from(prefix),))
                .and_then(|any| any.try_into()),
        )
    }

    /// A name derived from `name` that the supply has not given out, now reserved; `add_prefix`
    /// prepends the prefix and `add_underscore` puts `_` before the numeric suffix
    /// (`ir.UniqueNameSupplyFreshName`).
    pub fn fresh_name(&self, name: &str, add_prefix: bool, add_underscore: bool) -> Result<String> {
        tvm_ffi::cached_global_func!("ir.UniqueNameSupplyFreshName")
            .call_tuple((self, String::from(name), add_prefix, add_underscore))?
            .try_into()
    }

    /// Reserve `name`, with the prefix if `add_prefix`, and return the reserved name
    /// (`ir.UniqueNameSupplyReserveName`).
    pub fn reserve_name(&self, name: &str, add_prefix: bool) -> Result<String> {
        tvm_ffi::cached_global_func!("ir.UniqueNameSupplyReserveName")
            .call_tuple((self, String::from(name), add_prefix))?
            .try_into()
    }

    /// Whether the supply has given out or reserved `name`, with the prefix if `add_prefix`
    /// (`ir.UniqueNameSupplyContainsName`).
    pub fn contains_name(&self, name: &str, add_prefix: bool) -> Result<bool> {
        tvm_ffi::cached_global_func!("ir.UniqueNameSupplyContainsName")
            .call_tuple((self, String::from(name), add_prefix))?
            .try_into()
    }
}