cornell-zhang / heterocl

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

[API][Backend] Introduce external HLS IP module #355

Closed hecmay closed 3 years ago

hecmay commented 3 years ago

The PR introduces the HLS IP integration feature. Here is an example of integrating a vector-add IP function into heterocl program. The IP function will be included into HLS program as a individual Stage. Programmers should be responsible for the correctness of the provided IP cores.

HLS IP definiton

In this example, the function definition of the vector-add kernel is defined in vadd.cpp. The input operands (i.e. op1, op2) can either be HCL tensors or real values. We recommend the programmers to write down the IP's behavior specification using HCL's APIs inside the Stage body, so that programmers can verify the IP's correctness by comparing the result of CPU simulation and hardware simulation.

If the IP's behavior is too complex, hlib.register_tensors can be used to register the input operand tensors in the IP. But in this case, the programmers would not be able to verify the correct using CPU simulation.

    @hlib.register_extern_ip(type="vhls")
    def vadd_vhls_ip(op1, op2, size, name=None):

        with hcl.Stage("ExternModule.vadd") as Module:
            hlib.register_tensors([op1, op2])

        # HLS IP function name
        Module.ext_ip_name = name
        # HLS IP function arguments
        Module.inputs = [op1, op2, size]

        # include cpp/hpp files
        source = [ "vadd.cpp" ]
        Module.source = hlib.include_dependency(source)
        hlib.create_extern_module(Module, ip_type="HLS")

Call extern IP in HCL program

The IP function call be called inside HCL program after it is registered using @hlib.register_extern_ip. You can link an IP function's input and output to other HCL modules using s.to() primitive. The IR passes will automatically check access pattern and throw out warning for any potential deadlocks.

    op1 = hcl.placeholder((size,), dtype=dtype, name="op1")
    op2 = hcl.placeholder((size,), dtype=dtype, name="op2")
    def math_func(op1, op2):
        vadd_vhls_ip(op1, op2, size)

    target = hcl.Platform.aws_f1
    s = hcl.create_schedule([op1, op2], math_func)
hecmay commented 3 years ago

@seanlatias do we want to put AutoSA & SODA integration together with this extern IP PR?

seanlatias commented 3 years ago

Yes, it's better to separate them into different PRs.

hecmay commented 3 years ago

The formatting is kind of problematic. I used doc/.clang-format and my clang-format is exactly the same version as installed on our server. @seanlatias

root@5b589a0f46e2:/usr/src/heterocl# clang-format --version
clang-format version 8.0.1 (tags/RELEASE_801/final)
seanlatias commented 3 years ago

Can you first finish the TBA in the description?

hecmay commented 3 years ago

@seanlatias I think this is ready for review.