def compile(compiler, flags, inputs, output, combind_build=False):
    def do_compile(cmd):
        if jit_utils.cc:
            return jit_utils.cc.cache_compile(cmd, cache_path, jittor_path)
        else:
            run_cmd(cmd)
            return True

    link = link_flags
    base_output = output.split('/')[-1].split('.')[0]
    # if output is core, add core_link_flags
    if output.startswith("jittor_core"):
        link = link + core_link_flags
    output = os.path.join(cache_path, output)
    # don't recompile object file in inputs
    obj_files = []
    new_inputs = []
    for name in inputs:
        if name.endswith(".o"):
            obj_files.append(name)
        else:
            new_inputs.append(os.path.join(jittor_path, name))
            obj_files.append(
                os.path.join(cache_path, "obj_files",
                             os.path.basename(name) + ".o"))
    inputs = new_inputs

    if len(inputs) == 1 or combind_build:
        cmd = f"{compiler} {' '.join(inputs)} {flags} {link} -o {output}"
        return do_compile(cmd)
    # split compile object file and link
    # remove -l -L flags when compile object files
    oflags = remove_flags(flags, ['-l', '-L', '-Wl,'])
    cmds = []
    for input, obj_file in zip(inputs, obj_files):
        cc = compiler
        nflags = oflags
        if input.endswith(".cu"):
            if has_cuda:
                nflags = convert_nvcc_flags(oflags)
                cc = nvcc_path
            else:
                continue
        cmd = f"{cc} {input} {nflags} -c {lto_flags} -o {obj_file}"
        if "nan_checker" in input:
            # nan checker needs to disable fast_math
            cmd = cmd.replace("--use_fast_math", "")
            cmd = cmd.replace("-Ofast", "-O2")
        cmds.append(cmd)
    jit_utils.run_cmds(cmds, cache_path, jittor_path,
                       "Compiling " + base_output)
    cmd = f"{compiler} {' '.join(obj_files)} {flags} {lto_flags} {link} -o {output}"
    return do_compile(cmd)
예제 #2
0
def compile(compiler, flags, inputs, output, combind_build=False):
    def do_compile(cmd):
        if jit_utils.cc:
            return jit_utils.cc.cache_compile(cmd, cache_path, jittor_path)
        else:
            run_cmd(cmd)
            return True

    link = link_flags
    # if output is core, add core_link_flags
    if output.startswith("jittor_core"):
        link = link + core_link_flags
    output = os.path.join(cache_path, output)
    # don't recompile object file in inputs
    obj_files = []
    new_inputs = []
    for name in inputs:
        if name.endswith(".o"):
            obj_files.append(name)
        else:
            new_inputs.append(os.path.join(jittor_path, name))
            obj_files.append(
                os.path.join(cache_path, "obj_files",
                             os.path.basename(name) + ".o"))
    inputs = new_inputs

    if len(inputs) == 1 or combind_build:
        cmd = f"{compiler} {' '.join(inputs)} {flags} {link} -o {output}"
        return do_compile(cmd)
    # split compile object file and link
    # remove -l -L flags when compile object files
    oflags = remove_flags(flags, ['-l', '-L', '-Wl,'])
    cmds = []
    for input, obj_file in zip(inputs, obj_files):
        cc = compiler
        nflags = oflags
        if has_cuda and input.endswith(".cu"):
            nflags = convert_nvcc_flags(oflags)
            cc = nvcc_path
        cmd = f"{cc} {input} {nflags} -c {lto_flags} -o {obj_file}"
        cmds.append(cmd)
    jit_utils.run_cmds(cmds, cache_path, jittor_path, "compiling")
    cmd = f"{compiler} {' '.join(obj_files)} {flags} {lto_flags} {link} -o {output}"
    return do_compile(cmd)
예제 #3
0
파일: compiler.py 프로젝트: Exusial/jittor
def compile(compiler,
            flags,
            inputs,
            output,
            combind_build=False,
            cuda_flags=""):
    def do_compile(cmd):
        if jit_utils.cc:
            return jit_utils.cc.cache_compile(cmd, cache_path, jittor_path)
        else:
            run_cmd(cmd)
            return True

    base_output = os.path.basename(output).split('.')[0]
    if os.name == 'nt':
        # windows do not combind build, need gen def
        combind_build = False
        # windows need xxxx.lib
        afile = output.rsplit('.', 1)[0] + ".lib"
        afile = os.path.join(cache_path, afile)
        if cc_type != 'cl':
            # initialize order in windows seems reversed
            inputs = list(inputs[::-1])
            link = link + f' -Wl,--export-all-symbols,--out-implib,"{afile}" '

    if not os.path.isabs(output):
        output = os.path.join(cache_path, output)
    # don't recompile object file in inputs
    obj_files = []
    ex_obj_files = []
    new_inputs = []
    for name in inputs:
        if name[-1] in 'oab':
            ex_obj_files.append(name)
        else:
            new_inputs.append(os.path.join(jittor_path, name))
            obj_files.append(
                os.path.join(cache_path, "obj_files",
                             os.path.basename(name) + ".o"))
    inputs = new_inputs
    cm = lambda s: f"\"{s}\""
    cms = lambda arr: [f"\"{s}\"" for s in arr]

    if len(inputs) == 1 or combind_build:
        cmd = f"\"{compiler}\" {' '.join(cms(inputs))} {flags} -o {cm(output)}"
        return do_compile(fix_cl_flags(cmd))
    # split compile object file and link
    # remove -l -L flags when compile object files
    oflags = remove_flags(flags, ['-l', '-L', '-Wl,', '.lib', '-shared'])
    cmds = []
    for input, obj_file in zip(inputs, obj_files):
        cc = compiler
        nflags = oflags
        cmd = f"{cm(input)} {nflags} {lto_flags} -c -o {cm(obj_file)}"
        if input.endswith(".cu"):
            if has_cuda:
                cmd = f"\"{nvcc_path}\" {cuda_flags} {cmd}"
                cmd = convert_nvcc_flags(fix_cl_flags(cmd))
            else:
                continue
        else:
            cmd = f"\"{cc}\" {cmd}"
            cmd = fix_cl_flags(cmd)
        if "nan_checker" in input:
            # nan checker needs to disable fast_math
            cmd = cmd.replace("--use_fast_math", "")
            cmd = cmd.replace("-Ofast", "-O2")
        cmds.append(cmd)
    jit_utils.run_cmds(cmds, cache_path, jittor_path,
                       "Compiling " + base_output)
    obj_files += ex_obj_files
    if os.name == 'nt':
        dumpdef_path = os.path.join(jittor_path, "utils", "dumpdef.py")
        cmd = f"\"{sys.executable}\" \"{dumpdef_path}\" {' '.join(cms(obj_files))} -Fo: \"{output}.def\""
        do_compile(fix_cl_flags(cmd))
    cmd = f"\"{compiler}\" {' '.join(cms(obj_files))} -o {cm(output)} {flags} {lto_flags}"
    return do_compile(fix_cl_flags(cmd))