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

[bug] Fix assign may lose precision warning & improve related logging #8553

Merged
merged 11 commits into from
Jun 24, 2024
Merged
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
36 changes: 36 additions & 0 deletions 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 @@ -195,6 +203,34 @@ IRNode *Stmt::get_parent() const {
return parent;
}

std::string Stmt::get_last_tb() const {
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 stmt->get_tb();
}
++it_this;
}
}

const auto stmt_type_name = typeid(*this).name();
const auto callable_name = get_callable()->get_name();

return fmt::format("{}::{} of type {}\n", callable_name, name(),
cpp_demangle(stmt_type_name));
}

return 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
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
Loading