Skip to content

Commit 832a770

Browse files
Disable HIP_PLATFORM auto-detect if already defined.
This code appears to be old copy-pasta and is effectively just a no-op if -DHIP_PLATFORM=amd is passed explicitly. It has a number of nit-picky checks which will fail in this case for no real reason. This is actually worse than that because it leaks global variables that establish a different default value/scoping vs the definitions in the inner hipamd/CMakeLists.txt. I opted to rewrite this section to scope the detection logic and keep it from leaking with respect to the inner build file. The detection logic is still not great but at least will not actively cause damage or make control flow hard to reason about. It seems like this is for legacy/compatibility so I opted to simply sequester it vs applying more diligence to it. Also fixes a bug in hip-config.cmake where it enforces that hipcc exists even if not taking the install branch in the code above. See: ROCm/TheRock#21
1 parent 1b9c177 commit 832a770

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

CMakeLists.txt

+27-16
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,51 @@ endif()
4040
#############
4141
# Build steps
4242
#############
43-
if(CLR_BUILD_HIP)
44-
if (UNIX)
45-
set(HIPCC_EXECUTABLE "hipcc")
46-
set(HIPCONFIG_EXECUTABLE "hipconfig")
47-
else()
48-
set(HIPCC_EXECUTABLE "hipcc.exe")
49-
set(HIPCONFIG_EXECUTABLE "hipconfig.exe")
50-
endif()
43+
44+
# Attempt to auto-detect HIP_PLATFORM by interrogating hipconfig. This is kept
45+
# for compatibility: users are advised to pass HIP_PLATFORM explicitly.
46+
# Sets the HIP_PLATFORM variable in the parent scope.
47+
function(_hip_clr_auto_detect_hip_platform)
48+
if (UNIX)
49+
set(HIPCC_EXECUTABLE "hipcc")
50+
set(HIPCONFIG_EXECUTABLE "hipconfig")
51+
else()
52+
set(HIPCC_EXECUTABLE "hipcc.exe")
53+
set(HIPCONFIG_EXECUTABLE "hipconfig.exe")
54+
endif()
5155

5256
# Set default HIPCC_BIN_DIR to /opt/rocm/bin
5357
if(NOT DEFINED HIPCC_BIN_DIR AND UNIX)
54-
set(HIPCC_BIN_DIR "/opt/rocm/bin" CACHE STRING "Default hipcc directory on linux.")
58+
set(HIPCC_BIN_DIR "/opt/rocm/bin")
5559
endif()
56-
message(STATUS "HIPCC Binary Directory: ${HIPCC_BIN_DIR}")
60+
message(STATUS "Auto-detect HIP_PLATFORM from HIPCC Binary Directory: ${HIPCC_BIN_DIR}")
5761

5862
if(NOT EXISTS ${HIPCC_BIN_DIR}/${HIPCONFIG_EXECUTABLE})
5963
message(FATAL_ERROR "Please pass hipcc/build or hipcc/bin using -DHIPCC_BIN_DIR.")
6064
endif()
6165

62-
message(STATUS "HIP Common Directory: ${HIP_COMMON_DIR}")
63-
if(NOT DEFINED HIP_COMMON_DIR)
64-
message(FATAL_ERROR "Please pass HIP using -DHIP_COMMON_DIR. HIP_COMMON_DIR is incorrect")
65-
endif()
6666
# Determine HIP_PLATFORM
6767
if(NOT DEFINED HIP_PLATFORM)
6868
if(NOT DEFINED ENV{HIP_PLATFORM})
6969
execute_process(COMMAND ${HIPCC_BIN_DIR}/${HIPCONFIG_EXECUTABLE} --platform
70-
OUTPUT_VARIABLE HIP_PLATFORM
70+
OUTPUT_VARIABLE _detected_hip_platform
7171
OUTPUT_STRIP_TRAILING_WHITESPACE)
72+
set(HIP_PLATFORM "${_detected_hip_platform}" PARENT_SCOPE)
7273
else()
73-
set(HIP_PLATFORM $ENV{HIP_PLATFORM} CACHE STRING "HIP Platform")
74+
set(HIP_PLATFORM "$ENV{HIP_PLATFORM}" PARENT_SCOPE)
7475
endif()
7576
endif()
77+
endfunction()
78+
if(CLR_BUILD_HIP)
79+
message(STATUS "HIP Common Directory: ${HIP_COMMON_DIR}")
80+
if(NOT DEFINED HIP_COMMON_DIR)
81+
message(FATAL_ERROR "Please pass HIP using -DHIP_COMMON_DIR. HIP_COMMON_DIR is incorrect")
82+
endif()
83+
if(NOT DEFINED HIP_PLATFORM)
84+
_hip_clr_auto_detect_hip_platform()
85+
endif()
7686
endif()
87+
7788
if(CLR_BUILD_HIP)
7889
option(BUILD_SHARED_LIBS "Build the shared library" ON)
7990
if (NOT BUILD_SHARED_LIBS)

