Clarification on the supported out-of-tree target extension path for Mojo 1.2+

Hello,

I am experimenting with adding an out-of-tree accelerator target to Mojo and
would appreciate clarification on the supported extension path for Mojo 1.2
and later.

With the Mojo 1.1.x toolchain, I was able to integrate an external target using
the compiler plugin mechanism:

  • load a shared library through MODULAR_COMPILER_PLUGINS /
    mojo_plugin_paths;
  • provide the target-lowering entry points;
  • contribute a late MLIR pass through
    addPostLowerToLLVMPasses(mlir::OpPassManager &);
  • consume and transform LLVM-dialect MLIR before it is translated to LLVM IR.

This integration is working with the 1.1.x toolchain, and I have not encountered
any functional limitations so far.

In the current Mojo 1.2 development source, I see a newer architecture:

  • TargetLowering and TargetLoweringRegistry;
  • late target-specific passes still running after canonicalize/CSE and before
    translation of LLVM-dialect MLIR to LLVM IR;
  • static registration through RegisterTargetLowering<T>;
  • no obvious public connection from MODULAR_COMPILER_PLUGINS or
    mojo_plugin_paths to TargetLoweringRegistry;
  • the historical PluginUtils loading path appears to have been removed.

I understood that the 1.1.x plugin interface was not a public, stable API, but
used it for this experiment. I would like to continue the work with Mojo 1.2
and would appreciate guidance on the intended migration path:

  1. Is TargetLowering and addPostLowerToLLVMPasses() intended to support
    external, out-of-tree targets in Mojo 1.2, or are they currently
    in-tree/private interfaces?

  2. What replaces the older PluginUtils /
    MODULAR_COMPILER_PLUGINS loading path? Specifically, what is the supported
    mechanism and ABI for loading or registering an external target with
    TargetLoweringRegistry?

  3. Can an out-of-tree target register a custom MLIR dialect and target triple?
    If so, how should triple dispatch and target gating such as
    isBaseTarget() be integrated?

Thanks!

Kai

Hey @kting ! Really great that you’re experimenting here. As you’ve noticed, we’re not quite ready to accept outside contributions right now, this is partly because the plugin mechanisms for various parts of the stack are actively being tidied up. We’re advising people with an interest in bringing more hardware to Mojo/MAX to reach out to alliance@modular.com, please see the ModCon announcements blog post for details.

I’ll share this post with the compiler team who may be able to provide some insight into where the compiler plugin mechanism is going!

@kting I heard back from @weiwei.chen on the compiler team. There are a few pieces of this puzzle for which the source is not available right now. We’ve committed to making MAX more open over time and rumor has it answers to your questions will become self evident in due course, stay tuned and thank you for your patience!

Yes, in the meantime, you can probably refer to how host backend is being registered to add your backend as if they are builtin ones. Similar support will be source available soon once MAX is open.

As for building backend as a real .so plugin, we are ironing out some issues wrt dynamic linking LLVM/MLIR for real .so plugins which will be available as part of Max, but the APIs will be pretty much similar to the builtin ones + plugin shim and some bazel setup stuff.

As for backend abstraction APIs/ABIs, they will evolve over time as we are adding more backend supports.

It’s not quite perfect yet for adding backends, but we have a plan and will make things more open as references. Feel free to ask us questions if you run into any specific issues with the current architecture support.

Thanks @weiwei.chen @AlisterBurt . Yes, I was debating whether to add my target directly similar to the builtin ones or backport the PluginUtils to 1.2 so I can still do the .so load (after updating the ABI ) But given your message above, I will just add it as a built-in for now. But that said there’s plenty I can do with 1.1’s way of plugin loading so I will stay on 1.1 for now and wait for the upcoming updates.

+1 @kting , we have some updates coming in a couple weeks that will make this much easier. Please stay tuned.