-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.py
More file actions
393 lines (316 loc) · 10.3 KB
/
optimizer.py
File metadata and controls
393 lines (316 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import sys
import time
import atexit
import os
from dataclasses import dataclass
from concurrent.futures import ProcessPoolExecutor
@dataclass
class Token:
kind: str # "int", "ident", "num", "+", "=", "+=", ";", ",", "(", ")"
value: str
@dataclass
class Statement:
target: str
op1: str
op2: str | None
line_num: int
def tokenize(line):
tokens = []
i = 0
while i < len(line):
c = line[i]
if c.isspace():
i += 1
elif c in ('"', "'"):
i += 1
while i < len(line) and line[i] != c:
if line[i] == '\\':
i += 1
i += 1
if i < len(line):
i += 1
elif c == '/' and i + 1 < len(line) and line[i + 1] == '/':
break
elif (line[i:i+3] == 'int'
and (i + 3 >= len(line) or not (line[i+3].isalnum() or line[i+3] == '_'))):
tokens.append(Token("int", "int"))
i += 3
elif c.isalpha() or c == '_':
j = i
while j < len(line) and (line[j].isalnum() or line[j] == '_'):
j += 1
tokens.append(Token("ident", line[i:j]))
i = j
elif c.isdigit():
j = i
while j < len(line) and line[j].isdigit():
j += 1
tokens.append(Token("num", line[i:j]))
i = j
elif c == '+' and i + 1 < len(line) and line[i + 1] == '=':
tokens.append(Token("+=", "+="))
i += 2
elif c == '+':
tokens.append(Token("+", "+"))
i += 1
elif c == '-' and i + 1 < len(line) and line[i + 1].isdigit():
j = i + 1
while j < len(line) and line[j].isdigit():
j += 1
tokens.append(Token("num", line[i:j]))
i = j
elif c == '=':
tokens.append(Token("=", "="))
i += 1
elif c == ';':
tokens.append(Token(";", ";"))
i += 1
elif c == ',':
tokens.append(Token(",", ","))
i += 1
elif c in ('(', ')'):
tokens.append(Token(c, c))
i += 1
else:
i += 1
return tokens
def strip_parens(tokens):
"""Remove outer parens from token list: (a + b) → a + b"""
out = [t for t in tokens if t.kind not in ("(", ")")]
return out
def parse_expr(tokens):
"""operand ('+' operand)* — returns list of operands or None"""
tokens = strip_parens(tokens)
if not tokens:
return None
operands = []
for i, tok in enumerate(tokens):
if i % 2 == 0:
if tok.kind not in ("ident", "num"):
return None
operands.append(tok.value)
else:
if tok.kind != "+":
return None
return operands if operands else None
def split_by_comma(tokens):
"""Split token list on ',' and ';', returning sublists."""
segments = []
cur = []
for t in tokens:
if t.kind in (",", ";"):
if cur:
segments.append(cur)
cur = []
else:
cur.append(t)
if cur:
segments.append(cur)
return segments
def has_function_call(tokens):
"""True if tokens contain ident( pattern — a function call or definition."""
for i in range(len(tokens) - 1):
if tokens[i].kind == "ident" and tokens[i + 1].kind == "(":
return True
return False
def parse_line(tokens):
"""Parse one source line into (target, operands) pairs.
Handles:
int x = 5; int x = (a + b) + c;
int x, y, z; int x = -5;
int x = 5, y = a + b; x += 5;
x = 5; x = a + b;
Skips function calls (printf, main, etc).
"""
if not tokens:
return []
if has_function_call(tokens):
return []
results = []
if tokens[0].kind == "int":
rest = tokens[1:]
for seg in split_by_comma(rest):
if not seg:
continue
if seg[0].kind != "ident":
continue
if len(seg) == 1:
results.append((seg[0].value, ["0"]))
elif len(seg) >= 3 and seg[1].kind == "=":
ops = parse_expr(seg[2:])
if ops:
results.append((seg[0].value, ops))
return results
# x += expr; → x = x + expr
if tokens[0].kind == "ident" and len(tokens) >= 3 and tokens[1].kind == "+=":
semi = len(tokens)
for idx, t in enumerate(tokens):
if t.kind == ";":
semi = idx
break
ops = parse_expr(tokens[2:semi])
if ops:
return [(tokens[0].value, [tokens[0].value] + ops)]
# x = expr;
if tokens[0].kind == "ident" and len(tokens) >= 3 and tokens[1].kind == "=":
semi = len(tokens)
for idx, t in enumerate(tokens):
if t.kind == ";":
semi = idx
break
ops = parse_expr(tokens[2:semi])
if ops:
return [(tokens[0].value, ops)]
return []
_temp_counter = 0
_POOL = None
_MAX_WORKERS = max(1, os.cpu_count() or 1)
_POOL_WARM = False
_BURN_ITERATIONS = max(1, int(os.environ.get("OPTIMIZER_BURN_ITERATIONS", "8000000")))
def lower_to_tac(target, operands, line_num):
global _temp_counter
if len(operands) == 1:
return [Statement(target, operands[0], None, line_num)]
stmts = []
prev = operands[0]
for i in range(1, len(operands)):
if i == len(operands) - 1:
dest = target
else:
dest = f"_t{_temp_counter}"
_temp_counter += 1
stmts.append(Statement(dest, prev, operands[i], line_num))
prev = dest
return stmts
def parse_to_ir(cpp_source):
global _temp_counter
_temp_counter = 0
stmts = []
for num, line in enumerate(cpp_source.strip().split('\n')):
tokens = tokenize(line.strip())
for target, operands in parse_line(tokens):
stmts.extend(lower_to_tac(target, operands, num))
return stmts
def build_dep_graph(stmts):
targets = {}
graph = {}
for i, s in enumerate(stmts):
deps = []
for operand in (s.op1, s.op2):
if operand and operand in targets:
deps.append(targets[operand])
if deps:
graph[i] = deps
targets[s.target] = i
return graph
def find_batches(stmts, dep_graph):
assigned = {}
for i in range(len(stmts)):
if i not in dep_graph:
assigned[i] = 0
else:
assigned[i] = max(assigned[j] for j in dep_graph[i]) + 1
if not assigned:
return []
num_batches = max(assigned.values()) + 1
return [[i for i, b in assigned.items() if b == level] for level in range(num_batches)]
def execute_stmt(stmt, var_store):
def resolve(operand):
if operand is None:
return 0
return int(operand) if operand.lstrip('-').isdigit() else var_store[operand]
# Every IR statement simulates CPU work so wide independent batches
# have something meaningful to parallelize in the demo.
x = 0
for _ in range(_BURN_ITERATIONS):
x += 1
a = resolve(stmt.op1)
if stmt.op2 is None:
return stmt.target, a
b = resolve(stmt.op2)
return stmt.target, a + b
def execute_batch(batch_stmts, var_store):
return [execute_stmt(stmt, var_store) for stmt in batch_stmts]
def get_process_pool():
global _POOL
if _POOL is None:
_POOL = ProcessPoolExecutor(max_workers=_MAX_WORKERS)
return _POOL
def _prime_worker():
return None
def warm_process_pool():
global _POOL_WARM
if _POOL_WARM:
return
pool = get_process_pool()
futures = [pool.submit(_prime_worker) for _ in range(_MAX_WORKERS)]
for future in futures:
future.result()
_POOL_WARM = True
def shutdown_process_pool():
global _POOL
if _POOL is not None:
_POOL.shutdown(wait=False, cancel_futures=True)
_POOL = None
atexit.register(shutdown_process_pool)
def chunked(values, chunk_count):
if not values:
return []
size = max(1, (len(values) + chunk_count - 1) // chunk_count)
return [values[index:index + size] for index in range(0, len(values), size)]
def run_sequential(stmts):
var_store = {}
start = time.perf_counter()
for s in stmts:
target, val = execute_stmt(s, var_store)
var_store[target] = val
return var_store, time.perf_counter() - start
def run_parallel(stmts, batches):
var_store = {}
warm_process_pool()
pool = get_process_pool()
start = time.perf_counter()
for batch in batches:
if len(batch) <= 1:
for stmt_idx in batch:
target, val = execute_stmt(stmts[stmt_idx], var_store)
var_store[target] = val
continue
worker_count = min(len(batch), _MAX_WORKERS)
stmt_groups = [
[stmts[stmt_idx] for stmt_idx in stmt_group]
for stmt_group in chunked(batch, worker_count)
]
futures = [pool.submit(execute_batch, stmt_group, var_store) for stmt_group in stmt_groups]
for future in futures:
for target, val in future.result():
var_store[target] = val
return var_store, time.perf_counter() - start
if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: python optimizer.py <input.cpp>")
sys.exit(1)
with open(sys.argv[1]) as f:
source = f.read()
stmts = parse_to_ir(source)
graph = build_dep_graph(stmts)
batches = find_batches(stmts, graph)
print("IR:")
for s in stmts:
if s.op2:
print(f" {s.target} = {s.op1} + {s.op2}")
else:
print(f" {s.target} = {s.op1}")
print("\ndep graph:")
for idx, deps in graph.items():
print(f" stmt {idx} -> depends on {deps}")
print("\nbatches:")
for i, batch in enumerate(batches):
print(f" {i}: statements {batch}")
seq_results, seq_time = run_sequential(stmts)
par_results, par_time = run_parallel(stmts, batches)
print(f"\nsequential: {seq_time:.4f}s")
print(f"parallel: {par_time:.4f}s")
if par_time > 0:
print(f"speedup: {seq_time / par_time:.2f}x")
print(f"\nresults: {seq_results}")