From 6406087713772df869bf9bee67bd8f87f4036f8e 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: Ic8c95228eb139058da8b5d66ba8439486154ca6f [ROCm/hip commit: da27fd2b09e67285a62624fdce2439bae5c55629] --- projects/hip/bin/hipcc | 14 +++++++--- .../hip/samples/0_Intro/bit_extract/Makefile | 7 ++++- projects/hip/samples/0_Intro/square/Makefile | 7 +++-- projects/hip/vdi/CMakeLists.txt | 26 +++++++++++++++++-- projects/hip/vdi/hip_internal.hpp | 6 +++++ projects/hip/vdi/hip_platform.cpp | 2 +- 6 files changed, 52 insertions(+), 10 deletions(-) diff --git a/projects/hip/bin/hipcc b/projects/hip/bin/hipcc index 5ed781bc60..25b9078cd6 100755 --- a/projects/hip/bin/hipcc +++ b/projects/hip/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/projects/hip/samples/0_Intro/bit_extract/Makefile b/projects/hip/samples/0_Intro/bit_extract/Makefile index 08bca6e642..4a3a0bb4fe 100644 --- a/projects/hip/samples/0_Intro/bit_extract/Makefile +++ b/projects/hip/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/projects/hip/samples/0_Intro/square/Makefile b/projects/hip/samples/0_Intro/square/Makefile index 9bb0dd8205..aa046eeaaa 100644 --- a/projects/hip/samples/0_Intro/square/Makefile +++ b/projects/hip/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/projects/hip/vdi/CMakeLists.txt b/projects/hip/vdi/CMakeLists.txt index 8c1ca1f2de..bfeff521d0 100644 --- a/projects/hip/vdi/CMakeLists.txt +++ b/projects/hip/vdi/CMakeLists.txt @@ -156,13 +156,35 @@ add_library(amdhip64_static STATIC $ ) +# We expect amdhip64_static to contain objects of vdi and hip. But linker +# let amdhip64_static contain objects of hip only. So we will use a +# a custom amdhip64_static_combiner to combine objects of vid and hip into +# amdhip64_static. To avoid amdhip64_static contains itself, +# amdhip64_static_temp is created internally. +add_library(amdhip64_static_temp STATIC + $ + ) + add_library(host INTERFACE) target_link_libraries(host INTERFACE amdhip64) add_library(device INTERFACE) target_link_libraries(device INTERFACE host) +# TODO: we may create host_static and device_static to let app +# link amdhip64_static -target_link_libraries(amdhip64_static PRIVATE amdvdi_static pthread dl) target_link_libraries(amdhip64 PRIVATE amdvdi_static pthread dl) +target_link_libraries(amdhip64_static PRIVATE pthread dl) +target_link_libraries(amdhip64_static_temp PRIVATE pthread dl) + +# combine objects of vid and hip into amdhip64_static +add_custom_target( + amdhip64_static_combiner + ALL + COMMAND rm -f $ # Must remove old one, otherwise the new one will contain obsolete stuff + COMMAND ${CMAKE_AR} -rcsT $ $ $ + DEPENDS amdhip64_static amdhip64_static_temp amdvdi_static # To make sure this is the last step + COMMENT "Combining static libs into amdhip64_static" +) INSTALL(PROGRAMS $ DESTINATION lib COMPONENT MAIN) @@ -174,6 +196,6 @@ 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(TARGETS amdhip64 amdhip64_static host device EXPORT hip-targets DESTINATION ${LIB_INSTALL_DIR}) INSTALL(EXPORT hip-targets DESTINATION ${CONFIG_PACKAGE_INSTALL_DIR} NAMESPACE hip::) diff --git a/projects/hip/vdi/hip_internal.hpp b/projects/hip/vdi/hip_internal.hpp index 9b4bd17042..10819350f5 100755 --- a/projects/hip/vdi/hip_internal.hpp +++ b/projects/hip/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/projects/hip/vdi/hip_platform.cpp b/projects/hip/vdi/hip_platform.cpp index 5ece473e06..822f68dce6 100755 --- a/projects/hip/vdi/hip_platform.cpp +++ b/projects/hip/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_; // Initiaized as nullptr by default struct __CudaFatBinaryWrapper { unsigned int magic;