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  ffi::String name;
42  ffi::Map<ffi::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  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetTag", TargetTagNode, Object);
51 
52  private:
54  uint32_t AttrRegistryIndex() const { return index_; }
56  ffi::String AttrRegistryName() const { return name; }
58  uint32_t index_;
59 
60  template <typename, typename>
61  friend class AttrRegistry;
62  template <typename>
64  friend class TargetTagRegEntry;
65 };
66 
71 class TargetTag : public ObjectRef {
72  public:
78  TVM_DLL static ffi::Optional<Target> Get(const ffi::String& target_tag_name);
83  TVM_DLL static ffi::Map<ffi::String, Target> ListTags();
91  TVM_DLL static Target AddTag(ffi::String name, ffi::Map<ffi::String, Any> config, bool override);
92 
94 
95  private:
97  TargetTagNode* operator->() { return static_cast<TargetTagNode*>(data_.get()); }
98  friend class TargetTagRegEntry;
99 };
100 
102  public:
107  inline TargetTagRegEntry& set_config(ffi::Map<ffi::String, Any> config);
113  inline TargetTagRegEntry& with_config(ffi::String key, Any value);
115  inline TargetTagRegEntry& set_name();
121  TVM_DLL static TargetTagRegEntry& RegisterOrGet(const ffi::String& target_tag_name);
122 
123  private:
124  TargetTag tag_;
125  ffi::String name;
126 
128  explicit TargetTagRegEntry(uint32_t reg_index) : tag_(ffi::make_object<TargetTagNode>()) {
129  tag_->index_ = reg_index;
130  }
131  template <typename, typename>
132  friend class AttrRegistry;
133  friend class TargetTag;
134 };
135 
136 inline TargetTagRegEntry& TargetTagRegEntry::set_config(ffi::Map<ffi::String, Any> config) {
137  tag_->config = std::move(config);
138  return *this;
139 }
140 
141 inline TargetTagRegEntry& TargetTagRegEntry::with_config(ffi::String key, ffi::Any value) {
142  tag_->config.Set(key, value);
143  return *this;
144 }
145 
147  if (tag_->name.empty()) {
148  tag_->name = name;
149  }
150  return *this;
151 }
152 
153 #define TVM_TARGET_TAG_REGISTER_VAR_DEF \
154  static DMLC_ATTRIBUTE_UNUSED ::tvm::TargetTagRegEntry& __make_##TargetTag
155 
161 #define TVM_REGISTER_TARGET_TAG(TargetTagName) \
162  TVM_STR_CONCAT(TVM_TARGET_TAG_REGISTER_VAR_DEF, __COUNTER__) = \
163  ::tvm::TargetTagRegEntry::RegisterOrGet(TargetTagName).set_name()
164 
165 } // namespace tvm
166 
167 #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
ffi::String name
Name of the target.
Definition: tag.h:40
ffi::Map< ffi::String, Any > config
Config map to generate the target.
Definition: tag.h:42
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetTag", TargetTagNode, Object)
Definition: tag.h:101
TargetTagRegEntry & set_config(ffi::Map< ffi::String, Any > config)
Set the config dict corresponding to the target tag.
Definition: tag.h:136
TargetTagRegEntry & with_config(ffi::String key, Any value)
Add a key-value pair to the config dict.
Definition: tag.h:141
TargetTagRegEntry & set_name()
Set name of the TargetTag to be the same as registry if it is empty.
Definition: tag.h:146
static TargetTagRegEntry & RegisterOrGet(const ffi::String &target_tag_name)
Register or get a new entry.
Managed reference class to TargetTagNode.
Definition: tag.h:71
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.
Managed reference class to TargetNode.
Definition: target.h:192
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.