slot-allocation.scm 40 KB

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