Interactive C/C++ workflows in Jupyter using clang-repl

About

Anutosh profile picture Anutosh Bhat
Software engineer at QuantStack
Contributes to the xeus stack
Contributes to LLVM

Interactive C/C++ workflows in Jupyter using clang-repl

REPL

REPL1 diagram

REPL

REPL2 diagram

REPL

REPL3 diagram

Project Jupyter

JUPYTER1 diagram

Motivation: Why C++ Needs a REPL

Scientific Computing is Exploratory

  • Scientists and engineers don’t just write software — they explore
  • They iterate rapidly: write code, run it, visualize, inspect, repeat

Interpreted Languages Dominate This Space

  • Tools like Python and R excel due to their REPL-driven workflows
  • C++ is powerful, but its compile-run-debug loop is friction-heavy

C++ in a REPL = The Best of Both Worlds

  • Projects like Cling showed that interactive C++ is possible
  • C++ in a REPL format opens doors for teaching, rapid prototyping, and scientific computing

C++ Kernel through cling and Xeus-cling

  • cling — https://github.com/root-project/cling
  • xeus-cling — https://github.com/jupyter-xeus/xeus-cling
  • Blog — https://blog.jupyter.org/interactive-workflows-for-c-with-jupyter-fe9b54227d92
  • The above blog received ~500k views, highlighting the demand for interactive C++

C++ Kernel through cling and Xeus-cling

cling diagram

The Xeus Universe

What does Xeus even mean ?

  • xeus is a C++ library which simplifies the implementation of kernels for Jupyter
  • developers can focus on implementing the interpreter part of the kernel
  • Tutorial for creating a kernel based on xeus: https://xeus.readthedocs.io/en/latest/kernel_implementation.html
  • xeus is architected in components that can be replaced with ad-hoc implementations

The Xeus Universe

xeus-python xeus-python demo
xeus-cling xeus-cling demo
xeus-lua xeus-lua demo
xeus-sql xeus-sql demo
xeus-sqlite xeus-sqlite demo
xeus-robot xeus-robot demo

Xeus Architecture

architecture diagram

The need for a new C++ kernel

Limitations of Cling

  • Based on an outdated version of Clang
  • Limited capabilities and compatibility with modern C++ features
  • Hard to maintain on all the platforms

xeus-cpp, based on Clang

  • Recent versions of Clang incorporated REPL in their codebase
  • Extensible architecture based on plugins
  • Supports modern versions of C++, and many features (CUDA, python)

Clang-repl's Design

architecture diagram

Example Kernel Spec

kernel-spec diagram

🎉 LIVE DEMO 🎉

Runtime Execution: Native vs Wasm

Clang-Repl on Native Platforms

  • Uses LLVM’s ORC JIT for runtime compilation
  • Compiles code & executes directly from memory

Clang-Repl in the Browser

  • WebAssembly enforces a strict sandbox model
  • Can’t write or modify executable memory at runtime
  • Memory is separated into code and data (Harvard architecture)

WASM backend for Clang-Repl

LLVM 17 introduced a WASM-specific IncrementalExecutor that avoids LLVM's ORC JIT

The new WasmIncrementalExecutor handles the wasm execution as follows:

  • Each REPL input is parsed into a Partial Translation Unit (PTU)
  • PTU is lowered to LLVM IR, then compiled to a WASM object file
  • The object is linked with wasm-ld into a standalone binary (incr_module_x.wasm)
  • The side module is dynamically loaded with emscripten’s dlopen, extending the main module

WASM backend for Clang-Repl

These modules:

  • Share the same memory as the main wasm module
  • Resolve symbols from earlier cells (cross-cell linking)
  • Mimic dynamic linking, even though WASM doesn’t support shared libraries traditionally

🚀 This model effectively turned Clang-Repl into a live REPL for WebAssembly, enabling dynamic incremental C++ in the browser!

Timeit Magic

Timeit magic demo

Near Future Work

  • Last Value Printing
  • Debugger Support
  • CUDA Support
  • Python Interoperatability

Last Value Printing

Last Value Printing demo

Debugger Support

CUDA Support

Python Interoperability

Acknowledgements

Compiler Research Group

Compiler Research Group logo

Google Summer of Code

Google Summer of Code logo

The END

Questions?

QR code to slides

Scan for slides