Skip to content

Commit 12e0974

Browse files
committed
bazel: Autotuning framework for GPL heuristics
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent f9ec54a commit 12e0974

4 files changed

Lines changed: 164 additions & 1 deletion

File tree

‎flow/scripts/global_place.tcl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ append_env_var global_placement_args GPL_ROUTABILITY_DRIVEN -routability_driven
2828

2929
append_env_var global_placement_args GPL_RANDOM_SEED -random_seed 1
3030

31+
if { [info exists ::env(GPL_WIRELENGTH_PENALTY)] } {
32+
lappend global_placement_args -init_wirelength_coef $::env(GPL_WIRELENGTH_PENALTY)
33+
}
34+
35+
if { [info exists ::env(GPL_TIMING_SPAN_CLOCK_PERCENT)] } {
36+
lappend global_placement_args -virtual_cts_max_skew_fraction $::env(GPL_TIMING_SPAN_CLOCK_PERCENT)
37+
}
38+
3139
# Parameters for timing driven mode in global placement
3240
if { $::env(GPL_TIMING_DRIVEN) } {
3341
lappend global_placement_args {-timing_driven}

‎flow/scripts/tune_gpl_magic.py‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import itertools
4+
import json
5+
import subprocess
6+
import os
7+
import yaml
8+
from pathlib import Path
9+
10+
# The small/smoke test set
11+
SMOKE_TARGETS = [
12+
"//flow/designs/sky130hd/gcd:gcd_route",
13+
"//flow/designs/asap7/gcd:gcd_route",
14+
"//flow/designs/asap7/uart:uart_route"
15+
]
16+
17+
# The large/main campaign test set
18+
LARGE_TARGETS = [
19+
"//flow/designs/sky130hd/ibex:ibex_core_route",
20+
"//flow/designs/sky130hd/jpeg:jpeg_encoder_route",
21+
"//flow/designs/sky130hd/microwatt:microwatt_route",
22+
"//flow/designs/sky130hd/riscv32i:riscv_route",
23+
"//flow/designs/asap7/mock_array:mock_array_route",
24+
"//flow/designs/asap7/swerv:swerv_wrapper_route",
25+
"//flow/designs/asap7/rocket:RocketTile_route"
26+
]
27+
28+
# Grid of hyper-parameters
29+
PARAM_GRID = {
30+
"GPL_WIRELENGTH_PENALTY": [0.1, 0.25, 0.5],
31+
"GPL_TIMING_SPAN_CLOCK_PERCENT": [0.05, 0.10, 0.15]
32+
}
33+
34+
def parse_metrics(target_label):
35+
"""
36+
Given a target like //flow/designs/sky130hd/gcd:gcd_route,
37+
find its bazel-bin directory and parse the route metrics.
38+
"""
39+
# e.g. //flow/designs/sky130hd/gcd:gcd_route -> bazel-bin/flow/designs/sky130hd/gcd/gcd_route
40+
pkg, name = target_label.split(":")
41+
pkg_path = pkg.replace("//", "")
42+
bin_dir = Path("bazel-bin") / pkg_path / name / "reports"
43+
44+
# Check for the route timing report
45+
wns = None
46+
tns = None
47+
report_file = bin_dir / "route" / "6_1_fill_route_timing.rpt"
48+
if not report_file.exists():
49+
# Fallback to grt timing if route doesn't exist
50+
report_file = bin_dir / "grt" / "5_route_timing.rpt"
51+
52+
if report_file.exists():
53+
with open(report_file, "r") as f:
54+
for line in f:
55+
if "wns" in line.lower():
56+
try:
57+
wns = float(line.split()[-1])
58+
except ValueError:
59+
pass
60+
if "tns" in line.lower():
61+
try:
62+
tns = float(line.split()[-1])
63+
except ValueError:
64+
pass
65+
return {"wns": wns, "tns": tns}
66+
67+
def run_experiment(targets, params):
68+
"""Run bazelisk build on the targets with the given params."""
69+
cmd = ["bazelisk", "build"]
70+
for k, v in params.items():
71+
cmd.append(f"--define={k}={v}")
72+
cmd.extend(targets)
73+
74+
print(f"Running: {' '.join(cmd)}")
75+
result = subprocess.run(cmd, capture_output=True, text=True)
76+
77+
if result.returncode != 0:
78+
print(f"Build failed for params {params}")
79+
return None
80+
81+
results = {}
82+
for t in targets:
83+
results[t] = parse_metrics(t)
84+
return results
85+
86+
def compute_best_params(all_results):
87+
"""Simple pareto-optimal/best selection based on average WNS."""
88+
best_params = None
89+
best_score = float('-inf')
90+
91+
for run in all_results:
92+
metrics = run["results"]
93+
if not metrics:
94+
continue
95+
96+
score = 0
97+
valid = True
98+
for t, m in metrics.items():
99+
if m["wns"] is None:
100+
valid = False
101+
break
102+
score += m["wns"] # Higher WNS (closer to 0 or positive) is better
103+
104+
if valid and score > best_score:
105+
best_score = score
106+
best_params = run["params"]
107+
108+
return best_params
109+
110+
def main():
111+
parser = argparse.ArgumentParser()
112+
parser.add_argument("--campaign", choices=["smoke", "large"], default="smoke")
113+
parser.add_argument("--output", default="study_results.yaml")
114+
args = parser.parse_args()
115+
116+
targets = SMOKE_TARGETS if args.campaign == "smoke" else LARGE_TARGETS
117+
118+
keys, values = zip(*PARAM_GRID.items())
119+
combinations = [dict(zip(keys, v)) for v in itertools.product(*values)]
120+
121+
all_results = []
122+
123+
for params in combinations:
124+
metrics = run_experiment(targets, params)
125+
all_results.append({
126+
"params": params,
127+
"results": metrics
128+
})
129+
130+
best = compute_best_params(all_results)
131+
132+
final_output = {
133+
"campaign": args.campaign,
134+
"best_parameters": best,
135+
"all_results": all_results
136+
}
137+
138+
with open(args.output, "w") as f:
139+
yaml.dump(final_output, f, default_flow_style=False)
140+
141+
print(f"Study complete. Results saved to {args.output}")
142+
print(f"Best parameters: {best}")
143+
144+
if __name__ == "__main__":
145+
main()

‎flow/scripts/variables.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,16 @@ GPL_TIMING_DRIVEN:
664664
stages:
665665
- place
666666
default: 1
667+
GPL_WIRELENGTH_PENALTY:
668+
description: |
669+
Global placement wirelength penalty (passed as -init_wirelength_coef or similar logic).
670+
stages:
671+
- place
672+
GPL_TIMING_SPAN_CLOCK_PERCENT:
673+
description: |
674+
Global placement timing weight span as a percent of clock (passed to virtual_cts_max_skew_fraction or similar logic).
675+
stages:
676+
- place
667677
GPL_ROUTABILITY_DRIVEN:
668678
description: |
669679
Specifies whether the placer should use routability driven placement.

‎tools/OpenROAD‎

Submodule OpenROAD updated 131 files

0 commit comments

Comments
 (0)