Skip to content

Commit

Permalink
[lang] Passing DebugInfo instead of std::string traceback (#8293)
Browse files Browse the repository at this point in the history
Issue: #

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 3304bee</samp>

Refactor the IR code to use `DebugInfo` objects to store and pass source
info. This simplifies the IR construction and debugging, and avoids
duplication of the `tb` argument. Modify various files in
`python/taichi/lang` and `taichi/ir` to use the new `DebugInfo`
constructor and argument.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 3304bee</samp>

* Unify the way source info is stored and passed in the IR by using
`DebugInfo` objects instead of `Traceback` objects or raw strings.
([link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-2e623ee0b0eec1b200fead36c0627a3c54738f6d83d79757398dc67decc01da8L91-R91),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-3e22417ffade4af0564893b98dc5101d714b8ba6fd4423ab5bc5129e360fee8fL991-R991),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-3e22417ffade4af0564893b98dc5101d714b8ba6fd4423ab5bc5129e360fee8fL1000-R1000),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-99744c5ae5f6a754d6f68408fdc64fb0d6097216518a7f3d1ef43ffe12599577L253-R262),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-c48bb572255ef55d0c9fd89c9febab88b9668e10dfcfc1fac88feb1be7bd94caL600-R600),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-c48bb572255ef55d0c9fd89c9febab88b9668e10dfcfc1fac88feb1be7bd94caL615-R615),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-059028cb0798284bed05638becbc32d256736846de19746e196fe5f5ee7fd061L1361-R1361),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-aee943d584058490d7717d34c02a3783d3487694dc091653d42b202e45b1e097L69-R69),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-9d4212518d49c780c1b10ace5a5d873aed525373a2751e7d888f164ea51edd7fR44-R47),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L41-R44),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L281-R287),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L458-R459),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L465-R466),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L604-R609),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L625-R624),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L633-R635),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L709-R707),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L735-R733),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L743-R741),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L757-R761),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L792-R791),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L800-R801),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L807),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L948-R947),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L955-R954),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1075-R1075),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1110-R1111),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1128-R1127),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1144-R1150),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1413-R1408),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1419-R1414),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1431-R1427),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1591-R1589),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1600-R1595),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1614-R1609),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1786-R1781),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1792-R1787),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-a6e92dd2dd707d705dc44ef91463ddc0423575188e6e8e0555de9e439db88c35L1801-R1796),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-0bb8e07f3f606e45f4e4284c02b32f986d420388fdd71b115a7475aacb50a42cL143-R145),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-0bb8e07f3f606e45f4e4284c02b32f986d420388fdd71b115a7475aacb50a42cL642-R649),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-0bb8e07f3f606e45f4e4284c02b32f986d420388fdd71b115a7475aacb50a42cL996-R999),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-0bb8e07f3f606e45f4e4284c02b32f986d420388fdd71b115a7475aacb50a42cL1027-R1029),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-0bb8e07f3f606e45f4e4284c02b32f986d420388fdd71b115a7475aacb50a42cL1034-R1038),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-50be2dc708119a4c9b53e977807d2f05e4ff6ce98c3f51fa91d1fa9e229962f1R127-R130),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-61484fa2a50e309478017fb2a436198aa4b0afdf72a4039bf574fc4f2aedbe4eR393),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5L9-R12),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5L65-R70),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5L101-R108),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5L139-R145),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5L317-R329),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5L328-R344),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-917d9436dcaafa0f1e41ae9bad90273a303f036f00da94e417788a7fa1dc5260L21-R22),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-917d9436dcaafa0f1e41ae9bad90273a303f036f00da94e417788a7fa1dc5260L159-R162),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-917d9436dcaafa0f1e41ae9bad90273a303f036f00da94e417788a7fa1dc5260L203-R209),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-917d9436dcaafa0f1e41ae9bad90273a303f036f00da94e417788a7fa1dc5260L260-R268),
[link](https://github.com/taichi-dev/taichi/pull/8293/files?diff=unified&w=0#diff-917d9436dcaafa0f1e41ae9bad90273a303f036f00da94e417788a7fa1dc5260L287-R299))
  • Loading branch information
dream189free authored Jul 21, 2023
1 parent 772199e commit c41eec1
Show file tree
Hide file tree
Showing 21 changed files with 175 additions and 122 deletions.
2 changes: 1 addition & 1 deletion python/taichi/lang/any_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def subscript(self, i, j):
ast_builder.expr_subscript(
self.arr.ptr,
make_expr_group(*indices),
impl.get_runtime().get_current_src_info(),
_ti_core.DebugInfo(impl.get_runtime().get_current_src_info()),
)
)

Expand Down
4 changes: 2 additions & 2 deletions python/taichi/lang/ast/ast_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ def build_Attribute(ctx, node):
.expr_subscript(
node.value.ptr.ptr,
make_expr_group(keygroup.index(node.attr)),
impl.get_runtime().get_current_src_info(),
_ti_core.DebugInfo(impl.get_runtime().get_current_src_info()),
)
)
else:
Expand All @@ -997,7 +997,7 @@ def build_Attribute(ctx, node):
node.value.ptr.ptr,
[make_expr_group(keygroup.index(ch)) for ch in node.attr],
(attr_len,),
impl.get_runtime().get_current_src_info(),
_ti_core.DebugInfo(impl.get_runtime().get_current_src_info()),
)
)
else:
Expand Down
2 changes: 1 addition & 1 deletion python/taichi/lang/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, *args, tb=None, dtype=None):
else:
assert False
if self.tb:
self.ptr.set_tb(self.tb)
self.ptr.set_dbg_info(_ti_core.DebugInfo(self.tb))
if not self.ptr_type_checked:
self.ptr.type_check(impl.get_runtime().prog.config())
self.ptr_type_checked = True
Expand Down
15 changes: 9 additions & 6 deletions python/taichi/lang/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def expr_init(rhs):
if hasattr(rhs, "_data_oriented"):
return rhs
return Expr(
get_runtime().compiling_callable.ast_builder().expr_var(Expr(rhs).ptr, get_runtime().get_current_src_info())
get_runtime()
.compiling_callable.ast_builder()
.expr_var(Expr(rhs).ptr, _ti_core.DebugInfo(get_runtime().get_current_src_info()))
)


