AnyDSL / thorin

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

Partial evaluator too eager #90

Closed madmann91 closed 6 years ago

madmann91 commented 6 years ago

The partial evaluator is too eager in its evaluation, resulting in execution of dead code. This is incorrect, as demonstrated by the following valid program:

fn @get(i: int) -> int {
    if i == 2 {
        3
    } else {
        4
    }
}

fn @test(i: int) -> int {
    let array = [1, 2, 3, 4];
    let j = get(i);
    if j == 3 {
        array(i)
    } else {
        42
    }
}

fn main() -> int {
    test(4)
}

This example triggers the assertion:

thorin/src/thorin/util/array.h:78: const T& thorin::ArrayRef<T>::operator[](size_t) const [with T = const thorin::Def*; size_t = long unsigned int]: Assertion `i < size() && "index out of bounds"' failed.

This means that the j == 3 branch is mangled even if it should not be.

leissa commented 6 years ago

f0d468f is not temporary fix but the proper way to fix this. However, the evaluator could be made more intelligent such that these cases occur less frequent. Months ago we had an experimental branch where we tried that but this is actually harder to implement than it sounds. So, I'm closing this for now.