Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RUNTIME] Add compile_shared option to linux compile utility fn #5751

Merged
merged 5 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions python/tvm/contrib/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,39 @@ def create_shared(output,
The compiler command.
"""
if sys.platform == "darwin" or sys.platform.startswith("linux"):
_linux_compile(output, objects, options, cc)
_linux_compile(output, objects, options, cc, compile_shared=True)
elif sys.platform == "win32":
_windows_shared(output, objects, options)
else:
raise ValueError("Unsupported platform")


def create_executable(output,
objects,
options=None,
cc="g++"):
"""Create executable binary.

Parameters
----------
output : str
The target executable.

objects : List[str]
List of object files.

options : List[str]
The list of additional options string.

cc : Optional[str]
The compiler command.
"""
if sys.platform == "darwin" or sys.platform.startswith("linux"):
_linux_compile(output, objects, options, cc)
else:
raise ValueError("Unsupported platform")


def get_target_by_dump_machine(compiler):
""" Functor of get_target_triple that can get the target triple using compiler.

Expand Down Expand Up @@ -164,9 +191,10 @@ def _fcompile(outputs, objects, options=None):
return _fcompile


def _linux_compile(output, objects, options, compile_cmd="g++"):
def _linux_compile(output, objects, options,
compile_cmd="g++", compile_shared=False):
cmd = [compile_cmd]
if output.endswith(".so") or output.endswith(".dylib"):
if compile_shared or output.endswith(".so") or output.endswith(".dylib"):
cmd += ["-shared", "-fPIC"]
if sys.platform == "darwin":
cmd += ["-undefined", "dynamic_lookup"]
Expand All @@ -185,6 +213,7 @@ def _linux_compile(output, objects, options, compile_cmd="g++"):
if proc.returncode != 0:
msg = "Compilation error:\n"
msg += py_str(out)
msg += "\nCommand line: " + " ".join(cmd)
raise RuntimeError(msg)


Expand Down
2 changes: 1 addition & 1 deletion tests/python/contrib/test_binutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def make_binary():
tmp_obj = tmp_dir.relpath("obj.obj")
with open(tmp_source, "w") as f:
f.write(prog)
cc.create_shared(tmp_obj, tmp_source, [],
cc.create_executable(tmp_obj, tmp_source, [],
cc="{}gcc".format(TOOLCHAIN_PREFIX))
prog_bin = bytearray(open(tmp_obj, "rb").read())
return prog_bin
Expand Down
6 changes: 3 additions & 3 deletions tests/python/unittest/test_runtime_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pytest
import numpy as np
from tvm import rpc
from tvm.contrib import util
from tvm.contrib import util, cc
from tvm.rpc.tracker import Tracker


Expand Down Expand Up @@ -142,7 +142,7 @@ def check(remote):
# Test minrpc server.
temp = util.tempdir()
minrpc_exec = temp.relpath("minrpc")
tvm.rpc.with_minrpc("g++")(minrpc_exec, [])
tvm.rpc.with_minrpc(cc.create_executable)(minrpc_exec, [])
check(rpc.PopenSession(minrpc_exec))
# minrpc on the remote
server = rpc.Server("localhost")
Expand Down Expand Up @@ -208,7 +208,7 @@ def check_minrpc():
temp = util.tempdir()
f = tvm.build(s, [A, B], "llvm --system-lib", name="myadd")
path_minrpc = temp.relpath("dev_lib.minrpc")
f.export_library(path_minrpc, rpc.with_minrpc("g++"))
f.export_library(path_minrpc, rpc.with_minrpc(cc.create_executable))

with pytest.raises(RuntimeError):
rpc.PopenSession("filenotexist")
Expand Down