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

[HLSL] clangd support #58026

Open
llvm-beanz opened this issue Sep 27, 2022 · 3 comments
Open

[HLSL] clangd support #58026

llvm-beanz opened this issue Sep 27, 2022 · 3 comments
Labels
HLSL HLSL Language Support

Comments

@llvm-beanz
Copy link
Collaborator

Getting HLSL to work with clangd would be awesome

@sam-mccall
Copy link
Collaborator

It seems to kinda-sorta work...

First step is to convince your editor to send the file to clangd. A hacky way is to set the filetype in the editor to C or something (this works in vim and VSCode at least). clangd itself doesn't pay any attention to what language the server said the file was, it uses clang's driver which looks at the file extension.
Once this is closer to "ready" we can add extra filetypes in vscode-clangd and other extensions so this is recognized automatically.

At this point I can hover on things (struct names etc), it knows what float3 is and I can go-to-definition and wind up in hlsl_basic_types.h so the basic headers seem to have been included. The cc1 invocation has -x hlsl (easiest way to see this is clangd --check=test.hlsl) and I can reproduce the HLSL-specific diagnostics from lit tests.

The examples I copy/paste from the internet (I don't know HLSL) show plenty of errors, things like TEXCOORD0 are unresolved - but I see the same running clang on the command line, so I guess it's just language features that are not finished?

There's an annoying "HLSL code generation is unsupported for target 'x86_64-grtev4-linux-gnu'" at the top of the file, which would be nice to suppress somehow if you want this to be useful without setting up compile_commands.json (pick a different default target, suppress the warning in -fsyntax-only mode, block that diagnostic by default in clangd...).

Ideas of how to maybe further improve things:

  • code completion: turning on/off particular types of snippets in SemaCodeComplete based on whether they're relevant in the current language mode might be helpful
  • code completion: clangd will complete symbols from your project based on an index, it doesn't restrict anything based on language, I guess you would want some barriers here, this would need some design
  • handling of any custom AST node types: supporting them in RecursiveASTVisitor is the very basic thing, resolving nodes to decls for go-to-definition and xrefs is handled by clangd/FindTarget.cpp, features like semantic highlighting, hover support etc should be easy to find

@llvm-beanz
Copy link
Collaborator Author

@sam-mccall Than you so much for the feedback! It is extremely well timed. I actually just started playing with this yesterday myself, and was pleasantly surprised how functional it was.

I encountered the same issue that you saw with the triple, and one other assert caused by a bug in some of the HLSL-specific code that I have a patch for locally.

There’s a lot of the language that we haven’t yet added to clang, so I expect support will be pretty limited for a while as we’re working on that. The one area that I was a little concerned about (but seems to be working fine), is that we use an ExternalSemaSource to inject AST nodes. We had to make some changes in Clang around the MultiplexSemaSource so that we could also support reading serialized ASTs (PCH). That is another part of clang that probably could use some design adjustment since the ownership of external sources is a bit wonky.

@llvm-beanz
Copy link
Collaborator Author

@sam-mccall I don't know if you have thoughts on this, but I put up a PR to enable HLSL in the clangd VSCode plugin: clangd/vscode-clangd#392

With clang-15 it doesn't work, but doesn't do anything awful. Just errors out early and moves on. For clang-16 it hits the issues you described. I'll probably try to spend some time getting this into better shape before the clang-16 release so that we can have some rudimentary support.

Personally, I'm loving the go to definition and basic hover support that I'm currently getting in vim. I cannot shower enough love on clangd. It is my favorite LLVM tool I haven't contributed to (yet :D ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HLSL HLSL Language Support
Projects
Status: No status
Development

No branches or pull requests

2 participants