tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
adt.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_RUNTIME_CONTAINER_ADT_H_
25 #define TVM_RUNTIME_CONTAINER_ADT_H_
26 
27 #include <utility>
28 #include <vector>
29 
30 #include "./base.h"
31 
32 namespace tvm {
33 namespace runtime {
34 
36 class ADTObj : public Object, public InplaceArrayBase<ADTObj, ObjectRef> {
37  public:
39  int32_t tag;
41  uint32_t size;
42  // The fields of the structure follows directly in memory.
43 
44  static constexpr const uint32_t _type_index = TypeIndex::kRuntimeADT;
45  static constexpr const char* _type_key = "runtime.ADT";
47 
48  private:
52  size_t GetSize() const { return size; }
53 
61  template <typename Iterator>
62  void Init(Iterator begin, Iterator end) {
63  size_t num_elems = std::distance(begin, end);
64  this->size = 0;
65  auto it = begin;
66  for (size_t i = 0; i < num_elems; ++i) {
68  // Only increment size after the initialization succeeds
69  this->size++;
70  }
71  }
72 
73  friend class ADT;
75 };
76 
78 class ADT : public ObjectRef {
79  public:
86  ADT(int32_t tag, std::vector<ObjectRef> fields) : ADT(tag, fields.begin(), fields.end()){};
87 
95  template <typename Iterator>
96  ADT(int32_t tag, Iterator begin, Iterator end) {
97  size_t num_elems = std::distance(begin, end);
98  auto ptr = make_inplace_array_object<ADTObj, ObjectRef>(num_elems);
99  ptr->tag = tag;
100  ptr->Init(begin, end);
101  data_ = std::move(ptr);
102  }
103 
110  ADT(int32_t tag, std::initializer_list<ObjectRef> init) : ADT(tag, init.begin(), init.end()){};
111 
118  const ObjectRef& operator[](size_t idx) const { return operator->()->operator[](idx); }
119 
123  int32_t tag() const { return operator->()->tag; }
124 
128  size_t size() const { return operator->()->size; }
129 
137  template <typename... Args>
138  static ADT Tuple(Args&&... args) {
139  return ADT(0, std::forward<Args>(args)...);
140  }
141 
143 };
144 } // namespace runtime
145 } // namespace tvm
146 #endif // TVM_RUNTIME_CONTAINER_ADT_H_
ADT(int32_t tag, std::initializer_list< ObjectRef > init)
construct an ADT object reference.
Definition: adt.h:110
uint32_t size
Number of fields in the ADT object.
Definition: adt.h:41
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
static constexpr const uint32_t _type_index
Definition: adt.h:44
ADT(int32_t tag, Iterator begin, Iterator end)
construct an ADT object reference.
Definition: adt.h:96
An object representing a structure or enumeration.
Definition: adt.h:36
Base utilities for common POD(plain old data) container types.
base class of all object containers.
Definition: object.h:167
int32_t tag() const
Return the ADT tag.
Definition: adt.h:123
Base template for classes with array like memory layout.
Definition: base.h:100
size_t size() const
Return the number of fields.
Definition: adt.h:128
ADT(int32_t tag, std::vector< ObjectRef > fields)
construct an ADT object reference.
Definition: adt.h:86
TVM_DECLARE_FINAL_OBJECT_INFO(ADTObj, Object)
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:713
const ObjectRef & operator[](size_t idx) const
Access element at index.
Definition: adt.h:118
Base class of all object reference.
Definition: object.h:511
static ADT Tuple(Args &&... args)
Construct a tuple object.
Definition: adt.h:138
reference to algebraic data type objects.
Definition: adt.h:78
void EmplaceInit(size_t idx, Args &&... args)
Construct a value in place with the arguments.
Definition: base.h:149
int32_t tag
The tag representing the constructor used.
Definition: adt.h:39
static constexpr const char * _type_key
Definition: adt.h:45
friend class ADT
Definition: adt.h:73