Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-138451: Support custom LLVM installation path#138452

Open
xhochy wants to merge 11 commits intopython:mainfrom
xhochy:gh-138451
Open

gh-138451: Support custom LLVM installation path#138452
xhochy wants to merge 11 commits intopython:mainfrom
xhochy:gh-138451

Conversation

@xhochy
Copy link
Contributor

@xhochyxhochy commentedSep 3, 2025
edited by bedevere-appbot
Loading

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 thePATH before the local ones.

You can see this in usage in conda-forge's latest Python 3.13 build:conda-forge/python-feedstock#807

@python-cla-bot
Copy link

python-cla-botbot commentedSep 3, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

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)

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.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Actually there is aLLVM_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?

Choose a reason for hiding this comment

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

Yeah that seems better

Copy link
Member

Choose a reason for hiding this comment

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

IsLLVM_TOOLS_INSTALL_DIR (orLLVM_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?

@AA-TurnerAA-Turner requested a review froma teamSeptember 4, 2025 20:12
@_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:
Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

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

Also:

Suggested change
if(llvm_tools_dir:=os.getenv("LLVM_TOOLS_INSTALL_DIR"))isnotNone:
ifllvm_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.

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.

Copy link
ContributorAuthor

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.

Copy link
Member

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?

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 inhttps://github.com/python/cpython/blob/main/Tools/jit/README.md#building. We'd probably want to update this for now.

@xhochy
Copy link
ContributorAuthor

Rebased and added documentation to the mentioned README. Is there anything more I can do here to get it merged?

Copy link
Member

@savannahostrowskisavannahostrowski 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 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 runmake without remembering to passLLVM_TOOLS_INSTALL_DIR each time. That'd make it more consistent with other build options.

@bedevere-app
Copy link

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 phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@xhochy
Copy link
ContributorAuthor

I have made the requested changes; please review again

@bedevere-app
Copy link

Thanks for making the requested changes!

@savannahostrowski: please review the changes made to this pull request.

Copy link
Member

@savannahostrowskisavannahostrowski left a comment

Choose a reason for hiding this comment

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

One comment but I think this looks good.

@chris-eibl
Copy link
Member

activating MSVC via vcvars.bat will put LLVM tools on the PATH

I think this is the same problem I've faced in#141967 (comment), so thanks for doing this!

I've noticed there, that e.g. running

build.bat --experimental-jit --tail-call-interp "/p:PlatformToolset=ClangCL" "/p:LLVMInstallDir=<my-path-to>\llvm-21.1.4.0" "/p:LLVMToolsVersion=21"

will smuggle in the include path of the clang specified on the command line in front of the environment variable

INCLUDE=<my-path-to>\llvm-21.1.4.0\lib\clang\21\include;<other include paths>

which the hard coded (and maybe different) clang version used in the jit builds doesn't like depending on the version mismatch ...

@chris-eibl
Copy link
Member

LLVMInstallDir is the variable documented herehttps://learn.microsoft.com/en-us/cpp/build/clang-support-msbuild?view=msvc-170 which we already use at various places like

<!--
This ensures, that in case of clang-cl bundled with VS
we find e.g. clang_rt.profile.lib in the correct bitness.
In case the user sets LLVMInstallDir and LLVMToolsVersion,
they are responsible to use the correct tool architecture.
For details please see GH-131473.
-->
<PreferredToolArchitectureCondition="$(LLVMInstallDir) == '' and $(PlatformToolset) == 'ClangCL' and $(Platform) == 'x64'">x64</PreferredToolArchitecture>
<PreferredToolArchitectureCondition="$(LLVMInstallDir) == '' and $(PlatformToolset) == 'ClangCL' and $(Platform) == 'Win32'">x86</PreferredToolArchitecture>

Command='"$(LLVMInstallDir)\bin\llvm-profdata.exe" merge -output="$(OutDir)instrumented\profdata.profdata" "$(OutDir)instrumented\*_*.profraw"' />

You can also use a specific version of clang-cl downloaded from
https://github.com/llvm/llvm-project/releases, e.g.
clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz.
Given you have extracted that to <my-clang-dir>, you can use it like so
build.bat --pgo "/p:PlatformToolset=ClangCL" "/p:LLVMInstallDir=<my-clang-dir>" "/p:LLVMToolsVersion=18"

Does that mean that we have to specify the additionalLLVM_TOOLS_INSTALL_DIR variable to let the jit build pick up the same clang that is used for building cPython?

@zooba
Copy link
Member

Does that mean that we have to specify the additionalLLVM_TOOLS_INSTALL_DIR variable to let the jit build pick up the same clang that is used for building cPython?

There's no strict requirement for make/configure variables to match pcbuild.proj variables, but if it's specified as an environment variable then we should probably detect if it's set and copy it over the actual variable. That's one line inpython.props. I wouldn't bother updating all the references, just do a read in one place.

chris-eibl reacted with thumbs up emoji

@chris-eibl
Copy link
Member

chris-eibl commentedDec 3, 2025
edited
Loading

IIUC, you mean settingLLVMInstallDir andLLVMToolsVersion based onLLVM_TOOLS_INSTALL_DIR andLLVM_VERSION?

I think it might be "too late", since AFAIR internally this is used by MS when preparing the environment according to "their" variable names, but we'll see.

What's definitely easy: just use the MS variable names here

    <Exec Command='$(PythonForBuild) "$(PySourcePath)Tools\jit\build.py" $(JITArgs) --output-dir "$(GeneratedJitStencilsDir)" --pyconfig-dir "$(PySourcePath)PC" --llvm-version="$(LLVM_VERSION)" --llvm-tools-install-dir="$(LLVM_TOOLS_INSTALL_DIR)"'/>

because, as you said

There's no strict requirement for make/configure variables

and just update the jit readme accordingly if we cannot map the variables easily "early" enough ...

Or we do it inbuild.bat. Let's see ...

@zooba
Copy link
Member

Any file that setsPlatformToolset is loaded early enough to influence all the default settings in MSBuild, so it can certainly be set there. I believe that's onlypython.props, though it might be part/all ofpyproject.props as well, but I think this is a better setting forpython.props along with the other properties relating to external dependencies.

@xhochy
Copy link
ContributorAuthor

@zooba@chris-eibl I'm a bit unsure what I need to do here to get this merged. Is there anything from your side that I should address in my code for windows builds?

@chris-eibl
Copy link
Member

First I thought we have these choices:

  1. do nothing, i.e. allow people specifyingLLVMInstallDir the "regular" way to determine the clang-cl used for compiling Python andLLVM_TOOLS_INSTALL_DIR to specify the clang used for the jit stencils. With the potential drawback, that those two might interfere like I'vementioned above. It doesn't always happen and depends on the "compatibility" of the clang versions, so I thought most probably we should prohibit that (and don't force users to specify the same args twice).
  2. use justLLVMInstallDir andLLVMToolsVersion, i.e. different variables than on configure based builds
  3. ifLLVM_TOOLS_INSTALL_DIR is specified, overrideLLVMInstallDir. Likewise forLLVM_VERSION /LLVMToolsVersion.
  4. ifLLVMInstallDir is given, overrideLLVM_TOOLS_INSTALL_DIR. Likewise forLLVM_VERSION /LLVMToolsVersion.

@chris-eibl
Copy link
Member

I've first tried option 4 via adding

<LLVM_TOOLS_INSTALL_DIR Condition="$(LLVMInstallDir) != '' and $(LLVM_TOOLS_INSTALL_DIR) == ''">$(LLVMInstallDir)</LLVM_TOOLS_INSTALL_DIR><LLVM_VERSION Condition="$(LLVMToolsVersion) != '' and $(LLVM_VERSION) == ''">$(LLVMToolsVersion)</LLVM_VERSION>

to

<PropertyGroup>
<JITArgsCondition="$(Platform) == 'ARM64'">aarch64-pc-windows-msvc</JITArgs>
<JITArgsCondition="$(Platform) == 'Win32'">i686-pc-windows-msvc</JITArgs>
<JITArgsCondition="$(Platform) == 'x64'">x86_64-pc-windows-msvc</JITArgs>
<JITArgsCondition="$(Configuration) == 'Debug'">$(JITArgs) --debug</JITArgs>
</PropertyGroup>

This would have been my preferred choice, because then we would not have to "teach something new" and could have the same variables on Windows / Linux.

However, if during installing of Visual Studio the bundled clang-cl is chosen to be installed, it turned out thatLLVM_VERSION is always set to this bundled version, even if doing just a regular MSVC build. So option 2 and 4 are out ...

@chris-eibl
Copy link
Member

Implementing option 3 is easy, too: just add

<LLVMInstallDir Condition="$(LLVM_TOOLS_INSTALL_DIR) != ''">$(LLVM_TOOLS_INSTALL_DIR)</LLVMInstallDir><LLVMToolsVersion Condition="$(LLVM_VERSION) != ''">$(LLVM_VERSION)</LLVMToolsVersion>

after

<PreferredToolArchitectureCondition="$(LLVMInstallDir) == '' and $(PlatformToolset) == 'ClangCL' and $(Platform) == 'x64'">x64</PreferredToolArchitecture>
<PreferredToolArchitectureCondition="$(LLVMInstallDir) == '' and $(PlatformToolset) == 'ClangCL' and $(Platform) == 'Win32'">x86</PreferredToolArchitecture>

Now we'd have to document that when overriding the clang used to build the jit stencils will chose that clang for building Python, too, if built with"/p:PlatformToolset=ClangCL".

So option 1 gives more flexibility, but needs (?) a warning and "specifying the same thing twice" might feel like a wart.
Option 3 needs even more documentation?

I am unsure what to do best, but considering the jit is maybe no longer experimental, we should care to find a good solution here?

@Fidget-Spinner
Copy link
Member

I am unsure what to do best, but considering the jit is maybe no longer experimental, we should care to find a good solution here?

The JIT is still experimental, but Python build flags and in general build.bat flags are bound by backwards compatibility, so when adding things we have to treat it as non-experimental.

savannahostrowski and chris-eibl reacted with thumbs up emoji

@savannahostrowski
Copy link
Member

Just checking in here, is there anything else required for this one?

@chris-eibl
Copy link
Member

chris-eibl commentedJan 26, 2026
edited
Loading

Sorry, seems I've stalled this. IMHO we can handle the "clang version to build python" vs "clang version to build the jit" quirk in a follow-up PR? Especially since the first seems to be niche on Windows?

@savannahostrowski
Copy link
Member

I think that's reasonable but I'd defer to@zooba on how he feels about that. I also don't want the follow up to get lost in the ether.

chris-eibl reacted with thumbs up emoji

@chris-eibl
Copy link
Member

I think that's reasonable but I'd defer to@zooba on how he feels about that

I've furthermore tested this PR now locally successfully, too, in addition to my initial review(s).
@xhochy Thanks for this, nice improvement!
@zooba: any objections to merging this?

I also don't want the follow up to get lost in the ether.

I've created#146210.

@chris-eiblchris-eibl self-requested a reviewMarch 20, 2026 14:24
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@Fidget-SpinnerFidget-SpinnerFidget-Spinner left review comments

@savannahostrowskisavannahostrowskisavannahostrowski approved these changes

@chris-eiblchris-eiblchris-eibl approved these changes

@brandtbucherbrandtbucherAwaiting requested review from brandtbucherbrandtbucher is a code owner

@diegorussodiegorussoAwaiting requested review from diegorussodiegorusso is a code owner

@erlend-aaslanderlend-aaslandAwaiting requested review from erlend-aaslanderlend-aasland is a code owner

@corona10corona10Awaiting requested review from corona10corona10 is a code owner

@AA-TurnerAA-TurnerAwaiting requested review from AA-TurnerAA-Turner is a code owner

@emmatypingemmatypingAwaiting requested review from emmatypingemmatyping is a code owner

@zoobazoobaAwaiting requested review from zooba

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Wrong LLVM tools picked up if using MSVC activation and custom LLVM build

7 participants

@xhochy@chris-eibl@zooba@Fidget-Spinner@savannahostrowski@brandtbucher@nazeerali4325-commits

[8]ページ先頭

©2009-2026 Movatter.jp