Stream: cranelift

Topic: How can I convert this code to a "JIT"? Using cranelift


view this post on Zulip David flogar (Feb 18 2024 at 22:24):

Hello everyone, I'm learning to use Cranelift, so I appreciate your patience on this topic. I understand that a JIT, during program "compilation," translates the code into machine code. At certain points, the JIT knows the values and data types it's working with, while a compiler knows data types during compilation but not the exact values. I'm interested in creating a JIT, and at a certain point in my program, I'd like to know the values and data types I'm working with. However, based on the example provided by bytecodealliance on how to make a JIT with Cranelift, it doesn't seem to achieve exactly that.

I'll be using this example code: https://users.rust-lang.org/t/calling-a-rust-function-from-cranelift/103948. In this code, a Rust function is called from Cranelift, and it's perfect for explaining my question. Towards the end of the code, using mem::transmute, the Cranelift-generated code is converted into a function callable from Rust, which executes the Cranelift-generated code. Before that, both the result of the Rust function call and the passed parameters are simple Values, representing each value but it is not the actual value. What I want to know is, based on the provided code, how could I execute the same Rust function with Cranelift, then obtain the returned value of that function but get the real value, not a Value? So before finishing all the code, get the actual value returned by the function, do whatever with the value and then continue using cranelift. This is how I think a JIT works. If there's anything I'm missing or misunderstanding, I appreciate your help.

I have been experimenting with Cranelift, This is where I have got to. Mostly I think I understand what is going on. It seems to work as expected. The "scary" bit is taking the raw address of rustfunc and then having to declare the parameters and return type to match correctly. It seems there is no way in Rust to automate this (unless I am missing something). I was expecting to have to choose and declare an explicit calling convention, but it seems to work correctly without doing that. The other...

view this post on Zulip Jeremy Mei(梅杰) (Feb 19 2024 at 00:45):

As far as I known: Cranelift provide rust api to generate IR for function to compile. so the Value is the struct for generating the IR of value but not real data. then JIT compile the IR of function to executable code and use mem::transmute convert for other rust function call.

view this post on Zulip David flogar (Feb 19 2024 at 01:17):

Jeremy Mei(梅杰) said:

As far as I known: Cranelift provide rust api to generate IR for function to compile. so the Value is the struct for generating the IR of value but not real data. then JIT compile the IR of function to executable code and use mem::transmute convert for other rust function call.

But then in order to get the real values I have to run mem::transmute to execute simple functions? Could I see some example code or reference?

view this post on Zulip Jeremy Mei(梅杰) (Feb 19 2024 at 02:03):

@David flogar FYI https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/jit/examples/jit-minimal.rs#L90

view this post on Zulip Jeremy Mei(梅杰) (Feb 19 2024 at 02:04):

@David flogar FYI https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/jit/examples/jit-minimal.rs#L90

view this post on Zulip David flogar (Feb 19 2024 at 02:12):

Jeremy Mei(梅杰) said:

David flogar FYI https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/jit/examples/jit-minimal.rs#L90

Oh, so for every task I want to do I have to call module.get_finalized_function() and then mem::transmute() over and over again? Even though it has many functions?

view this post on Zulip David flogar (Feb 19 2024 at 02:12):

Sorry if this is a stupid question, I don't have much experience in this topic.

view this post on Zulip David flogar (Feb 19 2024 at 02:13):

But then I have to compile each function I want just as it is done there, right?


Last updated: Nov 22 2024 at 16:03 UTC