Skip to content

Fix round 2 for symforce#465: host-safe HIP compat header, Windows host compile - #3

Merged
jeffdaily merged 2 commits into
moat-portfrom
moat-fix-465
Nov 18, 2026
Merged

jeffdaily merged 2 commits into
moat-portfrom
moat-fix-465

Conversation

@jeffdaily

Copy link
Copy Markdown
Collaborator

Two commits on top of the published tip e7e0b00c, for the open upstream PR symforce-org#465. Nobody upstream asked for this round: a HIP build of colmap's Caspar kernels with this PR applied showed that a host-compiled file including a generated header does not build.

What changed

  • aa31e636 Keep device-only HIP headers out of host compiles. The HIP branch of cuda_to_hip.h included hip_cooperative_groups.h and hipcub.hpp unconditionally, so a .cc file compiled by g++ that includes a generated solver.h failed in rocPRIM (__builtin_amdgcn_wavefrontsize not declared). They now sit under __HIPCC__, mirroring the __CUDACC__ guard on the CUDA branch. The HIP test build now compiles the four caspar_*_cuda_test.cc files and both generated solver.cc files with the host compiler, as the CUDA build does, so the defect cannot come back unnoticed.
  • c665e5cc Fix host compile of generated solver.cc on Windows. Host-compiling those files on Windows exposed two portability gaps in shared code: sort_indices.h used the glibc typedef uint (now unsigned int, and <cstddef> in place of <sys/types.h>), and the solver.cc template used printf, std::to_string and std::swap without including <cstdio>, <string> and <utility>. The two checked-in generated solver.cc files are regenerated.

The second commit touches code the CUDA build compiles. With nvcc 12.8 the six host objects are byte-identical before and after, and all 199 kernel code sections are byte-identical.

Validation at c665e5cc

  • AMD Instinct MI250X (gfx90a, Linux): the four Caspar GPU tests pass (6, 24, 302 and 140 assertions; the tridiagonal test 5 of 5 runs); ctest 209/212, the three failures being missing host tools (rustfmt, a docs dependency).
  • AMD Radeon PRO W7800 (gfx1100, Linux): the four Caspar GPU tests pass, 10 runs each; ctest 208/212, the same three plus symengine_symforce_linter_test, where make lint run by hand against the same build passes.
  • AMD Radeon 8060S (gfx1151, Windows): the four Caspar GPU tests build and pass, each run once. At aa31e636 this build failed; c665e5cc is the fix.
  • CUDA 12.8 (no NVIDIA GPU): 78 nvcc compiles, 0 warnings, the four test binaries link.

Known and not addressed here: host-compiling the generated solver.cc files under HIP prints -Wunused-result / -Wunused-value warnings for ignored hipError_t return values (the same ignored returns that warned before when those files were compiled as HIP).

Upstream reply

Two more small commits. Building another project's Caspar kernels against this branch showed that a host .cc file that includes a generated solver.h did not compile in a HIP build: the HIP side of cuda_to_hip.h still pulled in cooperative groups and hipCUB unconditionally, which g++ can't parse. They are now included only when compiling HIP device code, the same way the CUDA side is guarded by __CUDACC__. To keep that covered, the HIP build of the tests now compiles the caspar_*_cuda_test.cc files and the generated solver.cc files with the host compiler, as the CUDA build does.

Doing that on Windows turned up two portability gaps that aren't specific to HIP: sort_indices.h used uint, which only glibc defines (now unsigned int), and the generated solver.cc used printf, std::to_string and std::swap without including <cstdio>, <string> and <utility>. The template and the two checked-in solver.cc files are updated. With CUDA 12.8 the object code is unchanged by this: the host objects are byte-identical and so is every kernel's code.

Tested again on an AMD Instinct MI250X and an AMD Radeon PRO W7800 on Linux (the Caspar GPU tests, the codegen tests and the rest of the suite, apart from the same three tests that need tools those machines lack) and an AMD Radeon 8060S on Windows (the Caspar GPU tests).

Notes for the approver

The [ROCm] prefix on c665e5cc is there because the commit gate requires it; the change itself is a shared-code portability fix. The cause of the PartiallyContainedTracks NaN seen in colmap is in colmap's vendored f32 kernels and is not part of this round.

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
```
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 <sys/types.h>, and solver.cc called std::to_string without including <string>. Compiled as HIP they had built, because the HIP headers supply both.

sort_indices.h now spells the type unsigned int and includes <cstddef> for size_t. The solver.cc template includes <string>, and also <cstdio> for printf and <utility> 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 <sys/types.h> (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
```
jeffdaily added a commit to AMD-Ecosystem/moat that referenced this pull request Nov 18, 2026
@jeffdaily

Copy link
Copy Markdown
Collaborator Author

To approve this fix round, leave a comment containing this line by itself:

/moat approve

To send it back to the porter instead:

/moat changes-requested

Approving covers the commits on this branch and the section under '## Upstream reply' in the body, which is posted verbatim as a comment on the upstream pull request after the merge, behind a standing line disclosing that it was drafted by an AI assistant. utils/upstream.py --merge-fix --apply then fast-forwards the open upstream PR's branch to exactly the approved tip. Anything pushed afterwards, or any edit to the body, voids the approval and needs a fresh one.

@jeffdaily

Copy link
Copy Markdown
Collaborator Author

/moat approve

@jeffdaily
jeffdaily merged commit c665e5c into moat-port Nov 18, 2026
@jeffdaily
jeffdaily deleted the moat-fix-465 branch November 18, 2026 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant