Skip to content

Commit 9707faf

Browse files
authored
Merge pull request #4864 from oharboe/delete-gdb-valgrind-switches
flow: OPENROAD_EXE is a command; delete the GDB and VALGRIND switches
2 parents 7833f96 + a3327a9 commit 9707faf

16 files changed

Lines changed: 536 additions & 29 deletions

‎docs/contrib/MakefileContract.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ Several of the rules exist for them.
103103
unsets it before the sub-make, and the fallback fires there with an in-tree
104104
path that a flow supplying its own tools does not have (the "HERE BE
105105
DRAGONS" comment in `flow/scripts/variables.mk`).
106+
- **`OPENROAD_EXE` and `YOSYS_EXE` are commands, never assumed to be a
107+
single path.** A wrapper can come first: `OPENROAD_EXE="gdb --args
108+
openroad"`. A recipe or shell script runs the value as the shell splits
109+
it, Python splits it with `shlex.split`, and a check looks at its first
110+
word.
111+
*Breaks:* running a tool under a debugger, valgrind or perf needs a
112+
switch of its own for each tool.
106113
- **Only the Makefile sets `RESULTS_DIR`, `LOG_DIR`, `REPORTS_DIR` and
107114
`OBJECTS_DIR`.** It derives them from `WORK_HOME`, which a flow sets. A
108115
variable a flow adds never reuses one of these names.

‎docs/user/FAQS.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ For development purposes, it is a good practice to work on branches and leave ma
4848

4949
See how to update using the Metrics [guide](../contrib/Metrics.md).
5050

51+
## How do I run OpenROAD under gdb, valgrind or perf?
52+
53+
Put the tool in front of the binary in `OPENROAD_EXE`. Every rule runs
54+
`OPENROAD_EXE` as a command, sub-makes included, so it reaches every
55+
stage and the `open_*`, `gui_*` and `web_*` targets. Below, `openroad`
56+
stands for the binary the flow would run otherwise. The binary is the
57+
last word of `OPENROAD_EXE`: the flow asks it alone for its version when
58+
it writes the metrics, so the tool in front is not run for that.
59+
60+
```shell
61+
# a backtrace when OpenROAD crashes, its own exit code otherwise
62+
make DESIGN_CONFIG=./designs/nangate45/gcd/config.mk OPENROAD_EXE="gdb -batch -return-child-result -ex run -ex 'thread apply all bt' --args openroad" floorplan
63+
64+
# memory errors
65+
make DESIGN_CONFIG=./designs/nangate45/gcd/config.mk OPENROAD_EXE="valgrind openroad" floorplan
66+
67+
# a profile, written to perf.data
68+
make DESIGN_CONFIG=./designs/nangate45/gcd/config.mk OPENROAD_EXE="perf record -g -- openroad" floorplan
69+
70+
# an interactive debugger on a result
71+
make DESIGN_CONFIG=./designs/nangate45/gcd/config.mk OPENROAD_EXE="gdb --args openroad" open_floorplan
72+
```
73+
74+
A stage logs OpenROAD's output through a pipe, so use gdb in batch mode
75+
for a stage, as in the first example; an interactive session suits
76+
`open_*`, `gui_*` and `command`. `OPENROAD_EXE` can also come from the
77+
environment.
78+
5179
## How do I get better search results?
5280

5381
As quoted from [ReadTheDocs](https://docs.readthedocs.io/en/stable/server-side-search/syntax.html#special-queries), this documentation is powered by [Simple Query String](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html#) from [Elasticsearch](https://www.elastic.co/elasticsearch/). Here are some helpful patterns:

‎flow/Makefile‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ do-auto-memories: $(RESULTS_DIR)/memories_inferred.json
275275
$(foreach file,$(ADDITIONAL_MEMORIES),--memories $(file)) \
276276
$(foreach file,$(AUTO_MEMORIES_REGFILES),--regfile-spec $(file)) \
277277
$(foreach m,$(AUTO_MEMORIES_MACRO_PLACE),--macro-place $(m)) \
278-
$(if $(AUTO_MEMORIES_REGFILES),--openroad $(OPENROAD_EXE)) \
278+
$(if $(AUTO_MEMORIES_REGFILES),--openroad '$(subst ','\'',$(OPENROAD_EXE))') \
279279
$(foreach file,$(TECH_LEF) $(SC_LEF),--lef $(file))
280280

281281
$(RESULTS_DIR)/memories.json: $(RESULTS_DIR)/memories_inferred.json $(ADDITIONAL_MEMORIES) \
@@ -366,8 +366,15 @@ UNSET_VARS = for var in $(UNSET_VARIABLES_NAMES); do unset $$var; done
366366
# However, on some versions of make, MAKEFILE_LIST can be empty, so
367367
# don't expand it in that case.
368368
FILE_MAKEFILE ?= $(if $(firstword $(MAKEFILE_LIST)),--file=$(firstword $(MAKEFILE_LIST)),)
369-
SUB_MAKE = $(MAKE) $(foreach V,$(COMMAND_LINE_ARGS), $(if $($V),$V=$(shell echo "$($V)" | $(FLOW_HOME)/scripts/escape.sh),$V='')) --no-print-directory $(FILE_MAKEFILE) DESIGN_CONFIG=$(DESIGN_CONFIG)
370-
UNSET_AND_MAKE = @bash -c '$(UNSET_VARS); $(SUB_MAKE) $$@' --
369+
370+
# UNSET_VARS clears MAKEFLAGS too, so the sub-make gets the command-line
371+
# variables from SUB_MAKE, as command-line variables again. Each one is a
372+
# single shell word holding the value this make sees: single-quoted, with
373+
# ' escaped for the shell and $ for the sub-make. Only the shell running
374+
# the recipe parses it, so quotes, $ and spaces reach the sub-make intact.
375+
SUB_MAKE_ARG = '$(subst ','\'',$1=$(subst $$,$$$$,$($1)))'
376+
SUB_MAKE = $(MAKE) $(foreach V,$(COMMAND_LINE_ARGS),$(call SUB_MAKE_ARG,$V)) --no-print-directory $(FILE_MAKEFILE) DESIGN_CONFIG=$(DESIGN_CONFIG)
377+
UNSET_AND_MAKE = @bash -c '$(UNSET_VARS); "$$@"' -- $(SUB_MAKE)
371378

372379
$(OBJECTS_DIR)/copyright.txt:
373380
@$(OPENROAD_CMD) $(SCRIPTS_DIR)/noop.tcl

‎flow/scripts/escape.sh‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎flow/scripts/flow.sh‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ rm -f "$RESULTS_DIR/$1-failed.odb" "$RESULTS_DIR/$1-failed.sdc"
2121
eval "$OPENROAD_EXE $OPENROAD_ARGS -exit \"$SCRIPTS_DIR/noop.tcl\"" \
2222
>"$LOG_DIR/$1.tmp.log" 2>&1
2323

24-
$RUN_CMD --log "$(realpath "$LOG_DIR/$1.tmp.log")" --append --tee -- \
25-
$OPENROAD_CMD -no_splash "$SCRIPTS_DIR/run_step.tcl" -metrics "$LOG_DIR/$1.json"
24+
# OPENROAD_CMD is a command line, quotes included: a wrapper in
25+
# OPENROAD_EXE can have a quoted argument of its own, as in
26+
# gdb -ex 'thread apply all bt' --args openroad. The shell parses it,
27+
# as it does in a make recipe; plain word splitting would hand the
28+
# wrapper the quotes and the pieces.
29+
log="$(realpath "$LOG_DIR/$1.tmp.log")"
30+
eval "$RUN_CMD --log \"\$log\" --append --tee -- $OPENROAD_CMD" \
31+
'-no_splash "$SCRIPTS_DIR/run_step.tcl" -metrics "$LOG_DIR/$1.json"'
2632
)
2733

