Skip to content

Commit dd2b3ef

Browse files
committed
Fix source propagation and offset
1 parent a61e51f commit dd2b3ef

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/kirin/ir/nodes/stmt.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ def insert_after(self, stmt: Statement) -> None:
267267
self.parent = stmt.parent
268268
stmt._next_stmt = self
269269

270+
if self.source is None and stmt.source is not None:
271+
self.source = stmt.source
272+
270273
if self.parent:
271274
self.parent._stmt_len += 1
272275

@@ -302,6 +305,9 @@ def insert_before(self, stmt: Statement) -> None:
302305
self.parent = stmt.parent
303306
stmt._prev_stmt = self
304307

308+
if self.source is None and stmt.source is not None:
309+
self.source = stmt.source
310+
305311
if self.parent:
306312
self.parent._stmt_len += 1
307313

@@ -506,6 +512,7 @@ def from_stmt(
506512
attributes=attributes or other.attributes,
507513
result_types=[result.type for result in other._results],
508514
args_slice=other._name_args_slice,
515+
source=other.source,
509516
)
510517
# inherit the hint:
511518
for result, other_result in zip(obj._results, other._results):

src/kirin/lowering/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def _push_stmt(self, stmt: StmtType) -> StmtType:
7575
raise BuildError(
7676
f"Unsupported dialect `{stmt.dialect.name}` from statement {stmt.name}"
7777
)
78-
self.curr_block.stmts.append(stmt)
7978
if stmt.source is None:
8079
stmt.source = self.state.source
80+
self.curr_block.stmts.append(stmt)
8181
return stmt
8282

8383
def _push_block(self, block: Block):

src/kirin/lowering/python/lowering.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def lower_global(self, state: State[ast.AST], node: ast.AST) -> LoweringABC.Resu
138138
def visit(self, state: State[ast.AST], node: ast.AST) -> Result:
139139
if hasattr(node, "lineno"):
140140
state.source = SourceInfo.from_ast(node, state.file)
141+
state.source.offset(state.lineno_offset - 1, state.col_offset)
141142
name = node.__class__.__name__
142143
if name in self.registry.ast_table:
143144
return self.registry.ast_table[name].lower(state, node)
@@ -148,7 +149,8 @@ def generic_visit(self, state: State[ast.AST], node: ast.AST) -> Result:
148149

149150
def visit_Call(self, state: State[ast.AST], node: ast.Call) -> Result:
150151
if hasattr(node.func, "lineno"):
151-
state.source = SourceInfo.from_ast(node.func, state.file)
152+
state.source = SourceInfo.from_ast(node, state.file)
153+
state.source.offset(state.lineno_offset - 1, state.col_offset)
152154

153155
global_callee_result = state.get_global(node.func, no_raise=True)
154156
if global_callee_result is None:

src/kirin/rewrite/inline.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ def inline_call_like(
9797

9898
# NOTE: we cannot change region because it may be used elsewhere
9999
inline_region: ir.Region = region.clone()
100+
101+
# Preserve source information by attributing inlined code to the call site
102+
if call_like.source is not None:
103+
for block in inline_region.blocks:
104+
if block.source is None:
105+
block.source = call_like.source
106+
for stmt in block.stmts:
107+
if stmt.source is None:
108+
stmt.source = call_like.source
109+
100110
parent_block: ir.Block = call_like.parent_block
101111
parent_region: ir.Region = call_like.parent_region
102112

0 commit comments

Comments
 (0)