Skip to content

Conversation

@serenity4
Copy link
Member

This PR leverages new capabilities that have been implemented in #56660 to also support caching methods for runtime calls under a custom AbstractInterpreter.

In my understanding, the idea is that when executing code compiled with a custom AbstractInterpreter, only methods that can be compiled ahead of execution will be cached, because dynamic calls requiring runtime compilation will not have enough context to know which interpreter and cache to use. They will instead use the native interpreter and cache.

This sample package demonstrates a way to ensure that runtime calls go through an entry point (with_new_compiler) that provides the context required to use the right cache and interpreter for subsequent compilation.

I'd be happy to get feedback on the logic used to redirect runtime calls to with_new_compiler. Overloading optimize(::SplitCacheInterp) seemed the most convenient (right after that, the IRCode gets converted to a CodeInfo), but perhaps there might be a better place for it.

@serenity4
Copy link
Member Author

serenity4 commented Jan 29, 2025

Tests are currently not run in the main test suite, because of issues with invoke(f, ::CodeInstance, args...) for external interpreters on master (@Keno said he could have a look). If you want to try this out, you can build Julia with #56660 (efa917e).

@serenity4 serenity4 marked this pull request as draft January 29, 2025 11:05
@Keno
Copy link
Member

Keno commented Jan 29, 2025

Some concrete comments, but this is largely what I had in mind, yes.

@vchuravy
Copy link
Member

After many years of maintaining Cassette I distinctly dislike the approach to propagate the compiler through the IR.

That's why in #52964 I approached this from the "what if they compiler was a scoped-value" perspective.
A fundamental challenge of the override approach is the question how to handle "indirect" calls like task launches.

I could rebase #52964 if there is interest (beyond myself) in exploring that alternative approach.

@Keno
Copy link
Member

Keno commented Jan 29, 2025

This specific utility is just for testing compiler changes (and exercising the caching and foreign CodeInstance APIs) without prejudice to having, more general mechanism, for compiler switches.

@serenity4
Copy link
Member Author

serenity4 commented Jan 31, 2025

The bug on invoking CodeInstances was bisected to 5c88b72 (from #56880) (@Keno)

Keno pushed a commit that referenced this pull request Feb 17, 2025
…egen cache (#57272)

Implements a way to add `CodeInstance`s compiled by external
interpreters to JIT, such that they become legal targets for `invoke`
calls.

Based on a design proposed by @Keno, the `AbstractInterpreter` interface
is extended to support providing a codegen cache that is filled during
inference for future use with `add_codeinsts_to_jit!`.

This allows `invoke(f, ::CodeInstance, args...)` to work on external
interpreters, which is currently failing on `master` (see #57193).

---------

Co-authored-by: Cédric Belmant <[email protected]>
@Keno
Copy link
Member

Keno commented Feb 17, 2025

Should be rebasable now that #57272 is merged.

@serenity4 serenity4 marked this pull request as ready for review February 18, 2025 17:09
@Keno Keno merged commit edf1cd0 into JuliaLang:master Feb 28, 2025
5 of 7 checks passed
KristofferC pushed a commit that referenced this pull request Mar 31, 2025
…egen cache (#57272)

Implements a way to add `CodeInstance`s compiled by external
interpreters to JIT, such that they become legal targets for `invoke`
calls.

Based on a design proposed by @Keno, the `AbstractInterpreter` interface
is extended to support providing a codegen cache that is filled during
inference for future use with `add_codeinsts_to_jit!`.

This allows `invoke(f, ::CodeInstance, args...)` to work on external
interpreters, which is currently failing on `master` (see #57193).

---------

Co-authored-by: Cédric Belmant <[email protected]>
(cherry picked from commit 9d2e9ed)
KristofferC pushed a commit that referenced this pull request Mar 31, 2025
…egen cache (#57272)

Implements a way to add `CodeInstance`s compiled by external
interpreters to JIT, such that they become legal targets for `invoke`
calls.

Based on a design proposed by @Keno, the `AbstractInterpreter` interface
is extended to support providing a codegen cache that is filled during
inference for future use with `add_codeinsts_to_jit!`.

This allows `invoke(f, ::CodeInstance, args...)` to work on external
interpreters, which is currently failing on `master` (see #57193).

---------

Co-authored-by: Cédric Belmant <[email protected]>
(cherry picked from commit 9d2e9ed)
Keno pushed a commit that referenced this pull request Apr 2, 2025
…ze` (#57640)

As a follow-up to #57193 now that #57375 is merged.

---------

Co-authored-by: Cédric Belmant <[email protected]>
serenity4 added a commit to serenity4/julia that referenced this pull request May 1, 2025
…egen cache (JuliaLang#57272)

Implements a way to add `CodeInstance`s compiled by external
interpreters to JIT, such that they become legal targets for `invoke`
calls.

Based on a design proposed by @Keno, the `AbstractInterpreter` interface
is extended to support providing a codegen cache that is filled during
inference for future use with `add_codeinsts_to_jit!`.

This allows `invoke(f, ::CodeInstance, args...)` to work on external
interpreters, which is currently failing on `master` (see JuliaLang#57193).

---------

Co-authored-by: Cédric Belmant <[email protected]>
serenity4 added a commit to serenity4/julia that referenced this pull request May 1, 2025
…iaLang#57193)

This PR leverages new capabilities that have been implemented in
JuliaLang#56660 to also support caching
methods for runtime calls under a custom `AbstractInterpreter`.

In my understanding, the idea is that when executing code compiled with
a custom `AbstractInterpreter`, only methods that can be compiled ahead
of execution will be cached, because dynamic calls requiring runtime
compilation will not have enough context to know which interpreter and
cache to use. They will instead use the native interpreter and cache.

This sample package demonstrates a way to ensure that runtime calls go
through an entry point (`with_new_compiler`) that provides the context
required to use the right cache and interpreter for subsequent
compilation.

I'd be happy to get feedback on the logic used to redirect runtime calls
to `with_new_compiler`. Overloading `optimize(::SplitCacheInterp)`
seemed the most convenient (right after that, the `IRCode` gets
converted to a `CodeInfo`), but perhaps there might be a better place
for it.

---------

Co-authored-by: Cédric Belmant <[email protected]>
serenity4 added a commit to serenity4/julia that referenced this pull request May 1, 2025
…ze` (JuliaLang#57640)

As a follow-up to JuliaLang#57193 now that JuliaLang#57375 is merged.

---------

Co-authored-by: Cédric Belmant <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants