Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Snektron opened this issue Oct 19, 2022 · 6 comments
Open
Labels

Comments

@Snektron
Copy link

Snektron commented Oct 19, 2022

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
Copy link
Author

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
Copy link
Collaborator

kzhuravl commented Aug 2, 2023

cc @yxsamliu

@dgaliffiAMD dgaliffiAMD added the hipcc Related to HIPCC label May 4, 2024
@lamb-j lamb-j transferred this issue from ROCm/HIPCC May 6, 2024
@yxsamliu
Copy link

yxsamliu commented May 6, 2024

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.

@ppanchad-amd
Copy link

@Snektron Internal ticket is created to investigate this issue. Thanks!

@schung-amd
Copy link

@Snektron Does the comment by @yxsamliu address your issue? It appears you've found a workaround, and based on the comment by @yxsamliu this is working as intended; i.e. hipcc is not intended to have this functionality.

@Snektron
Copy link
Author

Snektron commented Aug 4, 2024

Typically this is not the way that libraries are placed on the command line, as they are usually placed last. The workaround works, yes, but in my opinion it would be nice if there was a proper solution. If hipcc does it's own language detection, would it not be relatively straight forward to detect the .a type and automatically pass -x none? I'm fairly sure that nvcc let's me pass the device library as last argument.

Note c that this came up during development of the static_device_library ROCm sample, see here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants