Skip to content

Commit

Permalink
encapsulate segfault detector related
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhu-wang committed Dec 28, 2023
1 parent fc9942b commit 520cf29
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 392 deletions.
4 changes: 0 additions & 4 deletions src/common/snippets/include/snippets/emitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ class Emitter {
virtual void emit_data() const {}

virtual ~Emitter() = default;

#ifdef SNIPPETS_DEBUG_CAPS
virtual void set_custom_segfault_detector(const bool is_enable) {}
#endif
};

} // namespace snippets
Expand Down
5 changes: 1 addition & 4 deletions src/common/snippets/include/snippets/target_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class TargetMachine {
*/
virtual size_t get_lanes() const = 0;


/**
* @brief called by generator to all the emitter for a target machine
* @return a map by node's type info with callbacks to create an instance of emitter for corresponding operation type
Expand All @@ -64,10 +65,6 @@ class TargetMachine {
bool has(const ov::DiscreteTypeInfo& type) const;
virtual ~TargetMachine() = default;

#ifdef SNIPPETS_DEBUG_CAPS
bool custom_segfault_detector = false;
#endif

protected:
std::map<const ov::DiscreteTypeInfo, jitters_value> jitters;
};
Expand Down
5 changes: 0 additions & 5 deletions src/common/snippets/src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ void Generator::generate(lowered::LinearIR& linear_ir, LoweringResult& result, c
loops2DKernel->compile_params = compile_params;
auto loops2DKernelExpr = linear_ir.create_expression(loops2DKernel, std::vector<lowered::PortConnectorPtr>{});
std::shared_ptr<Emitter> kernel = target->get(op::Kernel::get_type_info_static())(loops2DKernelExpr);
#ifdef SNIPPETS_DEBUG_CAPS
// This is entrance emitter outside of LIR and not derived from MemoryAccess, but access memory, manually enable it.
if (target->custom_segfault_detector)
kernel->set_custom_segfault_detector(true);
#endif

kernel->emit_code({}, {});

Expand Down
10 changes: 1 addition & 9 deletions src/common/snippets/src/lowered/linear_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,8 @@ void LinearIR::debug_print(bool tds_as_pointers) const {

void LinearIR::init_emitters(const std::shared_ptr<TargetMachine>& target) {
for (auto& expr : m_expressions) {
if (!expr->get_emitter()) {
if (!expr->get_emitter())
expr->m_emitter = target->get(expr->get_node()->get_type_info())(expr);
#ifdef SNIPPETS_DEBUG_CAPS
if (target->custom_segfault_detector) {
if (is_type<ov::snippets::op::MemoryAccess>(expr->get_node())) {
expr->m_emitter->set_custom_segfault_detector(true);
}
}
#endif
}
}
}

Expand Down
74 changes: 74 additions & 0 deletions src/plugins/intel_cpu/src/emitters/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,79 @@ void RegPrinter::print_reg(jit_generator &h, REG_T reg, const char *name) {
postamble(h);
}

#ifdef SNIPPETS_DEBUG_CAPS
std::string get_type_name(const jit_emitter* emitter);
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;
}

void print_segfault_detector_result(jit_uni_segfault_detector_emitter* detector_emitter) {
auto print_memory_emitter_info = [&](MemoryEmitter *memory_emitter) {
std::cerr << "detailed emitter info is, src precision:" << memory_emitter->src_prc << ", dst precision:" << memory_emitter->dst_prc
<< ", load/store element number:" << memory_emitter->count
<< ", byte offset" << memory_emitter->byte_offset << std::endl;
// more memory address info tracked in detector_emitter.
std::cerr << "start_address:" << detector_emitter->start_address
<< ", current_address:" << detector_emitter->current_address
<< ", iteration:" << detector_emitter->iteration << "\n";
};
auto print_brgemm_emitter_info = [&](BrgemmEmitter* brgemm_emitter) {
std::cerr << "detailed emitter info is, m_brgCtx.M:" << brgemm_emitter->m_brgCtx.M
<< " m_brgCtx.K:" << brgemm_emitter->m_brgCtx.K
<< " m_brgCtx.N:" << brgemm_emitter->m_brgCtx.N
<< " m_brgCtx.LDA:" << brgemm_emitter->m_brgCtx.LDA
<< " m_brgCtx.LDB:" << brgemm_emitter->m_brgCtx.LDB
<< " m_brgCtx.LDC:" << brgemm_emitter->m_brgCtx.LDC
<< " m_brgCtx.dt_in0:" << brgemm_emitter->m_brgCtx.dt_in0
<< " m_brgCtx.dt_in1:" << brgemm_emitter->m_brgCtx.dt_in1
<< " m_brgCtx.palette:" << brgemm_emitter->m_brgCtx.palette
<< " m_brgCtx.is_with_amx:" << brgemm_emitter->m_brgCtx.is_with_amx
<< " m_brgCtx.is_with_comp:" << brgemm_emitter->m_brgCtx.is_with_comp
<< " m_brgCtx.beta:" << brgemm_emitter->m_brgCtx.beta
<< " m_load_offset_a:" << brgemm_emitter->m_load_offset_a
<< " m_load_offset_b:" << brgemm_emitter->m_load_offset_b
<< " m_load_offset_scratch:" << brgemm_emitter->m_load_offset_scratch
<< " m_store_offset_c:" << brgemm_emitter->m_store_offset_c
<< " m_with_scratch:" << brgemm_emitter->m_with_scratch
<< " m_with_comp:" << brgemm_emitter->m_with_comp << "\n";
};
auto print_brgemm_copy_emitter_info = [&](BrgemmCopyBEmitter* brgemm_copy_emitter) {
std::cerr << "detailed emitter info is, m_LDB:" << brgemm_copy_emitter->m_LDB
<< " m_K:" << brgemm_copy_emitter->m_K
<< " m_K_blk:" << brgemm_copy_emitter->m_K_blk
<< " m_K_tail:" << brgemm_copy_emitter->m_K_tail
<< " m_N:" << brgemm_copy_emitter->m_N
<< " m_N_blk:" << brgemm_copy_emitter->m_N_blk
<< " m_N_tail:" << brgemm_copy_emitter->m_N_tail
<< " m_brgemm_prc_in0:" << brgemm_copy_emitter->m_brgemm_prc_in0
<< " m_brgemm_prc_in1:" << brgemm_copy_emitter->m_brgemm_prc_in1
<< " m_brgemmVNNIFactor:" << brgemm_copy_emitter->m_brgemmVNNIFactor
<< " m_with_comp:" << brgemm_copy_emitter->m_with_comp
<< " m_in_offset:" << brgemm_copy_emitter->m_in_offset
<< " m_out_offset:" << brgemm_copy_emitter->m_out_offset
<< " m_comp_offset:" << brgemm_copy_emitter->m_comp_offset << "\n";
};
std::cerr << "Node name:" << detector_emitter->m_target_node_name << std::endl;
auto target_emitter = detector_emitter->m_target_emitter;
std::cerr << "Emitter type name:" << get_type_name(target_emitter) << std::endl;
if (auto *memory_emitter = dynamic_cast<MemoryEmitter *>(target_emitter)) {
print_memory_emitter_info(memory_emitter);
} else if (auto *brgemm_emitter = dynamic_cast<BrgemmEmitter *>(target_emitter)) {
print_brgemm_emitter_info(brgemm_emitter);
} else if (auto *brgemm_copy_emitter = dynamic_cast<BrgemmCopyBEmitter *>(target_emitter)) {
print_brgemm_copy_emitter_info(brgemm_copy_emitter);
}
}

#endif

} // namespace intel_cpu
} // namespace ov
7 changes: 7 additions & 0 deletions src/plugins/intel_cpu/src/emitters/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#pragma once

#include <cpu/x64/jit_generator.hpp>
#include "x64/jit_emitter.hpp"
#include "x64/jit_snippets_emitters.hpp"
#include "x64/jit_segfault_detector_emitter.hpp"

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

namespace ov {
namespace intel_cpu {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/intel_cpu/src/emitters/x64/cpu_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ bool intel_cpu::CPUGenerator::uses_precompiled_kernel(const std::shared_ptr<snip
bool need = std::dynamic_pointer_cast<intel_cpu::BrgemmEmitter>(e) ||
std::dynamic_pointer_cast<intel_cpu::BrgemmCopyBEmitter>(e);
#ifdef SNIPPETS_DEBUG_CAPS
need = need || target->custom_segfault_detector ||
const auto cpu_target_machine = std::dynamic_pointer_cast<intel_cpu::CPUTargetMachine>(target);
need = need || (cpu_target_machine && cpu_target_machine->custom_segfault_detector) ||
std::dynamic_pointer_cast<intel_cpu::jit_perf_count_chrono_start_emitter>(e) ||
std::dynamic_pointer_cast<intel_cpu::jit_perf_count_chrono_end_emitter>(e) ||
std::dynamic_pointer_cast<intel_cpu::jit_perf_count_rdtsc_start_emitter>(e) ||
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/intel_cpu/src/emitters/x64/cpu_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class CPUTargetMachine : public snippets::TargetMachine {
snippets::CompiledSnippetPtr get_snippet() override;
size_t get_lanes() const override;
dnnl::impl::cpu::x64::cpu_isa_t get_isa() const;
#ifdef SNIPPETS_DEBUG_CAPS
bool custom_segfault_detector = false;
#endif

private:
std::unique_ptr<dnnl::impl::cpu::x64::jit_generator> h;
Expand Down
14 changes: 0 additions & 14 deletions src/plugins/intel_cpu/src/emitters/x64/jit_conversion_emitters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ void jit_convert_truncation_emitter::dword2int8(const std::vector<size_t> &in_ve
}
}

#ifdef SNIPPETS_DEBUG_CAPS
void jit_convert_truncation_emitter::print_debug_info() const {
std::cerr << "Emitter type name:" << get_type_name(this) << "\n";
std::cerr << "input_type:" << input_type << " output_type" << output_type << "\n";
}
#endif

jit_convert_saturation_emitter::jit_convert_saturation_emitter(jit_generator *host, cpu_isa_t host_isa,
const std::shared_ptr<ov::Node>& node, ov::element::Type exec_prc)
: jit_convert_emitter(host, host_isa, node, exec_prc) {
Expand Down Expand Up @@ -338,12 +331,5 @@ size_t jit_convert_saturation_emitter::aux_vecs_count() const {
return output_type == ov::element::u8 && host_isa_ == dnnl::impl::cpu::x64::avx512_core? 1 : 0;
}

#ifdef SNIPPETS_DEBUG_CAPS
void jit_convert_saturation_emitter::print_debug_info() const {
std::cerr << "Emitter type name:" << get_type_name(this) << "\n";
std::cerr << "input_type:" << input_type << " output_type" << output_type << "\n";
}
#endif

} // namespace intel_cpu
} // namespace ov
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ class jit_convert_truncation_emitter : public jit_convert_emitter {
public:
jit_convert_truncation_emitter(dnnl::impl::cpu::x64::jit_generator *host, dnnl::impl::cpu::x64::cpu_isa_t host_isa,
const std::shared_ptr<ov::Node>& n, ov::element::Type exec_prc = ov::element::f32);
#ifdef SNIPPETS_DEBUG_CAPS
void print_debug_info() const override;
#endif

private:
void emit_impl(const std::vector<size_t>& in, const std::vector<size_t>& out) const override;
Expand All @@ -72,9 +69,6 @@ class jit_convert_saturation_emitter : public jit_convert_emitter {
public:
jit_convert_saturation_emitter(dnnl::impl::cpu::x64::jit_generator *host, dnnl::impl::cpu::x64::cpu_isa_t host_isa,
const std::shared_ptr<ov::Node>& n, ov::element::Type exec_prc = ov::element::f32);
#ifdef SNIPPETS_DEBUG_CAPS
void print_debug_info() const override;
#endif

private:
void emit_impl(const std::vector<size_t>& in, const std::vector<size_t>& out) const override;
Expand Down
11 changes: 0 additions & 11 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,10 +54,6 @@ 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 {
#ifdef SNIPPETS_DEBUG_CAPS
if (m_custom_emitter_segfault_detector)
build_debug_info();
#endif
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 Expand Up @@ -87,13 +83,6 @@ void jit_dnnl_emitter::emit_data() const {
}
}

#ifdef SNIPPETS_DEBUG_CAPS
void jit_dnnl_emitter::print_debug_info() const {
std::cerr << "Emitter type name:" << get_type_name(this) << "\n";
std::cerr << "dnnl_alg_kind:" << kind << " alpha" << alpha << " beta" << beta << "\n";
}
#endif

jit_dnnl_aux_emitter::jit_dnnl_aux_emitter(jit_generator *host, cpu_isa_t host_isa,
dnnl_alg_kind_t algKind, float inpAlpha, float inpBeta,
ov::element::Type exec_prc)
Expand Down
3 changes: 0 additions & 3 deletions src/plugins/intel_cpu/src/emitters/x64/jit_dnnl_emitters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class jit_dnnl_emitter : public jit_emitter {
void emit_impl(const std::vector<size_t> &in_idxs, const std::vector<size_t> &out_idxs) const override {};

static std::set<std::vector<element::Type>> get_supported_precisions(const std::shared_ptr<ov::Node>& node = nullptr);
#ifdef SNIPPETS_DEBUG_CAPS
void print_debug_info() const override;
#endif

protected:
jit_dnnl_emitter(dnnl::impl::cpu::x64::jit_generator *host, dnnl::impl::cpu::x64::cpu_isa_t host_isa,
Expand Down
15 changes: 1 addition & 14 deletions src/plugins/intel_cpu/src/emitters/x64/jit_eltwise_emitters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ void jit_power_dynamic_emitter::emit_isa(const std::vector<size_t> &in_vec_idxs,
h->add(h->rsp, n_gprs_to_save * gpr_size);
}


/// EQUAL ///
jit_equal_emitter::jit_equal_emitter(x64::jit_generator *host, x64::cpu_isa_t host_isa, const std::shared_ptr<ov::Node>& node, ov::element::Type exec_prc)
: jit_emitter(host, host_isa, exec_prc) {
Expand Down Expand Up @@ -1686,13 +1687,6 @@ size_t jit_power_static_emitter::aux_vecs_count() const {
return 1;
}

#ifdef SNIPPETS_DEBUG_CAPS
void jit_power_static_emitter::print_debug_info() const {
std::cerr << "Emitter type name:" << get_type_name(this) << "\n";
std::cerr << "power:" << power << " scale:" << scale << " shift:" << shift << "\n";
}
#endif

/// PRELU ///
jit_prelu_emitter::jit_prelu_emitter(x64::jit_generator* host,
x64::cpu_isa_t host_isa,
Expand Down Expand Up @@ -2192,13 +2186,6 @@ void jit_is_inf_emitter::register_table_entries() {
}
}

#ifdef SNIPPETS_DEBUG_CAPS
void jit_is_inf_emitter::print_debug_info() const {
std::cerr << "Emitter type name:" << get_type_name(this) << "\n";
std::cerr << "detect_negative:" << detect_negative << " detect_positive" << detect_positive << "\n";
}
#endif

/// IS_NAN ///
template <>
void jit_is_nan_emitter::emit_isa<x64::avx512_core>(const std::vector<size_t> &in_vec_idxs, const std::vector<size_t> &out_vec_idxs) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,7 @@ class jit_power_static_emitter : public jit_emitter {

size_t get_inputs_num() const override;
static std::set<std::vector<element::Type>> get_supported_precisions(const std::shared_ptr<ov::Node>& node = nullptr);
#ifdef SNIPPETS_DEBUG_CAPS
void print_debug_info() const override;
#endif


private:
void emit_impl(const std::vector<size_t> &in_vec_idxs, const std::vector<size_t> &out_vec_idxs) const override;
Expand Down Expand Up @@ -607,9 +605,6 @@ class jit_is_inf_emitter : public jit_emitter {
ov::element::Type execPrc = ov::element::f32): jit_emitter(host, hostIsa, execPrc) {
prepare_table();
}
#ifdef SNIPPETS_DEBUG_CAPS
void print_debug_info() const override;
#endif

size_t get_inputs_num() const override { return 1; };
static std::set<std::vector<element::Type>> get_supported_precisions(const std::shared_ptr<ov::Node>& node = nullptr) {
Expand Down
31 changes: 1 addition & 30 deletions src/plugins/intel_cpu/src/emitters/x64/jit_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
#include <vector>
#include "utils/general_utils.h"

using namespace dnnl::impl;
using namespace dnnl::impl::cpu;
using namespace dnnl::impl::cpu::x64;
using namespace dnnl::impl;
using namespace Xbyak;

namespace ov {
namespace intel_cpu {

#ifdef SNIPPETS_DEBUG_CAPS
std::shared_ptr<ThreadLocal<jit_emitter*>> g_custom_segfault_handler = std::make_shared<ThreadLocal<jit_emitter*>>();
#endif

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 @@ -213,35 +208,11 @@ 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);

#ifdef SNIPPETS_DEBUG_CAPS
if (m_custom_emitter_segfault_detector)
build_debug_info();
#endif

emit_impl(in_idxs, out_idxs);

emitter_postamble();
}

#ifdef SNIPPETS_DEBUG_CAPS
void jit_emitter::build_debug_info() const {
internal_call_preamble();

const auto &set_local_handler_overload = static_cast<void (*)(jit_emitter*)>(set_local_handler);
h->mov(h->rax, reinterpret_cast<size_t>(set_local_handler_overload));
h->mov(abi_param1, reinterpret_cast<uint64_t>(this));
internal_call_rsp_align();
h->call(h->rax);
internal_call_rsp_restore();

internal_call_postamble();
}

void jit_emitter::set_local_handler(jit_emitter* emitter_address) {
g_custom_segfault_handler->local() = emitter_address;
}
#endif

void jit_emitter::internal_call_preamble() const {
// gprs
Xbyak::Operand gprs_to_save[] = {h->r8, h->r9, h->r10, h->r11, h->r12, h->r13, h->r14, h->r15,
Expand Down
Loading

0 comments on commit 520cf29

Please sign in to comment.