#wasmer #frontend #fork #cranelift #ir #merge #builder

no-std wasmer-clif-fork-frontend

Cranelift IR builder helper

8 releases (breaking)

0.59.0 Feb 26, 2020
0.52.0 Jan 15, 2020
0.44.0 Sep 26, 2019
0.43.1 Sep 24, 2019
0.31.1 Jul 24, 2019
Download history 353/week @ 2022-06-02 182/week @ 2022-06-09 365/week @ 2022-06-16 326/week @ 2022-06-23 246/week @ 2022-06-30 118/week @ 2022-07-07 211/week @ 2022-07-14 187/week @ 2022-07-21 234/week @ 2022-07-28 384/week @ 2022-08-04 372/week @ 2022-08-11 218/week @ 2022-08-18 232/week @ 2022-08-25 425/week @ 2022-09-01 698/week @ 2022-09-08 356/week @ 2022-09-15

1,714 downloads per month
Used in 12 crates (5 directly)

Apache-2.0 WITH LLVM-exception

2.5K SLoC

This is a (hopefully short-lived) fork of Cranelift so that we can continue publishing Wasmer runtime related crates on crates.io. If you're reading this and want to use Cranelift, you almost certainly want to use the version this is forked from. If you're associated with Cranelift and would like to merge any changes we have, please reach out to us at engineering@wasmer.io. We'll be submitting pull requests, too.

NOTE: this is a fork of version 0.31 of Cranelift.

This crate provides a straightforward way to create a Cranelift IR function and fill it with instructions translated from another language. It contains an SSA construction module that provides convenient methods for translating non-SSA variables into SSA Cranelift IR values via use_var and def_var calls.


Cranelift IR builder library.

Provides a straightforward way to create a Cranelift IR function and fill it with instructions corresponding to your source program written in another language.

To get started, create an FunctionBuilderContext and pass it as an argument to a FunctionBuilder.

Mutable variables and Cranelift IR values

The most interesting feature of this API is that it provides a single way to deal with all your variable problems. Indeed, the FunctionBuilder struct has a type Variable that should be an index of your source language variables. Then, through calling the functions declare_var, def_var and use_var, the FunctionBuilder will create for you all the Cranelift IR values corresponding to your variables.

This API has been designed to help you translate your mutable variables into SSA form. use_var will return the Cranelift IR value that corresponds to your mutable variable at a precise point in the program. However, if you know beforehand that one of your variables is defined only once, for instance if it is the result of an intermediate expression in an expression-based language, then you can translate it directly by the Cranelift IR value returned by the instruction builder. Using the use_var API for such an immutable variable would also work but with a slight additional overhead (the SSA algorithm does not know beforehand if a variable is immutable or not).

The moral is that you should use these three functions to handle all your mutable variables, even those that are not present in the source code but artifacts of the translation. It is up to you to keep a mapping between the mutable variables of your language and their Variable index that is used by Cranelift. Caution: as the Variable is used by Cranelift to index an array containing information about your mutable variables, when you create a new Variable with [Variable::new(var_index)] you should make sure that var_index is provided by a counter incremented by 1 each time you encounter a new mutable variable.


Here is a pseudo-program we want to transform into Cranelift IR:

function(x) {
x, y, z : i32
   y = 2;
   z = x + y;
   jump block1
   z = z + y;
   brnz y, block3;
   jump block2
   z = z - x;
   return y
   y = y - x
   jump block1

Here is how you build the corresponding Cranelift IR function using FunctionBuilderContext:

extern crate cranelift_codegen;
extern crate cranelift_frontend;

use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::types::*;
use cranelift_codegen::ir::{AbiParam, ExternalName, Function, InstBuilder, Signature};
use cranelift_codegen::isa::CallConv;
use cranelift_codegen::settings;
use cranelift_codegen::verifier::verify_function;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Position, Variable};

let mut sig = Signature::new(CallConv::SystemV);
let mut fn_builder_ctx = FunctionBuilderContext::new();
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
    let mut builder = FunctionBuilder::new(&mut func, &mut fn_builder_ctx);

    let block0 = builder.create_block();
    let block1 = builder.create_block();
    let block2 = builder.create_block();
    let block3 = builder.create_block();
    let x = Variable::new(0);
    let y = Variable::new(1);
    let z = Variable::new(2);
    builder.declare_var(x, I32);
    builder.declare_var(y, I32);
    builder.declare_var(z, I32);

        let tmp = builder.block_params(block0)[0]; // the first function parameter
        builder.def_var(x, tmp);
        let tmp = builder.ins().iconst(I32, 2);
        builder.def_var(y, tmp);
        let arg1 = builder.use_var(x);
        let arg2 = builder.use_var(y);
        let tmp = builder.ins().iadd(arg1, arg2);
        builder.def_var(z, tmp);
    builder.ins().jump(block1, &[]);

        let arg1 = builder.use_var(y);
        let arg2 = builder.use_var(z);
        let tmp = builder.ins().iadd(arg1, arg2);
        builder.def_var(z, tmp);
        let arg = builder.use_var(y);
        builder.ins().brnz(arg, block3, &[]);
    builder.ins().jump(block2, &[]);

        let arg1 = builder.use_var(z);
        let arg2 = builder.use_var(x);
        let tmp = builder.ins().isub(arg1, arg2);
        builder.def_var(z, tmp);
        let arg = builder.use_var(y);


        let arg1 = builder.use_var(y);
        let arg2 = builder.use_var(x);
        let tmp = builder.ins().isub(arg1, arg2);
        builder.def_var(y, tmp);
    builder.ins().jump(block1, &[]);


let flags = settings::Flags::new(settings::builder());
let res = verify_function(&func, &flags);
println!("{}", func.display(None));
if let Err(errors) = res {
    panic!("{}", errors);


~112K SLoC