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