Skip to content

Commit 7d0e0a0

Browse files
committed
Refactor insert_node_here macro
1 parent da1df0b commit 7d0e0a0

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

src/transform/tearing/schedule.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ function find_eqs_vars(state::TransformationState)
3333
find_eqs_vars(state.structure.graph, compact)
3434
end
3535

36-
function ir_add!(compact::IncrementalCompact, line, settings::Settings, @nospecialize(_a), @nospecialize(_b))
36+
function ir_add!(compact::IncrementalCompact, line, settings::Settings, @nospecialize(_a), @nospecialize(_b), source = nothing)
3737
a, b = _a, _b
3838
(b === nothing || b === 0.) && return _a
3939
(a === nothing || b === 0.) && return _b
40-
idx = @insert_node_here compact line settings (a + b)::Any
40+
source = @something(source, @__SOURCE__)
41+
idx = _insert_node_here!(compact, line, settings, source, :($a + $b), Any)
4142
compact[idx][:flag] |= Compiler.IR_FLAG_REFINED
4243
idx
4344
end
4445

45-
function ir_mul_const!(compact, line, settings, coeff::Float64, _a)
46+
function ir_mul_const!(compact, line, settings, coeff::Float64, _a, source = nothing)
4647
if isone(coeff)
4748
return _a
4849
end
49-
idx = @insert_node_here compact line settings (coeff * _a)::Any
50+
source = @something(source, @__SOURCE__)
51+
idx = _insert_node_here!(compact, line, settings, source, :($coeff * $_a), Any)
5052
compact[idx][:flag] |= Compiler.IR_FLAG_REFINED
5153
return idx
5254
end
@@ -60,7 +62,7 @@ end
6062

6163
function schedule_incidence!(compact, curval, incT::Const, var, line, settings; vars=nothing, schedule_missing_var! = nothing)
6264
if curval !== nothing
63-
return (ir_add!(compact, line, settings, curval, incT.val), nothing)
65+
return (ir_add!(compact, line, settings, curval, incT.val, @__SOURCE__), nothing)
6466
end
6567
return (incT.val, nothing)
6668
end
@@ -93,8 +95,8 @@ function schedule_incidence!(compact, curval, incT::Incidence, var, line, settin
9395
end
9496
end
9597

96-
acc = ir_mul_const!(compact, line, settings, coeff, lin_var_ssa)
97-
curval = curval === nothing ? acc : ir_add!(compact, line, settings, curval, acc)
98+
acc = ir_mul_const!(compact, line, settings, coeff, lin_var_ssa, @__SOURCE__)
99+
curval = curval === nothing ? acc : ir_add!(compact, line, settings, curval, acc, @__SOURCE__)
98100
end
99101
(curval, _) = schedule_incidence!(compact, curval, incT.typ, var, line, settings; vars, schedule_missing_var!)
100102
return (curval, thiscoeff)
@@ -1085,7 +1087,7 @@ function tearing_schedule!(state::TransformationState, ci::CodeInstance, key::To
10851087
this_nonlinearssa = SSAValue(eqcallssa.id)
10861088
line = compact1[eqcallssa][:line]
10871089
end
1088-
nonlinearssa = nonlinearssa === nothing ? this_nonlinearssa : ir_add!(compact1, line, settings, this_nonlinearssa, nonlinearssa)
1090+
nonlinearssa = nonlinearssa === nothing ? this_nonlinearssa : ir_add!(compact1, line, settings, this_nonlinearssa, nonlinearssa, @__SOURCE__)
10891091
end
10901092
mapping = result.eq_callee_mapping[eq]
10911093
if mapping !== nothing
@@ -1113,7 +1115,7 @@ function tearing_schedule!(state::TransformationState, ci::CodeInstance, key::To
11131115
curval = nonlinearssa
11141116
(curval, thiscoeff) = schedule_incidence!(compact1, curval, incT, var, line, settings; vars=var_sols, schedule_missing_var!)
11151117
@assert isa(thiscoeff, Float64)
1116-
curval = ir_mul_const!(compact1, line, settings, 1/thiscoeff, curval)
1118+
curval = ir_mul_const!(compact1, line, settings, 1/thiscoeff, curval, @__SOURCE__)
11171119
var_sols[var] = isa(curval, SSAValue) ? CarriedSSAValue(ordinal, curval.id) : curval
11181120
insert_solved_var_here!(compact1, var, curval, line)
11191121
else

src/utils.jl

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,31 +114,44 @@ end
114114
"""
115115
macro insert_node_here(compact, line, settings, ex, reverse_affinity = false)
116116
source = :(LineNumberNode($(__source__.line), $(QuoteNode(__source__.file))))
117-
line = :($DAECompiler.maybe_insert_debuginfo!($compact, $settings, $source, $line, $compact.result_idx))
118-
generate_insert_node_here(compact, line, ex, reverse_affinity)
117+
generate_insert_node_here(compact, line, settings, ex, source, reverse_affinity)
119118
end
120119

121-
function generate_insert_node_here(compact, line, ex, reverse_affinity)
120+
function generate_insert_node_here(compact, line, settings, ex, source, reverse_affinity)
122121
isexpr(ex, :(::), 2) || throw(ArgumentError("Expected type-annotated expression, got $ex"))
123122
ex, type = ex.args
124-
if isexpr(ex, :call) && isa(ex.args[1], QuoteNode)
125-
# The called "function" is a non-call `Expr` head
126-
ex = Expr(ex.args[1].value, ex.args[2:end]...)
127-
end
128123
compact = esc(compact)
124+
settings = esc(settings)
129125
line = esc(line)
126+
inst_ex = esc(process_inst_expr(ex))
130127
type = esc(type)
131-
if isa(ex, Symbol)
132-
inst_ex = ex
133-
elseif isexpr(ex, :return)
134-
inst_ex = :(ReturnNode($(ex.args...)))
135-
else
136-
inst_ex = :(Expr($(QuoteNode(ex.head)), $(ex.args...)))
137-
end
138-
return quote
139-
inst = NewInstruction($(esc(inst_ex)), $type, $line)
140-
insert_node_here!($compact, inst, $(esc(reverse_affinity)))
128+
return :(_insert_node_here!($compact, $line, $settings, $source, $inst_ex, $type; reverse_affinity = $reverse_affinity))
129+
end
130+
131+
function process_inst_expr(ex)
132+
if isexpr(ex, :call) && isa(ex.args[1], QuoteNode)
133+
# The called "function" is a non-call `Expr` head
134+
ex = Expr(ex.args[1].value, ex.args[2:end]...)
141135
end
136+
isa(ex, Symbol) && return ex
137+
isexpr(ex, :return) && return :(ReturnNode($(ex.args...)))
138+
return :(Expr($(QuoteNode(ex.head)), $(ex.args...)))
139+
end
140+
141+
function _insert_node_here!(compact::IncrementalCompact, line, settings::Settings, source::LineNumberNode, args...; reverse_affinity::Bool = false)
142+
line = maybe_insert_debuginfo!(compact, settings, source, line, compact.result_idx)
143+
_insert_node_here!(compact, line, args...; reverse_affinity)
144+
end
145+
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
151+
152+
function _insert_node_here!(compact::IncrementalCompact, line, inst_ex, type; reverse_affinity::Bool = false)
153+
inst = NewInstruction(inst_ex, type, line)
154+
return insert_node_here!(compact, inst, reverse_affinity)
142155
end
143156

144157
"""

0 commit comments

Comments
 (0)