Skip to content

Commit d7b28d5

Browse files
committed
Merge remote-tracking branch 'upstream/master' into llvm20
2 parents e9f81cf + 5c869cd commit d7b28d5

29 files changed

+481
-150
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/abstractinterpretation.jl

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,14 +2437,15 @@ function abstract_eval_getglobal(interp::AbstractInterpreter, sv::AbsIntState, s
24372437
end
24382438

24392439
@nospecs function abstract_eval_get_binding_type(interp::AbstractInterpreter, sv::AbsIntState, M, s)
2440+
@nospecialize M s
24402441
= partialorder(typeinf_lattice(interp))
24412442
if isa(M, Const) && isa(s, Const)
24422443
(M, s) = (M.val, s.val)
24432444
if !isa(M, Module) || !isa(s, Symbol)
24442445
return CallMeta(Union{}, TypeError, EFFECTS_THROWS, NoCallInfo())
24452446
end
24462447
gr = GlobalRef(M, s)
2447-
(valid_worlds, rt) = scan_leaf_partitions(interp, gr, sv.world) do interp, _, partition
2448+
(valid_worlds, rt) = scan_leaf_partitions(interp, gr, sv.world) do interp::AbstractInterpreter, ::Core.Binding, partition::Core.BindingPartition
24482449
local rt
24492450
kind = binding_kind(partition)
24502451
if is_some_guard(kind) || kind == PARTITION_KIND_DECLARED
@@ -2574,13 +2575,14 @@ function abstract_eval_replaceglobal!(interp::AbstractInterpreter, sv::AbsIntSta
25742575
M isa Module || return CallMeta(Union{}, TypeError, EFFECTS_THROWS, NoCallInfo())
25752576
s isa Symbol || return CallMeta(Union{}, TypeError, EFFECTS_THROWS, NoCallInfo())
25762577
gr = GlobalRef(M, s)
2577-
(valid_worlds, (rte, T)) = scan_leaf_partitions(interp, gr, sv.world) do interp, binding, partition
2578+
v′ = RefValue{Any}(v)
2579+
(valid_worlds, (rte, T)) = scan_leaf_partitions(interp, gr, sv.world) do interp::AbstractInterpreter, binding::Core.Binding, partition::Core.BindingPartition
25782580
partition_T = nothing
25792581
partition_rte = abstract_eval_partition_load(interp, binding, partition)
25802582
if binding_kind(partition) == PARTITION_KIND_GLOBAL
25812583
partition_T = partition_restriction(partition)
25822584
end
2583-
partition_exct = Union{partition_rte.exct, global_assignment_binding_rt_exct(interp, partition, v)[2]}
2585+
partition_exct = Union{partition_rte.exct, global_assignment_binding_rt_exct(interp, partition, v′[])[2]}
25842586
partition_rte = RTEffects(partition_rte.rt, partition_exct, partition_rte.effects)
25852587
Pair{RTEffects, Any}(partition_rte, partition_T)
25862588
end
@@ -3558,7 +3560,7 @@ function abstract_eval_binding_partition!(interp::AbstractInterpreter, g::Global
35583560
return partition
35593561
end
35603562

3561-
function abstract_eval_partition_load(interp::Union{AbstractInterpreter, Nothing}, binding::Core.Binding, partition::Core.BindingPartition)
3563+
function abstract_eval_partition_load(interp::Union{AbstractInterpreter,Nothing}, binding::Core.Binding, partition::Core.BindingPartition)
35623564
kind = binding_kind(partition)
35633565
isdepwarn = (partition.kind & PARTITION_FLAG_DEPWARN) != 0
35643566
local_getglobal_effects = Effects(generic_getglobal_effects, effect_free=isdepwarn ? ALWAYS_FALSE : ALWAYS_TRUE)
@@ -3607,7 +3609,7 @@ function abstract_eval_partition_load(interp::Union{AbstractInterpreter, Nothing
36073609
return RTEffects(rt, exct, effects)
36083610
end
36093611

3610-
function scan_specified_partitions(query::Function, walk_binding_partition::Function, interp, g::GlobalRef, wwr::WorldWithRange)
3612+
function scan_specified_partitions(query::Function, walk_binding_partition::Function, interp::Union{AbstractInterpreter,Nothing}, g::GlobalRef, wwr::WorldWithRange)
36113613
local total_validity, rte, binding_partition
36123614
binding = convert(Core.Binding, g)
36133615
lookup_world = max_world(wwr.valid_worlds)
@@ -3640,19 +3642,25 @@ function scan_specified_partitions(query::Function, walk_binding_partition::Func
36403642
return Pair{WorldRange, typeof(rte)}(total_validity, rte)
36413643
end
36423644

3643-
scan_leaf_partitions(query::Function, interp, g::GlobalRef, wwr::WorldWithRange) =
3645+
scan_leaf_partitions(query::Function, ::Nothing, g::GlobalRef, wwr::WorldWithRange) =
3646+
scan_specified_partitions(query, walk_binding_partition, nothing, g, wwr)
3647+
scan_leaf_partitions(query::Function, interp::AbstractInterpreter, g::GlobalRef, wwr::WorldWithRange) =
36443648
scan_specified_partitions(query, walk_binding_partition, interp, g, wwr)
36453649

3646-
scan_partitions(query::Function, interp, g::GlobalRef, wwr::WorldWithRange) =
3647-
scan_specified_partitions(query,
3648-
(b::Core.Binding, bpart::Core.BindingPartition, world::UInt)->
3649-
Pair{WorldRange, Pair{Core.Binding, Core.BindingPartition}}(WorldRange(bpart.min_world, bpart.max_world), b=>bpart),
3650-
interp, g, wwr)
3650+
function scan_partitions(query::Function, interp::AbstractInterpreter, g::GlobalRef, wwr::WorldWithRange)
3651+
walk_binding_partition = function (b::Core.Binding, partition::Core.BindingPartition, world::UInt)
3652+
Pair{WorldRange, Pair{Core.Binding, Core.BindingPartition}}(
3653+
WorldRange(partition.min_world, partition.max_world), b=>partition)
3654+
end
3655+
return scan_specified_partitions(query, walk_binding_partition, interp, g, wwr)
3656+
end
36513657

3652-
abstract_load_all_consistent_leaf_partitions(interp, g::GlobalRef, wwr::WorldWithRange) =
3658+
abstract_load_all_consistent_leaf_partitions(interp::AbstractInterpreter, g::GlobalRef, wwr::WorldWithRange) =
36533659
scan_leaf_partitions(abstract_eval_partition_load, interp, g, wwr)
3660+
abstract_load_all_consistent_leaf_partitions(::Nothing, g::GlobalRef, wwr::WorldWithRange) =
3661+
scan_leaf_partitions(abstract_eval_partition_load, nothing, g, wwr)
36543662

3655-
function abstract_eval_globalref(interp, g::GlobalRef, saw_latestworld::Bool, sv::AbsIntState)
3663+
function abstract_eval_globalref(interp::AbstractInterpreter, g::GlobalRef, saw_latestworld::Bool, sv::AbsIntState)
36563664
if saw_latestworld
36573665
return RTEffects(Any, Any, generic_getglobal_effects)
36583666
end
@@ -3668,7 +3676,10 @@ function global_assignment_rt_exct(interp::AbstractInterpreter, sv::AbsIntState,
36683676
if saw_latestworld
36693677
return Pair{Any,Any}(newty, ErrorException)
36703678
end
3671-
(valid_worlds, ret) = scan_partitions((interp, _, partition)->global_assignment_binding_rt_exct(interp, partition, newty), interp, g, sv.world)
3679+
newty′ = RefValue{Any}(newty)
3680+
(valid_worlds, ret) = scan_partitions(interp, g, sv.world) do interp::AbstractInterpreter, ::Core.Binding, partition::Core.BindingPartition
3681+
global_assignment_binding_rt_exct(interp, partition, newty′[])
3682+
end
36723683
update_valid_age!(sv, valid_worlds)
36733684
return ret
36743685
end

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...)

0 commit comments

Comments
 (0)