Skip to content

Commit 144de95

Browse files
authored
IRShow: Print arg0 type when necessary to disambiguate invoke (JuliaLang#58893)
When invoking any "functor-like", such as a closure: ```julia bar(x) = @noinline ((y)->x+y)(x) ``` our IR printing was not showing the arg0 invoked, even when it is required to determine which MethodInstance this is invoking. Before: ```julia julia> @code_typed optimize=true bar(1) CodeInfo( 1 ─ %1 = %new(var"#bar#mmtk#2#bar#mmtk#3"{Int64}, x)::var"#bar#mmtk#2#bar#mmtk#3"{Int64} │ %2 = invoke %1(x::Int64)::Int64 └── return %2 ) => Int64 ``` After: ```julia julia> @code_typed optimize=true bar(1) CodeInfo( 1 ─ %1 = %new(var"#bar#mmtk#2#bar#mmtk#3"{Int64}, x)::var"#bar#mmtk#2#bar#mmtk#3"{Int64} │ %2 = invoke (%1::var"#bar#mmtk#2#bar#mmtk#3"{Int64})(x::Int64)::Int64 └── return %2 ) => Int64 ```
1 parent 04138bf commit 144de95

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Compiler/src/ssair/show.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,30 @@ function print_stmt(io::IO, idx::Int, @nospecialize(stmt), code::Union{IRCode,Co
105105
printstyled(io, "dynamic invoke "; color = :yellow)
106106
abi = (ci::Core.MethodInstance).specTypes
107107
end
108-
show_unquoted(io, stmt.args[2], indent)
109-
print(io, "(")
110108
# XXX: this is wrong if `sig` is not a concretetype method
111109
# more correct would be to use `fieldtype(sig, i)`, but that would obscure / discard Varargs information in show
112110
sig = abi == Tuple ? Core.svec() : Base.unwrap_unionall(abi).parameters::Core.SimpleVector
111+
f = stmt.args[2]
112+
ft = maybe_argextype(f, code, sptypes)
113+
114+
# We can elide the type for arg0 if it...
115+
skip_ftype = (length(sig) == 0) # doesn't exist...
116+
skip_ftype = skip_ftype || (
117+
# ... or, f prints as a user-accessible value...
118+
(f isa GlobalRef) &&
119+
# ... and matches the value of the singleton type of the invoked MethodInstance
120+
(singleton_type(ft) === singleton_type(sig[1]) !== nothing)
121+
)
122+
if skip_ftype
123+
show_unquoted(io, f, indent)
124+
else
125+
print(io, "(")
126+
show_unquoted(io, f, indent)
127+
print(io, "::", sig[1], ")")
128+
end
129+
130+
# Print the remaining arguments (with type annotations from the invoked MethodInstance)
131+
print(io, "(")
113132
print_arg(i) = sprint(; context=io) do io
114133
show_unquoted(io, stmt.args[i], indent)
115134
if (i - 1) <= length(sig)

0 commit comments

Comments
 (0)