julia icon indicating copy to clipboard operation
julia copied to clipboard

Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.

Open Alexander-Barth opened this issue 1 month ago • 3 comments

In julia 1.12.2 (Linux), I have this warning:

@inline __tf(result, time) = result
@inline function __tf(result, time::T, d1, dn...) where {T}
    return if d1 == 0
        __tf((result..., T(0)), 0, dn...)
    else
        p, time2 = divrem(time, d1, RoundDown)
        __tf((result..., T(p)), time2, dn...)
    end
end

@inline function _timetuple2(time, divi)
    return __tf((), time, divi...)
end

function timetuplefrac2(time)
    # 10 element tuple
    divi = (1.0f0, 0.041666668f0, 0.00069444446f0, 1.1574074f-5, 1.1574074f-8,
            1.1574074f-11, 1.1574073f-14, 1.1574074f-17,
            1.15740735f-20,
            1.1574074f-23,
            )

    return _timetuple2(time, divi)
end

time = 51545.0f0
timetuplefrac2(time)

The full output is:

julia> timetuplefrac2(time)
Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.
Internal error: during type inference of
timetuplefrac2(Float32)
Encountered stack overflow.
This might be caused by recursion over very long tuples or argument lists.
(51545.0f0, 0.0f0, 0.0f0, 0.0f0, 0.0f0, 0.0f0, 0.0f0, 0.0f0, 0.0f0, 0.0f0)

It used to work in julia 1.11 without this warning. I am wondering if a 10-element tuple, is already considered a very long tuple? Or what is the maximum number of inlining allowed?

Ref: https://github.com/JuliaGeo/CFTime.jl/issues/63

Alexander-Barth avatar Nov 27 '25 17:11 Alexander-Barth

Surprisingly, there is no warning if all the variables are Float64.

@inline __tf(result, time) = result
@inline function __tf(result, time::T, d1, dn...) where {T}
    return if d1 == 0
        __tf((result..., T(0)), 0, dn...)
    else
        p, time2 = divrem(time, d1, RoundDown)
        __tf((result..., T(p)), time2, dn...)
    end
end

@inline function _timetuple2(time, divi)
    return __tf((), time, divi...)
end

function timetuplefrac2(time)
    # 10 element tuple
    divi = (1.0e0, 0.041666668e0, 0.00069444446e0, 1.1574074e-5, 1.1574074e-8,
            1.1574074e-11, 1.1574073e-14, 1.1574074e-17,
            1.15740735e-20,
            1.1574074e-23,
            )

    return _timetuple2(time, divi)
end

time = 51545.0
timetuplefrac2(time)

Alexander-Barth avatar Dec 01 '25 13:12 Alexander-Barth

I bisected the new warning to https://github.com/JuliaLang/julia/pull/49637 . this doesn't really say much about root cause but at least it might explain the difference between float64 and float32

adienes avatar Dec 01 '25 18:12 adienes

The recursion is

Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.
ERROR: StackOverflowError:
Stacktrace:
  [1] finish_current_bb!(compact::Compiler.IncrementalCompact, active_bb::Int64, old_result_idx::Int64, unreachable::Bool)
    @ Compiler ./../usr/share/julia/Compiler/src/ssair/ir.jl:1736
  [2] iterate_compact(compact::Compiler.IncrementalCompact)
    @ Compiler ./../usr/share/julia/Compiler/src/ssair/ir.jl:1905
  [3] iterate_compact(compact::Compiler.IncrementalCompact) (repeats 9497 times)
    @ Compiler ./../usr/share/julia/Compiler/src/ssair/ir.jl:1906
  [4] iterate
    @ ./../usr/share/julia/Compiler/src/ssair/ir.jl:1862 [inlined]
  [5] adce_pass!(ir::Compiler.IRCode, inlining::Compiler.InliningState{Compiler.NativeInterpreter})
    @ Compiler ./../usr/share/julia/Compiler/src/ssair/passes.jl:2147
  [6] run_passes_ipo_safe(ci::Core.CodeInfo, sv::Compiler.OptimizationState{Compiler.NativeInterpreter}, optimize_until::Nothing)
    @ Compiler ./../usr/share/julia/Compiler/src/optimize.jl:1013
  [7] run_passes_ipo_safe
    @ ./../usr/share/julia/Compiler/src/optimize.jl:1027 [inlined]
  [8] optimize
    @ ./../usr/share/julia/Compiler/src/optimize.jl:1002 [inlined]
  [9] typeinf_frame(interp::Compiler.NativeInterpreter, mi::Core.MethodInstance, run_optimizer::Bool)
    @ Compiler ./../usr/share/julia/Compiler/src/typeinfer.jl:1123
 [10] typeinf_code
    @ ./../usr/share/julia/Compiler/src/typeinfer.jl:1070 [inlined]
 [11] typeinf_code(interp::Compiler.NativeInterpreter, match::Core.MethodMatch, run_optimizer::Bool)
    @ Compiler ./../usr/share/julia/Compiler/src/typeinfer.jl:1064

gbaraldi avatar Dec 01 '25 19:12 gbaraldi