Skip to content

Conversation

@nsajko
Copy link
Member

@nsajko nsajko commented Apr 1, 2025

Otherwise eltype is inferred as merely DataType. Too bad that constprop doesn't fix this on its own.

This will make the sysimage more resistant to invalidation, once JuliaLang/julia updates LinearAlgebra.

@nsajko
Copy link
Member Author

nsajko commented Apr 1, 2025

The change eliminates all invalidations in the sysimage from running this code as a user:

struct I <: Integer end
function Base.Int8(::I) end
function Base.UInt8(::I) end

Reproducer:

./julia -E 'using SnoopCompileCore; i = (@snoop_invalidations (struct I <: Integer end; function Base.Int8(::I) end; function Base.UInt8(::I) end)); using SnoopCompile; length(uinvalidated(i))'

The eliminated invalidations:

{
    "invalidation_count": 8,
    "trees": [
        {
            "method": "Int8(::Main.RawInvalidations.I) @ Main.RawInvalidations ~/invalidations/script/reproducer.jl:11",
            "reason": "inserting",
            "mt_backedges": [
                {
                    "type": "Tuple{Type{Int8}, Integer}",
                    "tree": {
                        "method_instance": {
                            "method": "convert(::Type{T}, x::Number) where T<:Number @ Base number.jl:7",
                            "method_instance": "MethodInstance for convert(::Type{Int8}, ::Integer)"
                        },
                        "children": [
                            {
                                "method_instance": {
                                    "method": "fill!(a::Union{Array{Int8}, Array{UInt8}}, x::Integer) @ Base array.jl:430",
                                    "method_instance": "MethodInstance for fill!(::Union{Matrix{Int8}, Matrix{UInt8}}, ::Integer)"
                                },
                                "children": [
                                    {
                                        "method_instance": {
                                            "method": "ones(::Type{T}, dims::NTuple{N, Integer}) where {T, N} @ Base array.jl:594",
                                            "method_instance": "MethodInstance for ones(::DataType, ::Tuple{Int64, Int64})"
                                        },
                                        "children": [
                                            {
                                                "method_instance": {
                                                    "method": "ones(::Type{T}, dims::Union{Integer, AbstractUnitRange}...) where T @ Base array.jl:591",
                                                    "method_instance": "MethodInstance for ones(::DataType, ::Int64, ::Int64)"
                                                },
                                                "children": [
                                                    {
                                                        "method_instance": {
                                                            "method": "var\"#peakflops#332\"(eltype::DataType, ntrials::Integer, parallel::Bool, ::typeof(LinearAlgebra.peakflops), n::Integer) @ LinearAlgebra ~/tmp/jl/jl/master-69a22cf4bc/share/julia/stdlib/v1.13/LinearAlgebra/src/LinearAlgebra.jl:753",
                                                            "method_instance": "MethodInstance for LinearAlgebra.var\"#peakflops#332\"(::DataType, ::Int64, ::Bool, ::typeof(LinearAlgebra.peakflops), ::Int64)"
                                                        },
                                                        "children": [
                                                        ]
                                                    },
                                                    {
                                                        "method_instance": {
                                                            "method": "var\"#peakflops#332\"(eltype::DataType, ntrials::Integer, parallel::Bool, ::typeof(LinearAlgebra.peakflops), n::Integer) @ LinearAlgebra ~/tmp/jl/jl/master-69a22cf4bc/share/julia/stdlib/v1.13/LinearAlgebra/src/LinearAlgebra.jl:753",
                                                            "method_instance": "MethodInstance for LinearAlgebra.var\"#peakflops#332\"(::DataType, ::Int64, ::Bool, ::typeof(LinearAlgebra.peakflops), ::Int64)"
                                                        },
                                                        "children": [
                                                        ]
                                                    },
                                                    {
                                                        "method_instance": {
                                                            "method": "var\"#peakflops#332\"(eltype::DataType, ntrials::Integer, parallel::Bool, ::typeof(LinearAlgebra.peakflops), n::Integer) @ LinearAlgebra ~/tmp/jl/jl/master-69a22cf4bc/share/julia/stdlib/v1.13/LinearAlgebra/src/LinearAlgebra.jl:753",
                                                            "method_instance": "MethodInstance for LinearAlgebra.var\"#peakflops#332\"(::DataType, ::Int64, ::Bool, ::typeof(LinearAlgebra.peakflops), ::Int64)"
                                                        },
                                                        "children": [
                                                        ]
                                                    },
                                                    {
                                                        "method_instance": {
                                                            "method": "var\"#peakflops#332\"(eltype::DataType, ntrials::Integer, parallel::Bool, ::typeof(LinearAlgebra.peakflops), n::Integer) @ LinearAlgebra ~/tmp/jl/jl/master-69a22cf4bc/share/julia/stdlib/v1.13/LinearAlgebra/src/LinearAlgebra.jl:753",
                                                            "method_instance": "MethodInstance for LinearAlgebra.var\"#peakflops#332\"(::DataType, ::Int64, ::Bool, ::typeof(LinearAlgebra.peakflops), ::Int64)"
                                                        },
                                                        "children": [
                                                            {
                                                                "method_instance": {
                                                                    "method": "peakflops(n::Integer; eltype, ntrials, parallel) @ LinearAlgebra ~/tmp/jl/jl/master-69a22cf4bc/share/julia/stdlib/v1.13/LinearAlgebra/src/LinearAlgebra.jl:753",
                                                                    "method_instance": "MethodInstance for LinearAlgebra.peakflops(::Int64)"
                                                                },
                                                                "children": [
                                                                ]
                                                            },
                                                            {
                                                                "method_instance": {
                                                                    "method": "peakflops(n::Integer; eltype, ntrials, parallel) @ LinearAlgebra ~/tmp/jl/jl/master-69a22cf4bc/share/julia/stdlib/v1.13/LinearAlgebra/src/LinearAlgebra.jl:753",
                                                                    "method_instance": "MethodInstance for LinearAlgebra.peakflops(::Int64)"
                                                                },
                                                                "children": [
                                                                    {
                                                                        "method_instance": {
                                                                            "method": "peakflops(; ...) @ LinearAlgebra ~/tmp/jl/jl/master-69a22cf4bc/share/julia/stdlib/v1.13/LinearAlgebra/src/LinearAlgebra.jl:753",
                                                                            "method_instance": "MethodInstance for LinearAlgebra.peakflops()"
                                                                        },
                                                                        "children": [
                                                                        ]
                                                                    }
                                                                ]
                                                            }
                                                        ]
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                }
            ],
            "backedges": [
            ],
            "mt_cache": [
            ],
            "mt_disable": [
            ]
        },
        {
            "method": "UInt8(::Main.RawInvalidations.I) @ Main.RawInvalidations ~/invalidations/script/reproducer.jl:12",
            "reason": "inserting",
            "mt_backedges": [
                {
                    "type": "Tuple{Type{UInt8}, Integer}",
                    "tree": {
                        "method_instance": {
                            "method": "convert(::Type{T}, x::Number) where T<:Number @ Base number.jl:7",
                            "method_instance": "MethodInstance for convert(::Type{UInt8}, ::Integer)"
                        },
                        "children": [
                            {
                                "method_instance": {
                                    "method": "fill!(a::Union{Array{Int8}, Array{UInt8}}, x::Integer) @ Base array.jl:430",
                                    "method_instance": "MethodInstance for fill!(::Union{Matrix{Int8}, Matrix{UInt8}}, ::Integer)"
                                },
                                "children": [
                                ]
                            }
                        ]
                    }
                }
            ],
            "backedges": [
            ],
            "mt_cache": [
            ],
            "mt_disable": [
            ]
        }
    ]
}

@KristofferC
Copy link
Member

Is this called when the sysimage is created? I don't see where (and that is required AFAIU to cause invalidations).

@nsajko
Copy link
Member Author

nsajko commented Apr 1, 2025

No idea why peakflops() is in the sysimage, LinearAlgebra is not mentioned in https://github.com/JuliaLang/julia/blob/master/contrib/generate_precompile.jl.

I suspect that any method callable without arguments gets precompiled. No idea where this is implemented, if so.

Otherwise `eltype` is inferred as merely `DataType`. Too bad that
constprop doesn't fix this on its own.

This makes the sysimage more resistant to invalidation.
@nsajko
Copy link
Member Author

nsajko commented Apr 2, 2025

Fixed the merge conflict.

@codecov
Copy link

codecov bot commented Apr 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.97%. Comparing base (e53b50c) to head (781eb5d).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1253      +/-   ##
==========================================
- Coverage   92.03%   91.97%   -0.06%     
==========================================
  Files          34       34              
  Lines       15459    15471      +12     
==========================================
+ Hits        14227    14230       +3     
- Misses       1232     1241       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nsajko
Copy link
Member Author

nsajko commented Apr 3, 2025

bump

@jishnub
Copy link
Member

jishnub commented Apr 3, 2025

The change looks ok to me

@ViralBShah ViralBShah merged commit e3e9987 into JuliaLang:master Apr 3, 2025
3 of 4 checks passed
@nsajko nsajko deleted the peakflops_type_method_static_parameter branch April 3, 2025 13:13
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