Skip to content

Bugfix on dealing with args #149

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

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/tapedfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ end
struct NOOPInstruction <: AbstractInstruction end

@inline result(t::TapedFunction) = t.binding_values[t.retval_binding_slot]
@inline function _arg(tf::TapedFunction, i::Int; default=nothing)
length(tf.arg_binding_slots) < i && return default
tf.arg_binding_slots[i] > 0 && return tf.binding_values[tf.arg_binding_slots[i]]
return default
end
@inline function _arg!(tf::TapedFunction, i::Int, v)
length(tf.arg_binding_slots) >= i &&
tf.arg_binding_slots[i] > 0 && _update_var!(tf, tf.arg_binding_slots[i], v)
end

function (tf::TapedFunction)(args...; callback=nothing, continuation=false)
if !continuation # reset counter and retval_binding_slot to run from the start
Expand All @@ -143,10 +152,10 @@ function (tf::TapedFunction)(args...; callback=nothing, continuation=false)
# set args
if tf.counter <= 1
# The first slot in `binding_values` is assumed to be `tf.func`.
tf.arg_binding_slots[1] > 0 && _update_var!(tf, tf.arg_binding_slots[1], tf.func)
_arg!(tf, 1, tf.func)
for i in 1:length(args) # the subsequent arg_binding_slots are arguments
slot = i + 1
tf.arg_binding_slots[slot] > 0 && _update_var!(tf, tf.arg_binding_slots[slot], args[i])
_arg!(tf, slot, args[i])
end
end

Expand Down Expand Up @@ -314,7 +323,7 @@ function translate!(tape::RawTape, ir::Core.CodeInfo)
for (k, v) in bcache
isa(k, Core.SlotNumber) && (slots[k.id] = v)
end
arg_binding_slots = fill(0, maximum(keys(slots)))
arg_binding_slots = fill(0, maximum(keys(slots); init=0))
for (k, v) in slots
arg_binding_slots[k] = v
end
Expand Down
2 changes: 1 addition & 1 deletion src/tapedtask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function Base.copy(t::TapedTask; args=())
# the task is running, we find the real args from the copied binding_values
map(1:length(t.args)) do i
s = i + 1
tf.arg_binding_slots[s] > 0 ? tf.binding_values[tf.arg_binding_slots[s]] : t.args[i]
_arg(tf, s; default=t.args[i])
end
else
# the task is not started yet, but no args is given
Expand Down
9 changes: 9 additions & 0 deletions test/tapedtask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,14 @@
ttask3 = copy(ttask; args=(4,))
@test consume(ttask3) == 5
end

@testset "Issue-148, unused argument" begin
function f(x)
produce(1)
end

ttask = TapedTask(f, 2)
@test consume(ttask) == 1
end
end
end