Skip to content

Commit

Permalink
Merge branch 'main' into max_cap
Browse files Browse the repository at this point in the history
  • Loading branch information
donn authored Jan 10, 2024
2 parents 3521766 + fd43968 commit 12ec369
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 7 deletions.
1 change: 1 addition & 0 deletions openlane/flows/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
from .optimizing import Optimizing
from .classic import Classic, VHDLClassic
from .misc import OpenInKLayout, OpenInOpenROAD
from .synth_explore import SynthesisExploration
170 changes: 170 additions & 0 deletions openlane/flows/synth_explore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Copyright 2023 Efabless Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from decimal import Decimal
import os

import rich
import rich.table
from concurrent.futures import Future
from typing import Dict, List, Optional, Tuple

from .flow import Flow
from ..state import State
from ..config import Config
from ..logging import success
from ..logging import options, console
from ..steps import Step, Yosys, OpenROAD, StepError


# "Synthesis Exploration" is a non-seqeuential flow that tries all synthesis
# strategies and shows which ones yield the best area XOR delay
@Flow.factory.register()
class SynthesisExploration(Flow):
"""
Synthesis Exploration is a feature that tries multiple synthesis strategies
(in the form of different scripts for the ABC utility) to try and find which
strategy is better by either minimizing area or maximizing slack (and thus
frequency.)
The output is represented in a tabulated format, e.g.: ::
┏━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ ┃ ┃ ┃ Worst Setup ┃ Total Negative ┃
┃ SYNTH_STRATEGY ┃ Gates ┃ Area (µm²) ┃ Slack (ns) ┃ Setup Slack (ns) ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│ AREA 0 │ 8781 │ 97141.916800 │ 6.288794 │ 0.0 │
│ AREA 1 │ 8692 │ 96447.500800 │ 6.434102 │ 0.0 │
│ AREA 2 │ 8681 │ 96339.897600 │ 6.276806 │ 0.0 │
│ AREA 3 │ 11793 │ 111084.038400 │ 7.011374 │ 0.0 │
│ DELAY 0 │ 8969 │ 101418.518400 │ 6.511191 │ 0.0 │
│ DELAY 1 │ 8997 │ 101275.881600 │ 6.656564 │ 0.0 │
│ DELAY 2 │ 9013 │ 101177.036800 │ 6.691765 │ 0.0 │
│ DELAY 3 │ 8733 │ 99190.131200 │ 6.414865 │ 0.0 │
│ DELAY 4 │ 8739 │ 101011.878400 │ 6.274565 │ 0.0 │
└────────────────┴───────┴───────────────┴──────────────────┴──────────────────┘
You can then update your config file with the best ``SYNTH_STRATEGY`` for your
use-case so it can be used with other flows.
"""

Steps = [
Yosys.Synthesis,
OpenROAD.CheckSDCFiles,
OpenROAD.STAPrePNR,
]

def run(
self,
initial_state: State,
**kwargs,
) -> Tuple[State, List[Step]]:
step_list: List[Step] = []

self.progress_bar.set_max_stage_count(1)

synth_futures: List[Tuple[Config, Future[State]]] = []
self.progress_bar.start_stage("Synthesis Exploration")

options.set_condensed_mode(True)

for strategy in [
"AREA 0",
"AREA 1",
"AREA 2",
"AREA 3",
"DELAY 0",
"DELAY 1",
"DELAY 2",
"DELAY 3",
"DELAY 4",
]:
config = self.config.copy(SYNTH_STRATEGY=strategy)

synth_step = Yosys.Synthesis(
config,
id=f"synthesis-{strategy}",
state_in=initial_state,
)
synth_future = self.start_step_async(synth_step)
step_list.append(synth_step)

sdc_step = OpenROAD.CheckSDCFiles(
config,
id=f"sdc-{strategy}",
state_in=synth_future,
)
sdc_future = self.start_step_async(sdc_step)
step_list.append(sdc_step)

sta_step = OpenROAD.STAPrePNR(
config,
state_in=sdc_future,
id=f"sta-{strategy}",
)

step_list.append(sta_step)
sta_future = self.start_step_async(sta_step)

synth_futures.append((config, sta_future))

results: Dict[str, Optional[Tuple[Decimal, Decimal, Decimal, Decimal]]] = {}
for config, future in synth_futures:
strategy = config["SYNTH_STRATEGY"]
results[strategy] = None
try:
state = future.result()
results[strategy] = (
state.metrics["design__instance__count"],
state.metrics["design__instance__area"],
state.metrics["timing__setup__ws"],
state.metrics["timing__setup__tns"],
)
except StepError:
pass # None == failure
self.progress_bar.end_stage()
options.set_condensed_mode(False)

