Giter Site home page Giter Site logo

Comments (3)

leissa avatar leissa commented on May 24, 2024 1

So, here is the deal:

Thorin at its core is designed to be type-safe. This means you have to track all array sizes explicitly in the type. In order to support unsafe features such as C-like arrays, we need unsafe axioms such as :bitcast. On top of that, indices of arity ⊤∷nat such as 23∷(int ⊤∷nat) encode unsafe indices which makes «⊤∷nat; r32» an unsafe array.

If I understand your use case correctly, the real solution would be to simply not use an unsafe array but a safe array at the allocation. Later on, you can cast the safe array to an unsafe one like so:

N: nat = ...; // doesn't need to be a constant btw
_1: [:mem, :ptr («N; r32», 0∷nat)] = :alloc («N; r32», 0∷nat) mem1;
_2: :ptr («⊤∷nat; r32», 0∷nat) = :bitcast (:ptr («⊤∷nat; r32», 0∷nat), :ptr («N; r32», 0∷nat)) _1#1∷i1;

And here a little background regarding the :alloc vs :malloc:

Both things mean the same thing. The only difference is that the size computation in :alloc happens implicitly by looking at its type whereas :malloc performs this computation explicitly. This just makes our life in the backend a little bit easier, as we have the size computation now explicitly at the term level. I find this situation a bit unpleasant and would love to see a solution where we don't need two axioms which are basically doing the same thing. What is more, the size argument of :malloc isn't type-safe, which is even more weird. I'm happy to hear suggestions :)

Finally, the LLVM backend should probably throw an exception with a reasonable error message instead of just asserting or returning "<TODO>" but this is already covered in issue #9.

Does this solve your issue?

from thorin2.

leissa avatar leissa commented on May 24, 2024 1

An unsafe Array only makes sense when having a pointer to it and :ptr («3∷nat; r32», 0∷nat) really is the same thing as float* in C. So there is no way for Thorin knowing the size. What is more, if you have an array like «n; r32», this is not a pair consisting of the actual array and a "size" field initialized to n. It is just an array of size n where n is some other term that occurs in the program. This leaves you with the following options.

  1. Use safe arrays instead of unsafe ones. For example, the signature of a matmul would look like this:
    [T: *, n: nat, m: nat, o: nat] -> [a: :ptr «n, m; T», b: :ptr «m, o; T»] -> :ptr «n, o; T»
    
    (I'm taking some liberties with the syntax here, but I guess you see the point).
  2. Instead of directly using Thorin arrays, use this type: [n: Nat, :ptr «n; r32»].
    This is a pair of size and and array of exactly that size.
  3. Alternatively, you can you do sth like this: [i64, :ptr «⊤∷nat; r32»].
    This is basically this:
    struct MyArray {
        size_t size;
        float* array;
    };

Note that with approach 2) and 3) you duplicate size information when doing sth like the matmul. Finally, 3) is an unsafe C-style way of doing things and is only present in Thorin in order to communicate with unsafe code and should only be used when you really have to do this.

from thorin2.

NeuralCoder3 avatar NeuralCoder3 commented on May 24, 2024

In my use case, I get provided with an unsafe array (_2 in the example above) for instance as an argument in a function.
I then want to create an array of the same size.
Therefore, the allocation would need to know the size of the provided array at runtime.
Is this possible or do I have to rely on the user to also provide me with the second array?

from thorin2.

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.