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

[llvm][CMake] Introduce LLVM_RUNTIME_<project>_BUILD in CMake #88934

Closed
wants to merge 4 commits into from

Conversation

mjklemm
Copy link
Contributor

@mjklemm mjklemm commented Apr 16, 2024

This PR adds CMake code to set LLVM_RUNTIME_<project>_BUILD similar to LLVM_TOOL_<project>_BUILD. This makes it easier to detect if a particular runtime build has been requested.

@mjklemm mjklemm self-assigned this Apr 16, 2024
@mjklemm mjklemm changed the title Introduce LLVM_RUNTIME_<project>_BUILD in CMake [llvm][CMake] Introduce LLVM_RUNTIME_<project>_BUILD in CMake Apr 16, 2024
@mjklemm
Copy link
Contributor Author

mjklemm commented Apr 22, 2024

@petrhosek @delcypher Would have a bit of time to have a look at this PR? I would greatly appreciate your review.

@jhuber6
Copy link
Contributor

jhuber6 commented Apr 22, 2024

So, is this adding a variable that's used to detect if a project has "any" runtime enabled for it? I know for detecting libc stuff it's kind of a mess because we need to check LLVM_ENABLE_PROJECTS and RUNTIMES_<triple>_LLVM_ENABLE_PROJECTS.

@petrhosek
Copy link
Member

In LLVM, we have runtimes and bootstrapping builds.

The runtimes build uses https://github.com/llvm/llvm-project/tree/main/runtimes to build runtimes specified by LLVM_ENABLE_RUNTIMES using whatever host compiler you specify by CMAKE_{C,CXX}_COMPILER.

The bootstrapping build uses https://github.com/llvm/llvm-project/tree/main/llvm/runtimes to first build a toolchain with projects specified by LLVM_ENABLE_PROJECTS, and then uses that just built toolchain to build selected runtimes using the runtimes build.

