|
| 1 | +# Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +# All rights not expressly granted are reserved. |
| 4 | +# |
| 5 | +# This software is distributed under the terms of the GNU General Public |
| 6 | +# License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +# |
| 8 | +# In applying this license CERN does not waive the privileges and immunities |
| 9 | +# granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +# or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +set(MODULE GPUTrackingMETAL) |
| 13 | +enable_language(ASM) |
| 14 | + |
| 15 | +message(STATUS "Building GPUTracking with Metal support") |
| 16 | + |
| 17 | +# convenience variables |
| 18 | +if(ALIGPU_BUILD_TYPE STREQUAL "Standalone") |
| 19 | + set(GPUDIR ${CMAKE_SOURCE_DIR}/../) |
| 20 | +else() |
| 21 | + set(GPUDIR ${CMAKE_SOURCE_DIR}/GPU/GPUTracking) |
| 22 | +endif() |
| 23 | +set(METAL_SRC ${GPUDIR}/Base/metal/GPUReconstructionMETAL.metal) |
| 24 | +set(METAL_BIN ${CMAKE_CURRENT_BINARY_DIR}/GPUReconstructionMetalCode) |
| 25 | + |
| 26 | +# MSL 4.1 is the first version with a generic address space: earlier versions |
| 27 | +# reject an unannotated pointer or `this` outright, which GPUCommonDefAPI.h |
| 28 | +# relies on for GPUgeneric() and GPUdDefault(). |
| 29 | +set(METAL_FLAGS -std=metal4.1 ${GPUCA_METAL_DENORMALS_FLAGS}) |
| 30 | +if(GPUCA_DETERMINISTIC_MODE GREATER_EQUAL ${GPUCA_DETERMINISTIC_MODE_MAP_NO_FAST_MATH}) |
| 31 | + set(METAL_FLAGS ${METAL_FLAGS} ${GPUCA_METAL_NO_FAST_MATH_FLAGS}) |
| 32 | +endif() |
| 33 | +set(METAL_DEFINES "-D$<JOIN:$<TARGET_PROPERTY:O2::GPUTracking,COMPILE_DEFINITIONS>,$<SEMICOLON>-D>" |
| 34 | + "-I$<JOIN:$<FILTER:$<TARGET_PROPERTY:O2::GPUTracking,INCLUDE_DIRECTORIES>,EXCLUDE,^/usr/include/?>,$<SEMICOLON>-I>" |
| 35 | + -I${CMAKE_SOURCE_DIR}/Detectors/TRD/base/src |
| 36 | + -I${CMAKE_SOURCE_DIR}/Detectors/Base/src |
| 37 | + -I${CMAKE_SOURCE_DIR}/DataFormats/Reconstruction/src |
| 38 | +) |
| 39 | + |
| 40 | +set(SRCS GPUReconstructionMetal.mm GPUReconstructionMetalKernels.mm) |
| 41 | +set(HDRS GPUReconstructionMetal.h GPUReconstructionMetalIncludesHost.h) |
| 42 | + |
| 43 | +if(ALIGPU_BUILD_TYPE STREQUAL "O2") |
| 44 | + o2_add_library(${MODULE} |
| 45 | + SOURCES ${SRCS} |
| 46 | + PUBLIC_LINK_LIBRARIES O2::GPUTracking |
| 47 | + TARGETVARNAME targetName) |
| 48 | + |
| 49 | + target_link_libraries(${targetName} PUBLIC ${METAL_FRAMEWORKS}) |
| 50 | + |
| 51 | + target_compile_definitions(${targetName} PRIVATE $<TARGET_PROPERTY:O2::GPUTracking,COMPILE_DEFINITIONS>) |
| 52 | + # the compile_defitions are not propagated automatically on purpose (they are |
| 53 | + # declared PRIVATE) so we are not leaking them outside of the GPU** |
| 54 | + # directories |
| 55 | +endif() |
| 56 | + |
| 57 | +if(ALIGPU_BUILD_TYPE STREQUAL "Standalone") |
| 58 | + add_library(${MODULE} SHARED ${SRCS}) |
| 59 | + target_link_libraries(${MODULE} GPUTracking) |
| 60 | + install(TARGETS ${MODULE}) |
| 61 | + set(targetName ${MODULE}) |
| 62 | +endif() |
| 63 | + |
| 64 | +if(METAL_ENABLED) # BUILD Metal source code for runtime compilation target |
| 65 | + |
| 66 | + # executes clang to preprocess |
| 67 | + add_custom_command( |
| 68 | + OUTPUT ${METAL_BIN}.metal |
| 69 | + COMMAND xcrun -sdk macosx metal |
| 70 | + -Wno-unused-command-line-argument |
| 71 | + ${METAL_FLAGS} |
| 72 | + ${METAL_DEFINES} |
| 73 | + -MD -MT ${METAL_BIN}.src -MF ${METAL_BIN}.src.d |
| 74 | + -E -P ${METAL_SRC} > ${METAL_BIN}.metal |
| 75 | + DEPENDS ${METAL_SRC} |
| 76 | + DEPFILE ${METAL_BIN}.src.d |
| 77 | + COMMAND_EXPAND_LISTS |
| 78 | + COMMENT "Preparing Metal source file for run time compilation ${METAL_BIN}.metal") |
| 79 | + |
| 80 | + # Create the ir |
| 81 | + add_custom_command( |
| 82 | + OUTPUT ${METAL_BIN}.ir |
| 83 | + COMMAND xcrun -sdk macosx metal |
| 84 | + -Wno-unused-command-line-argument |
| 85 | + -Wno-c++17-extensions |
| 86 | + -ferror-limit=10000 |
| 87 | + ${METAL_FLAGS} |
| 88 | + ${METAL_DEFINES} |
| 89 | + ${METAL_BIN}.metal |
| 90 | + -o ${METAL_BIN}.ir |
| 91 | + DEPENDS ${METAL_BIN}.metal |
| 92 | + COMMAND_EXPAND_LISTS |
| 93 | + COMMENT "Preparing Metal intermediate representation for run time compilation ${METAL_BIN}.ir") |
| 94 | + |
| 95 | + add_custom_target(metal_preprocessed_code ALL DEPENDS ${METAL_BIN}.metal COMMENT "Needed to inject dependency on its creation") |
| 96 | + add_custom_target(metal_intermediate_representation ALL DEPENDS ${METAL_BIN}.ir COMMENT "Needed to inject dependency on its creation") |
| 97 | + |
| 98 | + # Pack the compiled library into __DATA,__gpu_resource during final link. This |
| 99 | + # way we do not need to create an intermediate object. Compiling the source at |
| 100 | + # run time is not an option: the driver's compiler service dies on it. |
| 101 | + target_link_options(${targetName} |
| 102 | + PRIVATE |
| 103 | + "-Wl,-sectcreate,__DATA,__gpu_resource,${METAL_BIN}.ir") |
| 104 | + add_dependencies(${targetName} metal_preprocessed_code) |
| 105 | + add_dependencies(${targetName} metal_intermediate_representation) |
| 106 | +endif() |
| 107 | + |
| 108 | +install(FILES ${HDRS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/GPU) |
| 109 | + |
| 110 | +target_compile_definitions(${targetName} PRIVATE GPUCA_METAL_BUILD_FLAGS=$<JOIN:${METAL_FLAGS},\ > ) |
0 commit comments