-
Notifications
You must be signed in to change notification settings - Fork 41
feat: Enable panic stack traces in the emulator #2271
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
afa116f
Basic stack trace rendering
tatiana-s f87f078
Simplify debug arg passing + minimal opt in temp test
tatiana-s 10d6c8d
Add proper tests + require minimal opt + improve pretty printing
tatiana-s aca5b63
Merge remote-tracking branch 'origin/main' into ts/debug-trace
tatiana-s 83cea24
Add recursion test
tatiana-s 5bea6ec
Address comments
tatiana-s 0a3e68b
Merge remote-tracking branch 'origin/main' into ts/debug-trace
tatiana-s df82130
Merge remote-tracking branch 'origin/main' into ts/debug-trace
tatiana-s a939e78
Merge remote-tracking branch 'origin/main' into ts/debug-trace
tatiana-s f10cae7
Restore uv.lock to match main
tatiana-s e87e517
Add comment
tatiana-s 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
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,70 @@ | ||
| """Render Selene emulator panic stack traces.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from guppylang_internals.diagnostic import DiagnosticsRenderer | ||
| from guppylang_internals.span import Loc, SourceMap, Span | ||
|
|
||
| if TYPE_CHECKING: | ||
| from selene_sim.stack_trace import StackTrace | ||
|
|
||
|
|
||
| def render_stack_trace(stack_trace: StackTrace | None, message: str) -> str | None: | ||
| """Render a Selene stack trace as source-annotated snippets. | ||
|
|
||
| Each symbolized frame is rendered as its own code snippet. Returns `None` if no | ||
| frame could be resolved to a readable source location, e.g. because debug info | ||
| wasn't emitted. | ||
| """ | ||
| if stack_trace is None: | ||
| return None | ||
|
|
||
| source = SourceMap() | ||
| blocks: list[str] = [] | ||
|
|
||
| def append_frame_snippet(span: Span, label: str | None, function_name: str) -> None: | ||
| header = ( | ||
| f'File "{span.start.file}", line {span.start.line}, in {function_name}:' | ||
| ) | ||
| renderer = DiagnosticsRenderer(source) | ||
| try: | ||
| renderer.render_snippet( | ||
| span, | ||
| label, | ||
| span.end.line, | ||
| is_primary=True, | ||
| prefix_lines=renderer.PREFIX_ERROR_CONTEXT_LINES, | ||
| ) | ||
| except Exception: # noqa: BLE001 | ||
| # The source may have changed since compilation, so the recorded | ||
| # location might no longer be renderable. Fall back to the header | ||
| # rather than masking the panic we're reporting. | ||
| blocks.append(header) | ||
| return | ||
| blocks.append(f"{header}\n" + "\n".join(renderer.buffer)) | ||
|
|
||
| is_first_frame = True | ||
|
|
||
| for entry in stack_trace.entries: | ||
| for symbol in entry.symbols: | ||
| if symbol.filename not in source.sources: | ||
| source.add_file(symbol.filename) | ||
| if not source.sources[symbol.filename]: | ||
| # Source file isn't available, skip this frame. | ||
| continue | ||
|
|
||
| loc = Loc(symbol.filename, symbol.line, symbol.column) | ||
| span = Span(loc, loc.shift_right(1)) | ||
| append_frame_snippet( | ||
| span, message if is_first_frame else None, symbol.function_name | ||
| ) | ||
| is_first_frame = False | ||
|
|
||
| if not blocks: | ||
| return None | ||
|
|
||
| trace = "\n\n".join(blocks) | ||
| indented_trace = "\n".join(f" {line}" for line in trace.splitlines()) | ||
| return f"Guppy traceback (most recent call last):\n{indented_trace}\n" |
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
Empty file.
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,9 @@ | ||
| Panic (#1001): Always panics | ||
| Guppy traceback (most recent call last): | ||
| File "$PATH_TO_FILE/always_panic.py", line 7, in stack_traces.always_panic.main: | ||
| | | ||
| 5 | @guppy | ||
| 6 | def main() -> None: | ||
| 7 | panic("Always panics") | ||
| | ^ Always panics | ||
|
|
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,10 @@ | ||
| from guppylang import guppy | ||
| from guppylang.std.platform import panic | ||
|
|
||
|
|
||
| @guppy | ||
| def main() -> None: | ||
| panic("Always panics") | ||
|
|
||
|
|
||
| main.with_minimal_opt().emulator(n_qubits=1, debug_mode=True).run() |
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,9 @@ | ||
| Panic (#1002): Float value too big to convert to int of given width (64) | ||
| Guppy traceback (most recent call last): | ||
| File "$PATH_TO_FILE/global_panic_unwrap_error.py", line 10, in stack_traces.global_panic_unwrap_error.main: | ||
| | | ||
| 8 | # through a global compiler function, therefore testing stack traces for panics | ||
| 9 | # that are not directly annotated. | ||
| 10 | return int(0.0 / 0.0) | ||
| | ^ Float value too big to convert to int of given width (64) | ||
|
|
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,13 @@ | ||
| from guppylang import guppy | ||
| from guppylang.std.num import int | ||
|
|
||
|
|
||
| @guppy | ||
| def main() -> int: | ||
| # This should internally use the `UnwrapOpCompiler` which relies on a panic built | ||
| # through a global compiler function, therefore testing stack traces for panics | ||
| # that are not directly annotated. | ||
| return int(0.0 / 0.0) | ||
|
|
||
|
|
||
| main.with_minimal_opt().emulator(n_qubits=1, debug_mode=True).run() |
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,23 @@ | ||
| Panic (#1001): Array index out of bounds | ||
| Guppy traceback (most recent call last): | ||
| File "$PATH_TO_FILE/test_helper.py", line 8, in stack_traces.test_helper.array_with_three_elements: | ||
| | | ||
| 6 | def array_with_three_elements(x: int) -> int: | ||
| 7 | arr = array(1, 2, 3) | ||
| 8 | return arr[x] | ||
| | ^ Array index out of bounds | ||
|
|
||
| File "$PATH_TO_FILE/multi_file_out_of_bounds.py", line 8, in stack_traces.multi_file_out_of_bounds.array_out_of_bounds: | ||
| | | ||
| 6 | @guppy | ||
| 7 | def array_out_of_bounds(x: int) -> int: | ||
| 8 | return array_with_three_elements(x) | ||
| | ^ | ||
|
|
||
| File "$PATH_TO_FILE/multi_file_out_of_bounds.py", line 13, in stack_traces.multi_file_out_of_bounds.main: | ||
| | | ||
| 11 | @guppy | ||
| 12 | def main() -> None: | ||
| 13 | array_out_of_bounds(5) | ||
| | ^ | ||
|
|
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,16 @@ | ||
| from guppylang import guppy | ||
|
|
||
| from tests.stack_traces.test_helper import array_with_three_elements | ||
|
|
||
|
|
||
| @guppy | ||
| def array_out_of_bounds(x: int) -> int: | ||
| return array_with_three_elements(x) | ||
|
|
||
|
|
||
| @guppy | ||
| def main() -> None: | ||
| array_out_of_bounds(5) | ||
|
|
||
|
|
||
| main.with_minimal_opt().emulator(n_qubits=1, debug_mode=True).run() |
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,37 @@ | ||
| Panic (#1001): No more qubits available to allocate. | ||
| Guppy traceback (most recent call last): | ||
| File "$PATH_TO_FILE/recursive.py", line 7, in stack_traces.recursive.recursive_allocate: | ||
| | | ||
| 5 | @guppy | ||
| 6 | def recursive_allocate(source: qubit) -> None: | ||
| 7 | q = qubit() | ||
| | ^ No more qubits available to allocate. | ||
|
|
||
| File "$PATH_TO_FILE/recursive.py", line 9, in stack_traces.recursive.recursive_allocate: | ||
| | | ||
| 7 | q = qubit() | ||
| 8 | cx(source, q) | ||
| 9 | recursive_allocate(q) | ||
| | ^ | ||
|
|
||
| File "$PATH_TO_FILE/recursive.py", line 9, in stack_traces.recursive.recursive_allocate: | ||
| | | ||
| 7 | q = qubit() | ||
| 8 | cx(source, q) | ||
| 9 | recursive_allocate(q) | ||
| | ^ | ||
|
|
||
| File "$PATH_TO_FILE/recursive.py", line 9, in stack_traces.recursive.recursive_allocate: | ||
| | | ||
| 7 | q = qubit() | ||
| 8 | cx(source, q) | ||
| 9 | recursive_allocate(q) | ||
| | ^ | ||
|
|
||
| File "$PATH_TO_FILE/recursive.py", line 16, in stack_traces.recursive.main: | ||
| | | ||
| 14 | def main() -> None: | ||
| 15 | q = qubit() | ||
| 16 | recursive_allocate(q) | ||
| | ^ | ||
|
|
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,20 @@ | ||
| from guppylang import guppy | ||
| from guppylang.std.quantum import cx, discard, qubit | ||
|
|
||
|
|
||
| @guppy | ||
| def recursive_allocate(source: qubit) -> None: | ||
| q = qubit() | ||
| cx(source, q) | ||
| recursive_allocate(q) | ||
| discard(q) | ||
|
|
||
|
|
||
| @guppy | ||
| def main() -> None: | ||
| q = qubit() | ||
| recursive_allocate(q) | ||
| discard(q) | ||
|
|
||
|
|
||
| main.with_minimal_opt().emulator(n_qubits=4, debug_mode=True).run() |
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,8 @@ | ||
| from guppylang import guppy | ||
| from guppylang.std.array import array | ||
|
|
||
|
|
||
| @guppy | ||
| def array_with_three_elements(x: int) -> int: | ||
| arr = array(1, 2, 3) | ||
| return arr[x] |
Oops, something went wrong.
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.
What happens when optimization is enabled?
Do stack traces get mangled, or do we lose all the info?
Could this be a warning instead (once we have those #1654)?
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.
The main issues arising so far is stack traces being squashed due to LLVM tail calls and also Quantinuum/tket2#1964 - I think at least until the latter is solved I think having it error is better as showing wrong code snippets just looks confusing even with a warning, but later I think keeping it as a warning and just showing partial traces when some of the info is optimised is an option.
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.
👍
I would add a non-doc comment explaining that, and linking to the issue.