Skip to content

Commit 34f8573

Browse files
authoredDec 25, 2024
[OpenMP] Use generic IR for the OpenMP DeviceRTL (#119091)
Summary: We previously built this for every single architecture to deal with incompatibility. This patch updates it to use the 'generic' IR that `libc` and other projects use. Who knows if this will have any side-effects, probably worth testing more but it passes the tests I expect to pass on my side.
1 parent cd66c9b commit 34f8573

File tree

10 files changed

+76
-118
lines changed

10 files changed

+76
-118
lines changed
 

‎clang/docs/ReleaseNotes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,9 @@ OpenMP Support
12501250
- Added support for 'omp assume' directive.
12511251
- Added support for 'omp scope' directive.
12521252
- Added support for allocator-modifier in 'allocate' clause.
1253+
- Changed the OpenMP DeviceRTL to use 'generic' IR. The
1254+
``LIBOMPTARGET_DEVICE_ARCHITECTURES`` CMake argument is now unused and will
1255+
always build support for AMDGPU and NVPTX targets.
12531256

12541257
Improvements
12551258
^^^^^^^^^^^^

‎clang/lib/Driver/ToolChains/CommonArgs.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -2838,8 +2838,7 @@ void tools::addOpenMPDeviceRTL(const Driver &D,
28382838
: options::OPT_libomptarget_nvptx_bc_path_EQ;
28392839

28402840
StringRef ArchPrefix = Triple.isAMDGCN() ? "amdgpu" : "nvptx";
2841-
std::string LibOmpTargetName =
2842-
("libomptarget-" + ArchPrefix + "-" + BitcodeSuffix + ".bc").str();
2841+
std::string LibOmpTargetName = ("libomptarget-" + ArchPrefix + ".bc").str();
28432842

28442843
// First check whether user specifies bc library
28452844
if (const Arg *A = DriverArgs.getLastArg(LibomptargetBCPathOpt)) {

‎clang/lib/Driver/ToolChains/Cuda.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,6 @@ void CudaToolChain::addClangTargetOptions(
851851
HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
852852

853853
StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
854-
assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
855854
assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
856855
DeviceOffloadingKind == Action::OFK_Cuda) &&
857856
"Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");

‎clang/test/Driver/openmp-offload-gpu.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
// RUN: %s 2>&1 | FileCheck -check-prefix=CHK-ENV-BCLIB %s
9191

9292
// CHK-BCLIB: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-builtin-bitcode{{.*}}libomptarget-nvptx-test.bc
93-
// CHK-BCLIB-DIR: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-builtin-bitcode{{.*}}libomptarget{{/|\\\\}}libomptarget-nvptx-sm_52.bc
94-
// CHK-ENV-BCLIB: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-builtin-bitcode{{.*}}subdir{{/|\\\\}}libomptarget-nvptx-sm_52.bc
93+
// CHK-BCLIB-DIR: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-builtin-bitcode{{.*}}libomptarget{{/|\\\\}}libomptarget-nvptx.bc
94+
// CHK-ENV-BCLIB: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-builtin-bitcode{{.*}}subdir{{/|\\\\}}libomptarget-nvptx.bc
9595
// CHK-BCLIB-NOT: {{error:|warning:}}
9696

9797
/// ###########################################################################

‎offload/DeviceRTL/CMakeLists.txt

+17-59
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,6 @@ set(devicertl_base_directory ${CMAKE_CURRENT_SOURCE_DIR})
4242
set(include_directory ${devicertl_base_directory}/include)
4343
set(source_directory ${devicertl_base_directory}/src)
4444

45-
set(all_amdgpu_architectures "gfx700;gfx701;gfx801;gfx803"
46-
"gfx9-generic;gfx900;gfx902;gfx906;gfx908"
47-
"gfx90a;gfx90c"
48-
"gfx9-4-generic;gfx940;gfx941;gfx942;gfx950"
49-
"gfx10-1-generic;gfx1010;gfx1012"
50-
"gfx10-3-generic;gfx1030;gfx1031;gfx1032;gfx1033"
51-
"gfx1034;gfx1035;gfx1036"
52-
"gfx11-generic;gfx1100;gfx1101;gfx1102;gfx1103"
53-
"gfx1150;gfx1151;gfx1152;gfx1153"
54-
"gfx12-generic")
55-
set(all_nvptx_architectures "sm_35;sm_37;sm_50;sm_52;sm_53;sm_60;sm_61;sm_62"
56-
"sm_70;sm_72;sm_75;sm_80;sm_86;sm_87;sm_89;sm_90")
57-
set(all_gpu_architectures
58-
"${all_amdgpu_architectures};${all_nvptx_architectures}")
59-
60-
set(LIBOMPTARGET_DEVICE_ARCHITECTURES "all" CACHE STRING
61-
"List of device architectures to be used to compile the OpenMP DeviceRTL.")
62-
63-
if(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "all")
64-
set(LIBOMPTARGET_DEVICE_ARCHITECTURES ${all_gpu_architectures})
65-
elseif(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "amdgpu")
66-
set(LIBOMPTARGET_DEVICE_ARCHITECTURES ${all_amdgpu_architectures})
67-
elseif(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "nvptx")
68-
set(LIBOMPTARGET_DEVICE_ARCHITECTURES ${all_nvptx_architectures})
69-
elseif(LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "auto" OR
70-
LIBOMPTARGET_DEVICE_ARCHITECTURES STREQUAL "native")
71-
if(NOT LIBOMPTARGET_NVPTX_ARCH AND NOT LIBOMPTARGET_AMDGPU_ARCH)
72-
message(FATAL_ERROR
73-
"Could not find 'amdgpu-arch' and 'nvptx-arch' tools required for 'auto'")
74-
elseif(NOT LIBOMPTARGET_FOUND_NVIDIA_GPU AND NOT LIBOMPTARGET_FOUND_AMDGPU_GPU)
75-
message(FATAL_ERROR "No AMD or NVIDIA GPU found on the system when using 'auto'")
76-
endif()
77-
set(LIBOMPTARGET_DEVICE_ARCHITECTURES
78-
"${LIBOMPTARGET_NVPTX_DETECTED_ARCH_LIST};${LIBOMPTARGET_AMDGPU_DETECTED_ARCH_LIST}")
79-
endif()
80-
list(REMOVE_DUPLICATES LIBOMPTARGET_DEVICE_ARCHITECTURES)
81-
8245
set(include_files
8346
${include_directory}/Allocator.h
8447
${include_directory}/Configuration.h
@@ -146,20 +109,22 @@ set(bc_flags -c -foffload-lto -std=c++17 -fvisibility=hidden
146109

147110
# first create an object target
148111
add_library(omptarget.devicertl.all_objs OBJECT IMPORTED)
149-
function(compileDeviceRTLLibrary target_cpu target_name target_triple)
112+
function(compileDeviceRTLLibrary target_name target_triple)
150113
set(target_bc_flags ${ARGN})
151114

152115
set(bc_files "")
153116
foreach(src ${src_files})
154117
get_filename_component(infile ${src} ABSOLUTE)
155118
get_filename_component(outfile ${src} NAME)
156-
set(outfile "${outfile}-${target_cpu}.bc")
119+
set(outfile "${outfile}-${target_name}.bc")
157120
set(depfile "${outfile}.d")
158121

122+
# Passing an empty CPU to -march= suppressed target specific metadata.
159123
add_custom_command(OUTPUT ${outfile}
160124
COMMAND ${CLANG_TOOL}
161125
${bc_flags}
162-
--offload-arch=${target_cpu}
126+
-fopenmp-targets=${target_triple}
127+
-Xopenmp-target=${target_triple} -march=
163128
${target_bc_flags}
164129
-MD -MF ${depfile}
165130
${infile} -o ${outfile}
@@ -182,7 +147,7 @@ function(compileDeviceRTLLibrary target_cpu target_name target_triple)
182147
list(APPEND bc_files ${outfile})
183148
endforeach()
184149

185-
set(bclib_name "libomptarget-${target_name}-${target_cpu}.bc")
150+
set(bclib_name "libomptarget-${target_name}.bc")
186151

187152
# Link to a bitcode library.
188153
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
@@ -222,7 +187,7 @@ function(compileDeviceRTLLibrary target_cpu target_name target_triple)
222187
APPEND)
223188
endif()
224189

225-
set(bclib_target_name "omptarget-${target_name}-${target_cpu}-bc")
190+
set(bclib_target_name "omptarget-${target_name}-bc")
226191
add_custom_target(${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name})
227192

228193
# Copy library to destination.
@@ -244,7 +209,7 @@ function(compileDeviceRTLLibrary target_cpu target_name target_triple)
244209
# Package the bitcode in the bitcode and embed it in an ELF for the static library
245210
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
246211
COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
247-
"--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},${target_feature},triple=${target_triple},arch=${target_cpu},kind=openmp"
212+
"--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},${target_feature},triple=${target_triple},arch=generic,kind=openmp"
248213
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
249214
COMMENT "Packaging LLVM offloading binary ${bclib_name}.out"
250215
)
@@ -254,14 +219,14 @@ function(compileDeviceRTLLibrary target_cpu target_name target_triple)
254219
APPEND)
255220
endif()
256221

257-
set(output_name "${CMAKE_CURRENT_BINARY_DIR}/devicertl-${target_name}-${target_cpu}.o")
222+
set(output_name "${CMAKE_CURRENT_BINARY_DIR}/devicertl-${target_name}.o")
258223
add_custom_command(OUTPUT ${output_name}
259224
COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
260225
-Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
261226
-o ${output_name}
262227
${source_directory}/Stub.cpp
263228
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} ${source_directory}/Stub.cpp
264-
COMMENT "Embedding LLVM offloading binary in devicertl-${target_name}-${target_cpu}.o"
229+
COMMENT "Embedding LLVM offloading binary in devicertl-${target_name}.o"
265230
VERBATIM
266231
)
267232
if(TARGET clang)
@@ -274,11 +239,11 @@ function(compileDeviceRTLLibrary target_cpu target_name target_triple)
274239
set_property(TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name})
275240

