Skip to content

Commit c79ede3

Browse files
committed
fully ignore assignments to underscore symbols
closes #9343 fixes #20931 apply nospecialize to unused/underscore arguments
1 parent f90a12a commit c79ede3

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

src/julia-syntax.scm

+29-21
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@
148148
;; GF method does not need to keep decl expressions on lambda args
149149
;; except for rest arg
150150
(define (method-lambda-expr argl body rett)
151-
(let ((argl (map arg-name argl))
151+
(let ((argl (map (lambda (x)
152+
(let ((n (arg-name x)))
153+
(if (underscore-symbol? n) UNUSED n)))
154+
argl))
152155
(body (blockify body)))
153156
`(lambda ,argl ()
154157
(scope-block
@@ -324,7 +327,8 @@
324327
(append req opt vararg) rett)))))
325328
;; no optional positional args
326329
(let ((names (map car sparams))
327-
(anames (llist-vars argl)))
330+
(anames (map (lambda (x) (if (underscore-symbol? x) UNUSED x))
331+
(llist-vars argl))))
328332
(if (has-dups (filter (lambda (x) (not (eq? x UNUSED))) anames))
329333
(error "function argument names not unique"))
330334
(if (has-dups names)
@@ -337,7 +341,7 @@
337341
(let* ((gen (generated-version body))
338342
(nongen (non-generated-version body))
339343
(gname (symbol (string (gensy) "#" (current-julia-module-counter))))
340-
(gf (make-generator-function gname names (llist-vars argl) gen))
344+
(gf (make-generator-function gname names anames gen))
341345
(loc (function-body-lineno body)))
342346
(set! body (insert-after-meta
343347
nongen
@@ -2387,7 +2391,7 @@
23872391
((=)
23882392
(let ((v (decl-var (cadr e)))
23892393
(rest (find-assigned-vars (caddr e) env)))
2390-
(if (or (ssavalue? v) (memq v env) (globalref? v))
2394+
(if (or (ssavalue? v) (memq v env) (globalref? v) (underscore-symbol? v))
23912395
rest
23922396
(cons v rest))))
23932397
(else
@@ -2400,7 +2404,9 @@
24002404
(cond ((memq (car e) '(lambda scope-block module toplevel))
24012405
'())
24022406
((eq? (car e) kind)
2403-
(list (decl-var (cadr e))))
2407+
(if (underscore-symbol? (cadr e))
2408+
'()
2409+
(list (decl-var (cadr e)))))
24042410
(else
24052411
(apply append! (map (lambda (x) (find-decls kind x))
24062412
e))))))
@@ -2415,7 +2421,7 @@
24152421
(find-assigned-vars e env)))
24162422

24172423
(define (unbound-vars e bound tab)
2418-
(cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED)) tab)
2424+
(cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED) (underscore-symbol? e)) tab)
24192425
((symbol? e) (if (not (memq e bound)) (put! tab e #t)) tab)
24202426
((or (not (pair? e)) (quoted? e)) tab)
24212427
((memq (car e) '(lambda scope-block module toplevel)) tab)
@@ -2556,7 +2562,7 @@
25562562

25572563
;; compute set of variables referenced in a lambda but not bound by it
25582564
(define (free-vars- e tab)
2559-
(cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED)) tab)
2565+
(cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED) (underscore-symbol? e)) tab)
25602566
((symbol? e) (put! tab e #t))
25612567
((and (pair? e) (eq? (car e) 'outerref)) tab)
25622568
((and (pair? e) (eq? (car e) 'break-block)) (free-vars- (caddr e) tab))
@@ -3577,20 +3583,22 @@ f(x) = yt(x)
35773583
(value callex)
35783584
(else (emit callex)))))
35793585
((=)
3580-
(let* ((rhs (compile (caddr e) break-labels #t #f))
3581-
(lhs (cadr e))
3582-
(lhs (if (and arg-map (symbol? lhs))
3583-
(get arg-map lhs lhs)
3584-
lhs)))
3585-
(if (and value rhs)
3586-
(let ((rr (if (or (atom? rhs) (ssavalue? rhs) (eq? (car rhs) 'null))
3587-
rhs (make-ssavalue))))
3588-
(if (not (eq? rr rhs))
3589-
(emit `(= ,rr ,rhs)))
3590-
(emit `(= ,lhs ,rr))
3591-
(if tail (emit-return rr))
3592-
rr)
3593-
(emit-assignment lhs rhs))))
3586+
(let ((lhs (cadr e)))
3587+
(if (and (symbol? lhs) (underscore-symbol? lhs))
3588+
(compile (caddr e) break-labels value tail)
3589+
(let* ((rhs (compile (caddr e) break-labels #t #f))
3590+
(lhs (if (and arg-map (symbol? lhs))
3591+
(get arg-map lhs lhs)
3592+
lhs)))
3593+
(if (and value rhs)
3594+
(let ((rr (if (or (atom? rhs) (ssavalue? rhs) (eq? (car rhs) 'null))
3595+
rhs (make-ssavalue))))
3596+
(if (not (eq? rr rhs))
3597+
(emit `(= ,rr ,rhs)))
3598+
(emit `(= ,lhs ,rr))
3599+
(if tail (emit-return rr))
3600+
rr)
3601+
(emit-assignment lhs rhs))))))
35943602
((block)
35953603
(let* ((last-fname filename)
35963604
(fnm (first-non-meta e))

src/method.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,17 @@ static void jl_method_set_source(jl_method_t *m, jl_code_info_t *src)
469469
uint8_t j;
470470
uint8_t called = 0;
471471
int gen_only = 0;
472-
for (j = 1; j < m->nargs && j <= 8; j++) {
472+
for (j = 1; j < m->nargs && j <= sizeof(m->nospecialize) * 8; j++) {
473473
jl_value_t *ai = jl_array_ptr_ref(src->slotnames, j);
474-
if (ai == (jl_value_t*)unused_sym)
474+
if (ai == (jl_value_t*)unused_sym) {
475+
int sn = j-1;
476+
m->nospecialize |= (1 << sn);
475477
continue;
476-
if (jl_array_uint8_ref(src->slotflags, j) & 64)
477-
called |= (1 << (j - 1));
478+
}
479+
if (j <= 8) {
480+
if (jl_array_uint8_ref(src->slotflags, j) & 64)
481+
called |= (1 << (j - 1));
482+
}
478483
}
479484
m->called = called;
480485
m->pure = src->pure;

0 commit comments

Comments
 (0)