interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / ng / target / regalloc.om

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