From 84d9d114dc2e33e8def3a8a4d2022ebe8ce79129 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 10 Jan 2019 12:52:03 -0500 Subject: [PATCH] Fix for beta expand of functions called once The problem is an if expression within one of these functions may cause the same continuation to be expanded twice, introducing duplicate lambda defintions and identifiers. For now we are not going to beta expand such functions during the contraction phase. --- scheme/cyclone/cps-optimizations.sld | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/scheme/cyclone/cps-optimizations.sld b/scheme/cyclone/cps-optimizations.sld index 5db01a75..42674644 100644 --- a/scheme/cyclone/cps-optimizations.sld +++ b/scheme/cyclone/cps-optimizations.sld @@ -1455,7 +1455,14 @@ (not (adbv:reassigned? var)) (not (adbv:self-rec-call? var)) ;(not (fnc-depth>? (ast:lambda-body fnc) 4)))) - (not (fnc-depth>? (ast:lambda-body fnc) 5)))) + (not (fnc-depth>? (ast:lambda-body fnc) 5)) + ;; Issue here is we can run into code that calls the + ;; same continuation from both if branches. In this + ;; case we do not want to beta-expand as a contraction + ;; because duplicate instances of the same code may be + ;; introduced, causing problems downstream. + (not (contains-if? (ast:lambda-body fnc))) + )) ))) (else #f))) @@ -1477,6 +1484,19 @@ (scan exp depth) (return #f)))) + (define (contains-if? exp) + (call/cc + (lambda (return) + (define (scan exp) + (cond + ((ast:lambda? exp) (scan (ast:lambda-body exp))) + ((quote? exp) #f) + ((if? exp) (return #t)) + ((app? exp) (for-each scan exp)) + (else #f))) + (scan exp) + (return #f)))) + ;; Check app and beta expand if possible, else just return given code (define (beta-expand-app exp rename-lambdas) (let* ((args (cdr exp))