AnyDSL / thorin

The Higher-Order Intermediate Representation
https://anydsl.github.io
GNU Lesser General Public License v3.0
151 stars 15 forks source link

Vector comparisons #11

Closed madmann91 closed 9 years ago

madmann91 commented 9 years ago

Vector comparisons should be forbidden. Or return simd[bool * n].

leissa commented 9 years ago

simd[bool * n] is what it should yield.

madmann91 commented 9 years ago

Then I guess we need some kind of select function for that to be useful.

madmann91 commented 9 years ago

After some inspection of the code, it seems to be possible to write something insane like :

fn main() -> float {
    let t = 1.0f;
    let u = 2.78f;
    t >> u
}

This is because you have :

case SHL:
case SHR:
case OR:
case XOR:
case AND: {
    auto type = sema.check(lhs(), sema.check(rhs(), expected));
    sema.expect_num(lhs());
    sema.expect_num(rhs());
    return type;
}

That should be expect_int right ?

(This typechecks but ultimately segfaults)

leissa commented 9 years ago

yes, that's a bug.

madmann91 commented 9 years ago

So I have thought a bit about how to add these vectors of booleans and here is my plan:

Currently, the expression a ^ b or a & b doesn't typecheck when a and b are booleans. I would like to allow it, since the meaning is different from && or || (logical operators) which are lazy. This would then allow us to extend it to vectors of booleans.

Ultimately, we could add a select function (as different intrinsics for each vector size, or as a special function - ideal would be special function since its signature would be select([bool * n], [a * n], [a * n]), which is not encodable with the current framework).

Another option would be to extend the sematics of "if" so that it behaves like select when the condition is a vector of booleans.

What are your thoughts about this ?

leissa commented 9 years ago

a op b with op = {^,&,|} should work on booleans. That's a bug. And we should also allow that on simd[bool * n]

Regarding select: If you allow simd[bool * n] as condition of an if-expr, you will get sth like sierra: sierra-lang.org I'm not sure whether we want to go that route with Impala. So, we would need a function select of type fn[N: int, T](simd[bool * N], simd[T * N], simd[T * N]). But as you pointed out, we currently cannot have that as we don't have dependent types. Workaround would be some functions select4, select8, select16 and so forth.

madmann91 commented 9 years ago

I get it. Did you mention modulo on booleans, or did you mean & ? What about having a select "function" (syntactic construct) in the language itself (that would also work with booleans, not only vector of booleans) ?

leissa commented 9 years ago

yes %=& :) - edited above

leissa commented 9 years ago

regarding select in the language: I'm not too enthusiastic about that. That would require parsing, type checking, code generation and so forth. Quite a bit for a hack. I know the select4, select8, ... workaround is ugly but it does not require a hack in the frontend. However, if we had the select temporarily in the language we could at least remove that from the language again once we have dependent types. So, if you are willing to implement that and this solves an urgent issue, you are welcome to do it.

madmann91 commented 9 years ago

Alright. It is not really necessary. Particularly if you intend to have dependent types working at some point.

madmann91 commented 9 years ago

I have a patch ready. Should I create a new branch for you to review or should I push it directly ?

leissa commented 9 years ago

just push it :)

madmann91 commented 9 years ago

Marked as solved.