-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-148285: Allow recording uops after specializing uops #148373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adityakrmishra
wants to merge
4
commits into
python:main
Choose a base branch
from
adityakrmishra:fix-uop-recording-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+157
−6
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca81806
gh-148285: Allow recording uops after specializing uops
adityakrmishra 440b823
Apply reviewer suggestions, add tests, and regen cases
adityakrmishra 1bbac01
Add missing mypy return type annotation in test
adityakrmishra d9df27e
Revert generated C files to upstream state to fix CRLF
adityakrmishra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| """Tests for analyzer.py — specifically the add_macro() recording-uop placement rules. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this file and add any tests to https://github.com/python/cpython/blob/main/Lib/test/test_generated_cases.py |
||
|
|
||
| Run with: | ||
| cd Tools/cases_generator | ||
| python -m pytest test_analyzer.py -v | ||
| or: | ||
| python test_analyzer.py | ||
| """ | ||
|
|
||
| import sys | ||
| import os | ||
| import unittest | ||
| from typing import Any | ||
|
|
||
| # The cases_generator directory is not on sys.path when invoked from the repo | ||
| # root, so add it explicitly. | ||
| sys.path.insert(0, os.path.dirname(__file__)) | ||
|
|
||
| import parsing | ||
| from analyzer import analyze_forest | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def _parse(src: str) -> list[parsing.AstNode]: | ||
| """Parse a raw DSL string (no BEGIN/END markers needed) into an AST forest.""" | ||
| psr = parsing.Parser(src, filename="<test>") | ||
| nodes: list[parsing.AstNode] = [] | ||
| while node := psr.definition(): | ||
| nodes.append(node) # type: ignore[arg-type] | ||
| return nodes | ||
|
|
||
|
|
||
| def _analyze(src: str) -> Any: | ||
| """Parse *src* and run analyze_forest(); return the Analysis object.""" | ||
| return analyze_forest(_parse(src)) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Shared DSL fragments | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| # A minimal specializing op (tier == 1 because of the "specializing" annotation). | ||
| _SPECIALIZE_OP = """\ | ||
| specializing op(_SPECIALIZE_DUMMY, (counter/1, value -- value)) { | ||
| } | ||
| """ | ||
|
|
||
| # A minimal recording op: uses RECORD_VALUE → records_value == True. | ||
| _RECORD_OP = """\ | ||
| op(_RECORD_DUMMY, (value -- value)) { | ||
| RECORD_VALUE(PyStackRef_AsPyObjectBorrow(value)); | ||
| } | ||
| """ | ||
|
|
||
| # A plain (non-specializing, non-recording) worker op. | ||
| _WORKER_OP = """\ | ||
| op(_WORKER_DUMMY, (value -- res)) { | ||
| res = value; | ||
| } | ||
| """ | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Test class | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class TestAnalyzer(unittest.TestCase): | ||
|
|
||
| def test_recording_uop_position(self) -> None: | ||
| """Recording uops must be first, or immediately follow a specializing uop. | ||
|
|
||
| Case 1 — VALID: recording uop directly after specializing uop. | ||
| Case 2 — VALID: recording uop after specializing uop with a cache effect | ||
| (unused/1) between them; cache effects are transparent. | ||
| Case 3 — INVALID: recording uop after a plain (non-specializing) worker uop. | ||
| """ | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Case 1: _SPECIALIZE_DUMMY + _RECORD_DUMMY (no cache between them) | ||
| # ------------------------------------------------------------------ | ||
| src_valid_direct = ( | ||
| _SPECIALIZE_OP | ||
| + _RECORD_OP | ||
| + _WORKER_OP | ||
| + "macro(VALID_DIRECT) = _SPECIALIZE_DUMMY + _RECORD_DUMMY + _WORKER_DUMMY;\n" | ||
| ) | ||
| # Must not raise — the recording uop follows the specializing uop directly. | ||
| try: | ||
| _analyze(src_valid_direct) | ||
| except SyntaxError as exc: | ||
| self.fail( | ||
| f"Case 1 (valid: recording after specializing) raised unexpectedly: {exc}" | ||
| ) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Case 2: _SPECIALIZE_DUMMY + unused/1 + _RECORD_DUMMY | ||
| # A CacheEffect between them must be transparent. | ||
| # ------------------------------------------------------------------ | ||
| src_valid_with_cache = ( | ||
| _SPECIALIZE_OP | ||
| + _RECORD_OP | ||
| + _WORKER_OP | ||
| + "macro(VALID_CACHE) = _SPECIALIZE_DUMMY + unused/1 + _RECORD_DUMMY + _WORKER_DUMMY;\n" | ||
| ) | ||
| try: | ||
| _analyze(src_valid_with_cache) | ||
| except SyntaxError as exc: | ||
| self.fail( | ||
| f"Case 2 (valid: recording after specializing + cache) raised unexpectedly: {exc}" | ||
| ) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Case 3: _WORKER_DUMMY + _RECORD_DUMMY | ||
| # A recording uop after a non-specializing uop must be rejected. | ||
| # ------------------------------------------------------------------ | ||
| src_invalid = ( | ||
| _SPECIALIZE_OP | ||
| + _RECORD_OP | ||
| + _WORKER_OP | ||
| + "macro(INVALID) = _WORKER_DUMMY + _RECORD_DUMMY;\n" | ||
| ) | ||
| with self.assertRaises(SyntaxError) as ctx: | ||
| _analyze(src_invalid) | ||
|
|
||
| # Confirm the error message is the one we emit, not some unrelated error. | ||
| self.assertIn( | ||
| "Recording uop", | ||
| str(ctx.exception), | ||
| msg="Case 3: SyntaxError message should mention 'Recording uop'", | ||
| ) | ||
| self.assertIn( | ||
| "_RECORD_DUMMY", | ||
| str(ctx.exception), | ||
| msg="Case 3: SyntaxError message should name the offending uop", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exclude specializing too, so that the check is easier.