-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
reflection.jl
204 lines (173 loc) · 6.28 KB
/
reflection.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using Core: CodeInfo, Typeof
using Core.Compiler: InferenceState, widenconst, svec
using InteractiveUtils: typesof
worldcounter() = ccall(:jl_get_world_counter, UInt, ())
isprecompiling() = ccall(:jl_generating_output, Cint, ()) == 1
struct TypedMeta
frame::InferenceState
method::Method
code::CodeInfo
ret
end
function Base.show(io::IO, meta::TypedMeta)
print(io, "Typed metadata for ")
print(io, meta.method)
end
define_typeinf_code2() = isprecompiling() ||
@eval Core.Compiler function typeinf_code2(method::Method, @nospecialize(atypes), sparams::SimpleVector, run_optimizer::Bool, params::Params)
if $(VERSION >= v"1.2-")
code = specialize_method(method, atypes, sparams)
else
code = code_for_method(method, atypes, sparams, params.world)
end
code === nothing && return (nothing, Any)
ccall(:jl_typeinf_begin, Cvoid, ())
result = InferenceResult(code)
frame = InferenceState(result, false, params)
frame === nothing && return (nothing, Any)
if typeinf(frame) && run_optimizer
opt = OptimizationState(frame)
optimize(opt, result.result)
opt.src.inferred = true
end
ccall(:jl_typeinf_end, Cvoid, ())
frame.inferred || return (nothing, Any)
return frame
end
"""
typed_meta(Tuple{...})
Same as [`@meta`](@ref), but represents the method after type inference. IR
constructed with typed metadata will have type annotations.
See also [`@typed_meta`](@ref).
julia> IRTools.typed_meta(Tuple{typeof(gcd),Int,Int})
Typed metadata for gcd(a::T, b::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at intfuncs.jl:31
"""
function typed_meta(T; world = worldcounter(), optimize = false)
F = T.parameters[1]
F isa DataType && (F.name.module === Core.Compiler ||
F <: Core.Builtin ||
F <: Core.Builtin) && return nothing
_methods = Base._methods_by_ftype(T, -1, world)
length(_methods) == 1 || return nothing
type_signature, sps, method = first(_methods)
params = Core.Compiler.Params(world)
frame = Core.Compiler.typeinf_code2(method, type_signature, sps, optimize, params)
ci = frame.src
ci.inferred = true
ci.method_for_inference_limit_heuristics = method
if ci.ssavaluetypes == 0 # constant return; IRCode doesn't like this
ci.ssavaluetypes = Any[Any]
end
return TypedMeta(frame, method, ci, widenconst(frame.result.result))
end
struct Meta
method::Method
code::CodeInfo
nargs::Int
sparams
end
function Base.show(io::IO, meta::Meta)
print(io, "Metadata for ")
print(io, meta.method)
end
# Workaround for what appears to be a Base bug
untvar(t::TypeVar) = t.ub
untvar(x) = x
"""
meta(Tuple{...})
Construct metadata for a given method signature. Metadata can then be used to
construct [`IR`](@ref) or used to perform other reflection on the method.
See also [`@meta`](@ref), [`typed_meta`](@ref).
julia> IRTools.meta(Tuple{typeof(gcd),Int,Int})
Metadata for gcd(a::T, b::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at intfuncs.jl:31
"""
function meta(T; world = worldcounter())
F = T.parameters[1]
F == typeof(invoke) && return invoke_meta(T; world = world)
F isa DataType && (F.name.module === Core.Compiler ||
F <: Core.Builtin ||
F <: Core.Builtin) && return nothing
_methods = Base._methods_by_ftype(T, -1, world)
length(_methods) == 0 && return nothing
type_signature, sps, method = last(_methods)
sps = svec(map(untvar, sps)...)
@static if VERSION >= v"1.2-"
mi = Core.Compiler.specialize_method(method, type_signature, sps)
ci = Base.isgenerated(mi) ? Core.Compiler.get_staged(mi) : Base.uncompressed_ast(method)
else
mi = Core.Compiler.code_for_method(method, type_signature, sps, world, false)
ci = Base.isgenerated(mi) ? Core.Compiler.get_staged(mi) : Base.uncompressed_ast(mi)
end
ci.method_for_inference_limit_heuristics = method
if isdefined(ci, :edges)
ci.edges = Core.MethodInstance[mi]
end
Base.Meta.partially_inline!(ci.code, [], method.sig, Any[sps...], 0, 0, :propagate)
Meta(method, ci, method.nargs, sps)
end
function invoke_tweaks!(ci::CodeInfo)
ci.slotnames = [:invoke, ci.slotnames[1], :T, ci.slotnames[2:end]...]
ci.slotflags = [0x00, ci.slotflags[1], 0x00, ci.slotflags[2:end]...]
ci.code = map(ci.code) do x
prewalk(x) do x
x isa SlotNumber ? SlotNumber(x.id == 1 ? 2 : x.id+2) : x
end
end
end
function invoke_meta(T; world)
F = T.parameters[2]
A = T.parameters[3]::Type{<:Type{<:Tuple}}
T = Tuple{F,A.parameters[1].parameters...}
m = meta(T, world = world)
invoke_tweaks!(m.code)
return Meta(m.method, m.code, m.nargs+2, m.sparams)
end
"""
@meta f(args...)
Convenience macro for retrieving metadata without writing a full type signature.
julia> IRTools.@meta gcd(10, 5)
Metadata for gcd(a::T, b::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at intfuncs.jl:31
"""
macro meta(ex)
isexpr(ex, :call) || error("@meta f(args...)")
f, args = ex.args[1], ex.args[2:end]
:(meta(typesof($(esc.((f, args...))...))))
end
"""
@typed_meta f(args...)
Convenience macro for retrieving typed metadata without writing a full type signature.
julia> IRTools.@typed_meta gcd(10, 5)
Typed metadata for gcd(a::T, b::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at intfuncs.jl:31
"""
macro typed_meta(ex)
isexpr(ex, :call) || error("@meta f(args...)")
f, args = ex.args[1], ex.args[2:end]
:(typed_meta(typesof($(esc.((f, args...))...))))
end
function code_ir(f, T)
m = meta(Tuple{Typeof(f),T.parameters...})
return IR(m)
end
function code_irm(ex)
isexpr(ex, :call) || error("@code_ir f(args...)")
f, args = ex.args[1], ex.args[2:end]
:($code_ir($(esc(f)), typesof($(esc.(args)...))))
end
"""
@code_ir f(args...)
Convenience macro similar to `@code_lowered` or `@code_typed`. Retrieves the IR
for the given function call.
julia> @code_ir gcd(10, 5)
1: (%1, %2, %3)
%4 = %2 == 0
br 4 unless %4
2: ...
"""
macro code_ir(ex)
code_irm(ex)
end
codeinfo(m::Meta) = m.code
codeinfo(m::TypedMeta) = m.code
function argnames!(meta, names...)
meta.code.slotnames = [names...]
end