tvm
tag.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_TARGET_TAG_H_
25 #define TVM_TARGET_TAG_H_
26 
27 #include <tvm/ffi/reflection/registry.h>
29 #include <tvm/target/target.h>
30 
31 #include <utility>
32 
33 namespace tvm {
34 
36 class TargetTagNode : public Object {
37  public:
39  ffi::String name;
41  ffi::Map<ffi::String, Any> config;
42 
43  static void RegisterReflection() {
44  namespace refl = tvm::ffi::reflection;
45  refl::ObjectDef<TargetTagNode>()
46  .def_ro("name", &TargetTagNode::name)
47  .def_ro("config", &TargetTagNode::config);
48  }
49  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetTag", TargetTagNode, Object);
50 
51  private:
53  uint32_t AttrRegistryIndex() const { return index_; }
55  ffi::String AttrRegistryName() const { return name; }
57  uint32_t index_;
58 
59  template <typename, typename>
60  friend class AttrRegistry;
61  template <typename>
63  friend class TargetTagRegEntry;
64 };
65 
70 class TargetTag : public ObjectRef {
71  public:
77  TVM_DLL static ffi::Optional<Target> Get(const ffi::String& target_tag_name);
82  TVM_DLL static ffi::Map<ffi::String, Target> ListTags();
88  TVM_DLL static ffi::Optional<ffi::Map<ffi::String, Any>> GetConfig(
89  const ffi::String& target_tag_name);
97  TVM_DLL static Target AddTag(ffi::String name, ffi::Map<ffi::String, Any> config, bool override);
98 
100 
101  private:
103  TargetTagNode* operator->() { return static_cast<TargetTagNode*>(data_.get()); }
104  friend class TargetTagRegEntry;
105 };
106 
108  public:
113  inline TargetTagRegEntry& set_config(ffi::Map<ffi::String, Any> config);
119  inline TargetTagRegEntry& with_config(ffi::String key, Any value);
121  inline TargetTagRegEntry& set_name();
127  TVM_DLL static TargetTagRegEntry& RegisterOrGet(const ffi::String& target_tag_name);
128 
129  private:
130  TargetTag tag_;
131  ffi::String name;
132 
134  explicit TargetTagRegEntry(uint32_t reg_index) : tag_(ffi::make_object<TargetTagNode>()) {
135  tag_->index_ = reg_index;
136  }
137  template <typename, typename>
138  friend class AttrRegistry;
139  friend class TargetTag;
140 };
141 
142 inline TargetTagRegEntry& TargetTagRegEntry::set_config(ffi::Map<ffi::String, Any> config) {
143  tag_->config = std::move(config);
144  return *this;
145 }
146 
147 inline TargetTagRegEntry& TargetTagRegEntry::with_config(ffi::String key, ffi::Any value) {
148  tag_->config.Set(key, value);
149  return *this;
150 }
151 
153  if (tag_->name.empty()) {
154  tag_->name = name;
155  }
156  return *this;
157 }
158 
159 #define TVM_TARGET_TAG_REGISTER_VAR_DEF \
160  static TVM_ATTRIBUTE_UNUSED ::tvm::TargetTagRegEntry& __make_##TargetTag
161 
167 #define TVM_REGISTER_TARGET_TAG(TargetTagName) \
168  TVM_STR_CONCAT(TVM_TARGET_TAG_REGISTER_VAR_DEF, __COUNTER__) = \
169  ::tvm::TargetTagRegEntry::RegisterOrGet(TargetTagName).set_name()
170 
171 } // namespace tvm
172 
173 #endif // TVM_TARGET_TAG_H_
Attribute map used in registry.
Generic attribute map.
Definition: attr_registry_map.h:38
Definition: instruction.h:30
A target tag.
Definition: tag.h:36
static void RegisterReflection()
Definition: tag.h:43
ffi::String name
Name of the target.
Definition: tag.h:39
ffi::Map< ffi::String, Any > config
Config map to generate the target.
Definition: tag.h:41
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetTag", TargetTagNode, Object)
Definition: tag.h:107
TargetTagRegEntry & set_config(ffi::Map< ffi::String, Any > config)
Set the config dict corresponding to the target tag.
Definition: tag.h:142
TargetTagRegEntry & with_config(ffi::String key, Any value)
Add a key-value pair to the config dict.
Definition: tag.h:147
TargetTagRegEntry & set_name()
Set name of the TargetTag to be the same as registry if it is empty.
Definition: tag.h:152
static TargetTagRegEntry & RegisterOrGet(const ffi::String &target_tag_name)
Register or get a new entry.
Managed reference class to TargetTagNode.
Definition: tag.h:70
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TargetTag, ObjectRef, TargetTagNode)
static ffi::Map< ffi::String, Target > ListTags()
List all names of the existing target tags.
static ffi::Optional< Target > Get(const ffi::String &target_tag_name)
Retrieve the Target given it the name of target tag.
static Target AddTag(ffi::String name, ffi::Map< ffi::String, Any > config, bool override)
Add a tag into the registry.
static ffi::Optional< ffi::Map< ffi::String, Any > > GetConfig(const ffi::String &target_tag_name)
Retrieve the raw config dict for a target tag.
Managed reference class to TargetNode.
Definition: target.h:135
Definition: repr_printer.h:91
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
Compilation target object.