tvm
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:
85  ADT(int32_t tag, std::vector<ObjectRef> fields) : ADT(tag, fields.begin(), fields.end()){};
86 
93  template <typename Iterator>
94  ADT(int32_t tag, Iterator begin, Iterator end) {
95  size_t num_elems = std::distance(begin, end);
96  auto ptr = make_inplace_array_object<ADTObj, ObjectRef>(num_elems);
97  ptr->tag = tag;
98  ptr->Init(begin, end);
99  data_ = std::move(ptr);
100  }
101 
107  ADT(int32_t tag, std::initializer_list<ObjectRef> init) : ADT(tag, init.begin(), init.end()){};
108 
115  const ObjectRef& operator[](size_t idx) const { return operator->()->operator[](idx); }
116 
120  int32_t tag() const { return operator->()->tag; }
121 
125  size_t size() const { return operator->()->size; }
126 
134  template <typename... Args>
135  static ADT Tuple(Args&&... args) {
136  return ADT(0, std::forward<Args>(args)...);
137  }
138 
140 };
141 } // namespace runtime
142 } // namespace tvm
143 #endif // TVM_RUNTIME_CONTAINER_ADT_H_
An object representing a structure or enumeration.
Definition: adt.h:36
uint32_t size
Number of fields in the ADT object.
Definition: adt.h:41
int32_t tag
The tag representing the constructor used.
Definition: adt.h:39
static constexpr const char * _type_key
Definition: adt.h:45
TVM_DECLARE_FINAL_OBJECT_INFO(ADTObj, Object)
static constexpr const uint32_t _type_index
Definition: adt.h:44
reference to algebraic data type objects.
Definition: adt.h:78
ADT(int32_t tag, std::vector< ObjectRef > fields)
construct an ADT object reference.
Definition: adt.h:85
TVM_DEFINE_OBJECT_REF_METHODS(ADT, ObjectRef, ADTObj)
static ADT Tuple(Args &&... args)
Construct a tuple object.
Definition: adt.h:135
const ObjectRef & operator[](size_t idx) const
Access element at index.
Definition: adt.h:115
ADT(int32_t tag, std::initializer_list< ObjectRef > init)
construct an ADT object reference.
Definition: adt.h:107
int32_t tag() const
Return the ADT tag.
Definition: adt.h:120
ADT(int32_t tag, Iterator begin, Iterator end)
construct an ADT object reference.
Definition: adt.h:94
size_t size() const
Return the number of fields.
Definition: adt.h:125
Base template for classes with array like memory layout.
Definition: base.h:100
void EmplaceInit(size_t idx, Args &&... args)
Construct a value in place with the arguments.
Definition: base.h:149
Base class of all object reference.
Definition: object.h:519
const Object * operator->() const
Definition: object.h:556
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
base class of all object containers.
Definition: object.h:171
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
@ kRuntimeADT
Definition: object.h:81