tvm
buffer.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_TIRX_BUFFER_H_
25 #define TVM_TIRX_BUFFER_H_
26 
27 #include <tvm/ffi/container/array.h>
28 #include <tvm/ffi/reflection/registry.h>
29 #include <tvm/ffi/string.h>
30 #include <tvm/ir/expr.h>
31 #include <tvm/tirx/layout.h>
32 #include <tvm/tirx/var.h>
33 
34 #include <string>
35 
36 namespace tvm {
37 namespace tirx {
38 
39 #ifndef TVM_INDEX_DEFAULT_I64
40 #define TVM_INDEX_DEFAULT_I64 1
41 #endif
44 #if TVM_INDEX_DEFAULT_I64
45  static const PrimType default_index_ty = PrimType::Int(64);
46 #else
47  static const PrimType default_index_ty = PrimType::Int(32);
48 #endif
49  return default_index_ty;
50 }
51 
52 inline DLDataType DefaultIndexType() {
53 #if TVM_INDEX_DEFAULT_I64
54  return DLDataType{kDLInt, 64, 1};
55 #else
56  return DLDataType{kDLInt, 32, 1};
57 #endif
58 }
59 
60 // forward declare Stmt
61 class Stmt;
62 
64 enum BufferType : int {
65  kDefault = 1,
66  // Maps buffer[i][j][k] -> buffer[i][0][k] if dimension i's shape equals 1.
68 };
69 
71 class BufferNode : public ffi::Object {
72  public:
73  // Data fields.
87  ffi::Array<PrimExpr> shape;
96  ffi::Array<IntImm> axis_separators;
101  ffi::Array<PrimExpr> strides;
104  // Meta data
106  ffi::String name;
120  mutable Span span;
121 
123  ffi::Optional<Layout> layout;
124 
129  ffi::Array<PrimExpr> allocated_addr;
130 
133 
134  static void RegisterReflection() {
135  namespace refl = tvm::ffi::reflection;
136  refl::ObjectDef<BufferNode>()
137  // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
138  .def_ro("data", &BufferNode::data, refl::AttachFieldFlag::SEqHashDefRecursive())
139  .def_ro("dtype", &BufferNode::dtype)
140  // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
141  .def_ro("shape", &BufferNode::shape, refl::AttachFieldFlag::SEqHashDefRecursive())
142  // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
143  .def_ro("strides", &BufferNode::strides, refl::AttachFieldFlag::SEqHashDefRecursive())
144  .def_ro("axis_separators", &BufferNode::axis_separators,
145  refl::AttachFieldFlag::SEqHashDefRecursive())
146  // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
147  .def_ro("elem_offset", &BufferNode::elem_offset,
148  refl::AttachFieldFlag::SEqHashDefRecursive())
149  .def_ro("name", &BufferNode::name, refl::AttachFieldFlag::SEqHashIgnore())
150  .def_ro("data_alignment", &BufferNode::data_alignment)
151  .def_ro("offset_factor", &BufferNode::offset_factor)
152  .def_ro("buffer_type", &BufferNode::buffer_type)
153  .def_ro("span", &BufferNode::span, refl::AttachFieldFlag::SEqHashIgnore())
154  .def_ro("layout", &BufferNode::layout)
155  .def_ro("allocated_addr", &BufferNode::allocated_addr);
156  }
157 
159  DLDataType DefaultIndexType() const {
160  return shape.size() != 0 ? shape[0].ty()->dtype : tvm::tirx::DefaultIndexType();
161  }
162 
164  PrimType ElementType() const { return dtype; }
165 
175  ffi::Array<PrimExpr> ElemOffset(ffi::Array<PrimExpr> index, bool inner = false) const;
176 
177  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
178 
179  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Buffer", BufferNode, ffi::Object);
180 };
181 
187 class Buffer : public ffi::ObjectRef {
188  public:
189  // User can specify data_alignment and offset_factor to be 0
190  // A default value will be picked.
191  TVM_DLL Buffer(Var data, PrimType dtype, ffi::Array<PrimExpr> shape, ffi::Array<PrimExpr> strides,
192  PrimExpr elem_offset, ffi::String name, int data_alignment, int offset_factor,
193  BufferType buffer_type, ffi::Array<IntImm> axis_separators = {},
194  Span span = Span(), ffi::Optional<Layout> layout = std::nullopt,
195  ffi::Array<PrimExpr> allocated_addr = {});
196 
202  TVM_DLL Buffer MakeStrideView() const;
211  TVM_DLL Buffer MakeSlice(ffi::Array<PrimExpr> begins, ffi::Array<PrimExpr> extents) const;
220  TVM_DLL Expr access_ptr(int access_mask, PointerType ptr_type = PointerType::VoidPointerTy(),
221  int content_lanes = 1, PrimExpr offset = IntImm::Int32(0),
222  ffi::Optional<PrimExpr> input_extent = std::nullopt) const;
230  TVM_DLL PrimExpr vload(ffi::Array<PrimExpr> begin, PrimType dtype,
231  ffi::Optional<PrimExpr> predicate = std::nullopt) const;
239  TVM_DLL Stmt vstore(ffi::Array<PrimExpr> begin, PrimExpr value,
240  ffi::Optional<PrimExpr> predicate = std::nullopt) const;
241 
246 
253  ffi::Array<PrimExpr> OffsetOf(ffi::Array<PrimExpr> index) const;
254 
260  PrimExpr OffsetOf_p(const ffi::Array<PrimExpr>& indices) const;
261 
265  TVM_DLL ffi::String scope() const;
266 
270  TVM_DLL Buffer with_allocated_addr(ffi::Array<PrimExpr> allocated_addr) const;
271 
277  TVM_DLL bool IsScalar(bool alloc_or_decl = true) const;
278 
282  TVM_DLL Buffer with_dtype(PrimType dtype) const;
283 
285  PrimType ElementType() const { return (*this)->ElementType(); }
286 
290  TVM_DLL Buffer with_data(Var data) const;
291 
294 };
295 
307 TVM_DLL Buffer decl_buffer(ffi::Array<PrimExpr> shape, PrimType dtype = PrimType::Float(32),
308  ffi::String name = "buffer", ffi::String storage_scope = "",
309  ffi::Optional<ffi::Array<IntImm>> axis_separators = std::nullopt,
310  Span span = Span());
311 
325  public:
327  virtual ~DataProducerNode() {}
332  virtual ffi::Array<PrimExpr> GetShape() const = 0;
337  virtual PrimType GetDataType() const = 0;
342  virtual ffi::String GetNameHint() const = 0;
344 };
345 
351  public:
353 };
354 
368 TVM_DLL tirx::Buffer BufferWithOffsetAlignment(ffi::Array<PrimExpr> shape, PrimType dtype,
369  std::string name, int data_alignment,
370  int offset_factor, bool compact,
371  std::string memory_scope = "");
372 } // namespace tirx
373 } // namespace tvm
374 #endif // TVM_TIR_BUFFER_H_
Managed reference to ExprNode.
Definition: base_expr.h:311
static IntImm Int32(int64_t value, Span span=Span())
Construct a scalar int32 constant.
Definition: expr.h:402
Definition: type.h:71
static PointerType VoidPointerTy(ffi::String storage_scope="")
Construct an opaque pointer with void element type.
Base class for other IR constructs that can be converted to PrimExpr. This is useful for the FFI to c...
Definition: base_expr.h:391
Managed reference to PrimExprConvertibleNode.
Definition: base_expr.h:402
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Definition: base_expr.h:113
static PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
static PrimType Void()
Construct the void sentinel type, encoded as handle(0, 0).
static PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
Definition: source_map.h:111
Node to represent a buffer.
Definition: buffer.h:71
BufferNode()
constructor
Definition: buffer.h:132
Var data
The pointer to the head of the data.
Definition: buffer.h:78
PrimType ElementType() const
Definition: buffer.h:164
Span span
Span that points to the original source code. Reserved debug information.
Definition: buffer.h:120
ffi::Array< PrimExpr > shape
The type of the buffer prior to flattening.
Definition: buffer.h:87
PrimType dtype
dtype in the content of the tensor
Definition: buffer.h:80
static void RegisterReflection()
Definition: buffer.h:134
ffi::String name
optional name of the buffer
Definition: buffer.h:106
DLDataType DefaultIndexType() const
Definition: buffer.h:159
int offset_factor
Factor of elem_offset field, elem_offset is guaranteed to be multiple of offset_factor.
Definition: buffer.h:113
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: buffer.h:177
PrimExpr elem_offset
The offset in terms of number of dtype elements (including lanes)
Definition: buffer.h:103
ffi::Array< PrimExpr > allocated_addr
The allocated address of the buffer. The address might be multi-dimensional based on its scope....
Definition: buffer.h:129
ffi::Optional< Layout > layout
The layout of the buffer.
Definition: buffer.h:123
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Buffer", BufferNode, ffi::Object)
ffi::Array< PrimExpr > ElemOffset(ffi::Array< PrimExpr > index, bool inner=false) const
Determine the offset in the buffer of the given index.
ffi::Array< IntImm > axis_separators
Separators between input axes when generating flattened output axes.
Definition: buffer.h:96
ffi::Array< PrimExpr > strides
The strides of each dimension This can be an empty array, indicating array is contiguous.
Definition: buffer.h:101
int data_alignment
Alignment requirement of data pointer in bytes.
Definition: buffer.h:108
BufferType buffer_type
buffer type
Definition: buffer.h:115
Buffer is a symbolic n-darray structure. It is a composition of primitive symbolic types,...
Definition: buffer.h:187
PrimExpr vload(ffi::Array< PrimExpr > begin, PrimType dtype, ffi::Optional< PrimExpr > predicate=std::nullopt) const
Create an Expr that does a vector load at begin index.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Buffer, ffi::ObjectRef, BufferNode)
PrimType ElementType() const
Definition: buffer.h:285
Buffer MakeSlice(ffi::Array< PrimExpr > begins, ffi::Array< PrimExpr > extents) const
Make a new symbolic buffer representing a slice of the buffer.
Buffer with_data(Var data) const
Return a new buffer with the data.
Buffer with_allocated_addr(ffi::Array< PrimExpr > allocated_addr) const
Return a new buffer with the allocated address.
PrimExpr OffsetOf_p(const ffi::Array< PrimExpr > &indices) const
Get the buffer_offset op for the given index.
bool IsScalar(bool alloc_or_decl=true) const
Return true if the buffer is a scalar.
Buffer MakeStrideView() const
Return a new buffer that is equivalent with current one but always add stride field.
TVM_DEFINE_OBJECT_REF_COW_METHOD(BufferNode)
ffi::Array< PrimExpr > OffsetOf(ffi::Array< PrimExpr > index) const
Determine the offset in the buffer of the given index.
Buffer(Var data, PrimType dtype, ffi::Array< PrimExpr > shape, ffi::Array< PrimExpr > strides, PrimExpr elem_offset, ffi::String name, int data_alignment, int offset_factor, BufferType buffer_type, ffi::Array< IntImm > axis_separators={}, Span span=Span(), ffi::Optional< Layout > layout=std::nullopt, ffi::Array< PrimExpr > allocated_addr={})
ffi::String scope() const
Return the storage scope associated with this buffer.
Buffer with_dtype(PrimType dtype) const
Return a new buffer with the dtype.
Expr access_ptr(int access_mask, PointerType ptr_type=PointerType::VoidPointerTy(), int content_lanes=1, PrimExpr offset=IntImm::Int32(0), ffi::Optional< PrimExpr > input_extent=std::nullopt) const
Get access ptr to the entire buffer.
Buffer GetFlattenedBuffer() const
Get a flattened version of the buffer.
Stmt vstore(ffi::Array< PrimExpr > begin, PrimExpr value, ffi::Optional< PrimExpr > predicate=std::nullopt) const
Create a Stmt that does a vector store at begin index.
Base node for data producers.
Definition: buffer.h:324
virtual ffi::Array< PrimExpr > GetShape() const =0
Get the shape of the result.
virtual ~DataProducerNode()
destructor.
Definition: buffer.h:327
virtual ffi::String GetNameHint() const =0
Get the name hint of the data producer.
virtual PrimType GetDataType() const =0
Get the raw element dtype of the result.
TVM_FFI_DECLARE_OBJECT_INFO("tirx.DataProducer", DataProducerNode, PrimExprConvertibleNode)
Managed reference to DataProducerNode.
Definition: buffer.h:350
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DataProducer, PrimExprConvertible, DataProducerNode)
Container of all statements.
Definition: stmt.h:64
a named variable in TIR
Definition: var.h:68
Base expr nodes in TVM.
Definition of layout.
constexpr const char * axis_separators
Marks the physical axis separators.
Definition: stmt.h:233
tirx::Buffer BufferWithOffsetAlignment(ffi::Array< PrimExpr > shape, PrimType dtype, std::string name, int data_alignment, int offset_factor, bool compact, std::string memory_scope="")
Creates TIR Buffer for provided parameters.
PrimType DefaultIndexPrimType()
if TVM_INDEX_DEFAULT_I64 is set, return int64, otherwise return int32
Definition: buffer.h:43
Buffer decl_buffer(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::String name="buffer", ffi::String storage_scope="", ffi::Optional< ffi::Array< IntImm >> axis_separators=std::nullopt, Span span=Span())
Construct a new buffer given shape, and dtype.
DLDataType DefaultIndexType()
Definition: buffer.h:52
BufferType
buffer type
Definition: buffer.h:64
@ kDefault
Definition: buffer.h:65
@ kAutoBroadcast
Definition: buffer.h:67
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:2010
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Variables in the TIR.