Skip to content

Const-propagation through ntuple worse than on 1.12 #61552

@topolarity

Description

@topolarity
julia> function _all_ndims()
           M = (x::Array{Float64,3},y::Array{Float64,2})
           return ntuple((i)->ndims(M[i]), Val(2))
       end
julia> code_typed(_all_ndims, (); optimize=true)
1-element Vector{Any}:
 CodeInfo(
1%1  = Main.x::Any
│           builtin Core.typeassert(%1, Array{Float64, 3})::Array{Float64, 3}%3  = π (%1, Array{Float64, 3})
│   %4  = Main.y::Any
│           builtin Core.typeassert(%4, Matrix{Float64})::Matrix{Float64}%6  = π (%4, Matrix{Float64})
│   %7  =    invoke Main.ndims(%3::Array{Float64, 3})::Int64
└──       goto #2
2 ─       goto #3
3 ─       goto #4
4%11 =    invoke Main.ndims(%6::Matrix{Float64})::Int64
└──       goto #5
5 ─       goto #6
6%14 =   builtin Core.tuple(%7, %11)::Tuple{Int64, Int64}
└──       goto #7
7return %14
) => Tuple{Int64, Int64

compare to 1.12:

julia> code_typed(_all_ndims, (); optimize=true)
1-element Vector{Any}:
 CodeInfo(
1%1 = Main.x::Any
│          builtin Core.typeassert(%1, Array{Float64, 3})::Array{Float64, 3}%3 = Main.y::Any
│          builtin Core.typeassert(%3, Matrix{Float64})::Matrix{Float64}
└──      return (3, 2)
) => Tuple{Int64, Int64}

I don't know why the optimizer is unable to replace invoke Main.ndims(%6::Array{Float64, 3}) with the constant result.

It is able to do it if the array types are homogeneous:

julia> _all_ndims() = begin
                 M = (x::Array{Float64,3},y::Array{Float64,3})
                 return ntuple((i)->ndims(M[i]), Val(2))
             end
julia> code_typed(_all_ndims, ())
1-element Vector{Any}:
 CodeInfo(
1%1 = Main.x::Any
│          builtin Core.typeassert(%1, Array{Float64, 3})::Array{Float64, 3}%3 = Main.y::Any
│          builtin Core.typeassert(%3, Array{Float64, 3})::Array{Float64, 3}
└──      return (3, 3)
) => Tuple{Int64, Int64}

Metadata

Metadata

Assignees

No one assigned

    Labels

    regressionRegression in behavior compared to a previous version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions