Skip to content

Commit 31130a7

Browse files
JeffBezansonmartinholters
authored andcommitted
fix #21311, inlining _apply reordering evaluations
Backport of #21319. (cherry picked from commit a27ce01)
1 parent f797f92 commit 31130a7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

base/inference.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,8 @@ function inlining_pass(e::Expr, sv, linfo)
30443044
if is(f, _apply)
30453045
na = length(e.args)
30463046
newargs = Vector{Any}(na-2)
3047+
newstmts = Any[]
3048+
effect_free_upto = 0
30473049
for i = 3:na
30483050
aarg = e.args[i]
30493051
t = widenconst(exprtype(aarg, linfo))
@@ -3054,13 +3056,25 @@ function inlining_pass(e::Expr, sv, linfo)
30543056
newargs[i-2] = Any[ QuoteNode(x) for x in aarg ]
30553057
elseif isa(t, DataType) && t.name === Tuple.name && !isvatuple(t) &&
30563058
length(t.parameters) <= MAX_TUPLE_SPLAT
3059+
for k = (effect_free_upto+1):(i-3)
3060+
as = newargs[k]
3061+
for kk = 1:length(as)
3062+
ak = as[kk]
3063+
if !effect_free(ak, linfo, true)
3064+
tmpv = newvar!(sv, widenconst(exprtype(ak, linfo)))
3065+
push!(newstmts, Expr(:(=), tmpv, ak))
3066+
as[kk] = tmpv
3067+
end
3068+
end
3069+
end
3070+
effect_free_upto = i-3
30573071
if effect_free(aarg, linfo, true)
30583072
# apply(f,t::(x,y)) => f(t[1],t[2])
30593073
tmpv = aarg
30603074
else
30613075
# apply(f,t::(x,y)) => tmp=t; f(tmp[1],tmp[2])
30623076
tmpv = newvar!(sv, t)
3063-
push!(stmts, Expr(:(=), tmpv, aarg))
3077+
push!(newstmts, Expr(:(=), tmpv, aarg))
30643078
end
30653079
tp = t.parameters
30663080
newargs[i-2] = Any[ mk_getfield(tmpv,j,tp[j]) for j=1:length(tp) ]
@@ -3069,6 +3083,7 @@ function inlining_pass(e::Expr, sv, linfo)
30693083
return (e,stmts)
30703084
end
30713085
end
3086+
append!(stmts, newstmts)
30723087
e.args = [Any[e.args[2]]; newargs...]
30733088

30743089
# now try to inline the simplified call

test/inline.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,21 @@ f18948() = (local bar::Int64; bar=1.5)
7777
g18948() = (local bar::Int32; bar=0x80000000)
7878
@test_throws InexactError f18948()
7979
@test_throws InexactError g18948()
80+
81+
# issue #21311
82+
counter21311 = Ref(0)
83+
@noinline function update21311!(x)
84+
counter21311[] += 1
85+
x[] = counter21311[]
86+
return x
87+
end
88+
@noinline map21311(t::Tuple{Any}) = (update21311!(t[1]),)
89+
@inline map21311(t::Tuple) = (update21311!(t[1]), map21311(Base.tail(t))...)
90+
function read21311()
91+
xs = Ref(1), Ref(1)
92+
map21311(xs)
93+
return xs[1]
94+
end
95+
let a = read21311()
96+
@test a[] == 1
97+
end

0 commit comments

Comments
 (0)