From 35e94f4ae16d51717793fe437d020081dd2ea29c Mon Sep 17 00:00:00 2001
From: Yifan Zhao <yifanz16@illinois.edu>
Date: Mon, 26 Apr 2021 00:50:13 -0500
Subject: [PATCH] Removed an unused link dir in hpvm-clang that may cause
 conflict

When the GCC used to compile tensor runtime is not the system's default,
path to that GCC's GLIBC leaks into hpvm-clang
that can override system's default and cause GLIBC failure.
---
 hpvm/projects/hpvm-tensor-rt/CMakeLists.txt | 17 ++----------
 hpvm/tools/hpvm-clang/CMakeLists.txt        | 10 +++----
 hpvm/tools/hpvm-clang/main.py.in            | 29 +++++++++------------
 3 files changed, 18 insertions(+), 38 deletions(-)

diff --git a/hpvm/projects/hpvm-tensor-rt/CMakeLists.txt b/hpvm/projects/hpvm-tensor-rt/CMakeLists.txt
index 3f88dad2d0..a1c5a251f6 100644
--- a/hpvm/projects/hpvm-tensor-rt/CMakeLists.txt
+++ b/hpvm/projects/hpvm-tensor-rt/CMakeLists.txt
@@ -52,9 +52,8 @@ if(USE_GFLAGS)
   list(APPEND LINK_LIBS gflags)
 endif()
 
-# -- Manually list the directories (TRT_LINK_DIRS) and libraries (TRT_LINK_LIBS)
-# tensor_runtime links to, because CMake doesn't help us do this.
-# This is needed by both hpvm-clang and the RPATH setting step (below).
+# -- Manually list the directories (TRT_LINK_DIRS) tensor_runtime links to.
+# This is needed by the RPATH setting step below, manually, because CMake doesn't help us do this.
 # First, take a guess at the paths to the libraries that are used here.
 # (CMake, why do you make this so difficult?)
 foreach(interface_lib ${LINK_LIBS})
@@ -62,18 +61,13 @@ foreach(interface_lib ${LINK_LIBS})
   foreach(actual_lib ${actual_libs})
     # ${actual_lib} may not be a path, then taking the directory of it should return "".
     get_filename_component(libdir ${actual_lib} DIRECTORY)
-    get_filename_component(libname ${actual_lib} NAME)
     if(NOT ${libdir} STREQUAL "")
       list(APPEND TRT_LINK_DIRS ${libdir})
     endif()
-    if(NOT ${libname} STREQUAL "" AND NOT ${libname} STREQUAL "actual_libs-NOTFOUND")
-      list(APPEND TRT_LINK_LIBS ${libname})
-    endif()
   endforeach()
 endforeach()
 # Dedup, just for shorter compiler arguments.
 list(REMOVE_DUPLICATES TRT_LINK_DIRS)
-list(REMOVE_DUPLICATES TRT_LINK_LIBS)
 
 # -- Definitions
 set(DEFS -DPROMISE_TUNER_ENABLED -DSIMULATION_MODE=true)
@@ -120,13 +114,6 @@ function(add_tensor_runtime target_name)
   foreach(libdir ${TRT_LINK_DIRS})
     target_link_options(${target_name} PRIVATE "-Wl,-rpath,${libdir}")
   endforeach()
-  # Also slap TRT_LINK_DIRS and TRT_LINK_LIBS on this target
-  # so that hpvm-clang can read them. (we'll create our own properties.)
-  set_target_properties(
-    ${target_name} PROPERTIES
-    TRT_LINK_DIRS "${TRT_LINK_DIRS}"
-    TRT_LINK_LIBS "${TRT_LINK_LIBS}"
-  )
 endfunction(add_tensor_runtime)
 
 # Adding new rule for building a cuDNN runtime library
diff --git a/hpvm/tools/hpvm-clang/CMakeLists.txt b/hpvm/tools/hpvm-clang/CMakeLists.txt
index fdd4de2557..b0dcf1b601 100644
--- a/hpvm/tools/hpvm-clang/CMakeLists.txt
+++ b/hpvm/tools/hpvm-clang/CMakeLists.txt
@@ -7,8 +7,8 @@
 # ---[ Define variables for main.py.in
 # main.py.in requires the following variables:
 # LLVM_PROJECT_DIR, LLVM_BUILD_DIR
