File size: 17,270 Bytes
784ee2c | 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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | #!/usr/bin/env python3
from math import ceil, isinf
from os import chdir, getcwd
from os.path import isfile
from re import sub
import argparse
import fnmatch
import json
import operator
import os
import sys
def gen_rule_file(
rules_file,
new_rules_file,
update,
tighten,
failing,
variant,
metrics_file=None,
metrics_to_consider=[],
):
with open(metrics_file, "r") as f:
metrics = json.load(f)
if not isinstance(metrics, dict):
print(f"[ERROR] Invalid format for reference metrics {metrics_file}")
sys.exit(1)
rules = dict()
if isfile(rules_file):
with open(rules_file, "r") as f:
OLD_RULES = json.load(f)
else:
print(f"[WARNING] No old rules file found {rules_file}")
OLD_RULES = None
# Notes
# - Apply tighter margin on timing__setup__ws than timing__setup__tns
# because WNS is more important than TNS.
# - Apply the consistent margins on timing__setup__* and timing__hold__*
# - 'period_padding' mode is used for timing__setup__* and timing__hold__*
# to give small margin based on clock period to avoid failures by small
# violations.
# dict format
# 'metric_name': {
# 'mode': <str>, one of ['direct', 'literal', 'sum_fixed', 'period',
# 'padding', 'period_padding', 'abs_padding',
# 'metric']. 'literal' propagates the metric
# value verbatim (e.g. a hash string) and
# skips all numeric padding/rounding.
# 'padding': <float>, percentage of padding to use
# 'fixed': <float>, sum this number instead of using % padding
# 'round_value': <bool>, use the rounded value for the rule
# 'compare': <str>, one of ['<', '>', '<=', '>=', '==', '!=']
# 'metric': <str>, when mode is 'metric', use this metric to compute
# the rule value
# 'min_max': <function>, one of [min, max], optional
# 'min_max_direct': <float>, optional
# 'min_max_sum': <float>, optional
# 'min_max_period': <float>, optional
# 'level': <bool>, severity level. ['warning' or
# others (means ERROR, default) ]
# }
rules_dict = {
# all stages
"*flow__warnings__count:*": {
"mode": "direct",
"round_value": True,
"compare": "<=",
"level": "warning",
},
# synth
# Yosys netlist hash fingerprints. `mode: literal` propagates
# the string value verbatim; `level: warning` means a mismatch
# surfaces as a [WARN] diagnostic in checkMetadata.py without
# failing the build, matching how rules-base.json already
# treats warning counts.
"synth__canonical_netlist__hash": {
"mode": "literal",
"compare": "==",
"level": "warning",
},
"synth__netlist__hash": {
"mode": "literal",
"compare": "==",
"level": "warning",
},
"synth__design__instance__area__stdcell": {
"mode": "padding",
"padding": 15,
"round_value": False,
"compare": "<=",
},
# clock
"constraints__clocks__count": {
"mode": "direct",
"round_value": True,
"compare": "==",
},
# place
"placeopt__design__instance__area": {
"mode": "padding",
"padding": 15,
"round_value": True,
"compare": "<=",
},
"placeopt__design__instance__count__stdcell": {
"mode": "padding",
"padding": 15,
"round_value": True,
"compare": "<=",
},
"detailedplace__design__violations": {
"mode": "direct",
"round_value": True,
"compare": "==",
},
# cts
"cts__design__instance__count__setup_buffer": {
"mode": "metric",
"padding": 10,
"metric": "placeopt__design__instance__count__stdcell",
"round_value": True,
"compare": "<=",
},
"cts__design__instance__count__hold_buffer": {
"mode": "metric",
"padding": 10,
"metric": "placeopt__design__instance__count__stdcell",
"round_value": True,
"compare": "<=",
},
"cts__timing__setup__ws": {
"mode": "period_padding",
"padding": 5,
"round_value": False,
"compare": ">=",
},
"cts__timing__setup__tns": {
"mode": "period_padding",
"padding": 20,
"round_value": False,
"compare": ">=",
},
"cts__timing__hold__ws": {
"mode": "period_padding",
"padding": 5,
"round_value": False,
"compare": ">=",
},
"cts__timing__hold__tns": {
"mode": "period_padding",
"padding": 20,
"round_value": False,
"compare": ">=",
},
# route
"globalroute__antenna_diodes_count": {
"mode": "metric",
"padding": 0.1,
"metric": "globalroute__route__net",
"min_max": max,
"min_max_direct": 100,
"round_value": True,
"compare": "<=",
},
"globalroute__timing__setup__ws": {
"mode": "period_padding",
"padding": 5,
"round_value": False,
"compare": ">=",
},
"globalroute__timing__setup__tns": {
"mode": "period_padding",
"padding": 20,
"round_value": False,
"compare": ">=",
},
"globalroute__timing__hold__ws": {
"mode": "period_padding",
"padding": 5,
"round_value": False,
"compare": ">=",
},
"globalroute__timing__hold__tns": {
"mode": "period_padding",
"padding": 20,
"round_value": False,
"compare": ">=",
},
"detailedroute__route__wirelength": {
"mode": "padding",
"padding": 15,
"round_value": True,
"compare": "<=",
},
"detailedroute__route__drc_errors": {
"mode": "direct",
"round_value": True,
"compare": "<=",
},
"detailedroute__antenna__violating__nets": {
"mode": "padding",
"padding": 30,
"round_value": True,
"compare": "<=",
},
"detailedroute__antenna_diodes_count": {
"mode": "metric",
"padding": 0.1,
"metric": "detailedroute__route__net",
"min_max": max,
"min_max_direct": 100,
"round_value": True,
"compare": "<=",
},
# finish
"finish__timing__setup__ws": {
"mode": "period_padding",
"padding": 5,
"round_value": False,
"compare": ">=",
},
"finish__timing__setup__tns": {
"mode": "period_padding",
"padding": 20,
"round_value": False,
"compare": ">=",
},
"finish__timing__hold__ws": {
"mode": "period_padding",
"padding": 5,
"round_value": False,
"compare": ">=",
},
"finish__timing__hold__tns": {
"mode": "period_padding",
"padding": 20,
"round_value": False,
"compare": ">=",
},
"finish__design__instance__area": {
"mode": "padding",
"padding": 15,
"round_value": True,
"compare": "<=",
},
}
ops = {
"<": operator.lt,
">": operator.gt,
"<=": operator.le,
">=": operator.ge,
"==": operator.eq,
"!=": operator.ne,
}
period_list = metrics.get("constraints__clocks__details")
period = 0.0
if period_list:
period = float(sub(r"^.*: ", "", period_list[0]))
if len(period_list) != 1:
print(
f"[WARNING] Multiple clocks not supported. Will use first clock: {period_list[0]}."
)
else:
print(
"[WARNING] 'constraints__clocks__details' not found or is empty in metrics. Clock-related rules might be affected."
)
format_str = "| {:45} | {:10} | {:10} | {:8} |\n"
change_str = ""
processed_fields = set()
for pattern, option in rules_dict.items():
matching_fields = []
# Find all metric fields that match this pattern.
if "*" in pattern:
for metric_field in sorted(metrics.keys()):
if (
fnmatch.fnmatch(metric_field, pattern)
and metric_field not in processed_fields
):
matching_fields.append(metric_field)
elif pattern in metrics and pattern not in processed_fields:
matching_fields.append(pattern)
for field in matching_fields:
# Replace ':' with '__' as the dashboard DB does not accept
# ':' in # field names.
if ":" in field:
field = field.replace(":", "__")
processed_fields.add(field)
if isinstance(metrics[field], str) and option["mode"] != "literal":
print(f"[WARNING] Skipping string field {field} = {metrics[field]}")
continue
if len(period_list) != 1 and field == "globalroute__timing__clock__slack":
print("[WARNING] Skipping clock slack until multiple clocks support.")
continue
rule_value = None
if option["mode"] == "direct":
rule_value = metrics[field]
elif option["mode"] == "literal":
rule_value = metrics[field]
elif option["mode"] == "sum_fixed":
rule_value = metrics[field] + option["padding"]
elif option["mode"] == "period":
rule_value = metrics[field] - period * option["padding"] / 100
rule_value = min(rule_value, 0)
elif option["mode"] == "padding":
rule_value = metrics[field] * (1 + option["padding"] / 100)
elif option["mode"] == "period_padding":
negative_slack = min(metrics[field], 0)
rule_value = negative_slack - max(
negative_slack * option["padding"] / 100,
period * option["padding"] / 100,
)
elif option["mode"] == "abs_padding":
rule_value = abs(metrics[field]) * (1 + option["padding"] / 100)
elif option["mode"] == "metric":
rule_value = metrics[option["metric"]] * option["padding"] / 100
if (
field == "cts__design__instance__count__setup_buffer"
or field == "cts__design__instance__count__hold_buffer"
):
rule_value = max(rule_value, metrics[field] * 1.1)
if "min_max" in option.keys():
if "min_max_direct" in option.keys():
rule_value = option["min_max"](rule_value, option["min_max_direct"])
elif "min_max_sum" in option.keys():
rule_value = option["min_max"](
rule_value + option["min_max_sum"], option["min_max_sum"]
)
elif "min_max_period" in option.keys():
rule_value = option["min_max"](
rule_value, -period * option["min_max_period"] / 100.0
)
else:
print(
f"[ERROR] Metric {field} has 'min_max' field but no "
"'min_max_direct', 'min_max_sum', or 'min_max_period' field."
)
sys.exit(1)
if rule_value is None:
print(f"[ERROR] Metric {field} has invalid mode {option['mode']}.")
sys.exit(1)
if option["mode"] != "literal":
if option["round_value"] and not isinf(rule_value):
rule_value = int(round(rule_value))
else:
rule_value = float(f"{rule_value:.3g}")
preserve_old_rule = (
True
if len(metrics_to_consider) > 0 and field not in metrics_to_consider
else False
)
has_old_rule = OLD_RULES is not None and field in OLD_RULES.keys()
if has_old_rule and preserve_old_rule:
rule_value = OLD_RULES[field]["value"]
if has_old_rule and not preserve_old_rule:
old_rule = OLD_RULES[field]
if old_rule["compare"] != option["compare"]:
print("[WARNING] Compare operator changed since last update.")
compare = ops[option["compare"]]
if compare(rule_value, metrics[field]) and "padding" in option.keys():
rule_value = metrics[field] * (1 + option["padding"] / 100)
if option["round_value"] and not isinf(rule_value):
rule_value = int(round(rule_value))
else:
rule_value = float(f"{rule_value:.3g}")
need_to_update = False
if (
tighten
and rule_value != old_rule["value"]
and compare(rule_value, old_rule["value"])
):
need_to_update = True
change_str += format_str.format(
field, old_rule["value"], rule_value, "Tighten"
)
if failing and not compare(metrics[field], old_rule["value"]):
need_to_update = True
change_str += format_str.format(
field, old_rule["value"], rule_value, "Failing"
)
if update and old_rule["value"] != rule_value:
need_to_update = True
change_str += format_str.format(
field, old_rule["value"], rule_value, "Updating"
)
if not need_to_update:
rule_value = old_rule["value"]
rule_entry = {"value": rule_value, "compare": option["compare"]}
if "level" in option:
rule_entry["level"] = option["level"]
rules[field] = rule_entry
if len(change_str) > 0:
print(f"{os.path.normpath(rules_file)} updates:")
print(format_str.format("Metric", "Old", "New", "Type"), end="")
print(format_str.format("------", "---", "---", "----"), end="")
print(change_str)
with open(new_rules_file, "w") as f:
json.dump(rules, f, indent=4)
def comma_separated_list(value):
if value is None or value == "all":
return []
return [item.strip() for item in value.split(",")]
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generates or updates rules file for CI."
)
parser.add_argument(
"-v", "--variant", default="base", help='Flow variant [default="base"].'
)
parser.add_argument(
"-u",
"--update",
action="store_true",
default=False,
help="Update all rules independent of previous values.",
)
parser.add_argument(
"-t",
"--tighten",
action="store_true",
default=False,
help="Update passing rules if they became tighter",
)
parser.add_argument(
"-f",
"--failing",
action="store_true",
default=False,
help="Update failing rules.",
)
parser.add_argument(
"-r",
"--reference",
type=str,
default=None,
help="Reference metadata file.",
)
parser.add_argument(
"--rules",
type=str,
default=None,
help="Rules input file.",
)
parser.add_argument(
"--new-rules",
type=str,
default=None,
help="Rules output file.",
)
parser.add_argument(
"-m",
"--metrics",
type=comma_separated_list,
default="all",
help="Only consider the following metrics to change. [default=all]",
)
args = parser.parse_args()
if not args.update and not args.tighten and not args.failing:
print(
"[ERROR] Please select at least one of "
"-u/--update, -t/--tighten, -f/--failing."
)
parser.print_help()
sys.exit(1)
gen_rule_file(
args.rules,
args.new_rules,
args.update,
args.tighten,
args.failing,
args.variant,
args.reference,
args.metrics,
)
|