Expand Down Expand Up @@ -175,6 +177,7 @@ def validate_subscript_index(value, index):

@taichi_scope
def subscript(ast_builder, value, *_indices, skip_reordered=False):
dbg_info = _ti_core.DebugInfo(get_runtime().get_current_src_info())
ast_builder = get_runtime().compiling_callable.ast_builder()
# Directly evaluate in Python for non-Taichi types
if not isinstance(
Expand Down Expand Up @@ -251,14 +254,14 @@ def subscript(ast_builder, value, *_indices, skip_reordered=False):
)

if isinstance(value, MatrixField):
return Expr(ast_builder.expr_subscript(value.ptr, indices_expr_group, get_runtime().get_current_src_info()))
return Expr(ast_builder.expr_subscript(value.ptr, indices_expr_group, dbg_info))
if isinstance(value, StructField):
entries = {k: subscript(ast_builder, v, *indices) for k, v in value._items}
entries["__struct_methods"] = value.struct_methods
return _IntermediateStruct(entries)
return Expr(ast_builder.expr_subscript(_var, indices_expr_group, get_runtime().get_current_src_info()))
return Expr(ast_builder.expr_subscript(_var, indices_expr_group, dbg_info))
if isinstance(value, AnyArray):
return Expr(ast_builder.expr_subscript(value.ptr, indices_expr_group, get_runtime().get_current_src_info()))
return Expr(ast_builder.expr_subscript(value.ptr, indices_expr_group, dbg_info))
assert isinstance(value, Expr)
# Index into TensorType
# value: IndexExpression with ret_type = TensorType
Expand Down Expand Up @@ -291,10 +294,10 @@ def subscript(ast_builder, value, *_indices, skip_reordered=False):
value.ptr,
multiple_indices,
return_shape,
get_runtime().get_current_src_info(),
dbg_info,
)
)
return Expr(ast_builder.expr_subscript(value.ptr, indices_expr_group, get_runtime().get_current_src_info()))
return Expr(ast_builder.expr_subscript(value.ptr, indices_expr_group, dbg_info))


class SrcInfoGuard:
Expand Down
4 changes: 2 additions & 2 deletions python/taichi/lang/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def __init__(self, mesh: MeshInstance, element_type: MeshElementType, entry_expr
ast_builder.expr_subscript(
attr.ptr,
global_entry_expr_group,
impl.get_runtime().get_current_src_info(),
_ti_core.DebugInfo(impl.get_runtime().get_current_src_info()),
)
),
)
Expand All @@ -612,7 +612,7 @@ def __init__(self, mesh: MeshInstance, element_type: MeshElementType, entry_expr
ast_builder.expr_subscript(
var,
global_entry_expr_group,
impl.get_runtime().get_current_src_info(),
_ti_core.DebugInfo(impl.get_runtime().get_current_src_info()),
)
),
)
Expand Down
2 changes: 1 addition & 1 deletion python/taichi/lang/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ def atomic_xor(x, y):

@writeback_binary
def assign(a, b):
impl.get_runtime().compiling_callable.ast_builder().expr_assign(a.ptr, b.ptr, stack_info())
impl.get_runtime().compiling_callable.ast_builder().expr_assign(a.ptr, b.ptr, _ti_core.DebugInfo(stack_info()))
return a


Expand Down
2 changes: 1 addition & 1 deletion python/taichi/lang/simt/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def subscript(self, *indices):
ast_builder.expr_subscript(
self.shared_array_proxy,
make_expr_group(*indices),
impl.get_runtime().get_current_src_info(),
_ti_core.DebugInfo(impl.get_runtime().get_current_src_info()),
)
)
4 changes: 2 additions & 2 deletions taichi/ir/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace taichi::lang {

void Expr::set_tb(const std::string &tb) {
expr->set_tb(tb);
void Expr::set_dbg_info(const DebugInfo &dbg_info) {
expr->dbg_info = dbg_info;
}

const std::string &Expr::get_tb() const {
Expand Down
4 changes: 2 additions & 2 deletions taichi/ir/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class Expr {

SNode *snode() const;

// traceback for type checking error message
void set_tb(const std::string &tb);
// debug info, contains traceback for type checking error message
void set_dbg_info(const DebugInfo &dbg_info);

const std::string &get_tb() const;

Expand Down
4 changes: 4 additions & 0 deletions taichi/ir/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class Expression {
stmt = nullptr;
}

explicit Expression(const DebugInfo &dbg_info) : Expression() {
this->dbg_info = dbg_info;
}

virtual void type_check(const CompileConfig *config) = 0;

virtual void accept(ExpressionVisitor *visitor) = 0;
Expand Down
Loading

0 comments on commit c41eec1

Please sign in to comment.