strange omptimization(?) bug

21 views
Skip to first unread message

Sergey Pinaev

unread,
Dec 23, 2016, 5:43:58 AM12/23/16
to Racket Developers
if i run in repl following code:

(define (t1 f)
  (f '(123))
  1)
(define (tt)
  (lambda (a)
    (let* ((data (box '()))
           (v (t1 (lambda (d)
                    (set-box! data d)))))
      (cons (unbox data) v))))
(define tf (tt))

and then call (tf 1) i got '((123) . 1) as expected.

but if i put t1 and tt in foobar.scm:

#lang racket
(provide tt)
(define (t1 f)
  (f '(123))
  1)
(define (tt)
  (lambda (a)
    (let* ((data (box '()))
           (v (t1 (lambda (d)
                    (set-box! data d)))))
      (cons (unbox data) v))))

and then run in repl:

(require "foobar.scm")
(define tf (tt))
and call (tf 1) - i got '(() . 1)

and if i change return value in tt to (cons v (unbox data)) - result will be correct again.
also if i just put (printf "hello~n") before (cons ..) in tt - result will be correct also.
so i guess in some case (unbox data) sowehow calculated before computating value of v.

p.s. sorry for my poor english.

Matthew Flatt

unread,
Dec 23, 2016, 10:10:55 AM12/23/16
to Sergey Pinaev, Racket Developers
Yes, that's an optimizer bug. The optimizer sees that `unbox` is
applied to a known box, so it converts the call to `unsafe-unbox`.
Unfortunately, the optimizer also miscategorizes some unsafe
operations, including `unsafe-unbox`, as independent of side effects.

One workaround is to add `(set! data data)` to your program, since that
defeats the optimizer's tracking of the value of `data`.

Meanwhile, I've pushed a repair. Thanks very much for the report!
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-dev+...@googlegroups.com.
> To post to this group, send email to racke...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-dev/ff3a8778-98db-45c7-b870-bd8f43422b
> 02%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages