Skip to content

Commit 110b8f9

Browse files
committed
[REFACTOR][FFI] Cleanup PackedFunc related redirection (apache#17923)
1 parent 7d34eb8 commit 110b8f9

File tree

11 files changed

+54
-55
lines changed

11 files changed

+54
-55
lines changed

include/tvm/ffi/function.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class FunctionObjImpl : public FunctionObj {
127127
/*! \brief The type of derived object class */
128128
using TSelf = FunctionObjImpl<TCallable>;
129129
/*!
130-
* \brief Derived object class for constructing PackedFuncObj.
130+
* \brief Derived object class for constructing ffi::FunctionObj.
131131
* \param callable The type-erased callable object.
132132
*/
133133
explicit FunctionObjImpl(TCallable callable) : callable_(callable) {
@@ -292,15 +292,15 @@ class PackedArgs {
292292

293293
/*!
294294
* \brief ffi::Function is a type-erased function.
295-
* The arguments are passed by packed format.
295+
* The arguments are passed by "packed format" via AnyView
296296
*/
297297
class Function : public ObjectRef {
298298
public:
299299
/*! \brief Constructor from null */
300300
Function(std::nullptr_t) : ObjectRef(nullptr) {} // NOLINT(*)
301301
/*!
302302
* \brief Constructing a packed function from a callable type
303-
* whose signature is consistent with `PackedFunc`
303+
* whose signature is consistent with `ffi::Function`
304304
* \param packed_call The packed function signature
305305
* \note legacy purpose, should change to Function::FromPacked for mostfuture use.
306306
*/
@@ -310,7 +310,7 @@ class Function : public ObjectRef {
310310
}
311311
/*!
312312
* \brief Constructing a packed function from a callable type
313-
* whose signature is consistent with `PackedFunc`
313+
* whose signature is consistent with `ffi::Function`
314314
* \param packed_call The packed function signature
315315
*/
316316
template <typename TCallable>
@@ -462,7 +462,7 @@ class Function : public ObjectRef {
462462
* \param callable the internal container of packed function.
463463
*/
464464
template <typename TCallable>
465-
static Function FromUnpacked(TCallable callable) {
465+
static Function FromTyped(TCallable callable) {
466466
using FuncInfo = details::FunctionInfo<TCallable>;
467467
auto call_packed = [callable](const AnyView* args, int32_t num_args, Any* rv) mutable -> void {
468468
details::unpack_call<typename FuncInfo::RetType>(
@@ -477,7 +477,7 @@ class Function : public ObjectRef {
477477
* \param name optional name attacked to the function.
478478
*/
479479
template <typename TCallable>
480-
static Function FromUnpacked(TCallable callable, std::string name) {
480+
static Function FromTyped(TCallable callable, std::string name) {
481481
using FuncInfo = details::FunctionInfo<TCallable>;
482482
auto call_packed = [callable, name](const AnyView* args, int32_t num_args,
483483
Any* rv) mutable -> void {
@@ -540,7 +540,7 @@ class Function : public ObjectRef {
540540
private:
541541
/*!
542542
* \brief Constructing a packed function from a callable type
543-
* whose signature is consistent with `PackedFunc`
543+
* whose signature is consistent with `ffi::Function`
544544
* \param packed_call The packed function signature
545545
*/
546546
template <typename TCallable>
@@ -560,16 +560,16 @@ class TypedFunction;
560560

561561
/*!
562562
* \anchor TypedFunctionAnchor
563-
* \brief A PackedFunc wrapper to provide typed function signature.
564-
* It is backed by a PackedFunc internally.
563+
* \brief A ffi::Function wrapper to provide typed function signature.
564+
* It is backed by a ffi::Function internally.
565565
*
566566
* TypedFunction enables compile time type checking.
567567
* TypedFunction works with the runtime system:
568-
* - It can be passed as an argument of PackedFunc.
569-
* - It can be assigned to TVMRetValue.
570-
* - It can be directly converted to a type-erased PackedFunc.
568+
* - It can be passed as an argument of ffi::Function.
569+
* - It can be assigned to ffi::Any.
570+
* - It can be directly converted to a type-erased ffi::Function.
571571
*
572-
* Developers should prefer TypedFunction over PackedFunc in C++ code
572+
* Developers should prefer TypedFunction over ffi::Function in C++ code
573573
* as it enables compile time checking.
574574
* We can construct a TypedFunction from a lambda function
575575
* with the same signature.
@@ -584,8 +584,8 @@ class TypedFunction;
584584
* TypedFunction<int(int)> ftyped(addone);
585585
* // invoke the function.
586586
* int y = ftyped(1);
587-
* // Can be directly converted to PackedFunc
588-
* PackedFunc packed = ftype;
587+
* // Can be directly converted to ffi::Function
588+
* ffi::Function packed = ftype;
589589
* \endcode
590590
* \tparam R The return value of the function.
591591
* \tparam Args The argument signature of the function.
@@ -623,7 +623,7 @@ class TypedFunction<R(Args...)> {
623623
template <typename FLambda, typename = typename std::enable_if<std::is_convertible<
624624
FLambda, std::function<R(Args...)>>::value>::type>
625625
TypedFunction(FLambda typed_lambda, std::string name) { // NOLINT(*)
626-
packed_ = Function::FromUnpacked(typed_lambda, name);
626+
packed_ = Function::FromTyped(typed_lambda, name);
627627
}
628628
/*!
629629
* \brief construct from a lambda function with the same signature.
@@ -646,7 +646,7 @@ class TypedFunction<R(Args...)> {
646646
template <typename FLambda, typename = typename std::enable_if<std::is_convertible<
647647
FLambda, std::function<R(Args...)>>::value>::type>
648648
TypedFunction(const FLambda& typed_lambda) { // NOLINT(*)
649-
packed_ = Function::FromUnpacked(typed_lambda);
649+
packed_ = Function::FromTyped(typed_lambda);
650650
}
651651
/*!
652652
* \brief copy assignment operator from typed lambda
@@ -668,11 +668,11 @@ class TypedFunction<R(Args...)> {
668668
std::is_convertible<FLambda,
669669
std::function<R(Args...)>>::value>::type>
670670
TSelf& operator=(FLambda typed_lambda) { // NOLINT(*)
671-
packed_ = Function::FromUnpacked(typed_lambda);
671+
packed_ = Function::FromTyped(typed_lambda);
672672
return *this;
673673
}
674674
/*!
675-
* \brief copy assignment operator from PackedFunc.
675+
* \brief copy assignment operator from ffi::Function.
676676
* \param packed The packed function.
677677
* \returns reference to self.
678678
*/
@@ -698,16 +698,16 @@ class TypedFunction<R(Args...)> {
698698
}
699699
}
700700
/*!
701-
* \brief convert to PackedFunc
702-
* \return the internal PackedFunc
701+
* \brief convert to ffi::Function
702+
* \return the internal ffi::Function
703703
*/
704704
operator Function() const { return packed(); }
705705
/*!
706-
* \return reference the internal PackedFunc
706+
* \return reference the internal ffi::Function
707707
*/
708708
const Function& packed() const& { return packed_; }
709709
/*!
710-
* \return r-value reference the internal PackedFunc
710+
* \return r-value reference the internal ffi::Function
711711
*/
712712
constexpr Function&& packed() && { return std::move(packed_); }
713713
/*! \return Whether the packed function is nullptr */
@@ -797,7 +797,7 @@ class Function::Registry {
797797
*/
798798
template <typename FLambda>
799799
Registry& set_body_typed(FLambda f) {
800-
return Register(Function::FromUnpacked(f, name_));
800+
return Register(Function::FromTyped(f, name_));
801801
}
802802

803803
/*!
@@ -838,14 +838,14 @@ class Function::Registry {
838838
// call method pointer
839839
return (target.*f)(params...);
840840
};
841-
return Register(ffi::Function::FromUnpacked(fwrap, name_));
841+
return Register(ffi::Function::FromTyped(fwrap, name_));
842842
}
843843
if constexpr (std::is_base_of_v<Object, T>) {
844844
auto fwrap = [f](const T* target, Args... params) -> R {
845845
// call method pointer
846846
return (const_cast<T*>(target)->*f)(params...);
847847
};
848-
return Register(ffi::Function::FromUnpacked(fwrap, name_));
848+
return Register(ffi::Function::FromTyped(fwrap, name_));
849849
}
850850
return *this;
851851
}
@@ -859,14 +859,14 @@ class Function::Registry {
859859
// call method pointer
860860
return (target.*f)(params...);
861861
};
862-
return Register(ffi::Function::FromUnpacked(fwrap, name_));
862+
return Register(ffi::Function::FromTyped(fwrap, name_));
863863
}
864864
if constexpr (std::is_base_of_v<Object, T>) {
865865
auto fwrap = [f](const T* target, Args... params) -> R {
866866
// call method pointer
867867
return (target->*f)(params...);
868868
};
869-
return Register(ffi::Function::FromUnpacked(fwrap, name_));
869+
return Register(ffi::Function::FromTyped(fwrap, name_));
870870
}
871871
return *this;
872872
}

include/tvm/ffi/function_details.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct FuncFunctorImpl {
7373
static constexpr size_t num_args = sizeof...(Args);
7474
// MSVC is not that friendly to in-template nested bool evaluation
7575
#ifndef _MSC_VER
76-
/*! \brief Whether this function can be converted to ffi::Function via FromUnpacked */
76+
/*! \brief Whether this function can be converted to ffi::Function via FromTyped */
7777
static constexpr bool unpacked_supported = (ArgSupported<Args> && ...) && (RetSupported<R>);
7878
#endif
7979

@@ -108,7 +108,7 @@ struct FunctionInfo<R(Args...)> : FuncFunctorImpl<R, Args...> {};
108108
template <typename R, typename... Args>
109109
struct FunctionInfo<R (*)(Args...)> : FuncFunctorImpl<R, Args...> {};
110110

111-
/*! \brief Using static function to output TypedPackedFunc signature */
111+
/*! \brief Using static function to output typed function signature */
112112
typedef std::string (*FGetFuncSignature)();
113113

114114
/*!

include/tvm/ffi/rvalue_ref.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace ffi {
5353
* \code
5454
*
5555
* void Example() {
56-
* auto append = Function::FromUnpacked([](RValueRef<Array<int>> ref, int val) -> Array<int> {
56+
* auto append = Function::FromTyped([](RValueRef<Array<int>> ref, int val) -> Array<int> {
5757
* Array<int> arr = *std::move(ref);
5858
* assert(arr.unique());
5959
* arr.push_back(val);

src/ffi/container.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class MapForwardIterFunctor {
8888

8989
TVM_FFI_REGISTER_GLOBAL("ffi.MapForwardIterFunctor")
9090
.set_body_typed([](const ffi::MapObj* n) -> ffi::Function {
91-
return ffi::Function::FromUnpacked(MapForwardIterFunctor(n->begin(), n->end()));
91+
return ffi::Function::FromTyped(MapForwardIterFunctor(n->begin(), n->end()));
9292
});
9393

9494
} // namespace ffi

src/ffi/function.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ TVM_FFI_REGISTER_GLOBAL("ffi.FunctionListGlobalNamesFunctor").set_body_typed([](
279279
return names[i];
280280
}
281281
};
282-
return tvm::ffi::Function::FromUnpacked(return_functor);
282+
return tvm::ffi::Function::FromTyped(return_functor);
283283
});
284284

285285
TVM_FFI_REGISTER_GLOBAL("ffi.String").set_body_typed([](tvm::ffi::String val) -> tvm::ffi::String {

tests/cpp/test_array.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ TEST(Array, InsertEraseRange) {
185185
}
186186

187187
TEST(Array, FuncArrayAnyArg) {
188-
Function fadd_one =
189-
Function::FromUnpacked([](Array<Any> a) -> Any { return a[0].cast<int>() + 1; });
188+
Function fadd_one = Function::FromTyped([](Array<Any> a) -> Any { return a[0].cast<int>() + 1; });
190189
EXPECT_EQ(fadd_one(Array<Any>{1}).cast<int>(), 2);
191190
}
192191

tests/cpp/test_function.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ TEST(Func, PackedArgs) {
7272
EXPECT_EQ(data[2].cast<TInt>()->value, 12);
7373
}
7474

75-
TEST(Func, FromUnpacked) {
75+
TEST(Func, FromTyped) {
7676
// try decution
77-
Function fadd1 = Function::FromUnpacked([](const int32_t& a) -> int { return a + 1; });
77+
Function fadd1 = Function::FromTyped([](const int32_t& a) -> int { return a + 1; });
7878
int b = fadd1(1).cast<int>();
7979
EXPECT_EQ(b, 2);
8080

@@ -109,14 +109,14 @@ TEST(Func, FromUnpacked) {
109109
::tvm::ffi::Error);
110110

111111
// try decution
112-
Function fpass_and_return = Function::FromUnpacked(
112+
Function fpass_and_return = Function::FromTyped(
113113
[](TInt x, int value, AnyView z) -> Function {
114114
EXPECT_EQ(x.use_count(), 2);
115115
EXPECT_EQ(x->value, value);
116116
if (auto opt = z.as<int>()) {
117117
EXPECT_EQ(value, *opt);
118118
}
119-
return Function::FromUnpacked([value](int x) -> int { return x + value; });
119+
return Function::FromTyped([value](int x) -> int { return x + value; });
120120
},
121121
"fpass_and_return");
122122
TInt a(11);
@@ -139,18 +139,18 @@ TEST(Func, FromUnpacked) {
139139
::tvm::ffi::Error);
140140

141141
Function fconcact =
142-
Function::FromUnpacked([](const String& a, const String& b) -> String { return a + b; });
142+
Function::FromTyped([](const String& a, const String& b) -> String { return a + b; });
143143
EXPECT_EQ(fconcact("abc", "def").cast<String>(), "abcdef");
144144
}
145145

146146
TEST(Func, PassReturnAny) {
147-
Function fadd_one = Function::FromUnpacked([](Any a) -> Any { return a.cast<int>() + 1; });
147+
Function fadd_one = Function::FromTyped([](Any a) -> Any { return a.cast<int>() + 1; });
148148
EXPECT_EQ(fadd_one(1).cast<int>(), 2);
149149
}
150150

151151
TEST(Func, Global) {
152152
Function::SetGlobal("testing.add1",
153-
Function::FromUnpacked([](const int32_t& a) -> int { return a + 1; }));
153+
Function::FromTyped([](const int32_t& a) -> int { return a + 1; }));
154154
auto fadd1 = Function::GetGlobalRequired("testing.add1");
155155
int b = fadd1(1).cast<int>();
156156
EXPECT_EQ(b, 2);
@@ -199,7 +199,7 @@ TEST(Func, TypedFunctionAsAnyView) {
199199
TEST(Func, ObjectRefWithFallbackTraits) {
200200
// test cases to test automatic type conversion via ObjectRefWithFallbackTraits
201201
// through TPrimExpr
202-
Function freturn_primexpr = Function::FromUnpacked([](TPrimExpr a) -> TPrimExpr { return a; });
202+
Function freturn_primexpr = Function::FromTyped([](TPrimExpr a) -> TPrimExpr { return a; });
203203

204204
auto result_int = freturn_primexpr(1).cast<TPrimExpr>();
205205
EXPECT_EQ(result_int->dtype, "int64");

tests/cpp/test_map.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ TEST(Map, AnyConvertCheck) {
243243
::tvm::ffi::Error);
244244
}
245245

246-
TEST(Map, PackedFuncGetItem) {
247-
Function f = Function::FromUnpacked([](const MapObj* n, const Any& k) -> Any { return n->at(k); },
248-
"map_get_item");
246+
TEST(Map, ffi::FunctionGetItem) {
247+
Function f = Function::FromTyped([](const MapObj* n, const Any& k) -> Any { return n->at(k); },
248+
"map_get_item");
249249
Map<String, int64_t> map{{"x", 1}, {"y", 2}};
250250
Any k("x");
251251
Any v = f(map, k);

tests/cpp/test_rvalue_ref.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace tvm::ffi::testing;
3131

3232
TEST(RValueRef, Basic) {
3333
auto append =
34-
Function::FromUnpacked([](RValueRef<Array<int>> ref, int val, bool is_unique) -> Array<int> {
34+
Function::FromTyped([](RValueRef<Array<int>> ref, int val, bool is_unique) -> Array<int> {
3535
Array<int> arr = *std::move(ref);
3636
EXPECT_EQ(arr.unique(), is_unique);
3737
arr.push_back(val);
@@ -48,7 +48,7 @@ TEST(RValueRef, Basic) {
4848

4949
TEST(RValueRef, ParamChecking) {
5050
// try decution
51-
Function fadd1 = Function::FromUnpacked([](TInt a) -> int64_t { return a->value + 1; });
51+
Function fadd1 = Function::FromTyped([](TInt a) -> int64_t { return a->value + 1; });
5252

5353
// convert that triggers error
5454
EXPECT_THROW(
@@ -65,7 +65,7 @@ TEST(RValueRef, ParamChecking) {
6565
},
6666
::tvm::ffi::Error);
6767

68-
Function fadd2 = Function::FromUnpacked([](RValueRef<Array<int>> a) -> int {
68+
Function fadd2 = Function::FromTyped([](RValueRef<Array<int>> a) -> int {
6969
Array<int> arr = *std::move(a);
7070
return arr[0] + 1;
7171
});
@@ -86,7 +86,7 @@ TEST(RValueRef, ParamChecking) {
8686
},
8787
::tvm::ffi::Error);
8888
// triggered a rvalue based conversion
89-
Function func3 = Function::FromUnpacked([](RValueRef<TPrimExpr> a) -> String {
89+
Function func3 = Function::FromTyped([](RValueRef<TPrimExpr> a) -> String {
9090
TPrimExpr expr = *std::move(a);
9191
return expr->dtype;
9292
});

tests/cpp/test_tuple.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ TEST(Tuple, AnyConvert) {
8484
EXPECT_EQ(tuple2.get<1>()->value, 2);
8585
}
8686

87-
TEST(Tuple, FromUnpacked) {
87+
TEST(Tuple, FromTyped) {
8888
// try decution
89-
Function fadd1 = Function::FromUnpacked([](const Tuple<int, TPrimExpr>& a) -> int {
89+
Function fadd1 = Function::FromTyped([](const Tuple<int, TPrimExpr>& a) -> int {
9090
return a.get<0>() + static_cast<int>(a.get<1>()->value);
9191
});
9292
int b = fadd1(Tuple<int, float>(1, 2)).cast<int>();

0 commit comments

Comments
 (0)