tvm
Loading...
Searching...
No Matches
unique_name_supply.h
Go to the documentation of this file.
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
24#ifndef TVM_IR_UNIQUE_NAME_SUPPLY_H_
25#define TVM_IR_UNIQUE_NAME_SUPPLY_H_
26
27#include <tvm/ffi/reflection/registry.h>
28#include <tvm/ir/expr.h>
29
30#include <cstdint>
31#include <string>
32#include <utility>
33
34namespace tvm {
35
39class UniqueNameSupplyNode : public ffi::Object {
40 public:
45
51 UniqueNameSupplyNode(const ffi::String& prefix, ffi::Map<ffi::String, int64_t> name_map)
52 : prefix_(prefix), name_map(std::move(name_map)) {}
53
62 ffi::String FreshName(const ffi::String& name, bool add_prefix = true,
63 bool add_underscore = true);
64
72 ffi::String ReserveName(const ffi::String& name, bool add_prefix = true);
73
81 bool ContainsName(const ffi::String& name, bool add_prefix = true);
82
83 // Prefix for all GlobalVar names. It can be empty.
84 std::string prefix_;
85
86 static constexpr const bool _type_mutable = true;
87
88 static void RegisterReflection() {
89 namespace refl = tvm::ffi::reflection;
90 refl::ObjectDef<UniqueNameSupplyNode>();
91 }
92
93 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.UniqueNameSupply", UniqueNameSupplyNode, ffi::Object);
94
95 private:
97 ffi::String AddPrefixToName(const ffi::String& name);
98
105 std::string GetUniqueName(std::string name, bool add_underscore = true);
106
108 ffi::Map<ffi::String, int64_t> name_map;
109};
110
115class UniqueNameSupply : public ffi::ObjectRef {
116 public:
122 TVM_DLL explicit UniqueNameSupply(const ffi::String& prefix = "",
123 ffi::Map<ffi::String, int64_t> name_map = {});
124
130 template <typename Iter, typename Lambda>
132 for (auto it = begin; it != end; ++it) {
133 this->operator->()->ReserveName(f(*it), false);
134 }
135 }
136
139};
140
141} // namespace tvm
142
143#endif // TVM_IR_UNIQUE_NAME_SUPPLY_H_
UniqueNameSupply can be used to generate unique names.
Definition unique_name_supply.h:39
static void RegisterReflection()
Definition unique_name_supply.h:88
static constexpr const bool _type_mutable
Definition unique_name_supply.h:86
std::string prefix_
Definition unique_name_supply.h:84
bool ContainsName(const ffi::String &name, bool add_prefix=true)
Checks if this UniqueNameSupply already generated a name.
ffi::String ReserveName(const ffi::String &name, bool add_prefix=true)
Reserves an existing name with this UniqueNameSupply.
UniqueNameSupplyNode(const ffi::String &prefix, ffi::Map< ffi::String, int64_t > name_map)
Constructor.
Definition unique_name_supply.h:51
ffi::String FreshName(const ffi::String &name, bool add_prefix=true, bool add_underscore=true)
Generates a unique name from this UniqueNameSupply.
UniqueNameSupplyNode()=default
Empty constructor. Needed by the TVM_REGISTER_NODE_TYPE macro.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.UniqueNameSupply", UniqueNameSupplyNode, ffi::Object)
Managed reference class to UniqueNameSupplyNode.
Definition unique_name_supply.h:115
UniqueNameSupply(const ffi::String &prefix="", ffi::Map< ffi::String, int64_t > name_map={})
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(UniqueNameSupply, ffi::ObjectRef, UniqueNameSupplyNode)
UniqueNameSupply(Iter begin, Iter end, Lambda f)
Construct UniqueNameSupply by reserving names from the given iterator range.
Definition unique_name_supply.h:131
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Base expr nodes in TVM.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40