Skip to content

Commit 75c44d0

Browse files
KristofferCKristoffer Carlssongbaralditopolarity
authored
transition @timeit in Core.Compiler to use tracy instead (#49675)
Co-authored-by: Kristoffer Carlsson <[email protected]> Co-authored-by: gbaraldi <[email protected]> Co-authored-by: Cody Tapscott <[email protected]>
1 parent 37a7541 commit 75c44d0

File tree

15 files changed

+179
-36
lines changed

15 files changed

+179
-36
lines changed

Compiler/src/Compiler.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ function is_return_type(Core.@nospecialize(f))
120120
return false
121121
end
122122

123+
include("profiling.jl")
123124
include("sort.jl")
124125

125126
# We don't include some.jl, but this definition is still useful.

Compiler/src/inferencestate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ mutable struct LazyGenericDomtree{IsPostDom}
188188
end
189189
function get!(x::LazyGenericDomtree{IsPostDom}) where {IsPostDom}
190190
isdefined(x, :domtree) && return x.domtree
191-
return @timeit "domtree 2" x.domtree = IsPostDom ?
191+
return @zone "CC: DOMTREE_2" x.domtree = IsPostDom ?
192192
construct_postdomtree(x.ir) :
193193
construct_domtree(x.ir)
194194
end

Compiler/src/optimize.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ end
982982

983983
# run the optimization work
984984
function optimize(interp::AbstractInterpreter, opt::OptimizationState, caller::InferenceResult)
985-
@timeit "optimizer" ir = run_passes_ipo_safe(opt.src, opt)
985+
@zone "CC: OPTIMIZER" ir = run_passes_ipo_safe(opt.src, opt)
986986
ipo_dataflow_analysis!(interp, opt, ir, caller)
987987
finishopt!(interp, opt, ir)
988988
return nothing
@@ -992,7 +992,7 @@ const ALL_PASS_NAMES = String[]
992992
macro pass(name::String, expr)
993993
optimize_until = esc(:optimize_until)
994994
stage = esc(:__stage__)
995-
macrocall = :(@timeit $name $(esc(expr)))
995+
macrocall = :(@zone $name $(esc(expr)))
996996
macrocall.args[2] = __source__ # `@timeit` may want to use it
997997
push!(ALL_PASS_NAMES, name)
998998
quote
@@ -1017,20 +1017,20 @@ function run_passes_ipo_safe(
10171017

10181018
__stage__ = 0 # used by @pass
10191019
# NOTE: The pass name MUST be unique for `optimize_until::String` to work
1020-
@pass "convert" ir = convert_to_ircode(ci, sv)
1021-
@pass "slot2reg" ir = slot2reg(ir, ci, sv)
1020+
@pass "CC: CONVERT" ir = convert_to_ircode(ci, sv)
1021+
@pass "CC: SLOT2REG" ir = slot2reg(ir, ci, sv)
10221022
# TODO: Domsorting can produce an updated domtree - no need to recompute here
1023-
@pass "compact 1" ir = compact!(ir)
1024-
@pass "inlining" ir = ssa_inlining_pass!(ir, sv.inlining, ci.propagate_inbounds)
1025-
# @timeit "verify 2" verify_ir(ir)
1026-
@pass "compact 2" ir = compact!(ir)
1027-
@pass "SROA" ir = sroa_pass!(ir, sv.inlining)
1028-
@pass "ADCE" (ir, made_changes) = adce_pass!(ir, sv.inlining)
1023+
@pass "CC: COMPACT_1" ir = compact!(ir)
1024+
@pass "CC: INLINING" ir = ssa_inlining_pass!(ir, sv.inlining, ci.propagate_inbounds)
1025+
# @zone "CC: VERIFY 2" verify_ir(ir)
1026+
@pass "CC: COMPACT_2" ir = compact!(ir)
1027+
@pass "CC: SROA" ir = sroa_pass!(ir, sv.inlining)
1028+
@pass "CC: ADCE" (ir, made_changes) = adce_pass!(ir, sv.inlining)
10291029
if made_changes
1030-
@pass "compact 3" ir = compact!(ir, true)
1030+
@pass "CC: COMPACT_3" ir = compact!(ir, true)
10311031
end
10321032
if is_asserts()
1033-
@timeit "verify 3" begin
1033+
@zone "CC: VERIFY_3" begin
10341034
verify_ir(ir, true, false, optimizer_lattice(sv.inlining.interp), sv.linfo)
10351035
verify_linetable(ir.debuginfo, length(ir.stmts))
10361036
end
@@ -1291,10 +1291,10 @@ end
12911291
function slot2reg(ir::IRCode, ci::CodeInfo, sv::OptimizationState)
12921292
# need `ci` for the slot metadata, IR for the code
12931293
svdef = sv.linfo.def
1294-
@timeit "domtree 1" domtree = construct_domtree(ir)
1294+
@zone "CC: DOMTREE_1" domtree = construct_domtree(ir)
12951295
defuse_insts = scan_slot_def_use(Int(ci.nargs), ci, ir.stmts.stmt)
12961296
𝕃ₒ = optimizer_lattice(sv.inlining.interp)
1297-
@timeit "construct_ssa" ir = construct_ssa!(ci, ir, sv, domtree, defuse_insts, 𝕃ₒ) # consumes `ir`
1297+
@zone "CC: CONSTRUCT_SSA" ir = construct_ssa!(ci, ir, sv, domtree, defuse_insts, 𝕃ₒ) # consumes `ir`
12981298
# NOTE now we have converted `ir` to the SSA form and eliminated slots
12991299
# let's resize `argtypes` now and remove unnecessary types for the eliminated slots
13001300
resize!(ir.argtypes, ci.nargs)

Compiler/src/profiling.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const WITH_ITTAPI = ccall(:jl_ittapi_enabled, Cint, ()) != 0
2+
const WITH_TRACY = ccall(:jl_tracy_enabled, Cint, ()) != 0
3+
4+
include("profiling/tracy.jl")
5+
include("profiling/ittapi.jl")
6+
7+
if WITH_TRACY || WITH_ITTAPI
8+
macro zone(name, ex::Expr)
9+
srcloc = WITH_TRACY && Tracy.tracy_zone_create(name, ex, __source__)
10+
tracy_begin_expr = WITH_TRACY ? :(ctx_tracy = Tracy.tracy_zone_begin($srcloc, true)) : :()
11+
tracy_end_expr = WITH_TRACY ? :(Tracy.tracy_zone_end(ctx_tracy)) : :()
12+
13+
event = WITH_ITTAPI && ITTAPI.ittapi_zone_create(name, ex, __source__)
14+
ittapi_begin_expr = WITH_ITTAPI ? :(ctx_ittapi = ITTAPI.ittapi_zone_begin($event, true)) : :()
15+
ittapi_end_expr = WITH_ITTAPI ? :(ITTAPI.ittapi_zone_end(ctx_ittapi)) : :()
16+
17+
return quote
18+
$tracy_begin_expr
19+
$ittapi_begin_expr
20+
$(Expr(:tryfinally,
21+
:($(esc(ex))),
22+
quote
23+
$tracy_end_expr
24+
$ittapi_end_expr
25+
end
26+
))
27+
end
28+
end
29+
else
30+
macro zone(name::String, ex::Expr)
31+
esc(ex)
32+
end
33+
end

Compiler/src/profiling/ittapi.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Stubs
2+
module ITTAPI
3+
4+
import ..String, ..Expr, ..LineNumberNode, ..nothing
5+
6+
function ittapi_zone_create(name::String, ex::Expr, linfo::LineNumberNode)
7+
return nothing
8+
end
9+
10+
function ittapi_zone_begin(loc, active)
11+
return nothing
12+
end
13+
14+
function ittapi_zone_end(ctx)
15+
return nothing
16+
end
17+
18+
end

Compiler/src/profiling/tracy.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module Tracy
2+
3+
import ..@noinline, ..@atomic, ..Cint, ..Vector, ..push!, ..unsafe_convert, ..esc,
4+
..pointer_from_objref, ..String, ..Ptr, ..UInt8, ..Cvoid,
5+
..Expr, ..LineNumberNode, ..Symbol, ..UInt32, ..C_NULL, ..(===)
6+
7+
_strpointer(s::String) = ccall(:jl_string_ptr, Ptr{UInt8}, (Any,), s)
8+
9+
mutable struct TracySrcLoc
10+
@atomic zone_name::Ptr{UInt8}
11+
@atomic function_name::Ptr{UInt8}
12+
@atomic file::Ptr{UInt8}
13+
const line::UInt32
14+
const color::UInt32
15+
# Roots
16+
const zone_name_str::String
17+
const function_name_sym::Symbol
18+
const file_sym::Symbol
19+
end
20+
TracySrcLoc(zone_name::String, function_name::Symbol, file::Symbol, line::UInt32, color::UInt32) =
21+
TracySrcLoc(C_NULL, C_NULL, C_NULL, line, color, zone_name, function_name, file)
22+
23+
@noinline function reinit!(srcloc::TracySrcLoc)
24+
@atomic :monotonic srcloc.file = unsafe_convert(Ptr{UInt8}, srcloc.file_sym)
25+
@atomic :monotonic srcloc.function_name = unsafe_convert(Ptr{UInt8}, srcloc.function_name_sym)
26+
@atomic :release srcloc.zone_name = _strpointer(srcloc.zone_name_str)
27+
end
28+
29+
struct TracyZoneCtx
30+
id::UInt32
31+
active::Cint
32+
end
33+
34+
const srclocs = Vector{TracySrcLoc}()
35+
36+
function tracy_zone_create(name::String, ex::Expr, linfo::LineNumberNode)
37+
# Intern strings
38+
for loc in srclocs
39+
if loc.zone_name_str === name
40+
name = loc.zone_name_str
41+
break
42+
end
43+
end
44+
loc = TracySrcLoc(name, Symbol("unknown"), linfo.file, UInt32(linfo.line), UInt32(0))
45+
# Also roots `loc` in `srclocs`
46+
push!(srclocs, loc)
47+
return loc
48+
end
49+
50+
function tracy_zone_begin(loc, active)
51+
if (@atomic :acquire loc.zone_name) === Ptr{UInt8}(0)
52+
reinit!(loc)
53+
end
54+
# `loc` is rooted in the global `srclocs`
55+
ptr = Ptr{TracySrcLoc}(pointer_from_objref(loc))
56+
return ccall((:___tracy_emit_zone_begin, "libTracyClient"), TracyZoneCtx, (Ptr{TracySrcLoc}, Cint), ptr, active)
57+
end
58+
59+
function tracy_zone_end(ctx)
60+
ccall((:___tracy_emit_zone_end, "libTracyClient"), Cvoid, (TracyZoneCtx,), ctx)
61+
end
62+
63+
end

Compiler/src/ssair/inlining.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ add_inlining_edge!(et::InliningEdgeTracker, edge::MethodInstance) = add_inlining
7373
function ssa_inlining_pass!(ir::IRCode, state::InliningState, propagate_inbounds::Bool)
7474
# Go through the function, performing simple inlining (e.g. replacing call by constants
7575
# and analyzing legality of inlining).
76-
@timeit "analysis" todo = assemble_inline_todo!(ir, state)
76+
@zone "CC: ANALYSIS" todo = assemble_inline_todo!(ir, state)
7777
isempty(todo) && return ir
7878
# Do the actual inlining for every call we identified
79-
@timeit "execution" ir = batch_inline!(ir, todo, propagate_inbounds, state.interp)
79+
@zone "CC: EXECUTION" ir = batch_inline!(ir, todo, propagate_inbounds, state.interp)
8080
return ir
8181
end
8282

Compiler/src/ssair/slot2ssa.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, sv::OptimizationState,
574574
for (; leave_block) in catch_entry_blocks
575575
new_phic_nodes[leave_block] = NewPhiCNode2[]
576576
end
577-
@timeit "idf" for (idx, slot) in Iterators.enumerate(defuses)
577+
@zone "CC: IDF" for (idx, slot) in Iterators.enumerate(defuses)
578578
# No uses => no need for phi nodes
579579
isempty(slot.uses) && continue
580580
# TODO: Restore this optimization
@@ -600,7 +600,7 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, sv::OptimizationState,
600600
continue
601601
end
602602

603-
@timeit "liveness" (live = compute_live_ins(cfg, slot))
603+
@zone "CC: LIVENESS" (live = compute_live_ins(cfg, slot))
604604
for li in live.live_in_bbs
605605
push!(live_slots[li], idx)
606606
cidx = findfirst(x::TryCatchRegion->x.leave_block==li, catch_entry_blocks)
@@ -671,7 +671,7 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, sv::OptimizationState,
671671
worklist = Tuple{Int, Int, Vector{Pair{Any, Any}}}[(1, 0, initial_incoming_vals)]
672672
visited = BitSet()
673673
new_nodes = ir.new_nodes
674-
@timeit "SSA Rename" while !isempty(worklist)
674+
@zone "CC: SSA_RENAME" while !isempty(worklist)
675675
(item, pred, incoming_vals) = pop!(worklist)
676676
if sv.bb_vartables[item] === nothing
677677
continue
@@ -891,6 +891,6 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, sv::OptimizationState,
891891
local node = new_nodes.stmts[i]
892892
node[:stmt] = new_to_regular(renumber_ssa!(node[:stmt], ssavalmap), nstmts)
893893
end
894-
@timeit "domsort" ir = domsort_ssa!(ir, domtree)
894+
@zone "CC: DOMSORT" ir = domsort_ssa!(ir, domtree)
895895
return ir
896896
end

Compiler/src/utilities.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# generic #
55
###########
66

7-
if !@isdefined(var"@timeit")
7+
if !@isdefined(var"@zone")
88
# This is designed to allow inserting timers when loading a second copy
99
# of inference for performing performance experiments.
1010
macro timeit(args...)

Compiler/test/inline.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,7 @@ multi_inlining1(a::Int, b::Int) = @noinline func_mul_int(a, b)
18521852
let i::Int, continue_::Bool
18531853
interp = Compiler.NativeInterpreter()
18541854
# check if callsite `@noinline` annotation works
1855-
ir, = only(Base.code_ircode(multi_inlining1, (Int,Int); optimize_until="inlining", interp))
1855+
ir, = only(Base.code_ircode(multi_inlining1, (Int,Int); optimize_until="CC: INLINING", interp))
18561856
i = findfirst(isinvoke(:func_mul_int), ir.stmts.stmt)
18571857
@test i !== nothing
18581858
# now delete the callsite flag, and see the second inlining pass can inline the call
@@ -1876,7 +1876,7 @@ multi_inlining2(a::Int, b::Int) = call_func_mul_int(a, b)
18761876
let i::Int, continue_::Bool
18771877
interp = Compiler.NativeInterpreter()
18781878
# check if callsite `@noinline` annotation works
1879-
ir, = only(Base.code_ircode(multi_inlining2, (Int,Int); optimize_until="inlining", interp))
1879+
ir, = only(Base.code_ircode(multi_inlining2, (Int,Int); optimize_until="CC: INLINING", interp))
18801880
i = findfirst(isinvoke(:func_mul_int), ir.stmts.stmt)
18811881
@test i !== nothing
18821882
# now delete the callsite flag, and see the second inlining pass can inline the call
@@ -2220,7 +2220,7 @@ struct Issue52644
22202220
end
22212221
issue52644(::DataType) = :DataType
22222222
issue52644(::UnionAll) = :UnionAll
2223-
let ir = Base.code_ircode((Issue52644,); optimize_until="inlining") do t
2223+
let ir = Base.code_ircode((Issue52644,); optimize_until="CC: INLINING") do t
22242224
issue52644(t.tuple)
22252225
end |> only |> first
22262226
ir.argtypes[1] = Tuple{}
@@ -2229,7 +2229,7 @@ let ir = Base.code_ircode((Issue52644,); optimize_until="inlining") do t
22292229
@test irfunc(Issue52644(Tuple{<:Integer})) === :UnionAll
22302230
end
22312231
issue52644_single(x::DataType) = :DataType
2232-
let ir = Base.code_ircode((Issue52644,); optimize_until="inlining") do t
2232+
let ir = Base.code_ircode((Issue52644,); optimize_until="CC: INLINING") do t
22332233
issue52644_single(t.tuple)
22342234
end |> only |> first
22352235
ir.argtypes[1] = Tuple{}

0 commit comments

Comments
 (0)