successful_results = {k: v for k, v in results.items() if v is not None}
min_gates = min(map(lambda x: x[0], successful_results.values()))
min_area = min(map(lambda x: x[1], successful_results.values()))
max_slack = max(map(lambda x: x[2], successful_results.values()))
max_tns = max(map(lambda x: x[3], successful_results.values()))

table = rich.table.Table(width=80)
table.add_column("SYNTH_STRATEGY")
table.add_column("Gates")
table.add_column("Area (µm²)")
table.add_column("Worst Setup Slack (ns)")
table.add_column("Total Negative Setup Slack (ns)")
for key, result in results.items():
gates_s = "[red]Failed"
area_s = "[red]Failed"
slack_s = "[red]Failed"
tns_s = "[red]Failed"
if result is not None:
gates, area, slack, tns = result
gates_s = f"{'[green]' if gates == min_gates else ''}{gates}"
area_s = f"{'[green]' if area == min_area else ''}{area}"
slack_s = f"{'[green]' if slack == max_slack else ''}{slack}"
tns_s = f"{'[green]' if tns == max_tns else ''}{tns}"
table.add_row(key, gates_s, area_s, slack_s, tns_s)

console.print(table)
assert self.run_dir is not None
with open(os.path.join(self.run_dir, "summary.rpt"), "w") as f:
rich.print(table, file=f)

success("Flow complete.")
return (initial_state, step_list)
14 changes: 9 additions & 5 deletions openlane/scripts/yosys/synthesize.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ proc synth_strategy_format_err { } {
upvar area_scripts area_scripts
upvar delay_scripts delay_scripts
log -stderr "\[ERROR] Misformatted SYNTH_STRATEGY (\"$::env(SYNTH_STRATEGY)\")."
log -stderr "\[ERROR] Correct format is \"DELAY|AREA 0-[expr [llength $delay_scripts]-1]|0-[expr [llength $area_scripts]-1]\"."
log -stderr "\[ERROR] Correct format is \"DELAY 0-[expr [llength $delay_scripts]-1]|AREA 0-[expr [llength $area_scripts]-1]\"."
exit 1
}

Expand Down Expand Up @@ -240,7 +240,8 @@ if { $::env(SYNTH_ELABORATE_ONLY) } {
opt_clean -purge

tee -o "$report_dir/chk.rpt" check
tee -o "$report_dir/stat.json" stat -json
tee -o "$report_dir/stat.json" stat -json {*}$lib_args
tee -o "$report_dir/stat.log" stat {*}$lib_args

write_verilog -noattr -noexpr -nohex -nodec -defparam "$::env(SAVE_NETLIST)"
write_json "$::env(SAVE_NETLIST).json"
Expand Down Expand Up @@ -338,7 +339,8 @@ if { $adder_type == "FA" } {
opt
opt_clean -purge

tee -o "$report_dir/pre_techmap.json" stat -json
tee -o "$report_dir/pre_techmap.json" stat -json {*}$lib_args
tee -o "$report_dir/pre_techmap.log" stat {*}$lib_args

# Map tri-state buffers
if { $tbuf_map } {
Expand All @@ -362,7 +364,8 @@ if { [info exists ::env(SYNTH_LATCH_MAP)] } {
}

dfflibmap {*}$dfflib_args
tee -o "$report_dir/post_dff.json" stat -json
tee -o "$report_dir/post_dff.json" stat -json {*}$lib_args
tee -o "$report_dir/post_dff.log" stat {*}$lib_args

proc run_strategy {output script strategy_name {postfix_with_strategy 0}} {
upvar clock_period clock_period
Expand Down Expand Up @@ -395,7 +398,8 @@ proc run_strategy {output script strategy_name {postfix_with_strategy 0}} {
}

tee -o "$report_dir/chk.rpt" check
tee -o "$report_dir/stat.json" stat -json
tee -o "$report_dir/stat.json" stat -json {*}$lib_args
tee -o "$report_dir/stat.log" stat {*}$lib_args

if { $::env(SYNTH_AUTONAME) } {
# Generate public names for the various nets, resulting in very long names that include
Expand Down
104 changes: 103 additions & 1 deletion openlane/steps/openroad.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class OpenROADStep(TclStep):
]

@abstractmethod
def get_script_path(self):
def get_script_path(self) -> str:
pass

def prepare_env(self, env: dict, state: State) -> dict:
Expand Down Expand Up @@ -1077,13 +1077,115 @@ class CheckAntennas(OpenROADStep):
def get_script_path(self):
return os.path.join(get_script_dir(), "openroad", "antenna_check.tcl")

def __summarize_antenna_report(self, report_file: str, output_file: str):
"""
Extracts the list of violating nets from an ARC report file"
"""

class AntennaViolation:
def __init__(self, net, pin, required_ratio, partial_ratio, layer):
self.net = net
self.pin = pin
self.required_ratio = float(required_ratio)
self.partial_ratio = float(partial_ratio)
self.layer = layer
self.partial_to_required = self.partial_ratio / self.required_ratio

def __lt__(self, other):
return self.partial_to_required < other.partial_to_required

net_pattern = re.compile(r"\s*Net:\s*(\S+)")
required_ratio_pattern = re.compile(r"\s*Required ratio:\s+([\d.]+)")
partial_ratio_pattern = re.compile(r"\s*Partial area ratio:\s+([\d.]+)")
layer_pattern = re.compile(r"\s*Layer:\s+(\S+)")
pin_pattern = re.compile(r"\s*Pin:\s+(\S+)")

required_ratio = None
layer = None
partial_ratio = None
required_ratio = None
pin = None
net = None
violations: List[AntennaViolation] = []

net_pattern = re.compile(r"\s*Net:\s*(\S+)")
required_ratio_pattern = re.compile(r"\s*Required ratio:\s+([\d.]+)")
partial_ratio_pattern = re.compile(r"\s*Partial area ratio:\s+([\d.]+)")
layer_pattern = re.compile(r"\s*Layer:\s+(\S+)")
pin_pattern = re.compile(r"\s*Pin:\s+(\S+)")

with open(report_file, "r") as f:
for line in f:
pin_new = pin_pattern.match(line)
required_ratio_new = required_ratio_pattern.match(line)
partial_ratio_new = partial_ratio_pattern.match(line)
layer_new = layer_pattern.match(line)
net_new = net_pattern.match(line)
required_ratio = (
required_ratio_new.group(1)
if required_ratio_new is not None
else required_ratio
)
partial_ratio = (
partial_ratio_new.group(1)
if partial_ratio_new is not None
else partial_ratio
)
layer = layer_new.group(1) if layer_new is not None else layer
pin = pin_new.group(1) if pin_new is not None else pin
net = net_new.group(1) if net_new is not None else net

if "VIOLATED" in line:
violations.append(
AntennaViolation(
net=net,
pin=pin,
partial_ratio=partial_ratio,
layer=layer,
required_ratio=required_ratio,
)
)

violations.sort(reverse=True)

# Partial/Required: 2.36, Required: 3091.96, Partial: 7298.29,
# Net: net384, Pin: _22354_/A, Layer: met5
table = rich.table.Table()
decimal_places = 2
row = []
table.add_column("Partial/Required")
table.add_column("Required")
table.add_column("Partial")
table.add_column("Net")
table.add_column("Pin")
table.add_column("Layer")
for violation in violations:
row = [
f"{violation.partial_to_required:.{decimal_places}f}",
f"{violation.required_ratio:.{decimal_places}f}",
f"{violation.partial_ratio:.{decimal_places}f}",
f"{violation.net}",
f"{violation.pin}",
f"{violation.layer}",
]
table.add_row(*row)

if not options.get_condensed_mode():
console.print(table)
with open(output_file, "w") as f:
rich.print(table, file=f)

def run(self, state_in: State, **kwargs) -> Tuple[ViewsUpdate, MetricsUpdate]:
views_updates, metrics_updates = super().run(state_in, **kwargs)

metrics_updates["route__antenna_violations__count"] = get_antenna_nets(
open(os.path.join(self.step_dir, "antenna.rpt")),
open(os.path.join(self.step_dir, "antenna_net_list.txt"), "w"),
)
self.__summarize_antenna_report(
os.path.join(self.step_dir, "antenna.rpt"),
os.path.join(self.step_dir, "antenna_summary.rpt"),
)

return views_updates, metrics_updates

Expand Down
1 change: 0 additions & 1 deletion openlane/steps/yosys.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ class SynthesisCommon(YosysStep):
"AREA 1",
"AREA 2",
"AREA 3",
"AREA 4",
"DELAY 0",
"DELAY 1",
"DELAY 2",
Expand Down

0 comments on commit 12ec369

Please sign in to comment.