Comments (22)
For each function you have to call define_function
once with the return value of declare_function
for the function you want to compile. If you want to call another function you would do declare_function
for this function name to get a FuncId
, then declare_func_in_func
with this FuncId
and the Function
you are currently building and finally add a call
instruction with the FuncRef
returned by declare_func_in_func
. This works even if the function you want to call isn't compiled yet. You just need to make sure it is compiled by the time you call it.
from cranelift-jit-demo.
Do I make a seperate function builder for all of them?
from cranelift-jit-demo.
Yes. You can reuse the FunctionBuilderContext
for slightly better performance, but each FunctionBuilder
only works for a single function.
from cranelift-jit-demo.
when I use the FuncRef using a builder.ins().call() I get a index out of bounds
from cranelift-jit-demo.
The return value of declare_func_in_func can only be used in the Function
which you passed to declare_func_in_func. I did recommend calling it right before inserting a call instruction instead of keeping it around to prevent accidentally using it with the wrong Function
.
from cranelift-jit-demo.
I got 2 functions to work however now my print function spits out completely wrong data again (except when you send in 0).
Also how do i get a value from the return data of call()?
from cranelift-jit-demo.
correction: The data gets completely mangled the print function prints the correct data but the data itself is inherently worng
its suppose to give me
y1 = x+1
y2 = x+2
y3 = y1+y2
when I send in 0 I get the correct value back (3)
however when I send anything above zero it sends me 251892446413572
from cranelift-jit-demo.
Also how do i get a value from the return data of call()?
You can use func.dfg.inst_results(call_inst)
where call_inst
is the return value of creating the call instruction.
however when I send anything above zero it sends me 251892446413572
Can you show the clif ir you are using? You can print the Function
right after building it using eg println!("{}", func);
.
from cranelift-jit-demo.
function u1:1(i64, i64) -> i64 windows_fastcall {
sig0 = (i64, i64) system_v
block0(v0: i64, v1: i64):
v2 = iconst.i64 0x7ff6_63df_d750
v3 = iadd_imm v0, 1
call_indirect sig0, v2(v3, v0) ; v2 = 0x7ff6_63df_d750
return v3
}
function u0:0(i64) -> i64 windows_fastcall {
sig0 = (i64, i64) -> i64 windows_fastcall
sig1 = (i64, i64) system_v
fn0 = colocated u0:0 sig0
block0(v0: i64):
v1 = iconst.i64 0x7ff6_63df_d6a0
v2 = iadd_imm v0, 1
v3 = iadd_imm v0, 2
v4 = iadd v2, v3
call_indirect sig1, v1(v4, v0) ; v1 = 0x7ff6_63df_d6a0
v5 = call fn0(v4, v3)
return v4
}
from cranelift-jit-demo.
How do you call the function from native code? Do you cast it to a function pointer of type extern "C" fn(u64) -> u64
before calling it? And are you on Windows? (I assume you are given that you use the windows calling convention. If you are not on Windows, you will have to use the system_v calling convention.)
from cranelift-jit-demo.
im using extern "sysv64" fn(i64) -> i64
. but I dont think that is the issue.
from cranelift-jit-demo.
If you use extern "sysv64"
you have to define the function as having the system_v
calling convention, not the windows_fastcall
calling convention. Never mix calling conventions. It breaks in all kinds of ways.
from cranelift-jit-demo.
Its working now.
The so for the dfg.inst_result() you have to append two results with dfg.append_result()?
I noticed only the second returned the updated data
from cranelift-jit-demo.
You shouldn't ever need to use append_results
as regular cranelift user. It is meant for in place modification of clif ir, like during optimizations. What exactly is the problem you have with inst_results
?
from cranelift-jit-demo.
Well inst results just returns an empty array.
Is the the Function
of the current Function or the Function being called?
from cranelift-jit-demo.
You should call inst_results
on the same Function
as which you added the call
instruction to. (The current function)
from cranelift-jit-demo.
The issue with that is that the current Function
is borrowed to the function builder
from cranelift-jit-demo.
You can use the func
field of the function builder.
from cranelift-jit-demo.
How do I allocate a chunk of memory?
from cranelift-jit-demo.
You did call a function to do so like malloc. Basically the same way you did do in C.
from cranelift-jit-demo.
Is there a builtin function for malloc in cranelift IR or do I need to make like a function myself and implement it? I saw there were built in address functions that use u32 instead of i64 how do I recast them or can I just do as u32
?
from cranelift-jit-demo.
Is there a builtin function for malloc in cranelift IR or do I need to make like a function myself and implement it?
You can directly import the malloc from libc. If you don't define a function yourself, cranelift-jit will try to look it up using dlsym as fallback.
I saw there were built in address functions that use u32 instead of i64 how do I recast them or can I just do as u32?
You can use the pointer_type()
function to get the right pointer type:
Line 388 in 4b12506
from cranelift-jit-demo.
Related Issues (20)
- EXC_BAD_ACCESS on OSX HOT 10
- Readme is slightly out of date
- Update for basic-blocks
- using cranelift-simplejit inside a "rust compiled to wasm" application HOT 1
- Change default branch name HOT 1
- cranelift "simplejit" backend has been renamed "jit" HOT 3
- Jit demo does "PermissionDenied" HOT 4
- how to printout the IR for learning purposes? HOT 2
- how to concatenate strings? Send or get string args HOT 2
- Disable access to libc HOT 2
- Add an example of calling rust code from the jit HOT 2
- Demo also runs in Windows HOT 2
- Not compiled on fedora with 'selinux-fix' feature
- How do I call a function with arbitrary arguments? HOT 1
- How can I get type of Value? HOT 6
- Is there a way to view the JIT compiled code? HOT 3
- Add example that uses `load` and `store`
- Ability to call JIT-compiled function without hard-coding function signature with mem::transmute HOT 2
- How to call a function written by Rust? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cranelift-jit-demo.