Skip to content

Add native runtime function specialization with exact-type fallback - #138

Open
sharktide wants to merge 3 commits into
mainfrom
optimizer/native-runtime
Open

sharktide wants to merge 3 commits into
mainfrom
optimizer/native-runtime

Conversation

@sharktide

@sharktide sharktide commented Dec 6, 2026 •

Copy link
Copy Markdown
Owner

Compiler-recognized IR for explicit native=True functions: integer sum, Unicode length reduction and ordered substring matching. Original Python fallback for unsupported runtime values, overflow, overridden len, keyword calls and tracing. C kernels hold the GIL, avoid callbacks, and use checked arithmetic. Unsupported function shapes produce diagnostics. Includes randomized differential tests and sanitizer coverage. This is a narrowly scoped initial native runtime backend, not arbitrary AOT compilation.

Summary by Sourcery

Introduce an experimental, explicitly enabled native runtime path for a narrow set of supported functions while preserving their Python implementations as fallbacks.

New Features:

  • Add experimental opt-in native runtime specialization for supported integer and Unicode reductions and ordered substring matching, with Python fallback for unsupported calls and values.

Enhancements:

  • Reject unsupported native function shapes and incompatible native optimization options with compilation diagnostics.

CI:

  • Expand the native sanitizer workflow to run all native-related test modules.

Documentation:

  • Document native runtime supported shapes, fallback conditions, limits, and opt-in behavior in the README and a new guide.

Tests:

  • Add native runtime tests covering Unicode, overflow, fallback behavior, input variants, and randomized differential equivalence.

@sourcery-ai

sourcery-ai Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Expose compiler-recognized native kernel dispatch through the runtime wrapper, retaining the original function for unsupported calls and runtime conditions.

  • restructuredpython/native_runtime.py L1-49

Recognize the supported function shapes, lower opted-in functions to runtime decorators, and validate generated-name bindings across combined source.

  • restructuredpython/native_compiler.py L1-194

Uses of the API

Recognize native optimization directives during parsing and lower eligible functions, while preserving existing unrolling behavior.

  • restructuredpython/parser.py L536-554 (+3 small edits)

Validate native generated-name bindings after include expansion without reserving compiler-looking names in unchanged Python source.

  • restructuredpython/restructuredpython.py L28, L116-119, L148-152

Implementation

Implement bounded, exact-built-in C kernels for string-length sums, integer sums with overflow fallback, and ordered substring matching, including signal checks.

  • restructuredpython/lib/compiler.c L11, L258-381

Configuration

Run all native test modules under the sanitizer workflow.

  • .github/workflows/native-sanitizer.yml L37

Documentation

Document the experimental specialization scope, supported patterns, fallback conditions, and runtime safety limits.

  • README.md L84-87
  • docs/native-runtime.md L1-40

Tests

Cover native dispatch, Python fallback, Unicode and overflow behavior, differential equivalence, diagnostics, and generated-name validation.

  • tests/test_native_runtime.py L1-129

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 6 issues


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Py_RETURN_NOTIMPLEMENTED;
}
Py_ssize_t total = 0;
for (Py_ssize_t i = 0; i < n; ++i) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Long native calls block threads and interrupts

When a native reduction or search processes a large input while a signal is pending, the C loops hold the GIL and do not check pending signals, so other Python threads cannot run and KeyboardInterrupt is delayed until the kernel finishes.

Release the GIL where safe and check for pending signals during the scans.

Also at restructuredpython/lib/compiler.c:305, restructuredpython/lib/compiler.c:334.

Comment thread restructuredpython/native_runtime.py Resolved
Comment thread restructuredpython/native_compiler.py Resolved
Comment thread restructuredpython/native_compiler.py Resolved
Comment thread restructuredpython/native_runtime.py Resolved
Comment thread tests/test_native_runtime.py Resolved

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant