peval.scm 67 KB

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