IRTools.jl icon indicating copy to clipboard operation
IRTools.jl copied to clipboard

Do dynamo call broken for calls inside of methods

Open femtomc opened this issue 5 years ago • 0 comments

I found this interesting bug today as I was trying to move some of my code away from using macros. In the following, I'm using anonymous funcs, but I encountered the same bug in methods.

anon = () -> begin
    tr = Trace()
    tr() do
        foo()
    end
    tr
end

This code works fine when you evaluate anon - it returns the Trace, which has been modified in the dynamo call, as expected. However, if I abstract out the call to foo:

anon = (call, args) -> begin
    tr = Trace()
    tr() do
        call(args...)
    end
    tr
end

the dynamo does not apply (i.e. the Trace is not mutated, as a side effect). I found a related bug as well - here, foo is nullary function so you might expect this to work:

anon = call -> begin
    tr = Trace()
    tr() do
        call()
    end
    tr
end

but when I pass in foo as anon(foo), it throws:

LoadError: BoundsError: attempt to access 0-element Array{Any,1} at index [0]

This is obviously not what happens outside of the dynamo, i.e.:

anon2 = call -> begin
    call()
end

returns the result of calling foo.


Lastly, maybe I should show what the result of the dynamo is:

tr = Trace()
tr() do
    foo()
end
println(tr, [:val])

results in

| <> (:foo => :bar) => (:q => 2) <>
|          val  = 1.0100076975990129
|
| <> (:foo => :bar) => (:q => 10) <>
|          val  = -1.0067463813037731
|
| <> (:foo => :bar) => (:q => 9) <>
|          val  = 0.3533852496897451
|
| <> :foo => :y <>
|          val  = -0.29516473908558977
|
| <> (:foo => :bar) => (:q => 3) <>
|          val  = -0.3524573630007816
...

when called outside of methods.

Edit: I'm guessing this is an @generated thing?

femtomc avatar May 21 '20 13:05 femtomc