Cloud Compilation

Using the Wolfram Compiler to compile Wolfram Language code to WebAssembly which can be embedded and used from Cloud Notebooks.

What is WebAssembly?

WebAssembly is a binary code format for executable programs. It is typed.
Supported natively in browsers (alongside HTML, CSS and JavaScript).
Designed to be compiled into from other languages (different from JavaScript).
Tools exist to compile C/C++ and other LLVM-based languages into WebAssembly.
Runs in minimal environment, e.g. no C library – everything has to be built/provided.
Allowed to interact with the JavaScript layer.

Features

Execution of Wolfram Language code directly in the browser without having to call the server.
Can be an optimization, so if something can't be done or fails, it can go back to the server.

Use case: Plot in a Manipulate

Plot in Dynamic could use WebAssembly – to get real-time updating.
Perhaps runs interactively, then plot snaps to use the server when updating finishes.

Use case: Dynamic

The JavaScript layer has a client-side evaluator (essentially a WL interpreter in JS), but the compiler could provide much more extensive and robust support for various functions in WL.
Simple dynamic computations have their own internal evaluator, e.g. for updating UI elements. This could be replaced by code generated by compilation.

Benefits for the Wolfram Cloud

Faster experience, reduces latency.
Lower cost to the company, lower demand on servers.
Increased robustness, avoiding network and server problems.
Makes the cloud look modern, i.e. a value proposition relative to e.g. Jupyter [???].

Technology Roadmap

Initial Explorations

Q1-2 2020
A proof of concept was put together that allowed WL code to be compiled, downloaded to the browser and executed.
Core problems of security, use of RTL and C library functions were solved.
Customizing the Wolfram Compiler so it could generate Web Assembly (rather than its existing output) was done.

Expression/Box Integration

Everything in the JavaScript layer for Cloud Notebooks is based on an Expression representation. This includes notebook elements and (native) graphics.
E.g. graphics are sent to Cloud Notebooks as graphical elements such as LineBox and these are rendered.
WebAssembly programs compiled from Wolfram Language code could generate and return boxes compatible with the JavaScript layer. These would be directly used by Cloud Notebooks.
Working with an Expression layer is similar to how the Wolfram Compiler already works.
More complex graphics such as Plot are not rendered natively yet (but based on JSON data generated on the server). But it would be straight-forward to implement some cases in JS, e.g. to render Plots even if insets and ticks are not generally implemented yet.
Incidentally, compilation might also help with fully implementing native graphics, e.g. compiling the tick logic from C to WebAssembly, like we're already doing for CompressedData. That is a separate effort from this (WL-to-WebAssembly) project – unless somebody re-implements that tick logic in WL.

Resources Required

The initial exploration work came to an end due to a lack of resources. To move forward there should be a dedicated developer who knows something about the Wolfram Language and compilation.