cornell-zhang/hcl-dialect

hcl.print with empty tuple generates build exception

Closed this issue · 2 comments

def test_print_emptytuple():
    hcl.init()
    def kernel():
        hcl.print((), "print A\n")
        def do(i):
            hcl.print((), "print B\n")
            return 0
        r = hcl.compute((1,), do, dtype=hcl.UInt(32))
        return r
    s = hcl.create_schedule([], kernel)
    hcl_res = hcl.asarray(np.zeros((1,), dtype=np.uint32), dtype=hcl.UInt(32))
    f = hcl.build(s)
    f(hcl_res)

I'm seeing different errors depending on which print is enabled.

print B uncommented, print A commented or uncommented:

  File "test_list.py", line 102, in do
    hcl.print((), "print B\n")
  File "python/heterocl/print.py", line 44, in print
    printOp.built_op.attributes["format"] = StringAttr.get(format_str)
AttributeError: 'NoneType' object has no attribute 'attributes'

print B commented and print A uncommented

  File "python/heterocl/build_module.py", line 72, in build
    return build_llvm(schedule, target, stmt)
  File "python/heterocl/build_module.py", line 317, in build_llvm
    module = Module.parse(str(schedule.device_module), ctx)
ValueError: Unable to parse module assembly (see diagnostics)

hcl.print didn't support empty tuple, this enhancement is added by these two commits:

  • IR: 9f8c4e8
  • Frontend: 0731cdd3bcdcc795fcec0e45dcb91528de978acb

However, there's no easy way to support having imperative code inside declarative code:

def do(i):
    hcl.print((), "print B\n")
    return 0
 r = hcl.compute((1,), do, dtype=hcl.UInt(32))

hcl.print is a statement with side effect. When we build the AST for do function, statements are not included in the result expression because it is not assignable. In such case, the printOp inside do function is not built (not in the IR), but there won't be an error if the user runs this program.

Added empty tuple print test case in mlir/test_api_print.py::test_print_expr with this commit: cornell-zhang/heterocl@0e7f617