Skip to content

Commit b4d2a57

Browse files
committed
Adjustments for (upcoming) LLVM-23 and Clang's new offload driver
This patch updates chipStar to work on non-forked, upstream LLVM-23. The recent Clang has switched to use its new offload driver by default for HIP compilation and chipStar is adjusted to it. There is a still an option to use the old while it still around in Clang. That said, chipStar doesn't yet work on the recent Clang until the following upstream patch lands: llvm/llvm-project#187655.
1 parent 2431307 commit b4d2a57

6 files changed

Lines changed: 110 additions & 26 deletions

File tree

CMakeLists.txt

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,22 @@ set(SHARE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/share CACHE PATH "share install di
235235
set(CONFIG_PACKAGE_INSTALL_DIR ${LIB_INSTALL_DIR}/cmake/hip CACHE PATH "cmake config install dir")
236236
set(CONFIG_LANG_PACKAGE_INSTALL_DIR ${LIB_INSTALL_DIR}/cmake/hip-lang CACHE PATH "cmake lang install dir")
237237

238+
set(HAVE_CLANG_NEW_OFFLOAD_DRIVER OFF)
239+
if(LLVM_VERSION_MAJOR GREATER_EQUAL 23)
240+
# LLVM-22: The new offload driver exists on this version but HIPSPV
241+
# toolchain doesn't support it.
242+
#
243+
# LLVM-23: HIPSPV toolchain supports the new offload driver. The new
244+
# offload driver is enabled by default. To disable it
245+
# --no-offload-new-driver needs to be issued in HIP compilation and
246+
# link steps.
247+
#
248+
# LLVM-23+: the old offload driver may be removed.
249+
set(HAVE_CLANG_NEW_OFFLOAD_DRIVER ON)
250+
endif()
251+
252+
include(CMakeDependentOption)
253+
238254
# HIP options
239255
option(STANDALONE_TESTS "Create a separate executable for each test instead of combining tests into a shared lib by category" ON)
240256

@@ -281,6 +297,10 @@ option(OCML_BASIC_ROUNDED_OPERATIONS "Use OCML implementations for devicelib fun
281297
option(CHIP_MALI_GPU_WORKAROUNDS "Apply work-arounds for avoiding SPIR-V \
282298
consumption issues in ARM Mali GPU driver." OFF)
283299

300+
cmake_dependent_option(
301+
USE_NEW_OFFLOAD_DRIVER "Use Clang's new offload driver."
302+
ON HAVE_CLANG_NEW_OFFLOAD_DRIVER OFF)
303+
284304
if(CHIP_EXT_FLOAT_ATOMICS)
285305
message(DEPRECATION "-DCHIP_EXT_FLOAT_ATOMICS is no longer effective.")
286306
endif()
@@ -404,7 +424,15 @@ endif()
404424

405425
message(STATUS "chipStar will be installed to: ${CMAKE_INSTALL_PREFIX}")
406426

407-
set(OFFLOAD_TRIPLE spirv64)
427+
if(LLVM_VERSION_MAJOR GREATER_EQUAL 23)
428+
# On LLVM-23+ the emitted SPIR-V version can be controlled with
429+
# subarch string (v#.#). The new "chipstar" OS component sets
430+
# allowed SPIR-V extensions tailored for chipStar.
431+
set(OFFLOAD_TRIPLE spirv64v1.2-unknown-chipstar)
432+
else()
433+
set(OFFLOAD_TRIPLE spirv64)
434+
endif()
435+
408436
# =============================================================================
409437
# chipStar BINARIES & TESTS
410438
add_subdirectory(llvm_passes)
@@ -415,6 +443,7 @@ add_subdirectory(bitcode)
415443
target_sources(CHIP PRIVATE $<TARGET_OBJECTS:rtdevlib>)
416444

417445
set(HIPCC_BUILD_PATH "${CMAKE_BINARY_DIR}/bin")
446+
set(HIPCC_SPIRV_TRIPLE ${OFFLOAD_TRIPLE})
418447
add_subdirectory(HIPCC)
419448

420449
# Override stale version set by HIPCC subdirectory with values from HIP/VERSION
@@ -595,22 +624,39 @@ set(HIP_OFFLOAD_COMPILE_ONLY_OPTIONS_
595624
# excludes the wrappers.
596625
-nohipwrapperinc)
597626

627+
if(LLVM_VERSION_MAJOR GREATER_EQUAL 23 AND NOT USE_NEW_OFFLOAD_DRIVER)
628+
list(APPEND HIP_OFFLOAD_COMPILE_ONLY_OPTIONS_ --no-offload-new-driver)
629+
endif()
630+
598631
# Include a header for applying fixups before any user or system includes.
599632
set(HIP_FIXUPS_HEADER_BUILD
600633
-include ${CMAKE_SOURCE_DIR}/include/hip/spirv_fixups.h)
601634
set(HIP_FIXUPS_HEADER_INSTALL
602635
-include ${CMAKE_INSTALL_PREFIX}/include/hip/spirv_fixups.h)
603636

604637
# Flags needed additionally for linking phase with -fgpu-rdc.
605-
set(HIP_RDC_SUPPLEMENT_LINK_FLAGS_
606-
# Infors clang the type of the code object inputs (which are different than
607-
# in regular host C/C++ linking)
608-
--hip-link
609-
# Required for selecting HIPSPV toolchain instead of AMD's one in clang.
610-
--offload=spirv64
611-
# --hip-path is also needed but not included here (different option
612-
# value is needed for build and installation).
613-
)
638+
#
639+
# --hip-path is also needed in RDC linking stage but on purpose it is
640+
# not added to HIP_RDC_SUPPLEMENT_LINK_FLAGS_ (different option value
641+
# is needed for build and installation). The option is used to
642+
# implicitly load and run chipStar's LLVM passes during this stage.
643+
#
644+
# On the new offload driver, the --hip-path needs to be wrapped in
645+
# -Xoffload-compiler-${OFFLOAD_TRIPLE}.
646+
if(USE_NEW_OFFLOAD_DRIVER)
647+
set(HIP_RDC_SUPPLEMENT_LINK_FLAGS_ --offload-link)
648+
else()
649+
set(HIP_RDC_SUPPLEMENT_LINK_FLAGS_
650+
# Infors clang the type of the code object inputs (which are different than
651+
# in regular host C/C++ linking).
652+
--hip-link
653+
# Required for selecting HIPSPV toolchain instead of AMD's one in clang.
654+
--offload=${OFFLOAD_TRIPLE})
655+
656+
if(LLVM_VERSION_MAJOR GREATER_EQUAL 23)
657+
list(APPEND HIP_RDC_SUPPLEMENT_LINK_FLAGS_ --no-offload-new-driver)
658+
endif()
659+
endif()
614660

615661
if (NOT CLANG_VERSION_LESS_15)
616662
list(APPEND HIP_RDC_SUPPLEMENT_LINK_FLAGS_
@@ -728,13 +774,16 @@ target_link_libraries(host INTERFACE CHIP)
728774
add_library(deviceRDCInternal INTERFACE)
729775
target_compile_options(deviceRDCInternal INTERFACE -fgpu-rdc)
730776
target_link_options(deviceRDCInternal INTERFACE
731-
-fgpu-rdc ${HIP_RDC_SUPPLEMENT_LINK_FLAGS_} --hip-path=${CMAKE_BINARY_DIR})
777+
-fgpu-rdc ${HIP_RDC_SUPPLEMENT_LINK_FLAGS_}
778+
$<$<BOOL:${USE_NEW_OFFLOAD_DRIVER}>:-Xoffload-compiler-${OFFLOAD_TRIPLE}>
779+
--hip-path=${CMAKE_BINARY_DIR})
732780
target_link_libraries(deviceRDCInternal INTERFACE deviceInternal)
733781

734782
add_library(deviceRDC INTERFACE)
735783
target_compile_options(deviceRDC INTERFACE -fgpu-rdc)
736784
target_link_options(deviceRDC INTERFACE
737785
-fgpu-rdc ${HIP_RDC_SUPPLEMENT_LINK_FLAGS_}
786+
$<$<BOOL:${USE_NEW_OFFLOAD_DRIVER}>:-Xoffload-compiler-${OFFLOAD_TRIPLE}>
738787
--hip-path=${CMAKE_INSTALL_PREFIX})
739788
target_link_libraries(deviceRDC INTERFACE device)
740789

@@ -822,9 +871,15 @@ add_to_config(_hipInfo_install
822871
HIP_OFFLOAD_COMPILE_OPTIONS "${HIP_OFFLOAD_COMPILE_OPTIONS_INSTALL}")
823872
add_to_config(_hipInfo_install
824873
HIP_OFFLOAD_LINK_OPTIONS "${HIP_OFFLOAD_LINK_OPTIONS_INSTALL}")
825-
add_to_config(_hipInfo_install
826-
HIP_OFFLOAD_RDC_SUPPLEMENT_LINK_OPTIONS
827-
"${HIP_RDC_SUPPLEMENT_LINK_FLAGS} --hip-path=${CMAKE_INSTALL_PREFIX}")
874+
if(USE_NEW_OFFLOAD_DRIVER)
875+
add_to_config(_hipInfo_install
876+
HIP_OFFLOAD_RDC_SUPPLEMENT_LINK_OPTIONS
877+
"${HIP_RDC_SUPPLEMENT_LINK_FLAGS} -Xoffload-compiler-${OFFLOAD_TRIPLE} --hip-path=${CMAKE_INSTALL_PREFIX}")
878+
else()
879+
add_to_config(_hipInfo_install
880+
HIP_OFFLOAD_RDC_SUPPLEMENT_LINK_OPTIONS
881+
"${HIP_RDC_SUPPLEMENT_LINK_FLAGS} --hip-path=${CMAKE_INSTALL_PREFIX}")
882+
endif()
828883

829884
# Build version
830885
set(_hipInfo_build "# Auto-generated by cmake on ${_timestamp} UTC\n")
@@ -837,9 +892,15 @@ add_to_config(_hipInfo_build
837892
HIP_OFFLOAD_COMPILE_OPTIONS "${HIP_OFFLOAD_COMPILE_OPTIONS_BUILD}")
838893
add_to_config(_hipInfo_build
839894
HIP_OFFLOAD_LINK_OPTIONS "${HIP_OFFLOAD_LINK_OPTIONS_BUILD}")
840-
add_to_config(_hipInfo_build
841-
HIP_OFFLOAD_RDC_SUPPLEMENT_LINK_OPTIONS
842-
"${HIP_RDC_SUPPLEMENT_LINK_FLAGS} --hip-path=${CMAKE_BINARY_DIR}")
895+
if(USE_NEW_OFFLOAD_DRIVER)
896+
add_to_config(_hipInfo_build
897+
HIP_OFFLOAD_RDC_SUPPLEMENT_LINK_OPTIONS
898+
"${HIP_RDC_SUPPLEMENT_LINK_FLAGS} -Xoffload-compiler-${OFFLOAD_TRIPLE} --hip-path=${CMAKE_BINARY_DIR}")
899+
else()
900+
add_to_config(_hipInfo_build
901+
HIP_OFFLOAD_RDC_SUPPLEMENT_LINK_OPTIONS
902+
"${HIP_RDC_SUPPLEMENT_LINK_FLAGS} --hip-path=${CMAKE_BINARY_DIR}")
903+
endif()
843904

844905
add_library(hip::host ALIAS host)
845906
add_library(hip::device ALIAS device)

llvm_passes/HipPrintf.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ getFormatStringPieces(Value *FmtStrArg, unsigned &NumberOfFormatSpecs) {
133133
dyn_cast<ConstantDataSequential>(OrigFmtStr->getInitializer());
134134

135135
if (FmtStrData == nullptr) {
136+
#if LLVM_VERSION_MAJOR >= 23
137+
assert(OrigFmtStr->getInitializer()->isNullValue());
138+
#else
136139
assert(OrigFmtStr->getInitializer()->isZeroValue());
140+
#endif
137141
FmtStrPieces.push_back("");
138142
NumberOfFormatSpecs = 0;
139143
return FmtStrPieces;
@@ -218,9 +222,16 @@ std::string getCDSAsString(GlobalVariable *OrigStr, bool &IsEmpty) {
218222
ConstantDataSequential *CDSInitializer =
219223
dyn_cast<ConstantDataSequential>(OrigStr->getInitializer());
220224

225+
#if LLVM_VERSION_MAJOR >= 23
226+
assert(OrigStr->getInitializer()->isNullValue() || CDSInitializer != nullptr);
227+
228+
IsEmpty = OrigStr->getInitializer()->isNullValue();
229+
#else
221230
assert(OrigStr->getInitializer()->isZeroValue() || CDSInitializer != nullptr);
222231

223232
IsEmpty = OrigStr->getInitializer()->isZeroValue();
233+
#endif
234+
224235
return (IsEmpty ? "" : std::string(CDSInitializer->getAsCString()));
225236
}
226237

tests/compiler/TestHipcc692Regression.bash

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ mkdir -p ${OUT_DIR}/CMakeFiles/test_hipcub_basic.dir
1515
export HIPCC_VERBOSE=7
1616

1717
${HIPCC} \
18-
-DGTEST_LINKED_AS_SHARED_LIBRARY=1 \
19-
-O3 -DNDEBUG \
20-
-x hip -D__HIP_PLATFORM_SPIRV__= --offload=spirv64 -nohipwrapperinc --hip-path=${BIN_DIR} --target=x86_64-unknown-linux-gnu \
21-
-include ${SRC_DIR}/include/hip/spirv_fixups.h -std=c++14 \
18+
-O3 -DNDEBUG -std=c++14 \
2219
-c ${OUT_DIR}/test_hipcub_basic.cpp \
2320
-MD -MT ${OUT_DIR}/CMakeFiles/test_hipcub_basic.dir/test_hipcub_basic.cpp.o \
2421
-MF ${OUT_DIR}/CMakeFiles/test_hipcub_basic.dir/test_hipcub_basic.cpp.o.d \

tests/compiler/rdcLink/TestStaticLibRDC.bash

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ ar rcs "${OUT_DIR}/libk.a" "${OUT_DIR}/k.o" "${OUT_DIR}/k1.o"
2727
${HIPCC} -fgpu-rdc -I"${SRC_DIR}" -c "${SRC_DIR}/t.cpp" -o "${OUT_DIR}/t.o"
2828

2929
# Link the main file and the static library
30-
${HIPCC} --save-temps -v -fgpu-rdc --hip-link "${OUT_DIR}/t.o" "${OUT_DIR}/libk.a" -o "${OUT_DIR}/TestStaticLibRDC"
30+
${HIPCC} --save-temps -v -fgpu-rdc "${OUT_DIR}/t.o" "${OUT_DIR}/libk.a" \
31+
-o "${OUT_DIR}/TestStaticLibRDC"
3132
echo "TestStaticLibRDC.log: ${OUT_DIR}/TestStaticLibRDC.log"
3233
${OUT_DIR}/TestStaticLibRDC
3334

tests/runtime/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ add_hip_runtime_test(TestHipLaunchHostFunc.cpp)
185185

186186
add_hip_runtime_test(TestBoolKernelParam.hip)
187187
find_program(SPIRV_DIS spirv-dis)
188-
if(SPIRV_DIS)
188+
find_program(
189+
CLANG_OFFLOAD_BUNDLER
190+
NAMES clang-offload-bundler-${LLVM_VERSION_MAJOR} clang-offload-bundler
191+
PATHS ${HIP_CLANG_PATH})
192+
if(SPIRV_DIS AND (NOT USE_NEW_OFFLOAD_DRIVER OR CLANG_OFFLOAD_BUNDLER))
189193
add_shell_test(TestBoolKernelParamSPIRV.bash)
190194
endif()
191195
# Test hipLaunchHostFunc with multi-stream and events (from PR #1071)

tests/runtime/TestBoolKernelParamSPIRV.bash

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ fi
2222

2323
# Compile with --save-temps to get the .out SPIR-V file
2424
cd "$WORKDIR"
25-
"$HIPCC" --save-temps "$SRC" -o test_bool 2>/dev/null
2625

27-
# Find the SPIR-V binary
28-
SPV_FILE=$(ls *.out 2>/dev/null | head -1)
26+
if [ "@USE_NEW_OFFLOAD_DRIVER@" = "ON" ]; then
27+
"$HIPCC" --offload-device-only -c "$SRC" -o device.o 2>/dev/null
28+
"@CLANG_OFFLOAD_BUNDLER@" -unbundle --type=o \
29+
--targets=hip-@OFFLOAD_TRIPLE@--generic \
30+
--inputs=device.o --output=spv_binary.out
31+
SPV_FILE=spv_binary.out
32+
else
33+
"$HIPCC" --save-temps "$SRC" -o test_bool 2>/dev/null
34+
35+
# Find the SPIR-V binary
36+
SPV_FILE=$(ls *.out 2>/dev/null | head -1)
37+
fi
38+
2939
if [ -z "$SPV_FILE" ]; then
3040
echo "FAIL: No .out SPIR-V file produced by hipcc --save-temps"
3141
exit 1

0 commit comments

Comments
 (0)