Skip to content

Commit ffad80c

Browse files
authored
compiler: eliminate most of unused variables in the compiler (#59202)
Found by opening the compiler folder with JETLS.
1 parent e986983 commit ffad80c

14 files changed

+58
-68
lines changed

Compiler/src/abstractinterpretation.jl

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(fun
111111
arginfo::ArgInfo, si::StmtInfo, @nospecialize(atype),
112112
sv::AbsIntState, max_methods::Int)
113113
𝕃ₚ, 𝕃ᵢ = ipo_lattice(interp), typeinf_lattice(interp)
114-
ₚ, ₚ, ₚ, = partialorder(𝕃ₚ), strictneqpartialorder(𝕃ₚ), join(𝕃ₚ), join(𝕃ᵢ)
114+
ₚ, ₚ, = partialorder(𝕃ₚ), join(𝕃ₚ), join(𝕃ᵢ)
115115
argtypes = arginfo.argtypes
116116
if si.saw_latestworld
117117
add_remark!(interp, sv, "Cannot infer call, because we previously saw :latestworld")
@@ -309,7 +309,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(fun
309309
local napplicable = length(applicable)
310310
local multiple_matches = napplicable > 1
311311
while inferidx[] <= napplicable
312-
(; match, edges, edge_idx) = applicable[inferidx[]]
312+
(; match) = applicable[inferidx[]]
313313
inferidx[] += 1
314314
local method = match.method
315315
local sig = match.spec_types
@@ -343,7 +343,7 @@ function find_method_matches(interp::AbstractInterpreter, argtypes::Vector{Any},
343343
max_union_splitting::Int = InferenceParams(interp).max_union_splitting,
344344
max_methods::Int = InferenceParams(interp).max_methods)
345345
if is_union_split_eligible(typeinf_lattice(interp), argtypes, max_union_splitting)
346-
return find_union_split_method_matches(interp, argtypes, atype, max_methods)
346+
return find_union_split_method_matches(interp, argtypes, max_methods)
347347
end
348348
return find_simple_method_matches(interp, atype, max_methods)
349349
end
@@ -353,7 +353,7 @@ is_union_split_eligible(𝕃::AbstractLattice, argtypes::Vector{Any}, max_union_
353353
1 < unionsplitcost(𝕃, argtypes) <= max_union_splitting
354354

355355
function find_union_split_method_matches(interp::AbstractInterpreter, argtypes::Vector{Any},
356-
@nospecialize(atype), max_methods::Int)
356+
max_methods::Int)
357357
split_argtypes = switchtupleunion(typeinf_lattice(interp), argtypes)
358358
infos = MethodMatchInfo[]
359359
applicable = MethodMatchTarget[]
@@ -994,7 +994,7 @@ function collect_const_args(argtypes::Vector{Any}, start::Int)
994994
end
995995

996996
function concrete_eval_call(interp::AbstractInterpreter,
997-
@nospecialize(f), result::MethodCallResult, arginfo::ArgInfo, sv::AbsIntState,
997+
@nospecialize(f), result::MethodCallResult, arginfo::ArgInfo, ::AbsIntState,
998998
invokecall::Union{InvokeCall,Nothing}=nothing)
999999
args = collect_const_args(arginfo, #=start=#2)
10001000
if invokecall !== nothing
@@ -1006,7 +1006,7 @@ function concrete_eval_call(interp::AbstractInterpreter,
10061006
edge = result.edge::CodeInstance
10071007
value = try
10081008
Core._call_in_world_total(world, f, args...)
1009-
catch e
1009+
catch
10101010
# The evaluation threw. By :consistent-cy, we're guaranteed this would have happened at runtime.
10111011
# Howevever, at present, :consistency does not mandate the type of the exception
10121012
concrete_result = ConcreteResult(edge, result.effects)
@@ -1368,7 +1368,6 @@ function const_prop_call(interp::AbstractInterpreter,
13681368
inf_result.result = concrete_eval_result.rt
13691369
inf_result.ipo_effects = concrete_eval_result.effects
13701370
end
1371-
typ = inf_result.result
13721371
return const_prop_result(inf_result)
13731372
end
13741373

@@ -1466,7 +1465,7 @@ function ssa_def_slot(@nospecialize(arg), sv::InferenceState)
14661465
end
14671466

14681467
# No slots in irinterp
1469-
ssa_def_slot(@nospecialize(arg), sv::IRInterpretationState) = nothing
1468+
ssa_def_slot(@nospecialize(arg), ::IRInterpretationState) = nothing
14701469

14711470
struct AbstractIterationResult
14721471
cti::Vector{Any}
@@ -2262,7 +2261,7 @@ function abstract_invoke(interp::AbstractInterpreter, arginfo::ArgInfo, si::Stmt
22622261
end
22632262
else
22642263
hasintersect(widenconst(types), Union{Method, CodeInstance}) && return Future(CallMeta(Any, Any, Effects(), NoCallInfo()))
2265-
(types, isexact, isconcrete, istype) = instanceof_tfunc(argtype_by_index(argtypes, 3), false)
2264+
types, isexact, _, _ = instanceof_tfunc(argtype_by_index(argtypes, 3), false)
22662265
isexact || return Future(CallMeta(Any, Any, Effects(), NoCallInfo()))
22672266
unwrapped = unwrap_unionall(types)
22682267
types === Bottom && return Future(CallMeta(Bottom, Any, EFFECTS_THROWS, NoCallInfo()))
@@ -2344,7 +2343,7 @@ function abstract_finalizer(interp::AbstractInterpreter, argtypes::Vector{Any},
23442343
if length(argtypes) == 3
23452344
finalizer_argvec = Any[argtypes[2], argtypes[3]]
23462345
call = abstract_call(interp, ArgInfo(nothing, finalizer_argvec), StmtInfo(false, false), sv, #=max_methods=#1)::Future
2347-
return Future{CallMeta}(call, interp, sv) do call, interp, sv
2346+
return Future{CallMeta}(call, interp, sv) do call, _, _
23482347
return CallMeta(Nothing, Any, Effects(), FinalizerInfo(call.info, call.effects))
23492348
end
23502349
end
@@ -2369,7 +2368,7 @@ function abstract_throw(interp::AbstractInterpreter, argtypes::Vector{Any}, ::Ab
23692368
return Future(CallMeta(Union{}, exct, EFFECTS_THROWS, NoCallInfo()))
23702369
end
23712370

2372-
function abstract_throw_methoderror(interp::AbstractInterpreter, argtypes::Vector{Any}, ::AbsIntState)
2371+
function abstract_throw_methoderror(::AbstractInterpreter, argtypes::Vector{Any}, ::AbsIntState)
23732372
exct = if length(argtypes) == 1
23742373
ArgumentError
23752374
elseif !isvarargtype(argtypes[2])
@@ -2579,7 +2578,7 @@ end
25792578
function abstract_eval_replaceglobal!(interp::AbstractInterpreter, sv::AbsIntState, saw_latestworld::Bool, argtypes::Vector{Any})
25802579
if !isvarargtype(argtypes[end])
25812580
if length(argtypes) in (5, 6, 7)
2582-
(M, s, x, v) = argtypes[2], argtypes[3], argtypes[4], argtypes[5]
2581+
(M, s, v) = argtypes[2], argtypes[3], argtypes[5]
25832582
T = nothing
25842583
if isa(M, Const) && isa(s, Const)
25852584
M, s = M.val, s.val
@@ -2855,7 +2854,6 @@ function abstract_call_opaque_closure(interp::AbstractInterpreter,
28552854
end
28562855

28572856
function most_general_argtypes(closure::PartialOpaque)
2858-
ret = Any[]
28592857
cc = widenconst(closure)
28602858
argt = (unwrap_unionall(cc)::DataType).parameters[1]
28612859
if !isa(argt, DataType) || argt.name !== typename(Tuple)
@@ -3054,7 +3052,7 @@ function abstract_call(interp::AbstractInterpreter, arginfo::ArgInfo, sstate::St
30543052
end
30553053
si = StmtInfo(!unused, sstate.saw_latestworld)
30563054
call = abstract_call(interp, arginfo, si, sv)::Future
3057-
Future{Any}(call, interp, sv) do call, interp, sv
3055+
Future{Any}(call, interp, sv) do call, _, sv
30583056
# this only is needed for the side-effect, sequenced before any task tries to consume the return value,
30593057
# which this will do even without returning this Future
30603058
sv.stmt_info[sv.currpc] = call.info
@@ -3072,7 +3070,7 @@ function abstract_eval_call(interp::AbstractInterpreter, e::Expr, sstate::Statem
30723070
end
30733071
arginfo = ArgInfo(ea, argtypes)
30743072
call = abstract_call(interp, arginfo, sstate, sv)::Future
3075-
return Future{RTEffects}(call, interp, sv) do call, interp, sv
3073+
return Future{RTEffects}(call, interp, sv) do call, _, _
30763074
(; rt, exct, effects, refinements) = call
30773075
return RTEffects(rt, exct, effects, refinements)
30783076
end
@@ -3081,7 +3079,7 @@ end
30813079
function abstract_eval_new(interp::AbstractInterpreter, e::Expr, sstate::StatementState,
30823080
sv::AbsIntState)
30833081
𝕃ᵢ = typeinf_lattice(interp)
3084-
rt, isexact = instanceof_tfunc(abstract_eval_value(interp, e.args[1], sstate, sv), true)
3082+
rt, _... = instanceof_tfunc(abstract_eval_value(interp, e.args[1], sstate, sv), true)
30853083
ut = unwrap_unionall(rt)
30863084
exct = Union{ErrorException,TypeError}
30873085
if isa(ut, DataType) && !isabstracttype(ut)
@@ -3234,7 +3232,7 @@ function abstract_eval_new_opaque_closure(interp::AbstractInterpreter, e::Expr,
32343232
pushfirst!(argtypes, rt.env)
32353233
callinfo = abstract_call_opaque_closure(interp, rt,
32363234
ArgInfo(nothing, argtypes), StmtInfo(true, false), sv, #=check=#false)::Future
3237-
Future{Any}(callinfo, interp, sv) do callinfo, interp, sv
3235+
Future{Any}(callinfo, interp, sv) do callinfo, _, sv
32383236
sv.stmt_info[sv.currpc] = OpaqueClosureCreateInfo(callinfo)
32393237
nothing
32403238
end
@@ -3311,7 +3309,7 @@ function abstract_eval_isdefinedglobal(interp::AbstractInterpreter, mod::Module,
33113309
end
33123310
end
33133311

3314-
(valid_worlds, rte) = abstract_load_all_consistent_leaf_partitions(interp, gr, sv.world)
3312+
(_, rte) = abstract_load_all_consistent_leaf_partitions(interp, gr, sv.world)
33153313
if rte.exct == Union{}
33163314
rt = Const(true)
33173315
elseif rte.rt === Union{} && rte.exct === UndefVarError
@@ -3607,13 +3605,10 @@ world_range(compact::IncrementalCompact) = world_range(compact.ir)
36073605

36083606
function abstract_eval_globalref_type(g::GlobalRef, src::Union{CodeInfo, IRCode, IncrementalCompact})
36093607
worlds = world_range(src)
3610-
partition = lookup_binding_partition(min_world(worlds), g)
3611-
36123608
(valid_worlds, rte) = abstract_load_all_consistent_leaf_partitions(nothing, g, WorldWithRange(min_world(worlds), worlds))
36133609
if min_world(valid_worlds) > min_world(worlds) || max_world(valid_worlds) < max_world(worlds)
36143610
return Any
36153611
end
3616-
36173612
return rte.rt
36183613
end
36193614

@@ -3730,7 +3725,7 @@ scan_leaf_partitions(query::F, interp::AbstractInterpreter, g::GlobalRef, wwr::W
37303725
scan_specified_partitions(query, walk_binding_partition, interp, g, wwr)
37313726

37323727
function scan_partitions(query::F, interp::AbstractInterpreter, g::GlobalRef, wwr::WorldWithRange) where F
3733-
walk_binding_partition = function (b::Core.Binding, partition::Core.BindingPartition, world::UInt)
3728+
walk_binding_partition = function (b::Core.Binding, partition::Core.BindingPartition, ::UInt)
37343729
Pair{WorldRange, Pair{Core.Binding, Core.BindingPartition}}(
37353730
WorldRange(partition.min_world, partition.max_world), b=>partition)
37363731
end
@@ -4489,7 +4484,6 @@ function typeinf(interp::AbstractInterpreter, frame::InferenceState)
44894484
nextstates = CurrentState[]
44904485
takenext = frame.frameid
44914486
minwarn = warnlength
4492-
takeprev = 0
44934487
while takenext >= frame.frameid
44944488
callee = takenext == 0 ? frame : callstack[takenext]::InferenceState
44954489
if !isempty(callstack)

Compiler/src/bootstrap.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function bootstrap!()
4848
end
4949
end
5050
starttime = time()
51-
methods = Any[]
5251
world = get_world_counter()
5352
for f in fs
5453
if isa(f, DataType) && f.name === typename(Tuple)

Compiler/src/inferenceresult.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
function matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance)
3+
function matching_cache_argtypes(::AbstractLattice, mi::MethodInstance)
44
(; def, specTypes) = mi
55
return most_general_argtypes(isa(def, Method) ? def : nothing, specTypes)
66
end
@@ -16,7 +16,7 @@ struct WidenedArgtypes
1616
argtypes::Vector{Any}
1717
end
1818

19-
function matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance,
19+
function matching_cache_argtypes(𝕃::AbstractLattice, ::MethodInstance,
2020
simple_argtypes::Union{SimpleArgtypes, WidenedArgtypes},
2121
cache_argtypes::Vector{Any})
2222
(; argtypes) = simple_argtypes

Compiler/src/inferencestate.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,12 @@ mutable struct InferenceState
340340
ssavalue_uses = find_ssavalue_uses(code, nssavalues)
341341
nstmts = length(code)
342342
edges = []
343-
stmt_info = CallInfo[ NoCallInfo() for i = 1:nstmts ]
343+
stmt_info = CallInfo[ NoCallInfo() for _ = 1:nstmts ]
344344

345345
nslots = length(src.slotflags)
346346
slottypes = Vector{Any}(undef, nslots)
347-
bb_saw_latestworld = Bool[false for i = 1:length(cfg.blocks)]
348-
bb_vartables = Union{Nothing,VarTable}[ nothing for i = 1:length(cfg.blocks) ]
347+
bb_saw_latestworld = Bool[false for _ = 1:length(cfg.blocks)]
348+
bb_vartables = Union{Nothing,VarTable}[ nothing for _ = 1:length(cfg.blocks) ]
349349
bb_vartable1 = bb_vartables[1] = VarTable(undef, nslots)
350350
argtypes = result.argtypes
351351

@@ -360,7 +360,7 @@ mutable struct InferenceState
360360
slottypes[i] = argtyp
361361
bb_vartable1[i] = VarState(argtyp, i > nargtypes)
362362
end
363-
src.ssavaluetypes = ssavaluetypes = Any[ NOT_FOUND for i = 1:nssavalues ]
363+
src.ssavaluetypes = ssavaluetypes = Any[ NOT_FOUND for _ = 1:nssavalues ]
364364
ssaflags = copy(src.ssaflags)
365365

366366
unreachable = BitSet()
@@ -549,7 +549,7 @@ function (::ComputeTryCatch{Handler})(code::Vector{Any}, bbs::Union{Vector{Basic
549549
l += 1
550550
end
551551
cur_hand = cur_stacks[1]
552-
for i = 1:l
552+
for _ = 1:l
553553
cur_hand = handler_at[get_enter_idx(handlers[cur_hand])][1]
554554
end
555555
cur_stacks = (cur_hand, cur_stacks[2])

Compiler/src/optimize.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function src_inlining_policy(interp::AbstractInterpreter, mi::MethodInstance,
143143
return src_inlining_policy(interp, src, info, stmt_flag)
144144
end
145145

146-
function src_inlining_policy(interp::AbstractInterpreter,
146+
function src_inlining_policy(::AbstractInterpreter,
147147
@nospecialize(src), @nospecialize(info::CallInfo), stmt_flag::UInt32)
148148
isa(src, OptimizationState) && (src = src.src)
149149
if isa(src, MaybeCompressed)
@@ -238,17 +238,17 @@ function OptimizationState(mi::MethodInstance, src::CodeInfo, interp::AbstractIn
238238
# prepare src for running optimization passes if it isn't already
239239
nssavalues = src.ssavaluetypes
240240
if nssavalues isa Int
241-
src.ssavaluetypes = Any[ Any for i = 1:nssavalues ]
241+
src.ssavaluetypes = Any[ Any for _ = 1:nssavalues ]
242242
else
243243
nssavalues = length(src.ssavaluetypes::Vector{Any})
244244
end
245245
sptypes = sptypes_from_meth_instance(mi)
246246
nslots = length(src.slotflags)
247247
slottypes = src.slottypes
248248
if slottypes === nothing
249-
slottypes = Any[ Any for i = 1:nslots ]
249+
slottypes = Any[ Any for _ = 1:nslots ]
250250
end
251-
stmt_info = CallInfo[ NoCallInfo() for i = 1:nssavalues ]
251+
stmt_info = CallInfo[ NoCallInfo() for _ = 1:nssavalues ]
252252
# cache some useful state computations
253253
def = mi.def
254254
mod = isa(def, Method) ? def.module : def
@@ -258,7 +258,7 @@ function OptimizationState(mi::MethodInstance, src::CodeInfo, interp::AbstractIn
258258
cfg = compute_basic_blocks(src.code)
259259
unreachable = BitSet()
260260
bb_vartables = Union{VarTable,Nothing}[]
261-
for block = 1:length(cfg.blocks)
261+
for _ = 1:length(cfg.blocks)
262262
push!(bb_vartables, VarState[
263263
VarState(slottypes[slot], src.slotflags[slot] & SLOT_USEDUNDEF != 0)
264264
for slot = 1:nslots
@@ -554,7 +554,7 @@ abstract_eval_ssavalue(s::SSAValue, src::Union{IRCode,IncrementalCompact}) = typ
554554
555555
Called at the end of optimization to store the resulting IR back into the OptimizationState.
556556
"""
557-
function finishopt!(interp::AbstractInterpreter, opt::OptimizationState, ir::IRCode)
557+
function finishopt!(::AbstractInterpreter, opt::OptimizationState, ir::IRCode)
558558
opt.optresult = OptimizationResult(ir, ccall(:jl_ir_flag_inlining, UInt8, (Any,), opt.src), false)
559559
return nothing
560560
end
@@ -1013,7 +1013,7 @@ function ipo_dataflow_analysis!(interp::AbstractInterpreter, opt::OptimizationSt
10131013
check_inconsistentcy!(sv, scanner)
10141014
else
10151015
# No longer any dataflow concerns, just scan the flags
1016-
scan!(scanner, false) do inst::Instruction, lstmt::Int, bb::Int
1016+
scan!(scanner, false) do inst::Instruction, ::Int, ::Int
10171017
scan_non_dataflow_flags!(inst, sv)
10181018
# bail out early if there are no possibilities to refine the effects
10191019
if !any_refinable(sv)
@@ -1337,7 +1337,6 @@ end
13371337

13381338
function slot2reg(ir::IRCode, ci::CodeInfo, sv::OptimizationState)
13391339
# need `ci` for the slot metadata, IR for the code
1340-
svdef = sv.linfo.def
13411340
@zone "CC: DOMTREE_1" domtree = construct_domtree(ir)
13421341
defuse_insts = scan_slot_def_use(Int(ci.nargs), ci, ir.stmts.stmt)
13431342
𝕃ₒ = optimizer_lattice(sv.inlining.interp)

Compiler/src/stmtinfo.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function add_one_edge!(edges::Vector{Any}, edge::CodeInstance)
135135
push!(edges, edge)
136136
nothing
137137
end
138-
nsplit_impl(info::MethodMatchInfo) = 1
138+
nsplit_impl(::MethodMatchInfo) = 1
139139
getsplit_impl(info::MethodMatchInfo, idx::Int) = (@assert idx == 1; info.results)
140140
getresult_impl(::MethodMatchInfo, ::Int) = nothing
141141

@@ -279,7 +279,7 @@ struct InvokeCICallInfo <: CallInfo
279279
end
280280
add_edges_impl(edges::Vector{Any}, info::InvokeCICallInfo) =
281281
add_inlining_edge!(edges, info.edge)
282-
nsplit_impl(info::InvokeCICallInfo) = 0
282+
nsplit_impl(::InvokeCICallInfo) = 0
283283

284284
"""
285285
info::InvokeCallInfo
@@ -392,7 +392,7 @@ function add_inlining_edge!(edges::Vector{Any}, edge::CodeInstance)
392392
nothing
393393
end
394394

395-
nsplit_impl(info::InvokeCallInfo) = 1
395+
nsplit_impl(::InvokeCallInfo) = 1
396396
getsplit_impl(info::InvokeCallInfo, idx::Int) = (@assert idx == 1; MethodLookupResult(Core.MethodMatch[info.match],
397397
WorldRange(typemin(UInt), typemax(UInt)), false))
398398
getresult_impl(info::InvokeCallInfo, idx::Int) = (@assert idx == 1; info.result)

0 commit comments

Comments
 (0)