Git Product home page Git Product logo

Comments (6)

bjorn3 avatar bjorn3 commented on July 18, 2024

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.

lechatthecat avatar lechatthecat commented on July 18, 2024

@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.

bjorn3 avatar bjorn3 commented on July 18, 2024

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.

lechatthecat avatar lechatthecat commented on July 18, 2024

@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.

lechatthecat avatar lechatthecat commented on July 18, 2024

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.

lechatthecat avatar lechatthecat commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.