Skip to content

Commit

Permalink
[bug] Fix assign may lose precision warning & improve related logging (
Browse files Browse the repository at this point in the history
…#8553)

Does these few things:
1. Removed printing of "No DebugInfo Available". This is simply a lot of
spam when printing IR. Don't print when there's nothing to print
2. Improve debug info handling for things that doesn't have source
correlation, by first searching the immediate preceding statements for
debug info (so the user at least know the ballpark), and when that fails
prints the callable name & statement id
3. Fixed the issue within snode_writer that emits `Assign may lose
precision: unknown <- f32` like crazy. (The unknown type is caused by a
missing `type_check` on the indexing expression)
4. Use our exception system to emit more helpful warnings for
load-to-store forwarding, e.g.:

```
[W 06/23/24 19:08:39.945 11791278] TaichiWarning
File "/Users/bobcao3/taichi/python/taichi/lang/matrix_ops.py", line 281, in _matmul_helper:
                mat_z[i, j] = mat_z[i, j] + mat_x[i, k] * mat_y[k, j]
                                            ^^^^^^^^^^^
Loading variable 937 before anything is stored to it.
```

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
bobcao3 and pre-commit-ci[bot] authored Jun 24, 2024
1 parent d86e69f commit 37a0563
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 11 deletions.
6 changes: 5 additions & 1 deletion taichi/common/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ struct DebugInfo {
explicit DebugInfo(const char *tb_) : tb(tb_) {
}

std::string get_last_tb() const {
return tb;
}

std::string const &get_tb() const {
return tb;
}
Expand Down Expand Up @@ -141,7 +145,7 @@ struct ErrorEmitter {
// tb here
error.msg_ = error_msg;
} else {
error.msg_ = p_dbg_info->get_tb() + error_msg;
error.msg_ = p_dbg_info->get_last_tb() + error_msg;
}

if constexpr (std::is_base_of_v<TaichiWarning, std::decay_t<E>>) {
Expand Down
8 changes: 6 additions & 2 deletions taichi/ir/control_flow_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <queue>
#include <unordered_set>

#include "taichi/common/exceptions.h"
#include "taichi/ir/analysis.h"
#include "taichi/ir/statements.h"
#include "taichi/system/profiler.h"
Expand Down Expand Up @@ -337,8 +338,11 @@ Stmt *CFGNode::get_store_forwarding_data(Stmt *var, int position) const {
}
if (!result) {
// The UD-chain is empty.
TI_WARN("stmt {} loaded in stmt {} before storing.", var->id,
block->statements[position]->id);
auto offending_load = block->statements[position].get();
ErrorEmitter(
TaichiIrWarning(), offending_load,
fmt::format("Loading variable {} before anything is stored to it.",
var->id));
return nullptr;
}
if (!result_visible) {
Expand Down
4 changes: 4 additions & 0 deletions taichi/ir/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class Expression {
return stmt;
}

std::string get_last_tb() const {
return dbg_info.get_last_tb();
}

std::string const &get_tb() const {
return dbg_info.tb;
}
Expand Down
43 changes: 42 additions & 1 deletion taichi/ir/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ IRNode *IRNode::get_ir_root() {
return node;
}

const IRNode *IRNode::get_ir_root() const {
auto node = this;
while (node->get_parent()) {
node = node->get_parent();
}
return node;
}

std::unique_ptr<IRNode> IRNode::clone() {
std::unique_ptr<IRNode> new_irnode;
if (is<Block>())
Expand Down Expand Up @@ -139,7 +147,7 @@ Callable *Stmt::get_callable() const {
}
irpass::print((IRNode *)this);

TI_ASSERT_INFO(false, "Stmt is not in a kernel.");
TI_WARN("Stmt is not in a kernel.");
return nullptr;
}

Expand Down Expand Up @@ -195,6 +203,39 @@ IRNode *Stmt::get_parent() const {
return parent;
}

std::string Stmt::get_last_tb() const {
const auto *callable = get_callable();
const auto callable_name = callable ? callable->get_name() : "";

std::string prefix =
!callable_name.empty() ? "While compiling `" + callable_name + "`, " : "";

const auto &tb = dbg_info.tb;
if (tb.empty()) {
// If has no tb, try to find tb from the immediate previous statement
if (parent) {
auto it_this = std::find_if(
parent->statements.rbegin(), parent->statements.rend(),
[this](const pStmt &stmt) { return stmt.get() == this; });

while (it_this != parent->statements.rend()) {
const auto &stmt = *it_this;
if (!stmt->get_tb().empty()) {
return prefix + stmt->get_tb();
}
++it_this;
}
}

const auto stmt_type_name = typeid(*this).name();

return fmt::format("{}Statement {} (type={});\n", prefix, name(),
cpp_demangle(stmt_type_name));
}

return prefix + tb;
}

std::vector<Stmt *> Stmt::get_operands() const {
std::vector<Stmt *> ret;
for (int i = 0; i < num_operands(); i++) {
Expand Down
3 changes: 3 additions & 0 deletions taichi/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class IRNode {
virtual IRNode *get_parent() const = 0;

IRNode *get_ir_root();
const IRNode *get_ir_root() const;

virtual ~IRNode() = default;

Expand Down Expand Up @@ -441,6 +442,8 @@ class Stmt : public IRNode {
return *operands[i];
}

std::string get_last_tb() const;

TI_FORCE_INLINE std::string const &get_tb() const {
return dbg_info.tb;
}
Expand Down
1 change: 1 addition & 0 deletions taichi/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ Kernel &Program::get_snode_writer(SNode *snode) {
ASTBuilder &builder = kernel->context->builder();
auto expr =
builder.expr_subscript(Expr(snode_to_fields_.at(snode)), indices);
expr.type_check(&this->compile_config());
auto argload_expr = Expr::make<ArgLoadExpression>(
std::vector<int>{snode->num_active_indices},
snode->dt->get_compute_type());
Expand Down
14 changes: 10 additions & 4 deletions taichi/system/demangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <cxxabi.h>
#endif

#if defined(TI_PLATFORM_WINDOWS)
#include <DbgHelp.h>
#endif

namespace taichi {

// From https://en.wikipedia.org/wiki/Name_mangling
Expand All @@ -22,6 +26,12 @@ std::string cpp_demangle(const std::string &mangled_name) {
std::string ret(demangled_name);
free(demangled_name);
return ret;
#elif defined(TI_PLATFORM_WINDOWS)
PCSTR mangled = mangled_name.c_str();
char demangled[1024];
DWORD length =
UnDecorateSymbolName(mangled, demangled, 1024, UNDNAME_NAME_ONLY);
return std::string(demangled, size_t(length));
#else
TI_NOT_IMPLEMENTED
#endif
Expand All @@ -33,11 +43,7 @@ class Demangling : public Task {
printf("There should be at least one parameter for demangling.\n");
}
for (auto p : parameters) {
#if !defined(_WIN64)
printf("Demangled C++ Identifier: %s\n", cpp_demangle(p).c_str());
#else
TI_NOT_IMPLEMENTED
#endif
}
return "";
}
Expand Down
6 changes: 3 additions & 3 deletions taichi/transforms/ir_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class IRPrinter : public IRVisitor {
if (print_ir_dbg_info) {
dbg_info_printer_ = [this](const Stmt *stmt) {
auto tb = stmt->get_tb();
print_raw(tb.empty() ? "No DebugInfo avaliable.\n"
: tb.substr(0, tb.length()),
"");
if (!tb.empty()) {
this->print_raw(tb.substr(0, tb.length()), "");
}
};
}
}
Expand Down

0 comments on commit 37a0563

Please sign in to comment.