slot-allocation.scm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. ;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013-2021,2023 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. ;;; Commentary:
  17. ;;;
  18. ;;; A module to assign stack slots to variables in a CPS term.
  19. ;;;
  20. ;;; Code:
  21. (define-module (language cps slot-allocation)
  22. #:use-module (ice-9 control)
  23. #:use-module (ice-9 match)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-9)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (language cps)
  29. #:use-module (language cps graphs)
  30. #:use-module (language cps utils)
  31. #:use-module (language cps intmap)
  32. #:use-module (language cps intset)
  33. #:export (allocate-slots
  34. lookup-slot
  35. lookup-maybe-slot
  36. lookup-representation
  37. lookup-nlocals
  38. lookup-call-proc-slot
  39. lookup-send-parallel-moves
  40. lookup-receive-parallel-moves
  41. lookup-slot-map))
  42. (define-record-type $allocation
  43. (make-allocation slots representations call-allocs shuffles frame-size)
  44. allocation?
  45. ;; A map of VAR to slot allocation. A slot allocation is an integer,
  46. ;; if the variable has been assigned a slot.
  47. ;;
  48. (slots allocation-slots)
  49. ;; A map of VAR to representation. A representation is 'scm, 'f64,
  50. ;; 'u64, 's64, 'ptr, 'bv-contents, or 'code.
  51. ;;
  52. (representations allocation-representations)
  53. ;; A map of LABEL to /call allocs/, for non-tail $call/$callk, and for
  54. ;; $prompt.
  55. ;;
  56. ;; A call alloc contains two pieces of information: the call's /proc
  57. ;; slot/ and a /dead slot map/. The proc slot indicates the slot of a
  58. ;; procedure in a procedure call, or where the procedure would be in a
  59. ;; multiple-value return.
  60. ;;
  61. ;; The dead slot map indicates, what slots should be ignored by GC
  62. ;; when marking the frame. A dead slot map is a bitfield, as an
  63. ;; integer.
  64. ;;
  65. (call-allocs allocation-call-allocs)
  66. ;; A map of LABEL to /parallel moves/. Parallel moves shuffle locals
  67. ;; into position for a $call, $callk, $calli, or $values, or shuffle
  68. ;; returned values back into place for a $kreceive.
  69. ;;
  70. ;; A set of moves is expressed as an ordered list of (SRC . DST)
  71. ;; moves, where SRC and DST are slots. This may involve a temporary
  72. ;; variable.
  73. ;;
  74. (shuffles allocation-shuffles)
  75. ;; The number of local slots needed for this function. Because we can
  76. ;; contify common clause tails, we use one frame size for all clauses
  77. ;; to avoid having to adjust the frame size when continuing to labels
  78. ;; from other clauses.
  79. ;;
  80. (frame-size allocation-frame-size))
  81. (define-record-type $call-alloc
  82. (make-call-alloc proc-slot slot-map)
  83. call-alloc?
  84. (proc-slot call-alloc-proc-slot)
  85. (slot-map call-alloc-slot-map))
  86. (define (lookup-maybe-slot var allocation)
  87. (intmap-ref (allocation-slots allocation) var (lambda (_) #f)))
  88. (define (lookup-slot var allocation)
  89. (intmap-ref (allocation-slots allocation) var))
  90. (define (lookup-representation var allocation)
  91. (intmap-ref (allocation-representations allocation) var))
  92. (define *absent* (list 'absent))
  93. (define (lookup-call-alloc k allocation)
  94. (intmap-ref (allocation-call-allocs allocation) k))
  95. (define (lookup-call-proc-slot k allocation)
  96. (or (call-alloc-proc-slot (lookup-call-alloc k allocation))
  97. (error "Call has no proc slot" k)))
  98. (define (lookup-send-parallel-moves k allocation)
  99. (match (intmap-ref (allocation-shuffles allocation) k)
  100. ((send . receive) send)))
  101. (define (lookup-receive-parallel-moves k allocation)
  102. (match (intmap-ref (allocation-shuffles allocation) k)
  103. ((send . receive) receive)))
  104. (define (lookup-slot-map k allocation)
  105. (or (call-alloc-slot-map (lookup-call-alloc k allocation))
  106. (error "Call has no slot map" k)))
  107. (define (lookup-nlocals allocation)
  108. (allocation-frame-size allocation))
  109. (define* (add-prompt-control-flow-edges conts succs #:key complete?)
  110. "For all prompts in DFG in the range [MIN-LABEL, MIN-LABEL +
  111. LABEL-COUNT), invoke F with arguments PROMPT, HANDLER, and BODY for each
  112. body continuation in the prompt."
  113. (define (intset-filter pred set)
  114. (intset-fold (lambda (i set)
  115. (if (pred i) set (intset-remove set i)))
  116. set
  117. set))
  118. (define (intset-any pred set)
  119. (intset-fold (lambda (i res)
  120. (if (or res (pred i)) #t res))
  121. set
  122. #f))
  123. (define (compute-prompt-body label)
  124. (persistent-intset
  125. (let visit-cont ((label label) (level 1) (labels empty-intset))
  126. (cond
  127. ((zero? level) labels)
  128. ((intset-ref labels label) labels)
  129. (else
  130. (let ((labels (intset-add! labels label)))
  131. (match (intmap-ref conts label)
  132. (($ $kreceive arity k) (visit-cont k level labels))
  133. (($ $kargs names syms ($ $continue k src ($ $primcall 'wind)))
  134. (visit-cont k (1+ level) labels))
  135. (($ $kargs names syms ($ $continue k src ($ $primcall 'unwind)))
  136. (visit-cont k (1- level) labels))
  137. (($ $kargs names syms ($ $continue k src exp))
  138. (visit-cont k level labels))
  139. (($ $kargs names syms ($ $branch kf kt))
  140. (visit-cont kf level (visit-cont kt level labels)))
  141. (($ $kargs names syms ($ $switch kf kt*))
  142. (fold1 (lambda (label labels)
  143. (visit-cont label level labels))
  144. (cons kf kt*) labels))
  145. (($ $kargs names syms ($ $prompt k kh src escape? tag))
  146. (visit-cont kh level (visit-cont k (1+ level) labels)))
  147. (($ $kargs names syms ($ $throw)) labels))))))))
  148. (define (visit-prompt label handler succs)
  149. (let ((body (compute-prompt-body label)))
  150. (define (out-or-back-edge? label)
  151. ;; Most uses of visit-prompt-control-flow don't need every body
  152. ;; continuation, and would be happy getting called only for
  153. ;; continuations that postdominate the rest of the body. Unless
  154. ;; you pass #:complete? #t, we only invoke F on continuations
  155. ;; that can leave the body, or on back-edges in loops.
  156. (not (intset-any (lambda (succ)
  157. (and (intset-ref body succ) (< label succ)))
  158. (intmap-ref succs label))))
  159. (intset-fold (lambda (pred succs)
  160. (intmap-replace succs pred handler intset-add))
  161. (if complete? body (intset-filter out-or-back-edge? body))
  162. succs)))
  163. (intmap-fold
  164. (lambda (label cont succs)
  165. (match cont
  166. (($ $kargs _ _ ($ $prompt k kh))
  167. (visit-prompt k kh succs))
  168. (_ succs)))
  169. conts
  170. succs))
  171. (define (compute-needs-slot cps defs uses)
  172. (define (get-defs k) (intmap-ref defs k))
  173. (define (get-uses label) (intmap-ref uses label))
  174. (intmap-fold
  175. (lambda (label cont needs-slot)
  176. (intset-union
  177. needs-slot
  178. (match cont
  179. (($ $kargs)
  180. (intset-union (get-defs label) (get-uses label)))
  181. (($ $kreceive arity k)
  182. ;; Only allocate results of function calls to slots if they are
  183. ;; used.
  184. empty-intset)
  185. (($ $kclause arity body alternate)
  186. (get-defs label))
  187. (($ $kfun src meta self)
  188. (get-defs label))
  189. (($ $ktail)
  190. empty-intset))))
  191. cps
  192. empty-intset))
  193. (define (compute-lazy-vars cps live-in live-out defs needs-slot)
  194. "Compute and return a set of vars whose allocation can be delayed
  195. until their use is seen. These are \"lazy\" vars. A var is lazy if its
  196. uses are calls, it is always dead after the calls, and if the uses flow
  197. to the definition. A flow continues across a node iff the node kills no
  198. values that need slots, and defines only lazy vars. Calls also kill
  199. flows; there's no sense in trying to juggle a pending frame while there
  200. is an active call."
  201. (define (list->intset list)
  202. (persistent-intset
  203. (fold (lambda (i set) (intset-add! set i)) empty-intset list)))
  204. (let* ((succs (compute-successors cps))
  205. (gens (intmap-map
  206. (lambda (label cont)
  207. (match cont
  208. (($ $kargs _ _ ($ $continue _ _ ($ $call proc args)))
  209. (intset-subtract (intset-add (list->intset args) proc)
  210. (intmap-ref live-out label)))
  211. (($ $kargs _ _ ($ $continue _ _ ($ $callk _ proc args)))
  212. (let ((args (list->intset args)))
  213. (intset-subtract (if proc (intset-add args proc) args)
  214. (intmap-ref live-out label))))
  215. (($ $kargs _ _ ($ $continue _ _ ($ $calli args callee)))
  216. (intset-subtract (list->intset (cons callee args))
  217. (intmap-ref live-out label)))
  218. (($ $kargs _ _ ($ $continue k _($ $values args)))
  219. (match (intmap-ref cps k)
  220. (($ $ktail) (list->intset args))
  221. (_ #f)))
  222. (_ #f)))
  223. cps))
  224. (kills (intmap-map
  225. (lambda (label in)
  226. (let* ((out (intmap-ref live-out label))
  227. (killed (intset-subtract in out))
  228. (killed-slots (intset-intersect killed needs-slot)))
  229. (and (eq? killed-slots empty-intset)
  230. ;; Kill output variables that need slots.
  231. (intset-intersect (intmap-ref defs label)
  232. needs-slot))))
  233. live-in))
  234. (preds (invert-graph succs))
  235. (old->new (compute-reverse-control-flow-order preds)))
  236. (define (subtract lazy kill)
  237. (cond
  238. ((eq? lazy empty-intset)
  239. lazy)
  240. ((not kill)
  241. empty-intset)
  242. ((and lazy (eq? empty-intset (intset-subtract kill lazy)))
  243. (intset-subtract lazy kill))
  244. (else
  245. empty-intset)))
  246. (define (add live gen) (or gen live))
  247. (define (meet in out)
  248. ;; Initial in is #f.
  249. (if in (intset-intersect in out) out))
  250. (call-with-values
  251. (lambda ()
  252. (let ((succs (rename-graph preds old->new))
  253. (init (persistent-intmap
  254. (intmap-fold
  255. (lambda (old new in)
  256. (intmap-add! in new #f))
  257. old->new empty-intmap)))
  258. (kills (rename-keys kills old->new))
  259. (gens (rename-keys gens old->new)))
  260. (solve-flow-equations succs init init kills gens
  261. subtract add meet)))
  262. (lambda (in out)
  263. ;; A variable is lazy if its uses reach its definition.
  264. (intmap-fold (lambda (label out lazy)
  265. (match (intmap-ref cps label)
  266. (($ $kargs names vars)
  267. (let ((defs (list->intset vars)))
  268. (intset-union lazy (intset-intersect out defs))))
  269. (_ lazy)))
  270. (rename-keys out (invert-bijection old->new))
  271. empty-intset)))))
  272. (define (find-first-zero n)
  273. ;; Naive implementation.
  274. (let lp ((slot 0))
  275. (if (logbit? slot n)
  276. (lp (1+ slot))
  277. slot)))
  278. (define (find-first-trailing-zero n)
  279. (let lp ((slot (let lp ((count 2))
  280. (if (< n (ash 1 (1- count)))
  281. count
  282. ;; Grow upper bound slower than factor 2 to avoid
  283. ;; needless bignum allocation on 32-bit systems
  284. ;; when there are more than 16 locals.
  285. (lp (+ count (ash count -1)))))))
  286. (if (or (zero? slot) (logbit? (1- slot) n))
  287. slot
  288. (lp (1- slot)))))
  289. (define (integers from count)
  290. (if (zero? count)
  291. '()
  292. (cons from (integers (1+ from) (1- count)))))
  293. (define (solve-parallel-move src dst tmp)
  294. "Solve the parallel move problem between src and dst slot lists, which
  295. are comparable with eqv?. A tmp slot may be used."
  296. ;; This algorithm is taken from: "Tilting at windmills with Coq:
  297. ;; formal verification of a compilation algorithm for parallel moves"
  298. ;; by Laurence Rideau, Bernard Paul Serpette, and Xavier Leroy
  299. ;; <http://gallium.inria.fr/~xleroy/publi/parallel-move.pdf>
  300. (define (split-move moves reg)
  301. (let loop ((revhead '()) (tail moves))
  302. (match tail
  303. (((and s+d (s . d)) . rest)
  304. (if (eqv? s reg)
  305. (cons d (append-reverse revhead rest))
  306. (loop (cons s+d revhead) rest)))
  307. (_ #f))))
  308. (define (replace-last-source reg moves)
  309. (match moves
  310. ((moves ... (s . d))
  311. (append moves (list (cons reg d))))))
  312. (let loop ((to-move (map cons src dst))
  313. (being-moved '())
  314. (moved '())
  315. (last-source #f))
  316. ;; 'last-source' should always be equivalent to:
  317. ;; (and (pair? being-moved) (car (last being-moved)))
  318. (match being-moved
  319. (() (match to-move
  320. (() (reverse moved))
  321. (((and s+d (s . d)) . t1)
  322. (if (or (eqv? s d) ; idempotent
  323. (not s)) ; src is a constant and can be loaded directly
  324. (loop t1 '() moved #f)
  325. (loop t1 (list s+d) moved s)))))
  326. (((and s+d (s . d)) . b)
  327. (match (split-move to-move d)
  328. ((r . t1) (loop t1 (acons d r being-moved) moved last-source))
  329. (#f (match b
  330. (() (loop to-move '() (cons s+d moved) #f))
  331. (_ (if (eqv? d last-source)
  332. (loop to-move
  333. (replace-last-source tmp b)
  334. (cons s+d (acons d tmp moved))
  335. tmp)
  336. (loop to-move b (cons s+d moved) last-source))))))))))
  337. (define-inlinable (add-live-slot slot live-slots)
  338. (logior live-slots (ash 1 slot)))
  339. (define-inlinable (kill-dead-slot slot live-slots)
  340. (logand live-slots (lognot (ash 1 slot))))
  341. (define-inlinable (compute-slot live-slots hint)
  342. (if (and hint (not (logbit? hint live-slots)))
  343. hint
  344. (find-first-zero live-slots)))
  345. (define (compute-shuffles cps slots call-allocs live-in)
  346. (define (get-cont label)
  347. (intmap-ref cps label))
  348. (define (get-slot var)
  349. (intmap-ref slots var (lambda (_) #f)))
  350. (define (get-slots vars)
  351. (let lp ((vars vars))
  352. (match vars
  353. ((var . vars) (cons (get-slot var) (lp vars)))
  354. (_ '()))))
  355. (define (get-proc-slot label)
  356. (call-alloc-proc-slot (intmap-ref call-allocs label)))
  357. (define (compute-live-slots label)
  358. (intset-fold (lambda (var live)
  359. (match (get-slot var)
  360. (#f live)
  361. (slot (add-live-slot slot live))))
  362. (intmap-ref live-in label)
  363. 0))
  364. ;; Although some parallel moves may proceed without a temporary slot,
  365. ;; in general one is needed. That temporary slot must not be part of
  366. ;; the source or destination sets, and that slot should not correspond
  367. ;; to a live variable. Usually the source and destination sets are a
  368. ;; subset of the union of the live sets before and after the move.
  369. ;; However for stack slots that don't have names -- those slots that
  370. ;; correspond to function arguments or to function return values -- it
  371. ;; could be that they are out of the computed live set. In that case
  372. ;; they need to be adjoined to the live set, used when choosing a
  373. ;; temporary slot.
  374. (define (compute-tmp-slot live stack-slots)
  375. (find-first-zero (fold add-live-slot live stack-slots)))
  376. (define (parallel-move src-slots dst-slots tmp-slot)
  377. (solve-parallel-move src-slots dst-slots tmp-slot))
  378. ;; A term can have two sets of shuffles: one set to shuffle operands
  379. ;; to the term (the "send moves"), and one set to shuffle results (the
  380. ;; "receive moves"). An example of send moves would be a call getting
  381. ;; its arguments into position, or a $values performing a parallel
  382. ;; move. Receive moves come when binding call results to values, for
  383. ;; local returns (call returns) or non-local returns (prompt
  384. ;; handlers).
  385. (define (add-shuffles shuffles label send-moves receive-moves)
  386. (intmap-add! shuffles label (cons send-moves receive-moves)))
  387. (define (compute-receive-shuffles k proc-slot)
  388. (match (get-cont k)
  389. (($ $kreceive arity kargs)
  390. (compute-receive-shuffles kargs proc-slot))
  391. (($ $kargs names results)
  392. (let* ((value-slots (integers proc-slot (length results)))
  393. (result-slots (get-slots results))
  394. ;; Filter out unused results.
  395. (value-slots (filter-map (lambda (val result) (and result val))
  396. value-slots result-slots))
  397. (result-slots (filter (lambda (x) x) result-slots))
  398. (live (compute-live-slots k)))
  399. (parallel-move value-slots
  400. result-slots
  401. (compute-tmp-slot live value-slots))))))
  402. (define (add-call-shuffles label k args shuffles)
  403. (match (get-cont k)
  404. (($ $ktail)
  405. (let* ((live (compute-live-slots label))
  406. (tail-slots (integers 0 (length args)))
  407. (send-moves (parallel-move (get-slots args)
  408. tail-slots
  409. (compute-tmp-slot live tail-slots))))
  410. (add-shuffles shuffles label send-moves '())))
  411. ((or ($ $kargs) ($ $kreceive))
  412. (let* ((live (compute-live-slots label))
  413. (proc-slot (get-proc-slot label))
  414. (call-slots (integers proc-slot (length args)))
  415. (send-moves (parallel-move (get-slots args)
  416. call-slots
  417. (compute-tmp-slot live call-slots)))
  418. (receive-moves (compute-receive-shuffles k proc-slot)))
  419. (add-shuffles shuffles label send-moves receive-moves)))))
  420. (define (add-values-shuffles label k args shuffles)
  421. (match (get-cont k)
  422. (($ $ktail)
  423. (let* ((live (compute-live-slots label))
  424. (src-slots (get-slots args))
  425. (dst-slots (integers 0 (length args)))
  426. (send-moves (parallel-move src-slots dst-slots
  427. (compute-tmp-slot live dst-slots))))
  428. (add-shuffles shuffles label send-moves '())))
  429. (($ $kargs _ dst-vars)
  430. (let* ((live (logior (compute-live-slots label)
  431. (compute-live-slots k)))
  432. (src-slots (get-slots args))
  433. (dst-slots (get-slots dst-vars))
  434. (send-moves (parallel-move src-slots dst-slots
  435. (compute-tmp-slot live '()))))
  436. (add-shuffles shuffles label send-moves '())))))
  437. (define (add-prompt-shuffles label k handler shuffles)
  438. (define receive-moves
  439. (compute-receive-shuffles handler (get-proc-slot label)))
  440. (add-shuffles shuffles label '() receive-moves))
  441. (define (compute-shuffles label cont shuffles)
  442. (match cont
  443. (($ $kargs names vars ($ $continue k src exp))
  444. (match exp
  445. (($ $call proc args)
  446. (add-call-shuffles label k (cons proc args) shuffles))
  447. (($ $callk _ proc args)
  448. (add-call-shuffles label k (if proc (cons proc args) args) shuffles))
  449. (($ $calli args callee)
  450. (add-call-shuffles label k (append args (list callee)) shuffles))
  451. (($ $values args)
  452. (add-values-shuffles label k args shuffles))
  453. (_ shuffles)))
  454. (($ $kargs names vars ($ $prompt k kh src escape? tag))
  455. (add-prompt-shuffles label k kh shuffles))
  456. (_ shuffles)))
  457. (persistent-intmap
  458. (intmap-fold compute-shuffles cps empty-intmap)))
  459. (define (compute-frame-size cps slots call-allocs shuffles)
  460. ;; Minimum frame has one slot: the closure.
  461. (define minimum-frame-size 1)
  462. (define (get-shuffles label)
  463. (intmap-ref shuffles label))
  464. (define (get-proc-slot label)
  465. (match (intmap-ref call-allocs label (lambda (_) #f))
  466. (#f 0) ;; Tail call.
  467. (($ $call-alloc proc-slot) proc-slot)))
  468. (define (max-size var size)
  469. (match (intmap-ref slots var (lambda (_) #f))
  470. (#f size)
  471. (slot (max size (1+ slot)))))
  472. (define (max-size* vars size)
  473. (fold max-size size vars))
  474. (define (shuffle-size* moves size)
  475. (match moves
  476. (() size)
  477. (((src . dst) . moves)
  478. (shuffle-size* moves (max size (1+ src) (1+ dst))))))
  479. (define (shuffle-size send+receive size)
  480. (match send+receive
  481. ((send . receive) (shuffle-size* send (shuffle-size* receive size)))))
  482. (define (call-size label nargs size)
  483. (shuffle-size (get-shuffles label)
  484. (max (+ (get-proc-slot label) nargs) size)))
  485. (define (measure-cont label cont size)
  486. (match cont
  487. (($ $kargs names vars term)
  488. (let ((size (max-size* vars size)))
  489. (match term
  490. (($ $continue _ _ ($ $call proc args))
  491. (call-size label (1+ (length args)) size))
  492. (($ $continue _ _ ($ $callk _ proc args))
  493. (let ((nclosure (if proc 1 0)))
  494. (call-size label (+ nclosure (length args)) size)))
  495. (($ $continue _ _ ($ $calli args callee))
  496. (call-size label (1+ (length args)) size))
  497. (($ $continue _ _ ($ $values args))
  498. (shuffle-size (get-shuffles label) size))
  499. (($ $prompt)
  500. (shuffle-size (get-shuffles label) size))
  501. (_ size))))
  502. (_ size)))
  503. (intmap-fold measure-cont cps minimum-frame-size))
  504. (define (allocate-args cps)
  505. (define (add-clause entry first-slot slots)
  506. (match (intmap-ref cps entry)
  507. (($ $kclause arity body alt)
  508. (let ((slots (add-clause body first-slot slots)))
  509. (if alt
  510. (add-clause alt first-slot slots)
  511. slots)))
  512. (($ $kargs names vars)
  513. (let lp ((vars vars) (n first-slot) (slots slots))
  514. (match vars
  515. (() slots)
  516. ((var . vars)
  517. (lp vars
  518. (1+ n)
  519. (intmap-add slots var n))))))))
  520. (match (intmap-ref cps (intmap-next cps))
  521. (($ $kfun src meta self tail entry)
  522. (add-clause
  523. entry
  524. (if self 1 0)
  525. (if self
  526. (intmap-add empty-intmap self 0)
  527. empty-intmap)))))
  528. (define (allocate-lazy-vars cps slots call-allocs live-in lazy)
  529. (define (compute-live-slots slots label)
  530. (intset-fold (lambda (var live)
  531. (match (intmap-ref slots var (lambda (_) #f))
  532. (#f live)
  533. (slot (add-live-slot slot live))))
  534. (intmap-ref live-in label)
  535. 0))
  536. (define (allocate var hint slots live)
  537. (match (and hint (intmap-ref slots var (lambda (_) #f)))
  538. (#f (if (intset-ref lazy var)
  539. (let ((slot (compute-slot live hint)))
  540. (values (intmap-add! slots var slot)
  541. (add-live-slot slot live)))
  542. (values slots live)))
  543. (slot (values slots (add-live-slot slot live)))))
  544. (define (allocate* vars hints slots live)
  545. (match (vector vars hints)
  546. (#(() ()) slots)
  547. (#((var . vars) (hint . hints))
  548. (let-values (((slots live) (allocate var hint slots live)))
  549. (allocate* vars hints slots live)))))
  550. (define (get-proc-slot label)
  551. (match (intmap-ref call-allocs label (lambda (_) #f))
  552. (#f 0)
  553. (call (call-alloc-proc-slot call))))
  554. (define (allocate-call label args slots)
  555. (allocate* args (integers (get-proc-slot label) (length args))
  556. slots (compute-live-slots slots label)))
  557. (define (allocate-values label k args slots)
  558. (match (intmap-ref cps k)
  559. (($ $ktail)
  560. (allocate* args (integers 0 (length args))
  561. slots (compute-live-slots slots label)))
  562. (($ $kargs names vars)
  563. (allocate* args
  564. (map (cut intmap-ref slots <> (lambda (_) #f)) vars)
  565. slots (compute-live-slots slots label)))))
  566. (define (allocate-lazy label cont slots)
  567. (match cont
  568. (($ $kargs names vars ($ $continue k src exp))
  569. (match exp
  570. (($ $call proc args)
  571. (allocate-call label (cons proc args) slots))
  572. (($ $callk _ proc args)
  573. (allocate-call label (if proc (cons proc args) args) slots))
  574. (($ $calli args callee)
  575. (allocate-call label (append args (list callee)) slots))
  576. (($ $values args)
  577. (allocate-values label k args slots))
  578. (_ slots)))
  579. (_
  580. slots)))
  581. ;; Sweep right to left to visit uses before definitions.
  582. (persistent-intmap
  583. (intmap-fold-right allocate-lazy cps slots)))
  584. (define* (allocate-slots cps #:key (precolor-calls? #t))
  585. (let*-values (((defs uses) (compute-defs-and-uses cps))
  586. ((representations) (compute-var-representations cps))
  587. ((live-in live-out)
  588. (let* ((succs (compute-successors cps))
  589. (succs+ (add-prompt-control-flow-edges cps succs))
  590. (preds (invert-graph succs+)))
  591. (compute-live-variables preds defs uses)))
  592. ((needs-slot) (compute-needs-slot cps defs uses))
  593. ((lazy) (if precolor-calls?
  594. (compute-lazy-vars cps live-in live-out defs
  595. needs-slot)
  596. empty-intset)))
  597. (define frame-size 3)
  598. (define (empty-live-slots)
  599. #b0)
  600. (define (compute-call-proc-slot live-slots)
  601. (+ frame-size (find-first-trailing-zero live-slots)))
  602. (define (compute-prompt-handler-proc-slot live-slots)
  603. (find-first-trailing-zero live-slots))
  604. (define (get-cont label)
  605. (intmap-ref cps label))
  606. (define (get-slot slots var)
  607. (intmap-ref slots var (lambda (_) #f)))
  608. (define (get-slots slots vars)
  609. (let lp ((vars vars))
  610. (match vars
  611. ((var . vars) (cons (get-slot slots var) (lp vars)))
  612. (_ '()))))
  613. (define (compute-live-slots* slots label live-vars)
  614. (intset-fold (lambda (var live)
  615. (match (get-slot slots var)
  616. (#f live)
  617. (slot (add-live-slot slot live))))
  618. (intmap-ref live-vars label)
  619. 0))
  620. (define (compute-live-in-slots slots label)
  621. (compute-live-slots* slots label live-in))
  622. (define (compute-live-out-slots slots label)
  623. (compute-live-slots* slots label live-out))
  624. (define slot-desc-dead 0)
  625. (define slot-desc-live-raw 1)
  626. (define slot-desc-live-scm 2)
  627. (define slot-desc-unused 3)
  628. (define (compute-slot-map slots live-vars nslots)
  629. (intset-fold
  630. (lambda (var slot-map)
  631. (match (get-slot slots var)
  632. (#f slot-map)
  633. (slot
  634. (let ((desc (match (intmap-ref representations var)
  635. ((or 'u64 'f64 's64 'ptr 'bv-contents 'code)
  636. slot-desc-live-raw)
  637. ('scm
  638. slot-desc-live-scm))))
  639. (logior slot-map (ash desc (* 2 slot)))))))
  640. live-vars 0))
  641. (define (allocate var hint slots live)
  642. (cond
  643. ((not (intset-ref needs-slot var))
  644. (values slots live))
  645. ((get-slot slots var)
  646. => (lambda (slot)
  647. (values slots (add-live-slot slot live))))
  648. ((and (not hint) (intset-ref lazy var))
  649. (values slots live))
  650. (else
  651. (let ((slot (compute-slot live hint)))
  652. (values (intmap-add! slots var slot)
  653. (add-live-slot slot live))))))
  654. (define (allocate* vars hints slots live)
  655. (match (vector vars hints)
  656. (#(() ()) (values slots live))
  657. (#((var . vars) (hint . hints))
  658. (call-with-values (lambda () (allocate var hint slots live))
  659. (lambda (slots live)
  660. (allocate* vars hints slots live))))))
  661. (define (allocate-defs label vars slots)
  662. (let ((live (compute-live-in-slots slots label))
  663. (live-vars (intmap-ref live-in label)))
  664. (let lp ((vars vars) (slots slots) (live live))
  665. (match vars
  666. (() (values slots live))
  667. ((var . vars)
  668. (call-with-values (lambda () (allocate var #f slots live))
  669. (lambda (slots live)
  670. (lp vars slots
  671. (let ((slot (get-slot slots var)))
  672. (if (and slot (not (intset-ref live-vars var)))
  673. (kill-dead-slot slot live)
  674. live))))))))))
  675. ;; PRE-LIVE are the live slots coming into the term. POST-LIVE
  676. ;; is the subset of PRE-LIVE that is still live after the term
  677. ;; uses its inputs.
  678. (define (allocate-call label k args slots call-allocs pre-live)
  679. (match (get-cont k)
  680. (($ $ktail)
  681. (let ((tail-slots (integers 0 (length args))))
  682. (values (allocate* args tail-slots slots pre-live)
  683. call-allocs)))
  684. (($ $kreceive arity kargs)
  685. (allocate-call label kargs args slots call-allocs pre-live))
  686. (($ $kargs names results)
  687. (let*-values
  688. (((post-live) (compute-live-out-slots slots label))
  689. ((proc-slot) (compute-call-proc-slot post-live))
  690. ((call-slots) (integers proc-slot (length args)))
  691. ((slots pre-live) (allocate* args call-slots slots pre-live))
  692. ;; Allow the first result to be hinted by its use, but
  693. ;; hint the remaining results to stay in place. This
  694. ;; strikes a balance between avoiding shuffling,
  695. ;; especially for unused extra values, and avoiding frame
  696. ;; size growth due to sparse locals.
  697. ((slots result-live)
  698. (match results
  699. (()
  700. (values slots post-live))
  701. ((_ . results*)
  702. (let ((result-slots (integers (+ proc-slot 1)
  703. (length results*))))
  704. (allocate* results* result-slots slots post-live)))))
  705. ((slot-map) (compute-slot-map slots (intmap-ref live-out label)
  706. (- proc-slot frame-size)))
  707. ((call) (make-call-alloc proc-slot slot-map)))
  708. (values slots
  709. (intmap-add! call-allocs label call))))))
  710. (define (allocate-values label k args slots call-allocs)
  711. (match (get-cont k)
  712. (($ $ktail)
  713. (values slots call-allocs))
  714. (($ $kargs (_) (dst))
  715. ;; When there is only one value in play, we allow the dst to be
  716. ;; hinted (see compute-lazy-vars). If the src doesn't have a
  717. ;; slot, then the actual slot for the dst would end up being
  718. ;; decided by the call that args it. Because we don't know the
  719. ;; slot, we can't really compute the parallel moves in that
  720. ;; case, so just bail and rely on the bytecode emitter to
  721. ;; handle the one-value case specially.
  722. (match args
  723. ((src)
  724. (let ((post-live (compute-live-out-slots slots label)))
  725. (values (allocate dst (get-slot slots src) slots post-live)
  726. call-allocs)))))
  727. (($ $kargs _ dst-vars)
  728. (let ((src-slots (get-slots slots args))
  729. (post-live (compute-live-out-slots slots label)))
  730. (values (allocate* dst-vars src-slots slots post-live)
  731. call-allocs)))))
  732. (define (allocate-prompt label k handler slots call-allocs)
  733. (match (get-cont handler)
  734. (($ $kreceive arity kargs)
  735. (let*-values
  736. (((handler-live) (compute-live-in-slots slots handler))
  737. ((proc-slot) (compute-prompt-handler-proc-slot handler-live))
  738. ((slot-map) (compute-slot-map slots (intmap-ref live-in handler)
  739. (- proc-slot frame-size)))
  740. ((result-vars) (match (get-cont kargs)
  741. (($ $kargs names vars) vars)))
  742. ((value-slots) (integers proc-slot (length result-vars)))
  743. ((slots result-live) (allocate* result-vars value-slots
  744. slots handler-live)))
  745. (values slots
  746. (intmap-add! call-allocs label
  747. (make-call-alloc proc-slot slot-map)))))))
  748. (define (allocate-cont label cont slots call-allocs)
  749. (match cont
  750. (($ $kargs names vars term)
  751. (let-values (((slots live) (allocate-defs label vars slots)))
  752. (match term
  753. (($ $continue k src ($ $call proc args))
  754. (allocate-call label k (cons proc args) slots call-allocs live))
  755. (($ $continue k src ($ $callk _ proc args))
  756. (allocate-call label k (if proc (cons proc args) args)
  757. slots call-allocs live))
  758. (($ $continue k src ($ $calli args callee))
  759. (allocate-call label k (append args (list callee))
  760. slots call-allocs live))
  761. (($ $continue k src ($ $values args))
  762. (allocate-values label k args slots call-allocs))
  763. (($ $prompt k kh src escape? tag)
  764. (allocate-prompt label k kh slots call-allocs))
  765. (_
  766. (values slots call-allocs)))))
  767. (_
  768. (values slots call-allocs))))
  769. (call-with-values (lambda ()
  770. (let ((slots (allocate-args cps)))
  771. (intmap-fold allocate-cont cps slots empty-intmap)))
  772. (lambda (slots calls)
  773. (let* ((slots (allocate-lazy-vars cps slots calls live-in lazy))
  774. (shuffles (compute-shuffles cps slots calls live-in))
  775. (frame-size (compute-frame-size cps slots calls shuffles)))
  776. (make-allocation slots representations calls shuffles frame-size))))))