Skip to content
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

fix #25678: return matters for generated functions #40778

Merged
merged 3 commits into from
Aug 24, 2021
Merged

Conversation

simeonschaub
Copy link
Member

Just explicitly check for CodeInfo objects when doing the
interpolation of the generated parts. I initially tried a more
complicated version of this, which avoided doing this check if there was
more than one generated part, but that's probably not necessary here.

@simeonschaub simeonschaub added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label May 10, 2021
@JeffBezanson JeffBezanson self-assigned this May 14, 2021
@JeffBezanson
Copy link
Member

I think we should only apply this at the top-level, not recursively, so it's still possible to splice in a CodeInfo (there may not be any use case for that, but better to allow it). E.g.:

--- a/base/expr.jl
+++ b/base/expr.jl
@@ -434,7 +434,9 @@ macro generated(f)
                          Expr(:block,
                               lno,
                               Expr(:if, Expr(:generated),
-                                   body,
+                                   Expr(:block,
+                                        :(local tmp = $body),
+                                        :(if tmp isa Core.CodeInfo; return tmp; else tmp; end)),
                                    Expr(:block,
                                         Expr(:meta, :generated_only),

@simeonschaub
Copy link
Member Author

I did think a while about how this should best be handled, but somehow that didn't occur to me. Yours seems like the better solution.

Just explicitely check for `CodeInfo` objects and use an explicit
`return` in this case inside the `@generated` macro.

Co-authored-by: Jeff Bezanson <[email protected]>
@vtjnash
Copy link
Member

vtjnash commented May 19, 2021

Still seems rather buggy that lowering can tell whether the return keyword was used, when these appear to be redundant expressions?

@simeonschaub
Copy link
Member Author

simeonschaub commented May 19, 2021

I didn't actually know about this, but the fact that you can just use if @generated anywhere in a function and it's basically just an interpolation if the function is called as generated is a pretty cool feature I think.
Or are you saying that lowering should just remove any explicit returns in the @generated() == true part? That might be quite nontrivial and once you understand what if @generated is doing, I think the current behavior is kind of intuitive.

@simeonschaub simeonschaub requested a review from JeffBezanson May 19, 2021 21:49
@simeonschaub
Copy link
Member Author

Bump

@simeonschaub
Copy link
Member Author

This just came up again, is this OK to merge?

@JeffBezanson JeffBezanson merged commit 92c84bf into master Aug 24, 2021
@JeffBezanson JeffBezanson deleted the sds/fix_25678 branch August 24, 2021 18:19
@simeonschaub simeonschaub added backport 1.6 Change should be backported to release-1.6 backport 1.7 labels Nov 22, 2021
@KristofferC KristofferC mentioned this pull request Dec 2, 2021
29 tasks
KristofferC pushed a commit that referenced this pull request Dec 7, 2021
Just explicitely check for `CodeInfo` objects and use an explicit
`return` in this case inside the `@generated` macro.

Co-authored-by: Jeff Bezanson <[email protected]>
(cherry picked from commit 92c84bf)
@KristofferC KristofferC removed the backport 1.6 Change should be backported to release-1.6 label Dec 11, 2021
KristofferC pushed a commit that referenced this pull request Jan 10, 2022
Just explicitely check for `CodeInfo` objects and use an explicit
`return` in this case inside the `@generated` macro.

Co-authored-by: Jeff Bezanson <[email protected]>
(cherry picked from commit 92c84bf)
# https://github.com/JuliaLang/julia/issues/25678
Expr(:block,
:(local tmp = $body),
:(if tmp isa Core.CodeInfo; return tmp; else tmp; end)),
Copy link
Contributor

@devmotion devmotion Jan 14, 2022

Choose a reason for hiding this comment

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

@simeonschaub I guess generally it's a bad idea to name a module Core but this caused an error in Turing: TuringLang/Turing.jl#1756 (comment) and discussion in the issue. I think this could be fixed by interpolating Core.CodeInfo. I could prepare a PR (my first to base actually 😮) if you consider this to be a bug that should be fixed in base (I'll submit a PR to Turing in any case that renames the offending module).

Copy link
Member Author

@simeonschaub simeonschaub Jan 14, 2022

Choose a reason for hiding this comment

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

That's funny, I was just staring at code generated by this today and was wondering whether having a call to getproperty in here was the right thing! It's generally more idiomatic to handle this as $(GlobalRef(Core, :CodeInfo)) instead of just interpolating the object though since that's what lowering would generate, but that sounds like the right thing to do.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ref: #43823

LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
…g#40778)

Just explicitely check for `CodeInfo` objects and use an explicit
`return` in this case inside the `@generated` macro.

Co-authored-by: Jeff Bezanson <[email protected]>
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
…g#40778)

Just explicitely check for `CodeInfo` objects and use an explicit
`return` in this case inside the `@generated` macro.

Co-authored-by: Jeff Bezanson <[email protected]>
staticfloat pushed a commit that referenced this pull request Dec 23, 2022
Just explicitely check for `CodeInfo` objects and use an explicit
`return` in this case inside the `@generated` macro.

Co-authored-by: Jeff Bezanson <[email protected]>
(cherry picked from commit 92c84bf)
vtjnash added a commit that referenced this pull request Feb 9, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the uesr must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix #25678: return matters for generated functions
(#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.
maleadt pushed a commit that referenced this pull request Feb 21, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the uesr must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix #25678: return matters for generated functions
(#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.
maleadt pushed a commit that referenced this pull request Feb 22, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the user must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix #25678: return matters for generated functions
(#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.
maleadt pushed a commit that referenced this pull request Feb 23, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the user must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix #25678: return matters for generated functions
(#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.
maleadt pushed a commit that referenced this pull request Feb 24, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the user must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix #25678: return matters for generated functions
(#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.
vtjnash added a commit that referenced this pull request Mar 22, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the user must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix #25678: return matters for generated functions
(#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.
vtjnash added a commit that referenced this pull request Mar 24, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the user must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix #25678: return matters for generated functions
(#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.

Note that the passed world may be `typemax(UInt)`, which demands that
the generator must return code that is unspecialized (guaranteed to run
correctly in any world).

Co-authored-by: Jameson Nash <[email protected]>
Xnartharax pushed a commit to Xnartharax/julia that referenced this pull request Apr 19, 2023
Expose the demanded world to the GeneratedFunctionStub caller, for users
such as Cassette. If this argument is used, the user must return a
CodeInfo with the min/max world field set correctly.

Make the internal representation a tiny bit more compact also, removing
a little bit of unnecessary metadata.

Remove support for returning `body isa CodeInfo` via this wrapper, since
it is impossible to return a correct object via the
GeneratedFunctionStub since it strips off the world argument, which is
required for it to do so. This also removes support for not inferring
these fully (expand_early=false).

Also answer method lookup queries about the future correctly, by
refusing to answer them. This helps keeps execution correct as methods
get added to the system asynchronously.

This reverts "fix JuliaLang#25678: return matters for generated functions
(JuliaLang#40778)" (commit 92c84bf), since this
is no longer sensible to return here anyways, so it is no longer
permitted or supported by this macro.

Fixes various issues where we failed to specify the correct world.

Note that the passed world may be `typemax(UInt)`, which demands that
the generator must return code that is unspecialized (guaranteed to run
correctly in any world).

Co-authored-by: Jameson Nash <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants