{"id":4320,"library":"wasmer-compiler-cranelift","title":"Wasmer Cranelift Compiler","description":"The `wasmer-compiler-cranelift` library provides the Cranelift compiler backend for the `wasmer` Python package, enabling the compilation of WebAssembly (Wasm) modules. Cranelift is known for its balanced approach, offering faster compilation times compared to LLVM, moderate runtime performance, and enhanced security against malicious Wasm input. The current Python package version is `1.1.0` (released Jan 2022), though the underlying Wasmer runtime frequently updates, with Wasmer 7.0 (Jan 2026) featuring an upgraded Cranelift backend and new capabilities for Python integration.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/wasmerio/wasmer-python","tags":["webassembly","wasm","compiler","runtime","cranelift"],"install":[{"cmd":"pip install wasmer wasmer-compiler-cranelift","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required for the core WebAssembly runtime and API.","package":"wasmer","optional":false}],"imports":[{"note":"Imports the Cranelift compiler backend.","symbol":"Compiler","correct":"from wasmer_compiler_cranelift import Compiler"},{"note":"Imports the Wasm store context.","symbol":"Store","correct":"from wasmer import Store"},{"note":"Imports the Wasm module class.","symbol":"Module","correct":"from wasmer import Module"},{"note":"Imports the Wasm instance class.","symbol":"Instance","correct":"from wasmer import Instance"},{"note":"Imports the Wasmer engine types (e.g., JIT, Native).","symbol":"engine","correct":"from wasmer import engine"},{"note":"Utility to convert WebAssembly Text (WAT) to Wasm bytes.","symbol":"wat2wasm","correct":"from wasmer import wat2wasm"}],"quickstart":{"code":"from wasmer import Store, Module, Instance, engine, wat2wasm\nfrom wasmer_compiler_cranelift import Compiler\n\n# 1. Create a Store with the Cranelift compiler\n# You can choose between JIT (Just-In-Time) or Native engine.\n# JIT is good for development and dynamic compilation.\nstore = Store(engine.JIT(Compiler))\n\n# 2. Define a WebAssembly module in WebAssembly Text Format (WAT)\nwasm_bytes = wat2wasm(\n    \"\"\"\n    (module\n      (type (func (param i32 i32) (result i32)))\n      (func (export \"sum\") (type 0) (param i32) (param i32) (result i32)\n        local.get 0\n        local.get 1\n        i32.add\n      )\n    )\n    \"\"\"\n)\n\n# 3. Compile the Wasm module\nmodule = Module(store, wasm_bytes)\n\n# 4. Instantiate the module\ninstance = Instance(module)\n\n# 5. Call an exported function\nresult = instance.exports.sum(5, 37)\nprint(f\"Result of sum(5, 37): {result}\") # Expected: 42","lang":"python","description":"This quickstart demonstrates how to initialize a Wasmer Store with the Cranelift compiler, compile a simple WebAssembly module defined in WAT format, instantiate it, and call an exported function."},"warnings":[{"fix":"Refer to the `wasmer-python` 1.x documentation for updated API usage, particularly for `Store`, `Module`, and `Instance` creation.","message":"The `wasmer-python` API, including how compilers are used, underwent significant changes with the `1.0.0` release to better align with the Wasmer C API. Code written for `0.x` versions will likely break.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Ensure both `wasmer` and `wasmer-compiler-cranelift` are installed: `pip install wasmer wasmer-compiler-cranelift`.","message":"`wasmer-compiler-cranelift` is a companion package and requires the `wasmer` package to be installed alongside it to function correctly. It is not a standalone runtime.","severity":"gotcha","affected_versions":"All"},{"fix":"Consider `wasmer-compiler-llvm` for production workloads requiring maximum execution speed: `from wasmer_compiler_llvm import Compiler`.","message":"While Cranelift offers faster compilation, the `wasmer-compiler-llvm` backend typically provides approximately 50% better runtime performance for production environments where execution speed is critical. Choose the compiler based on your specific needs (fast compilation vs. fast execution).","severity":"gotcha","affected_versions":"All"},{"fix":"Verify that a compatible binary wheel is available for your platform/Python version. Check the official `wasmer` documentation or PyPI for supported environments. You might need to build from source in some cases.","message":"If a pre-compiled binary wheel for `wasmer` (and its compilers) is not available for your specific system or Python version, a fallback pure-Python wheel might be installed. This can lead to an `ImportError` at runtime with the message 'Wasmer is not available on this system'.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}