Skip to content
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

[tir] Add line level debug info #13012

Merged
merged 7 commits into from
Jan 6, 2023
Merged

[tir] Add line level debug info #13012

merged 7 commits into from
Jan 6, 2023

Conversation

driazati
Copy link
Member

@driazati driazati commented Oct 7, 2022

This adds a pass InstallDebugSpans that uses the printout from the TIR text printer to override the source spans on statements and expressions in an IRModule. The pass runs a new printer that records statements and their corresponding line number as they are printed. The LLVM CPU codegen then reads these spans to set the debug location on llvm::Instructions as they are created in the builder.

@driazati driazati changed the title Cleanup 5 (broken) Add line level debug info for TIR Oct 7, 2022
@driazati driazati force-pushed the gdb3 branch 6 times, most recently from 43c6b08 to a2fdb8f Compare October 13, 2022 07:21
@areusch areusch added needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it and removed needs-triage PRs or issues that need to be investigated by maintainers to find the right assignees to address it labels Oct 19, 2022
@areusch
Copy link
Contributor

areusch commented Oct 19, 2022

cc @kparzysz-quic

@driazati driazati force-pushed the gdb3 branch 3 times, most recently from 9966735 to dec0d8a Compare October 21, 2022 18:17
@driazati driazati marked this pull request as ready for review October 21, 2022 18:21
@driazati driazati changed the title Add line level debug info for TIR [tir] Add line level debug info Oct 21, 2022
@driazati driazati marked this pull request as draft October 21, 2022 18:22
@driazati driazati force-pushed the gdb3 branch 3 times, most recently from 9c96a2d to d66fe0d Compare October 25, 2022 18:20
@driazati driazati marked this pull request as ready for review October 25, 2022 18:20
@driazati driazati force-pushed the gdb3 branch 3 times, most recently from 34e8283 to d68def3 Compare October 28, 2022 21:37
@tvm-bot
Copy link
Collaborator

tvm-bot commented Oct 29, 2022

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

  • Built docs for commit 4a5c1a2 can be found here.
  • This PR had no significant effect on CI runtime: +15.13% (167.15m -> 192.44m)

Generated by tvm-bot

Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

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

thanks @driazati

@kparzysz-quic can you review the LLVM parts?

@@ -526,6 +526,7 @@ class IntImm : public PrimExpr {
TVM_DLL IntImm(DataType dtype, int64_t value, Span span = Span());

TVM_DEFINE_OBJECT_REF_METHODS(IntImm, PrimExpr, IntImmNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(IntImmNode);
Copy link
Contributor

Choose a reason for hiding this comment

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

just wondering why you needed this one?

Copy link
Member Author

Choose a reason for hiding this comment

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

do you mean for TVM_DEFINE_OBJECT_REF_COW_METHOD or for IntImmNode in particular? the copy on write is used when creating new nodes so every supported op needs to have it defined, and IntImm is one of the supported ops in install_debug_spans_ops.h so it needs it as well

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you create a separate PR with the COW changes?

src/printer/tir_text_printer.cc Show resolved Hide resolved
src/target/llvm/codegen_cpu.cc Show resolved Hide resolved
src/tir/transforms/install_debug_spans.h Show resolved Hide resolved
src/tir/transforms/install_debug_spans.cc Show resolved Hide resolved
src/tir/transforms/install_debug_spans.h Show resolved Hide resolved
@@ -526,6 +526,7 @@ class IntImm : public PrimExpr {
TVM_DLL IntImm(DataType dtype, int64_t value, Span span = Span());

TVM_DEFINE_OBJECT_REF_METHODS(IntImm, PrimExpr, IntImmNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(IntImmNode);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you create a separate PR with the COW changes?


Pass InstallDebugSpans() {
auto pass_func = [](PrimFunc f, IRModule m, PassContext ctx) {
ICHECK(m->functions.size() == 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if it's an error to run it on a module with multiple functions... If multiple functions aren't supported, then maybe just exit without doing anything?

Is this a temporary restriction?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this is just there for the first pass. Seeing how this pass has to be explicitly enabled I think it makes more sense to keep it strict rather than permissive so errors are surfaced early

driazati added a commit to driazati/tvm that referenced this pull request Nov 29, 2022
This enables copy on write methods for all nodes since some were missing
it before (see apache#13012 for more context)
@tqchen
Copy link
Member

tqchen commented Nov 29, 2022

Given that we are moving more towards TVMScript, It would be good to think about how to make it compatible with TVMScript printing and debug info. cc @junrushao @yelite

kparzysz-quic pushed a commit that referenced this pull request Nov 29, 2022
This enables copy on write methods for all nodes since some were missing
it before (see #13012 for more context)

Co-authored-by: driazati <driazati@users.noreply.github.com>
@driazati
Copy link
Member Author

Given that we are moving more towards TVMScript, It would be good to think about how to make it compatible with TVMScript printing and debug info. cc @junrushao @yelite

One shortcut this PR takes is to explicitly avoid TVM script (at least for source reporting) and use the TIR text printer so it can be run at any point in the compilation pipeline. I think the eventual plan would be to have true source line numbers from the actual written TVM script rather than the generated TIR

@yelite
Copy link
Contributor

yelite commented Dec 1, 2022

One shortcut this PR takes is to explicitly avoid TVM script (at least for source reporting) and use the TIR text printer so it can be run at any point in the compilation pipeline. I think the eventual plan would be to have true source line numbers from the actual written TVM script rather than the generated TIR

The new TVMScript printer is able to print IR fragments (not constraining to print IRModule and PrimFunc only). And it's able to record span of an IR node based on printed result (https://github.com/apache/tvm/blob/main/src/script/printer/base_doc_printer.cc#L331, used for diagnostic marker printing) and distinguish the same node in different places (for example, the same VarNode can be found in different places of the IR graph). The implementation of TIR printing is finished. But we need to sort out the detail on how to track ObjectPath in a more ergonomic and readable way before finally upstreaming this part.

The plan of migrating to TVMScript printer sounds good to me. If we are interested in tracing back to the TVMScript code of Var or Buffer, we need to be aware of that such nodes can appear multiple times in the graph (which means multiple spans in the TVMScript code), and the 1-1 mapping between node and span in TVM will cause some trouble.

@driazati driazati force-pushed the gdb3 branch 2 times, most recently from 4a5c1a2 to bf0e9d6 Compare December 6, 2022 22:40
@masahi
Copy link
Member

masahi commented Dec 12, 2022

Can we merge this? @areusch @kparzysz-quic @tqchen

@areusch
Copy link
Contributor

areusch commented Jan 6, 2023

@tvm-bot rerun

@kparzysz-quic kparzysz-quic merged commit 123f1f5 into apache:main Jan 6, 2023
fzi-peccia pushed a commit to fzi-peccia/tvm that referenced this pull request Mar 27, 2023
* TIR debug info

* Fix location emission

* Comments 1/N (docs, cleanups)

* Remove leaky macro usage

* Add unit test

* Remove dead code

* Add accuracy test

Co-authored-by: driazati <driazati@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants