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 invalidations for Dicts from Static.jl #46490

Merged
merged 1 commit into from
Aug 29, 2022
Merged

Conversation

ranocha
Copy link
Member

@ranocha ranocha commented Aug 26, 2022

This should hopefully fix quite some invalidations coming from Static.jl.

Here is the code:
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.8.0 (2022-08-17)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

(@v1.8) pkg> activate --temp

(jl_PDrBpd) pkg> add Static
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `/tmp/jl_PDrBpd/Project.toml`
  [aedffcd0] + Static v0.7.6
    Updating `/tmp/jl_PDrBpd/Manifest.toml`
  [615f187c] + IfElse v0.1.1
  [aedffcd0] + Static v0.7.6

julia> using SnoopCompileCore

julia> invalidations = @snoopr using Static

julia> using SnoopCompile

julia> trees = invalidation_trees(invalidations)
7-element Vector{SnoopCompile.MethodInvalidations}:
...
 inserting !(::False) in Static at ~/.julia/packages/Static/sVI3g/src/Static.jl:427 invalidated:
...
                 161: signature Tuple{typeof(!), Any} triggered MethodInstance for setindex!(::Dict{_A, Nothing} where _A, ::Nothing, ::Any) (193 children)
                 162: signature Tuple{typeof(!), Any} triggered MethodInstance for setindex!(::Dict, ::Any, ::Any) (424 children)

Since !isequal(...) is used here in an if stament - and if statements only accept Bools -
it appears to be sane to assert that isequal returns a Bool here.

See also #46481

@oscardssmith
Copy link
Member

Rather than going 1 by 1 and type annotating every if, it seems to me like we might be able to teach type inference that anything that goes into an if statement must be a boolean. @JeffBezanson does that seem possible to you?

@ranocha
Copy link
Member Author

ranocha commented Aug 26, 2022

That would be nice, of course. However, that would basically mean annotating

if (!isequal(key, key0))::Bool

automatically but we need

if !(isequal(key, key0)::Bool)

in this case to fix the invalidations (stemming from a new definition of !).

@ChrisRackauckas
Copy link
Member

Also, I'd be very weary of "perfect is the enemy of good" here. We can come up with some super elegant solution that fixes all problems and is released in Julia v1.11, but really though, this is a nice small fix that will improve a lot of things so even if there's some big grand plans I'd want this kind of thing in 1.8.1 ASAP as it has major real-world user effects.

@gbaraldi
Copy link
Member

Don't we already assume that anything that goes to an if is a bool, and if it isn't we error? Or is that just a runtime check not a compile time one?

@ranocha
Copy link
Member Author

ranocha commented Aug 26, 2022

Don't we already assume that anything that goes to an if is a bool, and if it isn't we error? Or is that just a runtime check not a compile time one?

I do not know. Even if we do, that's not the problem this PR is intended to fix - the input of ! in the if block is the problem, not the output of !.

@timholy timholy added the compiler:latency Compiler latency label Aug 29, 2022
@timholy timholy merged commit 5c5af1f into JuliaLang:master Aug 29, 2022
@ranocha ranocha deleted the patch-7 branch August 29, 2022 15:49
@timholy
Copy link
Member

timholy commented Aug 29, 2022

Test failure https://buildkite.com/julialang/julia-master/builds/15063#0182d9a2-cb0e-4c97-95ec-f16ab36b312c looks like memory corruption; I ran

julia> badm = Ref{Any}()
Base.RefValue{Any}(#undef)

julia> for _ in 1:10^6
           A  = 10*I + sprandn(10, 10, 0.4)
           badm[] = copy(A)
           F1 = lu(A)
           any(isnan, F1.nzval) && error("NaN")
       end

and never got an instance of NaN.

@ranocha
Copy link
Member Author

ranocha commented Aug 29, 2022

Thanks a lot! Can we get a backport-1.8 label for this to get it into Julia v1.8.1?

@timholy timholy added the backport 1.8 Change should be backported to release-1.8 label Aug 29, 2022
@ranocha
Copy link
Member Author

ranocha commented Aug 29, 2022

Thanks!

ranocha added a commit to ranocha/julia that referenced this pull request Aug 30, 2022
KristofferC pushed a commit that referenced this pull request Aug 30, 2022
@vtjnash
Copy link
Member

vtjnash commented Aug 31, 2022

Rather than going 1 by 1 and type annotating every if, it seems to me like we might be able to teach type inference that anything that goes into an if statement must be a boolean. @JeffBezanson does that seem possible to you?

It is doable by moving the ! to the end, but doesn't read quite as cleanly:

julia> if false else # if !false
           1
       end

though has the advantage that parenthesis are never needed for it.

@KristofferC KristofferC removed the backport 1.8 Change should be backported to release-1.8 label Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:latency Compiler latency
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants