This is an archive of the discontinued LLVM Phabricator instance.

[NFC][libc] Disable benchmarks when the LLVM benchmark target is not available
ClosedPublic

Authored by gchatelet on Mar 25 2022, 6:54 AM.

Diff Detail

Event Timeline

gchatelet created this revision.Mar 25 2022, 6:54 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMar 25 2022, 6:54 AM
gchatelet requested review of this revision.Mar 25 2022, 6:54 AM

I tried this patch locally and got the error message like https://discourse.llvm.org/t/error-when-building-llvm-libc-with-gcc/4166 instead of The target "benchmark" does not exist..
It means this patch can solve the issue.

But I am still confused: why the conditional statement if(LLVM_INCLUDE_BENCHMARKS) can solve this issue?
Accoding to the following document and configuration, the LLVM_INCLUDE_BENCHMARKS is default to ON.
The LLVM_INCLUDE_BENCHMARKS is ON, so the if(LLVM_INCLUDE_BENCHMARKS) seems not neccesary.
I am a newbie in llvm project and may miss something. Thanks for your help.

doc: llvm/docs/CMake.rst

LLVM_INCLUDE_BENCHMARKS:BOOL
Generate build targets for the LLVM benchmarks. Defaults to ON.

config: llvm/CMakeLists.txt

option(LLVM_INCLUDE_BENCHMARKS "Generate benchmark targets. If OFF, benchmarks can't be built." ON)
gchatelet updated this revision to Diff 418555.Mar 28 2022, 5:27 AM
  • Guard libc benchmarks behind a flag
  • Use ExternalProject as the benchmark target is not always available

Thx for challenging my patch @lgxbslgx.

I intended to use LIBC_INCLUDE_BENCHMARKS and not LLVM_INCLUDE_BENCHMARKS.
The fact that it "works" was puzzling to me so I ran a bunch of experiments to understand what's going on.

When libc is part of the LLVM_ENABLE_RUNTIMES list, the CMake configuration is created in such a way that it does not inherit from llvm/CMakeLists.txt. So LLVM_INCLUDE_BENCHMARKS is not defined. This explains why my patch "works".
Similarly, the benchmark target was not available when libc is instantiated as part of LLVM_ENABLE_RUNTIMES, but it is when it is part of LLVM_ENABLE_PROJECTS which explains the bug in https://github.com/llvm/llvm-project/issues/53686.

Because the benchmark target is not necessarily defined when libc is instantiated (it depends whether libc is part of LLVM_ENABLE_RUNTIMES or LLVM_ENABLE_PROJECTS) I have to make sure it is defined in libc/benchmarks/CMakeLists.txt, at this point it is not possible to use the add_subdirectory directive as it may result in duplicate target names. That's why I reverted to the original implementation using ExternalProject.

Additionally, it was not wise to refer to Google Benchmark by the benchmark identifier as CMake is "smart" and if it does not find a target it infers that it is a system wide library and appends -lbenchmark to the linker which also happens to work depending on your local environment. To fix this I now use the benchmark::benchmark alias that cannot be conflated with precompiled libraries.

I believe this patch now solves all the use cases. Let me know if it looks good to you and I'll submit it.

lgxbslgx accepted this revision.Mar 28 2022, 10:47 AM

Thanks for the detailed analysis. I tried the new patch locally and the libc was built successfully.
But I am not an official reviewer(committer). Maybe it needs other developers to review again.

This revision is now accepted and ready to land.Mar 28 2022, 10:47 AM
gchatelet updated this revision to Diff 418820.Mar 29 2022, 1:46 AM

rebase with all the diffs

This revision was landed with ongoing or failed builds.Mar 29 2022, 1:46 AM
This revision was automatically updated to reflect the committed changes.