aviatesk / JET.jl

An experimental code analyzer for Julia. No need for additional type annotations.
https://aviatesk.github.io/JET.jl/dev/
MIT License
708 stars 29 forks source link

Possible regression in Julia 1.11 for throw block #643

Closed kbarros closed 13 hours ago

kbarros commented 3 days ago

The following code passes in 1.10, but flags an error in 1.11.0-rc1

using JET

function g(x)
    throw("Should JET ignore this? $x")
end

@test_opt unoptimize_throw_blocks=true g(1/3)

The error is:

[ Info: tracking Base
  ═════ 4 possible errors found ═════
  ┌ g(x::Float64) @ Main ./REPL[2]:2
  │┌ string(::String, ::Float64) @ Base ./strings/io.jl:189
  ││┌ print_to_string(::String, ::Float64) @ Base ./strings/io.jl:150
  │││┌ _unsafe_take!(io::IOBuffer) @ Base ./iobuffer.jl:504
  ││││┌ wrap(::Type{Array}, m::MemoryRef{UInt8}, l::Int64) @ Base ./array.jl:3099
  │││││ failed to optimize due to recursion: Base.wrap(::Type{Array}, ::MemoryRef{UInt8}, ::Int64)
  ││││└────────────────────
  │││┌ print_to_string(::String, ::Vararg{Any}) @ Base ./strings/io.jl:143
  ││││ runtime dispatch detected: Base._str_sizehint(%17::Any)::Int64
  │││└────────────────────
  │││┌ print_to_string(::String, ::Vararg{Any}) @ Base ./strings/io.jl:148
  ││││ runtime dispatch detected: print(%59::IOBuffer, %97::Any)::Any
  │││└────────────────────
  │││┌ string(::String, ::Int64, ::String, ::Tuple{Int64}, ::String, ::Int64, ::String, ::Int64, ::String) @ Base ./strings/io.jl:189
  ││││ failed to optimize due to recursion: string(::String, ::Int64, ::String, ::Tuple{Int64}, ::String, ::Int64, ::String, ::Int64, ::String)
  │││└────────────────────

My understanding of skip_unoptimized_throw_blocks = true (the default) is that it should ignore unoptimization in g, which is known to throw. Is that correct?

aviatesk commented 2 days ago

Yes, cases like this should be hidden by skip_unoptimized_throw_blocks=true, but due to the current implementation issue of report_opt, such cases can sometimes be reported. I will consider re-implementing report_opt. However, regarding this particular issue, it is expected to be fixed in v1.11 rc2 and the release version without waiting for a fix on the JET side (xref: https://github.com/JuliaLang/julia/pull/54942).

kbarros commented 5 hours ago

Thanks! Is it possible to also make JET detect error as well as throw? In my actual use case, the exception originates with an error:

using JET

function g(x)
    error("Please ignore this: $x")
end

@test_opt unoptimize_throw_blocks=true g(1/3)