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

lowering: Only try to define the method once #57346

Merged
merged 1 commit into from
Feb 12, 2025
Merged

lowering: Only try to define the method once #57346

merged 1 commit into from
Feb 12, 2025

Conversation

Keno
Copy link
Member

@Keno Keno commented Feb 10, 2025

Before:

:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:thunk, CodeInfo(
1 ─     return $(Expr(:method, :(Main.f)))
)))
│         $(Expr(:method, :(Main.f)))
│   %3  = Main.f
│   %4  =   dynamic Core.Typeof(%3)
│   %5  =   builtin Core.svec(%4, Core.Any)
│   %6  =   builtin Core.svec()
│   %7  =   builtin Core.svec(%5, %6, $(QuoteNode(:(#= REPL[2]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%7), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %10 = Main.f
└──       return %10
))))

After:

julia> @Meta.lower f(x)=1
:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:method, :(Main.f)))
│         $(Expr(:latestworld))
│         Main.f
│         $(Expr(:latestworld))
│   %5  = Main.f
│   %6  =   dynamic Core.Typeof(%5)
│   %7  =   builtin Core.svec(%6, Core.Any)
│   %8  =   builtin Core.svec()
│   %9  =   builtin Core.svec(%7, %8, $(QuoteNode(:(#= REPL[1]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%9), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %12 = Main.f
└──       return %12
))))

This doesn't really make a semantic difference, but if f is a type, we may now give a warning, so the prior definition would give the warning twice (#57311 (comment)). We may want to consider rate-limiting the warning independently, but for now at least give the correct number of warnings.

@Keno Keno requested a review from JeffBezanson February 10, 2025 18:38
,e
(thunk (lambda () (() () 0 ()) (block (return ,e))))))))
Copy link
Member Author

Choose a reason for hiding this comment

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

@JeffBezanson I don't really know why this thunk was here. Tests passed without it, but if you happen to remember and I'm missing something, please chime in.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's a bit weird. This is 6 years old so maybe something changed around it.

Copy link
Member

Choose a reason for hiding this comment

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

It changes the order of evaluation of these statements, see lift-toplevel, which relies on the presence of thunk to keep the IR ssavalues and struct definitions in the correct order after it chaotically reorders when statements run to follow the toplevel semantic rules

Keno added a commit to timholy/Revise.jl that referenced this pull request Feb 11, 2025
Before:
```
:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:thunk, CodeInfo(
1 ─     return $(Expr(:method, :(Main.f)))
)))
│         $(Expr(:method, :(Main.f)))
│   %3  = Main.f
│   %4  =   dynamic Core.Typeof(%3)
│   %5  =   builtin Core.svec(%4, Core.Any)
│   %6  =   builtin Core.svec()
│   %7  =   builtin Core.svec(%5, %6, $(QuoteNode(:(#= REPL[2]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%7), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %10 = Main.f
└──       return %10
))))
```

After:
```
julia> @Meta.lower f(x)=1
:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:method, :(Main.f)))
│         $(Expr(:latestworld))
│         Main.f
│         $(Expr(:latestworld))
│   %5  = Main.f
│   %6  =   dynamic Core.Typeof(%5)
│   %7  =   builtin Core.svec(%6, Core.Any)
│   %8  =   builtin Core.svec()
│   %9  =   builtin Core.svec(%7, %8, $(QuoteNode(:(#= REPL[1]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%9), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %12 = Main.f
└──       return %12
))))
```

