@@ -145,21 +145,78 @@ function(get_nvptx_compile_options output_var gpu_arch)
145
145
set (${output_var} ${nvptx_options} PARENT_SCOPE )
146
146
endfunction ()
147
147
148
- # Builds the object target for the GPU.
148
+ # Build the object target for a single GPU arch.
149
+ # Usage:
150
+ # _build_gpu_object_for_single_arch(
151
+ # <target_name>
152
+ # <gpu_arch>
153
+ # SRCS <list of .cpp files>
154
+ # HDRS <list of .h files>
155
+ # DEPENDS <list of dependencies>
156
+ # COMPILE_OPTIONS <optional list of special compile options for this target>
157
+ # FLAGS <optional list of flags>
158
+ # )
159
+ function (_build_gpu_object_for_single_arch fq_target_name gpu_arch )
160
+ cmake_parse_arguments (
161
+ "ADD_GPU_OBJ"
162
+ "" # No optional arguments
163
+ "NAME;CXX_STANDARD" # Single value arguments
164
+ "SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;FLAGS" # Multi value arguments
165
+ ${ARGN}
166
+ )
167
+
168
+ if (NOT ADD_GPU_OBJ_CXX_STANDARD )
169
+ set (ADD_GPU_OBJ_CXX_STANDARD ${CMAKE_CXX_STANDARD} )
170
+ endif ()
171
+
172
+ set (compile_options ${ADD_GPU_OBJ_COMPILE_OPTIONS} )
173
+ # Derive the triple from the specified architecture.
174
+ if ("${gpu_arch} " IN_LIST all_amdgpu_architectures )
175
+ set (gpu_target_triple ${AMDGPU_TARGET_TRIPLE} )
176
+ list (APPEND compile_options "-mcpu=${gpu_arch} " )
177
+ list (APPEND compile_options "SHELL:-Xclang -mcode-object-version=none" )
178
+ list (APPEND compile_options "-emit-llvm" )
179
+ elseif ("${gpu_arch} " IN_LIST all_nvptx_architectures )
180
+ set (gpu_target_triple ${NVPTX_TARGET_TRIPLE} )
181
+ get_nvptx_compile_options (nvptx_options ${gpu_arch} )
182
+ list (APPEND compile_options "${nvptx_options} " )
183
+ else ()
184
+ message (FATAL_ERROR "Unknown GPU architecture '${gpu_arch} '" )
185
+ endif ()
186
+ list (APPEND compile_options "--target=${gpu_target_triple} " )
187
+
188
+ # Build the library for this target architecture. We always emit LLVM-IR for
189
+ # packaged GPU binaries.
190
+ add_library (${fq_target_name}
191
+ EXCLUDE_FROM_ALL
192
+ OBJECT
193
+ ${ADD_GPU_OBJ_SRCS}
194
+ ${ADD_GPU_OBJ_HDRS}
195
+ )
196
+
197
+ target_compile_options (${fq_target_name} PRIVATE ${compile_options} )
198
+ target_include_directories (${fq_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR} )
199
+ target_include_directories (${fq_target_name} PRIVATE ${LIBC_SOURCE_DIR} )
200
+ set_target_properties (${fq_target_name} PROPERTIES CXX_STANDARD ${ADD_GPU_OBJ_CXX_STANDARD} )
201
+ if (ADD_GPU_OBJ_DEPENDS )
202
+ add_dependencies (${fq_target_name} ${ADD_GPU_OBJ_DEPENDS} )
203
+ set_target_properties (${fq_target_name} PROPERTIES DEPS "${ADD_GPU_OBJ_DEPENDS} " )
204
+ endif ()
205
+ endfunction (_build_gpu_object_for_single_arch )
206
+
207
+ # Build the object target for the GPU.
149
208
# This compiles the target for all supported architectures and embeds it into
150
- # host binary for installing. The internal target contains the GPU code directly
151
- # compiled for a single architecture used internally.
209
+ # host binary for installing.
152
210
# Usage:
153
- # _build_gpu_objects (
211
+ # _build_gpu_object_bundle (
154
212
# <target_name>
155
- # <internal_target_name>
156
213
# SRCS <list of .cpp files>
157
214
# HDRS <list of .h files>
158
215
# DEPENDS <list of dependencies>
159
216
# COMPILE_OPTIONS <optional list of special compile options for this target>
160
217
# FLAGS <optional list of flags>
161
218
# )
162
- function (_build_gpu_objects fq_target_name internal_target_name )
219
+ function (_build_gpu_object_bundle fq_target_name )
163
220
cmake_parse_arguments (
164
221
"ADD_GPU_OBJ"
165
222
"" # No optional arguments
@@ -168,7 +225,6 @@ function(_build_gpu_objects fq_target_name internal_target_name)
168
225
${ARGN}
169
226
)
170
227
171
- set (common_compile_options ${ADD_GPU_OBJ_COMPILE_OPTIONS} )
172
228
if (NOT ADD_GPU_OBJ_CXX_STANDARD )
173
229
set (ADD_GPU_OBJ_CXX_STANDARD ${CMAKE_CXX_STANDARD} )
174
230
endif ()
@@ -179,49 +235,28 @@ function(_build_gpu_objects fq_target_name internal_target_name)
179
235
foreach (gpu_arch ${LIBC_GPU_ARCHITECTURES} )
180
236
get_filename_component (src_name ${add_gpu_obj_src} NAME )
181
237
set (gpu_target_name ${fq_target_name} .${src_name}.${gpu_arch} )
182
- set (compile_options ${ADD_GPU_OBJ_COMPILE_OPTIONS} )
183
- # Derive the triple from the specified architecture.
184
- if ("${gpu_arch} " IN_LIST all_amdgpu_architectures )
185
- set (gpu_target_triple "amdgcn-amd-amdhsa" )
186
- list (APPEND compile_options "-mcpu=${gpu_arch} " )
187
- list (APPEND compile_options "SHELL:-Xclang -mcode-object-version=none" )
188
- elseif ("${gpu_arch} " IN_LIST all_nvptx_architectures )
189
- set (gpu_target_triple "nvptx64-nvidia-cuda" )
190
- get_nvptx_compile_options (nvptx_options ${gpu_arch} )
191
- list (APPEND compile_options "${nvptx_options} " )
192
- else ()
193
- message (FATAL_ERROR "Unknown GPU architecture '${gpu_arch} '" )
194
- endif ()
195
- list (APPEND compile_options "--target=${gpu_target_triple} " )
196
- list (APPEND compile_options "-emit-llvm" )
197
-
198
- # Build the library for this target architecture. We always emit LLVM-IR for
199
- # packaged GPU binaries.
200
- add_library (${gpu_target_name}
201
- EXCLUDE_FROM_ALL
202
- OBJECT
203
- ${add_gpu_obj_src}
204
- ${ADD_GPU_OBJ_HDRS}
205
- )
206
-
207
- target_compile_options (${gpu_target_name} PRIVATE ${compile_options} )
208
- target_include_directories (${gpu_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR} )
209
- target_include_directories (${gpu_target_name} PRIVATE ${LIBC_SOURCE_DIR} )
210
- target_compile_definitions (${gpu_target_name} PRIVATE LIBC_COPT_PUBLIC_PACKAGING )
211
- set_target_properties (${gpu_target_name} PROPERTIES CXX_STANDARD ${ADD_GPU_OBJ_CXX_STANDARD} )
212
- if (ADD_GPU_OBJ_DEPENDS )
213
- add_dependencies (${gpu_target_name} ${ADD_GPU_OBJ_DEPENDS} )
214
- endif ()
215
238
239
+ _build_gpu_object_for_single_arch (
240
+ ${gpu_target_name}
241
+ ${gpu_arch}
242
+ CXX_STANDARD ${ADD_GPU_OBJ_CXX_STANDARD}
243
+ HDRS ${ADD_GPU_OBJ_HDRS}
244
+ SRCS ${add_gpu_obj_src}
245
+ COMPILE_OPTIONS
246
+ ${ADD_GPU_OBJ_COMPILE_OPTIONS}
247
+ "-emit-llvm"
248
+ DEPENDS ${ADD_GPU_OBJ_DEPENDS}
249
+ )
216
250
# Append this target to a list of images to package into a single binary.
217
251
set (input_file $< TARGET_OBJECTS:${gpu_target_name} > )
218
252
if ("${gpu_arch} " IN_LIST all_nvptx_architectures )
253
+ get_nvptx_compile_options (nvptx_options ${gpu_arch} )
219
254
string (REGEX MATCH "\\ +ptx[0-9]+" nvptx_ptx_feature ${nvptx_options} )
220
255
list (APPEND packager_images
221
- --image=file=${input_file},arch=${gpu_arch},triple=${gpu_target_triple },feature=${nvptx_ptx_feature} )
256
+ --image=file=${input_file},arch=${gpu_arch},triple=${NVPTX_TARGET_TRIPLE },feature=${nvptx_ptx_feature} )
222
257
else ()
223
258
list (APPEND packager_images
224
- --image=file=${input_file},arch=${gpu_arch},triple=${gpu_target_triple } )
259
+ --image=file=${input_file},arch=${gpu_arch},triple=${AMDGPU_TARGET_TRIPLE } )
225
260
endif ()
226
261
list (APPEND gpu_target_objects ${input_file} )
227
262
endforeach ()
@@ -269,7 +304,7 @@ function(_build_gpu_objects fq_target_name internal_target_name)
269
304
${CMAKE_CURRENT_BINARY_DIR} /stubs/${stub_filename}
270
305
)
271
306
target_compile_options (${fq_target_name} BEFORE PRIVATE
272
- ${common_compile_options } -nostdlib )
307
+ ${ADD_GPU_OBJ_COMPILE_OPTIONS } -nostdlib )
273
308
foreach (packaged_gpu_binary ${packaged_gpu_binaries} )
274
309
target_compile_options (${fq_target_name} PRIVATE
275
310
"SHELL:-Xclang -fembed-offload-object=${packaged_gpu_binary} " )
@@ -278,33 +313,6 @@ function(_build_gpu_objects fq_target_name internal_target_name)
278
313
target_include_directories (${fq_target_name} PRIVATE ${LIBC_SOURCE_DIR} )
279
314
add_dependencies (${fq_target_name}
280
315
${full_deps_list} ${packaged_gpu_names} ${stub_target_name} )
281
-
282
- # We only build the internal target for a single supported architecture.
283
- if (LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU OR
284
- LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX )
285
- add_library (
286
- ${internal_target_name}
287
- EXCLUDE_FROM_ALL
288
- OBJECT
289
- ${ADD_GPU_OBJ_SRCS}
290
- ${ADD_GPU_OBJ_HDRS}
291
- )
292
- target_compile_options (${internal_target_name} BEFORE PRIVATE
293
- ${common_compile_options} --target=${LIBC_GPU_TARGET_TRIPLE} )
294
- if (LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU )
295
- target_compile_options (${internal_target_name} PRIVATE
296
- "SHELL:-Xclang -mcode-object-version=none"
297
- -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto )
298
- elseif (LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX )
299
- get_nvptx_compile_options (nvptx_options ${LIBC_GPU_TARGET_ARCHITECTURE} )
300
- target_compile_options (${internal_target_name} PRIVATE ${nvptx_options} )
301
- endif ()
302
- target_include_directories (${internal_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR} )
303
- target_include_directories (${internal_target_name} PRIVATE ${LIBC_SOURCE_DIR} )
304
- if (full_deps_list )
305
- add_dependencies (${internal_target_name} ${full_deps_list} )
306
- endif ()
307
- endif ()
308
316
endfunction ()
309
317
310
318
# Rule which is essentially a wrapper over add_library to compile a set of
@@ -354,24 +362,38 @@ function(create_object_library fq_target_name)
354
362
# The GPU build uses a separate internal file.
355
363
if (LIBC_TARGET_ARCHITECTURE_IS_GPU AND NOT ${ADD_OBJECT_NO_GPU_BUNDLE} )
356
364
set (internal_target_name ${fq_target_name} .__internal__ )
365
+ set (public_packaging_for_internal "" )
357
366
else ()
358
367
set (internal_target_name ${fq_target_name} )
368
+ set (public_packaging_for_internal "-DLIBC_COPT_PUBLIC_PACKAGING" )
359
369
endif ()
360
370
361
371
_get_common_compile_options (compile_options "${ADD_OBJECT_FLAGS} " )
362
372
list (APPEND compile_options ${ADD_OBJECT_COMPILE_OPTIONS} )
363
373
364
374
# GPU builds require special handling for the objects because we want to
365
375
# export several different targets at once, e.g. for both Nvidia and AMD.
366
- if (LIBC_TARGET_ARCHITECTURE_IS_GPU AND NOT ${ADD_OBJECT_NO_GPU_BUNDLE} )
367
- _build_gpu_objects (
368
- ${fq_target_name}
376
+ if (LIBC_TARGET_ARCHITECTURE_IS_GPU )
377
+ if (NOT ${ADD_OBJECT_NO_GPU_BUNDLE} )
378
+ _build_gpu_object_bundle (
379
+ ${fq_target_name}
380
+ SRCS ${ADD_OBJECT_SRCS}
381
+ HDRS ${ADD_OBJECT_HDRS}
382
+ CXX_STANDARD ${ADD_OBJECT_CXX_STANDARD}
383
+ COMPILE_OPTIONS ${compile_options} "-DLIBC_COPT_PUBLIC_PACKAGING"
384
+ DEPENDS ${fq_deps_list}
385
+ )
386
+ endif ()
387
+ # When the target for GPU is not bundled, internal_target_name is the same
388
+ # as fq_targetname
389
+ _build_gpu_object_for_single_arch (
369
390
${internal_target_name}
391
+ ${LIBC_GPU_TARGET_ARCHITECTURE}
370
392
SRCS ${ADD_OBJECT_SRCS}
371
393
HDRS ${ADD_OBJECT_HDRS}
372
- DEPENDS ${fq_deps_list}
373
394
CXX_STANDARD ${ADD_OBJECT_CXX_STANDARD}
374
- COMPILE_OPTIONS ${compile_options}
395
+ COMPILE_OPTIONS ${compile_options} ${public_packaging_for_internal}
396
+ DEPENDS ${fq_deps_list}
375
397
)
376
398
else ()
377
399
add_library (
@@ -567,9 +589,18 @@ function(create_entrypoint_object fq_target_name)
567
589
# GPU builds require special handling for the objects because we want to
568
590
# export several different targets at once, e.g. for both Nvidia and AMD.
569
591
if (LIBC_TARGET_ARCHITECTURE_IS_GPU )
570
- _build_gpu_objects (
592
+ _build_gpu_object_bundle (
571
593
${fq_target_name}
594
+ SRCS ${ADD_ENTRYPOINT_OBJ_SRCS}
595
+ HDRS ${ADD_ENTRYPOINT_OBJ_HDRS}
596
+ COMPILE_OPTIONS ${common_compile_options} "-DLIBC_COPT_PUBLIC_PACKAGING"
597
+ CXX_STANDARD ${ADD_ENTRYPOINT_OBJ_CXX_STANDARD}
598
+ DEPENDS ${full_deps_list}
599
+ FLAGS "${ADD_ENTRYPOINT_OBJ_FLAGS} "
600
+ )
601
+ _build_gpu_object_for_single_arch (
572
602
${internal_target_name}
603
+ ${LIBC_GPU_TARGET_ARCHITECTURE}
573
604
SRCS ${ADD_ENTRYPOINT_OBJ_SRCS}
574
605
HDRS ${ADD_ENTRYPOINT_OBJ_HDRS}
575
606
COMPILE_OPTIONS ${common_compile_options}
0 commit comments