Skip to main content

Language server for navigating FFI calls

Project description

FFI Navigator

Most modern IDEs support find function definition within the same language(e.g. python or c++). However, it is very hard to do that for cross-language FFI calls. While solving this general problem can be very technically challenging, we can get around it by building a project-specific analyzer that matches the FFI registration code patterns and recovers the necessary information.

This project is an example of that. Currently, it supports the PackedFunc FFI in the Apache TVM project. It is implemented as a language server that provides getDefinition function for FFI calls and returns the location of the corresponding C++ API in the TVM project. It complements the IDE tools that support navigation within the same language.

Structure

  • python/ffi_navigator The analysis code and language server
  • vscode-extension language server extension for vscode

Features

  • Find definition/references of PackedFunc on the python and c++ side.
  • Find definition/references of Object/Node on the python and c++ side.
    • move cursor to the object class name on the python side.
    • move cursor to the type key string on the c++ side.

These action works for a python symbol that refers a global PackedFunc(e.g. python/tvm/api.py:L59 _api_internal._min_value) and function name strings that occur in TVM_REGISTER_GLOBAL, @register_func and GetPackedFunc. Here are some examples you can try:

  • python/tvm/api.py:L59 move cursor to _api_internal._min_value and run goto definition.
  • src/relay/backend/compile_engine.cc:L728 runtime::Registry::Get("relay.backend.lower"), move cursor to the name and run goto definition.
  • python/tvm/relay/expr.py:L191 move cursor to Tuple and run goto definition. You can also try out find references in all these cases

Installation

Install python package

pip install --user ffi-navigator

For developing the python package locally, we can just make sure ffi_navigator is in your python path in bashrc.

export PYTHONPATH=${PYTHONPATH}:/path/to/ffi-navigator/python

You can also directly install fft_navigator to system Python packages so that you don't need to setup PYTHONPATH. Note that if you choose to install the package, you have to re-install it everytime you update the package.

python setup.py install

VSCode

See vscode-extension

Emacs

Install lsp-mode

Add the following configuration

(lsp-register-client
 (make-lsp-client
  :new-connection (lsp-stdio-connection '("python3" "-m" "ffi_navigator.langserver"))
  :major-modes '(python-mode c++-mode)
  :server-id 'ffi-navigator
  :add-on? t))

Set the project root to be /path/to/tvm using M-x lsp-workspace-folders-add [RET] /path/to/tvm Try out the goto definition by opening a python file

  • Move cursor to python/tvm/api.py line 59 _api_internal._min_value, type M-x lsp-find-definition

if you use eglot instead, check out this PR. eglot does not support multiple servers per language at the moment, the PR above contains a workaround.

Adding Support for New FFI Patterns

The project is modularized into dialect namespace which is FFI convention(or even project) specific and generic parts. You will be able to extend the support by adding a new dialect.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ffi-navigator-0.3.2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ffi_navigator-0.3.2-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file ffi-navigator-0.3.2.tar.gz.

File metadata

  • Download URL: ffi-navigator-0.3.2.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.9

File hashes

Hashes for ffi-navigator-0.3.2.tar.gz
Algorithm Hash digest
SHA256 227c2b524f772a46f12766592e3bd6021222da20844f4c1ac301168d5a17feb9
MD5 665236faeb4ab3cceef7ac3ce600318c
BLAKE2b-256 cab0daa9ad5c7261e3eb36c41d9bd9e4a7c318426c09dc3cd60dd5d691a742d1

See more details on using hashes here.

File details

Details for the file ffi_navigator-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: ffi_navigator-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.9

File hashes

Hashes for ffi_navigator-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 749b3443c9462df0beb6e74cb36e42e214e5ca99f9d8e37ae9c364b6cdb2be6f
MD5 91345bc79154e233d15c2421d82bbc97
BLAKE2b-256 6bf473be444fdc0ebe5ad1519ceeed1ead14a4cf7928cc2332e3450494bdc5a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page