Cthulhu.jl icon indicating copy to clipboard operation
Cthulhu.jl copied to clipboard

Displays long chains of gotos not displayed by code-typed

Open oxinabox opened this issue 2 years ago • 3 comments

I think this might be something to do with the function doing a lot of inlining. Consider code-typed:

julia> @code_typed (x->[x])(1)
CodeInfo(
1 ─ %1 = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Vector{Int64}, svec(Any, Int64), 0, :(:ccall), Vector{Int64}, 1, 1))::Vector{Int64}
└──      goto #3 if not true
2 ─      Base.arrayset(false, %1, x, 1)::Vector{Int64}
3 ┄      goto #4
4 ─      return %1
) => Vector{Int64}

vs descend

julia> @descend (x->[x])(1)
(::var"#23#24")(x) @ Main REPL[42]:1
  ∘ ── %0 = invoke #23(::Int64)::Vector{Int64}                                                 
1 1 ── %1 = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Vector{Int64}, svec(Any, Int64), 0, :(:ccall), Vector{Int64}, 1, 1))::Vector{Int64}
  2 ──      goto #3                                                                    ││╻╷╷╷╷   LinearIndices
  3 ──      goto #4                                                                    │││┃│││    axes
  4 ──      goto #5                                                                    ││││┃│      map
  5 ──      goto #6                                                                    │││││   
  6 ──      goto #7                                                                    ││││    
  7 ──      goto #8                                                                    │││     
  8 ──      nothing::Nothing                                                           │       
  9 ──      goto #10                                                                   │││╻╷╷╷╷   axes1
  10 ─      goto #11                                                                   ││││┃││     axes
  11 ─      goto #12                                                                   │││││┃       oneto
  12 ─      goto #13                                                                   │││││   
  13 ─      goto #14                                                                   ││││    
  14 ─      goto #15                                                                   │││     
  15 ─      nothing::Nothing                                                           │       
  16 ─      nothing::Nothing                                                           │       
  17 ─      Base.arrayset(false, %1, x, 1)::Vector{Int64}                              ││╻       setindex!
  18 ─      goto #19                                                                   ││      
  19 ─      return %1                                                                  │       
Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark.
Toggles: [o]ptimize, [w]arn, [h]ide type-stable statements, [d]ebuginfo, [r]emarks, [e]ffects, [i]nlining costs, [t]ype annotations, [s]yntax highlight for Source/LLVM/Native.
Show: [S]ource code, [A]ST, [T]yped code, [L]LVM IR, [N]ative code
Actions: [E]dit source code, [R]evise and redisplay
Advanced: dump [P]arams cache.

I assume this is to do with the info shown on the right (you will need to scroll across to see on github). I think we should be willing to do some kind of goto compression (to match code_typed) at least by default

oxinabox avatar Feb 27 '23 07:02 oxinabox

@oscardssmith this is why we were seeing lots of gotos. It wasn't the optimizer missing a compression step on nightly, it's Cthulu apparently it always does this even on 1.8

oxinabox avatar Feb 27 '23 07:02 oxinabox

I'm interested in why @code_typed doesn't have this; is there a list of optimizations that are desynchronized between Cthulhu and Base?

staticfloat avatar Mar 02 '23 15:03 staticfloat

I'm interested in why @code_typed doesn't have this; is there a list of optimizations that are desynchronized between Cthulhu and Base?

There's no extra optimizations, but code_typed converts back to legacy format (without basic blocks), so the basic block get recomputed much more frequently, which can have the same effects. We do have BB optimization passes we could run that are not enabled by default.

Keno avatar Mar 02 '23 16:03 Keno