Using clangd to retrieve backend optimization remarks


I’m fairly new to this community so I hope I’m not asking a very trivial question.
Anyways, I’m looking into the possibility of returning optimization remarks like the option -Rpass=loop-vectorize would do. By looking at clangd’s source code I realized that clangd only preprocesses the project but does not really invoke the backend.
Thus, I would like to implement an action that enables the output of optimization remarks. Could I maybe get some guidance on to what I need to do to achieve this? I saw that I most likely need to subclass Action but I’m not quite sure what subclass I should have a closer look at.

Thanks a lot for any kind of help.


Hi Laurin,

I'm not really familiar with clang/LLVM backends, but here's a guess.

The simplest idea is to change the existing code that parses the file and
generates diagnostics.
At least to prototype, I think you might want to change
clangd::ClangdFrontendAction (in ClangdUnit.cpp) to subclass
clang::EmitCodeGenOnlyAction (or one of the other CodeGenActions) instead
of clang::SyntaxOnlyAction. And then add the relevant flags.

However I'm not sure that's going to work well:
- with the backend enabled, everything is going to get a lot slower
- we have optimizations (like not parsing function bodies in the preamble)
that probably aren't compatible with codegen
- doing codegen is probably going to force clang to deserialize all of the
preamble, which is also slow
- obviously it means linking in the backend, which is quite a big thing
for a release build if this is a niche feature

For performance, I'm not sure how feasible it is to run codegen for code
directly referenced by the main file only (again, not a backend person!),
this would be the analogue to how we keep frontend diagnostics fast.

We have a vague wish to support some more-expensive diagnostics (template
expansions, clang-tidy analysis) that can't be computed every loop, but
haven't come up with a good design yet.

Cheers, Sam

Hi Sam,

Thank you very much for your help. I will prototype this and see where it goes :slight_smile:


+backend guys

Hi Laurin,

Would you mind sharing more details about your project? What is your goal here?



Hi Jan,

Essentially, I want to create a vs code extension that annotates the code to display various optimization remarks. So for example, I would like to highlight a loop which clang wasn’t able to vectorize. Thus, I’m now trying to make clangd invoke the backend as well.

Could you maybe share some hints that I can look into to make this work? Thank you very much in any case :slight_smile:


Why do you want to use clangd for this rather than clang itself? You will need to effectively perform full compiles for this which is clang’s job. (The remarks are emitted by the various optimization passes *as* LLVM runs them.)


Clangd is needed so that vs code will eventually display the remarks in the UI of the IDE. Do you have another approach in order to achieve this?