ohua-dev / ohua-core

Core Haskell library for the compiler
https://ohua-dev.github.io
Eclipse Public License 1.0
5 stars 0 forks source link

Unnecessary `ctrl` operator with no arguments #32

Closed Feliix42 closed 5 years ago

Feliix42 commented 5 years ago

The current version of ohua-core (cab3ff9ee43fa3fb6dedff7c869345c9026805e5) and I think the current version from master as well produce an unnecessary ctrl operator for this simple smap algorithm:

ns some_ns;

use sf smap::smap_fns::{gen_input, printout, splice};

fn main() -> Vec<String> {
    let input = gen_input();
    for i in input {
        let s = splice(i);
        printout(s);
        s
    }
}

This leads to the problem that incorrect code is being generated for the ctrl operator, as there is nothing to context. The current workaround is that the backend contains an optimization pass that identifies these ctrl operators and removes them, leaving behind a DeadEndArc that just discards all inputs. But as @sertel pointed out it would be wiser to do this in an earlier step in core.

alang-resolved|dflang-initial|dflang-core

sertel commented 5 years ago

The problem is that we always create a ctrl assuming that we always need to context a variable or independent function. This is only true for conditionals because these do not have an input, i.e., the branches themselves are independent functions. This is not true for smap and recur.

The right thing to do is to alter the transformations for both of these constructs in such a way that they do not produce a ctrl when there is nothing to be contexted. This is trivial but has a little twist: smapFun and recur always have an output arc for the ctrl. If we do not create a ctrl then the versions of smapFun and recur must also change, i.e., to versions without this ctrl. This would require to implement them twice at the backend and would probably lead to code duplication.

That's why we might want to consider leaving the concept of DeadEndArcs in and only make sure that we do not create the ctrl.

Comments, opinions?

Feliix42 commented 5 years ago

I agree with your view that the ctrl operator in question should not be created but the concept of DeadEndArcs should be retained. This allows for a rather smooth fix that avoids unnecessary duplicate code and is probably the easiest to implement.

JustusAdam commented 5 years ago

I think I fixed it and I'll close the issue. Should more problems arise feel free to reopen

JustusAdam commented 5 years ago

I'm pretty sure the problems with unused ctrl args are done.

However a related issue has arisen, which I am now tracking with #33