Skip to content

Commit

Permalink
globals are not affect_free (ref #9535)
Browse files Browse the repository at this point in the history
(cherry picked from commit d071904)

Conflicts:
	base/inference.jl
	test/core.jl
  • Loading branch information
vtjnash authored and tkelman committed Jan 16, 2015
1 parent 9c6bb24 commit 41bfd37
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
25 changes: 17 additions & 8 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,19 @@ end
# and some affect-free calls (allow_volatile=false) -- affect_free means the call
# cannot be affected by previous calls, except assignment nodes
function effect_free(e::ANY, sv, allow_volatile::Bool)
if isa(e,Symbol) || isa(e,SymbolNode) || isa(e,Number) || isa(e,String) ||
if isa(e,SymbolNode)
allow_volatile && return true
if is_global(sv, (e::SymbolNode).name)
return false
end
end
if isa(e,Symbol)
allow_volatile && return true
if is_global(sv, e::Symbol)
return false
end
end
if isa(e,Number) || isa(e,String) ||
isa(e,TopNode) || isa(e,QuoteNode) || isa(e,Type) || isa(e,Tuple)
return true
end
Expand Down Expand Up @@ -2328,9 +2340,7 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
# we need to keep it as a variable name
for vi in vinflist
if vi[1] === a && vi[3] != 0
if !islocal
islocal = true
end
islocal = true
aeitype = tmerge(aeitype, vi[2])
if aeitype === Any
break
Expand Down Expand Up @@ -2370,14 +2380,13 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
occ = 0
for j = length(body.args):-1:1
b = body.args[j]
if occ < 1
occ += occurs_more(b, x->is(x,a), 5)
if occ < 6
occ += occurs_more(b, x->is(x,a), 6)
end
if occ > 0 && affect_free && !effect_free(b, sv, true) #TODO: we could short-circuit this test better by memoizing effect_free(b) in the for loop over i
affect_free = false
end
if occ > 5
occ = 6
if occ > 5 && !affect_free
break
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1876,3 +1876,10 @@ let x = [1,2,3]
@test ccall(:jl_new_bits, Any, (Any,Ptr{Void},), (Int,Int,Int), x) === (1,2,3)
@test (ccall(:jl_new_bits, Any, (Any,Ptr{Void},), (Int16,(Nothing,),Int8,(),Int,Nothing,Int), x)::Tuple)[[2,4,5,6,7]] === ((nothing,),(),2,nothing,3)
end

# issue #9535
counter9535 = 0
f9535() = (global counter9535; counter9535 += 1; counter9535)
g9535() = (f9535(),f9535())
@assert g9535() == (1,2)
@assert g9535() == (3,4)

0 comments on commit 41bfd37

Please sign in to comment.