Mysterious behavior with s8vector and -specialize?

Diego via Chicken-users <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <72Wj7ifYwx9i7BetKSDTIE-dDUKk_C1pBMP0cdRbo77AP3ZAU0uL5YG3KquKA5Nb0wPR4lxVc_JankHyy0N_wb30PpcGROOEqpNz_MIC35U=@pm.me>
I was testing SRFI-160 with CHICKEN 6 and found some interesting behavior. I bisected the individual -O3 optimizations to debug the behavior, and found -specialize might be the culprit?

Attached is a minimal reproduction example (or at least, as minimal as I could get it). Surprisingly, this behavior only seems to occur with an s8vector; all the other specialized vector types behave as expected as far as I can tell (e.g. replacing s8 with u8 in the example makes the behavior go away).

When I compile it with:

csc test.scm -optimize-leaf-routines -inline -lfa2 -local -inline-global

Then run the "test" program, I see:

0
(move-memory! #s8(1 2 3 4 5) #s8(-16 98 -108 -80 -70 127 0 0 16 0) 5 0 0)
5
(move-memory! #s8(1 2 3 4 5) #s8(1 2 3 4 5 127 0 0 16 0) 5 0 5)
#s8(1 2 3 4 5 1 2 3 4 5)

When I add -specialize to the same command line above, I instead see:

0
(move-memory! #s8(1 2 3 4 5) #s8(-48 -64 -31 -59 -112 127 0 0 16 27) 5 0 0)
2
(move-memory! #s8(1 2 3 4 5) #s8(1 2 3 4 5 127 0 0 16 27) 5 0 2)
#s8(1 2 1 2 3 4 5 0 16 27)

What could be going on here?

Thanks,
Diego
test.scm (text/x-scheme, 1.4 KB)
(module test ()
  (import (scheme base)
          (scheme case-lambda)
          (chicken memory)
          (chicken number-vector)
          (chicken fixnum)
          (chicken base))
  (define (len-sum vecs)
    (if (null? vecs)
        0
        (fx+ (s8vector-length (car vecs))
             (len-sum (cdr vecs)))))

  (define s8vector-copy!
    (case-lambda
      ((to at from)
       (move-memory! from to (fx* 1 (s8vector-length from)) 0 (fx* at 1)))
      ((to at from start)
       (move-memory! from to (fx* 1 (s8vector-length from)) (fx* start 1) (fx* at 1)))
      ((to at from start end)
       (chicken.base#print `(move-memory! ,from ,to
                                          ,(fx* 1 (fx- end start))
                                          ,(fx* start 1)
                                          ,(fx* at 1)))
       (move-memory! from to
                     (fx* 1 (fx- end start))
                     (fx* start 1)
                     (fx* at 1)))))

  (define (s8vector-concatenate vecs)
    (let ((v (make-s8vector (len-sum vecs))))
      (let loop ((vecs vecs) (at 0))
        (unless (null? vecs)
          (let ((vec (car vecs)))
            (print at)
            (s8vector-copy! v at vec 0 (s8vector-length vec))
            (loop (cdr vecs) (fx+ at (s8vector-length vec)))))
        v)))

  (define s5 (s8vector 1 2 3 4 5))
  (print (s8vector-concatenate (list s5 s5))))
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.