Skip to content

Commit 2d74196

Browse files
authored
Merge branch 'master' into mb/mapreducedim_empty
2 parents 813bcf3 + 2270fcd commit 2d74196

File tree

137 files changed

+2852
-2591
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+2852
-2591
lines changed

Compiler/extras/CompilerDevTools/src/CompilerDevTools.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747

4848
function Compiler.transform_result_for_cache(interp::SplitCacheInterp, result::Compiler.InferenceResult, edges::Compiler.SimpleVector)
4949
opt = result.src::Compiler.OptimizationState
50-
ir = opt.optresult.ir::Compiler.IRCode
50+
ir = opt.result.ir::Compiler.IRCode
5151
override = with_new_compiler
5252
for inst in ir.stmts
5353
stmt = inst[:stmt]

Compiler/src/Compiler.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ using Base: @_foldable_meta, @_gc_preserve_begin, @_gc_preserve_end, @nospeciali
6565
structdiff, tls_world_age, unconstrain_vararg_length, unionlen, uniontype_layout,
6666
uniontypes, unsafe_convert, unwrap_unionall, unwrapva, vect, widen_diagonal,
6767
_uncompressed_ir, maybe_add_binding_backedge!, datatype_min_ninitialized,
68-
partialstruct_init_undefs, fieldcount_noerror
68+
partialstruct_init_undefs, fieldcount_noerror, _eval_import, _eval_using
6969
using Base.Order
7070

7171
import Base: ==, _topmod, append!, convert, copy, copy!, findall, first, get, get!,
@@ -120,7 +120,7 @@ function is_return_type(Core.@nospecialize(f))
120120
return false
121121
end
122122

123-
include("profiling.jl")
123+
include("timing.jl")
124124
include("sort.jl")
125125

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

Compiler/src/abstractinterpretation.jl

Lines changed: 29 additions & 16 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,8 @@ 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::F1, walk_binding_partition::F2,
3613+
interp::Union{AbstractInterpreter,Nothing}, g::GlobalRef, wwr::WorldWithRange) where {F1,F2}
36113614
local total_validity, rte, binding_partition
36123615
binding = convert(Core.Binding, g)
36133616
lookup_world = max_world(wwr.valid_worlds)
@@ -3640,19 +3643,25 @@ function scan_specified_partitions(query::Function, walk_binding_partition::Func
36403643
return Pair{WorldRange, typeof(rte)}(total_validity, rte)
36413644
end
36423645

3643-
scan_leaf_partitions(query::Function, interp, g::GlobalRef, wwr::WorldWithRange) =
3646+
scan_leaf_partitions(query::F, ::Nothing, g::GlobalRef, wwr::WorldWithRange) where F =
3647+
scan_specified_partitions(query, walk_binding_partition, nothing, g, wwr)
3648+
scan_leaf_partitions(query::F, interp::AbstractInterpreter, g::GlobalRef, wwr::WorldWithRange) where F =
36443649
scan_specified_partitions(query, walk_binding_partition, interp, g, wwr)
36453650

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)
3651+
function scan_partitions(query::F, interp::AbstractInterpreter, g::GlobalRef, wwr::WorldWithRange) where F
3652+
walk_binding_partition = function (b::Core.Binding, partition::Core.BindingPartition, world::UInt)
3653+
Pair{WorldRange, Pair{Core.Binding, Core.BindingPartition}}(
3654+
WorldRange(partition.min_world, partition.max_world), b=>partition)
3655+
end
3656+
return scan_specified_partitions(query, walk_binding_partition, interp, g, wwr)
3657+
end
36513658

3652-
abstract_load_all_consistent_leaf_partitions(interp, g::GlobalRef, wwr::WorldWithRange) =
3659+
abstract_load_all_consistent_leaf_partitions(interp::AbstractInterpreter, g::GlobalRef, wwr::WorldWithRange) =
36533660
scan_leaf_partitions(abstract_eval_partition_load, interp, g, wwr)
3661+
abstract_load_all_consistent_leaf_partitions(::Nothing, g::GlobalRef, wwr::WorldWithRange) =
3662+
scan_leaf_partitions(abstract_eval_partition_load, nothing, g, wwr)
36543663

3655-
function abstract_eval_globalref(interp, g::GlobalRef, saw_latestworld::Bool, sv::AbsIntState)
3664+
function abstract_eval_globalref(interp::AbstractInterpreter, g::GlobalRef, saw_latestworld::Bool, sv::AbsIntState)
36563665
if saw_latestworld
36573666
return RTEffects(Any, Any, generic_getglobal_effects)
36583667
end
@@ -3668,7 +3677,10 @@ function global_assignment_rt_exct(interp::AbstractInterpreter, sv::AbsIntState,
36683677
if saw_latestworld
36693678
return Pair{Any,Any}(newty, ErrorException)
36703679
end
3671-
(valid_worlds, ret) = scan_partitions((interp, _, partition)->global_assignment_binding_rt_exct(interp, partition, newty), interp, g, sv.world)
3680+
newty′ = RefValue{Any}(newty)
3681+
(valid_worlds, ret) = scan_partitions(interp, g, sv.world) do interp::AbstractInterpreter, ::Core.Binding, partition::Core.BindingPartition
3682+
global_assignment_binding_rt_exct(interp, partition, newty′[])
3683+
end
36723684
update_valid_age!(sv, valid_worlds)
36733685
return ret
36743686
end
@@ -3716,8 +3728,9 @@ struct AbstractEvalBasicStatementResult
37163728
end
37173729
end
37183730

3719-
function abstract_eval_basic_statement(interp::AbstractInterpreter, @nospecialize(stmt), sstate::StatementState, frame::InferenceState,
3720-
result::Union{Nothing,Future{RTEffects}}=nothing)
3731+
@inline function abstract_eval_basic_statement(
3732+
interp::AbstractInterpreter, @nospecialize(stmt), sstate::StatementState, frame::InferenceState,
3733+
result::Union{Nothing,Future{RTEffects}}=nothing)
37213734
rt = nothing
37223735
exct = Bottom
37233736
changes = nothing

Compiler/src/optimize.jl

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,11 @@ function inline_cost_clamp(x::Int)
116116
return convert(InlineCostType, x)
117117
end
118118

119-
const SRC_FLAG_DECLARED_INLINE = 0x1
120-
const SRC_FLAG_DECLARED_NOINLINE = 0x2
121-
122119
is_declared_inline(@nospecialize src::MaybeCompressed) =
123-
ccall(:jl_ir_flag_inlining, UInt8, (Any,), src) == SRC_FLAG_DECLARED_INLINE
120+
ccall(:jl_ir_flag_inlining, UInt8, (Any,), src) == 1
124121

125122
is_declared_noinline(@nospecialize src::MaybeCompressed) =
126-
ccall(:jl_ir_flag_inlining, UInt8, (Any,), src) == SRC_FLAG_DECLARED_NOINLINE
123+
ccall(:jl_ir_flag_inlining, UInt8, (Any,), src) == 2
127124

128125
#####################
129126
# OptimizationState #
@@ -160,7 +157,6 @@ code_cache(state::InliningState) = WorldView(code_cache(state.interp), state.wor
160157

161158
mutable struct OptimizationResult
162159
ir::IRCode
163-
inline_flag::UInt8
164160
simplified::Bool # indicates whether the IR was processed with `cfg_simplify!`
165161
end
166162

@@ -172,7 +168,7 @@ end
172168
mutable struct OptimizationState{Interp<:AbstractInterpreter}
173169
linfo::MethodInstance
174170
src::CodeInfo
175-
optresult::Union{Nothing, OptimizationResult}
171+
result::Union{Nothing, OptimizationResult}
176172
stmt_info::Vector{CallInfo}
177173
mod::Module
178174
sptypes::Vector{VarState}
@@ -240,29 +236,13 @@ include("ssair/EscapeAnalysis.jl")
240236
include("ssair/passes.jl")
241237
include("ssair/irinterp.jl")
242238

243-
function ir_to_codeinf!(opt::OptimizationState, frame::InferenceState, edges::SimpleVector)
244-
ir_to_codeinf!(opt, edges, compute_inlining_cost(frame.interp, frame.result, opt.optresult))
245-
end
246-
247-
function ir_to_codeinf!(opt::OptimizationState, edges::SimpleVector, inlining_cost::InlineCostType)
248-
src = ir_to_codeinf!(opt, edges)
249-
src.inlining_cost = inlining_cost
250-
src
251-
end
252-
253-
function ir_to_codeinf!(opt::OptimizationState, edges::SimpleVector)
254-
src = ir_to_codeinf!(opt)
255-
src.edges = edges
256-
src
257-
end
258-
259239
function ir_to_codeinf!(opt::OptimizationState)
260-
(; linfo, src, optresult) = opt
261-
if optresult === nothing
240+
(; linfo, src, result) = opt
241+
if result === nothing
262242
return src
263243
end
264-
src = ir_to_codeinf!(src, optresult.ir)
265-
opt.optresult = nothing
244+
src = ir_to_codeinf!(src, result.ir)
245+
opt.result = nothing
266246
opt.src = src
267247
maybe_validate_code(linfo, src, "optimized")
268248
return src
@@ -505,12 +485,63 @@ end
505485
abstract_eval_ssavalue(s::SSAValue, src::Union{IRCode,IncrementalCompact}) = types(src)[s]
506486

507487
"""
508-
finishopt!(interp::AbstractInterpreter, opt::OptimizationState, ir::IRCode)
488+
finish(interp::AbstractInterpreter, opt::OptimizationState,
489+
ir::IRCode, caller::InferenceResult)
509490
510-
Called at the end of optimization to store the resulting IR back into the OptimizationState.
491+
Post-process information derived by Julia-level optimizations for later use.
492+
In particular, this function determines the inlineability of the optimized code.
511493
"""
512-
function finishopt!(interp::AbstractInterpreter, opt::OptimizationState, ir::IRCode)
513-
opt.optresult = OptimizationResult(ir, ccall(:jl_ir_flag_inlining, UInt8, (Any,), opt.src), false)
494+
function finish(interp::AbstractInterpreter, opt::OptimizationState,
495+
ir::IRCode, caller::InferenceResult)
496+
(; src, linfo) = opt
497+
(; def, specTypes) = linfo
498+
499+
force_noinline = is_declared_noinline(src)
500+
501+
# compute inlining and other related optimizations
502+
result = caller.result
503+
@assert !(result isa LimitedAccuracy)
504+
result = widenslotwrapper(result)
505+
506+
opt.result = OptimizationResult(ir, false)
507+
508+
# determine and cache inlineability
509+
if !force_noinline
510+
sig = unwrap_unionall(specTypes)
511+
if !(isa(sig, DataType) && sig.name === Tuple.name)
512+
force_noinline = true
513+
end
514+
if !is_declared_inline(src) && result === Bottom
515+
force_noinline = true
516+
end
517+
end
518+
if force_noinline
519+
set_inlineable!(src, false)
520+
elseif isa(def, Method)
521+
if is_declared_inline(src) && isdispatchtuple(specTypes)
522+
# obey @inline declaration if a dispatch barrier would not help
523+
set_inlineable!(src, true)
524+
else
525+
# compute the cost (size) of inlining this code
526+
params = OptimizationParams(interp)
527+
cost_threshold = default = params.inline_cost_threshold
528+
if (optimizer_lattice(interp), result, Tuple) && !isconcretetype(widenconst(result))
529+
cost_threshold += params.inline_tupleret_bonus
530+
end
531+
# if the method is declared as `@inline`, increase the cost threshold 20x
532+
if is_declared_inline(src)
533+
cost_threshold += 19*default
534+
end
535+
# a few functions get special treatment
536+
if def.module === _topmod(def.module)
537+
name = def.name
538+
if name === :iterate || name === :unsafe_convert || name === :cconvert
539+
cost_threshold += 4*default
540+
end
541+
end
542+
src.inlining_cost = inline_cost(ir, params, cost_threshold)
543+
end
544+
end
514545
return nothing
515546
end
516547

@@ -984,8 +1015,7 @@ end
9841015
function optimize(interp::AbstractInterpreter, opt::OptimizationState, caller::InferenceResult)
9851016
@zone "CC: OPTIMIZER" ir = run_passes_ipo_safe(opt.src, opt)
9861017
ipo_dataflow_analysis!(interp, opt, ir, caller)
987-
finishopt!(interp, opt, ir)
988-
return nothing
1018+
return finish(interp, opt, ir, caller)
9891019
end
9901020

9911021
const ALL_PASS_NAMES = String[]
@@ -1436,7 +1466,7 @@ function statement_or_branch_cost(@nospecialize(stmt), line::Int, src::Union{Cod
14361466
return thiscost
14371467
end
14381468

1439-
function inline_cost_model(ir::IRCode, params::OptimizationParams, cost_threshold::Int)
1469+
function inline_cost(ir::IRCode, params::OptimizationParams, cost_threshold::Int)
14401470
bodycost = 0
14411471
for i = 1:length(ir.stmts)
14421472
stmt = ir[SSAValue(i)][:stmt]

Compiler/src/profiling.jl

Lines changed: 0 additions & 33 deletions
This file was deleted.

Compiler/src/profiling/ittapi.jl

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)