letrectify.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ;;; transformation of top-level bindings into letrec*
  2. ;; Copyright (C) 2019 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. (define-module (language tree-il letrectify)
  17. #:use-module ((srfi srfi-1) #:select (fold-right))
  18. #:use-module (srfi srfi-11)
  19. #:use-module (ice-9 match)
  20. #:use-module (language tree-il)
  21. #:use-module (language tree-il effects)
  22. #:export (letrectify))
  23. ;; Take a sequence of top-level definitions and turn the defintions into
  24. ;; letrec*. From this:
  25. ;;
  26. ;; (begin
  27. ;; (define a 10)
  28. ;; (define b (lambda () a))
  29. ;; (foo a)
  30. ;; (define c (lambda () (set! c b) (c))))
  31. ;;
  32. ;; To this:
  33. ;;
  34. ;; (letrec* ((a-var (module-make-local-var! (current-module) 'a))
  35. ;; (a 10)
  36. ;; (_ (begin (variable-set! a-var a)))
  37. ;; (b-var (module-make-local-var! (current-module) 'b))
  38. ;; (b (lambda () a))
  39. ;; ;; Note, declarative lambda definitions are eta-expanded when
  40. ;; ;; referenced by value to make the callee well-known in the
  41. ;; ;; compilation unit.
  42. ;; (_ (begin (variable-set! b-var (lambda () (b)))))
  43. ;; (_ (begin (foo a) #t))
  44. ;; (c-var (module-make-local-var! (current-module) 'c)))
  45. ;; (c (lambda () (variable-set! c-var b) ((variable-ref c-var))))
  46. ;; ;; Here `c' is not eta-expanded, as it's not a declarative
  47. ;; ;; binding.
  48. ;; (_ (begin (variable-set! c-var (lambda () (c))))))
  49. ;; (void))
  50. ;;
  51. ;; Inside the compilation unit, references to "declarative" top-level
  52. ;; definitions are accessed directly as lexicals. A declarative
  53. ;; definition is a variable for which the expander knows the module,
  54. ;; which is defined in the compilation unit exactly one time, and which
  55. ;; is not assigned in the compilation unit.
  56. ;;
  57. ;; The assumption is that it's safe for the compiler to reason about the
  58. ;; *values* of declarative bindings, because they are immutable in
  59. ;; practice. Of course someone can come later from another compilation
  60. ;; unit or another module and use the private module API to mutate
  61. ;; definitions from this compilation unit; in that case, updates from
  62. ;; that third party may not be visible to users of declarative
  63. ;; definitions. That kind of use is not common, though. The letrectify
  64. ;; transformation is so important for performance that most users are
  65. ;; willing to accept the restrictions of this transformation.
  66. ;;
  67. ;; Incidentally, the later fix-letrec and peval passes should optimize
  68. ;; the above example to:
  69. ;;
  70. ;; (begin
  71. ;; (variable-set! (module-make-local-var! (current-module) 'a) 10)
  72. ;; (variable-set! (module-make-local-var! (current-module) 'b)
  73. ;; (lambda () 10))
  74. ;; (foo 10)
  75. ;; (let ((c-var (module-make-local-var! (current-module) 'c)))
  76. ;; (variable-set! c-var
  77. ;; (lambda ()
  78. ;; (variable-set! c-var (lambda () 10))
  79. ;; ((variable-ref c-var))))
  80. ;; (void)))
  81. ;;
  82. ;; As you can see, letrectification allowed for inlining of the uses of
  83. ;; both A and B.
  84. ;;
  85. (define for-each-fold (make-tree-il-folder))
  86. (define (tree-il-for-each f x)
  87. (for-each-fold x (lambda (x) (f x) (values)) (lambda (x) (values))))
  88. (define (compute-declarative-toplevels x)
  89. (define dynamic (make-hash-table))
  90. (define defined (make-hash-table))
  91. (define assigned (make-hash-table))
  92. (tree-il-for-each
  93. (lambda (x)
  94. (match x
  95. (($ <toplevel-set> src mod name)
  96. (if mod
  97. (hash-set! assigned (cons mod name) #t)
  98. (hashq-set! dynamic name #t)))
  99. (($ <toplevel-define> src mod name expr)
  100. (if mod
  101. (hash-set! (if (hash-ref defined (cons mod name))
  102. assigned
  103. defined)
  104. (cons mod name) expr)
  105. (hashq-set! dynamic name #t)))
  106. (_ (values))))
  107. x)
  108. (let ((declarative (make-hash-table)))
  109. (define (declarative-module? mod)
  110. (let ((m (resolve-module mod #f #:ensure #f)))
  111. (and m (module-declarative? m))))
  112. (hash-for-each (lambda (k expr)
  113. (match k
  114. ((mod . name)
  115. (unless (or (hash-ref assigned k)
  116. (hashq-ref dynamic name)
  117. (not (declarative-module? mod)))
  118. (hash-set! declarative k expr)))))
  119. defined)
  120. declarative))
  121. (define (compute-private-toplevels declarative)
  122. ;; Set of variables exported by the modules of declarative bindings in
  123. ;; this compilation unit.
  124. (define exports (make-hash-table))
  125. ;; If a module exports a macro, that macro could implicitly export any
  126. ;; top-level binding in a module; we have to avoid sealing private
  127. ;; bindings in that case.
  128. (define exports-macro? (make-hash-table))
  129. (hash-for-each
  130. (lambda (k _)
  131. (match k
  132. ((mod . name)
  133. (unless (hash-get-handle exports-macro? mod)
  134. (hash-set! exports-macro? mod #f)
  135. (let ((i (module-public-interface (resolve-module mod))))
  136. (when i
  137. (module-for-each
  138. (lambda (k v)
  139. (hashq-set! exports v k)
  140. (when (and (variable-bound? v) (macro? (variable-ref v)))
  141. (hash-set! exports-macro? mod #t)))
  142. i)))))))
  143. declarative)
  144. (let ((private (make-hash-table)))
  145. (hash-for-each
  146. (lambda (k _)
  147. (match k
  148. ((mod . name)
  149. (unless (or (hash-ref exports-macro? mod)
  150. (hashq-ref exports
  151. (module-local-variable (resolve-module mod) name)))
  152. (hash-set! private k #t)))))
  153. declarative)
  154. private))
  155. (define* (letrectify expr #:key (seal-private-bindings? #f))
  156. (define declarative (compute-declarative-toplevels expr))
  157. (define private
  158. (if seal-private-bindings?
  159. (compute-private-toplevels declarative)
  160. (make-hash-table)))
  161. (define declarative-box+value
  162. (let ((tab (make-hash-table)))
  163. (hash-for-each (lambda (key val)
  164. (let ((box (and (not (hash-ref private key))
  165. (gensym)))
  166. (val (gensym)))
  167. (hash-set! tab key (cons box val))))
  168. declarative)
  169. (lambda (mod name)
  170. (hash-ref tab (cons mod name)))))
  171. (define compute-effects
  172. ;; Assume all lexicals are assigned, for the purposes of this
  173. ;; transformation. (It doesn't matter.)
  174. (let ((assigned? (lambda (sym) #t)))
  175. (make-effects-analyzer assigned?)))
  176. (define (can-elide-statement? stmt)
  177. (let ((effects (compute-effects stmt)))
  178. (effect-free?
  179. (exclude-effects effects (logior &allocation &zero-values)))))
  180. (define (add-binding name var val tail)
  181. (match tail
  182. (($ <letrec> src #t names vars vals tail)
  183. (make-letrec src #t
  184. (cons name names) (cons var vars) (cons val vals)
  185. tail))
  186. (_
  187. (make-letrec (tree-il-src tail) #t
  188. (list name) (list var) (list val)
  189. tail))))
  190. (define (add-statement src stmt tail)
  191. (if (can-elide-statement? stmt)
  192. tail
  193. (add-binding '_ (gensym "_") (make-seq src stmt (make-void src))
  194. tail)))
  195. (define (residualize src mod name var expr)
  196. (let ((lexical (make-lexical-ref src name var)))
  197. (match expr
  198. ;; Eta-expand references to declarative procedure definitions so
  199. ;; that adding these bindings to the module doesn't cause
  200. ;; otherwise "well-known" (in the sense of closure conversion)
  201. ;; procedures to become not well-known.
  202. ;;
  203. ;; Note, this means that eq? will always return #f when
  204. ;; comparing a value to a <lexical-ref> of a declarative
  205. ;; procedure definition, because the residualized reference is a
  206. ;; fresh value (the <lambda> literal we return here). This is
  207. ;; permitted by R6RS as procedure equality is explicitly
  208. ;; unspecified, but if it's an irritation in practice, we could
  209. ;; disable this transformation.
  210. (($ <lambda> src1 meta
  211. ($ <lambda-case> src2 req #f rest #f () syms body #f))
  212. (let* ((syms (map gensym (map symbol->string syms)))
  213. (args (map (lambda (req sym) (make-lexical-ref src2 req sym))
  214. (if rest (append req (list rest)) req)
  215. syms))
  216. (body (if rest
  217. (make-primcall src 'apply (cons lexical args))
  218. (make-call src lexical args))))
  219. (make-lambda src1 meta
  220. (make-lambda-case src2 req #f rest #f '() syms
  221. body #f))))
  222. (_ lexical))))
  223. (define (visit-expr expr)
  224. (post-order
  225. (lambda (expr)
  226. (match expr
  227. (($ <toplevel-ref> src mod name)
  228. (match (declarative-box+value mod name)
  229. (#f expr)
  230. ((box . value)
  231. (residualize src mod name value
  232. (hash-ref declarative (cons mod name))))))
  233. (_ expr)))
  234. expr))
  235. (define (visit-top-level expr mod-vars)
  236. (match expr
  237. (($ <toplevel-define> src mod name exp)
  238. (match (declarative-box+value mod name)
  239. (#f (values (visit-expr expr) mod-vars))
  240. ((#f . value)
  241. (values (add-binding name value (visit-expr exp) (make-void src))
  242. mod-vars))
  243. ((box . value)
  244. (match (assoc-ref mod-vars mod)
  245. (#f
  246. (let* ((mod-var (gensym "mod"))
  247. (mod-vars (acons mod mod-var mod-vars)))
  248. (call-with-values (lambda () (visit-top-level expr mod-vars))
  249. (lambda (tail mod-vars)
  250. (values
  251. (add-binding 'mod
  252. mod-var
  253. (make-primcall src 'current-module '())
  254. tail)
  255. mod-vars)))))
  256. (mod-var
  257. (let* ((loc
  258. (make-primcall src 'module-ensure-local-variable!
  259. (list (make-lexical-ref src 'mod mod-var)
  260. (make-const src name))))
  261. (exp (visit-expr exp))
  262. (ref (residualize src mod name value exp))
  263. (init
  264. (make-primcall src '%variable-set!
  265. (list (make-lexical-ref src name box)
  266. ref))))
  267. (values
  268. (add-binding
  269. name box loc
  270. (add-binding
  271. name value exp
  272. (add-statement src init (make-void src))))
  273. mod-vars)))))))
  274. (($ <seq> src head tail)
  275. (let*-values (((head mod-vars) (visit-top-level head mod-vars))
  276. ((tail mod-vars) (visit-top-level tail mod-vars)))
  277. (values (match head
  278. (($ <letrec> src2 #t names vars vals head)
  279. (fold-right add-binding (add-statement src head tail)
  280. names vars vals))
  281. (else
  282. (add-statement src head tail)))
  283. mod-vars)))
  284. ;; What would the advantages/disadvantages be if we flattened all
  285. ;; bindings here, even those from nested let/letrec?
  286. (_ (values (visit-expr expr) mod-vars))))
  287. (values (visit-top-level expr '())))