This patch only sets LLVM_RUNTIME_<project>_BUILD in the bootstrapping build which is not the right place, it should be set in the runtimes build (that is https://github.com/llvm/llvm-project/blob/main/runtimes/CMakeLists.txt) to make sure that the variable is set whether we're using runtimes or bootstrapping build.

@mjklemm
Copy link
Contributor Author

mjklemm commented Apr 22, 2024

So, is this adding a variable that's used to detect if a project has "any" runtime enabled for it? I know for detecting libc stuff it's kind of a mess because we need to check LLVM_ENABLE_PROJECTS and RUNTIMES_<triple>_LLVM_ENABLE_PROJECTS.

I do hope that this will simply this. For the OpenMP tests, one would also have to explicitly check the presence of "openmp" in LLVM_ENABLE_PROJECTS. After this, this will hopefully be not needed anymore. If you could have a look and see if this PR helps there, I'd appreciate that.

Thanks to @petrhosek for the suggestion!
@mjklemm
Copy link
Contributor Author

mjklemm commented Apr 22, 2024

Thanks for taking the time to review and the explanation about the mechanics!

This patch only sets LLVM_RUNTIME_<project>_BUILD in the bootstrapping build which is not the right place, it should be set in the runtimes build (that is https://github.com/llvm/llvm-project/blob/main/runtimes/CMakeLists.txt) to make sure that the variable is set whether we're using runtimes or bootstrapping build.

I have moved the code to runtimes/CMakeLists.txt now. Does that look better now?

# Set LLVM_RUNTIME_<project>_BUILD variables if a sub-project is enabled to be built as
# a runtime. As with LLVM_TOOL_<project>_BUILD, the LLVM_RUNTIME_<project>_BUILD variables
# should be not directly used from the CMake configuration command line.
foreach(proj ${LLVM_SUPPORTED_RUNTIMES})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to check for each LLVM_RUNTIME_TARGETS if RUNTIMES_<triple>_LLVM_ENABLE_RUNTIMES is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I'm not qualified to answer it, though.

@petrhosek
Copy link
Member

I have moved the code to runtimes/CMakeLists.txt now. Does that look better now?

It should be moved to runtimes/CMakeLists.txt, not llvm/runtimes/CMakeLists.txt.

@mjklemm
Copy link
Contributor Author

mjklemm commented Apr 23, 2024

It should be moved to runtimes/CMakeLists.txt, not llvm/runtimes/CMakeLists.txt.

Hm. When I do this, it does not seem that it is picked up and when adding a debugging message, CMake does not seem to go through runtimes/CMakeList.txt, but llvm/runtimes/CMakeLists.txt for a configuration like this:

cmake -DLLVM_ENABLE_PROJECTS="clang;flang;lld" -DLLVM_ENABLE_RUNTIMES="openmp" [...] ../llvm

So, if I correctly understand your explanation above, then I'm doing a bootstrapping build with the above command line. I have to use a command line like this to trigger the code in runtimes/CMakefile.txt:

cmake -DLLVM_ENABLE_RUNTIMES="openmp" [...] ../runtimes

So, a bootstrapping build simply configures the request to build a runtime via llvm/runtimes/CMakeLists.txt and then, when actually building, invokes CMake with a runtime build (using a command line like the 2nd one and C/C++ compiler set to Clang), which in turn triggers runtime/CMakeLists.txt? Is this thinking remotely correct?

Because then it would seem like code would need to be duplicated (or be moved to a CMake file that can be included in both llvm/runtimes and runtimes). Because only that would allow a bootstrapping build to detect that a particular has been requested to, say, enable a bunch of LIT tests. Or, alternatively, even have LLVM_RUNTIME_<project>_BUILD and LLVM_BOOTSTRAP_<project>_BUILD to distinguish the two variants of these builds to enable other parts of the CMake files to detect either one.

…BUILD

With this a build can now see if a runtime build has been requested as
part of the bootstrap process.  In such a build,
LLVM_BOOTSTRAP_RUNTIME_..._BUILD will be set to tell the LLVM build
system that a particular runtime as been configured.  During the runtime
build itself or when building as a runtime build, LLVM_RUNTIME_..._BUILD
is set to show that a build of a runtime component has been requested.
@mjklemm mjklemm requested a review from a team as a code owner April 25, 2024 06:27
@llvmbot llvmbot added the cmake Build system in general and CMake in particular label Apr 25, 2024
@mjklemm
Copy link
Contributor Author

mjklemm commented Apr 25, 2024

Or, alternatively, even have LLVM_RUNTIME_<project>_BUILD and LLVM_BOOTSTRAP_<project>_BUILD to distinguish the two variants of these builds to enable other parts of the CMake files to detect either one.

I have implemented thing that way now. @petrhosek Please have a look and let me know that's an acceptable compromise to be as flexible as possible in detecting how builds are being configured.

@petrhosek
Copy link
Member

It should be moved to runtimes/CMakeLists.txt, not llvm/runtimes/CMakeLists.txt.

Hm. When I do this, it does not seem that it is picked up and when adding a debugging message, CMake does not seem to go through runtimes/CMakeList.txt, but llvm/runtimes/CMakeLists.txt for a configuration like this:

cmake -DLLVM_ENABLE_PROJECTS="clang;flang;lld" -DLLVM_ENABLE_RUNTIMES="openmp" [...] ../llvm

So, if I correctly understand your explanation above, then I'm doing a bootstrapping build with the above command line. I have to use a command line like this to trigger the code in runtimes/CMakefile.txt:

cmake -DLLVM_ENABLE_RUNTIMES="openmp" [...] ../runtimes

So, a bootstrapping build simply configures the request to build a runtime via llvm/runtimes/CMakeLists.txt and then, when actually building, invokes CMake with a runtime build (using a command line like the 2nd one and C/C++ compiler set to Clang), which in turn triggers runtime/CMakeLists.txt? Is this thinking remotely correct?

Yes, your understanding is correct.

Because then it would seem like code would need to be duplicated (or be moved to a CMake file that can be included in both llvm/runtimes and runtimes). Because only that would allow a bootstrapping build to detect that a particular has been requested to, say, enable a bunch of LIT tests. Or, alternatively, even have LLVM_RUNTIME_<project>_BUILD and LLVM_BOOTSTRAP_<project>_BUILD to distinguish the two variants of these builds to enable other parts of the CMake files to detect either one.

No, I don't think this is desirable. The reason is that the bootstrapping build also supports more advanced scenarios such as the following:

cmake -DLLVM_ENABLE_PROJECTS="clang;flang;lld" -DLLVM_RUNTIME_TARGETS="x86_64-linux-gnu;aarch64-linux-gnu" -DRUNTIMES_x86_64-linux-gnu_LLVM_ENABLE_RUNTIMES="libc" -DRUNTIMES_aarch64-linux-gnu_LLVM_ENABLE_RUNTIMES="openmp" [...] ../llvm

This would invoke the runtimes build once for requested target, so simply setting LLVM_BOOTSTRAP_openmp_BUILD=ON may be misleading; if your host platform is x86_64-linux-gnu, you still wouldn't want to run tests because although openmp was built, it was built for a different target.

Can you explain what the motivation behind introducing these variables is (and also ideally include it in the description of the PR)? I can try to think a better way we could address your needs but I need to understand the use case.

@mjklemm
Copy link
Contributor Author

mjklemm commented Apr 25, 2024

Can you explain what the motivation behind introducing these variables is (and also ideally include it in the description of the PR)? I can try to think a better way we could address your needs but I need to understand the use case.

Sure, one use case is to determine if, say, the OpenMP runtime was built and enable the tests for it. Right now flang/test/lit.site.cfg.py.in has this:

config.have_openmp_rtl = ("@LLVM_TOOL_OPENMP_BUILD@" == "TRUE")

However, there's no good way to detect and enable that test configuration if OpenMP is built via LLVM_ENABLE_RUNTIMES. There's a clumsy way doing it by looking at LLVM_ENABLE_RUNTIMES using Python string search, but does not seem like a good solution either, So, I was hoping for something along the lines of LLVM_TOOL_<project>_BUILD.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this approach.

It seems to me that you should instead have a parameter you pass to lit with --param and then have it set from the CI cache you use to configure your build. That would prevent having to infer whether a project is being built or not.

The reason why I'm lukewarm about this change is that it adds yet another procedural aspect to our CMake builds, and we've been working hard to get rid of that. We want to move towards more declarative (and more modern) CMake constructs where we define targets and avoid global state and logic, and this basically introduces another way to add such logic. So while I don't 100% understand your use case and the constraints you have to solve it, this specific approach seems like the wrong one to me at a high level.

@mjklemm
Copy link
Contributor Author

mjklemm commented May 9, 2024

Closing this PR to research for a better way.

@mjklemm mjklemm closed this May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants