radiance 1.0.0

This commit is contained in:
Deadcode committed 2026-10-04 12:02:41 +02:00
commit ae74a8aebf
616 files changed
+338692

No files matched your search

+206
View File
@@ -0,0 +1,206 @@
# rad_add_plugin / rad_hipify -- the ONE definition of how a source file becomes a loadable
# radiance plugin, shared by the in-tree build and by an out-of-tree one.
#
# This file is included from two different worlds. The top-level CMakeLists.txt includes it while
# the ABI target is being built in this very tree; the installed radiance-config.cmake includes it
# after pulling the same target in as an import from radianceTargets.cmake. It is a file of its own
# because a third party who runs find_package(radiance) needs it as much as the tree does: the
# single call that decides what a plugin links, where it lands, and how its device sources reach
# hipcc cannot be a private detail of radiance's own build, or every out-of-tree plugin has to
# reinvent it by reading our CMakeLists.
#
# Copying the function into the package config instead is the worse option. Two copies of the rule
# that fixes the plugin layout drift, and the drift does not present as a build error: it presents
# as a plugin that loads in the tree it was written in and is silently absent everywhere else,
# because it was emitted one directory level away from where the loader scans. So there is one
# file, and both worlds include it.
#
# What an includer must set first:
# radiance::abi the ABI interface target -- an in-tree ALIAS, or the imported target
# RAD_PLUGIN_OUT the build-tree $RADIANCE_HOME; a plugin lands in ${RAD_PLUGIN_OUT}/<kind>
# RAD_HIP_FOUND whether device sources can be compiled at all
# RAD_GPU_TARGETS the offload architectures this build targets. Any gfx name; a library that
# serves only some of them narrows the list with rad_plugin_gpu_targets()
# RAD_HIP_RUNTIME the amdhip64 library a device plugin links
# The top-level file sets all five as it detects them. radiance-config.cmake sets all five from what
# the install recorded, so that an out-of-tree plugin is compiled against the same runtime as the
# engine that will dlopen it; its GPU targets are the consumer's to name (-DRAD_GPU_TARGETS) and
# default to the ones the engine was built for.
#
# And one an includer MAY set:
# RAD_PLUGIN_INSTALL_HOME the $RADIANCE_HOME `cmake --install` puts a plugin under. Unset in
# the tree, where a plugin installs beside the engine under the same prefix;
# radiance-config.cmake sets it to the found installation's home, so a plugin
# built out of tree installs where that engine's loader scans.
# For CMAKE_INSTALL_DATADIR, below. Idempotent, and included here rather than left to the includer
# because this file is what needs it: a consumer project has no reason to have included it, and the
# install destination would silently become the relative path "radiance/kernels" -- interpreted
# against the prefix, so it lands in <prefix>/radiance/kernels and the loader never looks there.
include(GNUInstallDirs)
# What radiance's own tree sets for every plugin it builds, for a plugin built on its own against
# an installed radiance: C++20, and a build type that optimises when none was asked for. Every
# plugin directory in radiance's tree opens with the same few lines a third party's does --
#
# if(NOT COMMAND rad_add_plugin) # on its own, not inside radiance's tree
# cmake_minimum_required(VERSION 3.21)
# project(mylib LANGUAGES CXX) # CXX HIP for device code
# find_package(radiance REQUIRED)
# rad_plugin_defaults()
# endif()
#
# -- so the libraries radiance ships are built and installed exactly as anyone else's are.
macro(rad_plugin_defaults)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()
endmacro()
# A kernel, architecture or quantiser plugin: one call, so a new plugin is a directory and a line.
# `kind` is the $RADIANCE_HOME directory the engine scans for it. An installed plugin must name one
# of the three the engine scans -- a misspelt kind would build, install, and never be loaded; the
# test plugins, which are never installed (RAD_PLUGIN_INSTALL below), use directories of their own.
function(rad_add_plugin name kind)
cmake_parse_arguments(P "" "" "SOURCES;HIP_SOURCES;LIBS;INCLUDES;GPU_TARGETS" ${ARGN})
if((NOT DEFINED RAD_PLUGIN_INSTALL OR RAD_PLUGIN_INSTALL) AND
NOT kind MATCHES "^(kernels|architectures|quantizers)$")
message(FATAL_ERROR
"rad_add_plugin(${name} ${kind}): the kind is the directory the engine loads the plugin "
"from, and it is one of kernels, architectures or quantizers.")
endif()
set(srcs ${P_SOURCES})
if(P_HIP_SOURCES AND NOT RAD_HIP_FOUND)
# Said rather than skipped in silence: a kernel library whose device sources were dropped
# builds, installs, and has rows with no kernels behind them.
message(WARNING
"rad_add_plugin(${name}) was given HIP_SOURCES but this radiance has no device build "
"(RAD_HIP_FOUND is OFF), so they are left out. A radiance configured without ROCm, or on a "
"machine with no card and no -DRAD_GPU_TARGETS, has none.")
endif()
if(RAD_HIP_FOUND AND P_HIP_SOURCES)
# The HIP language has to be enabled by the project that owns the target, not by us:
# enable_language() is a project-scope decision and a library file that reaches out and enables
# a language behind the consumer's back changes their compiler set. In-tree the top-level file
# does it. Out-of-tree it is one line in the consumer's CMakeLists, and the failure without it
# is "Cannot find source file" or a source compiled as C++ with --offload-arch on the command
# line, neither of which names the missing enable_language(HIP).
get_property(_rad_langs GLOBAL PROPERTY ENABLED_LANGUAGES)
if(NOT "HIP" IN_LIST _rad_langs)
message(FATAL_ERROR
"rad_add_plugin(${name}) was given HIP_SOURCES but the HIP language is not enabled in this "
"project. Add `enable_language(HIP)` after project() -- radiance cannot enable it for you, "
"because the language a project compiles in is the project's own decision.")
endif()
set_source_files_properties(${P_HIP_SOURCES} PROPERTIES LANGUAGE HIP)
list(APPEND srcs ${P_HIP_SOURCES})
endif()
add_library(${name} MODULE ${srcs})
target_link_libraries(${name} PRIVATE radiance::abi ${P_LIBS})
target_include_directories(${name} PRIVATE ${P_INCLUDES})
# The build tree is a working $RADIANCE_HOME, and it is created here rather than assumed: a
# LIBRARY_OUTPUT_DIRECTORY that does not exist is created by the build, but the engine scans the
# directory at startup and an out-of-tree build with one kernel plugin and no architectures would
# otherwise have no architectures/ for the scan to find and fail at the scan rather than at the
# lookup.
file(MAKE_DIRECTORY ${RAD_PLUGIN_OUT}/${kind})
set_target_properties(${name} PROPERTIES
PREFIX "" OUTPUT_NAME "${name}"
LIBRARY_OUTPUT_DIRECTORY ${RAD_PLUGIN_OUT}/${kind})
if(RAD_HIP_FOUND AND P_HIP_SOURCES)
rad_hipify(${name} ${P_GPU_TARGETS})
endif()
# ...and so is the install tree, in the same shape. RAD_DEFAULT_HOME points at
# <prefix>/share/radiance, and without this rule nothing is installed into it, so the only
# $RADIANCE_HOME that works is somebody's build directory. A plugin built and not installed is the
# same class of defect as a plugin built and never loaded.
#
# RAD_PLUGIN_INSTALL is the opt-out, and it has exactly one in-tree user: core/plugin/testplugin
# (set in core/CMakeLists.txt, where that subtree is reached), whose .so files exist to be
# refused, to conflict, to report a stale ABI and to stand in for a card. Shipping those
# into a prefix would put rows into a real installation that implement nothing -- see the same
# argument, made about the build tree, in core/plugin/testplugin/CMakeLists.txt.
if(NOT DEFINED RAD_PLUGIN_INSTALL)
set(RAD_PLUGIN_INSTALL ON)
endif()
if(RAD_PLUGIN_INSTALL)
if(RAD_PLUGIN_INSTALL_HOME)
set(_rad_dest "${RAD_PLUGIN_INSTALL_HOME}/${kind}")
else()
set(_rad_dest "${CMAKE_INSTALL_DATADIR}/radiance/${kind}")
endif()
install(TARGETS ${name} LIBRARY DESTINATION "${_rad_dest}")
endif()
endfunction()
# Mark a target's device sources as HIP and pin the architecture. -ffp-contract=off is mandatory
# for any quantised cross-rank op: two ranks see the two dequantised products in the opposite order
# and contracting either into an FMA makes them disagree by ~1 ULP, which breaks the
# replicated-state invariant.
function(rad_hipify tgt)
# Any extra arguments are the architectures THIS target is compiled for, which is how a library
# that serves one generation stays out of the build for the others. With none given the target
# takes the whole build's list.
set(arches "${ARGN}")
if(NOT arches)
set(arches "${RAD_GPU_TARGETS}")
endif()
set_target_properties(${tgt} PROPERTIES HIP_ARCHITECTURES "${arches}")
target_compile_options(${tgt} PRIVATE $<$<COMPILE_LANGUAGE:HIP>:-ffp-contract=off>)
# THE DEVICE OPTIMISATION LEVEL, ON THE TARGET, because CMake supplies no usable one for HIP:
# depending on its version CMAKE_HIP_FLAGS_<CONFIG> is empty -- the kernels compile at -O0 with
# asserts live -- or the host-style '-O2 -g'. The top-level CMakeLists.txt sets the variables for
# the tree; a plugin built out of tree gets the same levels here, after the toolchain's flags on
# the command line, so they win. A later -O replaces an earlier one but a later -O does not undo
# -g, so the levels outside Debug end in -g0: device debug info costs compile time, inhibits
# scheduling, and makes an out-of-tree build's code objects differ from the tree's. NDEBUG is not
# decoration on the device: an assert is a hostcall, and the engine's queues provide no hostcall
# buffer (docs/PLUGIN.md). RAD_HIP_FLAGS_<CONFIG> overrides, as it does in the tree.
foreach(cfg DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
if(RAD_HIP_FLAGS_${cfg})
set(lvl "${RAD_HIP_FLAGS_${cfg}}")
elseif(cfg STREQUAL "DEBUG")
set(lvl "-O0 -g")
elseif(cfg STREQUAL "MINSIZEREL")
set(lvl "-Os -DNDEBUG -g0")
else()
set(lvl "-O3 -DNDEBUG -g0")
endif()
separate_arguments(lvl)
target_compile_options(${tgt} PRIVATE "$<$<AND:$<COMPILE_LANGUAGE:HIP>,$<CONFIG:${cfg}>>:${lvl}>")
endforeach()
# AN UNCOMPRESSED FAT BINARY, because the engine reads the code object out of it itself
# (core/device/aql_code.cpp) and refuses a compressed one -- at the first launch, as a kernel
# failure. A toolchain or a CMAKE_HIP_FLAGS that turns compression on is overridden here.
target_compile_options(${tgt} PRIVATE $<$<COMPILE_LANGUAGE:HIP>:--no-offload-compress>)
target_link_options(${tgt} PRIVATE $<$<LINK_LANGUAGE:HIP>:--no-offload-compress>)
target_link_libraries(${tgt} PRIVATE ${RAD_HIP_RUNTIME})
endfunction()
# Narrow RAD_GPU_TARGETS to the architectures ONE kernel library actually serves. A library states
# its coverage as regular expressions -- "^gfx12" for a generation, "^gfx1201$" for a single chip --
# and gets back the intersection with what this build is targeting, which may be empty.
#
# AN EMPTY RESULT IS NOT AN ERROR. It means this build's cards are served by some other kernel
# library, and the right response is for the plugin to skip itself and say so, leaving the rest of
# the tree to configure normally. A kernel library that failed the configure instead would make
# every card it does not cover an unbuildable project rather than an unserved one -- which is the
# opposite of what a plugin boundary is for.
function(rad_plugin_gpu_targets out)
set(keep "")
foreach(t ${RAD_GPU_TARGETS})
foreach(pat ${ARGN})
if(t MATCHES "${pat}")
list(APPEND keep ${t})
break()
endif()
endforeach()
endforeach()
list(REMOVE_DUPLICATES keep)
set(${out} "${keep}" PARENT_SCOPE)
endfunction()
+104
View File
@@ -0,0 +1,104 @@
# radiance-config.cmake -- what `find_package(radiance)` gets.
#
# This file exists so that a plugin can be built from a repository that is not this one, against
# the installed ABI headers rather than a copy of abi/ taken out of a checkout, and installed into
# the $RADIANCE_HOME the engine's loader scans. A plugin boundary that can only be crossed from
# inside the tree is not a plugin boundary; it is a build layout.
#
# What a consumer gets:
# radiance::abi the header-only ABI target -- abi/ plus RAD_DEFAULT_HOME,
# RAD_HAVE_HIP and the ROCm include path, exactly as the engine's own
# plugins were compiled with them
# rad_add_plugin() the same function radiance builds its own plugins with
# RADIANCE_ABI_VERSION the number the loader compares against, so a consumer can refuse at
# configure time rather than at dlopen
# RADIANCE_HOME the installed $RADIANCE_HOME; `cmake --install` puts a plugin
# built with rad_add_plugin under it (RAD_PLUGIN_INSTALL_HOME)
# RADIANCE_KERNELS_DIR ${RADIANCE_HOME}/kernels
# RADIANCE_ARCHITECTURES_DIR ${RADIANCE_HOME}/architectures
# RADIANCE_QUANTIZERS_DIR ${RADIANCE_HOME}/quantizers
# RADIANCE_INCLUDE_DIR the include root, for a consumer that wants the path rather than the
# target (arch/common/ is installed under ${RADIANCE_INCLUDE_DIR}/arch)
# RAD_GPU_TARGETS the offload architectures a device plugin is compiled for: the
# consumer's -DRAD_GPU_TARGETS when given, else the ones radiance was
# built for. The engine itself runs on every AMD GPU family, so a
# plugin for a card the engine's build machine does not have is
# ordinary.
@PACKAGE_INIT@
# The ABI version is not written here; it is read out of abi/rad_abi.h at radiance's
# configure time and carried through. One number, in the header the loader compares against, so a
# bump cannot be half-applied.
set(RADIANCE_ABI_VERSION @RAD_ABI_VERSION@)
set_and_check(RADIANCE_INCLUDE_DIR "@PACKAGE_RAD_INCLUDE_RELDIR@")
# $RADIANCE_HOME, as a CACHE PATH so that a consumer building a plugin for an installed engine can
# see and override where it is going in ccmake, and so that `cmake --install` on their project puts
# the .so where this engine's loader will scan for it. Computed from PACKAGE_PREFIX_DIR rather than
# baked absolute, so a relocated prefix still answers correctly.
set(RADIANCE_HOME "@PACKAGE_RAD_HOME_RELDIR@" CACHE PATH
"\$RADIANCE_HOME of the radiance installation found by find_package(radiance)")
set(RADIANCE_KERNELS_DIR "${RADIANCE_HOME}/kernels")
set(RADIANCE_ARCHITECTURES_DIR "${RADIANCE_HOME}/architectures")
set(RADIANCE_QUANTIZERS_DIR "${RADIANCE_HOME}/quantizers")
# Where rad_add_plugin installs: under the home of the engine this consumer built against, which is
# the one whose loader will scan it. A consumer installing somewhere else -- a home of its own on
# $RADIANCE_HOME's search path, say -- sets this.
set(RAD_PLUGIN_INSTALL_HOME "${RADIANCE_HOME}" CACHE PATH
"the \$RADIANCE_HOME rad_add_plugin installs plugins under")
# The imported targets: radiance::abi, and whatever else the export set carries.
include("${CMAKE_CURRENT_LIST_DIR}/radianceTargets.cmake")
# The device-build facts rad_add_plugin needs, recorded at radiance's configure time and replayed
# here under the same names the in-tree build uses -- one function body, one set of variable names.
# A plugin compiled for a different gfx target than the engine that dlopens it is not a link error
# and not a load error: it is a kernel that launches and computes garbage, so the target list is
# carried across rather than re-detected.
set(RAD_HIP_FOUND @RAD_HIP_FOUND@)
if(NOT DEFINED RAD_GPU_TARGETS)
set(RAD_GPU_TARGETS "@RAD_GPU_TARGETS@")
endif()
# The HIP runtime and headers as radiance found them, and where this machine has them when the
# recorded path is not here -- a consumer building against an installed radiance on another
# machine, or inside a container whose ROCm sits elsewhere.
set(RAD_HIP_RUNTIME "@RAD_HIP_RUNTIME@")
if(RAD_HIP_FOUND)
if(NOT EXISTS "${RAD_HIP_RUNTIME}")
find_library(RAD_HIP_RUNTIME_HERE amdhip64 HINTS ENV ROCM_PATH ENV HIP_PATH /opt/rocm
PATH_SUFFIXES lib lib64)
set(RAD_HIP_RUNTIME "${RAD_HIP_RUNTIME_HERE}")
endif()
set(_rad_rocm_inc "@RAD_ROCM_INCLUDE@")
if(NOT EXISTS "${_rad_rocm_inc}/hip/hip_runtime.h")
find_path(_rad_rocm_inc_here hip/hip_runtime.h HINTS ENV ROCM_PATH ENV HIP_PATH /opt/rocm
PATH_SUFFIXES include)
set(_rad_rocm_inc "${_rad_rocm_inc_here}")
endif()
if(_rad_rocm_inc)
set_property(TARGET radiance::abi APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${_rad_rocm_inc}")
set_property(TARGET radiance::abi APPEND PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_rad_rocm_inc}")
endif()
# The HIP compiler's two omissions the tree fixes for itself (CMakeLists.txt): no depfile, so no
# header is a dependency of a .hip object, and no system-include flag. Only where unset.
if(NOT CMAKE_DEPFILE_FLAGS_HIP)
set(CMAKE_DEPFILE_FLAGS_HIP "-MD -MT <DEP_TARGET> -MF <DEP_FILE>")
endif()
if(NOT CMAKE_INCLUDE_SYSTEM_FLAG_HIP)
set(CMAKE_INCLUDE_SYSTEM_FLAG_HIP "-isystem ")
endif()
endif()
# The build-tree $RADIANCE_HOME for the CONSUMER's build, so that a freshly built out-of-tree plugin
# can be exercised with RADIANCE_HOME=<their build dir>/radiance_home before it is installed --
# which is how an in-tree plugin is tested, and there is no reason the boundary should make that
# harder. Not forced: a consumer who has already set it keeps their own.
if(NOT DEFINED RAD_PLUGIN_OUT)
set(RAD_PLUGIN_OUT ${CMAKE_BINARY_DIR}/radiance_home)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/RadAddPlugin.cmake")
check_required_components(radiance)