Skip to content

For-loop produces a redundant BLOCK #45

@Synix4Life

Description

@Synix4Life

Description

When using a for loop, the Kongruent intermediate representation generates a redundant BLOCK. This block encapsulates both the loop condition variable (if defined in the for statement) and the while loop, even though the while loop already forms a block (WHILE_START -> WHILE_END).

Example

Given the shader.kong code:

var sum: float = 0.0;
for (var i: float = 0.0; i < 4.0; i = i + 1.0) {
        ...
}
return sum

This results in the IR code:

$84 = LOAD_FLOAT_CONSTANT 0.000000   // float sum
$83 = STORE_VARIABLE $84

BLOCK_START [ID: $86]   // Redundant Block (Start)

    $88 = LOAD_FLOAT_CONSTANT 0.000000  // float i
    $85 = STORE_VARIABLE $88 

    WHILE_START [ID: $89] -> BLOCK [start=$89, continue=$90, end=$91]   // While Block (Start)

        $92 = LOAD_FLOAT_CONSTANT 4.000000 
        $93 = LESS $85, $92 

        WHILE_COND $93   

        BLOCK_START [ID: $94]   // Loop Body Block (Start)
                 ...
        BLOCK_END [ID: $95]   // Loop Body Block (End)

    WHILE_END [ID: $91] -> BLOCK [start=$89, continue=$90, end=$91]   // While Block (End)

BLOCK_END [ID: $87]   // Redundant Block (End)

RETURN $83
...

In the example above, the statements $88 = LOAD_FLOAT_CONSTANT 0.000000 and $85 = STORE_VARIABLE $88 can be moved out of BLOCK $86, as they are either way only executed once. This results in the sequence

BLOCK START
WHILE START (aka. BLOCK  START)
...
WHILE END (aka. BLOCK END)
BLOCK END

, i.e. two blocks of which one is unnecessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions