Skip to content

Don't inline generated functions if we can't invoke their generator#59018

Merged
JeffBezanson merged 8 commits intoJuliaLang:masterfrom
serenity4:dont-inline-generator-if-cant-invoke
Aug 28, 2025
Merged

Don't inline generated functions if we can't invoke their generator#59018
JeffBezanson merged 8 commits intoJuliaLang:masterfrom
serenity4:dont-inline-generator-if-cant-invoke

Conversation

@serenity4
Copy link
Copy Markdown
Member

This PR disables inlining (via an outer src_inlining_policy method) for method instances obtained from @generated methods if we cannot invoke their generator during compilation, deferring their execution to runtime via dynamic dispatch.

Generated functions may be called with arguments that are not fully inferrable, yet we might inline them. The lack of concretely inferred argument types can cause the generator function to not be invoked may_invoke_generator(mi) === false, which causes its fallback definition to be executed when one is available to expand its body (the else branch of a if @generated construct, if present). However, fallback definitions were intended to enable more accurate type inference, but not to be actually used for execution; they will typically be slower, and in this case, it is best to defer the generation of the method body to runtime, where the generator can be invoked with concrete argument types.

Implements #59013 (comment), fixing #58915.

@serenity4 serenity4 self-assigned this Jul 16, 2025
@serenity4 serenity4 requested a review from topolarity July 16, 2025 02:50
@JeffBezanson
Copy link
Copy Markdown
Member

fallback definitions were intended to enable more accurate type inference, but not to be actually used for execution

That is basically correct, but they might be used for execution with --compile=all. Otherwise we can't guarantee it's possible to run all julia code without a JIT compiler available.

@JeffBezanson
Copy link
Copy Markdown
Member

This PR looks totally reasonable but is somehow very broken!

@JeffBezanson JeffBezanson merged commit c05b68f into JuliaLang:master Aug 28, 2025
7 checks passed
@topolarity
Copy link
Copy Markdown
Member

Thanks @serenity4 , great to have this fixed properly

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