[RFC] Bringing interactive MLIR compilation and execution to WebAssembly

Hi All,

I’m Anutosh, a core developer and contributor across clang/clang-repl and Project Jupyter—particularly JupyterLite and several Jupyter language kernels—and I currently work at QuantStack. Some of y’all might remember me from my talk at FOSDEM’s LLVM track on “Building Interactive C/C++ workflows in Jupyter through clang-repl” this Jan, 2026.

I would like to propose an upstream WebAssembly execution path for MLIR, together with the build support needed to use MLIR interactively from Python, C and C++ in a wasm32-unknown-emscripten process.

My prototypes run in JupyterLite, a distribution of JupyterLab that runs entirely in the browser, powered by WebAssembly. The language kernels, MLIR libraries, compiler pipeline, linker and generated programs all execute locally in the browser; no remote Python process or compiler service is involved.

I now have working prototypes in three JupyterLite kernels. From Python, C or C++23, users can construct or parse MLIR, run pass pipelines, inspect each lowering stage, compile an LLVM-dialect module into WebAssembly, dynamically load it and execute a resolved symbol.

The Python notebook is currently the most fully explored interface. It renders compiler graphs using an in-process WebAssembly build of Graphviz, a widely used graph-visualization toolkit, and passes the translated LLVM IR directly to llvmlite, the lightweight Python binding to LLVM used by Numba.

Here’s the demo for the same : https://youtu.be/ojzvWo7EgXI
(I had to upload it on Youtube because discourse won’t let me share a mp4 :slightly_frowning_face:)

TL;DR

  • MLIR can now run interactively inside the browser. MLIR has been built for wasm32-unknown-emscripten, with the same packaged runtime exercised through Jupyter language kernels for Python, C and C++, all running entirely in the browser through JupyterLite.
  • The complete compilation and execution pipeline can run as WebAssembly. The execution engine translates MLIR to LLVM IR, emits a position-independent WebAssembly object, invokes an in-process WebAssembly build of LLD to link it into a shared WebAssembly module, loads that module with dlopen, and resolves its exported C-interface symbols—all within the browser’s WebAssembly environment.
  • The execution engine is language-independent. The stable MLIR C API provides the boundary used by the C and C++ prototypes. The C++23 notebook adds RAII, exceptions and typed wrappers without depending on a fragile cross-module C++ ABI.
  • The approach builds on established WebAssembly execution infrastructure. It extends the model previously developed by us (Vassil Vassilev, Anubhab Ghosh & Me) for Clang-Repl and closely related out-of-tree MLIR work by Maksim Levental in llvm/eudsl.
  • The working implementation is ready to be divided into focused upstream patches. Feedback on the overall direction and API boundaries would be valuable during that process. Emscripten-based execution tests should also be feasible, following the existing approach used to build and run Clang-Repl’s interactive tests for WebAssembly.

Working notebook demoed above:

The same workflow has also been validated through C23 and C++23 Jupyter language kernels. Both prototypes load the packaged MLIR runtime, inspect the Linalg, Affine and LLVM-dialect stages, link the generated WebAssembly module using in-process LLD, and execute the compiled function—confirming that the execution path is not specific to Python. Shareable deployments of these notebooks remain packaging work in progress !

Here’s the demo for the C bindings : https://www.youtube.com/watch?v=FLrkBtuEr8s

Problem Statement

MLIR is particularly well suited to teaching and experimentation because an interesting compiler pipeline is not a single black-box invocation. A user may want to:

  1. construct or parse an operation;
  2. inspect the current dialect;
  3. apply one pass or a complete pass pipeline;
  4. inspect the transformed Intermediate Representation (IR);
  5. visualize control flow or operation structure;
  6. translate to LLVM IR;
  7. execute and validate the generated code.

Today, doing this generally requires a local LLVM/MLIR build or access to a server that owns one. MLIR offers C++, C and Python interfaces, but the native libraries and standard execution engine depend on facilities that are not directly available in an Emscripten browser process.

The goal is to make this complete learning and experimentation loop shareable as a notebook URL that runs locally in the reader’s browser—with no installation or remote compiler service required.

Motivation and prior work

From Clang-Repl to C++ in JupyterLite

This proposal follows a path that began in Clang-Repl.

Clang-Repl normally uses LLVM’s ORC JIT. For the browser, we added a different execution path: emit WebAssembly object files, link them into shared side modules and dynamically load each new module into the long-running process.
The initial support landed in llvm-project#86402, followed by fixes for shared Wasm binaries, browser execution, target creation, dynamic-library loading and temporary-file handling:

That work became the foundation for running xeus-cpp, Jupyter’s C & C++ kernels, completely inside JupyterLite. The browser can incrementally compile and execute C and C++ code and load libraries from the Emscripten ecosystem.

Examples:

MLIR precedent in llvm/eudsl

Maksim Levental (@makslevental ) later applied the same model to MLIR in llvm/eudsl#248. That project lowers an MLIR LLVM-dialect module to a Wasm object, links a shared module and loads it in the browser, explicitly referencing Clang-Repl’s Wasm backend.

This is important prior art and I do not want to present the execution idea as new or independent of EUDSL. The opportunity here is to consolidate the common design and move the generally useful parts into upstream MLIR, while retaining runtime- or distribution-specific integration outside LLVM where appropriate.

Numba JIT & llvmlite

More recently, the Numba JIT compiler was brought to the browser and can now compile and execute Python and NumPy functions through a Jupyter Python kernel running entirely in JupyterLite. The implementation follows similar fundamental execution model explained above.

The complete story and technical background are described in my blog : Numba in the Browser: Unlocking a New Scientific Python Stack in JupyterLite.

Interactive demo : Numba and its downstream scientific Python ecosystem

This has already enabled browser-native experiments across a much broader scientific Python ecosystem: probabilistic programming and statistical modeling with PyTensor and PyMC, numerical interpolation with Interpolation․py, economic modeling with Dolo․py, and large-scale data visualization with Datashader. It demonstrates the broader opportunity: a reusable compiler foundation in JupyterLite can unlock entire scientific workflows rather than a single isolated demonstration. For eg : a DataShader example

The same runtime from Python, C and C++

The same packaged MLIR runtime has been exercised through Jupyter language kernels for Python, C and C++. In each case, users can parse MLIR, run lowering and optimization passes, inspect the intermediate representations, compile the LLVM-dialect result into WebAssembly, and execute the generated function inside the browser.

Python currently provides the most polished interface, but the underlying execution path is language-independent. The C and C++ notebooks access MLIR through its stable C API, avoiding dependence on a version-sensitive cross-module C++ ABI. This makes the reusable core an MLIR WebAssembly execution and dynamic-loading foundation rather than a Python-specific implementation

Proposal and current patch series

A working implementation is already available as a patch series in emscripten-forge. Think of emscripten-forge as Emscripten + conda-forge: a conda-forge-style ecosystem for building and distributing packages for the emscripten-wasm32 platform.

Emscripten-forge was created at QuantStack to support browser-native Jupyter environments. QuantStack originated JupyterLite and is deeply involved in the conda-forge ecosystem, but conda-forge does not currently target emscripten-wasm32. A separate package ecosystem was therefore needed for the compilers, runtimes and libraries used by JupyterLite.

Today, emscripten-forge powers browser-based Jupyter language kernels for Python, C, C++, R, OCaml, JavaScript, Fortran, Haskell among others. The C and C++ demonstrations above, for example, depend on WebAssembly builds of LLVM, Clang and LLD distributed through emscripten-forge. The same ecosystem now provides Graphviz, MLIR’s Python bindings, llvmlite, Numba and much of the scientific Python stack used in these demonstrations.

The current MLIR patches demonstrate a complete working workflow in this environment, while their final upstream APIs and boundaries remain open for discussion.

1. Building MLIR and its Python bindings for WebAssembly with Emscripten

The build system is adapted to cross-compile MLIR’s libraries, C API and composable Python bindings for wasm32-unknown-emscripten. This includes handling the target Python ABI, nanobind extension modules, WebAssembly shared libraries and symbol isolation between independently packaged LLVM-based modules.

Current patch: Emscripten and WebAssembly execution-engine CMake support

2. Providing a language-independent WebAssembly execution API

A public C API provides the core operations needed to compile an LLVM-dialect MLIR module, link the resulting WebAssembly object into a shared module, load it dynamically and resolve exported functions. This API is usable from C, C++ and Python bindings.

Current patches:

3. Exposing the execution engine to Python

A small nanobind module and Python wrapper expose compilation, dynamic loading and symbol lookup. The wrapper can also return translated LLVM IR, enabling inspection and composition with LLVM-based tools such as llvmlite.

Current patches:

4. Supporting target-sized MemRef descriptors

MLIR’s Python runtime helpers currently assume 64-bit fields for MemRef offsets, shapes and strides. Wasm32 lowering uses 32-bit index fields. The implementation makes the descriptor width explicit while preserving 64-bit behavior as the native default.

Current patch: Target-sized NumPy-to-MemRef descriptors

5. Visualizing MLIR pipelines in the browser

Interactive compiler environments benefit greatly from being able to inspect control-flow graphs and lowering pipelines. Helpers such as show_cfg and show_dot provide this workflow directly inside the notebook.

The standard Python Graphviz workflow launches the external dot executable through a subprocess. Browser-based Emscripten environments do not provide conventional process creation, so this approach cannot be used in JupyterLite. Instead, DOT rendering uses Graphviz compiled to WebAssembly and invoked in-process through its C API, without launching a subprocess.

One possible upstream direction is to expose DOT generated by MLIR’s existing operation-graph infrastructure and leave the choice of rendering mechanism to the frontend.

Current patch: MLIR graph and in-process Graphviz helpers

6. Loading packaged MLIR libraries in Emscripten

The Python package initializer preloads the MLIR shared libraries before importing extension modules. It accounts for how Emscripten’s dynamic loader resolves dependency names and ensures that the libraries are available through the global dynamic-symbol table.

This may ultimately remain a distribution-level concern rather than becoming part of upstream MLIR.

Current patch: Emscripten shared-library preloader

7. Testing the WebAssembly workflow

The upstream work should include focused tests for parsing and transforming MLIR, repeated in-process linking, scalar and MemRef execution, dynamic module loading and symbol resolution. Clang-Repl already builds and runs interactive tests under Emscripten, providing a useful precedent for testing this workflow.

The existing package tests exercise the composable Python bindings, WebAssembly execution engine, target-sized MemRef descriptors and in-process Graphviz integration.

Current tests: Emscripten MLIR package tests

NOTE : These patches should be treated as evidence of a working end-to-end implementation, not as a requirement that every downstream detail be upstreamed unchanged. The purpose of this RFC is to agree on the reusable boundaries and then submit the work as small, independently reviewable changes.

Discussion

i) Why not Compiler Explorer?

This proposal is complementary to Compiler Explorer, not a replacement for it.

Compiler Explorer offers broad access to many compilers, versions and targets through remote native processes. This proposal explores a narrower but deeper, entirely client-side workflow.

In JupyterLite, MLIR, its pass manager, the WebAssembly build of LLD and the generated code all run inside the browser. Users can modify lowering pipelines, inspect each intermediate representation, execute the result and share the complete experiment as a notebook URL.

The workflow goes all the way from high-level MLIR dialects to LLVM IR and finally to identifiable WebAssembly modules such as mlir_conv_bias_relu_o0.wasm, making every stage available for inspection. The screenshot below shows the generated module loaded by the browser after passing through the MLIR WebAssembly execution engine.

The trade-off is clear: Compiler Explorer supports far more toolchains; a JupyterLite MLIR environment offers a zero-install, executable compiler laboratory.

This direction was partly inspired by educational projects such as llvm-tutor and
MLIR’s Toy tutorial. I first shared the prototype with Andrzej Warzynski ( @banach-space ) because an “mlir-tutor in the browser” felt like a natural teaching use case. His initial questions about Compiler Explorer, lowering paths and performance helped shape this RFC.

ii) Performance: What the current result means

The combined demonstration implements a fixed Conv2D + bias + ReLU kernel, lowers it through MLIR and then optionally optimizes the translated LLVM IR with llvmlite.

In the recorded browser run, the target-aware O3 variant is approximately:

  • about 60× faster than a pedagogical NumPy reference that performs the outer
    traversal in Python and launches many tiny operations; and
  • about 1.8× faster than a vectorized NumPy reference based on sliding windows
    and einsum.

Both results are reported because the first number is largely about removing Python interpreter and dispatch overhead; it must not be read as a general claim that “MLIR is 60× faster than NumPy”. The exact result is specific to the kernel, shapes, browser, target features and pass pipeline. The notebook checks matching outputs and records both compile time and warm-kernel timing.

