From aa31e636e72ba79fc676a04657fdd8bb6bf3c91f Mon Sep 17 00:00:00 2001 From: Jeff Daily Date: Wed, 18 Nov 2026 03:58:56 +0000 Subject: [PATCH 1/2] [ROCm] Keep device-only HIP headers out of host compiles The HIP branch of cuda_to_hip.h included hip_cooperative_groups.h and hipcub/hipcub.hpp, and declared namespace cub = hipcub, unconditionally. Every generated kernel.h, caspar_mappings.h and solver.h includes cuda_to_hip.h, so a host translation unit built with the host C++ compiler that includes a generated header fails inside rocPRIM, for example with g++: "'__builtin_amdgcn_wavefrontsize' was not declared in this scope". A project that links a generated Caspar library and includes solver.h from its own .cc files cannot build. The CUDA branch already keeps cooperative groups and CUB behind __CUDACC__; the HIP branch now does the same behind __HIPCC__, which clang defines when it compiles HIP and the host compiler does not. Host code still gets the HIP runtime API and the cuda* spellings. The CUDA branch is unchanged. The HIP build of the *_cuda_test.cc tests compiled every test and generated .cc file as HIP, which is why it never saw this. They now stay with the host C++ compiler, as in the CUDA build, so caspar_solver_cuda_test.cc, caspar_tridiagonal_cuda_test.cc and both generated solver.cc files check that solver.h is includable from host code. Linking hip::hipcub would pass its hip::device options (-x hip, --offload-arch) to those host sources too, so the test runtime takes only the hipCUB and rocPRIM headers, which is all it needs. Without the header change, the six host objects of the four test targets fail to compile with the error above; with it, all four build and pass on an MI250X. Authored with Claude. Test Plan: ``` cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_HIP_ARCHITECTURES=gfx90a cmake --build build ctest --test-dir build -R caspar --output-on-failure ctest --test-dir build ``` --- symforce/caspar/source/runtime/cuda_to_hip.h | 8 +++++++- test/CMakeLists.txt | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/symforce/caspar/source/runtime/cuda_to_hip.h b/symforce/caspar/source/runtime/cuda_to_hip.h index a8d959f8..122b886b 100644 --- a/symforce/caspar/source/runtime/cuda_to_hip.h +++ b/symforce/caspar/source/runtime/cuda_to_hip.h @@ -13,9 +13,13 @@ #if defined(USE_HIP) || defined(__HIP_PLATFORM_AMD__) -#include #include + +// Host-only translation units (e.g. code using the generated headers) get just the runtime API. +#if defined(__HIPCC__) +#include #include +#endif // CUDA runtime API -> HIP runtime API #define cudaMalloc hipMalloc @@ -48,8 +52,10 @@ inline hipError_t cudaFuncSetAttribute(T* func, hipFuncAttribute attr, int value // so atomicAdd_block is equivalent to atomicAdd on shared memory. #define atomicAdd_block atomicAdd +#if defined(__HIPCC__) // CUB -> hipCUB namespace namespace cub = hipcub; +#endif #else // CUDA path diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ce003cdd..3d1892f4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -114,7 +114,12 @@ if(CMAKE_CUDA_COMPILER OR CMAKE_HIP_COMPILER) get_target_property(CASPAR_RUNTIME_SOURCES caspar_test_runtime SOURCES) set_source_files_properties(${CASPAR_RUNTIME_SOURCES} PROPERTIES LANGUAGE HIP) target_compile_definitions(caspar_test_runtime PUBLIC USE_HIP) - target_link_libraries(caspar_test_runtime PUBLIC hip::host hip::hipcub) + # hip::hipcub also carries hip::device, whose -x hip would reach the host-compiled .cc test + # sources below. hipCUB and rocPRIM are header-only, so take just their headers. + target_link_libraries(caspar_test_runtime PUBLIC hip::host roc::rocprim) + target_include_directories(caspar_test_runtime PUBLIC + $ + ) endif() # Each caspar__cuda_test.cc runs the code generated into symforce_caspar__test, @@ -137,8 +142,12 @@ if(CMAKE_CUDA_COMPILER OR CMAKE_HIP_COMPILER) add_executable(${TARGET_NAME} ${SOURCE_FILE} ${DATA_SOURCES}) if(CMAKE_HIP_COMPILER) - # The test and generated .cc files include the HIP runtime too. - set_source_files_properties(${SOURCE_FILE} ${DATA_SOURCES} PROPERTIES LANGUAGE HIP) + # Only the .cu files are HIP. The test and generated .cc files stay with the host C++ + # compiler, as in the CUDA build, so the generated headers are checked to be includable + # from host code. + set(HIP_SOURCES ${DATA_SOURCES}) + list(FILTER HIP_SOURCES INCLUDE REGEX "\\.cu$") + set_source_files_properties(${HIP_SOURCES} PROPERTIES LANGUAGE HIP) endif() set_target_properties(${TARGET_NAME} PROPERTIES CUDA_ARCHITECTURES 75 From c665e5cc723ccefd02e3505b0adeabc99cdb4838 Mon Sep 17 00:00:00 2001 From: Jeff Daily Date: Wed, 18 Nov 2026 04:49:22 +0000 Subject: [PATCH 2/2] [ROCm] Fix host compile of generated solver.cc on Windows The HIP test build compiles the generated solver.cc files with the host C++ compiler, as the CUDA build does. On Windows (clang++ over the MSVC STL and UCRT) both failed on names that only glibc and libstdc++ supply: sort_indices.h used uint, a glibc typedef it took from , and solver.cc called std::to_string without including . Compiled as HIP they had built, because the HIP headers supply both. sort_indices.h now spells the type unsigned int and includes for size_t. The solver.cc template includes , and also for printf and for std::swap, which it used without including either; the checked-in generated solvers are regenerated from it. The changes are type spellings and includes only: uint is unsigned int wherever it exists, so the declarations, their mangled names and the CUDA build are unchanged. To check on Linux, compile the generated solver.cc with uint, ushort and ulong defined to undeclared names after (which hides the glibc typedefs as Windows lacks them): both files fail before this change and build after it. Authored with Claude. Test Plan: ``` cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_HIP_ARCHITECTURES=gfx90a cmake --build build ctest --test-dir build -R caspar --output-on-failure ctest --test-dir build ``` --- symforce/caspar/source/runtime/sort_indices.h | 15 ++++++++------- symforce/caspar/source/templates/solver.cc.jinja | 3 +++ .../symforce_caspar_solver_test/solver.cc | 3 +++ .../symforce_caspar_tridiagonal_test/solver.cc | 3 +++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/symforce/caspar/source/runtime/sort_indices.h b/symforce/caspar/source/runtime/sort_indices.h index 180be9b7..700622d8 100644 --- a/symforce/caspar/source/runtime/sort_indices.h +++ b/symforce/caspar/source/runtime/sort_indices.h @@ -5,7 +5,7 @@ #pragma once -#include +#include #if defined(USE_HIP) || defined(__HIP_PLATFORM_AMD__) #include @@ -17,15 +17,16 @@ namespace caspar { size_t SortIndicesGetTmpNbytes(size_t problem_size); -void SortIndices(void* tmp_storage, size_t tmp_storage_bytes, const uint* keys_in, uint* sorted_out, - uint* target_out, uint* argsort_out, size_t problem_size); +void SortIndices(void* tmp_storage, size_t tmp_storage_bytes, const unsigned int* keys_in, + unsigned int* sorted_out, unsigned int* target_out, unsigned int* argsort_out, + size_t problem_size); size_t SortKeysGetTmpNbytes(size_t problem_size); -void SortKeys(void* tmp_storage, size_t tmp_storage_bytes, const uint* keys_in, uint* sorted_out, - size_t problem_size); +void SortKeys(void* tmp_storage, size_t tmp_storage_bytes, const unsigned int* keys_in, + unsigned int* sorted_out, size_t problem_size); -void SelectIndex(const uint* const input, const uint* const selections, uint* const output, - size_t problem_size); +void SelectIndex(const unsigned int* const input, const unsigned int* const selections, + unsigned int* const output, size_t problem_size); } // namespace caspar diff --git a/symforce/caspar/source/templates/solver.cc.jinja b/symforce/caspar/source/templates/solver.cc.jinja index 866a31ef..1119f1a5 100644 --- a/symforce/caspar/source/templates/solver.cc.jinja +++ b/symforce/caspar/source/templates/solver.cc.jinja @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #include "caspar_mappings.h" #include "shared_indices.h" diff --git a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc index e910f2c6..266df822 100644 --- a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc +++ b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_solver_test/solver.cc @@ -2,7 +2,10 @@ #include #include +#include #include +#include +#include #include "caspar_mappings.h" #include "kernel_Matrix21_alpha_denominator_or_beta_numerator.h" diff --git a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc index e82e6cd0..69b3b22b 100644 --- a/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc +++ b/test/symforce_function_codegen_test_data/symengine/symforce_caspar_tridiagonal_test/solver.cc @@ -2,7 +2,10 @@ #include #include +#include #include +#include +#include #include "caspar_mappings.h" #include "kernel_ChainA_alpha_denominator_or_beta_numerator.h"