From 4c2ab3f41e8f5de16e16ee42c708a26cb166da3d Mon Sep 17 00:00:00 2001 From: Tao Sang Date: Mon, 6 Apr 2020 09:58:35 -0400 Subject: [PATCH] Solve issues with hip-vdi runtime static lib 1.Combine libamdhip64_static_base.a and libamdvdi_static.a into libamdhip64_static.a. 2.Let hipcc use -use-staticlib to link libamdhip64_static.a. 3.Add some samples for static lib. 4.Fix compiling failure of code object. Change-Id: Ia2333622a8d05639b90974c4c5d3d85654ba0138 --- bin/hipcc | 14 ++++++++++---- samples/0_Intro/bit_extract/Makefile | 7 ++++++- samples/0_Intro/square/Makefile | 7 +++++-- vdi/CMakeLists.txt | 23 ++++++++++++++++++----- vdi/hip_internal.hpp | 6 ++++++ vdi/hip_platform.cpp | 2 +- 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/bin/hipcc b/bin/hipcc index 5ed781bc60..25b9078cd6 100755 --- a/bin/hipcc +++ b/bin/hipcc @@ -211,10 +211,7 @@ if ($HIP_PLATFORM eq "clang") { $HIPCXXFLAGS .= " -isystem $HIP_CLANG_INCLUDE_PATH/.."; $HIPCFLAGS .= " -isystem $HIP_CLANG_INCLUDE_PATH/.."; $HIPLDFLAGS .= " -L$HIP_LIB_PATH"; - if (not $isWindows) { - $HIPLDFLAGS .= " -Wl,--rpath-link=$HIP_LIB_PATH"; - $HIPLDFLAGS .= " -lhip_hcc"; - } else { + if ($isWindows) { $HIPLDFLAGS .= " -lamdhip64"; } if ($HIP_CLANG_HCC_COMPAT_MODE) { @@ -480,6 +477,7 @@ foreach $arg (@ARGV) { $linkType = 0; $setLinkType = 1; + $swallowArg = 1; } if(($trimarg eq '-use-sharedlib') and ($setLinkType eq 0)) { @@ -770,6 +768,14 @@ if ($HIP_PLATFORM eq "clang") { if (not $isWindows) { $HIPLDFLAGS .= " -lgcc_s -lgcc -lpthread -lm"; } + + if (not $isWindows and not $compileOnly) { + if ($linkType eq 0) { + $toolArgs .= " -L$HIP_LIB_PATH -lamdhip64_static -L$ROCM_PATH/lib -lhsa-runtime64 -ldl "; + } else { + $toolArgs .= " -Wl,--enable-new-dtags -Wl,--rpath=$HIP_LIB_PATH:$ROCM_PATH/lib -lhip_hcc "; + } + } } diff --git a/samples/0_Intro/bit_extract/Makefile b/samples/0_Intro/bit_extract/Makefile index 08bca6e642..4a3a0bb4fe 100644 --- a/samples/0_Intro/bit_extract/Makefile +++ b/samples/0_Intro/bit_extract/Makefile @@ -13,10 +13,15 @@ ifeq (${HIP_PLATFORM}, nvcc) endif EXE=bit_extract +EXE_STATIC=bit_extract_static $(EXE): bit_extract.cpp $(HIPCC) $(HIPCC_FLAGS) $< -o $@ +$(EXE_STATIC): bit_extract.cpp + $(HIPCC) -use-staticlib $(HIPCC_FLAGS) $< -o $@ + +all: $(EXE) $(EXE_STATIC) clean: - rm -f *.o $(EXE) + rm -f *.o $(EXE) $(EXE_STATIC) diff --git a/samples/0_Intro/square/Makefile b/samples/0_Intro/square/Makefile index 9bb0dd8205..aa046eeaaa 100644 --- a/samples/0_Intro/square/Makefile +++ b/samples/0_Intro/square/Makefile @@ -11,7 +11,7 @@ else SOURCES=square.cpp endif -all: square.out +all: square.out square.out.static # Step square.cpp: square.cu @@ -20,5 +20,8 @@ square.cpp: square.cu square.out: $(SOURCES) $(HIPCC) $(CXXFLAGS) $(SOURCES) -o $@ +square.out.static: $(SOURCES) + $(HIPCC) -use-staticlib $(CXXFLAGS) $(SOURCES) -o $@ + clean: - rm -f *.o *.out square.cpp + rm -f *.o *.out *.out.static square.cpp diff --git a/vdi/CMakeLists.txt b/vdi/CMakeLists.txt index 8c1ca1f2de..aa82dec373 100644 --- a/vdi/CMakeLists.txt +++ b/vdi/CMakeLists.txt @@ -152,21 +152,33 @@ add_library(amdhip64 SHARED $ ) -add_library(amdhip64_static STATIC +add_library(amdhip64_static_base STATIC $ ) add_library(host INTERFACE) target_link_libraries(host INTERFACE amdhip64) +target_link_libraries(host INTERFACE amdhip64_static_base) add_library(device INTERFACE) target_link_libraries(device INTERFACE host) -target_link_libraries(amdhip64_static PRIVATE amdvdi_static pthread dl) +target_link_libraries(amdhip64_static_base PRIVATE amdvdi_static pthread dl) target_link_libraries(amdhip64 PRIVATE amdvdi_static pthread dl) +set(STATICLIBNAME "${hip_BINARY_DIR}/lib/libamdhip64_static.a") + +add_custom_command( + OUTPUT ${STATICLIBNAME} + COMMAND rm -f ${STATICLIBNAME} + COMMAND ${CMAKE_AR} -rcsT ${STATICLIBNAME} $ $ + DEPENDS amdhip64_static_base amdvdi_static + COMMENT "Combining static libs into ${STATICLIBNAME} " +) + +add_custom_target(amdhip64_static ALL + DEPENDS ${STATICLIBNAME} +) -INSTALL(PROGRAMS $ DESTINATION lib COMPONENT MAIN) -INSTALL(PROGRAMS $ DESTINATION lib COMPONENT MAIN) INSTALL(CODE "execute_process( COMMAND ${CMAKE_COMMAND} -E create_symlink libamdhip64.so lib/libhip_hcc.so )" DESTINATION lib COMPONENT MAIN) INSTALL(CODE "execute_process( COMMAND ${CMAKE_COMMAND} -E create_symlink libamdhip64.so lib/libhiprtc.so )" DESTINATION lib COMPONENT MAIN) @@ -174,6 +186,7 @@ INSTALL(FILES ${CMAKE_BINARY_DIR}/lib/libhip_hcc.so DESTINATION lib COMPONENT MA INSTALL(FILES ${CMAKE_BINARY_DIR}/lib/libhiprtc.so DESTINATION lib COMPONENT MAIN) -INSTALL(TARGETS amdhip64_static amdhip64 host device EXPORT hip-targets DESTINATION ${LIB_INSTALL_DIR}) +INSTALL(PROGRAMS ${STATICLIBNAME} DESTINATION ${LIB_INSTALL_DIR}) +INSTALL(TARGETS amdhip64_static_base amdhip64 host device EXPORT hip-targets DESTINATION ${LIB_INSTALL_DIR}) INSTALL(EXPORT hip-targets DESTINATION ${CONFIG_PACKAGE_INSTALL_DIR} NAMESPACE hip::) diff --git a/vdi/hip_internal.hpp b/vdi/hip_internal.hpp index 9b4bd17042..10819350f5 100755 --- a/vdi/hip_internal.hpp +++ b/vdi/hip_internal.hpp @@ -132,6 +132,7 @@ namespace hip { extern void init(); extern Device* getCurrentDevice(); + extern void setCurrentDevice(unsigned int index); /// Get VDI queue associated with hipStream @@ -255,6 +256,11 @@ private: ~PlatformState() {} public: static PlatformState& instance() { + if (platform_ == nullptr) { + // __hipRegisterFatBinary() will call this when app starts, thus + // there is no multiple entry issue here. + platform_ = new PlatformState(); + } return *platform_; } diff --git a/vdi/hip_platform.cpp b/vdi/hip_platform.cpp index 5ece473e06..52ad36c29d 100755 --- a/vdi/hip_platform.cpp +++ b/vdi/hip_platform.cpp @@ -30,7 +30,7 @@ constexpr unsigned __hipFatMAGIC2 = 0x48495046; // "HIPF" thread_local std::stack execStack_; -PlatformState* PlatformState::platform_ = new PlatformState(); +PlatformState* PlatformState::platform_ = nullptr; struct __CudaFatBinaryWrapper { unsigned int magic;