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))