omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / ng / target / regalloc.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/target/regalloc.om: Shared target-parameterized live-range register allocator + whole-program IPRA. |
| 3 | note Parameterized purely by TargetDesc (ngpr, two_addr, caller_save_mask) -- works across A64, x86-64, RV64, Thumb-2, SM121. |
| 4 | |
| 5 | const RA_MAX_VALS ulong 256 |
| 6 | const RA_MAX_FNS ulong 32 |
| 7 | const RA_SPILL ulong 65535 |
| 8 | |
| 9 | note Input live intervals per SSA value: [raStart[v], raEnd[v]] |
| 10 | global raNVal ulong 1 |
| 11 | global raStart ulong 256 |
| 12 | global raEnd ulong 256 |
| 13 | global raFixed ulong 256 |
| 14 | |
| 15 | note Output assignment: raPhys[v] = physical reg (0..ngpr-1) or RA_SPILL; raSlot[v] = spill slot |
| 16 | global raPhys ulong 256 |
| 17 | global raSlot ulong 256 |
| 18 | global raNSpill ulong 1 |
| 19 | global raUsedMask ulong 1 |
| 20 | |
| 21 | note Internal active-set tracking (max 64 physical regs) |
| 22 | global raBusy ulong 64 |
| 23 | global raBusyEnd ulong 64 |
| 24 | |
| 25 | note Whole-program IPRA tables (per function: direct clobbers, callee edges, propagated clobbers) |
| 26 | global raFnCnt ulong 1 |
| 27 | global raFnDirect ulong 32 |
| 28 | global raFnProp ulong 32 |
| 29 | global raEdgeSrc ulong 64 |
| 30 | global raEdgeDst ulong 64 |
| 31 | global raEdgeCnt ulong 1 |
| 32 | |
| 33 | fn raReset to void |
| 34 | put raNVal 0 0 |
| 35 | put raNSpill 0 0 |
| 36 | put raUsedMask 0 0 |
| 37 | for i ulong 64 |
| 38 | put raBusy i 0 |
| 39 | put raBusyEnd i 0 |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | fn raAddInterval s ulong e ulong fixedReg ulong to ulong |
| 44 | id = get raNVal 0 |
| 45 | put raStart id s |
| 46 | put raEnd id e |
| 47 | put raFixed id fixedReg |
| 48 | put raPhys id RA_SPILL |
| 49 | put raSlot id 0 |
| 50 | put raNVal 0 add id 1 |
| 51 | ret id |
| 52 | end |
| 53 | |
| 54 | fn raExpireAt pos ulong ngpr ulong to void |
| 55 | for r ulong ngpr bound 64 |
| 56 | if and eq get raBusy r 1 lt get raBusyEnd r pos |
| 57 | put raBusy r 0 |
| 58 | end |
| 59 | end |
| 60 | end |
| 61 | |
| 62 | fn raFindFree ngpr ulong to ulong |
| 63 | for r ulong ngpr bound 64 |
| 64 | if eq get raBusy r 0 |
| 65 | ret r |
| 66 | end |
| 67 | end |
| 68 | ret RA_SPILL |
| 69 | end |
| 70 | |
| 71 | fn raAllocOne v ulong ngpr ulong to void |
| 72 | s = get raStart v |
| 73 | e = get raEnd v |
| 74 | raExpireAt s ngpr |
| 75 | fx = get raFixed v |
| 76 | if and ne fx RA_SPILL lt fx ngpr |
| 77 | if eq get raBusy fx 0 |
| 78 | put raBusy fx 1 |
| 79 | put raBusyEnd fx e |
| 80 | put raPhys v fx |
| 81 | if lt fx 64 |
| 82 | put raUsedMask 0 or get raUsedMask 0 shl ulong 1 fx |
| 83 | end |
| 84 | ret |
| 85 | end |
| 86 | end |
| 87 | r = raFindFree ngpr |
| 88 | if ne r RA_SPILL |
| 89 | put raBusy r 1 |
| 90 | put raBusyEnd r e |
| 91 | put raPhys v r |
| 92 | if lt r 64 |
| 93 | put raUsedMask 0 or get raUsedMask 0 shl ulong 1 r |
| 94 | end |
| 95 | ret |
| 96 | end |
| 97 | sl = get raNSpill 0 |
| 98 | put raPhys v RA_SPILL |
| 99 | put raSlot v sl |
| 100 | put raNSpill 0 add sl 1 |
| 101 | end |
| 102 | |
| 103 | note Run target-parameterized linear-scan allocation for any TargetDesc tid! |
| 104 | fn raRunForTarget tid ulong to ulong |
| 105 | ngpr = tdGet tid TDF_NGPR |
| 106 | if gt ngpr 64 |
| 107 | ngpr = ulong 64 |
| 108 | end |
| 109 | put raNSpill 0 0 |
| 110 | put raUsedMask 0 0 |
| 111 | for r ulong 64 |
| 112 | put raBusy r 0 |
| 113 | put raBusyEnd r 0 |
| 114 | end |
| 115 | n = get raNVal 0 |
| 116 | for v ulong n bound 256 |
| 117 | raAllocOne v ngpr |
| 118 | end |
| 119 | ret get raNSpill 0 |
| 120 | end |
| 121 | |
| 122 | note Graph-coloring invariant verifier: returns 0 iff NO two overlapping intervals share a physical reg! |
| 123 | fn raVerifyColoring to ulong |
| 124 | n = get raNVal 0 |
| 125 | bad = ulong 0 |
| 126 | for i ulong n bound 256 |
| 127 | pi = get raPhys i |
| 128 | if ne pi RA_SPILL |
| 129 | si = get raStart i |
| 130 | ei = get raEnd i |
| 131 | for j ulong n bound 256 |
| 132 | if gt j i |
| 133 | if eq get raPhys j pi |
| 134 | sj = get raStart j |
| 135 | ej = get raEnd j |
| 136 | if and le si ej le sj ei |
| 137 | bad = add bad 1 |
| 138 | end |
| 139 | end |
| 140 | end |
| 141 | end |
| 142 | end |
| 143 | end |
| 144 | ret bad |
| 145 | end |
| 146 | |
| 147 | note Whole-program IPRA: propagate clobber bitmasks bottom-up across the call graph to fixed point! |
| 148 | fn raIpraReset nFns ulong to void |
| 149 | put raFnCnt 0 nFns |
| 150 | put raEdgeCnt 0 0 |
| 151 | for i ulong nFns bound 32 |
| 152 | put raFnDirect i 0 |
| 153 | put raFnProp i 0 |
| 154 | end |
| 155 | end |
| 156 | |
| 157 | fn raIpraSetDirect fnId ulong mask ulong to void |
| 158 | put raFnDirect fnId mask |
| 159 | put raFnProp fnId mask |
| 160 | end |
| 161 | |
| 162 | fn raIpraAddCall caller ulong callee ulong to void |
| 163 | e = get raEdgeCnt 0 |
| 164 | put raEdgeSrc e caller |
| 165 | put raEdgeDst e callee |
| 166 | put raEdgeCnt 0 add e 1 |
| 167 | end |
| 168 | |
| 169 | fn raIpraSolve to void |
| 170 | ec = get raEdgeCnt 0 |
| 171 | for iter ulong 32 |
| 172 | chg = ulong 0 |
| 173 | for e ulong ec bound 64 |
| 174 | u = get raEdgeSrc e |
| 175 | v = get raEdgeDst e |
| 176 | old = get raFnProp u |
| 177 | nw = or old get raFnProp v |
| 178 | if ne nw old |
| 179 | put raFnProp u nw |
| 180 | chg = ulong 1 |
| 181 | end |
| 182 | end |
| 183 | if eq chg 0 |
| 184 | ret |
| 185 | end |
| 186 | end |
| 187 | end |
| 188 | |
| 189 | fn raPopcount m ulong to ulong |
| 190 | x = m |
| 191 | c = ulong 0 |
| 192 | for i ulong 64 |
| 193 | c = add c and x 1 |
| 194 | x = shr x 1 |
| 195 | end |
| 196 | ret c |
| 197 | end |