1919
2020import functools as ft
2121import os
22+ import pathlib
2223import tvm
2324import tvm .ir
2425import tvm .contrib .cc as cc
3940#
4041# Subsequent calls to 'link_shared' will use the newly registered linker.
4142
42- hexagon_toolchain_root = os .environ .get ("HEXAGON_TOOLCHAIN" ) or "" # pylint: disable=invalid-name
43- hexagon_link_main = os .path .join ( # pylint: disable=invalid-name
44- hexagon_toolchain_root , "bin" , "hexagon-link"
43+ HEXAGON_TOOLCHAIN = os .environ .get (
44+ "HEXAGON_TOOLCHAIN" , default = None
45+ ) # pylint: disable=invalid-name
46+ HEXAGON_SDK_PATH = os .environ .get ("HEXAGON_SDK_PATH" , default = None ) # pylint: disable=invalid-name
47+ HEXAGON_LINK_MAIN = os .path .join ( # pylint: disable=invalid-name
48+ HEXAGON_TOOLCHAIN , "bin" , "hexagon-link"
4549)
50+ HEXAGON_CLANG_PLUS = os .path .join ( # pylint: disable=invalid-name
51+ HEXAGON_TOOLCHAIN , "bin" , "hexagon-clang++"
52+ )
53+ HEXAGON_SDK_INCLUDE_DIRS = [ # pylint: disable=invalid-name
54+ pathlib .Path (HEXAGON_SDK_PATH ) / "incs" ,
55+ pathlib .Path (HEXAGON_SDK_PATH ) / "incs" / "stddef" ,
56+ ]
4657
4758
4859def register_linker (f ):
@@ -53,7 +64,13 @@ def register_linker(f):
5364@register_func ("tvm.contrib.hexagon.hexagon.hexagon_link" )
5465def hexagon_link ():
5566 """Return path to the Hexagon linker."""
56- return hexagon_link_main
67+ return HEXAGON_LINK_MAIN
68+
69+
70+ @register_func ("tvm.contrib.hexagon.hexagon.hexagon_clang_plus" )
71+ def hexagon_clang_plus ():
72+ """Return path to the Hexagon clang++."""
73+ return HEXAGON_CLANG_PLUS
5774
5875
5976@register_func ("tvm.contrib.hexagon.hexagon.link_shared" )
@@ -101,12 +118,12 @@ def to_str(s):
101118 message += (
102119 " Please verify the value of the HEXAGON_LINKER environment variable "
103120 + '(currently set to "'
104- + hexagon_toolchain_root
121+ + HEXAGON_TOOLCHAIN
105122 + '").'
106123 )
107124 raise Exception (message )
108125
109- libpath = os .path .join (hexagon_toolchain_root , "target" , "hexagon" , "lib" , "v66" , "G0" )
126+ libpath = os .path .join (HEXAGON_TOOLCHAIN , "target" , "hexagon" , "lib" , "v66" , "G0" )
110127 cc .create_shared (
111128 so_name ,
112129 objs ,
@@ -248,3 +265,27 @@ def transform(func, mod, ctx):
248265
249266def ir_lower_vtcm_pass ():
250267 return [(3 , ir_lower_vtcm ())]
268+
269+
270+ @register_func ("tvm.contrib.hexagon.hexagon.aot_export" )
271+ def aot_export (so_name , files , ** kwargs ):
272+ tvm_dir = pathlib .Path (os .path .dirname (os .path .realpath (__file__ ))) / ".." / ".." / ".." / ".."
273+ options = [
274+ f"-I{ tvm_dir / 'include' } " ,
275+ f"-I{ tvm_dir / '3rdparty' / 'dlpack' / 'include' } " ,
276+ f"-I{ tvm_dir / '3rdparty' / 'dmlc-core' / 'include' } " ,
277+ f"-I{ tvm_dir / 'src' / 'runtime' / 'hexagon' / 'android' / 'sim' / 'driver' } " ,
278+ f"-DDMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>" ,
279+ f"-D_MACH_I32=int" ,
280+ ]
281+
282+ # For debugging
283+ for path in HEXAGON_SDK_INCLUDE_DIRS :
284+ options .append (f"-I{ str (path )} " )
285+
286+ cross_compile = cc .cross_compiler (
287+ compile_func = tvm .get_global_func ("tvm.contrib.hexagon.hexagon.hexagon_clang_plus" )()
288+ )
289+ cross_compile .output_format = "o"
290+ c_files = [str (file ) for file in files ]
291+ cross_compile (so_name , c_files , options = options )
0 commit comments