Skip to content
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

Support creating IR from scratch #45

Merged
merged 2 commits into from
Jan 30, 2020
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
17 changes: 13 additions & 4 deletions src/reflection/dynamo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function fallthrough(args...)
Expr(:call, [:(args[$i]) for i = 1:length(args)]...))
end

# Used only for its CodeInfo
dummy(args...) = nothing

function dynamo(f, args...)
try
ir = transform(f, args...)::Union{IR,Expr,Nothing}
Expand All @@ -29,10 +32,16 @@ function dynamo(f, args...)
end
ir isa Expr && return ir
ir == nothing && return fallthrough(args...)
m = ir.meta::Meta
ir = varargs!(m, ir)
argnames!(m, :args)
_self = splicearg!(m, ir, Symbol("#self#"))
if ir.meta isa Meta
m = ir.meta
ir = varargs!(m, ir)
argnames!(m, :args)
pushfirst!(m.code.slotnames, Symbol("#self#"))
else
m = @meta dummy(1)
m.code.method_for_inference_limit_heuristics = nothing
end
_self = splicearg!(ir)
prewalk!(x -> x === self ? _self : x, ir)
return update!(m.code, ir)
end
Expand Down
3 changes: 1 addition & 2 deletions src/reflection/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ function varargs!(meta, ir::IR, n = 0)
end

# TODO this is hacky and leaves `ir.defs` incorrect
function splicearg!(meta, ir::IR, name)
function splicearg!(ir::IR)
args = arguments(ir)
push!(ir.defs, (1, -1))
arg = var(length(ir.defs))
pushfirst!(args, arg)
pushfirst!(ir.blocks[1].argtypes, Any)
pushfirst!(meta.code.slotnames, name)
return arg
end

Expand Down
10 changes: 10 additions & 0 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ ir[var(8)] = xcall(:+, var(5), var(2))
mul = func(ir)

@test mul(nothing, 10, 3) == 31

@dynamo function ir_add(_, _)
ir = IR()
args = argument!(ir)
x = push!(ir, xcall(:getindex, args, 1))
y = push!(ir, xcall(:getindex, args, 2))
return!(ir, xcall(:+, x, y))
end

@test ir_add(5, 2) == 7