Skip to content

[LLVM] Automatically strip -fno-lifetime-dse from compile_commands.json #124623

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

Closed
wants to merge 2 commits into from

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Jan 27, 2025

Summary:
This flag is always passed by LLVM to bypass some LTO related bug with
GCC. The problem is that this is not supported by clang and shows up in
the compile_commands.json file. That means it will always show at least
one error complaining about this file. Since I already have a script to
merge the runtimes stuff, just strip this out there for convenience.

There was a discussion a long time ago about not adding this as a no-op
flag to clang, so this is the next best thing.

Copy link

github-actions bot commented Jan 27, 2025

✅ With the latest revision this PR passed the Python code formatter.

@jplehr
Copy link
Contributor

jplehr commented Jan 28, 2025

LGTM, though I don't feel confident enough to accept.

@Meinersbur
Copy link
Member

If I understand correctly, you are compiling LLVM with gcc, so the compile_commands.json will have entries of the form

"command": "/usr/bin/g++ ... -fno-lifetime-dse
"

Seems legitimate to me. I would want the file to accuratelty represent the command line the files were compiled with.

…json

Summary:
This flag is always passed by LLVM to bypass some LTO related bug with
GCC. The problem is that this is not supported by clang and shows up in
the compile_commands.json file. That means it will always show at least
one error complaining about this file. Since I already have a script to
merge the runtimes stuff, just strip this out there for convenience.

There was a discussion a long time ago about not adding this as a no-op
flag to clang, so this is the next best thing.
@petrhosek
Copy link
Member

petrhosek commented Jan 28, 2025

I don't understand the issue, the command field for each file in the compilation database should include a full compiler invocation, so if you merge compilation database from both the LLVM and runtime builds, so you should end up with something like:

[
  ...
  {
    "command": "/usr/bin/g++ ... -fno-lifetime-dse",
    ...
  },
  ...
  {
    "command": "/path/to/build/bin/clang ...",
    ...
  },
  ...
]

It sounds like you also end up with /path/to/build/bin/clang ... -fno-lifetime-dse entries in the database. Where do those come from?

@jhuber6
Copy link
Contributor Author

jhuber6 commented Jan 28, 2025

I don't understand the issue, the command field for each file in the compilation database should include a full compiler invocation, so if you merge compilation database from both the LLVM and runtime builds, so you should end up with something like:

[
  ...
  {
    "command": "/usr/bin/g++ ... -fno-lifetime-dse",
    ...
  },
  ...
  {
    "command": "/path/to/build/bin/clang ...",
    ...
  },
  ...
]

It sounds like you also end up with /path/to/build/bin/clang ... -fno-lifetime-dse entries in the database. Where do those come from?

They're added by the LLVM build itself, you cannot turn them off. It was done in the past as a fix for some GNU LTO bug.

append("-fno-lifetime-dse" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
.

I suppose in my case I could just force building with system clang all the time instead, but it's annoying in general.

@petrhosek
Copy link
Member

I see, so if I understand correctly this has nothing to do with compilation database merging. Rather the issue is when we build with GCC, the LLVM build would set -fno-lifetime-dse but if you then try to use the generated compilation databse with a Clang-based tool like clangd, you'll get an error because the flag is unsupported. Is that correct? If so I don't think this is the right solution, rather I think we should accept and ignore the flag in the Clang driver. Do you have a link to the past discussion?

@MaskRay
Copy link
Member

MaskRay commented Jan 29, 2025

I believe that the past discussion is that we don't take as a ground to add an ignored option to clangDriver.

clangDriver would become really messy if we add such GCC optimization specific options to Clang.
I am fine with adding this to convenience scripts, not user-facing products.

Perhaps language servers could strict such -f options. Most don't change indexing or completion behaviors anyway.

@Meinersbur
Copy link
Member

Just encountered the problem with CMAKE_CXX_INCLUDE_WHAT_YOU_USE=include-what-you-use. include-what-you-use use clang internally, so when using gcc as main compiler, the LLVM build system will add -fno-lifetime-dse which is rejected by clang/iwyu. This is independent of compile_commands.json.

@jhuber6
Copy link
Contributor Author

jhuber6 commented Jan 29, 2025

Was there ever an issue to track the original bug that this flag works around?

@nikic
Copy link
Contributor

nikic commented Jan 29, 2025

Was there ever an issue to track the original bug that this flag works around?

The issue is #24952.

Fixing it probably needs something along the lines of #105714, but for operator delete instead of operator new.

@jhuber6
Copy link
Contributor Author

jhuber6 commented Feb 5, 2025

Ping.

@jhuber6
Copy link
Contributor Author

jhuber6 commented Feb 11, 2025

Ping

1 similar comment
@jhuber6
Copy link
Contributor Author

jhuber6 commented Feb 25, 2025

Ping

@MaskRay
Copy link
Member

MaskRay commented Feb 26, 2025

@thesamesam Is it feasible to add -fno-lifetime-dse (currently for all GCC builds
47f5c54) only when GCC-specific -flto is used?

I don't think any developer uses GCC LTO for compile_commands.json. So preventing -fno-lifetime-dse for non-LTO gcc builds suffices.

Then we don't need a change to this Python script.

@jhuber6
Copy link
Contributor Author

jhuber6 commented Feb 26, 2025

@thesamesam Is it feasible to add -fno-lifetime-dse (currently for all GCC builds 47f5c54) only when GCC-specific -flto is used?

I don't think any developer uses GCC LTO for compile_commands.json. So preventing -fno-lifetime-dse for non-LTO gcc builds suffices.

Then we don't need a change to this Python script.

I would heavily prefer that solution.

@thesamesam
Copy link
Member

It's not ideal (as the issue could arise without LTO) but I'm fine with it as a compromise. It's also an easy thing for us to investigate if an issue pops up building with GCC again.

@jhuber6 jhuber6 closed this Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants