Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-125498: Update JIT builds to use LLVM 19 and use preserve_none#125499
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
48 commits Select commit Hold shift + click to select a range
94252cf Update to LLVM 19
savannahostrowski f8dc236 update syntax in disabled gil ci
savannahostrowski 80a3b40 📜🤖 Added by blurb_it.
blurb-it[bot] ba02d7c Merge branch 'main' into jit-llvm-19
savannahostrowski 7a1133e Update readme
savannahostrowski d8e38db fix free-threaded by pseudo-pinning version
savannahostrowski b8ae218 Add check to see that registers match in stencil generation
savannahostrowski 4368d5f Appease linters
savannahostrowski 12fc5cd Remove devcontainer instructions from readme
savannahostrowski 7f9fe5a Update README
savannahostrowski 8c21729 Merge branch 'main' into jit-llvm-19
savannahostrowski a597ea5 Remove ghccc
savannahostrowski 4df5efc Merge branch 'main' into remove-ghccc
savannahostrowski 85b858d add back cpu_count
savannahostrowski 9209651 Appease linter
savannahostrowski a842f90 Add sys import
savannahostrowski 73c725b Move preserve_none
savannahostrowski 4d8a012 define jit_func_preserve_none
savannahostrowski 994af97 add comment
savannahostrowski 0d86727 add comment
savannahostrowski 98f0535 Fix whitespace
savannahostrowski 9a20a2e Move header to separate file
savannahostrowski 4a2f3c4 Add newline
savannahostrowski 4c4ca2f Add newline
savannahostrowski 7d1745a Address PR comments
savannahostrowski c8d4692 Replace entry_symbol with string
savannahostrowski 72d5ed0 Appease linter
savannahostrowski a96af70 Add newline
savannahostrowski 9827ade Merge branch 'main' into remove-ghccc
savannahostrowski 4e32743 Merge branch 'main' into remove-ghccc
savannahostrowski ed29ae2 Merge branch 'main' into remove-ghccc
savannahostrowski 3a2ecee Merge branch 'main' into remove-ghccc
savannahostrowski 3259994 Merge branch 'main' into remove-ghccc
savannahostrowski 5ef69e6 📜🤖 Added by blurb_it.
blurb-it[bot] 709bb08 Rephrase
savannahostrowski 5ca8d61 Run pre-commit
savannahostrowski 24d9143 Add newline
savannahostrowski b351303 Add line to remove symlink
savannahostrowski 53ec962 Merge branch 'main' into remove-ghccc
savannahostrowski fe58a12 Fix typo
savannahostrowski a4b2d3e Merge branch 'remove-ghccc' of https://github.com/savannahostrowski/c…
savannahostrowski 0f88955 Fix wording
savannahostrowski 4e16dd6 Merge branch 'main' into remove-ghccc
savannahostrowski bb1e650 Update Misc/NEWS.d/next/Core_and_Builtins/2024-10-22-04-18-53.gh-issu…
savannahostrowski 5f3ec52 Apply suggestions from code review
savannahostrowski 46cee93 Address PR comments
savannahostrowski c119a5d Fix whitespace
savannahostrowski cbb0ddf Add newline to jit.h
savannahostrowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Core_and_Builtins/2024-09-14-20-09-39.gh-issue-123714.o1mbe4.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Update JIT compilation to use LLVM 19 |
4 changes: 4 additions & 0 deletions 4 Misc/NEWS.d/next/Core_and_Builtins/2024-10-22-04-18-53.gh-issue-125498.cFjPIn.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| The JIT has been updated to leverage Clang 19’s new ``preserve_none`` attribute, | ||
| which supports more platforms and is more useful than LLVM's existing ``ghccc`` | ||
| calling convention. This also removes the need to manually patch the calling | ||
| convention in LLVM IR, simplifying the JIT compilation process. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -26,7 +26,6 @@ | ||
| PYTHON_EXECUTOR_CASES_C_H = CPYTHON / "Python" / "executor_cases.c.h" | ||
| TOOLS_JIT_TEMPLATE_C = TOOLS_JIT / "template.c" | ||
| _S = typing.TypeVar("_S", _schema.COFFSection, _schema.ELFSection, _schema.MachOSection) | ||
| _R = typing.TypeVar( | ||
| "_R", _schema.COFFRelocation, _schema.ELFRelocation, _schema.MachORelocation | ||
| @@ -39,7 +38,6 @@ class _Target(typing.Generic[_S, _R]): | ||
| _: dataclasses.KW_ONLY | ||
| alignment: int = 1 | ||
| args: typing.Sequence[str] = () | ||
| ghccc: bool = False | ||
| prefix: str = "" | ||
| stable: bool = False | ||
| debug: bool = False | ||
| @@ -88,11 +86,7 @@ async def _parse(self, path: pathlib.Path) -> _stencils.StencilGroup: | ||
| sections: list[dict[typing.Literal["Section"], _S]] = json.loads(output) | ||
| for wrapped_section in sections: | ||
| self._handle_section(wrapped_section["Section"], group) | ||
| # The trampoline's entry point is just named "_ENTRY", since on some | ||
| # platforms we later assume that any function starting with "_JIT_" uses | ||
| # the GHC calling convention: | ||
| entry_symbol = "_JIT_ENTRY" if "_JIT_ENTRY" in group.symbols else "_ENTRY" | ||
| assert group.symbols[entry_symbol] == (_stencils.HoleValue.CODE, 0) | ||
| assert group.symbols["_JIT_ENTRY"] == (_stencils.HoleValue.CODE, 0) | ||
| if group.data.body: | ||
| line = f"0:{str(bytes(group.data.body)).removeprefix('b')}" | ||
| group.data.disassembly.append(line) | ||
| @@ -112,9 +106,6 @@ def _handle_relocation( | ||
| async def _compile( | ||
| self, opname: str, c: pathlib.Path, tempdir: pathlib.Path | ||
| ) -> _stencils.StencilGroup: | ||
| # "Compile" the trampoline to an empty stencil group if it's not needed: | ||
| if opname == "trampoline" and not self.ghccc: | ||
| return _stencils.StencilGroup() | ||
| o = tempdir / f"{opname}.o" | ||
| args = [ | ||
| f"--target={self.triple}", | ||
| @@ -128,6 +119,7 @@ async def _compile( | ||
| f"-I{CPYTHON / 'Include' / 'internal'}", | ||
| f"-I{CPYTHON / 'Include' / 'internal' / 'mimalloc'}", | ||
| f"-I{CPYTHON / 'Python'}", | ||
| f"-I{CPYTHON / 'Tools' / 'jit'}", | ||
| "-O3", | ||
| "-c", | ||
| # This debug info isn't necessary, and bloats out the JIT'ed code. | ||
| @@ -143,44 +135,12 @@ async def _compile( | ||
| # Don't call stack-smashing canaries that we can't find or patch: | ||
| "-fno-stack-protector", | ||
| "-std=c11", | ||
| "-o", | ||
| f"{o}", | ||
| f"{c}", | ||
| *self.args, | ||
| ] | ||
| if self.ghccc: | ||
savannahostrowski marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| # This is a bit of an ugly workaround, but it makes the code much | ||
| # smaller and faster, so it's worth it. We want to use the GHC | ||
| # calling convention, but Clang doesn't support it. So, we *first* | ||
| # compile the code to LLVM IR, perform some text replacements on the | ||
| # IR to change the calling convention(!), and then compile *that*. | ||
| # Once we have access to Clang 19, we can get rid of this and use | ||
| # __attribute__((preserve_none)) directly in the C code instead: | ||
| ll = tempdir / f"{opname}.ll" | ||
| args_ll = args + [ | ||
| # -fomit-frame-pointer is necessary because the GHC calling | ||
| # convention uses RBP to pass arguments: | ||
| "-S", | ||
| "-emit-llvm", | ||
| "-fomit-frame-pointer", | ||
| "-o", | ||
| f"{ll}", | ||
| f"{c}", | ||
| ] | ||
| await _llvm.run("clang", args_ll, echo=self.verbose) | ||
| ir = ll.read_text() | ||
| # This handles declarations, definitions, and calls to named symbols | ||
| # starting with "_JIT_": | ||
| ir = re.sub( | ||
| r"(((noalias|nonnull|noundef) )*ptr @_JIT_\w+\()", r"ghccc \1", ir | ||
| ) | ||
| # This handles calls to anonymous callees, since anything with | ||
| # "musttail" needs to use the same calling convention: | ||
| ir = ir.replace("musttail call", "musttail call ghccc") | ||
| # Sometimes *both* replacements happen at the same site, so fix it: | ||
| ir = ir.replace("ghccc ghccc", "ghccc") | ||
| ll.write_text(ir) | ||
| args_o = args + ["-Wno-unused-command-line-argument", "-o", f"{o}", f"{ll}"] | ||
| else: | ||
| args_o = args + ["-o", f"{o}", f"{c}"] | ||
| await _llvm.run("clang", args_o, echo=self.verbose) | ||
| await _llvm.run("clang", args, echo=self.verbose) | ||
| return await self._parse(o) | ||
| async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]: | ||
| @@ -519,7 +479,6 @@ def _handle_relocation( | ||
| def get_target(host: str) -> _COFF | _ELF | _MachO: | ||
| """Build a _Target for the given host "triple" and options.""" | ||
| # ghccc currently crashes Clang when combined with musttail on aarch64. :( | ||
| target: _COFF | _ELF | _MachO | ||
| if re.fullmatch(r"aarch64-apple-darwin.*", host): | ||
| target = _MachO(host, alignment=8, prefix="_") | ||
| @@ -535,16 +494,20 @@ def get_target(host: str) -> _COFF | _ELF | _MachO: | ||
| ] | ||
| target = _ELF(host, alignment=8, args=args) | ||
| elif re.fullmatch(r"i686-pc-windows-msvc", host): | ||
| args = ["-DPy_NO_ENABLE_SHARED"] | ||
| target = _COFF(host, args=args, ghccc=True, prefix="_") | ||
| args = [ | ||
| "-DPy_NO_ENABLE_SHARED", | ||
| # __attribute__((preserve_none)) is not supported | ||
| "-Wno-ignored-attributes", | ||
| ] | ||
| target = _COFF(host, args=args, prefix="_") | ||
| elif re.fullmatch(r"x86_64-apple-darwin.*", host): | ||
| target = _MachO(host, ghccc=True, prefix="_") | ||
| target = _MachO(host, prefix="_") | ||
| elif re.fullmatch(r"x86_64-pc-windows-msvc", host): | ||
| args = ["-fms-runtime-lib=dll"] | ||
| target = _COFF(host, args=args, ghccc=True) | ||
| target = _COFF(host, args=args) | ||
| elif re.fullmatch(r"x86_64-.*-linux-gnu", host): | ||
| args = ["-fpic"] | ||
| target = _ELF(host, args=args, ghccc=True) | ||
| target = _ELF(host, args=args) | ||
| else: | ||
| raise ValueError(host) | ||
| return target | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // To use preserve_none in JIT builds, we need to declare a separate function | ||
| // pointer with __attribute__((preserve_none)), since this attribute may not be | ||
| // supported by the compiler used to build the rest of the interpreter. | ||
| typedef jit_func __attribute__((preserve_none)) jit_func_preserve_none; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.