cornell-zhang / heterocl

HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Heterogeneous Computing
https://cornell-zhang.github.io/heterocl/
Apache License 2.0
326 stars 92 forks source link

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

Closed jcasas00 closed 2 years ago

jcasas00 commented 2 years ago

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).

hecmay commented 2 years ago

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