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/node/node.h>
30 #include <tvm/target/target.h>
31 
32 #include <utility>
33 
34 namespace tvm {
35 
37 class TargetTagNode : public Object {
38  public:
40  String name;
42  Map<String, Any> config;
43 
44  static void RegisterReflection() {
45  namespace refl = tvm::ffi::reflection;
46  refl::ObjectDef<TargetTagNode>()
47  .def_ro("name", &TargetTagNode::name)
48  .def_ro("config", &TargetTagNode::config);
49  }
50 
51  static constexpr const char* _type_key = "target.TargetTag";
52 
54 
55  private:
57  uint32_t AttrRegistryIndex() const { return index_; }
59  String AttrRegistryName() const { return name; }
61  uint32_t index_;
62 
63  template <typename, typename>
64  friend class AttrRegistry;
65  template <typename>
67  friend class TargetTagRegEntry;
68 };
69 
74 class TargetTag : public ObjectRef {
75  public:
81  TVM_DLL static Optional<Target> Get(const String& target_tag_name);
86  TVM_DLL static Map<String, Target> ListTags();
94  TVM_DLL static Target AddTag(String name, Map<String, Any> config, bool override);
95 
97 
98  private:
100  TargetTagNode* operator->() { return static_cast<TargetTagNode*>(data_.get()); }
101  friend class TargetTagRegEntry;
102 };
103 
105  public:
110  inline TargetTagRegEntry& set_config(Map<String, Any> config);
116  inline TargetTagRegEntry& with_config(String key, Any value);
118  inline TargetTagRegEntry& set_name();
124  TVM_DLL static TargetTagRegEntry& RegisterOrGet(const String& target_tag_name);
125 
126  private:
127  TargetTag tag_;
128  String name;
129 
131  explicit TargetTagRegEntry(uint32_t reg_index) : tag_(make_object<TargetTagNode>()) {
132  tag_->index_ = reg_index;
133  }
134  template <typename, typename>
135  friend class AttrRegistry;
136  friend class TargetTag;
137 };
138 
139 inline TargetTagRegEntry& TargetTagRegEntry::set_config(Map<String, Any> config) {
140  tag_->config = std::move(config);
141  return *this;
142 }
143 
144 inline TargetTagRegEntry& TargetTagRegEntry::with_config(String key, ffi::Any value) {
145  tag_->config.Set(key, value);
146  return *this;
147 }
148 
150  if (tag_->name.empty()) {
151  tag_->name = name;
152  }
153  return *this;
154 }
155 
156 #define TVM_TARGET_TAG_REGISTER_VAR_DEF \
157  static DMLC_ATTRIBUTE_UNUSED ::tvm::TargetTagRegEntry& __make_##TargetTag
158 
164 #define TVM_REGISTER_TARGET_TAG(TargetTagName) \
165  TVM_STR_CONCAT(TVM_TARGET_TAG_REGISTER_VAR_DEF, __COUNTER__) = \
166  ::tvm::TargetTagRegEntry::RegisterOrGet(TargetTagName).set_name()
167 
168 } // namespace tvm
169 
170 #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:37
static void RegisterReflection()
Definition: tag.h:44
TVM_DECLARE_FINAL_OBJECT_INFO(TargetTagNode, Object)
static constexpr const char * _type_key
Definition: tag.h:51
String name
Name of the target.
Definition: tag.h:40
Map< String, Any > config
Config map to generate the target.
Definition: tag.h:42
Definition: tag.h:104
static TargetTagRegEntry & RegisterOrGet(const String &target_tag_name)
Register or get a new entry.
TargetTagRegEntry & set_name()
Set name of the TargetTag to be the same as registry if it is empty.
Definition: tag.h:149
TargetTagRegEntry & set_config(Map< String, Any > config)
Set the config dict corresponding to the target tag.
Definition: tag.h:139
TargetTagRegEntry & with_config(String key, Any value)
Add a key-value pair to the config dict.
Definition: tag.h:144
Managed reference class to TargetTagNode.
Definition: tag.h:74
TVM_DEFINE_OBJECT_REF_METHODS(TargetTag, ObjectRef, TargetTagNode)
static Map< String, Target > ListTags()
List all names of the existing target tags.
static Target AddTag(String name, Map< String, Any > config, bool override)
Add a tag into the registry.
static Optional< Target > Get(const String &target_tag_name)
Retrieve the Target given it the name of target tag.
Managed reference class to TargetNode.
Definition: target.h:191
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
Definitions and helper macros for IR/AST nodes.
Compilation target object.