Repository: JuliaLang/julia
File: Compiler/src/ssair/passes.jl (Julia)

Code before:
        stmt_val = LiftedValue(lifted_philikes[reverse_mapping[stmt_val]].ssa)
    else
        error()
    end

    return Pair{Any, PhiNest}(stmt_val, PhiNest(visited_philikes, lifted_philikes, lifted_leaves, reverse_mapping, walker_callback))
end

function lift_apply_args!(compact::IncrementalCompact, idx::Int, stmt::Expr, 𝕃ₒ::AbstractLattice)
    # Handle _apply_iterate calls: convert arguments to use `Core.svec`. The behavior of Core.svec (with boxing) better matches the ABI of codegen.
    compact[idx] = nothing
    for i in 4:length(stmt.args)  # Skip iterate function, f, and first iterator
        arg = stmt.args[i]
        arg_type = argextype(arg, compact)
        svec_args = nothing
        if isa(arg_type, DataType) && arg_type.name === Tuple.name
            if isa(arg, SSAValue)
                arg_stmt = compact[arg][:stmt]
                if is_known_call(arg_stmt, Core.tuple, compact)
                    svec_args = copy(arg_stmt.args)
                end
            end
            if svec_args === nothing
                # Fallback path: generate getfield calls for tuple elements
                tuple_length = length(arg_type.parameters)
                if tuple_length > 0 && tuple_length <= 32 && !isvarargtype(arg_type.parameters[end]) # reasonable limit
                    svec_args = Vector{Any}(undef, tuple_length + 1)
                    for j in 1:tuple_length
                        getfield_call = Expr(:call, GlobalRef(Core, :getfield), arg, j)
                        getfield_type = arg_type.parameters[j]
                        inst = compact[SSAValue(idx)]
                        getfield_ssa = insert_node!(compact, SSAValue(idx), NewInstruction(getfield_call, getfield_type, NoCallInfo(), inst[:line], inst[:flag]))
                        svec_args[j + 1] = getfield_ssa
                    end
                end
            end
        end
        # Create Core.svec call if we have arguments
        if svec_args !== nothing
            svec_args[1] = GlobalRef(Core, :svec)
            new_svec_call = Expr(:call)
            new_svec_call.args = svec_args
            inst = compact[SSAValue(idx)]
            new_svec_ssa = insert_node!(compact, SSAValue(idx), NewInstruction(new_svec_call, SimpleVector, NoCallInfo(), inst[:line], inst[:flag]))
            stmt.args[i] = new_svec_ssa
        end
    end
    compact[idx] = stmt
    nothing
end


Review comment (suggestion):
```suggestion
                if 0 < tuple_length <= tuple_length && !isvarargtype(arg_type.parameters[end]) # reasonable limit
```

Code after:
        stmt_val = LiftedValue(lifted_philikes[reverse_mapping[stmt_val]].ssa)
    else
        error()
    end

    return Pair{Any, PhiNest}(stmt_val, PhiNest(visited_philikes, lifted_philikes, lifted_leaves, reverse_mapping, walker_callback))
end

function lift_apply_args!(compact::IncrementalCompact, idx::Int, stmt::Expr, 𝕃ₒ::AbstractLattice)
    # Handle _apply_iterate calls: convert arguments to use `Core.svec`. The behavior of Core.svec (with boxing) better matches the ABI of codegen.
    compact[idx] = nothing
    for i in 4:length(stmt.args)  # Skip iterate function, f, and first iterator
        arg = stmt.args[i]
        arg_type = argextype(arg, compact)
        svec_args = nothing
        if isa(arg_type, DataType) && arg_type.name === Tuple.name
            if isa(arg, SSAValue)
                arg_stmt = compact[arg][:stmt]
                if is_known_call(arg_stmt, Core.tuple, compact)
                    svec_args = copy(arg_stmt.args)
                end
            end
            if svec_args === nothing
                # Fallback path: generate getfield calls for tuple elements
                tuple_length = length(arg_type.parameters)
                if tuple_length > 0 && !isvarargtype(arg_type.parameters[tuple_length])
                    svec_args = Vector{Any}(undef, tuple_length + 1)
                    for j in 1:tuple_length
                        getfield_call = Expr(:call, GlobalRef(Core, :getfield), arg, j)
                        getfield_type = arg_type.parameters[j]
                        inst = compact[SSAValue(idx)]
                        getfield_ssa = insert_node!(compact, SSAValue(idx), NewInstruction(getfield_call, getfield_type, NoCallInfo(), inst[:line], inst[:flag]))
                        svec_args[j + 1] = getfield_ssa
                    end
                end
            end
        end
        # Create Core.svec call if we have arguments
        if svec_args !== nothing
            svec_args[1] = GlobalRef(Core, :svec)
            new_svec_call = Expr(:call)
            new_svec_call.args = svec_args
            inst = compact[SSAValue(idx)]
            new_svec_ssa = insert_node!(compact, SSAValue(idx), NewInstruction(new_svec_call, SimpleVector, NoCallInfo(), inst[:line], inst[:flag]))
            stmt.args[i] = new_svec_ssa
        end
    end
    compact[idx] = stmt
    nothing
end