clangd / clangd

clangd language server
https://clangd.llvm.org
Apache License 2.0
1.4k stars 60 forks source link

Detect hipcc in compile_commands.json #1300

Open ColonelPhantom opened 1 year ago

ColonelPhantom commented 1 year ago

When programming in AMD HIP, clangd by default does not recognize HIP-specific (or CUDA-specific) constructs (such as kernel launch with <<<>>>). I can fix this by adding -x hip to the file in compile_commands.json, but this is rather inconvenient since I'd rather be able to use the file generated by my build system directly.

HIP source files use hipcc as their compiler by default. Would it be possible to detect that the compiler is called hipcc and then automatically use HIP as input langauge?

I have no idea what clangd uses to distinguish between input languages (e.g. C, C++, CUDA) currently, but I don't think detecting by file extension would work, as HIP source files tend to just be called .cpp. AMD has not defined another normal extension such as .hip.

i-ky commented 1 year ago

HIP source files use hipcc as their compiler by default.

What does this mean exactly? How does build system know to use hipcc? How does build system distinguish HIP source files from the rest of .cpp files?

sam-mccall commented 1 year ago

Generally, clangd aims for compatibility with the C-family languages clang supports, and to parse args the same way.

If mainline clang supports hip files, would it make sense to have clang parse as hip by default when invoked as hipcc, as it parses as c++ by default when invoked as clang++ etc?

ColonelPhantom commented 1 year ago

What does this mean exactly? How does build system know to use hipcc? How does build system distinguish HIP source files from the rest of .cpp files?

Well, it all depends on how you set up your build system of course :) I'm fairly new to HIP, so I used what I found in some AMD sample based on CMake, which sets the C++ compiler to hipcc and sets some other flags.

Generally, clangd aims for compatibility with the C-family languages clang supports, and to parse args the same way.

If mainline clang supports hip files,

Clang does indeed support hip files, otherwise adding -x hip wouldn't do anything except throw an error! (When I tried to actually use it last time I did get a linker error but that's beside the point for clangd).

would it make sense to have clang parse as hip by default when invoked as hipcc, as it parses as c++ by default when invoked as clang++ etc?

Interesting, does clangd just invoke clang with the arguments (including name) from compile_commands.json? Because if so, adding hipcc as a supported name sounds like it could work.

HighCommander4 commented 1 year ago

I can fix this by adding -x hip to the file in compile_commands.json, but this is rather inconvenient since I'd rather be able to use the file generated by my build system directly.

A workaround that may be more palatable until a proper fix is in place is to use a clangd config file to add the -x hip (see https://clangd.llvm.org/config.html#add).

Interesting, does clangd just invoke clang with the arguments (including name) from compile_commands.json?

It doesn't invoke clang as a subprocess, but it links in the same libraries that clang does (including the code for interpreting the compiler command line) and performs substantially the same processing as the front-end part of clang.

colawithsauce commented 8 months ago

@HighCommander4

A workaround that may be more palatable until a proper fix is in place is to use a clangd config file to add the -x hip (see https://clangd.llvm.org/config.html#add).

Is add -x hip on .clangd file works for you? I added it, but got an error message: Language not recognized: ' hip'.

colawithsauce commented 8 months ago

Oh, I know! Its '-xhip', NOT '-x hip'