2834
# Log the hash for this step. The summary "make elapsed" in "make finish",

‎flow/scripts/memories/gen_memories.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ def main(argv: list[str] | None = None) -> int:
351351
p.add_argument(
352352
"--openroad",
353353
default=None,
354-
help="The openroad that runs generate_ram (OPENROAD_EXE).",
354+
help="The openroad command that runs generate_ram (OPENROAD_EXE), "
355+
"a wrapper first if any: gdb --args openroad.",
355356
)
356357
p.add_argument(
357358
"--lef",

‎flow/scripts/memories/gen_memories_test.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,14 @@ def test_fold_rule(self):
404404
# never an empty fold: 3 bits fold at most in two
405405
self.assertEqual(folds(600, 17.6, 600, 3), 2)
406406

407-
def _generate(self, bit_folds, outline):
407+
def test_openroad_is_a_command(self):
408+
# OPENROAD_EXE can start with a wrapper; generate_ram runs under it
409+
self._generate(8, "140000 140000 1000\n", "valgrind -q /opt/bin/openroad")
410+
self.assertEqual(len(self.argvs), 2)
411+
for argv in self.argvs:
412+
self.assertEqual(argv[:4], ["valgrind", "-q", "/opt/bin/openroad", "-exit"])
413+
414+
def _generate(self, bit_folds, outline, command="openroad"):
408415
# generate() with generate_ram stubbed: the generate script writes
409416
# the outline (dx dy dbu), the views script stops the run
410417
d = Path(self.tmp.name)
@@ -418,8 +425,10 @@ class Views(Exception):
418425
pass
419426

420427
scripts = []
428+
self.argvs = []
421429

422430
def openroad(cmd):
431+
self.argvs.append(cmd)
423432
script = Path(cmd[-1])
424433
scripts.append(script.name)
425434
if script.name.endswith(".generate.tcl"):
@@ -433,7 +442,7 @@ def openroad(cmd):
433442
):
434443
with self.assertRaises((Views, SystemExit)) as stop:
435444
gen_memories.regfile_ram.generate(
436-
spec, "rf", "inline", "openroad", [], d
445+
spec, "rf", "inline", command, [], d
437446
)
438447
return stop.exception, scripts
439448

‎flow/scripts/memories/regfile_ram.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import json
2626
import math
2727
import os
28+
import shlex
2829
import subprocess
2930
from pathlib import Path
3031

@@ -593,7 +594,9 @@ def build(spec_lines: list[list[str]]) -> tuple[float, float]:
593594
+ 'puts $f "[$die dx] [$die dy] [[ord::get_db_tech] getDbUnitsPerMicron]"\n'
594595
+ "close $f\n"
595596
)
596-
subprocess.check_call([openroad, "-exit", "-no_init", "-no_splash", str(gen)])
597+
subprocess.check_call(
598+
[*shlex.split(openroad), "-exit", "-no_init", "-no_splash", str(gen)]
599+
)
597600
width, height, _ = outline.read_text().split()
598601
return float(width), float(height)
599602

@@ -662,7 +665,9 @@ def build(spec_lines: list[list[str]]) -> tuple[float, float]:
662665
else:
663666
tcl += _MACRO_TCL.format(out=out_dir, block=block)
664667
views.write_text(tcl)
665-
subprocess.check_call([openroad, "-exit", "-no_init", "-no_splash", str(views)])
668+
subprocess.check_call(
669+
[*shlex.split(openroad), "-exit", "-no_init", "-no_splash", str(views)]
670+
)
666671
core_ports = _buses((out_dir / f"{block}.ports").read_text().splitlines())
667672
ram_ports = core_ports
668673
periphery = ""

‎flow/scripts/synth.sh‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
set -u -eo pipefail
33
mkdir -p $RESULTS_DIR $LOG_DIR $REPORTS_DIR $OBJECTS_DIR
44
touch $2
5-
$YOSYS_EXE -V > $(realpath $2)
6-
$PYTHON_EXE "$SCRIPTS_DIR/run_command.py" --log "$(realpath $2)" --append --tee -- \
7-
$YOSYS_EXE $YOSYS_FLAGS -c $1
5+
# YOSYS_EXE is a command line, quotes included: a wrapper in it can have
6+
# a quoted argument of its own. The shell parses it, as it does in a make
7+
# recipe.
8+
eval "$YOSYS_EXE -V" > "$(realpath $2)"
9+
eval '$PYTHON_EXE "$SCRIPTS_DIR/run_command.py" --log "$(realpath $2)" --append --tee --' \
10+
"$YOSYS_EXE $YOSYS_FLAGS" '-c "$1"'
811

