Skip to content

Python: bound shared CFG node reachability through basic blocks - #22699

Draft
yoff wants to merge 16 commits into
github:yoff/python-add-new-ssafrom
yoff:yoff-block-reachability-guardrail
Draft

yoff wants to merge 16 commits into
github:yoff/python-add-new-ssafrom
yoff:yoff-block-reachability-guardrail

Conversation

@yoff

@yoff yoff commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This draft prevents semmle.python.controlflow.internal.Cfg::ControlFlowNode.strictlyReaches from materializing a large transitive closure over individual CFG nodes. It computes cross-block reachability through the shared BasicBlock graph and uses node indices only within one block.

The PR has exactly two commits:

  1. add a direct inline-expectation shared-CFG reachability contract test;
  2. rewrite node reachability through BasicBlocks.

There are currently no production callers of this private shared-CFG method. This is a preventative peak-cardinality/memory guardrail, not a recovery of #21925's current fleet regression and not a demonstrated throughput optimization. No DCA was launched.

Shared-library use

The implementation uses the shared BasicBlock.Make support for canonical BasicBlock partitioning, cached node-to-block membership, and cached immediate BasicBlock successors.

The shared BasicBlock API does not currently expose a canonical pairwise reachability predicate. The Python facade therefore defines BasicBlock.strictlyReaches as the transitive closure of the shared immediate-successor relation. codeql.controlflow.ControlFlowReachability is a higher-level SSA/guard reachability analysis, not a replacement for generic BasicBlock-pair reachability.

The implementation adds no broad QL cache for the complete block-pair closure. The node wrapper remains inline so caller bindings can specialize the block closure.

Inline-only correctness contract

The fixture uses only standard inline expectation comments. There are no marker calls and no pair/truth whitelist in QL.

Fixture identifiers follow one generic structural convention:

reachability_<case>_source
reachability_<case>_destination  # $ reaches=<case>

or, for a negative relation:

reachability_<case>_destination = ...  # $ not-reaches=<case>

The QL query generically pairs store NameNodes whose identifiers share the same <case>, invokes shared-CFG source.strictlyReaches(destination), and emits the actual reaches or not-reaches tuple at the destination location. InlineExpectationsTest performs the repository-standard missing/spurious comparison against the comments; the generated expected file is empty when the contract holds.

Coverage includes:

  • forward and reverse ordering within one BasicBlock;
  • both branch arms reaching a join;
  • branch siblings not reaching each other;
  • a loop backedge and loop exit;
  • non-reachability across distinct scopes.

The focused test passes before and after. Exact decoded output is unchanged (CSV SHA-256 6bc0907e8f5ea1c0aeb7c301999be9ba2749eb8241fcf0ec2788830935754b2a), and codeql bqrs diff reports zero left-only and zero right-only rows for both cold and prewarmed controls. Full cold BQRS hashes differ only in metadata: before 2d4a8606a542a39c23d3e6762b162830d73c80d0b13f07350d953f510535f329, after 230bec3cec1afcfba27e2d51c44bf75a16ec7d5d6c584da7f6343c9c43e5b225.

Performance-measurement cache discipline

Cold baseline and candidate measurements used separate, fresh evaluator caches and isolated database copies. No evaluator cache directory was shared between revisions.

The BasicBlock-prewarmed comparison was a separate lifecycle control. Each revision first populated its own isolated cache with the same shared-CFG BasicBlock workload, then ran the inline-expectation node query against that same-revision cache. This distinguishes cold whole-query behavior from marginal behavior after the common block infrastructure exists; it does not let one revision inherit cache state from the other.

Exact local performance evidence

Measured with CodeQL CLI 2.26.3, isolated fresh databases and compilation/evaluation caches, serial evaluation (-j1 -M8192 --tuple-counting --keep-full-cache). Timing is secondary and shown only for transparency.

Cold direct query

Measure Before After Change
Direct reachability closure rows 2,932 node pairs 28 block pairs -99.0%
Maximum predicate result 2,932 285 -90.3%
Maximum pipeline cardinality 286 286 unchanged
Joined tuples 23,987 24,418 +431
Pipeline starts 802 832 +30
Predicate materializations 837 873 +36
Cache lookups 20 21 +1
Evaluator predicate time 524 ms 372 ms -152 ms (host-timed)
DIL size 793,866 B 834,547 B +5.12%
RA size 990,278 B 1,019,822 B +2.98%

Shared-CFG BasicBlock reachability prewarmed

The prewarm itself is deterministic-work identical on both commits: 21,605 joined tuples, 765 pipelines, 805 materializations, 20 CACHACA hits, maximum predicate result 285, and maximum pipeline cardinality 286.

Marginal node query Before After Change
Joined tuples 4,825 4,890 +65
Pipeline starts 147 153 +6
Predicate materializations 169 175 +6
Cache lookups 81 87 +6
Cache outcomes 1 CACHACA + 80 relation hits 1 CACHACA + 86 relation hits +6 relation hits
Maximum predicate result 2,932 251 -91.4%
Maximum pipeline cardinality 251 251 unchanged
Evaluator predicate time 81 ms 263 ms +182 ms (host-timed)

The rewrite removes the broad node-level materialization and bounds peak predicate cardinality. However, both cold and prewarmed controls show slightly higher joined-tuple, pipeline, materialization, and compiled-plan totals. Earlier bounded scaling controls with the same semantic pair contract likewise found no deterministic-work crossover even as the raw node closure grew much faster; the inline-only redesign preserves that conclusion in fresh cold and reuse controls.

Accordingly, this draft is framed only as preventative API design and cardinality containment—not as a demonstrated performance win.

Validation

  • focused inline-only test passes on commit 1 and commit 2;
  • 8/8 focused/shared-CFG tests pass;
  • cold and prewarmed outputs are exact;
  • QL formatting passes;
  • git diff --check passes;
  • exactly two commits over 0aad85c9e2bbbfc2b78935a8a98c1177b764dbb7;
  • no DCA was launched.

yoff and others added 14 commits August 20, 2026 08:54
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Preparatory refactor for the shared-CFG dataflow migration. Adds the
new Python SSA adapter additively, without changing any production
behaviour.

Library additions:

- semmle.python.dataflow.new.internal.SsaImpl — Python SSA
  implementation built on the new (shared) CFG. Mirrors the Java SSA
  adapter (java/ql/lib/semmle/code/java/dataflow/internal/SsaImpl.qll):
  an InputSig is defined in terms of positional (BasicBlock, int)
  variable references, and the shared
  codeql.ssa.Ssa::Make<Location, Cfg, Input> module is then
  instantiated.

  SourceVariable is the AST-level Py::Variable. Variable references
  are looked up via the new CFG facade's NameNode.defines/uses/deletes
  predicates (added in the preceding PR), which themselves are
  one-line bridges to AST-level Name.defines/uses/deletes.

  Implicit-entry definitions are inserted for non-local/global/builtin
  reads, captured variables, and (when needed) parameters.

Test additions:

- library-tests/dataflow-new-ssa/ — exercises the new SSA over a
  representative test corpus and checks expected def/use chains.

- library-tests/dataflow-new-ssa-vs-legacy/ — runs both new SSA and
  legacy ESSA over the same corpus and diffs the results, so any
  semantic divergence shows up as a test failure.

Production impact:

None. The new SSA adapter has zero callers in lib/ and src/ — the
legacy ESSA SSA (semmle/python/essa/*) remains the default. The
dataflow library is not migrated yet; that lands in a follow-up PR.

Verified by:
- All 367 lib + src + consistency-queries compile clean.
- All 641 ControlFlow + PointsTo + dataflow + essa + consistency
  library-tests pass.
- Both new dataflow-new-ssa[/vs-legacy] test packs pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- part of the ESSA adapter layer still refers to the raw SSA (now called Impl)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The adapter already instantiates Impl::MakeSsa<SsaInput>, so consume its cached public API for ordinary reads and phi inputs.

Synthetic normal-exit uses cannot be represented as SsaInput::Expr, so retain a narrow cached raw projection filtered to normal exits. Adjacency likewise remains a cached adapter boundary because MakeSsa does not expose it. This avoids adding broad cache policy to shared SSA.

Exact #156 to #158 DCA #39117 completed 48/48 with exact outputs, -0.748% aggregate query time, -0.541% median, and -3.29% DIL.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f3f2a493-1e3f-402e-b70d-4b558ac6bf29
@yoff
yoff force-pushed the yoff-block-reachability-guardrail branch from e33edf2 to ad84cb8 Compare September 29, 2026 14:21
@yoff yoff changed the title Python: compute shared CFG node reachability via basic blocks Python: bound shared CFG node reachability through basic blocks Sep 29, 2026
yoff and others added 2 commits September 29, 2026 16:42
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@yoff
yoff force-pushed the yoff-block-reachability-guardrail branch from ad84cb8 to 7753064 Compare September 29, 2026 14:52
@yoff
yoff force-pushed the yoff/python-add-new-ssa branch from 0aad85c to d82019d Compare November 23, 2026 16:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants