Skip to content

[Feature Request] compiler V2 optimization: code sinking #18362

@junxzm1990

Description

@junxzm1990

🚀 Feature Request

Given the following code (brought up by @georgemitenkov ):

t1 = expensive();
if c {
  use(t1); // t1 is only used here
}

use(t1) is not always executed. Thus, by moving t1 = expensive() under if will optimize the execution when c is false. This is a typical case of code sinking.

Comment by @vineethk:

For this to be safe, either:

  • expensive() has to be pure
  • or all code following it has to be pure until the point of usage

For this code motion to be advantageous:

  • we need a stack-machine cost model to show us that code motion does not cause increase in code size (what if there were multiple uses in multiple branches, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status

    🆕 New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions