Trufflewasm: a Webassembly Interpreter on Graalvm
Total Page:16
File Type:pdf, Size:1020Kb
The University of Manchester Research TruffleWasm: A WebAssembly Interpreter on GraalVM DOI: 10.1145/3381052.3381325 Document Version Accepted author manuscript Link to publication record in Manchester Research Explorer Citation for published version (APA): Salim, S., Nisbet, A., & Luján, M. (2020). TruffleWasm: A WebAssembly Interpreter on GraalVM. In Proceedings of the ACM SIGPLAN/SIGOPS International Conference on Virtual Execution Environments (VEE'20) Association for Computing Machinery. https://doi.org/10.1145/3381052.3381325 Published in: Proceedings of the ACM SIGPLAN/SIGOPS International Conference on Virtual Execution Environments (VEE'20) Citing this paper Please note that where the full-text provided on Manchester Research Explorer is the Author Accepted Manuscript or Proof version this may differ from the final Published version. If citing, it is advised that you check and use the publisher's definitive version. General rights Copyright and moral rights for the publications made accessible in the Research Explorer are retained by the authors and/or other copyright owners and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights. Takedown policy If you believe that this document breaches copyright please refer to the University of Manchester’s Takedown Procedures [http://man.ac.uk/04Y6Bo] or contact [email protected] providing relevant details, so we can investigate your claim. Download date:03. Oct. 2021 TruffleWasm: A WebAssembly Interpreter on GraalVM Salim S. Salim Andy Nisbet Mikel Luján University of Manchester University of Manchester University of Manchester Manchester, UK Manchester, UK Manchester, UK [email protected] [email protected] [email protected] Abstract ’20), March 17, 2020, Lausanne, Switzerland. ACM, New York, NY, WebAssembly is a binary format originally designed for web- USA, 13 pages. https://doi.org/10.1145/3381052.3381325 based deployment and execution combined with JavaScript. WebAssembly can also be used for standalone programs 1 Introduction provided a WebAssembly runtime environment is available. WebAssembly, sometimes abbreviated as Wasm, is a com- This paper describes the design and implementation of pact, portable, statically typed stack-based binary format. TruffleWasm, a guest language implementation of a WebAssem- WebAssembly is easy to parse, specialise, and optimise com- bly hosted on Truffle and GraalVM. Truffle is a Java frame- pared to equivalent code fragments in JavaScript [8, 19, 20]. work capable of constructing and interpreting an Abstract Performance critical components of web applications can be Syntax Tree (AST) representing a program on standard JVMs. expressed in languages such as C, C++ or Rust. These com- GraalVM is a JVM with a JIT compiler which optimises the ponents are compiled into WebAssembly which typically execution of ASTs from Truffle. executes faster than the equivalent JavaScript. Thus, initial Our work is motivated by trying to understand the advan- WebAssembly implementations have focused on client-side tages and disadvantages of using GraalVM, and its support language execution on the browser to support tight inte- for multiple programming languages, to build a standalone gration with JavaScript and its APIs. Nonetheless, recent WebAssembly runtime. This contrast with developing a new development efforts have included WebAssembly targets runtime, as Wasmtime and other projects are undertaking. for standalone execution, such as on IoT, embedded, mobile TruffleWasm can execute standalone WebAssembly mod- devices, and servers. ules, while offering also interoperability with other GraalVM Standalone WebAssembly environments require a runtime hosted languages, such as Java, JavaScript, R, Python and to provide access to IO and external resources, leading to the Ruby. WebAssembly System Interface (WASI) that defines a POSIX- The experimental results compare the peak performance like interface. Standalone environments can provide their of TruffleWasm to the standalone Wasmtime runtime for own developed optimising compiler, but typically, they reuse the Shootout, C benchmarks in JetStream, and the Poly- existing back-ends such as LLVM [11]. For example, Was- BenchC benchmarks. The results show the geo-mean peak mer (see Section 7) provides an option to choose between performance of TruffleWasm is 4% slower than Wasmtime different deployment specific compiler back-ends balancing for Shootout/JetStream, and 4% faster for PolyBenchC. compilation time vs. quality of generated code. Browser en- CCS Concepts • Software and its engineering → Inter- gines typically use less aggressive optimisations with Ahead preters; Runtime environments; of Time (AoT) compilation because of the relatively short execution times that are expected for client-side scenarios. Keywords WebAssembly, Wasm, JVM, GraalVM, Just In An alternative approach to support WebAssembly could Time compilation be based on a Java Virtual Machine (JVM). Modern JVMs ACM Reference Format: provide a highly optimised managed runtime execution en- Salim S. Salim, Andy Nisbet, and Mikel Luján. 2020. TruffleWasm: A vironment that has been ported to many different target plat- WebAssembly Interpreter on GraalVM. In ACM SIGPLAN/SIGOPS forms. In addition, they support the JVM compiler interface International Conference on Virtual Execution Environments (VEE (JVMCI) which, for example, enable the Graal JIT compiler VEE ’20, March 17, 2020, Lausanne, Switzerland to be integrated with the JVMs part of the OpenJDK com- © 2020 Copyright held by the owner/author(s). Publication rights licensed munity. The Truffle framework [25] enables guest language to ACM. interpreters with competitive runtime performance to be This is the author’s version of the work. It is posted here for your personal hosted on a JVM with relatively low development effort in use. Not for redistribution. The definitive Version of Record was published comparison to native implementations. GraalVM1, is a JVM in ACM SIGPLAN/SIGOPS International Conference on Virtual Execution Environments (VEE ’20), March 17, 2020, Lausanne, Switzerland, https://doi. distribution packaged with the Graal JIT compiler which org/10.1145/3381052.3381325. 1GraalVM website https://www.graalvm.org/ VEE ’20, March 17, 2020, Lausanne, Switzerland Salim S. Salim, Andy Nisbet, and Mikel Luján provides an aggressive general set of optimisations, and spe- cific transformations for Truffle-hosted guest languages [24] Wasm Modules as well as cross programming language interoperability [7] Imports - Imports - Imports Exports [6][3]. Using a JVM provides access to a wide range of - Exports ... - Exports the Java ecosystem support tools, such as debugging and - Tables - Tables - Memories ... - Memories ... instrumentation [18], including those designed specifically for GraalVM. Cross-language interoperability in GraalVM could allow efficient embedding of WebAssembly with other Linker Truffle-hosted languages; such as GraalJS (JavaScript), FastR JS Runtime (R), GraalPython (Python), and TruffleRuby (Ruby). We present TruffleWasm, the first implementation ofaWe- Internal API bAssembly language interpreter using Truffle and GraalVM with support for WASI, and compare its performance to the WASI ... Interface to other standalone Wasmtime implementation. TruffleWasm imple- runtimes ments version 1.0 (Minimal Viable Product) of the specifica- WebAssembly Runtime tion, and passes all core WebAssembly spec-tests provided by the specification. In summary, the main contributions of Figure 1. WebAssembly modules on an abstract runtime. this paper are: • A WebAssembly interpreter implemented using Truffle specify the functionality of a whole program as shown in on the JVM that exploits partial evaluation and JIT Figure 1. In the C/C++ ecosystem, there are currently three compilation (See Section 4). ways for C/C++ front end compilers to generate WebAssem- • A peak-performance comparison between WebAssem- bly modules. By compiling core execution source code to bly using TruffleWasm and Wasmtime (See Section WebAssembly and wrapping I/O and other necessary initiali- 6). sation in JavaScript, using standardised WASI API functions, • An evaluation of WebAssembly features and how they or by providing a standalone2 module where standard library map to Truffle language implementation framework. functions are added into a module as imports. Standalone • An addition to the GraalVM ecosystem which allows runtimes must implement any imported functionality using other Truffle languages to reuse existing WebAssembly their own (library code) mechanisms. modules and libraries using Truffle interoperability (See Section 4). 2.1 Imports and Exports We select Wasmtime as the main comparison point as it is Imports and exports are key features for inter-module and the main standalone WebAssembly runtime with support for language interoperability. Imported components are expected WASI required by the benchmarks. An anecdotal inspection to be supplied by a host runtime, such as a JavaScript runtime, of relative complexity using Lines of Code as a rough and or a standalone runtime defining its own built-ins or exports ready metric shows that Wasmtime requires more than dou- from another module. Clearly, imports such as JavaScript ble the number of lines of code than TruffleWasm. The next built-ins, and memory (that describe how storage is assigned sections of the paper are organised