Skip to content

Commit 70fe622

Browse files
authored
remove unnecessary Box when an argument is used before assigned (#36245)
1 parent abc7f75 commit 70fe622

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/ast.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@
288288
(symbol? (cadr e))))
289289

290290
(define (lam:args x) (cadr x))
291-
(define (lam:vars x) (llist-vars (lam:args x)))
291+
(define (lam:argnames x) (llist-vars (lam:args x)))
292292
(define (lam:vinfo x) (caddr x))
293293
(define (lam:body x) (cadddr x))
294294
(define (lam:sp x) (cadddr (lam:vinfo x)))

src/julia-syntax.scm

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,7 @@
25562556
(and (memq var (scope:locals scope)) 'local)
25572557
(and (memq var (scope:globals scope))
25582558
(if (and exclude-top-level-globals
2559-
(null? (lam:vars (scope:lam scope)))
2559+
(null? (lam:args (scope:lam scope)))
25602560
;; don't inherit global decls from the outermost scope block
25612561
;; in a top-level expression.
25622562
(or (not (scope:prev scope))
@@ -2636,13 +2636,13 @@
26362636
'(false)
26372637
'(true)))
26382638
((eq? (car e) 'lambda)
2639-
(let* ((args (lam:vars e))
2639+
(let* ((args (lam:argnames e))
26402640
(body (resolve-scopes- (lam:body e) (make-scope e args '() '() sp '() scope))))
26412641
`(lambda ,(cadr e) ,(caddr e) ,body)))
26422642
((eq? (car e) 'scope-block)
26432643
(let* ((blok (cadr e)) ;; body of scope-block expression
26442644
(lam (scope:lam scope))
2645-
(argnames (lam:vars lam))
2645+
(argnames (lam:argnames lam))
26462646
(toplevel? (and (null? argnames) (eq? e (lam:body lam))))
26472647
(current-locals (caddr lam)) ;; locals created so far in our lambda
26482648
(globals (find-global-decls blok))
@@ -2755,7 +2755,7 @@
27552755
,(resolve-scopes- (cadddr e) scope (method-expr-static-parameters e))))
27562756
(else
27572757
(if (and (eq? (car e) '=) (symbol? (cadr e))
2758-
scope (null? (lam:vars (scope:lam scope)))
2758+
scope (null? (lam:args (scope:lam scope)))
27592759
(warn-var?! (cadr e) scope)
27602760
(= *scopewarn-opt* 1))
27612761
(let* ((v (cadr e))
@@ -2782,7 +2782,7 @@
27822782

27832783
;; names of arguments and local vars
27842784
(define (lambda-all-vars e)
2785-
(append (lam:vars e) (caddr e)))
2785+
(append (lam:argnames e) (caddr e)))
27862786

27872787
;; compute set of variables referenced in a lambda but not bound by it
27882788
(define (free-vars- e tab)
@@ -3201,6 +3201,7 @@ f(x) = yt(x)
32013201
;; This does a basic-block-local dominance analysis to find variables that
32023202
;; are never used undef.
32033203
(let ((vi (car (lam:vinfo lam)))
3204+
(args (lam:argnames lam))
32043205
(unused (table)) ;; variables not (yet) used (read from) in the current block
32053206
(live (table)) ;; variables that have been set in the current block
32063207
(seen (table))) ;; all variables we've seen assignments to
@@ -3221,6 +3222,11 @@ f(x) = yt(x)
32213222
(restore (table)))
32223223
(define (mark-used var)
32233224
;; remove variable from the unused table
3225+
;; Note arguments are only "used" for purposes of this analysis when
3226+
;; they are captured, since they are never undefined.
3227+
(if (and (has? unused var) (not (memq var args)))
3228+
(del! unused var)))
3229+
(define (mark-captured var)
32243230
(if (has? unused var)
32253231
(del! unused var)))
32263232
(define (assign! var)
@@ -3272,7 +3278,7 @@ f(x) = yt(x)
32723278
(get-methods e (lam:body lam))
32733279
(list e))))
32743280
(for-each (lambda (ex)
3275-
(for-each mark-used
3281+
(for-each mark-captured
32763282
(map car (cadr (lam:vinfo (cadddr ex))))))
32773283
all-methods)
32783284
(assign! (cadr e))))

test/core.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,6 +5178,12 @@ end
51785178
@test let_Box5()() == 46
51795179
@test let_noBox()() == 21
51805180

5181+
function _assigns_and_captures_arg(a)
5182+
a = a
5183+
return ()->a
5184+
end
5185+
@test !any(contains_Box, code_lowered(_assigns_and_captures_arg,(Any,))[1].code)
5186+
51815187
module TestModuleAssignment
51825188
using Test
51835189
@eval $(GlobalRef(TestModuleAssignment, :x)) = 1

0 commit comments

Comments
 (0)