This doesn't really make a semantic difference, but if `f` is a type,
we may now give a warning, so the prior definition would give the
warning twice (#57311 (comment)).
We may want to consider rate-limiting the warning independently,
but for now at least give the correct number of warnings.
Keno added a commit to timholy/Revise.jl that referenced this pull request Feb 12, 2025
Keno added a commit to timholy/Revise.jl that referenced this pull request Feb 12, 2025
@Keno Keno merged commit 512eb5e into master Feb 12, 2025
5 of 7 checks passed
@Keno Keno deleted the kf/methodonce branch February 12, 2025 20:30
@KristofferC KristofferC added the backport 1.12 Change should be backported to release-1.12 label Feb 14, 2025
KristofferC pushed a commit that referenced this pull request Feb 14, 2025
Before:
```
:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:thunk, CodeInfo(
1 ─     return $(Expr(:method, :(Main.f)))
)))
│         $(Expr(:method, :(Main.f)))
│   %3  = Main.f
│   %4  =   dynamic Core.Typeof(%3)
│   %5  =   builtin Core.svec(%4, Core.Any)
│   %6  =   builtin Core.svec()
│   %7  =   builtin Core.svec(%5, %6, $(QuoteNode(:(#= REPL[2]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%7), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %10 = Main.f
└──       return %10
))))
```

After:
```
julia> @Meta.lower f(x)=1
:($(Expr(:thunk, CodeInfo(
1 ─       $(Expr(:method, :(Main.f)))
│         $(Expr(:latestworld))
│         Main.f
│         $(Expr(:latestworld))
│   %5  = Main.f
│   %6  =   dynamic Core.Typeof(%5)
│   %7  =   builtin Core.svec(%6, Core.Any)
│   %8  =   builtin Core.svec()
│   %9  =   builtin Core.svec(%7, %8, $(QuoteNode(:(#= REPL[1]:1 =#))))
│         $(Expr(:method, :(Main.f), :(%9), CodeInfo(
1 ─     return 1
)))
│         $(Expr(:latestworld))
│   %12 = Main.f
└──       return %12
))))
```

This doesn't really make a semantic difference, but if `f` is a type, we
may now give a warning, so the prior definition would give the warning
twice
(#57311 (comment)).
We may want to consider rate-limiting the warning independently, but for
now at least give the correct number of warnings.

(cherry picked from commit 512eb5e)
@KristofferC KristofferC mentioned this pull request Feb 14, 2025
31 tasks
KristofferC added a commit that referenced this pull request Feb 17, 2025
Backported PRs:
- [x] #57346 <!-- lowering: Only try to define the method once -->
- [x] #57341 <!-- bpart: When backdating replace the entire bpart chain
-->
- [x] #57381 <!-- staticdata: Set min validation world to require world
-->
- [x] #57357 <!-- Only implicitly `using` Base, not Core -->
- [x] #57383 <!-- staticdata: Fix typo in recursive edge revalidation
-->
- [x] #57385 <!-- bpart: Move kind enum into its intended place -->
- [x] #57275 <!-- Compiler: fix unsoundness of getfield_tfunc on Tuple
Types -->
- [x] #57378 <!-- print admonition for auto-import only once per module
-->
- [x] #57392 <!-- [LateLowerGCFrame] fix PlaceGCFrameReset for
returns_twice -->
- [x] #57388 <!-- Bump JuliaSyntax to v1.0.2 -->
- [x] #57266 <!-- 🤖 [master] Bump the Statistics stdlib from d49c2bf to
77bd570 -->
- [x] #57395 <!-- lowering: fix has_fcall computation -->
- [x] #57204 <!-- Clarify mathematical definition of `gcd` -->
- [x] #56794 <!-- Make `Pairs` public -->
- [x] #57407 <!-- staticdata: corrected implementation of
jl_collect_new_roots -->
- [x] #57405 <!-- bpart: Also partition the export flag -->
- [x] #57420 <!-- Compiler: Fix check for IRShow definedness -->
- [x] #55875 <!-- fix `(-Inf)^-1` inconsistency -->
- [x] #57317 <!-- internals: add _defaultctor function for defining
ctors -->
- [x] #57406 <!-- bpart: Ignore guard bindings for ambiguity purposes
-->
- [x] #49933 <!-- Allow for :foreigncall to transition to GC safe
automatically -->
@KristofferC KristofferC removed the backport 1.12 Change should be backported to release-1.12 label Feb 17, 2025
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