1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00

peval: elide effect-free primcalls in effect context

* module/language/tree-il/peval.scm (peval): fix-letrec can residualize
useless primcalls, when a let or letrec-bound var is unused.  Fix to
elide these.
This commit is contained in:
Andy Wingo 2023-11-15 15:17:29 +01:00
parent e529db04a4
commit 28a428135f

View file

@ -683,6 +683,11 @@ top-level bindings from ENV and return the resulting expression."
;; mutable data (like `car' or toplevel references).
(constant? (compute-effects x)))
(define (can-elide-statement? stmt)
(let ((effects (compute-effects stmt)))
(effect-free?
(exclude-effects effects (logior &allocation &zero-values)))))
(define (prune-bindings ops in-order? body counter ctx build-result)
;; This helper handles both `let' and `letrec'/`fix'. In the latter
;; cases we need to make sure that if referenced binding A needs
@ -1520,7 +1525,10 @@ top-level bindings from ENV and return the resulting expression."
(fold-constants src name args ctx))
((name . args)
(make-primcall src name args))))
(let ((exp (make-primcall src name args)))
(if (and (eq? ctx 'effect) (can-elide-statement? exp))
(make-void src)
exp)))))
(($ <call> src orig-proc orig-args)
;; todo: augment the global env with specialized functions