asteria icon indicating copy to clipboard operation
asteria copied to clipboard

RFC: Deblockification

Open lhmouse opened this issue 4 years ago • 1 comments

The program

var i = 1;
{
  i += 2;
  i *= 3;
}
std.io.putf("i = $1\n", i);  // 9

can be transformed to

var i = 1;
// {
i += 2;
i *= 3;
// }
std.io.putf("i = $1\n", i);  // 9

However this requires rewriting the IR, as folows:

var i = 1;
{
  i += 2;   // depth of local reference `i` is 1
  i *= 3;
}
std.io.putf("i = $1\n", i);  // 9
var i = 1;
// {
i += 2;    // depth of local reference `i` is now 0
i *= 3;
// }
std.io.putf("i = $1\n", i);  // 9

This can be performed during either code generation or IR solidification. We also have to apply the same transformation to not only plain blocks, but if branches, switch clauses, while bodies, etc. (note due to type completeness limitation, these cannot be Statements).

lhmouse avatar Apr 24 '20 13:04 lhmouse

This is lambda lifting and closure eliminiation is the classical sense, or if you're using ANF it can be directly encoded in the "basic block" during transformation of high-level IR. It is error-prone to reinvent this particular kind of optimization technique.

FrankHB avatar Apr 04 '21 12:04 FrankHB

I have an idea now. This should be doable in AST.

lhmouse avatar Nov 05 '23 16:11 lhmouse