Th Mmory Hirrhy Avn Compilrs CMPSCI 710 Spring 2003 Highr = smllr, str, losr to CPU A rl sktop mhin (min) Rgistr Allotion Emry Brgr rgistrs 8 intgr, 8 loting-point; 1-yl ltny L1 h 8K t & instrutions; 2-yl ltny L2 h 512K; 7-yl ltny Univrsity o Msshustts, Amhrst Min mmory (stors & los) Disk (il I/O) Two things mintin this strtion: Hrwr Movs t twn mmory n hs Compilr Movs t twn mmory n rgistrs Cnit vlus: Vrils Tmporris Lrg onstnts Whn n, spill rgistrs to mmory Expliit nms Unlik ll othr lvls o hirrhy Sr Smll rgistr ils (st o ll rgistrs) Som rsrv y oprting systm Importnt low-lvl optimiztion Rgistrs r 2x 7x str thn h ¾ Juiious us ig prormn improvmnts 4 Rgistr Allotion: Complitions Rgistr llotion ssigns rgistrs to vlus 2 Dinition History Intrrn grphs Grph oloring Rgistr spilling 3 Rgistr Allotion: Dinition 40 GB; 38,000,000 yl ltny (!) Introution Rgistr Allotion Disk Ovrviw Progrmmr viw: only two lvls o mmory 1GB; 100 yl ltny Mnging th Mmory Hirrhy RAM 5.g., BP, SP Complit Wir onstrints, sp. on CISC rhitturs Spil rgistrs: zro-lo 6 1
History Rgistr Allotion Exmpl As ol s intrmit o Consir this progrm with six vrils: := + := + := - 1 Us in th originl FORTRAN ompilr (1950 s) Vry ru lgorithms with th ssumption tht n i tr us No rkthroughs until 1981! Chitin invnt rgistr llotion shm s on grph oloring ¾ Equivln irst not y Cok t l., 1971 r1 := r2 + r3 r1 := r1 + r4 r1 := r1-1 7 Bsi Rgistr Allotion I 8 Algorithm: Prt I Vlu in tmporry not n or rst o th omputtion Cn llot,, n ll to on rgistr (r1): Simpl huristi, works wll in prti Tmporry n rus tr := + Sm with tmporry Comput liv vrils or h point: {,,} {,,} D tmporry n rus {,,} := + := - := + {,,,} {,} Bsi rul: Tmporris t1 n t2 n shr sm rgistr i t ny point in th progrm t most on o t1 or t2 is liv! := + := 2 * {,} {,,,} := - 1 {,} {} := + {} Two tmporris liv simultnously ¾ I liv simultnously t som point in th progrm Two tmporris n llot to sm rgistr i no g onnts thm For our xmpl: {,,} {,,} {,,} {,,,} {,} {,,,} {,} {} No or h tmporry Unirt g twn t1 n t2 Cnnot llot in th sm rgistr Construt rgistr intrrn grph 10 Rgistr Intrrn Grph: Exmpl Rgistr Intrrn Grph 9 n nnot in th sm rgistr n n in th sm rgistr 11 12 2
Rgistr Intrrn Grph: Proprtis Extrts xtly th inormtion n to hrtriz lgl rgistr ssignmnts Givs glol pitur o rgistr rquirmnts A itionl gs in RIG to no rhitturl intriis Nos onnt y g hv irnt olors Equivlntly: no jnt nos hv sm olor Grph k-olorl = n olor with k olors Now wht o w o with this grph? Consir th xmpl RIG r2 W n to ssign olors (rgistrs) to grph nos (tmporris) Lt k = numr o mhin rgistrs r3 r1 r4 r2 I th RIG is k-olorl, thr s rgistr ssignmnt tht uss no mor thn k rgistrs 15 16 Computing Grph Colorings Unr this oloring th o oms: r2 := r3 + r4 r3 := -r2 r2 := r3 + r1 How o w omput oloring or intrrn grph? r3 := r3 + r2 r1 := 2 * r2 r3 Thr is no oloring with wr thn 4 olors Thr r 4-olorings o this grph Grph Coloring Exmpl, Continu 14 Grph Coloring Exmpl In our prolm, olors = rgistrs 13 Rgistr Allotion Through Grph Coloring Grph oloring: ssignmnt o olors to nos Ovr th ntir low grph Atr RIG onstrution, rgistr llotion is rhittur-inpnnt Grph Coloring NP-hr! For givn # o rgistrs, oloring my not xist r2 := r2-1 r3 := r1 + r4 Solution 17 Us huristis (hr, Briggs) 18 3
Grph Coloring Huristi Osrvtion: gr < k rul Ru grph: Pik no t with < k nighors in RIG Elimint t n its gs rom RIG I th rsulting grph hs k-oloring, so os th originl grph Why? Lt 1,, n olors ssign to nighors o t in ru grph Sin n < k, w n pik som olor or t irnt rom thos o its nighors Grph Coloring Huristi, Continu Huristi: Pik no t with wr thn k nighors Put t on stk n rmov it rom th RIG Rpt until th grph hs on no Strt ssigning olors to nos on th stk (strting with th lst no ) At h stp, pik olor irnt rom thos ssign to lry-olor nighors Dprtmnt o Computr Sin 19 Dprtmnt o Computr Sin 20 Grph Coloring Exmpl (1) Strt with th RIG n with k = 4: Stk: {} Rmov n thn Grph Coloring Exmpl (2) Now ll nos hv wr thn 4 nighors n n rmov:,,, Stk: {, } Dprtmnt o Computr Sin 21 Dprtmnt o Computr Sin 22 Grph Coloring Exmpl (2) Strt ssigning olors to:,,,,, r 1 r 2 r 2 r 3 r 3 r 4 Dprtmnt o Computr Sin 23 Wht i th Huristi Fils? Wht i uring simpliition w gt to stt whr ll nos hv k or mor nighors? Exmpl: try to in 3-oloring o th RIG: Dprtmnt o Computr Sin 24 4
Wht i th Huristi Fils? Wht i th Huristi Fils? Rmov n gt stuk (s shown low) Pik no s nit or spilling Assum tht is pik Rmov n ontinu th simpliition Simpliition now sus:,,, 25 Wht i th Huristi Fils? Optimisti oloring il must spill tmporry Allot mmory lotion s hom o r3 r2 Typilly in urrnt stk rm Cll this rss Bor h oprtion tht uss, insrt Atr h oprtion tht ins, insrt := lo r1 stor, r3 28 Romputing Livnss Inormtion Nw o tr spilling Nw livnss inormtion tr spilling: {,,} {,,} := + := - := lo := + {,,} := + := 2 * stor, 27 Spilling Exmpl 26 Spilling During ssignmnt phs, w gt to th point whn w hv to ssign olor to Hop: mong th 4 nighors o, w us lss thn 3 olors optimisti oloring? := 2 * stor, {,} := - 1 {,} {,} := lo := + {,} := + := - := lo := + {,,} {,,,} := + {,} := lo := + {,,,} := - 1 {} {} 29 30 5
Romputing Livnss Inormtion Nw livnss ino lmost s or, ut: is liv only Btwn := lo n th nxt instrution Btwn stor, n th pring instrution Spilling rus th liv rng o Rus its intrrns Rsults in wr nighors in RIG or Dprtmnt o Computr Sin 31 Romput RIG Atr Spilling Rmov som gs o spill no Hr, still intrrs only with n Rsulting RIG is 3-olorl Dprtmnt o Computr Sin 32 Spilling, Continu Aitionl spills might rquir or oloring is oun Triky prt: iing wht to spill Possil huristis: Spill tmporris with most onlits Spill tmporris with w initions n uss Avoi spilling in innr loops All r orrt Conlusion Rgistr llotion: must hv optimiztion in most ompilrs: Intrmit o uss too mny tmporris Mks ig irn in prormn Grph oloring: Powrul rgistr llotion shm Dprtmnt o Computr Sin 33 Dprtmnt o Computr Sin 34 Nxt Tim Shuling R ACDI Chptr 17 Dprtmnt o Computr Sin 35 6