Skip to content

Conversation

@rafaelha
Copy link
Contributor

@rafaelha rafaelha commented Oct 23, 2025

This PR ensures that source info is preserved after inlining or cloning in .similar().

Additionally I found that offsets were lost (which are now added in lowering.py).

I tested all of these changes on kirin 0.17.30 together with bloqade-circuit.

Blocks QuEraComputing/bloqade-circuit#552

Addresses #540

@github-actions
Copy link
Contributor

github-actions bot commented Oct 23, 2025

PR Preview Action v1.6.2
Preview removed because the pull request was closed.
2025-10-28 14:59 UTC

Copy link
Collaborator

@kaihsin kaihsin left a comment

Choose a reason for hiding this comment

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

LGTM.

@kaihsin
Copy link
Collaborator

kaihsin commented Oct 23, 2025

The CI is failing tho

@rafaelha rafaelha changed the title Ensure SourceInfo is kept through inline pass Ensure SourceInfo is preserved in inline pass and offsets are kepts Oct 23, 2025
f"Unsupported dialect `{stmt.dialect.name}` from statement {stmt.name}"
)
self.curr_block.stmts.append(stmt)
if stmt.source is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

since stmts.append calls Statement.insert which sets the source info, it's now important to set the source before.

Copy link
Collaborator

@kaihsin kaihsin left a comment

Choose a reason for hiding this comment

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

CI is failing

@rafaelha
Copy link
Contributor Author

I broke my own unit test in the last commit. I'll fix it.

@rafaelha
Copy link
Contributor Author

rafaelha commented Oct 23, 2025

The issue is actually subtle.

Let's consider this kernel:

@squin.kernel
def inline(q):
    squin.correlated_qubit_loss(0.1, qubits=q)


@squin.kernel
def my_test():
    q = squin.qubit.new(3)
    inline(q)
    squin.correlated_qubit_loss(0.1, qubits=q)
    squin.x(q[0])

I want the source info to correctly point to the inline kernel. However, the inline kernel contains the squin.correlated_qubit_loss statement, which is actually just a kernel defined in bloqade/squin/stdlib, so now the source will come from stdlib.

So, the source info will always be coming from the inlined stdlib kernels, which is not helpful to the user.

Ideally I would want inlining of source info to stop right before stdlib. A hacky way to do this is the following code in inline.py:

if call_like.source is not None:
    for block in inline_region.blocks:
        if block.source is None or "stdlib" in block.source.file:
            block.source = call_like.source
        for stmt in block.stmts:
            if stmt.source is None or "stdlib" in stmt.source.file:
                stmt.source = call_like.source

But this is bad. kirin does not even know about stdlib, so we shouldn't be allowed to mention this here.

Do you have any recommendations how to break the inline chain at the right spot?

EDIT: The proper way would be to have a source info stack and a believe that's how MLIR handles this.

Previously call_site.f_lineno was used but this definition changed by an offset of 1 in Python 3.11

You can verify this, e.g., by running this script:
class MyDecorator:
    def __call__(self, func):
        frame = inspect.currentframe()
        call_site = frame.f_back
        func_def_line = func.__code__.co_firstlineno

        if sys.version_info < (3, 11):
            assert call_site.f_lineno - func_def_line == 1
        else:
            assert call_site.f_lineno - func_def_line == 2

        return func

decorator = MyDecorator()

@decorator  # Line 26
def test_func():  # Line 27
    pass
@rafaelha rafaelha force-pushed the rafaelha/fix-source-propagation branch from 293cda1 to d3756a1 Compare October 23, 2025 17:01
@codecov
Copy link

codecov bot commented Oct 23, 2025

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/kirin/rewrite/inline.py 85.71% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions
Copy link
Contributor

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
11395 10156 89% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
src/kirin/ir/group.py 87% 🟢
src/kirin/ir/nodes/stmt.py 85% 🟢
src/kirin/lowering/frame.py 94% 🟢
src/kirin/lowering/python/lowering.py 89% 🟢
src/kirin/rewrite/inline.py 96% 🟢
TOTAL 90% 🟢

updated for commit: d3756a1 by action🐍

@Roger-luo Roger-luo merged commit 537525f into main Oct 28, 2025
13 checks passed
@Roger-luo Roger-luo deleted the rafaelha/fix-source-propagation branch October 28, 2025 14:59
Roger-luo pushed a commit that referenced this pull request Oct 28, 2025
…541)

This PR ensures that source info is preserved after inlining or cloning
in `.similar()`.

Additionally I found that offsets were lost (which are now added in
`lowering.py`).

I tested all of these changes on kirin 0.17.30 together with
bloqade-circuit.

Blocks QuEraComputing/bloqade-circuit#552

Addresses #540
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.

4 participants