-# INCLUDE_DIRS, LINK_DIRS, LINK_LIBS
-# DIRECT_LINK_LIBS, HPVM_USE_OPENCL (defined globally), HPVM_HAS_TRT
+# INCLUDE_DIRS, DIRECT_LINK_LIBS
+# HPVM_USE_OPENCL (defined globally), HPVM_HAS_TRT
 # AVAILABLE_PASSES, HPVM_RT_PATH
 
 set(LLVM_PROJECT_DIR ${CMAKE_SOURCE_DIR})
@@ -22,12 +22,8 @@ if(TARGET tensor_runtime)
   set(HPVM_HAS_TRT 1)  # Has 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)
-
   list(APPEND INCLUDE_DIRS ${TRT_INCLUDE_DIRS})
-  list(APPEND LINK_DIRS ${TRT_LINK_DIRS})
-  list(APPEND LINK_LIBS ${TRT_LINK_LIBS})
+
   list(APPEND DIRECT_LINK_LIBS "$<TARGET_FILE:tensor_runtime>")
   list(APPEND DEPS tensor_runtime)
 else()
diff --git a/hpvm/tools/hpvm-clang/main.py.in b/hpvm/tools/hpvm-clang/main.py.in
index 9d1fe40a78..8ab8a801bc 100644
--- a/hpvm/tools/hpvm-clang/main.py.in
+++ b/hpvm/tools/hpvm-clang/main.py.in
@@ -11,8 +11,8 @@ LLVM_BUILD_BIN = Path("@LLVM_BUILD_DIR@") / "bin"
 
 # Directories to include
 INCLUDE_DIRS = "@INCLUDE_DIRS@".split(";")
-LINK_DIRS = [Path(s) for s in "@LINK_DIRS@".split(";")]
-LINK_LIBS = "@LINK_LIBS@".split(";")
+# TODO: This dependency comes from hpvm-rt. Should have CMake inject this instead.
+LINK_LIBS = ["pthread"]
 DIRECT_LINK_LIBS = "@DIRECT_LINK_LIBS@".split(";")
 HPVM_USE_OPENCL = int("@HPVM_USE_OPENCL@")
 HPVM_HAS_TRT = int("@HPVM_HAS_TRT@")
@@ -131,32 +131,29 @@ def link_binary(
     extra_link_dirs: List[PathLike] = None,
     extra_link_libs: List[str] = None
 ) -> List[str]:
-    link_dirs, link_libs = _link_args(extra_link_dirs or [], extra_link_libs or [])
+    link_dirs, link_libnames = _parse_direct_link_libs()
+    link_dirs += (extra_link_dirs or [])
+    link_libstems = (extra_link_libs or []) + LINK_LIBS
+
     linker_dir_flags = []
     for path in link_dirs:
         linker_dir_flags.extend([f"-L{path}", f"-Wl,-rpath={path}"])
-    linker_lib_flags = [f"-l{lib}" for lib in link_libs]
+    linker_lib_flags = (
+        [f"-l{lib}" for lib in link_libstems] +
+        [f"-l:{libname}" for libname in link_libnames]
+    )
     return [
         str(LLVM_BUILD_BIN / "clang++"), str(src_file),
         "-o", str(target_file), *linker_dir_flags, *linker_lib_flags
     ]
 
 
-def _link_args(extra_link_dirs: List[PathLike], extra_link_libs: List[str]):
-    def drop_suffix(libname: str):
-        import re
-
-        match = re.match(r"lib(.*)\.so", libname)
-        return libname if match is None else match.group(1)
-
-    link_dirs, link_libs = extra_link_dirs.copy(), extra_link_libs.copy()
+def _parse_direct_link_libs():
+    link_dirs, link_libnames = [], []
     for lib in DIRECT_LINK_LIBS:
         lib = Path(lib)
         link_dirs.append(lib.parent)
-        link_libs.append(lib.name)
-    link_dirs += LINK_DIRS
-    link_libs += LINK_LIBS
-    link_libnames = [drop_suffix(s) for s in link_libs]
+        link_libnames.append(lib.name)
     return link_dirs, link_libnames
 
 
-- 
GitLab