From 2dd29f7e3d6b7f55b99ee81bd1c3db7be120e788 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Thu, 26 Mar 2015 08:23:15 -0600 Subject: [PATCH] fix pessimism in optimizer reordering When determing whether expressions can be reordered, a reference to a module-defined variable was considered unreorderable when it is known to have a value and no further mutation, but the value isn't constant across all runs. --- .../tests/racket/optimize.rktl | 34 +++++++++++++++++++ racket/src/racket/src/optimize.c | 4 +-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkgs/racket-test-core/tests/racket/optimize.rktl b/pkgs/racket-test-core/tests/racket/optimize.rktl index e96cd1c94c..782676f7aa 100644 --- a/pkgs/racket-test-core/tests/racket/optimize.rktl +++ b/pkgs/racket-test-core/tests/racket/optimize.rktl @@ -1847,6 +1847,40 @@ '(lambda (z) (list (list (z 2)) (list z)))) +(test-comp '(module m racket/base + ;; Reference to a ready module-level variable shouldn't + ;; prevent let-values splitting + (#%plain-module-begin + (define z (random)) + (define (f) + (let-values ([(a b) (values (cons 1 z) (cons 2 z))]) + (list a b))))) + '(module m racket/base + ;; Reference to a ready module-level variable shouldn't + ;; prevent let-values splitting + (#%plain-module-begin + (define z (random)) + (define (f) + (list (cons 1 z) (cons 2 z)))))) + +(test-comp '(module m racket/base + ;; Don't reorder references to a mutable variable + (#%plain-module-begin + (define z (random)) + (define (f) + (let-values ([(a b) (values (cons 1 z) (cons 2 z))]) + (list a b))) + (set! z 5))) + '(module m racket/base + ;; Reference to a ready module-level variable shouldn't + ;; prevent let-values splitting + (#%plain-module-begin + (define z (random)) + (define (f) + (list (cons 1 z) (cons 2 z))) + (set! z 5))) + #f) + (test-comp '(lambda (z) ;; It's ok to reorder unsafe operations relative ;; to each other: diff --git a/racket/src/racket/src/optimize.c b/racket/src/racket/src/optimize.c index 876068022b..21177b67d9 100644 --- a/racket/src/racket/src/optimize.c +++ b/racket/src/racket/src/optimize.c @@ -341,9 +341,9 @@ int scheme_omittable_expr(Scheme_Object *o, int vals, int fuel, int resolved, if (vtype == scheme_compiled_toplevel_type) { note_match(1, vals, warn_info); if ((vals == 1) || (vals < 0)) { - if (!no_id && (SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_READY) + if (!no_id && ((SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_READY)) return 1; - else if ((SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_CONST) + else if ((SCHEME_TOPLEVEL_FLAGS(o) & SCHEME_TOPLEVEL_FLAGS_MASK) >= SCHEME_TOPLEVEL_FIXED) return 1; else return 0;