From c79ede37d1d02e89e84170f950bb48e9678b55f1 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 2 Aug 2018 16:38:35 -0400 Subject: [PATCH] fully ignore assignments to underscore symbols closes #9343 fixes #20931 apply nospecialize to unused/underscore arguments --- src/julia-syntax.scm | 50 +++++++++++++++++++++++++------------------- src/method.c | 13 ++++++++---- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index 47001857a4e37..054cb07b3468b 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -148,7 +148,10 @@ ;; GF method does not need to keep decl expressions on lambda args ;; except for rest arg (define (method-lambda-expr argl body rett) - (let ((argl (map arg-name argl)) + (let ((argl (map (lambda (x) + (let ((n (arg-name x))) + (if (underscore-symbol? n) UNUSED n))) + argl)) (body (blockify body))) `(lambda ,argl () (scope-block @@ -324,7 +327,8 @@ (append req opt vararg) rett))))) ;; no optional positional args (let ((names (map car sparams)) - (anames (llist-vars argl))) + (anames (map (lambda (x) (if (underscore-symbol? x) UNUSED x)) + (llist-vars argl)))) (if (has-dups (filter (lambda (x) (not (eq? x UNUSED))) anames)) (error "function argument names not unique")) (if (has-dups names) @@ -337,7 +341,7 @@ (let* ((gen (generated-version body)) (nongen (non-generated-version body)) (gname (symbol (string (gensy) "#" (current-julia-module-counter)))) - (gf (make-generator-function gname names (llist-vars argl) gen)) + (gf (make-generator-function gname names anames gen)) (loc (function-body-lineno body))) (set! body (insert-after-meta nongen @@ -2387,7 +2391,7 @@ ((=) (let ((v (decl-var (cadr e))) (rest (find-assigned-vars (caddr e) env))) - (if (or (ssavalue? v) (memq v env) (globalref? v)) + (if (or (ssavalue? v) (memq v env) (globalref? v) (underscore-symbol? v)) rest (cons v rest)))) (else @@ -2400,7 +2404,9 @@ (cond ((memq (car e) '(lambda scope-block module toplevel)) '()) ((eq? (car e) kind) - (list (decl-var (cadr e)))) + (if (underscore-symbol? (cadr e)) + '() + (list (decl-var (cadr e))))) (else (apply append! (map (lambda (x) (find-decls kind x)) e)))))) @@ -2415,7 +2421,7 @@ (find-assigned-vars e env))) (define (unbound-vars e bound tab) - (cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED)) tab) + (cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED) (underscore-symbol? e)) tab) ((symbol? e) (if (not (memq e bound)) (put! tab e #t)) tab) ((or (not (pair? e)) (quoted? e)) tab) ((memq (car e) '(lambda scope-block module toplevel)) tab) @@ -2556,7 +2562,7 @@ ;; compute set of variables referenced in a lambda but not bound by it (define (free-vars- e tab) - (cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED)) tab) + (cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED) (underscore-symbol? e)) tab) ((symbol? e) (put! tab e #t)) ((and (pair? e) (eq? (car e) 'outerref)) tab) ((and (pair? e) (eq? (car e) 'break-block)) (free-vars- (caddr e) tab)) @@ -3577,20 +3583,22 @@ f(x) = yt(x) (value callex) (else (emit callex))))) ((=) - (let* ((rhs (compile (caddr e) break-labels #t #f)) - (lhs (cadr e)) - (lhs (if (and arg-map (symbol? lhs)) - (get arg-map lhs lhs) - lhs))) - (if (and value rhs) - (let ((rr (if (or (atom? rhs) (ssavalue? rhs) (eq? (car rhs) 'null)) - rhs (make-ssavalue)))) - (if (not (eq? rr rhs)) - (emit `(= ,rr ,rhs))) - (emit `(= ,lhs ,rr)) - (if tail (emit-return rr)) - rr) - (emit-assignment lhs rhs)))) + (let ((lhs (cadr e))) + (if (and (symbol? lhs) (underscore-symbol? lhs)) + (compile (caddr e) break-labels value tail) + (let* ((rhs (compile (caddr e) break-labels #t #f)) + (lhs (if (and arg-map (symbol? lhs)) + (get arg-map lhs lhs) + lhs))) + (if (and value rhs) + (let ((rr (if (or (atom? rhs) (ssavalue? rhs) (eq? (car rhs) 'null)) + rhs (make-ssavalue)))) + (if (not (eq? rr rhs)) + (emit `(= ,rr ,rhs))) + (emit `(= ,lhs ,rr)) + (if tail (emit-return rr)) + rr) + (emit-assignment lhs rhs)))))) ((block) (let* ((last-fname filename) (fnm (first-non-meta e)) diff --git a/src/method.c b/src/method.c index 046d55681e47d..eec396192122e 100644 --- a/src/method.c +++ b/src/method.c @@ -469,12 +469,17 @@ static void jl_method_set_source(jl_method_t *m, jl_code_info_t *src) uint8_t j; uint8_t called = 0; int gen_only = 0; - for (j = 1; j < m->nargs && j <= 8; j++) { + for (j = 1; j < m->nargs && j <= sizeof(m->nospecialize) * 8; j++) { jl_value_t *ai = jl_array_ptr_ref(src->slotnames, j); - if (ai == (jl_value_t*)unused_sym) + if (ai == (jl_value_t*)unused_sym) { + int sn = j-1; + m->nospecialize |= (1 << sn); continue; - if (jl_array_uint8_ref(src->slotflags, j) & 64) - called |= (1 << (j - 1)); + } + if (j <= 8) { + if (jl_array_uint8_ref(src->slotflags, j) & 64) + called |= (1 << (j - 1)); + } } m->called = called; m->pure = src->pure;