Skip to content

Commit

Permalink
apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhu-wang committed Nov 1, 2023
1 parent 8f88567 commit 11e6bca
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/common/snippets/include/snippets/lowered/linear_ir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Config {
// Minimal advised work amount that should be processed during one call of the executable produced by Subgraph::generate
// Set by a backend, should be large enough to compensate for the kernel call overheads
size_t m_min_kernel_work_amount = 256;
bool m_enable_segfault_detector = true;
bool m_enable_segfault_detector = false;
};

/* The control flow of Snippets is built on Linear Intermediate Representation (Linear IR).
Expand Down
6 changes: 5 additions & 1 deletion src/common/snippets/src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "snippets/itt.hpp"

bool g_enable_snippets_err_detector = false;

namespace ov {
namespace snippets {

Expand Down Expand Up @@ -44,13 +46,15 @@ void Generator::generate(lowered::LinearIR& linear_ir, LoweringResult& result, c
}
OV_ITT_TASK_NEXT(GENERATE, "::GetSnippet")

// Note: some emitters use precompiled kernels. They need to be saved, so the kernels are accessible at runtime.
if (linear_ir.get_config().m_enable_segfault_detector) {
// Need save emitters to print usefull info in runtime if segfault happens.
for (const auto& expr : linear_ir) {
const auto& emitter = expr->get_emitter();
result.m_saved_emitters.emplace_back(emitter);
}
g_enable_snippets_err_detector = true;
} else if (linear_ir.get_config().m_save_expressions) {
// Note: some emitters use precompiled kernels. They need to be saved, so the kernels are accessible at runtime.
for (const auto& expr : linear_ir) {
const auto& emitter = expr->get_emitter();
if (uses_precompiled_kernel(emitter))
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/intel_cpu/src/emitters/x64/jit_dnnl_emitters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ size_t jit_dnnl_emitter::get_inputs_num() const { return 1; }

void jit_dnnl_emitter::emit_code(const std::vector<size_t> &in_vec_idxs, const std::vector<size_t> &out_vec_idxs,
const std::vector<size_t> &pool_vec_idxs, const std::vector<size_t> &pool_gpr_idxs) const {
if (g_enable_snippets_err_detector)
build_debug_info();
if (host_isa_ == cpu::x64::sse41) {
if (out_vec_idxs[0] != in_vec_idxs[0])
h->uni_vmovups(Xmm(out_vec_idxs[0]), Xmm(in_vec_idxs[0]));
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/intel_cpu/src/emitters/x64/jit_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace Xbyak;
namespace ov {
namespace intel_cpu {

std::shared_ptr<ThreadLocal<jit_emitter*>> g_debug_err_handler = std::make_shared<ThreadLocal<jit_emitter*>>();
std::shared_ptr<ThreadLocal<jit_emitter*>> g_snippets_err_handler = std::make_shared<ThreadLocal<jit_emitter*>>();

size_t jit_emitter::get_max_vecs_count() const {
return one_of(host_isa_, cpu::x64::avx512_core, cpu::x64::avx512_core) ? 32 : 16;
Expand Down Expand Up @@ -211,7 +211,8 @@ void jit_emitter::emit_code(const std::vector<size_t> &in_idxs, const std::vecto
const std::vector<size_t> &pool_vec_idxs, const std::vector<size_t> &pool_gpr_idxs) const {
emitter_preamble(in_idxs, out_idxs, pool_vec_idxs, pool_gpr_idxs);

build_debug_info();
if (g_enable_snippets_err_detector)
build_debug_info();

emit_impl(in_idxs, out_idxs);

Expand All @@ -232,7 +233,7 @@ void jit_emitter::build_debug_info() const {
}

void jit_emitter::set_local_handler(jit_emitter* emitter_address) {
g_debug_err_handler->local() = emitter_address;
g_snippets_err_handler->local() = emitter_address;
}

} // namespace intel_cpu
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/intel_cpu/src/emitters/x64/jit_emitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

#pragma once
#include <cxxabi.h>
#include <ie_common.h>
#include <cpu/x64/jit_generator.hpp>

Expand All @@ -15,13 +14,19 @@

#include <set>

#ifndef _WIN32
#include <cxxabi.h>
#endif

using namespace ov::threading;

extern bool g_enable_snippets_err_detector;

namespace ov {
namespace intel_cpu {

class jit_emitter;
extern std::shared_ptr<ThreadLocal<jit_emitter*>> g_debug_err_handler;
extern std::shared_ptr<ThreadLocal<jit_emitter*>> g_snippets_err_handler;

enum emitter_in_out_map {
vec_to_vec,
Expand Down
64 changes: 34 additions & 30 deletions src/plugins/intel_cpu/src/emitters/x64/jit_load_store_emitters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,25 @@ size_t jit_load_emitter::aux_gprs_count() const {
}

void jit_load_emitter::emit_impl(const std::vector<size_t> &in_idxs, const std::vector<size_t> &out_idxs) const {
// save runtime debug info
h->push(h->r15);
Xbyak::Label label_set_current;
h->mov(h->r15, reinterpret_cast<size_t>(&start_address));
h->cmp(h->qword[h->r15], 0);
h->jne(label_set_current);
h->mov(h->qword[h->r15], Xbyak::Reg64(in_idxs[0]));
h->L(label_set_current);
{
h->mov(h->r15, reinterpret_cast<size_t>(&current_address));
if (g_enable_snippets_err_detector) {
// save runtime debug info
h->push(h->r15);
Xbyak::Label label_set_current;
h->mov(h->r15, reinterpret_cast<size_t>(&start_address));
h->cmp(h->qword[h->r15], 0);
h->jne(label_set_current);
h->mov(h->qword[h->r15], Xbyak::Reg64(in_idxs[0]));

// iteration++
h->mov(h->r15, reinterpret_cast<size_t>(&iteration));
h->add(h->qword[h->r15], 0x01);
h->L(label_set_current);
{
h->mov(h->r15, reinterpret_cast<size_t>(&current_address));
h->mov(h->qword[h->r15], Xbyak::Reg64(in_idxs[0]));

// iteration++
h->mov(h->r15, reinterpret_cast<size_t>(&iteration));
h->add(h->qword[h->r15], 0x01);
}
h->pop(h->r15);
}
h->pop(h->r15);

const int offset = in_idxs.size() == 2 ? in_idxs[1] : 0;
if (host_isa_ == cpu::x64::sse41) {
Expand Down Expand Up @@ -686,23 +688,25 @@ void jit_store_emitter::emit_data() const {
}

void jit_store_emitter::emit_impl(const std::vector<size_t> &in_idxs, const std::vector<size_t> &out_idxs) const {
// save runtime debug info
h->push(h->r15);
Xbyak::Label label_set_current;
h->mov(h->r15, reinterpret_cast<size_t>(&start_address));
h->cmp(h->qword[h->r15], 0);
h->jne(label_set_current);
h->mov(h->qword[h->r15], Xbyak::Reg64(out_idxs[0]));
h->L(label_set_current);
{
h->mov(h->r15, reinterpret_cast<size_t>(&current_address));
if (g_enable_snippets_err_detector) {
// save runtime debug info
h->push(h->r15);
Xbyak::Label label_set_current;
h->mov(h->r15, reinterpret_cast<size_t>(&start_address));
h->cmp(h->qword[h->r15], 0);
h->jne(label_set_current);
h->mov(h->qword[h->r15], Xbyak::Reg64(out_idxs[0]));

// iteration++
h->mov(h->r15, reinterpret_cast<size_t>(&iteration));
h->add(h->qword[h->r15], 0x01);
h->L(label_set_current);
{
h->mov(h->r15, reinterpret_cast<size_t>(&current_address));
h->mov(h->qword[h->r15], Xbyak::Reg64(out_idxs[0]));

// iteration++
h->mov(h->r15, reinterpret_cast<size_t>(&iteration));
h->add(h->qword[h->r15], 0x01);
}
h->pop(h->r15);
}
h->pop(h->r15);

const int offset = in_idxs.size() == 2 ? in_idxs[1] : 0;
if (host_isa_ == cpu::x64::sse41) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class jit_load_emitter : public jit_emitter {
size_t start_address = 0;
size_t current_address = 0;
size_t iteration = 0;
std::shared_ptr<snippets::op::Store> m_load_node;
};

class jit_store_emitter : public jit_emitter {
Expand Down
26 changes: 7 additions & 19 deletions src/plugins/intel_cpu/src/emitters/x64/jit_snippets_emitters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "transformations/snippets/x64/op/brgemm_copy_b.hpp"
#include "transformations/snippets/x64/op//brgemm_cpu.hpp"
#include "snippets/op/rank_normalization.hpp"
// #include <cxxabi.h>

using namespace InferenceEngine;
using namespace Xbyak;
Expand All @@ -28,19 +27,6 @@ using ExpressionPtr = ov::snippets::lowered::ExpressionPtr;

namespace {
constexpr size_t gpr_size = 8;

// std::string get_type_name(const jit_emitter* emitter) {
// std::string name = typeid(*emitter).name();
// #ifndef _WIN32
// int status;
// std::unique_ptr<char, void (*)(void*)> demangled_name(
// abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status),
// std::free);
// name = demangled_name.get();
// #endif
// return name;
// }

} // namespace

inline static void transform_idxs_to_regs(const std::vector<size_t>& idxs, std::vector<Reg64>& regs) {
Expand Down Expand Up @@ -115,10 +101,7 @@ KernelEmitter::KernelEmitter(jit_generator* h, cpu_isa_t isa, const ExpressionPt
: jit_container_emitter(h, isa, expr),
reg_indexes_idx(abi_param1.getIdx()),
reg_const_params_idx(abi_param2.getIdx()) {
// const auto kernel = ov::as_type_ptr<snippets::op::Kernel>(expr->get_node());
m_kernel_node = ov::as_type_ptr<snippets::op::Kernel>(expr->get_node());
// const auto kernel = ov::as_type_ptr<snippets::op::Kernel>(n);
const auto kernel = m_kernel_node;
const auto kernel = ov::as_type_ptr<snippets::op::Kernel>(expr->get_node());
if (!kernel)
IE_THROW() << "KernelEmitter invoked with invalid op argument";
if (kernel->region.empty())
Expand Down Expand Up @@ -224,7 +207,8 @@ KernelEmitter::KernelEmitter(jit_generator* h, cpu_isa_t isa, const ExpressionPt
void KernelEmitter::emit_code(const std::vector<size_t> &in,
const std::vector<size_t> &out) const {
validate_arguments(in, out);
build_debug_info();
if (g_enable_snippets_err_detector)
build_debug_info();
emit_impl(in, out);
}

Expand Down Expand Up @@ -375,6 +359,8 @@ void LoopBeginEmitter::print_debug_info() const {
void LoopBeginEmitter::emit_code(const std::vector<size_t> &in,
const std::vector<size_t> &out) const {
validate_arguments(in, out);
if (g_enable_snippets_err_detector)
build_debug_info();
emit_impl(in, out);
}

Expand Down Expand Up @@ -430,6 +416,8 @@ void LoopEndEmitter::print_debug_info() const {
void LoopEndEmitter::emit_code(const std::vector<size_t> &in,
const std::vector<size_t> &out) const {
validate_arguments(in, out);
if (g_enable_snippets_err_detector)
build_debug_info();
emit_impl(in, out);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class KernelEmitter : public jit_container_emitter {

const size_t reg_indexes_idx;
const size_t reg_const_params_idx;
std::shared_ptr<snippets::op::Kernel> m_kernel_node;
};

class LoopBeginEmitter : public jit_emitter {
Expand Down
36 changes: 20 additions & 16 deletions src/plugins/intel_cpu/src/nodes/subgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,16 @@ void Snippet::SnippetJitExecutor::schedule_6d(const std::vector<MemoryPtr>& inMe
int64_t indexes[] = {d0, d1, d2, d3, d4};
jit_snippets_call_args call_args;
update_ptrs(call_args, inMemPtrs, outMemPtrs);
#ifdef __linux__
__sighandler_t signal_handler = [](int signal) {
ov::intel_cpu::g_debug_err_handler->local()->print_debug_info();
OPENVINO_THROW("Segfault was caught by the signal handler");
};
struct sigaction new_handler{};
new_handler.sa_handler = signal_handler;
sigaction(SIGSEGV, &new_handler, nullptr);
#ifndef _WIN32
if (g_enable_snippets_err_detector) {
__sighandler_t signal_handler = [](int signal) {
ov::intel_cpu::g_snippets_err_handler->local()->print_debug_info();
OPENVINO_THROW("Segfault was caught by the signal handler");
};
struct sigaction new_handler{};
new_handler.sa_handler = signal_handler;
sigaction(SIGSEGV, &new_handler, nullptr);
}
#endif
callable(indexes, &call_args);
});
Expand All @@ -530,14 +532,16 @@ void Snippet::SnippetJitExecutor::schedule_nt(const std::vector<MemoryPtr>& inMe
indexes[j] = static_cast<int64_t>(tmp % work_size[j]);
tmp /= work_size[j];
}
#ifdef __linux__
__sighandler_t signal_handler = [](int signal) {
ov::intel_cpu::g_debug_err_handler->local()->print_debug_info();
OPENVINO_THROW("Segfault was caught by the signal handler");
};
struct sigaction new_handler{};
new_handler.sa_handler = signal_handler;
sigaction(SIGSEGV, &new_handler, nullptr);
#ifndef _WIN32
if (g_enable_snippets_err_detector) {
__sighandler_t signal_handler = [](int signal) {
ov::intel_cpu::g_snippets_err_handler->local()->print_debug_info();
OPENVINO_THROW("Segfault was caught by the signal handler");
};
struct sigaction new_handler{};
new_handler.sa_handler = signal_handler;
sigaction(SIGSEGV, &new_handler, nullptr);
}
#endif
schedule.get_callable<kernel>()(indexes.data(), &call_args);
}
Expand Down

0 comments on commit 11e6bca

Please sign in to comment.