Subject: unwanted source references showing up w/ -O0 (with patch)

From: Matt Wette <[email-masked]>
Date: Sun, 20 Mar 2022 08:11:10 -0700

In guile-3.0.8, when I compile w/ optimiation-level 0, I am seeing references
to the beginning let-form.   The following illustrates the issue.  Consider

foo.scm:
(define *a* (make-parameter 0))
(define (foo . args)
     (let* ((b (+ (*a*) 1))
            (c (+ (*a*) b 2)))
       (simple-format #t "foo a= ~S\n" (*a*))
       (simple-format #t "foo b= ~S\n" b)
       (simple-format #t "foo c= ~S\n" c)))

and p2.scm:
(use-modules (system base compile))
(use-modules (system vm program))
(use-modules (ice-9 pretty-print))

(compile-and-load "foo.scm" #:optimization-level 0)
(define sources (program-sources-pre-retire foo))
(pretty-print sources)

With guile-3.0.8, I see the followiing output.
Note the references back to line 2.((0 "foo.scm" 1 . 0)
  (20 "foo.scm" 2 . 4)
  (20 "foo.scm" 2 . 15)
  (76 "foo.scm" 2 . 18)
  (132 "foo.scm" 2 . 17)
  (152 "foo.scm" 2 . 23)
  (156 "foo.scm" 2 . 14)
  (176 "foo.scm" 2 . 4)
  (176 "foo.scm" 3 . 15)
  (232 "foo.scm" 3 . 18)
  (288 "foo.scm" 3 . 17)
  (308 "foo.scm" 3 . 23)
  (312 "foo.scm" 3 . 25)
  (316 "foo.scm" 3 . 14)
  (336 "foo.scm" 2 . 4)
  (336 "foo.scm" 4 . 7)
  (392 "foo.scm" 4 . 21)
  (396 "foo.scm" 4 . 24)
  (404 "foo.scm" 4 . 39)
  (460 "foo.scm" 4 . 38)
  (480 "foo.scm" 4 . 6)
  (496 "foo.scm" 2 . 4)
  (496 "foo.scm" 5 . 7)
  (552 "foo.scm" 5 . 21)
  (556 "foo.scm" 5 . 24)
  (564 "foo.scm" 5 . 38)
  (568 "foo.scm" 5 . 6)
  (584 "foo.scm" 6 . 7)
  (640 "foo.scm" 6 . 21)
  (644 "foo.scm" 6 . 24)
  (652 "foo.scm" 6 . 38)
  (656 "foo.scm" 6 . 6))

With the patch shown below, this is what I get:
((0 "foo.scm" 1 . 0)
  (20 "foo.scm" 2 . 4)
  (20 "foo.scm" 2 . 15)
  (76 "foo.scm" 2 . 18)
  (132 "foo.scm" 2 . 17)
  (152 "foo.scm" 2 . 23)
  (156 "foo.scm" 2 . 14)
  (176 "foo.scm" 2 . 4)
  (176 "foo.scm" 3 . 15)
  (232 "foo.scm" 3 . 18)
  (288 "foo.scm" 3 . 17)
  (308 "foo.scm" 3 . 23)
  (312 "foo.scm" 3 . 25)
  (316 "foo.scm" 3 . 14)
  (336 "foo.scm" 4 . 7)
  (392 "foo.scm" 4 . 21)
  (396 "foo.scm" 4 . 24)
  (404 "foo.scm" 4 . 39)
  (460 "foo.scm" 4 . 38)
  (480 "foo.scm" 4 . 6)
  (496 "foo.scm" 5 . 7)
  (552 "foo.scm" 5 . 21)
  (556 "foo.scm" 5 . 24)
  (564 "foo.scm" 5 . 38)
  (568 "foo.scm" 5 . 6)
  (584 "foo.scm" 6 . 7)
  (640 "foo.scm" 6 . 21)
  (644 "foo.scm" 6 . 24)
  (652 "foo.scm" 6 . 38)
  (656 "foo.scm" 6 . 6))

Patch follows.  In a sequence, the source of the forms should be emitted,
not the parent sequence source (start).
--- guile-3.0.8-dist/module/language/tree-il/compile-bytecode.scm	2021-09-30 05:30:38.000000000 -0700
+++ guile-3.0.8-srcfix/module/language/tree-il/compile-bytecode.scm	2022-03-19 09:37:28.126740690 -0700
@@ -910,7 +910,7 @@
      (define (visit-seq exp env ctx)
        (match exp
          (($ <seq> src head tail)
-         (maybe-emit-source src)
+         ;;(maybe-emit-source src)
           (for-effect head env)
           (for-context tail env ctx))))