JuliaLabs / Cassette.jl

Overdub Your Julia Code
Other
370 stars 34 forks source link

More problems with `_apply_iterate`? #162

Closed KristofferC closed 4 years ago

KristofferC commented 4 years ago

I am looking at https://github.com/triscale-innov/GFlops.jl/issues/6 again (which still fails on the newest Cassette, note that the Project compat needs to be updated to use it) and doing something like:

using Cassette
Cassette.@context Ctx;

function Cassette.prehook(ctx::Ctx,
                          op::Any,
                          a::T1, b::T2) where {T1, T2}
    println((op, T1, T2))
end

ctx = Ctx()
Cassette.overdub(ctx, ()->pi*2.0)

on 1.3 and 1.4 shows that the two last instructions

(*, Float64, Float64)
(mul_float, Float64, Float64)

are missing on 1.4. Stepping through it in the debugger

In promote(x, y) at promotion.jl:281
 280  function promote(x, y)
 281      @_inline_meta
 282      px, py = _promote(x, y)
 283      not_sametype((x,y), (px,py))
>284      px, py
 285  end

About to run: return (2.0, 3.141592653589793)
1|debug>
In *(x, y) at promotion.jl:312
>312  *(x::Number, y::Number) = *(promote(x,y)...)

About to run: (Core._apply_iterate)(iterate, *, (2.0, 3.141592653589793))
1|debug>
In *(x, y) at float.jl:405
>405  *(x::Float64, y::Float64) = mul_float(x, y)

About to run: (Core.Intrinsics.mul_float)(2.0, 3.141592653589793)
1|debug>
In *(x, y) at float.jl:405
>405  *(x::Float64, y::Float64) = mul_float(x, y)

About to run: return 6.283185307179586

We can see that Cassette returns when the __apply_iterate is called. This indicates that maybe https://github.com/jrevels/Cassette.jl/pull/158 didn't fix all instances of the problem.

KristofferC commented 4 years ago

Doing things manually shows that the overdub is not called?

julia> args = (*, (3.14, 2.0))
(*, (3.14, 2.0))

julia> f = iterate
iterate (generic function with 212 methods)

julia> Core._apply_iterate((args...) -> Cassette.overdub(ctx, f, args...), args...)
6.28

julia> Cassette.overdub(ctx, args[1], args[2]...)
(Core.Intrinsics.mul_float, Float64, Float64)
6.28
KristofferC commented 4 years ago

Ah, the args[1] function also need to be overdubbed.