Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvements to lambda-optimize-vars #16386

Merged
merged 1 commit into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2620,11 +2620,25 @@ f(x) = yt(x)
'())))
args)))))

(define (take-statements-while pred body)
(let ((acc '()))
(define (take expr)
;; returns #t as long as exprs match and we should continue
(cond ((and (pair? expr) (memq (car expr) '(block body)))
(let loop ((xs (cdr expr)))
(cond ((null? xs) #t)
((take (car xs)) (loop (cdr xs)))
(else #f))))
((pred expr)
(set! acc (cons expr acc))
#t)
(else #f)))
(take body)
(reverse! acc)))

;; clear capture bit for vars assigned once at the top, to avoid allocating
;; some unnecessary Boxes.
(define (lambda-optimize-vars! lam)
;; flattening blocks helps us find more dominating statements
(set-car! (cdddr lam) (flatten-blocks (lam:body lam)))
(define (expr-uses-var ex v)
(cond ((assignment? ex) (expr-contains-eq v (caddr ex)))
((eq? (car ex) 'method)
Expand All @@ -2638,15 +2652,16 @@ f(x) = yt(x)
(let* ((leading
(filter (lambda (x) (and (pair? x)
(or (and (eq? (car x) 'method)
(length> (car x) 2))
(length> x 2))
(eq? (car x) '=))))
(take-while (lambda (e)
(or (atom? e)
(memq (car e) '(quote top core line inert local
meta inbounds boundscheck simdloop
implicit-global global globalref
const newvar = null method))))
(lam:body lam))))
(take-statements-while
(lambda (e)
(or (atom? e)
(memq (car e) '(quote top core line inert local
meta inbounds boundscheck simdloop
implicit-global global globalref
const newvar = null method))))
(lam:body lam))))
(unused (map cadr leading))
(def (table)))
;; TODO: reorder leading statements to put assignments where the RHS is
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,13 @@ Yc(f) = (h->f(x->h(h)(x)))(h->f(x->h(h)(x)))
yfib = Yc(fib->(n->(n < 2 ? n : fib(n-1) + fib(n-2))))
@test yfib(20) == 6765

function capt_before_def()
f() = y
y = 2
f
end
@test capt_before_def()() == 2

# variable scope, globals
glob_x = 23
function glotest()
Expand Down