Skip to content

Commit 34884ae

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 b9f42fe commit 34884ae

4 files changed

Lines changed: 502 additions & 0 deletions

File tree

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
## Known non-determinism sources
6+
7+
- **`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
8+
- The following are **deterministic** (even multi-threaded): `pin_access`, `global_route`, `repair_design`, `detailed_placement`
9+
- The non-determinism in `repair_timing` propagates through subsequent incremental GRT, affecting final metrics
10+
11+
## Arguments
12+
13+
- `$ARGUMENTS` contains a GitHub PR URL (e.g., https://github.com/The-OpenROAD-Project/OpenROAD-flow-scripts/pull/3991)
14+
15+
## Procedure
16+
17+
### 1. Fetch PR information
18+
19+
Extract the PR number from `$ARGUMENTS` (the number after `/pull/`). Extract owner/repo from the URL.
20+
21+
```bash
22+
gh pr view <number> --repo <owner>/<repo> --json number,title,headRefName,headRefOid,baseRefName,state,statusCheckRollup
23+
gh pr checks <number> --repo <owner>/<repo>
24+
```
25+
26+
For failed Jenkins checks, get the details URL and fetch status context:
27+
```bash
28+
HEAD_SHA=$(gh pr view <number> --repo <owner>/<repo> --json headRefOid -q .headRefOid)
29+
gh api repos/<owner>/<repo>/statuses/$HEAD_SHA
30+
```
31+
32+
### 2. Identify failed designs
33+
34+
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.
35+
36+
### 3. Save current state and check out pr-head
37+
38+
```bash
39+
ORIG_BRANCH=$(git branch --show-current || git rev-parse HEAD)
40+
git stash
41+
gh pr checkout <number>
42+
```
43+
44+
Do NOT use `--recurse-submodules` or `git submodule update --init --recursive` — it's unnecessary and slow.
45+
46+
### 4. Build OpenROAD
47+
48+
Build from inside the `tools/OpenROAD` directory (it has its own MODULE.bazel). First ensure `src/sta` matches:
49+
```bash
50+
cd tools/OpenROAD
51+
git submodule update --init src/sta
52+
bazelisk run //:install
53+
```
54+
55+
**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:
56+
```bash
57+
cd tools/OpenROAD && git submodule update --init src/sta
58+
```
59+
Do NOT try to fix the API mismatches in OpenROAD source — just get the right STA version.
60+
61+
Verify the binary:
62+
```bash
63+
tools/install/OpenROAD/bin/openroad -version
64+
```
65+
66+
### 5. Idempotency sweep — escalate from fast to slow designs
67+
68+
Use `flow/util/test_idempotency.sh` to run each design twice and compare per-stage ODB hashes automatically:
69+
70+
```bash
71+
cd flow
72+
./util/test_idempotency.sh asap7/gcd
73+
./util/test_idempotency.sh nangate45/gcd
74+
./util/test_idempotency.sh <failing-platform>/<failing-design>
75+
```
76+
77+
Test in this order, stopping as soon as non-determinism is found:
78+
79+
1. **asap7/gcd** — fastest (~1-2 min), catches systemic issues
80+
2. **nangate45/gcd** — different platform, still fast
81+
3. **The specific failing design** from step 2 (e.g., nangate45/swerv_wrapper)
82+
4. If all deterministic so far, keep testing progressively larger designs from the CI job
83+
84+
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.
85+
86+
If ANY design fails, note the earliest diverging stage. Proceed to step 6.
87+
88+
### 6. Drilldown with A/B tricks
89+
90+
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:
91+
92+
```bash
93+
cd flow
94+
./util/drilldown_idempotency.sh <platform/design>
95+
```
96+
97+
This tests: plain re-run, single-threaded (`NUM_CORES=1`), ASLR disabled, `MALLOC_PERTURB_=1`. All run in parallel.
98+
99+
If any variant diverges from the baseline, you've found an A/B pair.
100+
101+
### 7. Bisect within a stage's TCL script
102+
103+
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:
104+
105+
```bash
106+
make DESIGN_CONFIG=./designs/<platform>/<design>/config.mk <stage_script>_issue
107+
```
108+
109+
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.
110+
111+
Use `FLOW_VARIANT` overrides for each run so they write to separate directories and can run in parallel.
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+
## Reference: checking build determinism
143+
144+
The flow logs SHA1 hashes of each stage's `.odb` via `genElapsedTime.py`:
145+
146+
python3 flow/util/genElapsedTime.py -d flow/logs/<platform>/<design>/base/
147+
148+
The `sha1sum .odb [0:20)` column is the hash to compare. Use `FLOW_VARIANT` to run A/B tests in parallel with independent output directories.
149+
150+
To diff divergent ODB files as human-readable DEF:
151+
152+
openroad -exit -cmd "read_db run1.odb; write_def run1.def"
153+
openroad -exit -cmd "read_db run2.odb; write_def run2.def"
154+
diff run1.def run2.def
155+
156+
## Reference: common sources of non-determinism
157+
158+
- **Thread scheduling** — parallel algorithms (OpenMP `schedule(dynamic)`, thread pools) accumulate results in arrival order. Test with `-threads 1`.
159+
- **Unordered container iteration** — `std::unordered_map`/`set` iterate in implementation-defined order. Replace with `std::map`/`set` or sort keys before iterating.
160+
- **Pointer-based sorting** — sorting by pointer value varies due to ASLR. Test with `setarch $(uname -m) -R`.
161+
- **Uninitialized memory** — use of freed/uninitialized memory. Test with `MALLOC_PERTURB_=1`.
162+
- **Random seeds** — algorithms using `rand()` or `std::mt19937` without a fixed seed.
163+
164+
## Reference: A/B techniques
165+
166+
Use these make variables to perturb runs (each with its own `FLOW_VARIANT`):
167+
168+
| Technique | Make override | What it tests |
169+
|-----------|--------------|---------------|
170+
| Single thread | `NUM_CORES=1` | Thread scheduling |
171+
| No ASLR | wrap with `setarch $(uname -m) -R` | Pointer-dependent ordering |
172+
| Malloc perturb | `env MALLOC_PERTURB_=1` | Uninitialized memory |
173+
174+
## Reference: whittle.py for non-determinism
175+
176+
For non-determinism the "error" is that two runs differ. Create a wrapper that runs the stage twice, compares hashes, and prints a marker:
177+
178+
```bash
179+
#!/bin/bash
180+
STAGE_ODB=results/<platform>/<design>/base/<stage>.odb
181+
make DESIGN_CONFIG=... EQUIVALENCE_CHECK=0 <stage>
182+
HASH1=$(sha1sum $STAGE_ODB | cut -c1-40)
183+
make DESIGN_CONFIG=... EQUIVALENCE_CHECK=0 clean_<stage> <stage>
184+
HASH2=$(sha1sum $STAGE_ODB | cut -c1-40)
185+
[ "$HASH1" != "$HASH2" ] && echo "NON_DETERMINISTIC"
186+
```
187+
188+
Then: `whittle.py --error_string NON_DETERMINISTIC --step ./wrapper.sh --base_db_path <input>.odb`
189+
190+
Only whittle if the reproducer takes >10 minutes. For fast reproducers, whittling can make the bug harder to trigger.
191+
192+
## Important
193+
194+
- Always use `EQUIVALENCE_CHECK=0` to skip the eqy dependency
195+
- Use relative paths from the repo root
196+
- Use `git commit -s` for DCO sign-off on any commits
197+
- If the PR modifies `tools/OpenROAD`, the build step is essential
198+
- Do NOT recurse submodules from the ORFS root — only the top-level checkout is needed
199+
- 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.

‎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

0 commit comments

Comments
 (0)