From bd1a3361fad91df4538b1df87067bb44ffe800ef Mon Sep 17 00:00:00 2001
From: Yifan Zhao <yifanz16@illinois.edu>
Date: Tue, 30 Mar 2021 17:33:06 -0500
Subject: [PATCH] Fixed missing OpenCL link

---
 hpvm/CMakeLists.txt                     |  3 +--
 hpvm/projects/hpvm-rt/CMakeLists.txt    |  2 --
 hpvm/tools/py-approxhpvm/CMakeLists.txt |  6 +++++-
 hpvm/tools/py-approxhpvm/main.py.in     | 18 +++++++++++++-----
 4 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/hpvm/CMakeLists.txt b/hpvm/CMakeLists.txt
index 431c042b63..fcfaf264a6 100644
--- a/hpvm/CMakeLists.txt
+++ b/hpvm/CMakeLists.txt
@@ -8,10 +8,9 @@ message(STATUS "CUDA Architecture: ${CMAKE_CUDA_ARCHITECTURES}")
 # find_package will use the auxillary cmake/Find*.cmake we provide
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
 find_package(CUDNN 7 EXACT REQUIRED)  # CUDNN_INCLUDE_PATH, CUDNN_LIBRARY_DIR and CUDNN::cudnn
+find_package(OpenCL REQUIRED)  # Defines ${OpenCL_INCLUDE_DIRS} and ${OpenCL_LIBRARY}
 
 include_directories(./include/)
-# find_package will use the auxillary cmake/Find*.cmake we provide
-list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
 
 # Generate TENSOR_RT_PREFIX into config.h
 set(TENSOR_RT_PREFIX ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
diff --git a/hpvm/projects/hpvm-rt/CMakeLists.txt b/hpvm/projects/hpvm-rt/CMakeLists.txt
index 6efd8d3d0a..ad78c35828 100644
--- a/hpvm/projects/hpvm-rt/CMakeLists.txt
+++ b/hpvm/projects/hpvm-rt/CMakeLists.txt
@@ -3,8 +3,6 @@ add_definitions(-DNUM_CORES=8)
 SET(CMAKE_C_COMPILER ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/clang)
 SET(CMAKE_CXX_COMPILER ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/clang++)
 SET(CMAKE_CXX_STANDARD 11)
-# Defines ${OpenCL_INCLUDE_DIRS} and ${OpenCL_LIBRARY} if found
-find_package(OpenCL REQUIRED)
 
 # This puts libhpvm-rt.a in lib/ which we don't care about
 # we want ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/hpvm-rt.dir/hpvm-rt.cpp.o
diff --git a/hpvm/tools/py-approxhpvm/CMakeLists.txt b/hpvm/tools/py-approxhpvm/CMakeLists.txt
index 4d48fe6be9..f9d9d6ec60 100644
--- a/hpvm/tools/py-approxhpvm/CMakeLists.txt
+++ b/hpvm/tools/py-approxhpvm/CMakeLists.txt
@@ -3,16 +3,20 @@
 # main.py.in (to become approxhpvm.py) requires the following variables:
 # LLVM_PROJECT_DIR, LLVM_BUILD_DIR
 # TRT_PATH, TRT_INCLUDE_DIRS, TRT_LINK_DIRS, TRT_LINK_LIBS
+# DIRECT_LINK_LIBS
 # AVAILABLE_PASSES, HPVM_RT_PATH
 
 set(LLVM_PROJECT_DIR ${CMAKE_SOURCE_DIR})
 set(LLVM_BUILD_DIR ${CMAKE_BINARY_DIR})
 
-set(TRT_PATH "$<TARGET_FILE:tensor_runtime>")
 get_target_property(TRT_INCLUDE_DIRS tensor_runtime INCLUDE_DIRECTORIES)
 get_target_property(TRT_LINK_DIRS tensor_runtime TRT_LINK_DIRS)
 get_target_property(TRT_LINK_LIBS tensor_runtime TRT_LINK_LIBS)
 
+# This is defined globally. We need to manually link to this
+# because OpenCL functions are injected by HPVM Passes.
+set(DIRECT_LINK_LIBS ${OpenCL_LIBRARY} "$<TARGET_FILE:tensor_runtime>")
+
 # The hpvm-rt runtime
 # This has to be explicitly set as hpvm-rt.bc is created in a custom_target
 # and does not export its file location.
diff --git a/hpvm/tools/py-approxhpvm/main.py.in b/hpvm/tools/py-approxhpvm/main.py.in
index 7f5459d9e6..7b21191164 100644
--- a/hpvm/tools/py-approxhpvm/main.py.in
+++ b/hpvm/tools/py-approxhpvm/main.py.in
@@ -10,10 +10,10 @@ HPVM_PROJECT_DIR = Path("@LLVM_PROJECT_DIR@") / "tools/hpvm"
 LLVM_BUILD_BIN = Path("@LLVM_BUILD_DIR@") / "bin"
 
 # Directories to include
-TRT_PATH = Path("@TRT_PATH@")
 TRT_INCLUDE_DIRS = "@TRT_INCLUDE_DIRS@".split(";")
 TRT_LINK_DIRS = [Path(s) for s in "@TRT_LINK_DIRS@".split(";")]
 TRT_LINK_LIBS = "@TRT_LINK_LIBS@".split(";")
+DIRECT_LINK_LIBS = "@DIRECT_LINK_LIBS@".split(";")
 
 AVAILABLE_PASSES = "@AVAILABLE_PASSES@".split(";")
 HPVM_RT_PATH = "@HPVM_RT_PATH@"
@@ -117,13 +117,21 @@ def link_binary(src_file: PathLike, target_file: PathLike) -> List[str]:
         match = re.match(r"lib(.*)\.so", libname)
         return libname if match is None else match.group(1)
 
+    link_dirs, link_libnames = [], []
+    for lib in DIRECT_LINK_LIBS:
+        lib = Path(lib)
+        link_dirs.append(lib.parent)
+        link_libnames.append(drop_suffix(lib.name))
+    link_dirs += TRT_LINK_DIRS
+    link_libnames += TRT_LINK_LIBS
+
     linker_dir_flags = []
-    for path in TRT_LINK_DIRS:
+    for path in link_dirs:
         linker_dir_flags.extend([f"-L{path}", f"-Wl,-rpath={path}"])
-    linker_lib_flags = [f"-l{drop_suffix(lib)}" for lib in TRT_LINK_LIBS]
+    linker_lib_flags = [f"-l{drop_suffix(lib)}" for lib in link_libnames]
     return [
-        str(LLVM_BUILD_BIN / "clang++"), str(src_file), str(TRT_PATH), "-o", str(target_file),
-        *linker_dir_flags, *linker_lib_flags
+        str(LLVM_BUILD_BIN / "clang++"), str(src_file),
+        "-o", str(target_file), *linker_dir_flags, *linker_lib_flags
     ]
 
 
-- 
GitLab