ROCm / llvm-project

This is the AMD-maintained fork of the LLVM git repository. This repository accepts pull requests and issues related to AMD fork-specific topics (amd/*). For all other issues/PRs, please submit upstream at https://github.com/llvm/llvm-project.
Other
92 stars 47 forks source link

Linking static library with device functions depends on argument order #77

Open Snektron opened 1 year ago

Snektron commented 1 year ago

After creating a static library containing device functions using -fgpu-rdc and ar, the library is linked with a command like:

hipcc libdevicelibrary.a main.cpp -fgpu-rdc -o program

This works fine, however, when the argument order is reversed and main.cpp is passed before libdevicelibrary.a, the compiler tries to interpret the files from libdevicelibrary.a as source file:

$ hipcc main.cpp libdevicelibrary.a -fgpu-rdc -o program
/tmp/library.o:1:1: error: expected unqualified-id
<U+007F>ELF<U+0002><U+0001><U+0001><U+0000>.... many more lines

This is caused by that placing main.cpp on the command line causes hipcc to emit -x hip before it, which causes clang++ to interpret library.o as a .hip source file:

$ HIPCC_VERBOSE=1 hipcc main.cpp libdevicelibrary.a -fgpu-rdc -o program
hipcc-cmd: /opt/rocm/llvm/bin/clang++ [...extra options omitted for brevity] -x hip main.hip """/tmp/library.o""" -std=c++17 -fgpu-rdc -o "program" 
Snektron commented 1 year ago

It seems like the issue can be resolved by "resetting" the language before the object file is passed by using -x none. Manually invoking

/opt/rocm/llvm/bin/clang++ [...extra options omitted for brevity] -x hip main.hip -x none """/tmp/library.o""" -std=c++17 -fgpu-rdc -o "program" 

seems to work fine.

kzhuravl commented 9 months ago

cc @yxsamliu

yxsamliu commented 1 week ago

That is a hipcc issue. hipcc by default adds -x hip before .cpp files. -x hip applies to all input files after it.

hipcc is supposed to be a thin wrapper for clang and not intended for complicated command arguments handling. It is recommended to use clang++ instead of hipcc. Also it is recommended to use .hip as extension of HIP program instead of .cpp. If .cpp has to be used and .a file has to be after .cpp file, use '-x hip' before .cpp and '-x none' before .a file.