Skip to content

Commit d1523a8

Browse files
oharboeclaude
andcommitted
Add idempotency test scripts and diagnose-pr-head skill
Add three scripts to flow/util/ for systematically testing build determinism: - test_idempotency.sh: runs a design twice sequentially with different FLOW_VARIANTs and compares per-stage ODB hashes - drilldown_idempotency.sh: tests all A/B perturbation tricks in parallel (plain re-run, single-threaded, no ASLR, MALLOC_PERTURB_) using FLOW_VARIANT for independent output directories - sweep_idempotency.sh: runs idempotency tests across multiple designs in parallel, escalating from fast to slow Add .claude/commands/diagnose-pr-head.md skill that orchestrates the full diagnostic procedure: fetch PR info, checkout, build, sweep designs, drilldown with A/B tricks, bisect within stages, and report. These were used to diagnose that repair_timing (RSZ) is non-deterministic with threads > 1, while pin_access, global_route, repair_design, and detailed_placement are all deterministic. See The-OpenROAD-Project/OpenROAD#9781 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent 27b6251 commit d1523a8

4 files changed

Lines changed: 453 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
Diagnose pr-head CI instabilities for an ORFS pull request by checking build determinism.
2+
3+
Usage: /diagnose-pr-head <github-pr-url>
4+
5+
Background: See `tools/OpenROAD/docs/user/Bazel.md` "Debugging strategies" section for generic idempotency checking and whittle.py techniques. This skill orchestrates those techniques for a specific PR.
6+
7+
## Known non-determinism sources
8+
9+
- **`repair_timing` (RSZ) is non-deterministic with threads > 1** — the multi-threaded STA timing analysis produces different results between runs, causing different repair decisions. With `-threads 1` it is deterministic. Filed as https://github.com/The-OpenROAD-Project/OpenROAD/issues/9781
10+
- The following are **deterministic** (even multi-threaded): `pin_access`, `global_route`, `repair_design`, `detailed_placement`
11+
- The non-determinism in `repair_timing` propagates through subsequent incremental GRT, affecting final metrics
12+
13+
## Arguments
14+
15+
- `$ARGUMENTS` contains a GitHub PR URL (e.g., https://github.com/The-OpenROAD-Project/OpenROAD-flow-scripts/pull/3991)
16+
17+
## Procedure
18+
19+
### 1. Fetch PR information
20+
21+
Extract the PR number from `$ARGUMENTS` (the number after `/pull/`). Extract owner/repo from the URL.
22+
23+
```bash
24+
gh pr view <number> --repo <owner>/<repo> --json number,title,headRefName,headRefOid,baseRefName,state,statusCheckRollup
25+
gh pr checks <number> --repo <owner>/<repo>
26+
```
27+
28+
For failed Jenkins checks, get the details URL and fetch status context:
29+
```bash
30+
HEAD_SHA=$(gh pr view <number> --repo <owner>/<repo> --json headRefOid -q .headRefOid)
31+
gh api repos/<owner>/<repo>/statuses/$HEAD_SHA
32+
```
33+
34+
### 2. Identify failed designs
35+
36+
Parse CI output for design/platform pairs and metric threshold violations. If specific failing designs cannot be determined from CI output, proceed — the idempotency sweep will still reveal systemic issues.
37+
38+
### 3. Save current state and check out pr-head
39+
40+
```bash
41+
ORIG_BRANCH=$(git branch --show-current || git rev-parse HEAD)
42+
git stash
43+
gh pr checkout <number>
44+
```
45+
46+
Do NOT use `--recurse-submodules` or `git submodule update --init --recursive` — it's unnecessary and slow.
47+
48+
### 4. Build OpenROAD
49+
50+
Build from inside the `tools/OpenROAD` directory (it has its own MODULE.bazel). First ensure `src/sta` matches:
51+
```bash
52+
cd tools/OpenROAD
53+
git submodule update --init src/sta
54+
bazelisk run //:install
55+
```
56+
57+
**Common build issue:** If the build fails with STA API mismatches (e.g., `StringSeq` vs `StdStringSeq`, pointer vs value), the `src/sta` submodule inside `tools/OpenROAD` is likely at the wrong commit (left over from a previous checkout). Fix with:
58+
```bash
59+
cd tools/OpenROAD && git submodule update --init src/sta
60+
```
61+
Do NOT try to fix the API mismatches in OpenROAD source — just get the right STA version.
62+
63+
Verify the binary:
64+
```bash
65+
tools/install/OpenROAD/bin/openroad -version
66+
```
67+
68+
### 5. Idempotency sweep — escalate from fast to slow designs
69+
70+
Use `flow/util/test_idempotency.sh` to run each design twice and compare per-stage ODB hashes automatically:
71+
72+
```bash
73+
cd flow
74+
./util/test_idempotency.sh asap7/gcd
75+
./util/test_idempotency.sh nangate45/gcd
76+
./util/test_idempotency.sh <failing-platform>/<failing-design>
77+
```
78+
79+
Test in this order, stopping as soon as non-determinism is found:
80+
81+
1. **asap7/gcd** — fastest (~1-2 min), catches systemic issues
82+
2. **nangate45/gcd** — different platform, still fast
83+
3. **The specific failing design** from step 2 (e.g., nangate45/swerv_wrapper)
84+
4. If all deterministic so far, keep testing progressively larger designs from the CI job
85+
86+
The script exits 0 if deterministic, 1 if non-deterministic. If ALL designs pass, the failure is a real regression or flaky threshold, not non-determinism. Report and stop.
87+
88+
If ANY design fails, note the earliest diverging stage. Proceed to step 6.
89+
90+
### 6. Drilldown with A/B tricks
91+
92+
Run the drilldown script which tests all A/B perturbation tricks in parallel, each using a unique `FLOW_VARIANT` so they write to independent directories:
93+
94+
```bash
95+
cd flow
96+
./util/drilldown_idempotency.sh <platform/design>
97+
```
98+
99+
This tests: plain re-run, single-threaded (`NUM_CORES=1`), ASLR disabled, `MALLOC_PERTURB_=1`. All run in parallel.
100+
101+
If any variant diverges from the baseline, you've found an A/B pair.
102+
103+
### 7. Bisect within a stage's TCL script
104+
105+
The flow stages (e.g., `5_1_grt`) often run multiple OpenROAD commands internally. Use `make <stage>_issue` to create an isolated environment, then bisect by creating TCL scripts that include progressively more commands:
106+
107+
```bash
108+
make DESIGN_CONFIG=./designs/<platform>/<design>/config.mk <stage_script>_issue
109+
```
110+
111+
Inside the issue directory, create bisection TCL scripts adding one command at a time. Run each twice and compare hashes to find the specific command that introduces non-determinism. Test with `-threads 1` vs `-threads N` to determine if it's threading-related.
112+
113+
### 8. Restore and report
114+
115+
```bash
116+
git checkout $ORIG_BRANCH
117+
git stash pop 2>/dev/null || true
118+
```
119+
120+
Print a structured report:
121+
122+
```
123+
## pr-head Idempotency Diagnosis
124+
125+
**PR:** #<number> - <title>
126+
**PR Head Commit:** <sha>
127+
**Failed Designs:** <list>
128+
129+
### Idempotency Results
130+
131+
| Design | Stage | Run 1 Hash | Run 2 Hash | Match? |
132+
|--------|-------|------------|------------|--------|
133+
| asap7/gcd | 1_1_yosys | abc123... | abc123... | YES |
134+
| ... | ... | ... | ... | ... |
135+
136+
### Conclusion
137+
138+
- Build is DETERMINISTIC / NON-DETERMINISTIC at stage <X>.
139+
- Root cause: <command> is non-deterministic with threads > 1.
140+
```
141+
142+
## Important
143+
144+
- Always use `EQUIVALENCE_CHECK=0` to skip the eqy dependency
145+
- Use relative paths from the repo root
146+
- Use `git commit -s` for DCO sign-off on any commits
147+
- If the PR modifies `tools/OpenROAD`, the build step is essential
148+
- Do NOT recurse submodules from the ORFS root — only the top-level checkout is needed
149+
- Before building OpenROAD, ALWAYS run `git submodule update --init src/sta` inside `tools/OpenROAD/` to ensure `src/sta` matches the pinned commit. A stale `src/sta` from a previous checkout causes misleading API mismatch errors. NEVER try to "fix" these mismatches in OpenROAD source — the fix is always to get the right submodule version.
150+
- Don't whittle non-determinism bugs if the reproducer is already fast (<10 min). Whittling can reduce the probability of triggering the bug.

‎flow/util/drilldown_idempotency.sh‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/bin/bash
2+
# Drilldown into idempotency for a single design using every A/B trick
3+
# in the book. Each trick uses a unique FLOW_VARIANT so all tests run
4+
# in parallel with independent output directories.
5+
#
6+
# Usage: ./util/drilldown_idempotency.sh <platform/design>
7+
# e.g.: ./util/drilldown_idempotency.sh asap7/gcd
8+
#
9+
# Make variables used for perturbation (from variables.mk):
10+
# FLOW_VARIANT — output directory selector (logs/results/objects/reports)
11+
# NUM_CORES — thread count, feeds into OPENROAD_ARGS via -threads
12+
# OR_ARGS — extra args appended to OPENROAD_ARGS
13+
# OPENROAD_EXE — path to openroad binary (can be a wrapper)
14+
#
15+
# Environment perturbations:
16+
# MALLOC_PERTURB_ — fills freed memory to detect uninitialized reads
17+
# setarch -R — disables ASLR
18+
#
19+
# Exit code: 0 if all match baseline, 1 if any trick reveals divergence.
20+
21+
set -euo pipefail
22+
23+
if [ $# -lt 1 ]; then
24+
echo "Usage: $0 <platform/design>" >&2
25+
exit 2
26+
fi
27+
28+
DESIGN="$1"
29+
DESIGN_CONFIG="./designs/${DESIGN}/config.mk"
30+
PLATFORM=$(echo "$DESIGN" | cut -d/ -f1)
31+
DESIGN_NAME=$(echo "$DESIGN" | cut -d/ -f2)
32+
33+
if [ ! -f "$DESIGN_CONFIG" ]; then
34+
echo "ERROR: Design config not found: $DESIGN_CONFIG" >&2
35+
exit 2
36+
fi
37+
38+
# Common make arguments shared by all variants
39+
MAKE="make DESIGN_CONFIG=$DESIGN_CONFIG EQUIVALENCE_CHECK=0"
40+
41+
extract_hashes() {
42+
local variant="$1"
43+
python3 util/genElapsedTime.py -d "logs/${PLATFORM}/${DESIGN_NAME}/${variant}/" 2>/dev/null \
44+
| awk 'NF>=4 && $4 ~ /^[0-9a-f]/ {print $1, $4}'
45+
}
46+
47+
echo "Drilldown idempotency: $DESIGN"
48+
echo ""
49+
50+
# --- Define variants ---
51+
# Each entry: "variant_name|shell command"
52+
# Uses make VAR=val syntax for readability; env wrappers where needed.
53+
54+
VARIANTS=(
55+
# Baseline: two identical runs
56+
"plain_a|$MAKE FLOW_VARIANT=plain_a finish"
57+
"plain_b|$MAKE FLOW_VARIANT=plain_b finish"
58+
59+
# Single-threaded: eliminates thread scheduling non-determinism
60+
"thread1|$MAKE FLOW_VARIANT=thread1 NUM_CORES=1 finish"
61+
62+
# ASLR disabled: eliminates pointer-address-dependent ordering
63+
"noaslr|setarch $(uname -m) -R $MAKE FLOW_VARIANT=noaslr finish"
64+
65+
# Memory perturbation: detects use of uninitialized memory
66+
"mallocperturb|env MALLOC_PERTURB_=1 $MAKE FLOW_VARIANT=mallocperturb finish"
67+
)
68+
69+
echo "Variants to test:"
70+
for entry in "${VARIANTS[@]}"; do
71+
name="${entry%%|*}"
72+
echo " - $name"
73+
done
74+
echo ""
75+
76+
# --- Clean all variants ---
77+
for entry in "${VARIANTS[@]}"; do
78+
name="${entry%%|*}"
79+
$MAKE FLOW_VARIANT="$name" clean_all >/dev/null 2>&1 || true
80+
done
81+
82+
# --- Launch all in parallel ---
83+
declare -A PIDS LOGS
84+
for entry in "${VARIANTS[@]}"; do
85+
name="${entry%%|*}"
86+
cmd="${entry#*|}"
87+
logfile="/tmp/drill_${PLATFORM}_${DESIGN_NAME}_${name}.log"
88+
LOGS[$name]="$logfile"
89+
echo "Starting: $name"
90+
bash -c "$cmd" > "$logfile" 2>&1 &
91+
PIDS[$name]=$!
92+
done
93+
94+
# --- Wait for all ---
95+
echo ""
96+
echo "Waiting for all variants to complete..."
97+
VARIANT_NAMES=()
98+
for entry in "${VARIANTS[@]}"; do
99+
name="${entry%%|*}"
100+
VARIANT_NAMES+=("$name")
101+
if wait "${PIDS[$name]}"; then
102+
echo " $name: completed"
103+
else
104+
echo " $name: FAILED (check ${LOGS[$name]})"
105+
fi
106+
done
107+
108+
# --- Extract hashes ---
109+
echo ""
110+
echo "========================================"
111+
echo "Hash results"
112+
echo "========================================"
113+
114+
declare -A HASH_FILES
115+
for name in "${VARIANT_NAMES[@]}"; do
116+
hfile="/tmp/drill_hashes_${PLATFORM}_${DESIGN_NAME}_${name}.txt"
117+
extract_hashes "$name" > "$hfile"
118+
HASH_FILES[$name]="$hfile"
119+
echo ""
120+
echo "--- $name ---"
121+
cat "$hfile"
122+
done
123+
124+
# --- Compare against baseline (plain_a) ---
125+
echo ""
126+
echo "========================================"
127+
echo "Comparisons against baseline (plain_a)"
128+
echo "========================================"
129+
130+
BASELINE="${HASH_FILES[plain_a]}"
131+
FAIL=0
132+
DIVERGENT=()
133+
134+
for name in "${VARIANT_NAMES[@]}"; do
135+
[ "$name" = "plain_a" ] && continue
136+
echo ""
137+
echo "--- plain_a vs $name ---"
138+
if diff -q "$BASELINE" "${HASH_FILES[$name]}" >/dev/null 2>&1; then
139+
echo "MATCH"
140+
else
141+
echo "DIVERGENT!"
142+
diff "$BASELINE" "${HASH_FILES[$name]}" || true
143+
FAIL=1
144+
DIVERGENT+=("$name")
145+
fi
146+
done
147+
148+
# --- Summary ---
149+
echo ""
150+
echo "========================================"
151+
if [ $FAIL -eq 0 ]; then
152+
echo "DRILLDOWN: $DESIGN — all variants match. Build is robust."
153+
else
154+
echo "DRILLDOWN: $DESIGN — DIVERGENCE in: ${DIVERGENT[*]}"
155+
echo ""
156+
echo "Next steps:"
157+
echo " 1. Identify which stage first diverges in: ${DIVERGENT[*]}"
158+
echo " 2. Create issue: make DESIGN_CONFIG=$DESIGN_CONFIG <stage>_issue"
159+
echo " 3. Whittle: python3 tools/OpenROAD/etc/whittle.py --error_string NON_DETERMINISTIC ..."
160+
fi
161+
echo "========================================"
162+
exit $FAIL

‎flow/util/sweep_idempotency.sh‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
# Sweep multiple designs for idempotency, running ALL designs in parallel.
3+
# Each design runs two flows (idem_run1, idem_run2) via FLOW_VARIANT,
4+
# and each design uses its own results/logs directory, so everything
5+
# is fully parallel.
6+
#
7+
# Usage: ./util/sweep_idempotency.sh [extra_design1 extra_design2 ...]
8+
# e.g.: ./util/sweep_idempotency.sh nangate45/swerv_wrapper
9+
#
10+
# Always tests asap7/gcd and nangate45/gcd first, plus any extra designs.
11+
# Exit code: 0 if all deterministic, 1 if any non-determinism found.
12+
13+
set -euo pipefail
14+
15+
DESIGNS=("asap7/gcd" "nangate45/gcd" "$@")
16+
17+
# Remove duplicates while preserving order
18+
UNIQUE_DESIGNS=()
19+
declare -A SEEN
20+
for d in "${DESIGNS[@]}"; do
21+
if [ -z "${SEEN[$d]:-}" ]; then
22+
UNIQUE_DESIGNS+=("$d")
23+
SEEN[$d]=1
24+
fi
25+
done
26+
27+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
28+
29+
echo "Running idempotency tests for ${#UNIQUE_DESIGNS[@]} designs IN PARALLEL:"
30+
for d in "${UNIQUE_DESIGNS[@]}"; do echo " - $d"; done
31+
echo ""
32+
33+
# Launch all designs in parallel
34+
declare -A PIDS
35+
declare -A LOGS
36+
for design in "${UNIQUE_DESIGNS[@]}"; do
37+
logfile="/tmp/idempotency_sweep_${design//\//_}.log"
38+
LOGS[$design]="$logfile"
39+
"$SCRIPT_DIR/test_idempotency.sh" "$design" > "$logfile" 2>&1 &
40+
PIDS[$design]=$!
41+
done
42+
43+
# Wait for all and collect results
44+
FAIL=0
45+
for design in "${UNIQUE_DESIGNS[@]}"; do
46+
echo "========================================"
47+
echo "Waiting for: $design (PID ${PIDS[$design]})"
48+
if wait "${PIDS[$design]}"; then
49+
echo "PASS: $design"
50+
else
51+
echo "FAIL: $design — NON-DETERMINISTIC"
52+
FAIL=1
53+
fi
54+
echo "--- Output ---"
55+
cat "${LOGS[$design]}"
56+
echo ""
57+
done
58+
59+
echo "========================================"
60+
if [ $FAIL -eq 0 ]; then
61+
echo "SWEEP RESULT: All ${#UNIQUE_DESIGNS[@]} designs are DETERMINISTIC"
62+
else
63+
echo "SWEEP RESULT: NON-DETERMINISM DETECTED in one or more designs"
64+
fi
65+
echo "========================================"
66+
exit $FAIL

0 commit comments

Comments
 (0)