Skip to content

Commit

Permalink
[refactor] Add format_error_message() method (#1955)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-ye authored Oct 14, 2020
1 parent d3bf553 commit f3fe997
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
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

0 comments on commit f3fe997

Please sign in to comment.