Skip to content

AST-based rule generation, #ifdef coverage, decision-oriented output - #10

Merged
stmcgovern merged 3 commits into
mainfrom
ifdef-reparse
Jun 10, 2026
Merged

stmcgovern merged 3 commits into
mainfrom
ifdef-reparse

Conversation

@stmcgovern

Copy link
Copy Markdown
Collaborator

Summary

  • gen_rules.py: regex → clang AST. All rule tables derived from stable headers via clang -ast-dump=json. Zero hand-maintained lists. When PyTorch adds stable APIs, regenerating Rules.h picks them up automatically.

  • #ifdef coverage. PPCallbacks::SourceRangeSkipped scans preprocessor-skipped regions for unstable patterns derived from rule tables. Fixes false "0 flags, PASS" on files with code behind #ifdef USE_ROCM. Handles all preprocessor conditions.

  • Decision-oriented audit output. Flags first (user's problem), rewrites summarized by kind with human-readable names (tool's problem). Relative paths. "ready for --mode=rewrite" when no flags remain.

Also: FindingKind::Flag split into UnstableType/UnstableRef/UnstableMethod, disjointness check in gen_rules.py, filter transitively included functions from ops extraction, kNamespaceRules table.

Test plan

  • ninja -C build -j30 builds clean
  • All tests pass including new ifdef-skipped-scan and method-catch-all
  • python3 scripts/gen_rules.py /path/to/pytorch src/Rules.h regenerates correctly
  • Audit custom_quickreduce.cu finds unstable API inside #ifdef USE_ROCM

Use clang -ast-dump=json to extract all declarations from stable
headers. Eliminates fragile regex patterns that missed noexcept,
const returns, and multi-line signatures.

What changed:
- extract_functions: FunctionDecl nodes, filtered by namespace and
  includedFrom to exclude transitively included helpers
- extract_methods: CXXMethodDecl from Tensor/Device/StableLibrary
- extract_enum_constants: EnumConstantDecl from ScalarType/DeviceType
- kStableMethodWhitelist: auto-generated (was hand-maintained)
- kNamespaceRules: derived from include rules (at::cuda:: mapping)

Only macro detection stays as text search (invisible to AST).
Exact namespace matching prevents false matches on similar names.
JSON parse errors produce clear diagnostics.
Two changes that strengthen the catch-all system:

1. Split FindingKind::Flag into UnstableType, UnstableRef, and
   UnstableMethod. Each catch-all now has its own kind, making
   findings filterable by category (UTYPE/UREF/UMETH in JSON).
   The overloaded Flag name (both a kind and an action) is resolved.

2. Override PPCallbacks::SourceRangeSkipped to detect unstable API
   inside #ifdef/#if blocks not compiled. Patterns derived from
   Rules.h tables at runtime — adapts when gen_rules.py regenerates.
   Handles all preprocessor conditions via a single callback.
   Fixes false "0 flags" on files with code behind #ifdef USE_ROCM.
Redesign text output to answer the user's question: "what do I need
to fix by hand?"

- Flags listed first (user's problem), rewrites summarized by kind
  (tool's problem) with human-readable names
- Relative paths when --project-root is set
- "ready for --mode=rewrite" when no flags remain
- (#ifdef) annotation on findings from skipped preprocessor regions
- Scanner patterns fully derived from rule tables — data_ptr added
  to kMethodRenameRules, template-arg variants (.method<) generated
  for all methods
- New tests: method-catch-all (exact count), ifdef-skipped-scan
- JSON output unchanged
@stmcgovern
stmcgovern requested a review from cleonard530 June 10, 2026 14:42
Comment thread scripts/gen_rules.py Resolved
@stmcgovern
stmcgovern merged commit 4f9f027 into main Jun 10, 2026
2 checks passed
@stmcgovern
stmcgovern deleted the ifdef-reparse branch June 10, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants