Comments (6)
A Value
can't contain a string. It can only contain one of the integer, boolean, float, vector and reference types. You can use builder.func.dfg.value_type()
to get the type of a value.
from cranelift-jit-demo.
@bjorn3
Thank you very much for your quick response :) I checked your suggestion, and it worked.
let lhs_type = self.builder.func.dfg.value_type(lhs);
let is_int = lhs_type.is_int(); // true
A Value can't contain a string.
Sorry for a vague question. But I meant, if every data is saved as int
like in this demo, is there a way to check data type?
For example, in this demo, I think String can be changed to a boxed_slice
then it can be saved like in this function:
https://github.com/bytecodealliance/cranelift-jit-demo/blob/main/src/jit.rs#L90
I think this data is treated as int
here:
https://github.com/bytecodealliance/cranelift-jit-demo/blob/main/src/jit.rs#L378
As another example of implementation, I checked lust
language that uses Cranelift-jit, and it was using immediate
to check the data type:
https://github.com/ezekiiel/lust/blob/main/lustc/src/conversions.rs
Though I am not the author, my understanding on the code above is that, it is adding some bits to a immediate, and those bits are representing data type of the immediate, so that the immediate can contain value and its data type together, even if they are always int
to cranelift-jit.
Cranelift-jit natively supports something like this?
I am sorry if my question is very beginner and doesn't make sense..
from cranelift-jit-demo.
Sorry for a vague question. But I meant, if every data is saved as int like in this demo, is there a way to check data type?
Pointers on 64bit systems are represented as i64. Cranelift doesn't store any information about what it points to. You as user of Cranelift are responsible for storing this information if you need it.
Though I am not the author, my understanding on the code above is that, it is adding some bits to a immediate, and those bits are representing data type of the immediate, so that the immediate can contain value and its data type together, even if they are always int to cranelift-jit.
I think that is pointer tagging I think. Pointer tagging is something done at runtime when it isn't known at compile time what the type of the value is.
Cranelift-jit natively supports something like this?
No, everyone does pointer tagging in their own way as everyone has their own set of types.
from cranelift-jit-demo.
@bjorn3
Thank you very much again for your quick response...!
OK, I understand, now your answer cleared my questions.
I appreciate you a lot for your actions. 🙇
from cranelift-jit-demo.
One idea might be, create a struct at first:
LangValue {
type: usize,
value: SomeValue,
}
We can change this struct to int by:
let l = LangValue {
type: 1,
value: value,
};
let intval = Box::into_raw(Box::new(l)) as i64;
let jit_intval = self.builder.ins().iconst(types::I64, intval);
And this jit_intval
can be passed to rust functions defined in jit world like:
#[no_mangle]
pub extern "C" fn test1(ptr: *mut LangValue) {
let r = unsafe {Box::from_raw(ptr)};
println!("test: {:?}", r);
}
So that we can pass more information to rust functions defined in jit world even if we always use pointer_type
which is actually i64 type. One warning is that we must always use Box::from_raw
for values created by Box::into_raw(Box::new(l))
. Otherwise it might lead to memory leak.
from cranelift-jit-demo.
Or using ArgumentPurpose::StructReturn
or returning multiple values from a function might be good too to handle more information.
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
- Is there a way to view the JIT compiled code? HOT 3
- Add example that uses `load` and `store`
- How do you have multiple functions in the same JIT compilation or do you have to compile them individually? HOT 22
- 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.