Skip to content

Commit ffccd86

Browse files
committed
[REFACTOR][IR] Use PrimType for compiler dtypes
Unify compiler-facing PrimExpr dtype state around PrimType as the source of expression type truth, while keeping runtime/storage dtype values as raw DLDataType at ABI and storage boundaries. This squashes the PrimType refactor, review/CI follow-ups, raw dtype boundary repairs, and the benchmark-confirmed rewrite hot-path recovery into one PR commit.
1 parent 4b0a039 commit ffccd86

434 files changed

Lines changed: 6790 additions & 5986 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

include/tvm/ir/base_expr.h

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
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+
20+
/*!
21+
* \file tvm/ir/base_expr.h
22+
* \brief Base expression and primitive type nodes.
23+
*/
24+
#ifndef TVM_IR_BASE_EXPR_H_
25+
#define TVM_IR_BASE_EXPR_H_
26+
27+
#include <tvm/ffi/cast.h>
28+
#include <tvm/ffi/dtype.h>
29+
#include <tvm/ffi/reflection/registry.h>
30+
#include <tvm/ir/source_map.h>
31+
32+
#include <cstddef>
33+
#include <cstdint>
34+
35+
namespace tvm {
36+
37+
/*!
38+
* \brief Type is the base type of all types.
39+
*
40+
* TVM's type system contains following subclasses:
41+
*
42+
* - PrimType: type of primitive type values used in the low-level IR.
43+
* - FuncType: type of a function.
44+
* - TensorType: type of certain Tensor values in the expression.
45+
*
46+
* There are also advanced types to support generic(polymorphic types).
47+
* \sa Type
48+
*/
49+
class TypeNode : public ffi::Object {
50+
public:
51+
/*!
52+
* \brief Span that points to the original source code.
53+
* Reserved debug information.
54+
*/
55+
mutable Span span;
56+
57+
static void RegisterReflection() {
58+
namespace refl = tvm::ffi::reflection;
59+
// span do not participate in structural equal and hash.
60+
refl::ObjectDef<TypeNode>().def_ro("span", &TypeNode::span, refl::DefaultValue(Span()),
61+
refl::AttachFieldFlag::SEqHashIgnore());
62+
}
63+
64+
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
65+
66+
static constexpr const uint32_t _type_child_slots = 14;
67+
TVM_FFI_DECLARE_OBJECT_INFO("ir.Type", TypeNode, ffi::Object);
68+
};
69+
70+
/*!
71+
* \brief Managed reference to TypeNode.
72+
* \sa TypeNode
73+
*/
74+
class Type : public ffi::ObjectRef {
75+
public:
76+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Type, ffi::ObjectRef, TypeNode);
77+
};
78+
79+
/*!
80+
* \brief Primitive data types used in the low-level IR.
81+
*
82+
* PrimType represents POD-values and handles that are
83+
* not automatically managed by the runtime.
84+
*
85+
* \sa PrimType
86+
*/
87+
class PrimTypeNode final : public TypeNode {
88+
public:
89+
/*!
90+
* \brief The raw DLPack dtype represented by this primitive type.
91+
*/
92+
DLDataType dtype;
93+
94+
static void RegisterReflection() {
95+
namespace refl = tvm::ffi::reflection;
96+
refl::ObjectDef<PrimTypeNode>().def_ro("dtype", &PrimTypeNode::dtype);
97+
}
98+
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.PrimType", PrimTypeNode, TypeNode);
99+
};
100+
101+
/*
102+
* \brief Managed reference to PrimTypeNode.
103+
* \sa PrimTypeNode
104+
*/
105+
class PrimType final : public Type {
106+
public:
107+
/*!
108+
* \brief Construct from a raw DLPack dtype.
109+
* \param dtype The corresponding DLPack dtype.
110+
*/
111+
TVM_DLL explicit PrimType(DLDataType dtype);
112+
113+
/*!
114+
* \brief Construct from DLPack dtype fields.
115+
* \param code The DLPack dtype code.
116+
* \param bits The scalar bit width.
117+
* \param lanes The fixed lane count.
118+
*/
119+
TVM_DLL PrimType(DLDataTypeCode code, int bits, int lanes = 1);
120+
121+
/*! \brief Construct a signed integer type with fixed lanes. */
122+
TVM_DLL static PrimType Int(int bits, int lanes = 1);
123+
/*! \brief Construct an unsigned integer type with fixed lanes. */
124+
TVM_DLL static PrimType UInt(int bits, int lanes = 1);
125+
/*! \brief Construct a floating-point type with fixed lanes. */
126+
TVM_DLL static PrimType Float(int bits, int lanes = 1);
127+
/*! \brief Construct a bfloat type with fixed lanes. */
128+
TVM_DLL static PrimType BFloat(int bits, int lanes = 1);
129+
/*! \brief Construct a boolean type with fixed lanes. */
130+
TVM_DLL static PrimType Bool(int lanes = 1);
131+
/*! \brief Construct an opaque handle type. */
132+
TVM_DLL static PrimType Handle(int bits = 64, int lanes = 1);
133+
/*! \brief Construct the void sentinel type, encoded as handle(0, 0). */
134+
TVM_DLL static PrimType Void();
135+
/*!
136+
* \brief Construct a scalable vector type.
137+
* \param code The DLPack dtype code.
138+
* \param bits The scalar bit width.
139+
* \param lanes The positive vscale factor to encode in the DLPack lane field.
140+
*/
141+
TVM_DLL static PrimType ScalableVector(DLDataTypeCode code, int bits, int lanes);
142+
143+
/*! \return The DLPack dtype code. */
144+
TVM_FFI_INLINE DLDataTypeCode code() const {
145+
return static_cast<DLDataTypeCode>(static_cast<int>(get()->dtype.code));
146+
}
147+
148+
/*! \return The scalar bit width. */
149+
TVM_FFI_INLINE int32_t bits() const { return get()->dtype.bits; }
150+
151+
/*!
152+
* \return The fixed lane count.
153+
* \note Throws on scalable vector types, where the encoded lane field stores a vscale factor.
154+
*/
155+
TVM_FFI_INLINE int32_t lanes() const {
156+
int16_t encoded_lanes = static_cast<int16_t>(get()->dtype.lanes);
157+
if (TVM_FFI_PREDICT_FALSE(encoded_lanes < 0)) {
158+
TVM_FFI_THROW(InternalError)
159+
<< "Can't fetch the lanes of a scalable vector at a compile time.";
160+
}
161+
return encoded_lanes;
162+
}
163+
164+
/*!
165+
* \brief Check the scalar element code and bit width.
166+
* \note Lane count and scalable-vector encoding are intentionally ignored.
167+
*/
168+
TVM_FFI_INLINE bool MatchesElementType(DLDataTypeCode code, int bits) const {
169+
DLDataType dtype = get()->dtype;
170+
return dtype.code == static_cast<uint8_t>(code) && dtype.bits == bits;
171+
}
172+
173+
/*!
174+
* \brief Check whether the dtype code matches any of the provided DLPack codes.
175+
* \note Bit width and lanes are intentionally ignored.
176+
*/
177+
template <typename... Codes>
178+
TVM_FFI_INLINE bool MatchesCode(Codes... codes) const {
179+
uint8_t dtype_code = get()->dtype.code;
180+
return ((dtype_code == static_cast<uint8_t>(codes)) || ...);
181+
}
182+
183+
/*! \brief Whether this type is a scalar, excluding fixed and scalable vectors. */
184+
TVM_FFI_INLINE bool IsScalar() const {
185+
int16_t encoded_lanes = static_cast<int16_t>(get()->dtype.lanes);
186+
return encoded_lanes == 1;
187+
}
188+
189+
/*! \brief Whether this type is the void sentinel `handle(0, 0)`. */
190+
TVM_FFI_INLINE bool IsVoid() const {
191+
DLDataType dtype = get()->dtype;
192+
return dtype.code == static_cast<uint8_t>(DLDataTypeCode::kDLOpaqueHandle) && dtype.bits == 0 &&
193+
static_cast<int16_t>(dtype.lanes) == 0;
194+
}
195+
196+
/*! \brief Whether this type is an opaque handle, excluding the void sentinel. */
197+
TVM_FFI_INLINE bool IsHandle() const {
198+
return this->code() == DLDataTypeCode::kDLOpaqueHandle && !this->IsVoid();
199+
}
200+
201+
/*! \brief Whether this type is a scalable vector. */
202+
TVM_FFI_INLINE bool IsScalableVector() const {
203+
return static_cast<int16_t>(get()->dtype.lanes) < -1;
204+
}
205+
206+
/*! \brief Whether this type is a fixed-length vector. */
207+
TVM_FFI_INLINE bool IsFixedLengthVector() const {
208+
return static_cast<int16_t>(get()->dtype.lanes) > 1;
209+
}
210+
211+
/*!
212+
* \brief Return the number of bytes needed to store one value of this type.
213+
*
214+
* This uses the same packed sub-byte dtype sizing rule as runtime tensors.
215+
* Scalable vector types have no compile-time storage size and are rejected.
216+
*/
217+
TVM_DLL size_t StorageBytes() const;
218+
219+
/*! \brief Return the same type with a different dtype code, preserving bits and lanes. */
220+
TVM_FFI_INLINE PrimType WithCode(DLDataTypeCode code) const {
221+
DLDataType dtype = get()->dtype;
222+
int16_t encoded_lanes = static_cast<int16_t>(dtype.lanes);
223+
if (encoded_lanes < -1) {
224+
return ScalableVector(code, dtype.bits, -encoded_lanes);
225+
}
226+
return PrimType(code, dtype.bits, encoded_lanes);
227+
}
228+
229+
/*! \brief Return the same type with a different scalar bit width, preserving code and lanes. */
230+
TVM_FFI_INLINE PrimType WithBits(int bits) const {
231+
DLDataType dtype = get()->dtype;
232+
int16_t encoded_lanes = static_cast<int16_t>(dtype.lanes);
233+
if (encoded_lanes < -1) {
234+
return ScalableVector(this->code(), bits, -encoded_lanes);
235+
}
236+
return PrimType(this->code(), bits, encoded_lanes);
237+
}
238+
239+
/*! \brief Return the same scalar element type with a fixed lane count. */
240+
TVM_FFI_INLINE PrimType WithLanes(int lanes) const {
241+
return PrimType(this->code(), this->bits(), lanes);
242+
}
243+
244+
/*! \return The vscale factor encoded in a scalable vector type. */
245+
TVM_FFI_INLINE int32_t VScaleFactor() const {
246+
int16_t encoded_lanes = static_cast<int16_t>(get()->dtype.lanes);
247+
if (encoded_lanes >= -1) {
248+
TVM_FFI_THROW(InternalError) << "A fixed length vector doesn't have a vscale factor.";
249+
}
250+
return -encoded_lanes;
251+
}
252+
253+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PrimType, Type, PrimTypeNode);
254+
};
255+
256+
inline bool operator==(const PrimType& lhs, const PrimType& rhs) {
257+
return lhs->dtype == rhs->dtype;
258+
}
259+
260+
inline bool operator!=(const PrimType& lhs, const PrimType& rhs) { return !(lhs == rhs); }
261+
262+
/*!
263+
* \brief Base type of all the expressions.
264+
* \sa Expr
265+
*/
266+
class BaseExprNode : public ffi::Object {
267+
public:
268+
/*!
269+
* \brief Span that points to the original source code.
270+
* Reserved debug information.
271+
*/
272+
mutable Span span;
273+
274+
/*!
275+
* \brief The deduced or annotated type of the expression.
276+
*
277+
* This field is intentionally nullable because type information may
278+
* be populated by later analysis passes instead of expression
279+
* constructors.
280+
*/
281+
mutable Type ty;
282+
283+
static void RegisterReflection() {
284+
namespace refl = tvm::ffi::reflection;
285+
// span and ty do not participate in structural equal and hash.
286+
refl::ObjectDef<BaseExprNode>()
287+
.def_ro("span", &BaseExprNode::span, refl::DefaultValue(Span()),
288+
refl::AttachFieldFlag::SEqHashIgnore())
289+
.def_ro("ty", &BaseExprNode::ty, refl::DefaultValue(Type()),
290+
refl::AttachFieldFlag::SEqHashIgnore());
291+
}
292+
293+
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
294+
295+
static constexpr const uint32_t _type_child_slots = 64;
296+
TVM_FFI_DECLARE_OBJECT_INFO("ir.BaseExpr", BaseExprNode, ffi::Object);
297+
};
298+
299+
/*!
300+
* \brief Managed reference to BaseExprNode.
301+
* \sa BaseExprNode
302+
*/
303+
class BaseExpr : public ffi::ObjectRef {
304+
public:
305+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(BaseExpr, ffi::ObjectRef, BaseExprNode);
306+
};
307+
308+
namespace ffi {
309+
template <>
310+
inline constexpr bool use_default_type_traits_v<PrimType> = false;
311+
312+
template <>
313+
struct TypeTraits<PrimType> : public ObjectRefWithFallbackTraitsBase<PrimType, DLDataType> {
314+
TVM_FFI_INLINE static PrimType ConvertFallbackValue(DLDataType dtype) { return PrimType(dtype); }
315+
};
316+
} // namespace ffi
317+
318+
} // namespace tvm
319+
320+
#endif // TVM_IR_BASE_EXPR_H_

0 commit comments

Comments
 (0)