|
| 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. |
0 commit comments