cornell-zhang / hcl-dialect

HeteroCL-MLIR dialect for accelerator design
https://cornell-zhang.github.io/heterocl/index.html
Other
40 stars 17 forks source link

operand #0 does not dominate this use error #155

Closed jcasas00 closed 1 year ago

jcasas00 commented 2 years ago
def test_print_before_if():
    hcl.init()
    rshape = (1,)
    def kernel():
        stype = hcl.Struct({"x": hcl.UInt(8), "y": hcl.UInt(8)})
        xy = hcl.scalar(0x1234, "foo", dtype=stype).v
        hcl.print((xy.x, xy.y), "match 0: %d %d\n")  # this line causes the error
        with hcl.if_(xy.x == 0):
            pass
        r = hcl.compute(rshape, lambda _:0, dtype=hcl.Int(32))
        return r
    #
    s = hcl.create_schedule([], kernel)
    print(hcl.lower(s))
    hcl_res = hcl.asarray(np.zeros(rshape, dtype=np.uint32), dtype=hcl.UInt(32))
    f = hcl.build(s)
    f(hcl_res)
    np_res = hcl_res.asnumpy()
    golden = np.zeros(rshape, dtype=np.int32)
    assert np.array_equal(golden, np_res)

generates the following error:

error: operand #0 does not dominate this use
// Verification failed, printing generic form
#map0 = affine_map<() -> (0)>
#map1 = affine_map<(d0) -> (d0)>
"builtin.module"() ({
  "func.func"() ({
    %0 = "arith.constant"() {value = 0 : index} : () -> index
    %1 = "memref.alloc"() {name = "foo", operand_segment_sizes = dense<0> : vector<2xi32>} : () -> memref<1x!hcl.struct<i8, i8>>
    %2 = "arith.constant"() {value = 0 : index} : () -> index
    %3 = "arith.constant"() {unsigned, value = 52 : i8} : () -> i8
    %4 = "arith.constant"() {unsigned, value = 18 : i8} : () -> i8
    %5 = "hcl.struct_construct"(%3, %4) : (i8, i8) -> !hcl.struct<i8, i8>
    "affine.store"(%5, %1) {map = #map0, to = "foo"} : (!hcl.struct<i8, i8>, memref<1x!hcl.struct<i8, i8>>) -> ()
    %6 = "hcl.struct_get"(%8) {index = 0 : i64} : (!hcl.struct<i8, i8>) -> i8
    %7 = "hcl.struct_get"(%8) {index = 1 : i64} : (!hcl.struct<i8, i8>) -> i8
    "hcl.print"(%6, %7) {format = "match 0: %d %d\0A", signedness = "__"} : (i8, i8) -> ()
    %8 = "affine.load"(%1) {from = "foo", map = #map0, moved} : (memref<1x!hcl.struct<i8, i8>>) -> !hcl.struct<i8, i8>
    ...

Line %6 and %7 (args to print call) refers to %8 which is defined later.
Likely related to the changes for #148.

zzzDavid commented 2 years ago

This is caused by moving the top-level if op's condition ops right before itself. I have fixed this issue in the latest commits and added test here: test_print_before_if

jcasas00 commented 2 years ago

Thanks. Not seeing this error anymore.

zzzDavid commented 1 year ago

HCL AST is a better solution to the scope issue, we are not using ASTMover anymore. Marking this issue as resolved.