The educational notebook currently shows more than one lowering route. Its Affine route follows the progressive style of Stephen Diehl’s MLIR memory tutorial, but I do not propose it as the one canonical production pipeline. A browser notebook is useful precisely because users can compare Linalg-to-SCF/CF lowering, Affine-based teaching paths and, later, Transform dialect or vectorized paths.

iii) Relationship to Numba-MLIR

The 2023 MLIR Open Meeting presentation on Numba-MLIR described a pipeline that reused Numba’s frontend and type inference, lowered Numba IR through project-specific MLIR dialects, and targeted CPUs and GPUs.

That project is no longer actively maintained, but it remains useful prior art. Now that Numba itself runs in JupyterLite, a future experiment could connect Numba IR to an MLIR lowering path and make the intermediate dialects inspectable in the same browser notebook. This is not part of the first RFC; it is an example of what a maintained, upstream MLIR WebAssembly foundation could enable.

iv) Possible follow-up work

The initial proposal focuses on establishing a reusable MLIR WebAssembly compilation and execution foundation. Once that foundation is available, several useful directions become possible.

A natural next step would be to bring the complete MLIR Toy tutorial to JupyterLite. Each chapter could become an executable, zero-install notebook in which readers inspect the dialects, modify passes and run the resulting program directly in the browser.

GPU execution is another promising direction. Recent EUDSL WebGPU work demonstrates a path from MLIR through SPIR-V and WGSL to WebGPU. Integrating such a workflow with JupyterLite would require additional browser-runtime and buffer-management work, so it is better explored independently after the CPU execution path is established.

Upstream MLIR’s emerging WasmSSA dialect also creates a complementary browser-native use case: importing existing WebAssembly binaries, inspecting their structured representation and raising them into standard MLIR dialects interactively in JupyterLite.

Longer term, projects such as WAMI suggest that a direct MLIR-to-WebAssembly lowering path could complement the LLVM-based execution path described here.

Other follow-up experiments could include WebAssembly SIMD128 lowering, Transform-dialect workflows, richer upstream graph export and connections to compiler frontends that already use MLIR. These are possible applications of the proposed foundation rather than requirements for the initial upstream work.

Next steps

The implementation is working end to end, and the next step is to split it into small, independently reviewable upstream patches. The existing emscripten-forge patch series provides a concrete starting point, while leaving room to refine the API boundaries, native-platform compatibility and testing strategy during review.

I would be happy to drive this upstreaming effort and work with the relevant MLIR maintainers and contributors throughout the process. The immediate goal is not to prescribe every final API, but to establish a shared upstream foundation for compiling, inspecting and executing MLIR through WebAssembly.

cc @stellaraccident , @mehdi_amini , @ftynse , @makslevental , @rolfmorel — given your work on MLIR’s Python bindings, C API, execution infrastructure and compiler education, your feedback on the overall direction and guidance during upstreaming would be greatly appreciated.

Any comments, concerns or suggestions are welcome!

Thanks,
Anutosh Bhat.

The reason I didn’t upstream the eudsl stuff is because you need to patch CRunnerUtils.h for compatibility with a 32b memref ABI on wasm32:

https://github.com/llvm/eudsl/blob/main/projects/mlir-python-bindings-wasm/32bCRunnerUtils.h

Without that change I thought it didn’t make sense to upstream the WasmExecutionEngine either :man_shrugging:.

FYI the Web GPU example already works: JupyterLite

Edit: I’m of course not opposed to this stuff being upstreamed, I just mean I didn’t want to figure out the correct way to fix the 32b issue so that’s why I didn’t upstream. Numpy is technically an original dep of the bindings so in principle this can land without that but it would be nice if we could decide on a way to land the 32b fix as well. Note, another thing I didn’t want to figure out was how to test this path in CI.

Anyway +1 from me and happy to review PRs on this from you @anutosh491.

Thanks @makslevental for the review :smiley:

I agree that we should tackle the 32-bit MemRef descriptor issue as part of this work. I’ll look into a clean upstream solution that preserves the current 64-bit behavior while keeping CRunnerUtils.h and np_to_memref consistent on 32-bit targets.

For testing/CI, we do build clang-repl tests for emscripten and we should be able to use ideas from there to test the framework here !

