Skip to content

Recursion disables constant propagation #39915

Closed
@bramtayl

Description

@bramtayl

So, this is a very annoying problem, and one that people don't seem to really know about. It's best demonstrated by example:

using Base: tail

function my_getindex(a_tuple, which_ones)
    rest = my_getindex(tail(a_tuple), tail(which_ones))
    if first(which_ones)
        (first(a_tuple), rest...)
    else
        rest
    end
end

function my_getindex(a_tuple::Tuple{}, which_ones::Tuple{})
    ()
end

function constant_select(a_tuple)
    my_getindex(a_tuple, (true, false, true, false))
end

@code_warntype constant_select((1, 1.0, "a", :a))
# Body::Tuple{Int64,Vararg{Any,N} where N}

My naive understanding of what's going on is that when Julia recurs into my_getindex, the compiler detects that which_ones is still a tuple of Bools (just a smaller one), figures out that we are recurring, and bails on constant propagation.

I've always gotten the impression that this is an unfixable problem, but...if it's fixable, that would be super cool.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions