Skip to content

Commit

Permalink
Merge pull request #57 from oxinabox/ox/ssamiss
Browse files Browse the repository at this point in the history
Redo how we decide where am allowed to call do Expr(isdefined)
  • Loading branch information
oxinabox authored Jul 14, 2019
2 parents f60c606 + c8c836c commit cc1561f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/pass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,38 @@ end

"""
created_on
Given an `ir` returns a vector the same length as slotnames,
Given an `Cassette.Reflection` returns a vector the same length as slotnames,
which each entry is the ir statment index for where the coresponding variable was delcared
"""
function created_on(ir)
created_stmt_ind = zeros(length(ir.slotnames)) # default to assuming everything created before start
for (ii,stmt) in enumerate(ir.code)
if stmt isa Core.NewvarNode
@assert created_stmt_ind[stmt.slot.id] == 0
created_stmt_ind[stmt.slot.id] = ii
end
function created_on(reflection)
# This is a simplification of
# https://github.com/JuliaLang/julia/blob/236df47251c203c71abd0604f2f19bf1f9c639fd/base/compiler/ssair/slot2ssa.jl#L47

ir = reflection.code_info
created_stmt_ind = fill(typemax(Int), length(ir.slotnames))

# #self# and all the arguments are created at start
nargs = reflection.method.nargs
if nargs > length(created_stmt_ind)
error("More arguments than slots")
end
for id in 1 : nargs
created_stmt_ind[id] = 0
end

# Scan for assignments or for uses
for (ii, stmt) in enumerate(ir.code)
if isexpr(stmt, :(=)) && stmt.args[1] isa Core.SlotNumber
id = stmt.args[1].id
created_stmt_ind[id] = min(created_stmt_ind[id], ii)
elseif isexpr(stmt, :call)
for arg in stmt.args
if arg isa Core.SlotNumber
id = arg.id
created_stmt_ind[id] = min(created_stmt_ind[id], ii)
end
end
end
end
return created_stmt_ind
end
Expand Down Expand Up @@ -163,7 +185,7 @@ it is the main method of this file, and calls all the ones defined earlier.
function instrument!(::Type{<:HandEvalCtx}, reflection::Cassette.Reflection)
ir = reflection.code_info

slot_created_ons = created_on(ir)
slot_created_ons = created_on(reflection)
extended_insert_statements!(
ir.code, ir.codelocs,
(stmt, i) -> stmt isa Expr ? enter_debug_statements_count(slot_created_ons, i) : nothing,
Expand Down
16 changes: 16 additions & 0 deletions test/test_pass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,19 @@ end
res = Cassette.recurse(ctx, danger11, 1)
@test res == 1
end

@testset "Closures that modify outer content" begin
function danger19()
y=2
function inner()
h=y
y=12
return h
end
inner()
end

ctx = HandEvalCtx(@__MODULE__)
res = Cassette.recurse(ctx, danger19)
@test res == 2
end

0 comments on commit cc1561f

Please sign in to comment.