cornell-zhang / hcl-dialect

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

__invert__ is not implemented #165

Open jcasas00 opened 1 year ago

jcasas00 commented 1 year ago
def test_bitnot():
    hcl.init()
    rshape = (1,)
    def kernel():
        r = hcl.compute(rshape, lambda _:0, dtype=hcl.UInt(32))
        a = hcl.scalar(10, "a", dtype='uint32')
        r[0] = ~a.v
        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)

generates error:

    r[0] = ~a.v
  File "/home/jcasas/dprive/heterocl_mlir/hcl-dialect-prototype/build/tools/hcl/python_packages/hcl_core/hcl_mlir/build_ir.py", line 626, in __invert__
    raise HCLNotImplementedError("__invert__ is not implemented")
chhzh123 commented 1 year ago

This is also a known limitation of MLIR. It does not provide bitwise NOT op in the arith dialect. https://mlir.llvm.org/docs/Dialects/ArithOps/

jcasas00 commented 1 year ago

I see. ~a can be implemented as arith.xori a ((1 << hcl.get_bitwidth(a.dtype))-1) as well.

chhzh123 commented 1 year ago

Right, this is a workaround. We probably need to provide our own bitwise NOT op. Using other bitwise ops to implement NOT is fine for the LLVM backend, but not a good solution for HLS.