cornell-zhang/heterocl

Bug in code simplification/optimization with common hcl.if_ conditions.

Closed this issue · 1 comments

Given code:

def two_stage(A):
    var = hcl.scalar(0, "v", dtype=hcl.UInt(32))
    var.v = 1
    with hcl.if_(var == 0):
        hcl.print((),"A\n")
    with hcl.else_():
        var.v = var - 1
        with hcl.if_(var == 0):
            hcl.print((),"B\n")
    return A

The lowered IR is:

produce _top {
  // attr [0] extern_scope = 0
  // attr [v] storage_scope = "global"
  allocate v[uint32 * 1]
  produce v {
    // attr [0] extern_scope = 0
    for "stage_name"="v" (x, 0, 1) {
      v[x] = (uint32)0
    }
  }
  v[0] = (uint32)1
  if ((v[0] == (uint32)0)) {
    print:
  } else {
    v[0] = (v[0] - (uint32)1)
  }
}

Notice that the "else" block no longer has the "with hcl.if(var == 0): hcl.print((), "B\n") statements. It looks like the second hcl.if_ block was optimized away since it is the same expression as the initial hcl.if_ without considering that var changed (decremented).

Tee issue is caused by the legacy Simplify() IR pass from Halide. I will fix it.