Skip to content

Commit efee448

Browse files
authored
[REFACTOR][FFI] Phase out SEqualReduce/SHashReduce (#18172)
This PR phases out old SEqualReduce/SHashReduce mechanism in favor of the new reflection mechanism via ffi/reflection. It helps us to reduce the places we need to register the reflection related information. See the current IR examples for upgrading to the new mechanism.
1 parent 3b60f1c commit efee448

Some content is hidden

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

48 files changed

+64
-3323
lines changed

docs/arch/runtime.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,6 @@ Each ``Object`` subclass will override this to register its members. Here is an
228228
refl::ObjectDef<IntImmNode>().def_ro("value", &IntImmNode::value);
229229
}
230230
231-
bool SEqualReduce(const IntImmNode* other, SEqualReducer equal) const {
232-
return equal(dtype, other->dtype) && equal(value, other->value);
233-
}
234-
235-
void SHashReduce(SHashReducer hash_reduce) const {
236-
hash_reduce(dtype);
237-
hash_reduce(value);
238-
}
239-
240231
static constexpr const char* _type_key = "ir.IntImm";
241232
TVM_DECLARE_FINAL_OBJECT_INFO(IntImmNode, PrimExprNode);
242233
};

ffi/include/tvm/ffi/object.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ class Object {
214214
static constexpr int32_t _type_depth = 0;
215215
// the structural equality and hash kind of the type
216216
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUnsupported;
217-
// extra fields used by plug-ins for attribute visiting
218-
// and structural information
219-
static constexpr const bool _type_has_method_sequal_reduce = false;
220-
static constexpr const bool _type_has_method_shash_reduce = false;
221217
// The following functions are provided by macro
222218
// TVM_FFI_DECLARE_BASE_OBJECT_INFO and TVM_DECLARE_FINAL_OBJECT_INFO
223219
/*!

ffi/tests/cpp/testing_object.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,9 @@ class TCustomFuncObj : public Object {
217217
bool SEqual(const TCustomFuncObj* other,
218218
ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> cmp) const {
219219
if (!cmp(params, other->params, true, "params")) {
220-
std::cout << "custom s_equal failed params" << std::endl;
221220
return false;
222221
}
223222
if (!cmp(body, other->body, false, "body")) {
224-
std::cout << "custom s_equal failed body" << std::endl;
225223
return false;
226224
}
227225
return true;

include/tvm/arith/analyzer.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ class ConstIntBoundNode : public Object {
9494
.def_ro("max_value", &ConstIntBoundNode::max_value);
9595
}
9696

97-
bool SEqualReduce(const ConstIntBoundNode* other, SEqualReducer equal) const {
98-
return equal(min_value, other->min_value) && equal(max_value, other->max_value);
99-
}
100-
10197
/*! \brief Number to represent +inf */
10298
static const constexpr int64_t kPosInf = std::numeric_limits<int64_t>::max();
10399
/*!
@@ -219,10 +215,6 @@ class ModularSetNode : public Object {
219215
.def_ro("base", &ModularSetNode::base);
220216
}
221217

222-
bool SEqualReduce(const ModularSetNode* other, SEqualReducer equal) const {
223-
return equal(coeff, other->coeff) && equal(base, other->base);
224-
}
225-
226218
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
227219
static constexpr const char* _type_key = "arith.ModularSet";
228220
TVM_DECLARE_FINAL_OBJECT_INFO(ModularSetNode, Object);

include/tvm/arith/int_set.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum SignType { kPositive, kNegative, kZero, kUnknown };
5757
class IntSetNode : public Object {
5858
public:
5959
static constexpr const char* _type_key = "ir.IntSet";
60-
static constexpr bool _type_has_method_sequal_reduce = false;
60+
6161
TVM_DECLARE_BASE_OBJECT_INFO(IntSetNode, Object);
6262
};
6363

include/tvm/arith/int_solver.h

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,8 @@ class IntGroupBoundsNode : public Object {
7171
.def_ro("upper", &IntGroupBoundsNode::upper);
7272
}
7373

74-
bool SEqualReduce(const IntGroupBoundsNode* other, SEqualReducer eq) const {
75-
return eq(coef, other->coef) && eq(lower, other->lower) && eq(equal, other->equal) &&
76-
eq(upper, other->upper);
77-
}
78-
79-
void SHashReduce(SHashReducer hash_reduce) const {
80-
hash_reduce(coef);
81-
hash_reduce(lower);
82-
hash_reduce(equal);
83-
hash_reduce(upper);
84-
}
85-
8674
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
87-
static constexpr const bool _type_has_method_sequal_reduce = true;
75+
8876
static constexpr const char* _type_key = "arith.IntGroupBounds";
8977
TVM_DECLARE_FINAL_OBJECT_INFO(IntGroupBoundsNode, Object);
9078
};
@@ -163,19 +151,8 @@ class IntConstraintsNode : public Object {
163151
.def_ro("relations", &IntConstraintsNode::relations);
164152
}
165153

166-
bool SEqualReduce(const IntConstraintsNode* other, SEqualReducer equal) const {
167-
return equal(variables, other->variables) && equal(ranges, other->ranges) &&
168-
equal(relations, other->relations);
169-
}
170-
171-
void SHashReduce(SHashReducer hash_reduce) const {
172-
hash_reduce(variables);
173-
hash_reduce(ranges);
174-
hash_reduce(relations);
175-
}
176-
177154
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
178-
static constexpr const bool _type_has_method_sequal_reduce = true;
155+
179156
static constexpr const char* _type_key = "arith.IntConstraints";
180157
TVM_DECLARE_FINAL_OBJECT_INFO(IntConstraintsNode, Object);
181158
};
@@ -228,20 +205,8 @@ class IntConstraintsTransformNode : public Object {
228205
.def_ro("dst_to_src", &IntConstraintsTransformNode::dst_to_src);
229206
}
230207

231-
bool SEqualReduce(const IntConstraintsTransformNode* other, SEqualReducer equal) const {
232-
return equal(src, other->src) && equal(dst, other->dst) &&
233-
equal(src_to_dst, other->src_to_dst) && equal(dst_to_src, other->dst_to_src);
234-
}
235-
236-
void SHashReduce(SHashReducer hash_reduce) const {
237-
hash_reduce(src);
238-
hash_reduce(dst);
239-
hash_reduce(src_to_dst);
240-
hash_reduce(dst_to_src);
241-
}
242-
243208
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
244-
static constexpr const bool _type_has_method_sequal_reduce = true;
209+
245210
static constexpr const char* _type_key = "arith.IntConstraintsTransform";
246211
TVM_DECLARE_FINAL_OBJECT_INFO(IntConstraintsTransformNode, Object);
247212
};

include/tvm/arith/iter_affine_map.h

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,8 @@ class IterMarkNode : public Object {
105105
.def_ro("extent", &IterMarkNode::extent);
106106
}
107107

108-
bool SEqualReduce(const IterMarkNode* other, SEqualReducer equal) const {
109-
equal->MarkGraphNode();
110-
return equal(source, other->source) && equal(extent, other->extent);
111-
}
112-
113-
void SHashReduce(SHashReducer hash_reduce) const {
114-
hash_reduce->MarkGraphNode();
115-
hash_reduce(source);
116-
hash_reduce(extent);
117-
}
118-
119108
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
120-
static constexpr const bool _type_has_method_sequal_reduce = true;
121-
static constexpr const bool _type_has_method_shash_reduce = true;
109+
122110
static constexpr const char* _type_key = "arith.IterMark";
123111
TVM_DECLARE_FINAL_OBJECT_INFO(IterMarkNode, Object);
124112
};
@@ -165,18 +153,6 @@ class IterSplitExprNode : public IterMapExprNode {
165153
.def_ro("scale", &IterSplitExprNode::scale);
166154
}
167155

168-
bool SEqualReduce(const IterSplitExprNode* other, SEqualReducer equal) const {
169-
return equal(source, other->source) && equal(lower_factor, other->lower_factor) &&
170-
equal(extent, other->extent) && equal(scale, other->scale);
171-
}
172-
173-
void SHashReduce(SHashReducer hash_reduce) const {
174-
hash_reduce(source);
175-
hash_reduce(lower_factor);
176-
hash_reduce(extent);
177-
hash_reduce(scale);
178-
}
179-
180156
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
181157
static constexpr const char* _type_key = "arith.IterSplitExpr";
182158
TVM_DECLARE_FINAL_OBJECT_INFO(IterSplitExprNode, IterMapExprNode);
@@ -232,15 +208,6 @@ class IterSumExprNode : public IterMapExprNode {
232208
.def_ro("base", &IterSumExprNode::base);
233209
}
234210

235-
bool SEqualReduce(const IterSumExprNode* other, SEqualReducer equal) const {
236-
return equal(args, other->args) && equal(base, other->base);
237-
}
238-
239-
void SHashReduce(SHashReducer hash_reduce) const {
240-
hash_reduce(args);
241-
hash_reduce(base);
242-
}
243-
244211
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
245212
static constexpr const char* _type_key = "arith.IterSumExpr";
246213
TVM_DECLARE_FINAL_OBJECT_INFO(IterSumExprNode, IterMapExprNode);

include/tvm/ir/attrs.h

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ class AttrFieldInfoNode : public Object {
8484

8585
static constexpr const char* _type_key = "ir.AttrFieldInfo";
8686
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
87-
static constexpr bool _type_has_method_sequal_reduce = false;
88-
static constexpr bool _type_has_method_shash_reduce = false;
87+
8988
TVM_DECLARE_FINAL_OBJECT_INFO(AttrFieldInfoNode, Object);
9089
};
9190

@@ -123,8 +122,7 @@ class BaseAttrsNode : public Object {
123122
bool allow_unknown = false) = 0;
124123

125124
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
126-
static constexpr const bool _type_has_method_sequal_reduce = true;
127-
static constexpr const bool _type_has_method_shash_reduce = true;
125+
128126
static constexpr const char* _type_key = "ir.Attrs";
129127
TVM_DECLARE_BASE_OBJECT_INFO(BaseAttrsNode, Object);
130128
};
@@ -154,11 +152,6 @@ class DictAttrsNode : public BaseAttrsNode {
154152
rfl::ObjectDef<DictAttrsNode>().def_ro("__dict__", &DictAttrsNode::dict);
155153
}
156154

157-
bool SEqualReduce(const DictAttrsNode* other, SEqualReducer equal) const {
158-
return equal(dict, other->dict);
159-
}
160-
161-
void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(dict); }
162155
void InitByPackedArgs(const ffi::PackedArgs& args, bool allow_unknown) final;
163156

164157
// type info
@@ -394,32 +387,6 @@ class AttrsNodeReflAdapter : public BaseAttrsNode {
394387
LOG(FATAL) << "`" << DerivedType::_type_key << "` uses new reflection mechanism for init";
395388
}
396389

397-
bool SEqualReduce(const DerivedType* other, SEqualReducer equal) const {
398-
const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(DerivedType::RuntimeTypeIndex());
399-
bool success = true;
400-
ffi::reflection::ForEachFieldInfoWithEarlyStop(
401-
type_info, [&](const TVMFFIFieldInfo* field_info) {
402-
ffi::reflection::FieldGetter field_getter(field_info);
403-
ffi::Any field_value = field_getter(self());
404-
ffi::Any other_field_value = field_getter(other);
405-
if (!equal.AnyEqual(field_value, other_field_value)) {
406-
success = false;
407-
return true;
408-
}
409-
return false;
410-
});
411-
return success;
412-
}
413-
414-
void SHashReduce(SHashReducer hash_reducer) const {
415-
const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(DerivedType::RuntimeTypeIndex());
416-
ffi::reflection::ForEachFieldInfo(type_info, [&](const TVMFFIFieldInfo* field_info) {
417-
ffi::reflection::FieldGetter field_getter(field_info);
418-
ffi::Any field_value = field_getter(self());
419-
hash_reducer(field_value);
420-
});
421-
}
422-
423390
private:
424391
DerivedType* self() const {
425392
return const_cast<DerivedType*>(static_cast<const DerivedType*>(this));

include/tvm/ir/diagnostic.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ class DiagnosticNode : public Object {
7474
.def_ro("message", &DiagnosticNode::message);
7575
}
7676

77-
bool SEqualReduce(const DiagnosticNode* other, SEqualReducer equal) const {
78-
return equal(this->level, other->level) && equal(this->span, other->span) &&
79-
equal(this->message, other->message);
80-
}
81-
8277
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
8378
static constexpr const char* _type_key = "Diagnostic";
8479
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticNode, Object);
@@ -211,10 +206,6 @@ class DiagnosticContextNode : public Object {
211206
.def_ro("diagnostics", &DiagnosticContextNode::diagnostics);
212207
}
213208

214-
bool SEqualReduce(const DiagnosticContextNode* other, SEqualReducer equal) const {
215-
return equal(module, other->module) && equal(diagnostics, other->diagnostics);
216-
}
217-
218209
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
219210
static constexpr const char* _type_key = "DiagnosticContext";
220211
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticContextNode, Object);

include/tvm/ir/env_func.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,9 @@ class EnvFuncNode : public Object {
5757
.def_ro("func", &EnvFuncNode::func, refl::AttachFieldFlag::SEqHashIgnore());
5858
}
5959

60-
bool SEqualReduce(const EnvFuncNode* other, SEqualReducer equal) const {
61-
// name uniquely identifies the env function.
62-
return name == other->name;
63-
}
64-
65-
void SHashReduce(SHashReducer hash_reduce) const {
66-
// Name uniquely identifies the env function.
67-
hash_reduce(name);
68-
}
69-
7060
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
7161
static constexpr const char* _type_key = "ir.EnvFunc";
72-
static constexpr bool _type_has_method_sequal_reduce = true;
73-
static constexpr bool _type_has_method_shash_reduce = true;
62+
7463
TVM_DECLARE_FINAL_OBJECT_INFO(EnvFuncNode, Object);
7564
};
7665

0 commit comments

Comments
 (0)