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 Value
s, 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.
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.
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 usemem::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?
@David flogar FYI https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/jit/examples/jit-minimal.rs#L90
@David flogar FYI https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/jit/examples/jit-minimal.rs#L90
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?
Sorry if this is a stupid question, I don't have much experience in this topic.
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