AnyDSL / thorin

The Higher-Order Intermediate Representation

Home Page:https://anydsl.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Partial evaluator too eager

madmann91 opened this issue · comments

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.

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.