Skip to content

Commit

Permalink
remove explicit world age propagation; turns out tests pass regardles…
Browse files Browse the repository at this point in the history
…s as long as we reflect on world=typemax(UInt)

Note that this is not technically correct, since always treating world=typemax(UInt) is basically equivalent to reintroducing JuliaLang/julia#265. Thus, we still need to fix #6 eventually.
  • Loading branch information
jrevels committed May 11, 2018
1 parent 36ed46d commit be7f8d3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Cassette.jl
Expand Up @@ -10,7 +10,7 @@ struct Unused end

abstract type AbstractPass end
abstract type AbstractTag end
abstract type AbstractContext{w,P<:Union{AbstractPass,Unused},T<:Union{AbstractTag,Nothing}} end
abstract type AbstractContext{P<:Union{AbstractPass,Unused},T<:Union{AbstractTag,Nothing}} end

const UNUSED = Unused()
const MAX_ARGS = 20
Expand Down
11 changes: 3 additions & 8 deletions src/macros.jl
Expand Up @@ -24,22 +24,20 @@ macro context(Ctx)
Base.@pure $CtxTag(x) = $CtxTag(nothing, x)
Base.@pure $CtxTag(::E, ::X) where {E,X} = $CtxTag{E,objectid(X)}()

struct $Ctx{M,w,P<:Union{$Cassette.AbstractPass,$Cassette.Unused},T<:Union{$CtxTag,Nothing}} <: $Cassette.AbstractContext{w,P,T}
struct $Ctx{M,P<:Union{$Cassette.AbstractPass,$Cassette.Unused},T<:Union{$CtxTag,Nothing}} <: $Cassette.AbstractContext{P,T}
metadata::M
world::Val{w}
pass::P
tag::T
end

function $Ctx(;
metadata = $Cassette.UNUSED,
world::Val = Val($Cassette.get_world_age()),
pass::Union{$Cassette.AbstractPass,$Cassette.Unused} = $Cassette.UNUSED)
return $Ctx(metadata, world, pass, nothing)
return $Ctx(metadata, pass, nothing)
end

function $Cassette.tag(ctx::$Ctx, f)
return $Ctx(ctx.metadata, ctx.world, ctx.pass, $CtxTag(f))
return $Ctx(ctx.metadata, ctx.pass, $CtxTag(f))
end

# default primitives/execution definitions
Expand Down Expand Up @@ -260,11 +258,8 @@ function contextual_definition!(f, signature::Expr, body::Expr)
"method signature missing `where` clause; `$(CONTEXT_BINDING) <: ContextType` "*
"must be defined in your method definition's `where` clause")
signature = typify_signature(signature)
world_binding = gensym("world")
push!(signature.args, world_binding)
signature.args[1] = Expr(:call, f,
:($CONTEXT_BINDING::$CONTEXT_TYPE_BINDING),
:(::Val{$world_binding}),
signature.args[1].args...)
pushfirst!(body.args, Expr(:meta, :inline))
return esc(Expr(:function, signature, body))
Expand Down
30 changes: 15 additions & 15 deletions src/overdub.jl
Expand Up @@ -2,16 +2,16 @@
# contextual operations #
#########################

@inline prehook(::AbstractContext, ::Val{w}, ::Vararg{Any}) where {w} = nothing
@inline posthook(::AbstractContext, ::Val{w}, ::Vararg{Any}) where {w} = nothing
@inline is_user_primitive(ctx::AbstractContext, ::Val{w}, ::Vararg{Any}) where {w} = false
@inline is_core_primitive(ctx::AbstractContext, ::Val{w}, args...) where {w} = _is_core_primitive(ctx, args...)
@inline execution(ctx::AbstractContext, ::Val{w}, f, args...) where {w} = f(args...)
@inline prehook(::AbstractContext, ::Vararg{Any}) = nothing
@inline posthook(::AbstractContext, ::Vararg{Any}) = nothing
@inline is_user_primitive(ctx::AbstractContext, ::Vararg{Any}) = false
@inline is_core_primitive(ctx::AbstractContext, args...) = _is_core_primitive(ctx, args...)
@inline execution(ctx::AbstractContext, f, args...) = f(args...)

@generated function _is_core_primitive(::C, args...) where {w,C<:AbstractContext{w}}
# TODO: this is slow, we should try to check whether the reflection is possible
# without going through the whole process of actually computing it
if isa(reflect(args, w), Reflection)
if isa(reflect(args), Reflection)
result = :(false)
else
result = :(true)
Expand All @@ -27,13 +27,13 @@ end
###################

@inline function overdub_execute(ctx::AbstractContext, args...)
prehook(ctx, ctx.world, args...)
if is_user_primitive(ctx, ctx.world, args...)
output = execution(ctx, ctx.world, args...)
prehook(ctx, args...)
if is_user_primitive(ctx, args...)
output = execution(ctx, args...)
else
output = overdub_recurse(ctx, args...)
end
posthook(ctx, ctx.world, output, args...)
posthook(ctx, output, args...)
return output
end

Expand Down Expand Up @@ -125,17 +125,17 @@ function overdub_recurse_pass!(reflection::Reflection,
end

# `args` is `(typeof(original_function), map(typeof, original_args_tuple)...)`
function overdub_recurse_generator(world, pass, self, ctx, args::Tuple)
function overdub_recurse_generator(pass, self, ctx, args::Tuple)
try
reflection = reflect(args, world)
reflection = reflect(args)
if isa(reflection, Reflection)
overdub_recurse_pass!(reflection, pass)
body = reflection.code_info
@safe_debug "returning overdubbed CodeInfo" args body
else
body = quote
$(Expr(:meta, :inline))
$Cassette.execution($OVERDUB_CTX_SYMBOL, $OVERDUB_CTX_SYMBOL.world, $OVERDUB_ARGS_SYMBOL...)
$Cassette.execution($OVERDUB_CTX_SYMBOL, $OVERDUB_ARGS_SYMBOL...)
end
@safe_debug "no CodeInfo found; executing as primitive" args
end
Expand All @@ -151,14 +151,14 @@ end

function overdub_recurse_definition(pass, line, file)
return quote
function overdub_recurse($OVERDUB_CTX_SYMBOL::AbstractContext{world,pass}, $OVERDUB_ARGS_SYMBOL...) where {world,pass<:$pass}
function overdub_recurse($OVERDUB_CTX_SYMBOL::AbstractContext{pass}, $OVERDUB_ARGS_SYMBOL...) where {pass<:$pass}
$(Expr(:meta,
:generated,
Expr(:new,
Core.GeneratedFunctionStub,
:overdub_recurse_generator,
Any[:overdub_recurse, OVERDUB_CTX_SYMBOL, OVERDUB_ARGS_SYMBOL],
Any[:world, :pass],
Any[:pass],
line,
QuoteNode(Symbol(file)),
true)))
Expand Down
2 changes: 1 addition & 1 deletion test/ExampleTests.jl
Expand Up @@ -86,7 +86,7 @@ f, x, y = hypot, rand(), rand(2)
sig_collection = DataType[]
@context PassCtx
mypass = @pass (sig, cinfo) -> (push!(sig_collection, sig); cinfo)
@overdub(PassCtx(pass=mypass), sum(rand(3)))
@test @overdub(PassCtx(pass=mypass), sum(rand(MersenneTwister(1), 3))) === sum(rand(MersenneTwister(1), 3))
@test !isempty(sig_collection) && all(T -> T <: Tuple, sig_collection)

############################################################################################
Expand Down

0 comments on commit be7f8d3

Please sign in to comment.