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

[refactor] Add format_error_message() method #1955

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 9 additions & 26 deletions taichi/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "taichi/ir/frontend_ir.h"
#include "taichi/program/async_engine.h"
#include "taichi/util/statistics.h"
#include "taichi/util/str.h"
#if defined(TI_WITH_CC)
#include "taichi/backends/cc/struct_cc.h"
#include "taichi/backends/cc/cc_layout.h"
Expand Down Expand Up @@ -428,7 +429,7 @@ void Program::check_runtime_error() {
// memory), use the device context instead.
tlctx = llvm_context_device.get();
}
auto runtime_jit_module = tlctx->runtime_jit_module;
auto *runtime_jit_module = tlctx->runtime_jit_module;
runtime_jit_module->call<void *>("runtime_retrieve_and_reset_error_code",
llvm_runtime);
auto error_code = fetch_result<int64>(taichi_result_buffer_error_id);
Expand All @@ -451,31 +452,13 @@ void Program::check_runtime_error() {
}

if (error_code == 1) {
std::string error_message_formatted;
int argument_id = 0;
for (int i = 0; i < (int)error_message_template.size(); i++) {
if (error_message_template[i] != '%') {
error_message_formatted += error_message_template[i];
} else {
auto dtype = error_message_template[i + 1];
runtime_jit_module->call<void *>(
"runtime_retrieve_error_message_argument", llvm_runtime,
argument_id);
auto argument = fetch_result<uint64>(taichi_result_buffer_error_id);
if (dtype == 'd') {
error_message_formatted += fmt::format(
"{}", taichi_union_cast_with_different_sizes<int32>(argument));
} else if (dtype == 'f') {
error_message_formatted += fmt::format(
"{}",
taichi_union_cast_with_different_sizes<float32>(argument));
} else {
TI_ERROR("Data type identifier %{} is not supported", dtype);
}
argument_id += 1;
i++; // skip the dtype char
}
}
const auto error_message_formatted = format_error_message(
error_message_template, [runtime_jit_module, this](int argument_id) {
runtime_jit_module->call<void *>(
"runtime_retrieve_error_message_argument", llvm_runtime,
argument_id);
return fetch_result<uint64>(taichi_result_buffer_error_id);
});
TI_ERROR("Assertion failure: {}", error_message_formatted);
} else {
TI_NOT_IMPLEMENTED
Expand Down
28 changes: 28 additions & 0 deletions taichi/util/str.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <sstream>

#include "taichi/inc/constants.h"

TLANG_NAMESPACE_BEGIN

std::string c_quoted(std::string const &str) {
Expand Down Expand Up @@ -32,4 +34,30 @@ std::string c_quoted(std::string const &str) {
return ss.str();
}

std::string format_error_message(const std::string &error_message_template,
const std::function<uint64(int)> &fetcher) {
std::string error_message_formatted;
int argument_id = 0;
for (int i = 0; i < (int)error_message_template.size(); i++) {
if (error_message_template[i] != '%') {
error_message_formatted += error_message_template[i];
} else {
const auto dtype = error_message_template[i + 1];
const auto argument = fetcher(argument_id);
if (dtype == 'd') {
error_message_formatted += fmt::format(
"{}", taichi_union_cast_with_different_sizes<int32>(argument));
} else if (dtype == 'f') {
error_message_formatted += fmt::format(
"{}", taichi_union_cast_with_different_sizes<float32>(argument));
} else {
TI_ERROR("Data type identifier %{} is not supported", dtype);
}
argument_id += 1;
i++; // skip the dtype char
}
}
return error_message_formatted;
}

TLANG_NAMESPACE_END
4 changes: 4 additions & 0 deletions taichi/util/str.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <functional>

#include "taichi/lang_util.h"

Expand All @@ -9,4 +10,7 @@ TLANG_NAMESPACE_BEGIN
// Quote |str| with a pair of ". Escape special characters like \n, \t etc.
std::string c_quoted(std::string const &str);

std::string format_error_message(const std::string &error_message_template,
const std::function<uint64(int)> &fetcher);

TLANG_NAMESPACE_END