I also looked through the WebGPU work, and a good part of the underlying SPIR-V support is already upstream. Once the CPU execution path is in place, we can explore what it would take to upstream the remaining pieces as a follow-up.

(Caveat here: I’m a Wasm and LLVM person but not very knowledgeable in MLIR at all)

But if the issue is the fact that Wasm is a 32-bit platform, don’t forget that the wasm64-emscripten triple exists. Support is not yet ubiquitous in browsers, but hopefully it’s not too far out. And if you wanted to use a 64-bit ABI on top of wasm32, Binaryen has a lowering pass that converts wasm64 code to run on wasm32 platforms. Your use of dynamic loading would complicate that, but if 32-bit is an intractable problem it could be worth discussin.

A quick update on where the work behind this RFC has taken me: PyTorch, JAX
alongside ML compilers XLA and IREE, are now running entirely in the browser through JupyterLite :wink:

Thanks @makslevental for sharing EUDSL’s WebGPU work, which helped inspire the next stage! Following @dschuff’s suggestion, the latest demonstrations use wasm64, avoiding the particular wasm32 MemRef descriptor mismatch discussed above in this configuration.

I went beyond the original MLIR execution experiments to explore complete Python ML workflows, including compilation, training and pretrained models, with CPU execution through WebAssembly and GPU execution through WebGPU.

I’ve written up that journey in my blog Train in Your Tab: PyTorch, JAX and ML Compilers in the Browser, covering the motivation, compiler pipelines, upstream contributions and working notebooks.

Click on any of the notebook links below to try them directly in your browser.

Compiler and framework notebooks

Examples adapted from IREE’s PyTorch guide

Vision and speech workloads also featured in the ExecuTorch playground

The framework and compiler packages are available through emscripten-forge. Users can inspect the models, change the computation, and compile and execute it again in the notebook.

Please try them out! I’d love to hear what you think !

I wanted to share this work with @ftynse @mehdi_amini @stellaraccident @rolfmorel @banach-space @FedericoBruzzone @rengolin, given your work across MLIR. Having Python ML frameworks, compilers and training available together in an editable browser notebook opens up some exciting possibilities for experimentation, teaching and research. I’d really value your perspective on what this could enable for the wider ecosystem.

And here’s me putting the object-detection notebook to work. It found my mug, my phone and, thankfully, me too hehe.

P.S. These demos give me even more motivation to upstream the RFC’s patches, already used in the emscripten-forge recipe for latest Emscripten 6.x on wasm32 and wasm64, with wasm64 as my initial upstreaming priority.

Also @makslevental, picking up your point about CI:

This is where I’d like to bring emscripten-forge into the picture :wink:

It already has 600+ WebAssembly package recipes, including Python, NumPy, PyTorch and JAX, alongside LLVM, Clang, LLD, MLIR, Torch-MLIR, IREE’s compiler/runtime and Turbine.

More than 200 recipes use its headless-browser Python testing infrastructure. This is already part of how the distribution is built and tested, and I think there’s a good opportunity to reuse that infrastructure for MLIR:

  • Build the MLIR revision being tested, with compatible Python, NumPy and other dependencies from emscripten-forge.
  • Start by exercising the Python bindings in the browser, then extend coverage to the Wasm execution and dynamic-loading path as the patches move upstream.
  • Use the same environment to publish runnable MLIR Python and Torch-MLIR examples through JupyterLite and GitHub Pages. This is already the deployment model used in the Jupyter ecosystem, for example this github deployment, which runs a Jupyter C++ kernel entirely in the browser.

I’d start with the Python bindings and packaged dependencies already available, and use those to make more documentation examples runnable in the browser. That gives a concrete starting point without having to settle the whole testing setup immediately.

For EUDSL, would you be open to trying an emscripten-forge-backed, wasm64 xeus-python environment for the playground?

I’d like to bring the existing examples over first, with matching EUDSL builds and adaptations for the Pyodide-specific browser helpers. That would also make the wider packaged stack available for experiments involving Torch-MLIR, PyTorch and IREE.

I’d really like to get this infrastructure working for LLVM/MLIR upstream, both for testing Wasm support and making compiler examples directly runnable from the documentation. I can help with the packaging and deployment, and build on that as the execution patches move forward. What do you think?