276241
if (CMAKE_EXPORT_COMPILE_COMMANDS)
277-
set(ide_target_name omptarget-ide-${target_name}-${target_cpu})
242+
set(ide_target_name omptarget-ide-${target_name})
278243
add_library(${ide_target_name} STATIC EXCLUDE_FROM_ALL ${src_files})
279244
target_compile_options(${ide_target_name} PRIVATE
280-
-fopenmp --offload-arch=${target_cpu} -fopenmp-cuda-mode
281-
-mllvm -openmp-opt-disable
245+
-fopenmp-targets=${target_triple} -Xopenmp-target=${target_triple} -march=
246+
-fopenmp -fopenmp-cuda-mode -mllvm -openmp-opt-disable
282247
-foffload-lto -fvisibility=hidden --offload-device-only
283248
-nocudalib -nogpulib -nogpuinc -nostdlibinc -Wno-unknown-cuda-version
284249
)
@@ -293,18 +258,11 @@ function(compileDeviceRTLLibrary target_cpu target_name target_triple)
293258
endif()
294259
endfunction()
295260

296-
# Generate a Bitcode library for all the gpu architectures the user requested.
297-
add_custom_target(omptarget.devicertl.nvptx)
298261
add_custom_target(omptarget.devicertl.amdgpu)
299-
foreach(gpu_arch ${LIBOMPTARGET_DEVICE_ARCHITECTURES})
300-
if("${gpu_arch}" IN_LIST all_amdgpu_architectures)
301-
compileDeviceRTLLibrary(${gpu_arch} amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
302-
elseif("${gpu_arch}" IN_LIST all_nvptx_architectures)
303-
compileDeviceRTLLibrary(${gpu_arch} nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
304-
else()
305-
message(FATAL_ERROR "Unknown GPU architecture '${gpu_arch}'")
306-
endif()
307-
endforeach()
262+
compileDeviceRTLLibrary(amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
263+
264+
add_custom_target(omptarget.devicertl.nvptx)
265+
compileDeviceRTLLibrary(nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
308266

309267
# Archive all the object files generated above into a static library
310268
add_library(omptarget.devicertl STATIC)

‎offload/DeviceRTL/src/Misc.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,7 @@ double getWTick() {
3939
}
4040

4141
double getWTime() {
42-
uint64_t NumTicks = 0;
43-
if constexpr (__has_builtin(__builtin_amdgcn_s_sendmsg_rtnl))
44-
NumTicks = __builtin_amdgcn_s_sendmsg_rtnl(0x83);
45-
else if constexpr (__has_builtin(__builtin_amdgcn_s_memrealtime))
46-
NumTicks = __builtin_amdgcn_s_memrealtime();
47-
else if constexpr (__has_builtin(__builtin_amdgcn_s_memtime))
48-
NumTicks = __builtin_amdgcn_s_memtime();
49-
50-
return static_cast<double>(NumTicks) * getWTick();
42+
return static_cast<double>(__builtin_readsteadycounter()) * getWTick();
5143
}
5244

5345
#pragma omp end declare variant

‎offload/DeviceRTL/src/Reduction.cpp

+46-45
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ void gpu_irregular_warp_reduce(void *reduce_data, ShuffleReductFnTy shflFct,
4444
}
4545
}
4646

47-
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ < 700
4847
static uint32_t gpu_irregular_simd_reduce(void *reduce_data,
4948
ShuffleReductFnTy shflFct) {
5049
uint32_t size, remote_id, physical_lane_id;
@@ -63,7 +62,6 @@ static uint32_t gpu_irregular_simd_reduce(void *reduce_data,
6362
} while (logical_lane_id % 2 == 0 && size > 1);
6463
return (logical_lane_id == 0);
6564
}
66-
#endif
6765

6866
static int32_t nvptx_parallel_reduce_nowait(void *reduce_data,
6967
ShuffleReductFnTy shflFct,
@@ -74,49 +72,53 @@ static int32_t nvptx_parallel_reduce_nowait(void *reduce_data,
7472
uint32_t NumThreads = omp_get_num_threads();
7573
if (NumThreads == 1)
7674
return 1;
77-
/*
78-
* This reduce function handles reduction within a team. It handles
79-
* parallel regions in both L1 and L2 parallelism levels. It also
80-
* supports Generic, SPMD, and NoOMP modes.
81-
*
82-
* 1. Reduce within a warp.
83-
* 2. Warp master copies value to warp 0 via shared memory.
84-
* 3. Warp 0 reduces to a single value.
85-
* 4. The reduced value is available in the thread that returns 1.
86-
*/
87-
88-
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700
89-
uint32_t WarpsNeeded =
90-
(NumThreads + mapping::getWarpSize() - 1) / mapping::getWarpSize();
91-
uint32_t WarpId = mapping::getWarpIdInBlock();
92-
93-
// Volta execution model:
94-
// For the Generic execution mode a parallel region either has 1 thread and
95-
// beyond that, always a multiple of 32. For the SPMD execution mode we may
96-
// have any number of threads.
97-
if ((NumThreads % mapping::getWarpSize() == 0) || (WarpId < WarpsNeeded - 1))
98-
gpu_regular_warp_reduce(reduce_data, shflFct);
99-
else if (NumThreads > 1) // Only SPMD execution mode comes thru this case.
100-
gpu_irregular_warp_reduce(reduce_data, shflFct,
101-
/*LaneCount=*/NumThreads % mapping::getWarpSize(),
102-
/*LaneId=*/mapping::getThreadIdInBlock() %
103-
mapping::getWarpSize());
10475

105-
// When we have more than [mapping::getWarpSize()] number of threads
106-
// a block reduction is performed here.
107-
//
108-
// Only L1 parallel region can enter this if condition.
109-
if (NumThreads > mapping::getWarpSize()) {
110-
// Gather all the reduced values from each warp
111-
// to the first warp.
112-
cpyFct(reduce_data, WarpsNeeded);
76+
//
77+
// This reduce function handles reduction within a team. It handles
78+
// parallel regions in both L1 and L2 parallelism levels. It also
79+
// supports Generic, SPMD, and NoOMP modes.
80+
//
81+
// 1. Reduce within a warp.
82+
// 2. Warp master copies value to warp 0 via shared memory.
83+
// 3. Warp 0 reduces to a single value.
84+
// 4. The reduced value is available in the thread that returns 1.
85+
//
11386

114-
if (WarpId == 0)
115-
gpu_irregular_warp_reduce(reduce_data, shflFct, WarpsNeeded,
116-
BlockThreadId);
87+
#if __has_builtin(__nvvm_reflect)
88+
if (__nvvm_reflect("__CUDA_ARCH") >= 700) {
89+
uint32_t WarpsNeeded =
90+
(NumThreads + mapping::getWarpSize() - 1) / mapping::getWarpSize();
91+
uint32_t WarpId = mapping::getWarpIdInBlock();
92+
93+
// Volta execution model:
94+
// For the Generic execution mode a parallel region either has 1 thread and
95+
// beyond that, always a multiple of 32. For the SPMD execution mode we may
96+
// have any number of threads.
97+
if ((NumThreads % mapping::getWarpSize() == 0) ||
98+
(WarpId < WarpsNeeded - 1))
99+
gpu_regular_warp_reduce(reduce_data, shflFct);
100+
else if (NumThreads > 1) // Only SPMD execution mode comes thru this case.
101+
gpu_irregular_warp_reduce(
102+
reduce_data, shflFct,
103+
/*LaneCount=*/NumThreads % mapping::getWarpSize(),
104+
/*LaneId=*/mapping::getThreadIdInBlock() % mapping::getWarpSize());
105+
106+
// When we have more than [mapping::getWarpSize()] number of threads
107+
// a block reduction is performed here.
108+
//
109+
// Only L1 parallel region can enter this if condition.
110+
if (NumThreads > mapping::getWarpSize()) {
111+
// Gather all the reduced values from each warp
112+
// to the first warp.
113+
cpyFct(reduce_data, WarpsNeeded);
114+
115+
if (WarpId == 0)
116+
gpu_irregular_warp_reduce(reduce_data, shflFct, WarpsNeeded,
117+
BlockThreadId);
118+
}
119+
return BlockThreadId == 0;
117120
}
118-
return BlockThreadId == 0;
119-
#else
121+
#endif
120122
__kmpc_impl_lanemask_t Liveness = mapping::activemask();
121123
if (Liveness == lanes::All) // Full warp
122124
gpu_regular_warp_reduce(reduce_data, shflFct);
@@ -150,10 +152,9 @@ static int32_t nvptx_parallel_reduce_nowait(void *reduce_data,
150152
return BlockThreadId == 0;
151153
}
152154

153-
// Get the OMP thread Id. This is different from BlockThreadId in the case of
154-
// an L2 parallel region.
155+
// Get the OMP thread Id. This is different from BlockThreadId in the case
156+
// of an L2 parallel region.
155157
return BlockThreadId == 0;
156-
#endif // __CUDA_ARCH__ >= 700
157158
}
158159

159160
uint32_t roundToWarpsize(uint32_t s) {

‎openmp/docs/ReleaseNotes.rst

+6
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ from the `LLVM releases web site <https://llvm.org/releases/>`_.
1919

2020
Non-comprehensive list of changes in this release
2121
=================================================
22+
23+
Device Runtime
24+
--------------
25+
- Changed the OpenMP DeviceRTL to use 'generic' IR. The
26+
``LIBOMPTARGET_DEVICE_ARCHITECTURES`` CMake argument is now unused and will
27+
always build support for AMDGPU and NVPTX targets.

0 commit comments

Comments
 (0)
Please sign in to comment.