-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-138451: Support custom LLVM installation path #138452
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
base: main
Are you sure you want to change the base?
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Tools/jit/_llvm.py
Outdated
| async def _find_tool(tool: str, *, echo: bool = False) -> str | None: | ||
| # Explicitly defined LLVM installation location | ||
| if (llvm_root := os.getenv("LLVM_ROOT")) is not None: | ||
| path = os.path.join(llvm_root, "bin", tool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are all LLVM installation folders guaranteed to have the tool located in the $LLVM_ROOT/bin directory? I think it's somewhat risky to assume that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually there is a LLVM_TOOLS_INSTALL_DIR in the CMakeLists to customize this: https://github.com/llvm/llvm-project/blob/74ec38fad0a1289f936e5388fa8bbe74653c55d9/llvm/CMakeLists.txt#L494
I should probably use that instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that seems better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is LLVM_TOOLS_INSTALL_DIR (or LLVM_ROOT for that matter) documented anywhere?
It seems like an env var used for building/installing LLVM itself, not necessarily something meant to be used after-the-fact by people trying to find LLVM. Or am I misunderstanding?
Tools/jit/_llvm.py
Outdated
| @_async_cache | ||
| async def _find_tool(tool: str, *, echo: bool = False) -> str | None: | ||
| # Explicitly defined LLVM installation location | ||
| if (llvm_tools_dir := os.getenv("LLVM_TOOLS_INSTALL_DIR")) is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we document this somewhere? If it gets a NEWS entry, then we probably have (or need) a list of environment variables that may need to be set when building.
I'd expect the list to be in the devguide, really, which is a different repo. But do we have one in the main repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also:
| if (llvm_tools_dir := os.getenv("LLVM_TOOLS_INSTALL_DIR")) is not None: | |
| if llvm_tools_dir := os.getenv("LLVM_TOOLS_INSTALL_DIR")): |
We shouldn't join to an empty value either (since that's interchangeable with "not set"). And possibly we should make sure it's an absolute path as well, though that may not matter so much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering the JIT is still experimental, I don't think we should document this in the CPython docs. I agree that it should be in the devguide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I did not know about the devguide before this review: Where should I add something? On first sight there was no section obvious to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it's somewhere on this page, but to be honest I didn't read the whole thing: https://github.com/python/devguide/blob/main/getting-started/setup-building.rst
Possibly we need a new section here anyway for building the JIT? @savannahostrowski @brandtbucher are there JIT-specific build docs somewhere to document a relevant environment variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have docs for building in https://github.com/python/cpython/blob/main/Tools/jit/README.md#building. We'd probably want to update this for now.
Add support for explicitly defined LLVM installation location. This is necessary as MSVC now comes with its own LLVM installation and activating MSVC via vcvars.bat will put LLVM tools on the `PATH` before the local ones.
Co-authored-by: Steve Dower <steve.dower@microsoft.com>
|
Rebased and added documentation to the mentioned README. Is there anything more I can do here to get it merged? |
Misc/NEWS.d/next/Build/2025-09-03-14-55-59.gh-issue-138451.-Qzh2S.rst
Outdated
Show resolved
Hide resolved
savannahostrowski
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we should make this a configure flag instead, like we did with #138498. That way, you can pass the flag into configure once (e.g., ./configure --with-llvm-tools-dir=/opt/homebrew/opt/llvm...) and then just run make without remembering to pass LLVM_TOOLS_INSTALL_DIR each time. That'd make it more consistent with other build options.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
…h2S.rst Co-authored-by: Savannah Ostrowski <savannah@python.org>
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @savannahostrowski: please review the changes made to this pull request. |
| The JIT compiler does not require end users to install any third-party dependencies, but part of it must be *built* using LLVM[^why-llvm]. You are *not* required to build the rest of CPython using LLVM, or even the same version of LLVM (in fact, this is uncommon). | ||
|
|
||
| LLVM version 19 is the officially supported version. You can modify if needed using the `LLVM_VERSION` env var during configure. Both `clang` and `llvm-readobj` need to be installed and discoverable (version suffixes, like `clang-19`, are okay). It's highly recommended that you also have `llvm-objdump` available, since this allows the build script to dump human-readable assembly for the generated code. | ||
| LLVM version 19 is the officially supported version. You can modify if needed using the `LLVM_VERSION` env var during configure. Both `clang` and `llvm-readobj` need to be installed and discoverable (version suffixes, like `clang-19`, are okay). It's highly recommended that you also have `llvm-objdump` available, since this allows the build script to dump human-readable assembly for the generated code. If you have multiple matching LLVM installations, you can use `LLVM_TOOLS_INSTALL_DIR` to point to the preferred installation prefix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should rephrase/restructure this section to say that you can configure LLVM_VERSION and LLVM_TOOLS_INSTALL_DIR during configure now that this PR has been refactored. Maybe something like:
You can customize the LLVM configuration using environment variables before running configure:
- LLVM_VERSION: Specify a different LLVM version (default: 19)
- LLVM_TOOLS_INSTALL_DIR: Point to a specific LLVM installation prefix when multiple installations exist
For additional clarity, it's probably worth specifying that LLVM_TOOLS_INSTALL_DIR should be the installation prefix (the directory containing bin/, not bin/ itself; related to my comment above).
| ) -> str | None: | ||
| # Explicitly defined LLVM installation location | ||
| if llvm_tools_install_dir: | ||
| path = os.path.join(llvm_tools_install_dir, tool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, I think this should point to the installation prefix where the tools are in bin/, like we have on lines 96 and 102, right?
| path = os.path.join(llvm_tools_install_dir, tool) | |
| path = os.path.join(llvm_tools_install_dir, "bin", tool) |
| [AS_VAR_APPEND([CFLAGS_NODIST], [" $jit_flags"]) | ||
| AS_VAR_SET([REGEN_JIT_COMMAND], | ||
| ["\$(PYTHON_FOR_REGEN) \$(srcdir)/Tools/jit/build.py ${ARCH_TRIPLES:-$host} --output-dir . --pyconfig-dir . --cflags=\"$CFLAGS_JIT\" --llvm-version=\"$LLVM_VERSION\""]) | ||
| ["\$(PYTHON_FOR_REGEN) \$(srcdir)/Tools/jit/build.py ${ARCH_TRIPLES:-$host} --output-dir . --pyconfig-dir . --cflags=\"$CFLAGS_JIT\" --llvm-version=\"$LLVM_VERSION\" --llvm-tools-install-dir=\"$LLVM_TOOLS_INSTALL_DIR\""]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this change need to be replicated under PCbuild somewhere?
Fixes #138451
Add support for explicitly defined LLVM installation location.
This is necessary as MSVC now comes with its own LLVM installation and activating MSVC via vcvars.bat will put LLVM tools on the
PATHbefore the local ones.You can see this in usage in conda-forge's latest Python 3.13 build: conda-forge/python-feedstock#807