Subject: Vector initialization loop remains even when allocation is elided

From: Ludovic Courtès <[email-masked]>
Date: Sun, 30 Oct 2022 22:38:51 +0100

Spot the issue in the code below?

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (version)
$28 = "3.0.8"
scheme@(guile-user)> ,c (let () (make-vector 12345 #\x) 42)
Disassembly of <unnamed function> at #xe8:

   0    (instrument-entry 42)                                 at (unknown file):5009:35
   2    (assert-nargs-ee/locals 1 1)    ;; 2 slots (0 args)
   3    (load-u64 1 0 12346)                                  at (unknown file):5009:11
   6    (load-u64 0 0 1)                
   9    (uadd/immediate 0 0 1)          
  10    (u64<? 0 1)                     
  11    (jnl 7)                         ;; -> L2
L1:
  12    (instrument-loop 30)            
  14    (handle-interrupts)             
  15    (uadd/immediate 0 0 1)          
  16    (u64<? 0 1)                     
  17    (jl -5)                         ;; -> L1
L2:
  18    (make-immediate 1 170)          ;; 42                 at (unknown file):5009:35
  19    (reset-frame 1)                 ;; 1 slot
  20    (handle-interrupts)             
  21    (return-values)                 

--8<---------------cut here---------------end--------------->8---

Vector allocation is elided (no ‘allocate-words’ instruction),
rightfully so (assuming we neglect the possibility of an out-of-memory
effect), but we still have the skeleton of the vector initialization
loop with its 12345 iterations.

Vector allocation is removed by the ‘eliminate-dead-code’ pass, which
happens on the VM instruction stream, so it explains why the loop
remains.

Still kinda surprising!

Also weird is the fact that ‘make-vector’ is kept by ‘peval’, on the
grounds that it might throw, but down the road it’s removed anyway.

Thoughts?

Ludo’.