hipamd/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ if(MSVC)
5353
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG:FULL")
5454
endif()
5555

56-
set(HIPCC_BIN_DIR "" CACHE STRING "HIPCC and HIPCONFIG binary directories")
56+
# Set default HIPCC_BIN_DIR to /opt/rocm/bin
57+
set(HIPCC_BIN_DIR "/opt/rocm/bin" CACHE STRING "HIPCC and HIPCONFIG binary directories")
5758

5859
if(__HIP_ENABLE_PCH)
5960
set(_pchStatus 1)
@@ -393,9 +394,11 @@ endif()
393394
install(FILES ${PROJECT_BINARY_DIR}/include/hip/hip_version.h
394395
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hip)
395396

397+
set(HIP_INSTALLS_HIPCC OFF)
396398
if (NOT ${HIPCC_BIN_DIR} STREQUAL "")
397399
file(TO_CMAKE_PATH "${HIPCC_BIN_DIR}" HIPCC_BIN_DIR)
398400
if(EXISTS ${HIPCC_BIN_DIR})
401+
set(HIP_INSTALLS_HIPCC ON)
399402
install(PROGRAMS ${HIPCC_BIN_DIR}/${HIPCC_EXECUTABLE} DESTINATION bin)
400403
install(PROGRAMS ${HIPCC_BIN_DIR}/${HIPCONFIG_EXECUTABLE} DESTINATION bin)
401404
install(PROGRAMS ${HIPCC_BIN_DIR}/hipcc.pl DESTINATION bin)

hipamd/hip-config.cmake.in

+12-9
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ set_and_check( hip_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@" )
101101
set_and_check( hip_INCLUDE_DIRS "${hip_INCLUDE_DIR}" )
102102
set_and_check( hip_LIB_INSTALL_DIR "@PACKAGE_LIB_INSTALL_DIR@" )
103103
set_and_check( hip_BIN_INSTALL_DIR "@PACKAGE_BIN_INSTALL_DIR@" )
104-
if (WIN32)
105-
set_and_check(hip_HIPCC_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipcc.exe")
106-
set_and_check(hip_HIPCONFIG_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipconfig.exe")
107-
else()
108-
set_and_check(hip_HIPCC_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipcc")
109-
set_and_check(hip_HIPCONFIG_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipconfig")
104+
if("@HIP_INSTALLS_HIPCC@")
105+
if (WIN32)
106+
set_and_check(hip_HIPCC_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipcc.exe")
107+
set_and_check(hip_HIPCONFIG_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipconfig.exe")
108+
else()
109+
set_and_check(hip_HIPCC_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipcc")
110+
set_and_check(hip_HIPCONFIG_EXECUTABLE "${hip_BIN_INSTALL_DIR}/hipconfig")
111+
endif()
110112
endif()
111113

112114
if(NOT DEFINED HIP_PLATFORM)
@@ -140,6 +142,7 @@ set(HIP_LIB_INSTALL_DIR ${hip_LIB_INSTALL_DIR})
140142
set(HIP_BIN_INSTALL_DIR ${hip_BIN_INSTALL_DIR})
141143
set(HIP_LIBRARIES ${hip_LIBRARIES})
142144
set(HIP_LIBRARY ${hip_LIBRARY})
143-
set(HIP_HIPCC_EXECUTABLE ${hip_HIPCC_EXECUTABLE})
144-
set(HIP_HIPCONFIG_EXECUTABLE ${hip_HIPCONFIG_EXECUTABLE})
145-
145+
if("@@HIP_INSTALLS_HIPCC@@")
146+
set(HIP_HIPCC_EXECUTABLE ${hip_HIPCC_EXECUTABLE})
147+
set(HIP_HIPCONFIG_EXECUTABLE ${hip_HIPCONFIG_EXECUTABLE})
148+
endif()

0 commit comments

Comments
 (0)