912
# Log result hash, mirroring flow.sh's epilogue for OpenROAD stages.
1013
# genElapsedTime.py understands .odb/.rtlil/.v so it hashes the yosys

‎flow/scripts/variables.mk‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@ export RUN_CMD = $(PYTHON_EXE) $(FLOW_HOME)/scripts/run_command.py
126126
export OPENROAD_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/OpenROAD/bin/openroad)
127127
export OPENSTA_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/OpenROAD/bin/sta)
128128

129-
OPENROAD_IS_VALID := $(if $(OPENROAD_EXE),$(shell test -x $(OPENROAD_EXE) && echo "true"),)
129+
# OPENROAD_EXE and YOSYS_EXE are commands, not only paths: a wrapper can
130+
# come first, as in OPENROAD_EXE="gdb --args openroad" or
131+
# OPENROAD_EXE="valgrind openroad". The check is on the program the command
132+
# starts with, a path or a name on PATH. The binary is the last word.
133+
# The shell splits the command, as a recipe does, so a quoted path with
134+
# spaces in it is one program: OPENROAD_EXE="'/opt/my tools/openroad'".
135+
command_is_valid = $(if $(1),$(shell set -- $(1) && command -v "$$1" >/dev/null && echo "true"),)
136+
OPENROAD_IS_VALID := $(call command_is_valid,$(OPENROAD_EXE))
130137

131138
export OPENROAD_ARGS = -no_init -threads $(NUM_CORES) $(OR_ARGS)
132139
export OPENROAD_CMD = $(OPENROAD_EXE) -exit $(OPENROAD_ARGS)
@@ -136,7 +143,7 @@ export OPENROAD_WEB_CMD = $(OPENROAD_EXE) -web -threads $(NUM_CORES) $(OR_ARGS)
136143

137144
export YOSYS_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/yosys/bin/yosys)
138145

139-
YOSYS_IS_VALID := $(if $(YOSYS_EXE),$(shell test -x $(YOSYS_EXE) && echo "true"),)
146+
YOSYS_IS_VALID := $(call command_is_valid,$(YOSYS_EXE))
140147

141148
KEPLER_FORMAL_EXE ?= $(abspath $(FLOW_HOME)/../tools/install/kepler-formal/bin/kepler-formal)
142149
export KEPLER_FORMAL_EXE
@@ -190,7 +197,7 @@ RESULTS_V = $(notdir $(sort $(wildcard $(RESULTS_DIR)/*.v)))
190197
GDS_MERGED_FILE = $(RESULTS_DIR)/6_1_merged.$(STREAM_SYSTEM_EXT)
191198

192199
define get_variables
193-
$(foreach V, $(.VARIABLES),$(if $(filter-out $(1), $(origin $V)), $(if $(filter-out .% %QT_QPA_PLATFORM% KLAYOUT% OPENROAD_EXE OPENROAD_ARGS OPENROAD_CMD OPENROAD_NO_EXIT_CMD OPENROAD_GUI_CMD OPENROAD_WEB_CMD OPENROAD_IS_VALID OPENSTA% PYTHON% YOSYS% GENERATE_ABSTRACT_RULE% do-step% do-copy% OPEN_GUI% OPEN_GUI_SHORTCUT% SUB_MAKE% UNSET_VARS% export%, $(V)), $V$ )))
200+
$(foreach V, $(.VARIABLES),$(if $(filter-out $(1), $(origin $V)), $(if $(filter-out .% %QT_QPA_PLATFORM% KLAYOUT% OPENROAD_EXE OPENROAD_ARGS OPENROAD_CMD OPENROAD_NO_EXIT_CMD OPENROAD_GUI_CMD OPENROAD_WEB_CMD OPENROAD_IS_VALID command_is_valid OPENSTA% PYTHON% YOSYS% GENERATE_ABSTRACT_RULE% do-step% do-copy% OPEN_GUI% OPEN_GUI_SHORTCUT% SUB_MAKE% UNSET_VARS% export%, $(V)), $V$ )))
194201
endef
195202

196203
export UNSET_VARIABLES_NAMES := $(call get_variables,command% line environment% default automatic)

0 commit comments

Comments
 (0)