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 of isinf from Static.jl #46493

Merged
merged 3 commits into from
Aug 30, 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:
...
                 155: signature Tuple{typeof(!), Any} triggered MethodInstance for isinf(::AbstractFloat) (30 children)

help?> isnan
search: isnan isinteractive isignorable issubnormal DimensionMismatch

  isnan(f) -> Bool

  Test whether a number value is a NaN, an indeterminate value which is neither an infinity nor a finite number
  ("not a number").

  See also: iszero, isone, isinf, ismissing.

help?> isfinite
search: isfinite

  isfinite(f) -> Bool

  Test whether a number is finite.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> isfinite(5)
  true

  julia> isfinite(NaN32)
  false

Since isnan and isfinite are documented to return Bools, it appears to be sane to assert that they do here.

@timholy
Copy link
Sponsor Member

timholy commented Aug 29, 2022

While it is documented to return a Bool, we also have

julia> isnan(missing)
missing

in Base. We could assert it is Union{Bool, Missing} but I wonder if there are packages that implement their own non-boolean logic? For #46490 it's safe because

julia> isequal(missing, missing)
true

julia> isequal(missing, 'a')
false

but it's a little less safe here.

@ranocha
Copy link
Member Author

ranocha commented Aug 30, 2022

But we have

julia> missing isa Real
false

so

julia> @which isinf(missing)
isinf(::Missing) in Base at missing.jl:101

does not use the method I changed here.

@ranocha
Copy link
Member Author

ranocha commented Aug 30, 2022

I also checked ForwardDiff.jl, which has

julia> using ForwardDiff

julia> x = ForwardDiff.Dual{Float64}(1.0, (0.0, 0.0))
Dual{Float64}(1.0,0.0,0.0)

julia> x isa Real
true

They implement their own logic for isinf etc.

julia> @which isinf(x)
isinf(d::ForwardDiff.Dual) in ForwardDiff at ~/.julia/packages/ForwardDiff/pDtsf/src/dual.jl:384

julia> @which isnan(x)
isnan(d::ForwardDiff.Dual) in ForwardDiff at ~/.julia/packages/ForwardDiff/pDtsf/src/dual.jl:384

julia> @which isfinite(x)
isfinite(d::ForwardDiff.Dual) in ForwardDiff at ~/.julia/packages/ForwardDiff/pDtsf/src/dual.jl:384

@ranocha
Copy link
Member Author

ranocha commented Aug 30, 2022

But I think you are right, this may be not a good approach to fix the invalidations. There could be other packages implementing their own special return types, e.g., some SIMD types. I will look into this later.

@ranocha ranocha marked this pull request as draft August 30, 2022 07:28
@ranocha ranocha marked this pull request as ready for review August 30, 2022 10:56
@ranocha
Copy link
Member Author

ranocha commented Aug 30, 2022

I was able to fix the invalidations one level higher. As far as I can tell, this should be save since it is

  • restricted to AbstractFloats
  • used in a Boolean context

I suggest the labels latency and backport-1.8 for this PR.

@timholy timholy added compiler:latency Compiler latency backport 1.8 Change should be backported to release-1.8 labels Aug 30, 2022
@vtjnash vtjnash merged commit c2a1650 into JuliaLang:master Aug 30, 2022
KristofferC pushed a commit that referenced this pull request Aug 30, 2022
@ranocha ranocha deleted the patch-9 branch August 31, 2022 04:10
@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.

None yet

4 participants