Skip to content

Add plugins of the shell, and the extension points a plugin and a Session subclass need - #2907

Open
ganeshgore wants to merge 3 commits into
session-tclfrom
shell-plugins
Open

ganeshgore wants to merge 3 commits into
session-tclfrom
shell-plugins

Conversation

@ganeshgore

Copy link
Copy Markdown
Collaborator

Stacked on #2906 (base session-tcl). It lets a project built on top of OpenFPGA add its own commands to the shell and its own methods to the Python Session, without patching OpenFPGA.

What it adds

Plugins of the shell (commit 1)

  • OPENFPGA_PLUGINS lists the plugins of a build (none by default), found in openfpga/plugins or in OPENFPGA_PLUGIN_DIRS. A plugin is a static library made with openfpga_add_plugin(NAME VERSION SOURCES) and a function register_plugin_<name>(PluginApi&); the executable, the Tcl module and the Python module get the same plugins.
  • The shell records who registered each command. A plugin may add commands, override how one runs (and add options), remove one (not a basic one, nor one others depend on), and hook a command before (and refuse it), after, or after every command. A name defined twice throws ShellRegistrationError and stops the start with the reason.
  • report_plugins (schema plugins), the start title, version and help say what each plugin did; Session.plugins() reads it; checkpoints and journals record the plugins and refuse another set.
  • openfpga/plugins/example shows each feature, with its test.

Extension points for plugins (commit 2)

  • query_answer.h: the helpers of the session reports (answer_query, stage_data, count_option, string_option, add_value_option, add_query_command, QUERY_LIST_DEFAULT_LIMIT) move out of session_report_commands.cpp, which uses them.
  • Session attachments: JSON data of a plugin kept with the architectures (set_attachment, attachment, clear_attachment, attachment_keys), cleared when an architecture is read, listed by report_session, set from a script with the new command set_attachment.
  • Reading an architecture also forgets a channel width made read only by an owner (set_fixed_channel_width_owner) and the options that read cached device files (set_cache_options). make_flow_device() makes the device at once, stage_generation() tells when a stage completed again.
  • PluginApi::register_app_option(name, default, stage); report_app_option --json gives each option its owner.
  • IoLocationMap::empty() and io_ports(). nlohmann/json is vendored in libs/EXTERNAL (header only, left out of format-cpp) to check attachments.

Extension points for a Session subclass (commit 3)

  • restore() calls documented hooks in order: options, _restore_inputs(manifest, directory), _restore_extra(extra, directory) (what _checkpoint_extra(directory) saved under extra), the attachments, then the circuit and the results.
  • RemoteSession(session_class="module:Class") / OPENFPGA_SESSION_CLASS runs a subclass in the worker, which makes and restores its session with it; the worker takes the methods of Session plus Class.REMOTE_METHODS.
  • Session.set_attachment(), attachments(); the journal keeps a copy of each attachment value; AppOption.owner.

Tests

  • C++: test_shell_plugins (registrars, overrides, removals, hooks), test_openfpga_session (attachments, fixed width owner, cache options, stage generation, plugin options), test_example_plugin (with -DOPENFPGA_PLUGINS=example).
  • Python: test_plugins, test_session_extensions (attachments, owners, subclass hooks, session class of a worker, journal of attachments).
  • Full openfpga_flow/tasks/python_binding_tests and the ctest subset pass in a build without plugins (3 tests of the example plugin skip) and in a build with the example plugin.

Not included

  • Runtime (dlopen) plugins: plugins are built with the tree, and the headers are not a stable interface for plugins built apart.
  • pyproject.toml sets no plugin.

A plugin of the shell is a static library that is built into OpenFPGA:
OPENFPGA_PLUGINS lists the plugins of a build (none by default), found in
openfpga/plugins or in a folder of OPENFPGA_PLUGIN_DIRS. Each one is made
with openfpga_add_plugin(NAME VERSION SOURCES) and has a function
register_plugin_<name>(PluginApi&), which CMake lists so that the plugins
register in order, after the commands of the shell and before the basic
ones. The executable, the Tcl module and the Python module have the same
plugins.

The shell records who registered each command. A name defined twice now
throws ShellRegistrationError instead of returning an invalid id, and a
plugin that cannot register stops the start with the reason. A plugin may
override how a command runs and add options to it, remove a command (not a
basic one, nor one that others depend on), and hook a command: before it
(and refuse it), after it (with its status), or after every command.
OpenfpgaContext keeps one object of each type for the data of a plugin
(extension<T>()).

report_plugins answers with what each plugin added, overrode, removed and
hooked (schema plugins); the start title, version and help name them.
Session.plugins() reads it, and the checkpoints and journals of a Session
record the plugins of the build: a checkpoint or a journal of a build with
other plugins is not restored or replayed, as their commands may not do the
same.

openfpga/plugins/example shows each feature, with its test.
The helpers of the reports of the shell move to query_answer.h, so that
the commands of a plugin answer as they do: answer_query() checks the
stage, runs a query on the data of VPR and gives the answer to the caller,
with stage_data(), count_option(), string_option(), add_value_option(),
add_query_command() and QUERY_LIST_DEFAULT_LIMIT.

A session keeps attachments: data of a plugin, as JSON, under a key that
starts with its name (set_attachment(), attachment(), clear_attachment(),
attachment_keys()). Reading an architecture, failing to, and reset() clear
them. report_session lists them, and the new command set_attachment sets
one from a script or a restore. nlohmann/json is vendored in libs/EXTERNAL,
which format-cpp leaves out, to check them.

Reading an architecture also forgets two more things that a plugin can set
with the architectures it reads: the owner of a channel width that is read
only (set_fixed_channel_width_owner(); set_app_option and reset_app_option
refuse another value of router.route_chan_width, and say who fixed it), and
the options that read cached files of the device (set_cache_options()),
which go back to their default. make_flow_device() makes the device at
once, and stage_generation() tells when a stage completed again.

PluginApi::register_app_option() adds an option named <plugin>.<name>
with the stage it tunes; report_app_option --json gives each option its
owner (vpr, openfpga or the plugin). The example plugin adds one.

IoLocationMap gets empty() and io_ports(), plain accessors of its map.
Session.set_attachment() and attachments() read and write the attachments
of the session; a checkpoint keeps them, and restore() sets again those the
restored session does not have. The journal keeps a copy of the value of
each set_attachment, as it does of the scripts of Yosys.

restore() now calls documented hooks, in order, on the new session: the
options are set, _restore_inputs(manifest, directory) reads the
architectures (a subclass that reads them another way overrides it), then
_restore_extra(extra, directory) gets back what _checkpoint_extra(directory)
saved under "extra" in checkpoint.json, then the attachments are set, and
the circuit and the results of the stages are read. A subclass no longer
relies on the order of the calls inside restore().

RemoteSession(session_class="module:Class"), or OPENFPGA_SESSION_CLASS, runs
a subclass of Session in the worker, which makes and restores its session
with it (also RemoteSession.restore(..., session_class=...)). The worker takes
the public methods of Session and those that Class.REMOTE_METHODS lists.
AppOption.owner says whose an option is.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant