peval.scm 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. ;;; Tree-IL partial evaluator
  2. ;; Copyright (C) 2011, 2012, 2013 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 peval)
  17. #:use-module (language tree-il)
  18. #:use-module (language tree-il primitives)
  19. #:use-module (language tree-il effects)
  20. #:use-module (ice-9 vlist)
  21. #:use-module (ice-9 match)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 control)
  27. #:export (peval))
  28. ;;;
  29. ;;; Partial evaluation is Guile's most important source-to-source
  30. ;;; optimization pass. It performs copy propagation, dead code
  31. ;;; elimination, inlining, and constant folding, all while preserving
  32. ;;; the order of effects in the residual program.
  33. ;;;
  34. ;;; For more on partial evaluation, see William Cook’s excellent
  35. ;;; tutorial on partial evaluation at DSL 2011, called “Build your own
  36. ;;; partial evaluator in 90 minutes”[0].
  37. ;;;
  38. ;;; Our implementation of this algorithm was heavily influenced by
  39. ;;; Waddell and Dybvig's paper, "Fast and Effective Procedure Inlining",
  40. ;;; IU CS Dept. TR 484.
  41. ;;;
  42. ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/.
  43. ;;;
  44. ;; First, some helpers.
  45. ;;
  46. (define-syntax *logging* (identifier-syntax #f))
  47. ;; For efficiency we define *logging* to inline to #f, so that the call
  48. ;; to log* gets optimized out. If you want to log, uncomment these
  49. ;; lines:
  50. ;;
  51. ;; (define %logging #f)
  52. ;; (define-syntax *logging* (identifier-syntax %logging))
  53. ;;
  54. ;; Then you can change %logging at runtime.
  55. (define-syntax log
  56. (syntax-rules (quote)
  57. ((log 'event arg ...)
  58. (if (and *logging*
  59. (or (eq? *logging* #t)
  60. (memq 'event *logging*)))
  61. (log* 'event arg ...)))))
  62. (define (log* event . args)
  63. (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print))
  64. 'pretty-print)))
  65. (pp `(log ,event . ,args))
  66. (newline)
  67. (values)))
  68. (define (tree-il-any proc exp)
  69. (let/ec k
  70. (tree-il-fold (lambda (exp res)
  71. (let ((res (proc exp)))
  72. (if res (k res) #f)))
  73. (lambda (exp res) #f)
  74. #f exp)))
  75. (define (vlist-any proc vlist)
  76. (let ((len (vlist-length vlist)))
  77. (let lp ((i 0))
  78. (and (< i len)
  79. (or (proc (vlist-ref vlist i))
  80. (lp (1+ i)))))))
  81. (define (singly-valued-expression? exp)
  82. (match exp
  83. (($ <const>) #t)
  84. (($ <lexical-ref>) #t)
  85. (($ <void>) #t)
  86. (($ <lexical-ref>) #t)
  87. (($ <primitive-ref>) #t)
  88. (($ <module-ref>) #t)
  89. (($ <toplevel-ref>) #t)
  90. (($ <primcall> _ (? singly-valued-primitive?)) #t)
  91. (($ <primcall> _ 'values (val)) #t)
  92. (($ <lambda>) #t)
  93. (($ <conditional> _ test consequent alternate)
  94. (and (singly-valued-expression? consequent)
  95. (singly-valued-expression? alternate)))
  96. (else #f)))
  97. (define (truncate-values x)
  98. "Discard all but the first value of X."
  99. (if (singly-valued-expression? x)
  100. x
  101. (make-primcall (tree-il-src x) 'values (list x))))
  102. ;; Peval will do a one-pass analysis on the source program to determine
  103. ;; the set of assigned lexicals, and to identify unreferenced and
  104. ;; singly-referenced lexicals.
  105. ;;
  106. (define-record-type <var>
  107. (make-var name gensym refcount set?)
  108. var?
  109. (name var-name)
  110. (gensym var-gensym)
  111. (refcount var-refcount set-var-refcount!)
  112. (set? var-set? set-var-set?!))
  113. (define* (build-var-table exp #:optional (table vlist-null))
  114. (tree-il-fold
  115. (lambda (exp res)
  116. (match exp
  117. (($ <lexical-ref> src name gensym)
  118. (let ((var (cdr (vhash-assq gensym res))))
  119. (set-var-refcount! var (1+ (var-refcount var)))
  120. res))
  121. (($ <lambda-case> src req opt rest kw init gensyms body alt)
  122. (fold (lambda (name sym res)
  123. (vhash-consq sym (make-var name sym 0 #f) res))
  124. res
  125. (append req (or opt '()) (if rest (list rest) '())
  126. (match kw
  127. ((aok? (kw name sym) ...) name)
  128. (_ '())))
  129. gensyms))
  130. (($ <let> src names gensyms vals body)
  131. (fold (lambda (name sym res)
  132. (vhash-consq sym (make-var name sym 0 #f) res))
  133. res names gensyms))
  134. (($ <letrec> src in-order? names gensyms vals body)
  135. (fold (lambda (name sym res)
  136. (vhash-consq sym (make-var name sym 0 #f) res))
  137. res names gensyms))
  138. (($ <fix> src names gensyms vals body)
  139. (fold (lambda (name sym res)
  140. (vhash-consq sym (make-var name sym 0 #f) res))
  141. res names gensyms))
  142. (($ <lexical-set> src name gensym exp)
  143. (set-var-set?! (cdr (vhash-assq gensym res)) #t)
  144. res)
  145. (_ res)))
  146. (lambda (exp res) res)
  147. table exp))
  148. ;; Counters are data structures used to limit the effort that peval
  149. ;; spends on particular inlining attempts. Each call site in the source
  150. ;; program is allocated some amount of effort. If peval exceeds the
  151. ;; effort counter while attempting to inline a call site, it aborts the
  152. ;; inlining attempt and residualizes a call instead.
  153. ;;
  154. ;; As there is a fixed number of call sites, that makes `peval' O(N) in
  155. ;; the number of call sites in the source program.
  156. ;;
  157. ;; Counters should limit the size of the residual program as well, but
  158. ;; currently this is not implemented.
  159. ;;
  160. ;; At the top level, before seeing any peval call, there is no counter,
  161. ;; because inlining will terminate as there is no recursion. When peval
  162. ;; sees a call at the top level, it will make a new counter, allocating
  163. ;; it some amount of effort and size.
  164. ;;
  165. ;; This top-level effort counter effectively "prints money". Within a
  166. ;; toplevel counter, no more effort is printed ex nihilo; for a nested
  167. ;; inlining attempt to proceed, effort must be transferred from the
  168. ;; toplevel counter to the nested counter.
  169. ;;
  170. ;; Via `data' and `prev', counters form a linked list, terminating in a
  171. ;; toplevel counter. In practice `data' will be the a pointer to the
  172. ;; source expression of the procedure being inlined.
  173. ;;
  174. ;; In this way peval can detect a recursive inlining attempt, by walking
  175. ;; back on the `prev' links looking for matching `data'. Recursive
  176. ;; counters receive a more limited effort allocation, as we don't want
  177. ;; to spend all of the effort for a toplevel inlining site on loops.
  178. ;; Also, recursive counters don't need a prompt at each inlining site:
  179. ;; either the call chain folds entirely, or it will be residualized at
  180. ;; its original call.
  181. ;;
  182. (define-record-type <counter>
  183. (%make-counter effort size continuation recursive? data prev)
  184. counter?
  185. (effort effort-counter)
  186. (size size-counter)
  187. (continuation counter-continuation)
  188. (recursive? counter-recursive? set-counter-recursive?!)
  189. (data counter-data)
  190. (prev counter-prev))
  191. (define (abort-counter c)
  192. ((counter-continuation c)))
  193. (define (record-effort! c)
  194. (let ((e (effort-counter c)))
  195. (if (zero? (variable-ref e))
  196. (abort-counter c)
  197. (variable-set! e (1- (variable-ref e))))))
  198. (define (record-size! c)
  199. (let ((s (size-counter c)))
  200. (if (zero? (variable-ref s))
  201. (abort-counter c)
  202. (variable-set! s (1- (variable-ref s))))))
  203. (define (find-counter data counter)
  204. (and counter
  205. (if (eq? data (counter-data counter))
  206. counter
  207. (find-counter data (counter-prev counter)))))
  208. (define* (transfer! from to #:optional
  209. (effort (variable-ref (effort-counter from)))
  210. (size (variable-ref (size-counter from))))
  211. (define (transfer-counter! from-v to-v amount)
  212. (let* ((from-balance (variable-ref from-v))
  213. (to-balance (variable-ref to-v))
  214. (amount (min amount from-balance)))
  215. (variable-set! from-v (- from-balance amount))
  216. (variable-set! to-v (+ to-balance amount))))
  217. (transfer-counter! (effort-counter from) (effort-counter to) effort)
  218. (transfer-counter! (size-counter from) (size-counter to) size))
  219. (define (make-top-counter effort-limit size-limit continuation data)
  220. (%make-counter (make-variable effort-limit)
  221. (make-variable size-limit)
  222. continuation
  223. #t
  224. data
  225. #f))
  226. (define (make-nested-counter continuation data current)
  227. (let ((c (%make-counter (make-variable 0)
  228. (make-variable 0)
  229. continuation
  230. #f
  231. data
  232. current)))
  233. (transfer! current c)
  234. c))
  235. (define (make-recursive-counter effort-limit size-limit orig current)
  236. (let ((c (%make-counter (make-variable 0)
  237. (make-variable 0)
  238. (counter-continuation orig)
  239. #t
  240. (counter-data orig)
  241. current)))
  242. (transfer! current c effort-limit size-limit)
  243. c))
  244. ;; Operand structures allow bindings to be processed lazily instead of
  245. ;; eagerly. By doing so, hopefully we can get process them in a way
  246. ;; appropriate to their use contexts. Operands also prevent values from
  247. ;; being visited multiple times, wasting effort.
  248. ;;
  249. ;; TODO: Record value size in operand structure?
  250. ;;
  251. (define-record-type <operand>
  252. (%make-operand var sym visit source visit-count use-count
  253. copyable? residual-value constant-value alias-value)
  254. operand?
  255. (var operand-var)
  256. (sym operand-sym)
  257. (visit %operand-visit)
  258. (source operand-source)
  259. (visit-count operand-visit-count set-operand-visit-count!)
  260. (use-count operand-use-count set-operand-use-count!)
  261. (copyable? operand-copyable? set-operand-copyable?!)
  262. (residual-value operand-residual-value %set-operand-residual-value!)
  263. (constant-value operand-constant-value set-operand-constant-value!)
  264. (alias-value operand-alias-value set-operand-alias-value!))
  265. (define* (make-operand var sym #:optional source visit alias)
  266. ;; Bind SYM to VAR, with value SOURCE. Unassigned bound operands are
  267. ;; considered copyable until we prove otherwise. If we have a source
  268. ;; expression, truncate it to one value. Copy propagation does not
  269. ;; work on multiply-valued expressions.
  270. (let ((source (and=> source truncate-values)))
  271. (%make-operand var sym visit source 0 0
  272. (and source (not (var-set? var))) #f #f
  273. (and (not (var-set? var)) alias))))
  274. (define* (make-bound-operands vars syms sources visit #:optional aliases)
  275. (if aliases
  276. (map (lambda (name sym source alias)
  277. (make-operand name sym source visit alias))
  278. vars syms sources aliases)
  279. (map (lambda (name sym source)
  280. (make-operand name sym source visit #f))
  281. vars syms sources)))
  282. (define (make-unbound-operands vars syms)
  283. (map make-operand vars syms))
  284. (define (set-operand-residual-value! op val)
  285. (%set-operand-residual-value!
  286. op
  287. (match val
  288. (($ <primcall> src 'values (first))
  289. ;; The continuation of a residualized binding does not need the
  290. ;; introduced `values' node, so undo the effects of truncation.
  291. first)
  292. (else
  293. val))))
  294. (define* (visit-operand op counter ctx #:optional effort-limit size-limit)
  295. ;; Peval is O(N) in call sites of the source program. However,
  296. ;; visiting an operand can introduce new call sites. If we visit an
  297. ;; operand outside a counter -- i.e., outside an inlining attempt --
  298. ;; this can lead to divergence. So, if we are visiting an operand to
  299. ;; try to copy it, and there is no counter, make a new one.
  300. ;;
  301. ;; This will only happen at most as many times as there are lexical
  302. ;; references in the source program.
  303. (and (zero? (operand-visit-count op))
  304. (dynamic-wind
  305. (lambda ()
  306. (set-operand-visit-count! op (1+ (operand-visit-count op))))
  307. (lambda ()
  308. (and (operand-source op)
  309. (if (or counter (and (not effort-limit) (not size-limit)))
  310. ((%operand-visit op) (operand-source op) counter ctx)
  311. (let/ec k
  312. (define (abort)
  313. ;; If we abort when visiting the value in a
  314. ;; fresh context, we won't succeed in any future
  315. ;; attempt, so don't try to copy it again.
  316. (set-operand-copyable?! op #f)
  317. (k #f))
  318. ((%operand-visit op)
  319. (operand-source op)
  320. (make-top-counter effort-limit size-limit abort op)
  321. ctx)))))
  322. (lambda ()
  323. (set-operand-visit-count! op (1- (operand-visit-count op)))))))
  324. ;; A helper for constant folding.
  325. ;;
  326. (define (types-check? primitive-name args)
  327. (case primitive-name
  328. ((values) #t)
  329. ((not pair? null? list? symbol? vector? struct?)
  330. (= (length args) 1))
  331. ((eq? eqv? equal?)
  332. (= (length args) 2))
  333. ;; FIXME: add more cases?
  334. (else #f)))
  335. (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
  336. #:key
  337. (operator-size-limit 40)
  338. (operand-size-limit 20)
  339. (value-size-limit 10)
  340. (effort-limit 500)
  341. (recursive-effort-limit 100))
  342. "Partially evaluate EXP in compilation environment CENV, with
  343. top-level bindings from ENV and return the resulting expression."
  344. ;; This is a simple partial evaluator. It effectively performs
  345. ;; constant folding, copy propagation, dead code elimination, and
  346. ;; inlining.
  347. ;; TODO:
  348. ;;
  349. ;; Propagate copies across toplevel bindings, if we can prove the
  350. ;; bindings to be immutable.
  351. ;;
  352. ;; Specialize lambda expressions with invariant arguments.
  353. (define local-toplevel-env
  354. ;; The top-level environment of the module being compiled.
  355. (let ()
  356. (define (env-folder x env)
  357. (match x
  358. (($ <toplevel-define> _ name)
  359. (vhash-consq name #t env))
  360. (($ <seq> _ head tail)
  361. (env-folder tail (env-folder head env)))
  362. (_ env)))
  363. (env-folder exp vlist-null)))
  364. (define (local-toplevel? name)
  365. (vhash-assq name local-toplevel-env))
  366. ;; gensym -> <var>
  367. ;; renamed-term -> original-term
  368. ;;
  369. (define store (build-var-table exp))
  370. (define (record-new-temporary! name sym refcount)
  371. (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
  372. (define (lookup-var sym)
  373. (let ((v (vhash-assq sym store)))
  374. (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
  375. (define (fresh-gensyms vars)
  376. (map (lambda (var)
  377. (let ((new (gensym (string-append (symbol->string (var-name var))
  378. " "))))
  379. (set! store (vhash-consq new var store))
  380. new))
  381. vars))
  382. (define (fresh-temporaries ls)
  383. (map (lambda (elt)
  384. (let ((new (gensym "tmp ")))
  385. (record-new-temporary! 'tmp new 1)
  386. new))
  387. ls))
  388. (define (assigned-lexical? sym)
  389. (var-set? (lookup-var sym)))
  390. (define (lexical-refcount sym)
  391. (var-refcount (lookup-var sym)))
  392. (define (with-temporaries src exps refcount can-copy? k)
  393. (let* ((pairs (map (match-lambda
  394. ((and exp (? can-copy?))
  395. (cons #f exp))
  396. (exp
  397. (let ((sym (gensym "tmp ")))
  398. (record-new-temporary! 'tmp sym refcount)
  399. (cons sym exp))))
  400. exps))
  401. (tmps (filter car pairs)))
  402. (match tmps
  403. (() (k exps))
  404. (tmps
  405. (make-let src
  406. (make-list (length tmps) 'tmp)
  407. (map car tmps)
  408. (map cdr tmps)
  409. (k (map (match-lambda
  410. ((#f . val) val)
  411. ((sym . _)
  412. (make-lexical-ref #f 'tmp sym)))
  413. pairs)))))))
  414. (define (make-begin0 src first second)
  415. (make-let-values
  416. src
  417. first
  418. (let ((vals (gensym "vals ")))
  419. (record-new-temporary! 'vals vals 1)
  420. (make-lambda-case
  421. #f
  422. '() #f 'vals #f '() (list vals)
  423. (make-seq
  424. src
  425. second
  426. (make-primcall #f 'apply
  427. (list
  428. (make-primitive-ref #f 'values)
  429. (make-lexical-ref #f 'vals vals))))
  430. #f))))
  431. ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
  432. ;; from it to ORIG.
  433. ;;
  434. (define (record-source-expression! orig new)
  435. (set! store (vhash-consq new (source-expression orig) store))
  436. new)
  437. ;; Find the source expression corresponding to NEW. Used to detect
  438. ;; recursive inlining attempts.
  439. ;;
  440. (define (source-expression new)
  441. (let ((x (vhash-assq new store)))
  442. (if x (cdr x) new)))
  443. (define (record-operand-use op)
  444. (set-operand-use-count! op (1+ (operand-use-count op))))
  445. (define (unrecord-operand-uses op n)
  446. (let ((count (- (operand-use-count op) n)))
  447. (when (zero? count)
  448. (set-operand-residual-value! op #f))
  449. (set-operand-use-count! op count)))
  450. (define* (residualize-lexical op #:optional ctx val)
  451. (log 'residualize op)
  452. (record-operand-use op)
  453. (if (memq ctx '(value values))
  454. (set-operand-residual-value! op val))
  455. (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
  456. (define (fold-constants src name args ctx)
  457. (define (apply-primitive name args)
  458. ;; todo: further optimize commutative primitives
  459. (catch #t
  460. (lambda ()
  461. (call-with-values
  462. (lambda ()
  463. (apply (module-ref the-scm-module name) args))
  464. (lambda results
  465. (values #t results))))
  466. (lambda _
  467. (values #f '()))))
  468. (define (make-values src values)
  469. (match values
  470. ((single) single) ; 1 value
  471. ((_ ...) ; 0, or 2 or more values
  472. (make-primcall src 'values values))))
  473. (define (residualize-call)
  474. (make-primcall src name args))
  475. (cond
  476. ((every const? args)
  477. (let-values (((success? values)
  478. (apply-primitive name (map const-exp args))))
  479. (log 'fold success? values name args)
  480. (if success?
  481. (case ctx
  482. ((effect) (make-void src))
  483. ((test)
  484. ;; Values truncation: only take the first
  485. ;; value.
  486. (if (pair? values)
  487. (make-const src (car values))
  488. (make-values src '())))
  489. (else
  490. (make-values src (map (cut make-const src <>) values))))
  491. (residualize-call))))
  492. ((and (eq? ctx 'effect) (types-check? name args))
  493. (make-void #f))
  494. (else
  495. (residualize-call))))
  496. (define (inline-values src exp nmin nmax consumer)
  497. (let loop ((exp exp))
  498. (match exp
  499. ;; Some expression types are always singly-valued.
  500. ((or ($ <const>)
  501. ($ <void>)
  502. ($ <lambda>)
  503. ($ <lexical-ref>)
  504. ($ <toplevel-ref>)
  505. ($ <module-ref>)
  506. ($ <primitive-ref>)
  507. ($ <lexical-set>) ; FIXME: these set! expressions
  508. ($ <toplevel-set>) ; could return zero values in
  509. ($ <toplevel-define>) ; the future
  510. ($ <module-set>) ;
  511. ($ <primcall> src (? singly-valued-primitive?)))
  512. (and (<= nmin 1) (or (not nmax) (>= nmax 1))
  513. (make-call src (make-lambda #f '() consumer) (list exp))))
  514. ;; Statically-known number of values.
  515. (($ <primcall> src 'values vals)
  516. (and (<= nmin (length vals)) (or (not nmax) (>= nmax (length vals)))
  517. (make-call src (make-lambda #f '() consumer) vals)))
  518. ;; Not going to copy code into both branches.
  519. (($ <conditional>) #f)
  520. ;; Bail on other applications.
  521. (($ <call>) #f)
  522. (($ <primcall>) #f)
  523. ;; Bail on prompt and abort.
  524. (($ <prompt>) #f)
  525. (($ <abort>) #f)
  526. ;; Propagate to tail positions.
  527. (($ <let> src names gensyms vals body)
  528. (let ((body (loop body)))
  529. (and body
  530. (make-let src names gensyms vals body))))
  531. (($ <letrec> src in-order? names gensyms vals body)
  532. (let ((body (loop body)))
  533. (and body
  534. (make-letrec src in-order? names gensyms vals body))))
  535. (($ <fix> src names gensyms vals body)
  536. (let ((body (loop body)))
  537. (and body
  538. (make-fix src names gensyms vals body))))
  539. (($ <let-values> src exp
  540. ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
  541. (let ((body (loop body)))
  542. (and body
  543. (make-let-values src exp
  544. (make-lambda-case src2 req opt rest kw
  545. inits gensyms body #f)))))
  546. (($ <seq> src head tail)
  547. (let ((tail (loop tail)))
  548. (and tail (make-seq src head tail)))))))
  549. (define compute-effects
  550. (make-effects-analyzer assigned-lexical?))
  551. (define (constant-expression? x)
  552. ;; Return true if X is constant, for the purposes of copying or
  553. ;; elision---i.e., if it is known to have no effects, does not
  554. ;; allocate storage for a mutable object, and does not access
  555. ;; mutable data (like `car' or toplevel references).
  556. (constant? (compute-effects x)))
  557. (define (prune-bindings ops in-order? body counter ctx build-result)
  558. ;; This helper handles both `let' and `letrec'/`fix'. In the latter
  559. ;; cases we need to make sure that if referenced binding A needs
  560. ;; as-yet-unreferenced binding B, that B is processed for value.
  561. ;; Likewise if C, when processed for effect, needs otherwise
  562. ;; unreferenced D, then D needs to be processed for value too.
  563. ;;
  564. (define (referenced? op)
  565. ;; When we visit lambdas in operator context, we just copy them,
  566. ;; as we will process their body later. However this does have
  567. ;; the problem that any free var referenced by the lambda is not
  568. ;; marked as needing residualization. Here we hack around this
  569. ;; and treat all bindings as referenced if we are in operator
  570. ;; context.
  571. (or (eq? ctx 'operator)
  572. (not (zero? (operand-use-count op)))))
  573. ;; values := (op ...)
  574. ;; effects := (op ...)
  575. (define (residualize values effects)
  576. ;; Note, values and effects are reversed.
  577. (cond
  578. (in-order?
  579. (let ((values (filter operand-residual-value ops)))
  580. (if (null? values)
  581. body
  582. (build-result (map (compose var-name operand-var) values)
  583. (map operand-sym values)
  584. (map operand-residual-value values)
  585. body))))
  586. (else
  587. (let ((body
  588. (if (null? effects)
  589. body
  590. (let ((effect-vals (map operand-residual-value effects)))
  591. (list->seq #f (reverse (cons body effect-vals)))))))
  592. (if (null? values)
  593. body
  594. (let ((values (reverse values)))
  595. (build-result (map (compose var-name operand-var) values)
  596. (map operand-sym values)
  597. (map operand-residual-value values)
  598. body)))))))
  599. ;; old := (bool ...)
  600. ;; values := (op ...)
  601. ;; effects := ((op . value) ...)
  602. (let prune ((old (map referenced? ops)) (values '()) (effects '()))
  603. (let lp ((ops* ops) (values values) (effects effects))
  604. (cond
  605. ((null? ops*)
  606. (let ((new (map referenced? ops)))
  607. (if (not (equal? new old))
  608. (prune new values '())
  609. (residualize values
  610. (map (lambda (op val)
  611. (set-operand-residual-value! op val)
  612. op)
  613. (map car effects) (map cdr effects))))))
  614. (else
  615. (let ((op (car ops*)))
  616. (cond
  617. ((memq op values)
  618. (lp (cdr ops*) values effects))
  619. ((operand-residual-value op)
  620. (lp (cdr ops*) (cons op values) effects))
  621. ((referenced? op)
  622. (set-operand-residual-value! op (visit-operand op counter 'value))
  623. (lp (cdr ops*) (cons op values) effects))
  624. (else
  625. (lp (cdr ops*)
  626. values
  627. (let ((effect (visit-operand op counter 'effect)))
  628. (if (void? effect)
  629. effects
  630. (acons op effect effects))))))))))))
  631. (define (small-expression? x limit)
  632. (let/ec k
  633. (tree-il-fold
  634. (lambda (x res) ; down
  635. (1+ res))
  636. (lambda (x res) ; up
  637. (if (< res limit)
  638. res
  639. (k #f)))
  640. 0 x)
  641. #t))
  642. (define (extend-env sym op env)
  643. (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
  644. (let loop ((exp exp)
  645. (env vlist-null) ; vhash of gensym -> <operand>
  646. (counter #f) ; inlined call stack
  647. (ctx 'values)) ; effect, value, values, test, operator, or call
  648. (define (lookup var)
  649. (cond
  650. ((vhash-assq var env) => cdr)
  651. (else (error "unbound var" var))))
  652. ;; Find a value referenced a specific number of times. This is a hack
  653. ;; that's used for propagating fresh data structures like rest lists and
  654. ;; prompt tags. Usually we wouldn't copy consed data, but we can do so in
  655. ;; some special cases like `apply' or prompts if we can account
  656. ;; for all of its uses.
  657. ;;
  658. ;; You don't want to use this in general because it introduces a slight
  659. ;; nonlinearity by running peval again (though with a small effort and size
  660. ;; counter).
  661. ;;
  662. (define (find-definition x n-aliases)
  663. (cond
  664. ((lexical-ref? x)
  665. (cond
  666. ((lookup (lexical-ref-gensym x))
  667. => (lambda (op)
  668. (if (var-set? (operand-var op))
  669. (values #f #f)
  670. (let ((y (or (operand-residual-value op)
  671. (visit-operand op counter 'value 10 10)
  672. (operand-source op))))
  673. (cond
  674. ((and (lexical-ref? y)
  675. (= (lexical-refcount (lexical-ref-gensym x)) 1))
  676. ;; X is a simple alias for Y. Recurse, regardless of
  677. ;; the number of aliases we were expecting.
  678. (find-definition y n-aliases))
  679. ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases)
  680. ;; We found a definition that is aliased the right
  681. ;; number of times. We still recurse in case it is a
  682. ;; lexical.
  683. (values (find-definition y 1)
  684. op))
  685. (else
  686. ;; We can't account for our aliases.
  687. (values #f #f)))))))
  688. (else
  689. ;; A formal parameter. Can't say anything about that.
  690. (values #f #f))))
  691. ((= n-aliases 1)
  692. ;; Not a lexical: success, but only if we are looking for an
  693. ;; unaliased value.
  694. (values x #f))
  695. (else (values #f #f))))
  696. (define (visit exp ctx)
  697. (loop exp env counter ctx))
  698. (define (for-value exp) (visit exp 'value))
  699. (define (for-values exp) (visit exp 'values))
  700. (define (for-test exp) (visit exp 'test))
  701. (define (for-effect exp) (visit exp 'effect))
  702. (define (for-call exp) (visit exp 'call))
  703. (define (for-tail exp) (visit exp ctx))
  704. (if counter
  705. (record-effort! counter))
  706. (log 'visit ctx (and=> counter effort-counter)
  707. (unparse-tree-il exp))
  708. (match exp
  709. (($ <const>)
  710. (case ctx
  711. ((effect) (make-void #f))
  712. (else exp)))
  713. (($ <void>)
  714. (case ctx
  715. ((test) (make-const #f #t))
  716. (else exp)))
  717. (($ <lexical-ref> _ _ gensym)
  718. (log 'begin-copy gensym)
  719. (let ((op (lookup gensym)))
  720. (cond
  721. ((eq? ctx 'effect)
  722. (log 'lexical-for-effect gensym)
  723. (make-void #f))
  724. ((operand-alias-value op)
  725. ;; This is an unassigned operand that simply aliases some
  726. ;; other operand. Recurse to avoid residualizing the leaf
  727. ;; binding.
  728. => for-tail)
  729. ((eq? ctx 'call)
  730. ;; Don't propagate copies if we are residualizing a call.
  731. (log 'residualize-lexical-call gensym op)
  732. (residualize-lexical op))
  733. ((var-set? (operand-var op))
  734. ;; Assigned lexicals don't copy-propagate.
  735. (log 'assigned-var gensym op)
  736. (residualize-lexical op))
  737. ((not (operand-copyable? op))
  738. ;; We already know that this operand is not copyable.
  739. (log 'not-copyable gensym op)
  740. (residualize-lexical op))
  741. ((and=> (operand-constant-value op)
  742. (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
  743. ;; A cache hit.
  744. (let ((val (operand-constant-value op)))
  745. (log 'memoized-constant gensym val)
  746. (for-tail val)))
  747. ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
  748. recursive-effort-limit operand-size-limit)
  749. =>
  750. ;; If we end up deciding to residualize this value instead of
  751. ;; copying it, save that residualized value.
  752. (lambda (val)
  753. (cond
  754. ((not (constant-expression? val))
  755. (log 'not-constant gensym op)
  756. ;; At this point, ctx is operator, test, or value. A
  757. ;; value that is non-constant in one context will be
  758. ;; non-constant in the others, so it's safe to record
  759. ;; that here, and avoid future visits.
  760. (set-operand-copyable?! op #f)
  761. (residualize-lexical op ctx val))
  762. ((or (const? val)
  763. (void? val)
  764. (primitive-ref? val))
  765. ;; Always propagate simple values that cannot lead to
  766. ;; code bloat.
  767. (log 'copy-simple gensym val)
  768. ;; It could be this constant is the result of folding.
  769. ;; If that is the case, cache it. This helps loop
  770. ;; unrolling get farther.
  771. (if (or (eq? ctx 'value) (eq? ctx 'values))
  772. (begin
  773. (log 'memoize-constant gensym val)
  774. (set-operand-constant-value! op val)))
  775. val)
  776. ((= 1 (var-refcount (operand-var op)))
  777. ;; Always propagate values referenced only once.
  778. (log 'copy-single gensym val)
  779. val)
  780. ;; FIXME: do demand-driven size accounting rather than
  781. ;; these heuristics.
  782. ((eq? ctx 'operator)
  783. ;; A pure expression in the operator position. Inline
  784. ;; if it's a lambda that's small enough.
  785. (if (and (lambda? val)
  786. (small-expression? val operator-size-limit))
  787. (begin
  788. (log 'copy-operator gensym val)
  789. val)
  790. (begin
  791. (log 'too-big-for-operator gensym val)
  792. (residualize-lexical op ctx val))))
  793. (else
  794. ;; A pure expression, processed for call or for value.
  795. ;; Don't inline lambdas, because they will probably won't
  796. ;; fold because we don't know the operator.
  797. (if (and (small-expression? val value-size-limit)
  798. (not (tree-il-any lambda? val)))
  799. (begin
  800. (log 'copy-value gensym val)
  801. val)
  802. (begin
  803. (log 'too-big-or-has-lambda gensym val)
  804. (residualize-lexical op ctx val)))))))
  805. (else
  806. ;; Visit failed. Either the operand isn't bound, as in
  807. ;; lambda formal parameters, or the copy was aborted.
  808. (log 'unbound-or-aborted gensym op)
  809. (residualize-lexical op)))))
  810. (($ <lexical-set> src name gensym exp)
  811. (let ((op (lookup gensym)))
  812. (if (zero? (var-refcount (operand-var op)))
  813. (let ((exp (for-effect exp)))
  814. (if (void? exp)
  815. exp
  816. (make-seq src exp (make-void #f))))
  817. (begin
  818. (record-operand-use op)
  819. (make-lexical-set src name (operand-sym op) (for-value exp))))))
  820. (($ <let> src
  821. (names ... rest)
  822. (gensyms ... rest-sym)
  823. (vals ... ($ <primcall> _ 'list rest-args))
  824. ($ <primcall> asrc 'apply
  825. (proc args ...
  826. ($ <lexical-ref> _
  827. (? (cut eq? <> rest))
  828. (? (lambda (sym)
  829. (and (eq? sym rest-sym)
  830. (= (lexical-refcount sym) 1))))))))
  831. (let* ((tmps (make-list (length rest-args) 'tmp))
  832. (tmp-syms (fresh-temporaries tmps)))
  833. (for-tail
  834. (make-let src
  835. (append names tmps)
  836. (append gensyms tmp-syms)
  837. (append vals rest-args)
  838. (make-call
  839. asrc
  840. proc
  841. (append args
  842. (map (cut make-lexical-ref #f <> <>)
  843. tmps tmp-syms)))))))
  844. (($ <let> src names gensyms vals body)
  845. (define (compute-alias exp)
  846. ;; It's very common for macros to introduce something like:
  847. ;;
  848. ;; ((lambda (x y) ...) x-exp y-exp)
  849. ;;
  850. ;; In that case you might end up trying to inline something like:
  851. ;;
  852. ;; (let ((x x-exp) (y y-exp)) ...)
  853. ;;
  854. ;; But if x-exp is itself a lexical-ref that aliases some much
  855. ;; larger expression, perhaps it will fail to inline due to
  856. ;; size. However we don't want to introduce a useless alias
  857. ;; (in this case, x). So if the RHS of a let expression is a
  858. ;; lexical-ref, we record that expression. If we end up having
  859. ;; to residualize X, then instead we residualize X-EXP, as long
  860. ;; as it isn't assigned.
  861. ;;
  862. (match exp
  863. (($ <lexical-ref> _ _ sym)
  864. (let ((op (lookup sym)))
  865. (and (not (var-set? (operand-var op)))
  866. (or (operand-alias-value op)
  867. exp))))
  868. (_ #f)))
  869. (let* ((vars (map lookup-var gensyms))
  870. (new (fresh-gensyms vars))
  871. (ops (make-bound-operands vars new vals
  872. (lambda (exp counter ctx)
  873. (loop exp env counter ctx))
  874. (map compute-alias vals)))
  875. (env (fold extend-env env gensyms ops))
  876. (body (loop body env counter ctx)))
  877. (cond
  878. ((const? body)
  879. (for-tail (list->seq src (append vals (list body)))))
  880. ((and (lexical-ref? body)
  881. (memq (lexical-ref-gensym body) new))
  882. (let ((sym (lexical-ref-gensym body))
  883. (pairs (map cons new vals)))
  884. ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
  885. (for-tail
  886. (list->seq
  887. src
  888. (append (map cdr (alist-delete sym pairs eq?))
  889. (list (assq-ref pairs sym)))))))
  890. (else
  891. ;; Only include bindings for which lexical references
  892. ;; have been residualized.
  893. (prune-bindings ops #f body counter ctx
  894. (lambda (names gensyms vals body)
  895. (if (null? names) (error "what!" names))
  896. (make-let src names gensyms vals body)))))))
  897. (($ <letrec> src in-order? names gensyms vals body)
  898. ;; Note the difference from the `let' case: here we use letrec*
  899. ;; so that the `visit' procedure for the new operands closes over
  900. ;; an environment that includes the operands. Also we don't try
  901. ;; to elide aliases, because we can't sensibly reduce something
  902. ;; like (letrec ((a b) (b a)) a).
  903. (letrec* ((visit (lambda (exp counter ctx)
  904. (loop exp env* counter ctx)))
  905. (vars (map lookup-var gensyms))
  906. (new (fresh-gensyms vars))
  907. (ops (make-bound-operands vars new vals visit))
  908. (env* (fold extend-env env gensyms ops))
  909. (body* (visit body counter ctx)))
  910. (if (and (const? body*) (every constant-expression? vals))
  911. ;; We may have folded a loop completely, even though there
  912. ;; might be cyclical references between the bound values.
  913. ;; Handle this degenerate case specially.
  914. body*
  915. (prune-bindings ops in-order? body* counter ctx
  916. (lambda (names gensyms vals body)
  917. (make-letrec src in-order?
  918. names gensyms vals body))))))
  919. (($ <fix> src names gensyms vals body)
  920. (letrec* ((visit (lambda (exp counter ctx)
  921. (loop exp env* counter ctx)))
  922. (vars (map lookup-var gensyms))
  923. (new (fresh-gensyms vars))
  924. (ops (make-bound-operands vars new vals visit))
  925. (env* (fold extend-env env gensyms ops))
  926. (body* (visit body counter ctx)))
  927. (if (const? body*)
  928. body*
  929. (prune-bindings ops #f body* counter ctx
  930. (lambda (names gensyms vals body)
  931. (make-fix src names gensyms vals body))))))
  932. (($ <let-values> lv-src producer consumer)
  933. ;; Peval the producer, then try to inline the consumer into
  934. ;; the producer. If that succeeds, peval again. Otherwise
  935. ;; reconstruct the let-values, pevaling the consumer.
  936. (let ((producer (for-values producer)))
  937. (or (match consumer
  938. (($ <lambda-case> src (req-name) #f #f #f () (req-sym) body #f)
  939. (for-tail
  940. (make-let src (list req-name) (list req-sym) (list producer)
  941. body)))
  942. ((and ($ <lambda-case> src () #f rest #f () (rest-sym) body #f)
  943. (? (lambda _ (singly-valued-expression? producer))))
  944. (let ((tmp (gensym "tmp ")))
  945. (record-new-temporary! 'tmp tmp 1)
  946. (for-tail
  947. (make-let
  948. src (list 'tmp) (list tmp) (list producer)
  949. (make-let
  950. src (list rest) (list rest-sym)
  951. (list
  952. (make-primcall #f 'list
  953. (list (make-lexical-ref #f 'tmp tmp))))
  954. body)))))
  955. (($ <lambda-case> src req opt rest #f inits gensyms body #f)
  956. (let* ((nmin (length req))
  957. (nmax (and (not rest) (+ nmin (if opt (length opt) 0)))))
  958. (cond
  959. ((inline-values lv-src producer nmin nmax consumer)
  960. => for-tail)
  961. (else #f))))
  962. (_ #f))
  963. (make-let-values lv-src producer (for-tail consumer)))))
  964. (($ <toplevel-ref> src (? effect-free-primitive? name))
  965. exp)
  966. (($ <toplevel-ref>)
  967. ;; todo: open private local bindings.
  968. exp)
  969. (($ <module-ref> src module (? effect-free-primitive? name) #f)
  970. (let ((module (false-if-exception
  971. (resolve-module module #:ensure #f))))
  972. (if (module? module)
  973. (let ((var (module-variable module name)))
  974. (if (eq? var (module-variable the-scm-module name))
  975. (make-primitive-ref src name)
  976. exp))
  977. exp)))
  978. (($ <module-ref>)
  979. exp)
  980. (($ <module-set> src mod name public? exp)
  981. (make-module-set src mod name public? (for-value exp)))
  982. (($ <toplevel-define> src name exp)
  983. (make-toplevel-define src name (for-value exp)))
  984. (($ <toplevel-set> src name exp)
  985. (make-toplevel-set src name (for-value exp)))
  986. (($ <primitive-ref>)
  987. (case ctx
  988. ((effect) (make-void #f))
  989. ((test) (make-const #f #t))
  990. (else exp)))
  991. (($ <conditional> src condition subsequent alternate)
  992. (define (call-with-failure-thunk exp proc)
  993. (match exp
  994. (($ <call> _ _ ()) (proc exp))
  995. (($ <primcall> _ _ ()) (proc exp))
  996. (($ <const>) (proc exp))
  997. (($ <void>) (proc exp))
  998. (($ <lexical-ref>) (proc exp))
  999. (_
  1000. (let ((t (gensym "failure-")))
  1001. (record-new-temporary! 'failure t 2)
  1002. (make-let
  1003. src (list 'failure) (list t)
  1004. (list
  1005. (make-lambda
  1006. #f '()
  1007. (make-lambda-case #f '() #f #f #f '() '() exp #f)))
  1008. (proc (make-call #f (make-lexical-ref #f 'failure t)
  1009. '())))))))
  1010. (define (simplify-conditional c)
  1011. (match c
  1012. ;; Swap the arms of (if (not FOO) A B), to simplify.
  1013. (($ <conditional> src ($ <primcall> _ 'not (pred))
  1014. subsequent alternate)
  1015. (simplify-conditional
  1016. (make-conditional src pred alternate subsequent)))
  1017. ;; Special cases for common tests in the predicates of chains
  1018. ;; of if expressions.
  1019. (($ <conditional> src
  1020. ($ <conditional> src* outer-test inner-test ($ <const> _ #f))
  1021. inner-subsequent
  1022. alternate)
  1023. (let lp ((alternate alternate))
  1024. (match alternate
  1025. ;; Lift a common repeated test out of a chain of if
  1026. ;; expressions.
  1027. (($ <conditional> _ (? (cut tree-il=? outer-test <>))
  1028. other-subsequent alternate)
  1029. (make-conditional
  1030. src outer-test
  1031. (simplify-conditional
  1032. (make-conditional src* inner-test inner-subsequent
  1033. other-subsequent))
  1034. alternate))
  1035. ;; Likewise, but punching through any surrounding
  1036. ;; failure continuations.
  1037. (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body)
  1038. (make-let
  1039. let-src (list name) (list sym) (list thunk)
  1040. (lp body)))
  1041. ;; Otherwise, rotate AND tests to expose a simple
  1042. ;; condition in the front. Although this may result in
  1043. ;; lexically binding failure thunks, the thunks will be
  1044. ;; compiled to labels allocation, so there's no actual
  1045. ;; code growth.
  1046. (_
  1047. (call-with-failure-thunk
  1048. alternate
  1049. (lambda (failure)
  1050. (make-conditional
  1051. src outer-test
  1052. (simplify-conditional
  1053. (make-conditional src* inner-test inner-subsequent failure))
  1054. failure)))))))
  1055. (_ c)))
  1056. (match (for-test condition)
  1057. (($ <const> _ val)
  1058. (if val
  1059. (for-tail subsequent)
  1060. (for-tail alternate)))
  1061. (c
  1062. (simplify-conditional
  1063. (make-conditional src c (for-tail subsequent)
  1064. (for-tail alternate))))))
  1065. (($ <primcall> src 'call-with-values
  1066. (producer
  1067. ($ <lambda> _ _
  1068. (and consumer
  1069. ;; No optional or kwargs.
  1070. ($ <lambda-case>
  1071. _ req #f rest #f () gensyms body #f)))))
  1072. (for-tail (make-let-values src (make-call src producer '())
  1073. consumer)))
  1074. (($ <primcall> src 'dynamic-wind (w thunk u))
  1075. (for-tail
  1076. (with-temporaries
  1077. src (list w u) 2 constant-expression?
  1078. (match-lambda
  1079. ((w u)
  1080. (make-seq
  1081. src
  1082. (make-seq
  1083. src
  1084. (make-conditional
  1085. src
  1086. ;; fixme: introduce logic to fold thunk?
  1087. (make-primcall src 'thunk? (list u))
  1088. (make-call src w '())
  1089. (make-primcall
  1090. src 'scm-error
  1091. (list
  1092. (make-const #f 'wrong-type-arg)
  1093. (make-const #f "dynamic-wind")
  1094. (make-const #f "Wrong type (expecting thunk): ~S")
  1095. (make-primcall #f 'list (list u))
  1096. (make-primcall #f 'list (list u)))))
  1097. (make-primcall src 'wind (list w u)))
  1098. (make-begin0 src
  1099. (make-call src thunk '())
  1100. (make-seq src
  1101. (make-primcall src 'unwind '())
  1102. (make-call src u '())))))))))
  1103. (($ <primcall> src 'with-fluid* (f v thunk))
  1104. (for-tail
  1105. (with-temporaries
  1106. src (list f v thunk) 1 constant-expression?
  1107. (match-lambda
  1108. ((f v thunk)
  1109. (make-seq src
  1110. (make-primcall src 'push-fluid (list f v))
  1111. (make-begin0 src
  1112. (make-call src thunk '())
  1113. (make-primcall src 'pop-fluid '()))))))))
  1114. (($ <primcall> src 'values exps)
  1115. (cond
  1116. ((null? exps)
  1117. (if (eq? ctx 'effect)
  1118. (make-void #f)
  1119. exp))
  1120. (else
  1121. (let ((vals (map for-value exps)))
  1122. (if (and (case ctx
  1123. ((value test effect) #t)
  1124. (else (null? (cdr vals))))
  1125. (every singly-valued-expression? vals))
  1126. (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
  1127. (make-primcall src 'values vals))))))
  1128. (($ <primcall> src 'apply (proc args ... tail))
  1129. (let lp ((tail* (find-definition tail 1)) (speculative? #t))
  1130. (define (copyable? x)
  1131. ;; Inlining a result from find-definition effectively copies it,
  1132. ;; relying on the let-pruning to remove its original binding. We
  1133. ;; shouldn't copy non-constant expressions.
  1134. (or (not speculative?) (constant-expression? x)))
  1135. (match tail*
  1136. (($ <const> _ (args* ...))
  1137. (let ((args* (map (cut make-const #f <>) args*)))
  1138. (for-tail (make-call src proc (append args args*)))))
  1139. (($ <primcall> _ 'cons
  1140. ((and head (? copyable?)) (and tail (? copyable?))))
  1141. (for-tail (make-primcall src 'apply
  1142. (cons proc
  1143. (append args (list head tail))))))
  1144. (($ <primcall> _ 'list
  1145. (and args* ((? copyable?) ...)))
  1146. (for-tail (make-call src proc (append args args*))))
  1147. (tail*
  1148. (if speculative?
  1149. (lp (for-value tail) #f)
  1150. (let ((args (append (map for-value args) (list tail*))))
  1151. (make-primcall src 'apply
  1152. (cons (for-value proc) args))))))))
  1153. (($ <primcall> src (? constructor-primitive? name) args)
  1154. (cond
  1155. ((and (memq ctx '(effect test))
  1156. (match (cons name args)
  1157. ((or ('cons _ _)
  1158. ('list . _)
  1159. ('vector . _)
  1160. ('make-prompt-tag)
  1161. ('make-prompt-tag ($ <const> _ (? string?))))
  1162. #t)
  1163. (_ #f)))
  1164. ;; Some expressions can be folded without visiting the
  1165. ;; arguments for value.
  1166. (let ((res (if (eq? ctx 'effect)
  1167. (make-void #f)
  1168. (make-const #f #t))))
  1169. (for-tail (list->seq src (append args (list res))))))
  1170. (else
  1171. (match (cons name (map for-value args))
  1172. (('cons x ($ <const> _ (? (cut eq? <> '()))))
  1173. (make-primcall src 'list (list x)))
  1174. (('cons x ($ <primcall> _ 'list elts))
  1175. (make-primcall src 'list (cons x elts)))
  1176. ((name . args)
  1177. (make-primcall src name args))))))
  1178. (($ <primcall> src 'thunk? (proc))
  1179. (case ctx
  1180. ((effect)
  1181. (for-tail (make-seq src proc (make-void src))))
  1182. (else
  1183. (match (for-value proc)
  1184. (($ <lambda> _ _ ($ <lambda-case> _ req))
  1185. (for-tail (make-const src (null? req))))
  1186. (proc
  1187. (match (find-definition proc 2)
  1188. (($ <lambda> _ _ ($ <lambda-case> _ req))
  1189. (for-tail (make-const src (null? req))))
  1190. (_
  1191. (make-primcall src 'thunk? (list proc)))))))))
  1192. (($ <primcall> src name args)
  1193. (match (cons name (map for-value args))
  1194. ;; FIXME: these for-tail recursions could take place outside
  1195. ;; an effort counter.
  1196. (('car ($ <primcall> src 'cons (head tail)))
  1197. (for-tail (make-seq src tail head)))
  1198. (('cdr ($ <primcall> src 'cons (head tail)))
  1199. (for-tail (make-seq src head tail)))
  1200. (('car ($ <primcall> src 'list (head . tail)))
  1201. (for-tail (list->seq src (append tail (list head)))))
  1202. (('cdr ($ <primcall> src 'list (head . tail)))
  1203. (for-tail (make-seq src head (make-primcall #f 'list tail))))
  1204. (('car ($ <const> src (head . tail)))
  1205. (for-tail (make-const src head)))
  1206. (('cdr ($ <const> src (head . tail)))
  1207. (for-tail (make-const src tail)))
  1208. (((or 'memq 'memv) k ($ <const> _ (elts ...)))
  1209. ;; FIXME: factor
  1210. (case ctx
  1211. ((effect)
  1212. (for-tail
  1213. (make-seq src k (make-void #f))))
  1214. ((test)
  1215. (cond
  1216. ((const? k)
  1217. ;; A shortcut. The `else' case would handle it, but
  1218. ;; this way is faster.
  1219. (let ((member (case name ((memq) memq) ((memv) memv))))
  1220. (make-const #f (and (member (const-exp k) elts) #t))))
  1221. ((null? elts)
  1222. (for-tail
  1223. (make-seq src k (make-const #f #f))))
  1224. (else
  1225. (let ((t (gensym "t "))
  1226. (eq (if (eq? name 'memq) 'eq? 'eqv?)))
  1227. (record-new-temporary! 't t (length elts))
  1228. (for-tail
  1229. (make-let
  1230. src (list 't) (list t) (list k)
  1231. (let lp ((elts elts))
  1232. (define test
  1233. (make-primcall #f eq
  1234. (list (make-lexical-ref #f 't t)
  1235. (make-const #f (car elts)))))
  1236. (if (null? (cdr elts))
  1237. test
  1238. (make-conditional src test
  1239. (make-const #f #t)
  1240. (lp (cdr elts)))))))))))
  1241. (else
  1242. (cond
  1243. ((const? k)
  1244. (let ((member (case name ((memq) memq) ((memv) memv))))
  1245. (make-const #f (member (const-exp k) elts))))
  1246. ((null? elts)
  1247. (for-tail (make-seq src k (make-const #f #f))))
  1248. (else
  1249. (make-primcall src name (list k (make-const #f elts))))))))
  1250. (((? equality-primitive?)
  1251. ($ <lexical-ref> _ _ sym) ($ <lexical-ref> _ _ sym))
  1252. (for-tail (make-const #f #t)))
  1253. (((? effect-free-primitive?) . args)
  1254. (fold-constants src name args ctx))
  1255. ((name . args)
  1256. (make-primcall src name args))))
  1257. (($ <call> src orig-proc orig-args)
  1258. ;; todo: augment the global env with specialized functions
  1259. (let revisit-proc ((proc (visit orig-proc 'operator)))
  1260. (match proc
  1261. (($ <primitive-ref> _ name)
  1262. (for-tail (make-primcall src name orig-args)))
  1263. (($ <lambda> _ _
  1264. ($ <lambda-case> _ req opt rest #f inits gensyms body #f))
  1265. ;; Simple case: no keyword arguments.
  1266. ;; todo: handle the more complex cases
  1267. (let* ((nargs (length orig-args))
  1268. (nreq (length req))
  1269. (opt (or opt '()))
  1270. (rest (if rest (list rest) '()))
  1271. (nopt (length opt))
  1272. (key (source-expression proc)))
  1273. (define (inlined-call)
  1274. (let ((req-vals (list-head orig-args nreq))
  1275. (opt-vals (let lp ((args (drop orig-args nreq))
  1276. (inits inits)
  1277. (out '()))
  1278. (match inits
  1279. (() (reverse out))
  1280. ((init . inits)
  1281. (match args
  1282. (()
  1283. (lp '() inits (cons init out)))
  1284. ((arg . args)
  1285. (lp args inits (cons arg out))))))))
  1286. (rest-vals (cond
  1287. ((> nargs (+ nreq nopt))
  1288. (list (make-primcall
  1289. #f 'list
  1290. (drop orig-args (+ nreq nopt)))))
  1291. (rest (list (make-const #f '())))
  1292. (else '()))))
  1293. (if (>= nargs (+ nreq nopt))
  1294. (make-let src
  1295. (append req opt rest)
  1296. gensyms
  1297. (append req-vals opt-vals rest-vals)
  1298. body)
  1299. ;; The required argument values are in the scope
  1300. ;; of the optional argument initializers.
  1301. (make-let src
  1302. (append req rest)
  1303. (append (list-head gensyms nreq)
  1304. (last-pair gensyms))
  1305. (append req-vals rest-vals)
  1306. (make-let src
  1307. opt
  1308. (list-head (drop gensyms nreq) nopt)
  1309. opt-vals
  1310. body)))))
  1311. (cond
  1312. ((or (< nargs nreq) (and (not rest) (> nargs (+ nreq nopt))))
  1313. ;; An error, or effecting arguments.
  1314. (make-call src (for-call orig-proc) (map for-value orig-args)))
  1315. ((or (and=> (find-counter key counter) counter-recursive?)
  1316. (lambda? orig-proc))
  1317. ;; A recursive call, or a lambda in the operator
  1318. ;; position of the source expression. Process again in
  1319. ;; tail context.
  1320. ;;
  1321. ;; In the recursive case, mark intervening counters as
  1322. ;; recursive, so we can handle a toplevel counter that
  1323. ;; recurses mutually with some other procedure.
  1324. ;; Otherwise, the next time we see the other procedure,
  1325. ;; the effort limit would be clamped to 100.
  1326. ;;
  1327. (let ((found (find-counter key counter)))
  1328. (if (and found (counter-recursive? found))
  1329. (let lp ((counter counter))
  1330. (if (not (eq? counter found))
  1331. (begin
  1332. (set-counter-recursive?! counter #t)
  1333. (lp (counter-prev counter)))))))
  1334. (log 'inline-recurse key)
  1335. (loop (inlined-call) env counter ctx))
  1336. (else
  1337. ;; An integration at the top-level, the first
  1338. ;; recursion of a recursive procedure, or a nested
  1339. ;; integration of a procedure that hasn't been seen
  1340. ;; yet.
  1341. (log 'inline-begin exp)
  1342. (let/ec k
  1343. (define (abort)
  1344. (log 'inline-abort exp)
  1345. (k (make-call src (for-call orig-proc)
  1346. (map for-value orig-args))))
  1347. (define new-counter
  1348. (cond
  1349. ;; These first two cases will transfer effort
  1350. ;; from the current counter into the new
  1351. ;; counter.
  1352. ((find-counter key counter)
  1353. => (lambda (prev)
  1354. (make-recursive-counter recursive-effort-limit
  1355. operand-size-limit
  1356. prev counter)))
  1357. (counter
  1358. (make-nested-counter abort key counter))
  1359. ;; This case opens a new account, effectively
  1360. ;; printing money. It should only do so once
  1361. ;; for each call site in the source program.
  1362. (else
  1363. (make-top-counter effort-limit operand-size-limit
  1364. abort key))))
  1365. (define result
  1366. (loop (inlined-call) env new-counter ctx))
  1367. (if counter
  1368. ;; The nested inlining attempt succeeded.
  1369. ;; Deposit the unspent effort and size back
  1370. ;; into the current counter.
  1371. (transfer! new-counter counter))
  1372. (log 'inline-end result exp)
  1373. result)))))
  1374. (($ <let> _ _ _ vals _)
  1375. ;; Attempt to inline `let' in the operator position.
  1376. ;;
  1377. ;; We have to re-visit the proc in value mode, since the
  1378. ;; `let' bindings might have been introduced or renamed,
  1379. ;; whereas the lambda (if any) in operator position has not
  1380. ;; been renamed.
  1381. (if (or (and-map constant-expression? vals)
  1382. (and-map constant-expression? orig-args))
  1383. ;; The arguments and the let-bound values commute.
  1384. (match (for-value orig-proc)
  1385. (($ <let> lsrc names syms vals body)
  1386. (log 'inline-let orig-proc)
  1387. (for-tail
  1388. (make-let lsrc names syms vals
  1389. (make-call src body orig-args))))
  1390. ;; It's possible for a `let' to go away after the
  1391. ;; visit due to the fact that visiting a procedure in
  1392. ;; value context will prune unused bindings, whereas
  1393. ;; visiting in operator mode can't because it doesn't
  1394. ;; traverse through lambdas. In that case re-visit
  1395. ;; the procedure.
  1396. (proc (revisit-proc proc)))
  1397. (make-call src (for-call orig-proc)
  1398. (map for-value orig-args))))
  1399. (_
  1400. (make-call src (for-call orig-proc) (map for-value orig-args))))))
  1401. (($ <lambda> src meta body)
  1402. (case ctx
  1403. ((effect) (make-void #f))
  1404. ((test) (make-const #f #t))
  1405. ((operator) exp)
  1406. (else (record-source-expression!
  1407. exp
  1408. (make-lambda src meta (and body (for-values body)))))))
  1409. (($ <lambda-case> src req opt rest kw inits gensyms body alt)
  1410. (define (lift-applied-lambda body gensyms)
  1411. (and (not opt) rest (not kw)
  1412. (match body
  1413. (($ <primcall> _ 'apply
  1414. (($ <lambda> _ _ (and lcase ($ <lambda-case>)))
  1415. ($ <lexical-ref> _ _ sym)
  1416. ...))
  1417. (and (equal? sym gensyms)
  1418. (not (lambda-case-alternate lcase))
  1419. lcase))
  1420. (_ #f))))
  1421. (let* ((vars (map lookup-var gensyms))
  1422. (new (fresh-gensyms vars))
  1423. (env (fold extend-env env gensyms
  1424. (make-unbound-operands vars new)))
  1425. (new-sym (lambda (old)
  1426. (operand-sym (cdr (vhash-assq old env)))))
  1427. (body (loop body env counter ctx)))
  1428. (or
  1429. ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
  1430. (lift-applied-lambda body new)
  1431. (make-lambda-case src req opt rest
  1432. (match kw
  1433. ((aok? (kw name old) ...)
  1434. (cons aok? (map list kw name (map new-sym old))))
  1435. (_ #f))
  1436. (map (cut loop <> env counter 'value) inits)
  1437. new
  1438. body
  1439. (and alt (for-tail alt))))))
  1440. (($ <seq> src head tail)
  1441. (let ((head (for-effect head))
  1442. (tail (for-tail tail)))
  1443. (if (void? head)
  1444. tail
  1445. (make-seq src
  1446. (if (and (seq? head)
  1447. (void? (seq-tail head)))
  1448. (seq-head head)
  1449. head)
  1450. tail))))
  1451. (($ <prompt> src escape-only? tag body handler)
  1452. (define (make-prompt-tag? x)
  1453. (match x
  1454. (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
  1455. #t)
  1456. (_ #f)))
  1457. (let ((tag (for-value tag))
  1458. (body (if escape-only? (for-tail body) (for-value body))))
  1459. (cond
  1460. ((find-definition tag 1)
  1461. (lambda (val op)
  1462. (make-prompt-tag? val))
  1463. => (lambda (val op)
  1464. ;; There is no way that an <abort> could know the tag
  1465. ;; for this <prompt>, so we can elide the <prompt>
  1466. ;; entirely.
  1467. (unrecord-operand-uses op 1)
  1468. (for-tail (if escape-only? body (make-call src body '())))))
  1469. (else
  1470. (let ((handler (for-value handler)))
  1471. (define (escape-only-handler? handler)
  1472. (match handler
  1473. (($ <lambda> _ _
  1474. ($ <lambda-case> _ (_ . _) _ _ _ _ (k . _) body #f))
  1475. (not (tree-il-any
  1476. (match-lambda
  1477. (($ <lexical-ref> _ _ (? (cut eq? <> k))) #t)
  1478. (_ #f))
  1479. body)))
  1480. (else #f)))
  1481. (if (and (not escape-only?) (escape-only-handler? handler))
  1482. ;; Prompt transitioning to escape-only; transition body
  1483. ;; to be an expression.
  1484. (for-tail
  1485. (make-prompt src #t tag (make-call #f body '()) handler))
  1486. (make-prompt src escape-only? tag body handler)))))))
  1487. (($ <abort> src tag args tail)
  1488. (make-abort src (for-value tag) (map for-value args)
  1489. (for-value tail))))))