Skip to content

fix(hip): stop HIP_ADD_LIBRARY from propagating -x hip to consumers - #15126

Open
david-salinas wants to merge 1 commit into
developfrom
users/dsalinas/fix-hip-add-library-device-propagation
Open

david-salinas wants to merge 1 commit into
developfrom
users/dsalinas/fix-hip-add-library-device-propagation

Conversation

@david-salinas

@david-salinas david-salinas commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

HIP_ADD_LIBRARY linked hip::device as PUBLIC, which propagates -x hip and --offload-arch to every target that links the library. This forces plain C++ sources in consumer targets to be compiled as HIP device code, changing semantics (e.g. clang's HIP-mode wrapper disables C99 complex functions, altering std::abs results).

Link hip::device PRIVATE so only the library's own sources get -x hip, and hip::host PUBLIC so consumers inherit the HIP runtime (headers + libamdhip64) without the device compilation flags.

Ref: https://amd-hub.atlassian.net/browse/ROCM-33939

Motivation

PR #5708 replaced the legacy FindHIP.cmake with a compatibility shim. The shim's HIP_ADD_LIBRARY macro linked hip::device as PUBLIC so consumers would inherit the HIP runtime. However, hip::device carries -x hip and --offload-arch on its interface, which forces all downstream consumer sources to be compiled as HIP device code. The old FindHIP.cmake never propagated these flags to consumers. This causes subtle correctness issues — for example, in HIP mode clang's wrapper disables C99 complex functions, so std::abs of a complex value uses libstdc++'s generic formula instead of cabs, producing results that differ in the last bit.

Technical Details

The hip::device and hip::host imported targets differ in one key way:

┌─────────────┬───────────┬────────┬─────────┐
│ Target │ -x hip │ --offload-arch │ HIP headers │ libamdhip64 │
├─────────────┼───────────┼────────┼─────────┼
│ hip::host │ No │ No │ Yes │ Yes │
├─────────────┼───────────┼────────┼─────────┼
│ hip::device │ Yes │ Yes │ Yes │ Yes │
└─────────────┴───────────┴────────┴─────────┴

The fix changes HIP_ADD_LIBRARY in projects/hip/cmake/FindHIP.cmake from:

target_link_libraries(${hip_target} PUBLIC hip::device)

to:

target_link_libraries(${hip_target} PRIVATE hip::device PUBLIC hip::host)

  • PRIVATE hip::device: the library's own sources get -x hip and --offload-arch for device code compilation.
  • PUBLIC hip::host: consumers inherit the HIP runtime (headers + libamdhip64) without the device compilation flags.

HIP_ADD_EXECUTABLE is unchanged — it already uses PRIVATE hip::device, which is correct since nothing consumes an executable's link interface.

Issue Tracking

JIRA ID: ROCM-33939

Test Plan

  • Created a minimal CMake project with a hip_add_library(mylib ...) target and a plain C++ consumer linked via target_link_libraries(consumer PRIVATE mylib).
  • Verified with VERBOSE=1 build output that the consumer's compile command does not contain -x hip or --offload-arch.
  • Verified the library itself still compiles and links correctly.

Test Result

  • CMake configure and build succeed.
  • Consumer target compiles as plain C++ — no -x hip or --offload-arch in its compile command.
  • No _hip_cuid symbols in the consumer object file.

Submission Checklist

HIP_ADD_LIBRARY linked hip::device as PUBLIC, which propagates -x hip
and --offload-arch to every target that links the library. This forces
plain C++ sources in consumer targets to be compiled as HIP device code,
changing semantics (e.g. clang's HIP-mode <complex> wrapper disables C99
complex functions, altering std::abs results).

Link hip::device PRIVATE so only the library's own sources get -x hip,
and hip::host PUBLIC so consumers inherit the HIP runtime (headers +
libamdhip64) without the device compilation flags.

Ref: https://amd-hub.atlassian.net/browse/ROCM-33939

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@david-salinas
david-salinas requested review from a team as code owners November 5, 2026 17:34
@therock-pr-bot

therock-pr-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

✅ All Policy Checks Passed

Check Status Details
📝 PR Description ✅ Pass —
⛔ Forbidden Files ✅ Pass —
🧪 Unit Test ✅ Pass PR does not contain code files — Unit Test auto-passed
🚫 Draft PR 🔜 To Be Enabled —
🚩 Feature Flag 🔜 To Be Enabled —
📊 Code Coverage 🔜 To Be Enabled —

🎉 All policy checks passed!

📖 Need help? See the Policy FAQ for details on every check and how to fix failures.

🙋 Wish to Override Policy?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants