Skip to content

Commit f72d616

Browse files
authored
Add timing @zones for profiling each pass (#69)
1 parent ece589d commit f72d616

File tree

8 files changed

+38
-21
lines changed

8 files changed

+38
-21
lines changed

src/closure_conversion.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ Invariants:
580580
* This pass must not introduce new K"Identifier" - only K"BindingId".
581581
* Any new binding IDs must be added to the enclosing lambda locals
582582
"""
583-
function convert_closures(ctx::VariableAnalysisContext, ex)
583+
@fzone "JL: closures" function convert_closures(ctx::VariableAnalysisContext, ex)
584584
ctx = ClosureConversionCtx(ctx.graph, ctx.bindings, ctx.mod,
585585
ctx.closure_bindings, ex.lambda_bindings)
586586
ex1 = closure_convert_lambda(ctx, ex)

src/compat.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function expr_to_syntaxtree(@nospecialize(e), lnn::Union{LineNumberNode, Nothing
2929
expr_to_syntaxtree(graph, e, lnn)
3030
end
3131

32-
function expr_to_syntaxtree(ctx, @nospecialize(e), lnn::Union{LineNumberNode, Nothing})
32+
@fzone "JL: expr_to_syntaxtree" function expr_to_syntaxtree(ctx, @nospecialize(e), lnn::Union{LineNumberNode, Nothing})
3333
graph = syntax_graph(ctx)
3434
toplevel_src = if isnothing(lnn)
3535
# Provenance sinkhole for all nodes until we hit a linenode

src/desugaring.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4586,7 +4586,7 @@ function expand_forms_2(ctx::StatementListCtx, args...)
45864586
expand_forms_2(ctx.ctx, args...)
45874587
end
45884588

4589-
function expand_forms_2(ctx::MacroExpansionContext, ex::SyntaxTree)
4589+
@fzone "JL: desugar" function expand_forms_2(ctx::MacroExpansionContext, ex::SyntaxTree)
45904590
ctx1 = DesugaringContext(ctx, ctx.expr_compat_mode)
45914591
ex1 = expand_forms_2(ctx1, reparent(ctx1, ex))
45924592
ctx1, ex1

src/eval.jl

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function to_code_info(ex, mod, funcname, slots)
156156

157157
stmt_offset = length(stmts)
158158
for stmt in children(ex)
159-
push!(stmts, to_lowered_expr(mod, stmt, stmt_offset))
159+
push!(stmts, _to_lowered_expr(mod, stmt, stmt_offset))
160160
add_ir_debug_info!(current_codelocs_stack, stmt)
161161
end
162162

@@ -223,7 +223,11 @@ function to_code_info(ex, mod, funcname, slots)
223223
)
224224
end
225225

226-
function to_lowered_expr(mod, ex, stmt_offset=0)
226+
@fzone "JL: to_lowered_expr" function to_lowered_expr(mod, ex)
227+
_to_lowered_expr(mod, ex, 0)
228+
end
229+
230+
function _to_lowered_expr(mod, ex, stmt_offset)
227231
k = kind(ex)
228232
if is_literal(k)
229233
ex.value
@@ -259,7 +263,7 @@ function to_lowered_expr(mod, ex, stmt_offset=0)
259263
elseif k == K"SSAValue"
260264
Core.SSAValue(ex.var_id + stmt_offset)
261265
elseif k == K"return"
262-
Core.ReturnNode(to_lowered_expr(mod, ex[1], stmt_offset))
266+
Core.ReturnNode(_to_lowered_expr(mod, ex[1], stmt_offset))
263267
elseif k == K"inert"
264268
e1 = ex[1]
265269
getmeta(ex, :as_Expr, false) ? QuoteNode(Expr(e1)) : e1
@@ -278,36 +282,36 @@ function to_lowered_expr(mod, ex, stmt_offset=0)
278282
elseif k == K"goto"
279283
Core.GotoNode(ex[1].id + stmt_offset)
280284
elseif k == K"gotoifnot"
281-
Core.GotoIfNot(to_lowered_expr(mod, ex[1], stmt_offset), ex[2].id + stmt_offset)
285+
Core.GotoIfNot(_to_lowered_expr(mod, ex[1], stmt_offset), ex[2].id + stmt_offset)
282286
elseif k == K"enter"
283287
catch_idx = ex[1].id
284288
numchildren(ex) == 1 ?
285289
Core.EnterNode(catch_idx) :
286-
Core.EnterNode(catch_idx, to_lowered_expr(mod, ex[2], stmt_offset))
290+
Core.EnterNode(catch_idx, _to_lowered_expr(mod, ex[2], stmt_offset))
287291
elseif k == K"method"
288-
cs = map(e->to_lowered_expr(mod, e, stmt_offset), children(ex))
292+
cs = map(e->_to_lowered_expr(mod, e, stmt_offset), children(ex))
289293
# Ad-hoc unwrapping to satisfy `Expr(:method)` expectations
290294
c1 = cs[1] isa QuoteNode ? cs[1].value : cs[1]
291295
Expr(:method, c1, cs[2:end]...)
292296
elseif k == K"newvar"
293-
Core.NewvarNode(to_lowered_expr(mod, ex[1], stmt_offset))
297+
Core.NewvarNode(_to_lowered_expr(mod, ex[1], stmt_offset))
294298
elseif k == K"opaque_closure_method"
295-
args = map(e->to_lowered_expr(mod, e, stmt_offset), children(ex))
299+
args = map(e->_to_lowered_expr(mod, e, stmt_offset), children(ex))
296300
# opaque_closure_method has special non-evaluated semantics for the
297301
# `functionloc` line number node so we need to undo a level of quoting
298302
@assert args[4] isa QuoteNode
299303
args[4] = args[4].value
300304
Expr(:opaque_closure_method, args...)
301305
elseif k == K"meta"
302-
args = Any[to_lowered_expr(mod, e, stmt_offset) for e in children(ex)]
306+
args = Any[_to_lowered_expr(mod, e, stmt_offset) for e in children(ex)]
303307
# Unpack K"Symbol" QuoteNode as `Expr(:meta)` requires an identifier here.
304308
args[1] = args[1].value
305309
Expr(:meta, args...)
306310
elseif k == K"static_eval"
307311
@assert numchildren(ex) == 1
308-
to_lowered_expr(mod, ex[1], stmt_offset)
312+
_to_lowered_expr(mod, ex[1], stmt_offset)
309313
elseif k == K"cfunction"
310-
args = Any[to_lowered_expr(mod, e, stmt_offset) for e in children(ex)]
314+
args = Any[_to_lowered_expr(mod, e, stmt_offset) for e in children(ex)]
311315
if kind(ex[2]) == K"static_eval"
312316
args[2] = QuoteNode(args[2])
313317
end
@@ -339,13 +343,13 @@ function to_lowered_expr(mod, ex, stmt_offset=0)
339343
if isnothing(head)
340344
throw(LoweringError(ex, "Unhandled form for kind $k"))
341345
end
342-
Expr(head, map(e->to_lowered_expr(mod, e, stmt_offset), children(ex))...)
346+
Expr(head, map(e->_to_lowered_expr(mod, e, stmt_offset), children(ex))...)
343347
end
344348
end
345349

346350
#-------------------------------------------------------------------------------
347351
# Our version of eval takes our own data structures
348-
function Core.eval(mod::Module, ex::SyntaxTree; expr_compat_mode::Bool=false)
352+
@fzone "JL: eval" function Core.eval(mod::Module, ex::SyntaxTree; expr_compat_mode::Bool=false)
349353
k = kind(ex)
350354
if k == K"toplevel"
351355
x = nothing
@@ -355,7 +359,7 @@ function Core.eval(mod::Module, ex::SyntaxTree; expr_compat_mode::Bool=false)
355359
return x
356360
end
357361
linear_ir = lower(mod, ex; expr_compat_mode)
358-
expr_form = to_lowered_expr(mod, linear_ir, 0)
362+
expr_form = to_lowered_expr(mod, linear_ir)
359363
eval(mod, expr_form)
360364
end
361365

src/linear_ir.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ Most of the compliexty of this pass is in lowering structured control flow (if,
11091109
loops, etc) to gotos and exception handling to enter/leave. We also convert
11101110
`K"BindingId"` into K"slot", `K"globalref"` or `K"SSAValue` as appropriate.
11111111
"""
1112-
function linearize_ir(ctx, ex)
1112+
@fzone "JL: linearize" function linearize_ir(ctx, ex)
11131113
graph = ensure_attributes(ctx.graph,
11141114
slots=Vector{Slot},
11151115
mod=Module,
@@ -1127,4 +1127,3 @@ function linearize_ir(ctx, ex)
11271127
res = compile_lambda(_ctx, reparent(_ctx, ex))
11281128
_ctx, res
11291129
end
1130-

src/macro_expansion.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
460460
end
461461
end
462462

463-
function expand_forms_1(mod::Module, ex::SyntaxTree, expr_compat_mode::Bool, macro_world::UInt)
463+
@fzone "JL: macroexpand" function expand_forms_1(mod::Module, ex::SyntaxTree, expr_compat_mode::Bool, macro_world::UInt)
464464
graph = ensure_attributes(syntax_graph(ex),
465465
var_id=IdTag,
466466
scope_layer=LayerId,

src/scope_analysis.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ metadata about each binding.
802802
This pass also records the set of binding IDs used locally within the
803803
enclosing lambda form and information about variables captured by closures.
804804
"""
805-
function resolve_scopes(ctx::DesugaringContext, ex)
805+
@fzone "JL: resolve_scopes" function resolve_scopes(ctx::DesugaringContext, ex)
806806
ctx2 = ScopeResolutionContext(ctx)
807807
ex2 = resolve_scopes(ctx2, reparent(ctx2, ex))
808808
ctx3 = VariableAnalysisContext(ctx2.graph, ctx2.bindings, ctx2.mod, ex2.lambda_bindings)

src/utils.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,17 @@ function _print_ir(io::IO, ex, indent)
169169
end
170170
end
171171
end
172+
173+
# Wrap a function body in Base.Compiler.@zone for profiling
174+
if isdefined(Base.Compiler, Symbol("@zone"))
175+
macro fzone(str, f)
176+
@assert f isa Expr && f.head === :function && length(f.args) === 2 && str isa String
177+
esc(Expr(:function, f.args[1],
178+
# Use source of our caller, not of this macro.
179+
Expr(:macrocall, :(Base.Compiler.var"@zone"), __source__, str, f.args[2])))
180+
end
181+
else
182+
macro fzone(str, f)
183+
esc(f)
184+
end
185+
end

0 commit comments

Comments
 (0)