spicy icon indicating copy to clipboard operation
spicy copied to clipboard

CFG: loops after a `return` cause missed optimizations

Open evantypanski opened this issue 7 months ago • 1 comments

It seems like anywhere that a loop appears makes dead code elimination not happen. Given this Hilti code:

module Test {

import hilti;

function void f() {
    local x = 5;
    return;
    while (True) {}
    hilti::print(x);
}

f();

}

The output f is:

$ HILTI_OPTIMIZER_ENABLE_CFG=1 hiltic -p test.hlt
function void f() {
    local uint<64> x = 5;
    return;

    while ( True ) {
    }

    hilti::print(x, True);
}

and replacing the while with if optimizes it correctly (with an extra block but meh):

$ HILTI_OPTIMIZER_ENABLE_CFG=1 hiltic -p test.hlt
function void f() {
    return;
    {
    }
}

Doesn't seem like this should be high priority, but it technically causes missed optimizations, for example if that store in x is expensive. I don't think it'll pop up much. I'm also not sure why it happens, though, so it might be more.

evantypanski avatar Jul 28 '25 20:07 evantypanski

This also seems to happen without the loop.

function void f() {
    local x = 5;
    return;
    hilti::print(x);
}

This code is not simplified further.

Agree on the low priority as we currently focus on patterns we generate, and I don't think we typically generate code of that form.

bbannier avatar Jul 29 '25 07:07 bbannier