sdaccel.py 2.15 KB
Newer Older
1 2 3 4 5 6 7 8
"""Utility for Interacting with SDAccel Tools"""
import subprocess
import os
from . import util
from ..api import register_func


@register_func("tvm_callback_sdaccel_compile")
9
def compile_vhls(kernel_info, device_name):
10 11 12 13
    """Compile Vivado HLS code for SDAccel.

    Parameters
    ----------
14 15 16
    kernel_info : list of (str, str)
        List of kernel information.  The kernel information is a tuple of
        function name and source code.
17

18 19 20
    device_name : str
        The name of the target device

21 22 23 24 25 26 27 28 29 30 31 32 33
    Return
    ------
    xclbin : bytearray
        The bytearray of the xclbin
    """
    tmp_dir = util.tempdir()

    sdk = os.environ.get("XILINX_SDX", None)
    xocc = os.path.join(sdk, "bin/xocc") if sdk else "xocc"
    target = os.environ.get("XCL_TARGET",
                            "sw_emu" if os.environ.get("XCL_EMULATION_MODE") else "hw")
    advanced_params = ["--xp", "param:compiler.preserveHlsOutput=1",
                       "--xp", "param:compiler.generateExtraRunData=true"]
34 35 36
    platform = device_name
    if not platform:
        platform = os.environ.get("XCL_PLATFORM", os.environ.get("AWS_PLATFORM"))
37 38

    if platform is None:
39
        raise RuntimeError("No Xlinx device specified.")
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    tmp_xo_files = []
    for funcname, code  in kernel_info:
        funcname = funcname.value
        code = code.value

        tmp_cpp = tmp_dir.relpath(funcname + ".cpp")
        tmp_xo = tmp_dir.relpath(funcname + ".xo")

        with open(tmp_cpp, "wb") as out_file:
            out_file.write(bytes(code))

        # build xo
        args = [xocc, "-c", "-t", target, "--platform", platform, "-o", tmp_xo, "-k", funcname] + \
               advanced_params + [tmp_cpp]
        returncode = subprocess.call(args)
        if returncode != 0:
            raise RuntimeError("Compile error")

        tmp_xo_files.append(tmp_xo)
60 61

    # build xclbin
62 63
    tmp_xclbin = tmp_dir.relpath("output.xclbin")
    args = [xocc, "-l", "-t", target, "--platform", platform, "-o", tmp_xclbin] + tmp_xo_files + \
64 65 66 67 68 69
           advanced_params
    returncode = subprocess.call(args)
    if returncode != 0:
        raise RuntimeError("Link error")

    return bytearray(open(tmp_xclbin, "rb").read())