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

Modulo operation fails on operands greater than 128-bit in LLVM backend #353

Open Sibylau opened 3 years ago

Sibylau commented 3 years ago

When using the LLVM backend, modulo operation % fails with integers longer than 128 bit. The test case is linked here:

import heterocl as hcl
import numpy as np

N_HASH_FUNC = 5
HASH_RANGE = 10

def func_wrapper(dtype):
    hcl.init(dtype)
    a=hcl.placeholder((N_HASH_FUNC,), dtype=dtype)
    b=hcl.placeholder((N_HASH_FUNC,), dtype=dtype)
    sketch = hcl.placeholder((HASH_RANGE,), dtype=dtype)
    key=hcl.placeholder((1,),dtype=dtype)
    def hash_index(sketch, a, b, key):
        index = hcl.compute(a.shape, lambda i: (a[i]*key[0] + b[i])%HASH_RANGE)
        with hcl.for_(0, N_HASH_FUNC) as i:
            sketch[index[i]] += 1

    s = hcl.create_schedule([sketch, a, b, key], hash_index)
    f = hcl.build(s, target="llvm")

    hcl_sketch = hcl.asarray(np.zeros(N_HASH_FUNC*HASH_RANGE),dtype=dtype)
    hcl_a = hcl.asarray(np.random.randint(10, size=(N_HASH_FUNC,)), dtype=dtype)
    hcl_b = hcl.asarray(np.random.randint(10, size=(N_HASH_FUNC,)), dtype=dtype)
    hcl_key = hcl.asarray(np.random.randint(10, size=(1,)), dtype=dtype)

    f(hcl_sketch, hcl_a, hcl_b, hcl_key)

if __name__ == "__main__":
    func_wrapper(hcl.Int(64))

compiles with error: LLVM ERROR: Unsupported library call operation!

seanlatias commented 3 years ago

It seems LLVM doesn't support long bit-width division (>128 bits). I'm trying to see if there's any solution. In the worst case, we might need to implement the division as a series of substractions.

seanlatias commented 3 years ago

I also found the same error in another public repo: https://github.com/ziglang/zig/issues/1534. @zhangzhiru any suggestion? @zhenkun can you help me tag Jeremy?

zhangzhiru commented 3 years ago

We need to prompt an error message first. Later we can provide an additional HCL library to support long division and other related arithmetic operators.

zhenkun commented 3 years ago

I also found the same error in another public repo: ziglang/zig#1534. @zhangzhiru any suggestion? @zhenkun can you help me tag Jeremy?

@jcasas00