Skip to content

Conversation

@tqchen
Copy link
Member

@tqchen tqchen commented Jul 3, 2025

This PR introduces reflection::GlobalDef for function registration, which makes the global function registration API more closely aligned with the new reflection style.

We will send followup PRs to transition some of the existing mechanisms to the new one.

This PR introduces reflection::GlobalDef for function
registration, which makes the global function registration API
more closely aligned with the new reflection style.

We will send followup PRs to transition some of the existing
mechanisms to the new one.
@tqchen
Copy link
Member Author

tqchen commented Jul 3, 2025

New example

TVM_FFI_STATIC_INIT_BLOCK({
  namespace refl = tvm::ffi::reflection;
  refl::GlobalDef()
    .def_packed("ffi.Array", [](ffi::PackedArgs args, Any* ret) {
      *ret = Array<Any>(args.data(), args.data() + args.size());
    })
    .def("ffi.ArrayGetItem", [](const ffi::ArrayObj* n, int64_t i) -> Any { return n->at(i); })
    .def("ffi.ArraySize", [](const ffi::ArrayObj* n) -> int64_t {
      return static_cast<int64_t>(n->size());
    })
    .def_packed("ffi.Map", [](ffi::PackedArgs args, Any* ret) {
      TVM_FFI_ICHECK_EQ(args.size() % 2, 0);
      Map<Any, Any> data;
      for (int i = 0; i < args.size(); i += 2) {
        data.Set(args[i], args[i + 1]);
      }
      *ret = data;
    })
    .def("ffi.MapSize", [](const ffi::MapObj* n) -> int64_t {
      return static_cast<int64_t>(n->size());
    })
    .def("ffi.MapGetItem", [](const ffi::MapObj* n, const Any& k) -> Any { return n->at(k); })
    .def("ffi.MapCount", [](const ffi::MapObj* n, const Any& k) -> int64_t { return n->count(k); })
    .def("ffi.MapForwardIterFunctor", [](const ffi::MapObj* n) -> ffi::Function {
      return ffi::Function::FromTyped(MapForwardIterFunctor(n->begin(), n->end()));
    });
});

@Hzfengsy Hzfengsy merged commit ed4c92c into apache:main Jul 4, 2025
13 checks passed
ShiboXing pushed a commit to ShiboXing/tvm that referenced this pull request Aug 10, 2025
This PR introduces reflection::GlobalDef for function
registration, which makes the global function registration API
more closely aligned with the new reflection style.

We will send followup PRs to transition some of the existing
mechanisms to the new one.
tqchen added a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
This PR introduces reflection::GlobalDef for function
registration, which makes the global function registration API
more closely aligned with the new reflection style.

We will send followup PRs to transition some of the existing
mechanisms to the new one.
tqchen added a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
This PR introduces reflection::GlobalDef for function
registration, which makes the global function registration API
more closely aligned with the new reflection style.

We will send followup PRs to transition some of the existing
mechanisms to the new one.
tqchen added a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
This PR introduces reflection::GlobalDef for function
registration, which makes the global function registration API
more closely aligned with the new reflection style.

We will send followup PRs to transition some of the existing
mechanisms to the new one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants