Language Server Tool to Navigate Across PackedFunc FFI for IDEs like VSCode and Emacs

Right now TVM relies heavily on the PackedFunc mechanism to register C++ APIs to the frontend. Existing IDE will be able to goto definition within the same language, but not necessarily across languages.

On the other hand, it is not too hard logically to find the definition point from the python or Registry::Get(), by running a grep on TVM_REGISTER_GLOBAL and matches the related name.

One way to bake that into the IDE is to provide IDE extensions. For VSCode, there is a callback API https://code.visualstudio.com/api/references/vscode-api (registerDefinitionProvider and registerReferenceProvider) to register the navigation APIs, reference code can be found in https://github.com/microsoft/vscode-go/tree/master/src t This way we might be able to nagivate across the code boundary more easily in the VSCode.

This seems would be a fun hacking project. Open this thread to see if this is of interest the community, and anyone want to hack a bit :slight_smile:

cc @jroesch who also mentioned similar ideas

6 Likes

This would be SUPER useful! Looking forward to seeing it happen.

Hope it also works on emacs …

1 Like

I use emacs for most of my development :slight_smile: unfortunately I am not familiar with elisp, my guess is that similar thing can be hacked up but will need a different solution, unless we adopt some common interface(there is a language server protocol which might be overkill) given it is only a grep and find

OK i did some hacks, it turns out to be a harder than i first thought…

Anyway, here is an initial version https://github.com/tqchen/ffi-navigator It contains a language server, and a vscode client, you can try it out in vscode.

Because most implementations are done through the common language server protocol, we might be able to use it in other IDEs, by make use of emacs plugins that talks to the language server.

I am a complete novice to this area(code analysis, typescript, language server) so please feel free to jump in and help if it can be useful.

2 Likes

ok I’m using emacs lsp-mode, will try if it can be used from emacs

1 Like

Instruction for emacs is added https://github.com/tqchen/ffi-navigator/blob/master/README.md#emacs

2 Likes

wow great! I can do M-. (xref-find-definitions) on _min_value and it worked out of the box! I didn’t have to set lsp-workspace-folders-add.

Great, in that case I guess lsp-mode automatically guess the root correctly.

Anyway, right now the feature only contains the find def from py to cxx.

I find it is interesting to add project specific analysis that can be minimum and enhance the IDE in this way.

Would love to see how will the such kind of IDE enhancements benefit our development and code navigation

I’m playing with this, it is so much fun :slight_smile: I encourage everyone to try it (but be sure to fetch my PR at https://github.com/tqchen/tvm-ffi-navigator/pull/1, which enables fallback to pyls)

It’s great to be able to do things like this without messing with elisp or typescript.

VS code extension works beautifully! I’ve also tested that the extension can be compiled locally once and install to a remote server via Remote SSH, although fft_navigator must be available on the remote server to make it work.

I would also recommend directly installing the package so that we don’t need to setup PYTHONPATH.

improvements(instructions and features) are more than welcomed. The PYTHONPATH is mainly used to ease development purposes.

I made some more hacks, the latest set of improvements are here https://github.com/tqchen/ffi-navigator#features Feel free play with it and enhance more to add features that might helps the development

1 Like

This could also be of interest to MXNet or PyTorch dev. Supporting MXNet seems straightforward, but pytorch generates C++ code from templates during build, so it requires source build be done beforehand.

To make it easier to support other projects, we want to separate TVM-specific component into one place and make BaseServer dispatch into project-specific handlers.

I just did anothe refactor to isolate the project specific part into a folder. I will stop adding major changes at this point, I think it should be pretty easy to add new ffi dialects, and enhance the project a bit further to allow configuration of things like path to scan and so on, feel free to add features that you think are good

Looks great, thanks. Will add mxnet dialect and possibly torch too.