-
Notifications
You must be signed in to change notification settings - Fork 10
backport basic refactor from PR#100 #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
mutable struct Instruction{F} | ||
fun::F | ||
input::Tuple | ||
output | ||
tape | ||
end | ||
|
||
abstract type AbstractInstruction end | ||
|
||
mutable struct Tape | ||
tape::Vector{Instruction} | ||
tape::Vector{<:AbstractInstruction} | ||
counter::Int | ||
owner | ||
end | ||
|
||
Tape() = Tape(Vector{Instruction}(), nothing) | ||
Tape(owner) = Tape(Vector{Instruction}(), owner) | ||
mutable struct Instruction{F} <: AbstractInstruction | ||
fun::F | ||
input::Tuple | ||
output | ||
tape::Tape | ||
end | ||
|
||
Tape() = Tape(Vector{AbstractInstruction}(), 1, nothing) | ||
Tape(owner) = Tape(Vector{AbstractInstruction}(), 1, owner) | ||
MacroTools.@forward Tape.tape Base.iterate, Base.length | ||
MacroTools.@forward Tape.tape Base.push!, Base.getindex, Base.lastindex | ||
const NULL_TAPE = Tape() | ||
|
||
function setowner!(tape::Tape, owner) | ||
tape.owner = owner | ||
end | ||
phipsgabler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
mutable struct Box{T} | ||
val::T | ||
end | ||
|
||
val(x) = x | ||
val(x::Box) = x.val | ||
box(x) = Box(x) | ||
any_box(x) = Box{Any}(x) | ||
box(x::Box) = x | ||
|
||
gettape(x) = nothing | ||
gettape(x::Instruction) = x.tape | ||
|
@@ -63,11 +69,20 @@ function (instr::Instruction{F})() where F | |
instr.output.val = output | ||
end | ||
|
||
function increase_counter(t::Tape) | ||
t.counter > length(t) && return | ||
# instr = t[t.counter] | ||
t.counter += 1 | ||
end | ||
phipsgabler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function run(tape::Tape, args...) | ||
input = map(box, args) | ||
tape[1].input = input | ||
if length(args) > 0 | ||
input = map(box, args) | ||
tape[1].input = input | ||
end | ||
for instruction in tape | ||
instruction() | ||
increase_counter(tape) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intuitively, I'd have expected this to also return the result of the function. Or is that extracted in another place? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The result is stored in |
||
|
||
|
@@ -77,21 +92,13 @@ function run_and_record!(tape::Tape, f, args...) | |
box(f(map(val, args)...)) | ||
catch e | ||
@warn e | ||
any_box(nothing) | ||
Box{Any}(nothing) | ||
end | ||
ins = Instruction(f, args, output, tape) | ||
push!(tape, ins) | ||
return output | ||
end | ||
|
||
function dry_record!(tape::Tape, f, args...) | ||
# We don't know the type of box.val now, so we use Box{Any} | ||
output = any_box(nothing) | ||
ins = Instruction(f, args, output, tape) | ||
push!(tape, ins) | ||
return output | ||
end | ||
|
||
function unbox_condition(ir) | ||
for blk in IRTools.blocks(ir) | ||
vars = keys(blk) | ||
|
@@ -188,27 +195,14 @@ function (tf::TapedFunction)(args...) | |
tape = IRTools.evalir(ir, tf.func, args...) | ||
tf.ir = ir | ||
tf.tape = tape | ||
tape.owner = tf | ||
setowner!(tape, tf) | ||
return result(tape) | ||
end | ||
# TODO: use cache | ||
run(tf.tape, args...) | ||
return result(tf.tape) | ||
end | ||
|
||
function dry_run(tf::TapedFunction) | ||
isempty(tf.tape) || (return tf) | ||
@assert tf.arity >= 0 "TapedFunction need a fixed arity to dry run." | ||
args = fill(nothing, tf.arity) | ||
ir = IRTools.@code_ir tf.func(args...) | ||
ir = intercept(ir; recorder=:dry_record!) | ||
tape = IRTools.evalir(ir, tf.func, args...) | ||
tf.ir = ir | ||
tf.tape = tape | ||
tape.owner = tf | ||
return tf | ||
end | ||
|
||
function Base.show(io::IO, tf::TapedFunction) | ||
buf = IOBuffer() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the extra buffer? You could directly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I use an extra buffer, because that there are too many calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good reason. Then put a comment there to remind future readers why this should not be refactored. |
||
println(buf, "TapedFunction:") | ||
|
Uh oh!
There was an error while loading. Please reload this page.