Skip to content

Commit 194ceab

Browse files
committed
Use insert_instruction! in more places
1 parent 7d0e0a0 commit 194ceab

File tree

3 files changed

+47
-62
lines changed

3 files changed

+47
-62
lines changed

src/analysis/structural.jl

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,8 @@ function _structural_analysis!(ci::CodeInstance, world::UInt, settings::Settings
362362
compact.result_idx -= 1
363363
new_args = _flatten_parameter!(Compiler.optimizer_lattice(refiner), compact, callee_codeinst.inferred.ir.argtypes, arg->stmt.args[arg+1], line, settings)
364364

365-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
366-
new_call = insert_node_here!(compact,
367-
NewInstruction(Expr(:invoke, (StructuralSSARef(compact.result_idx), callee_codeinst), new_args...), stmtype, info, thisline, stmtflags))
365+
new_call = insert_instruction!(compact, settings, @__SOURCE__,
366+
NewInstruction(Expr(:invoke, (StructuralSSARef(compact.result_idx), callee_codeinst), new_args...), stmtype, info, line, stmtflags))
368367
compact.ssa_rename[compact.idx - 1] = new_call
369368

370369
cms = CallerMappingState(result, refiner.var_to_diff, refiner.varclassification, refiner.varkinds, eqclassification, eqkinds)
@@ -388,8 +387,7 @@ function _structural_analysis!(ci::CodeInstance, world::UInt, settings::Settings
388387
Compiler.delete_inst_here!(compact)
389388

390389
(new_ret, ultimate_rt) = rewrite_ipo_return!(Compiler.typeinf_lattice(refiner), compact, line, settings, ret_stmt.val, ultimate_rt, eqvars)
391-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
392-
insert_node_here!(compact, NewInstruction(ReturnNode(new_ret), ultimate_rt, Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
390+
insert_instruction!(compact, settings, @__SOURCE__, NewInstruction(ReturnNode(new_ret), ultimate_rt, Compiler.NoCallInfo(), line, Compiler.IR_FLAG_REFINED), reverse_affinity = true)
393391
elseif isa(ultimate_rt, Type)
394392
# If we don't have any internal variables (in which case we might have to to do a more aggressive rewrite), strengthen the incidence
395393
# by demoting to full incidence over the argument variables. Incidence is not allowed to propagate through global mutable state, so
@@ -427,27 +425,23 @@ function rewrite_ipo_return!(𝕃, compact::IncrementalCompact, line, settings,
427425
new_types = Any[]
428426
for i = 1:length(ultimate_rt.fields)
429427
ssa_type = Compiler.getfield_tfunc(𝕃, ultimate_rt, Const(i))
430-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
431-
ssa_field = insert_node_here!(compact,
432-
NewInstruction(Expr(:call, getfield, variable), ssa_type, Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
428+
ssa_field = insert_instruction!(compact, settings, @__SOURCE__,
429+
NewInstruction(Expr(:call, getfield, variable), ssa_type, Compiler.NoCallInfo(), line, Compiler.IR_FLAG_REFINED), reverse_affinity = true)
433430

434431
(new_field, new_type) = rewrite_ipo_return!(𝕃, compact, line, settings, ssa_field, ssa_type, eqvars)
435432
push!(new_fields, new_field)
436433
push!(new_types, new_type)
437434
end
438435
newT = Compiler.PartialStruct(ultimate_rt.typ, new_types)
439436
if widenconst(ultimate_rt) <: Tuple
440-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
441-
retssa = insert_node_here!(compact,
442-
NewInstruction(Expr(:call, tuple, new_fields...), newT, Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
437+
retssa = insert_instruction!(compact, settings, @__SOURCE__,
438+
NewInstruction(Expr(:call, tuple, new_fields...), newT, Compiler.NoCallInfo(), line, Compiler.IR_FLAG_REFINED), reverse_affinity = true)
443439
else
444-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
445-
T = insert_node_here!(compact,
446-
NewInstruction(Expr(:call, typeof, ssa), Type, Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
440+
T = insert_instruction!(compact, settings, @__SOURCE__,
441+
NewInstruction(Expr(:call, typeof, ssa), Type, Compiler.NoCallInfo(), line, Compiler.IR_FLAG_REFINED), reverse_affinity = true)
447442

448-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
449-
retssa = insert_node_here!(compact,
450-
NewInstruction(Expr(:new, T, new_fields...), newT, Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
443+
retssa = insert_instruction!(compact, settings, @__SOURCE__,
444+
NewInstruction(Expr(:new, T, new_fields...), newT, Compiler.NoCallInfo(), line, Compiler.IR_FLAG_REFINED), reverse_affinity = true)
451445
end
452446
return Pair{Any, Any}(retssa, newT)
453447
end
@@ -460,9 +454,8 @@ function rewrite_ipo_return!(𝕃, compact::IncrementalCompact, line, settings,
460454
push!(eqvars.varclassification, External)
461455
push!(eqvars.varkinds, Intrinsics.Continuous)
462456

463-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
464-
new_var_ssa = insert_node_here!(compact,
465-
NewInstruction(Expr(:invoke, nothing, variable), Incidence(nonlinrepl), Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
457+
new_var_ssa = insert_instruction!(compact, settings,
458+
NewInstruction(Expr(:invoke, nothing, variable), Incidence(nonlinrepl), Compiler.NoCallInfo(), line, Compiler.IR_FLAG_REFINED), true)
466459

467460
eq_incidence = ultimate_rt - Incidence(nonlinrepl)
468461
push!(eqvars.total_incidence, eq_incidence)
@@ -471,17 +464,14 @@ function rewrite_ipo_return!(𝕃, compact::IncrementalCompact, line, settings,
471464
push!(eqvars.eqkinds, Intrinsics.Always)
472465
new_eq = length(eqvars.total_incidence)
473466

474-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
475-
new_eq_ssa = insert_node_here!(compact,
476-
NewInstruction(Expr(:invoke, nothing, equation), Eq(new_eq), Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
467+
new_eq_ssa = insert_instruction!(compact, settings, @__SOURCE__,
468+
NewInstruction(Expr(:invoke, nothing, equation), Eq(new_eq), Compiler.NoCallInfo(), LINE, Compiler.IR_FLAG_REFINED), true)
477469

478-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
479-
eq_val_ssa = insert_node_here!(compact,
480-
NewInstruction(Expr(:call, InternalIntrinsics.assign_var, new_var_ssa, ssa), eq_incidence, Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
470+
eq_val_ssa = insert_instruction!(compact, settings, @__SOURCE__,
471+
NewInstruction(Expr(:call, InternalIntrinsics.assign_var, new_var_ssa, ssa), eq_incidence, Compiler.NoCallInfo(), LINE, Compiler.IR_FLAG_REFINED), true)
481472

482-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), line)
483-
eq_call_ssa = insert_node_here!(compact,
484-
NewInstruction(Expr(:invoke, nothing, new_eq_ssa, eq_val_ssa), Nothing, Compiler.NoCallInfo(), thisline, Compiler.IR_FLAG_REFINED), true)
473+
eq_call_ssa = insert_instruction!(compact, settings, @__SOURCE__,
474+
NewInstruction(Expr(:invoke, nothing, new_eq_ssa, eq_val_ssa), Nothing, Compiler.NoCallInfo(), LINE, Compiler.IR_FLAG_REFINED), true)
485475

486476
T = widenconst(ultimate_rt)
487477
# TODO: We don't have a way to express that the return value is directly this variable for arbitrary types

src/transform/tearing/schedule.jl

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function ir_add!(compact::IncrementalCompact, line, settings::Settings, @nospeci
3838
(b === nothing || b === 0.) && return _a
3939
(a === nothing || b === 0.) && return _b
4040
source = @something(source, @__SOURCE__)
41-
idx = _insert_node_here!(compact, line, settings, source, :($a + $b), Any)
41+
idx = insert_instruction!(compact, line, settings, source, :($a + $b), Any)
4242
compact[idx][:flag] |= Compiler.IR_FLAG_REFINED
4343
idx
4444
end
@@ -48,7 +48,7 @@ function ir_mul_const!(compact, line, settings, coeff::Float64, _a, source = not
4848
return _a
4949
end
5050
source = @something(source, @__SOURCE__)
51-
idx = _insert_node_here!(compact, line, settings, source, :($coeff * $_a), Any)
51+
idx = insert_instruction!(compact, line, settings, source, :($coeff * $_a), Any)
5252
compact[idx][:flag] |= Compiler.IR_FLAG_REFINED
5353
return idx
5454
end
@@ -228,8 +228,7 @@ function schedule_nonlinear!(compact, settings, param_vars, var_eq_matching, ir,
228228
new_stmt.args[i] = arg
229229
end
230230

231-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), inst.line)
232-
ret = insert_node_here!(compact, NewInstruction(inst; stmt=new_stmt, line=thisline))
231+
ret = insert_instruction!(compact, settings, @__SOURCE__, NewInstruction(inst; stmt=new_stmt, line))
233232
end
234233

235234
ssa_rename[val.id] = isa(ret, SSAValue) ? CarriedSSAValue(ordinal, ret.id) : ret
@@ -904,10 +903,8 @@ function tearing_schedule!(state::TransformationState, ci::CodeInstance, key::To
904903
push!(in_param_vars.args, argval)
905904
end
906905

907-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), inst.line)
908-
new_stmt.args[2] = insert_node_here!(compact, NewInstruction(inst; stmt=in_param_vars, type=Tuple, flag=UInt32(0), line=thisline))
909-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), inst.line)
910-
sstate = insert_node_here!(compact, NewInstruction(inst; stmt=new_stmt, type=Tuple, flag=UInt32(0), line=thisline))
906+
new_stmt.args[2] = insert_instruction!(compact, settinsg, NewInstruction(inst; stmt=in_param_vars, type=Tuple, flag=UInt32(0), line))
907+
sstate = insert_instruction!(compact, settinsg, NewInstruction(inst; stmt=new_stmt, type=Tuple, flag=UInt32(0), line))
911908
carried_states[sref] = CarriedSSAValue(0, sstate.id)
912909
else
913910
carried_states[sref] = isdefined(callee_sicm_ci, :rettype_const) ? callee_sicm_ci.rettype_const : callee_sicm_ci.rettype.instance
@@ -1031,8 +1028,7 @@ function tearing_schedule!(state::TransformationState, ci::CodeInstance, key::To
10311028
push!(in_vars.args, argval)
10321029
end
10331030

1034-
thisline = maybe_insert_debuginfo!(compact1, settings, @__SOURCE__(), eqinst.line)
1035-
in_vars_ssa = insert_node_here!(compact1, NewInstruction(eqinst; stmt=in_vars, type=Tuple, line=thisline))
1031+
in_vars_ssa = insert_instruction!(compact1, settings, @__SOURCE__, NewInstruction(eqinst; stmt=in_vars, type=Tuple, line))
10361032

10371033
new_stmt = copy(eqinst[:stmt])
10381034
resize!(new_stmt.args, 2)
@@ -1050,21 +1046,17 @@ function tearing_schedule!(state::TransformationState, ci::CodeInstance, key::To
10501046

10511047
callee_ordinals[eq] = callee_ordinal+1
10521048

1053-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), eqinst.line)
1054-
this_call = insert_node_here!(compact1, NewInstruction(eqinst; stmt=urs[], line=thisline))
1049+
this_call = insert_instruction!(compact1, settings, @__SOURCE__, NewInstruction(eqinst; stmt=urs[], line))
10551050

1056-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), eqinst.line)
1057-
this_eqresids = insert_node_here!(compact1, NewInstruction(eqinst; stmt=Expr(:call, getfield, this_call, 1), type=Any, line=thisline))
1051+
this_eqresids = insert_instruction!(compact1, settings, @__SOURCE__, NewInstruction(eqinst; stmt=Expr(:call, getfield, this_call, 1), type=Any, line))
10581052

1059-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), eqinst.line)
1060-
new_state = insert_node_here!(compact1, NewInstruction(eqinst; stmt=Expr(:call, getfield, this_call, 2), type=Any, line=thisline))
1053+
new_state = insert_instruction!(compact1, settings, @__SOURCE__, NewInstruction(eqinst; stmt=Expr(:call, getfield, this_call, 2), type=Any, line))
10611054

10621055
carried_states[eq] = CarriedSSAValue(ordinal, new_state.id)
10631056

10641057
for (idx, this_callee_eq) in enumerate(callee_out_eqs)
10651058
this_eq = callee_eq_mapping[eq][this_callee_eq]
1066-
thisline = maybe_insert_debuginfo!(compact, settings, @__SOURCE__(), eqinst.line)
1067-
curval = insert_node_here!(compact1, NewInstruction(eqinst; stmt=Expr(:call, getfield, this_eqresids, idx), type=Any, line=thisline))
1059+
curval = insert_instruction!(compact1, settings, @__SOURCE__, NewInstruction(eqinst; stmt=Expr(:call, getfield, this_eqresids, idx), type=Any, line))
10681060
push!(eqs[this_eq][2], NewSSAValue(curval.id))
10691061
end
10701062
else

src/utils.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ end
103103

104104
"Get the current file location as a `LineNumberNode`."
105105
macro __SOURCE__()
106-
:(LineNumberNode($(__source__.line), $(QuoteNode(__source__.file))))
106+
return :(LineNumberNode($(__source__.line), $(QuoteNode(__source__.file))))
107107
end
108108

109109
"""
@@ -114,43 +114,46 @@ end
114114
"""
115115
macro insert_node_here(compact, line, settings, ex, reverse_affinity = false)
116116
source = :(LineNumberNode($(__source__.line), $(QuoteNode(__source__.file))))
117-
generate_insert_node_here(compact, line, settings, ex, source, reverse_affinity)
117+
return generate_insert_instruction(compact, line, settings, ex, source, reverse_affinity)
118118
end
119119

120-
function generate_insert_node_here(compact, line, settings, ex, source, reverse_affinity)
120+
function generate_insert_instruction(compact, line, settings, ex, source, reverse_affinity)
121121
isexpr(ex, :(::), 2) || throw(ArgumentError("Expected type-annotated expression, got $ex"))
122122
ex, type = ex.args
123123
compact = esc(compact)
124124
settings = esc(settings)
125125
line = esc(line)
126-
inst_ex = esc(process_inst_expr(ex))
126+
inst_ex = esc(process_instruction_expr(ex))
127127
type = esc(type)
128-
return :(_insert_node_here!($compact, $line, $settings, $source, $inst_ex, $type; reverse_affinity = $reverse_affinity))
128+
return :(insert_instruction!($compact, $line, $settings, $source, $inst_ex, $type; reverse_affinity = $reverse_affinity))
129129
end
130130

131-
function process_inst_expr(ex)
131+
function process_instruction_expr(ex)
132132
if isexpr(ex, :call) && isa(ex.args[1], QuoteNode)
133133
# The called "function" is a non-call `Expr` head
134134
ex = Expr(ex.args[1].value, ex.args[2:end]...)
135135
end
136136
isa(ex, Symbol) && return ex
137-
isexpr(ex, :return) && return :(ReturnNode($(ex.args...)))
137+
isexpr(ex, :return) && return :($ReturnNode($(ex.args...)))
138138
return :(Expr($(QuoteNode(ex.head)), $(ex.args...)))
139139
end
140140

141-
function _insert_node_here!(compact::IncrementalCompact, line, settings::Settings, source::LineNumberNode, args...; reverse_affinity::Bool = false)
141+
function insert_instruction!(compact::IncrementalCompact, line, settings::Settings, source::LineNumberNode, args...; reverse_affinity::Bool = false)
142142
line = maybe_insert_debuginfo!(compact, settings, source, line, compact.result_idx)
143-
_insert_node_here!(compact, line, args...; reverse_affinity)
143+
return insert_instruction!(compact, line, args...; reverse_affinity)
144144
end
145145

146-
# function _insert_node_here!(compact::IncrementalCompact, line, ex::Expr; reverse_affinity::Bool = false)
147-
# isexpr(ex, :(::), 2) || throw(ArgumentError("Expected type-annotated expression, got $ex"))
148-
# ex, type = ex.args
149-
# return _insert_node_here!(compact::IncrementalCompact, line, inst_ex, type; reverse_affinity)
150-
# end
146+
function insert_instruction!(compact::IncrementalCompact, settings::Settings, source::LineNumberNode, inst::NewInstruction; reverse_affinity::Bool = false)
147+
line = maybe_insert_debuginfo!(compact, settings, source, inst.line, compact.result_idx)
148+
inst_with_source = NewInstruction(inst.stmt, inst.type, inst.info, line, inst.flag)
149+
end
151150

152-
function _insert_node_here!(compact::IncrementalCompact, line, inst_ex, type; reverse_affinity::Bool = false)
151+
function insert_instruction!(compact::IncrementalCompact, line, inst_ex, type; reverse_affinity::Bool = false)
153152
inst = NewInstruction(inst_ex, type, line)
153+
return insert_instruction!(compact, inst; reverse_affinity)
154+
end
155+
156+
function insert_instruction!(compact::IncrementalCompact, inst::NewInstruction; reverse_affinity::Bool = false)
154157
return insert_node_here!(compact, inst, reverse_affinity)
155158
end
156159

0 commit comments

Comments
 (0)