Generate jitdump to support linux perf for LLVM JIT (#2788)

This commit is contained in:
liang.he
2023-11-27 15:42:00 +08:00
committed by GitHub
parent d7608690c0
commit 8aa813f44a
14 changed files with 165 additions and 14 deletions

View File

@ -158,7 +158,7 @@ static JitCompOptions jit_options = { 0 };
#endif
#if WASM_ENABLE_JIT != 0
static LLVMJITOptions llvm_jit_options = { 3, 3, 0 };
static LLVMJITOptions llvm_jit_options = { 3, 3, 0, false };
#endif
static RunningMode runtime_running_mode = Mode_Default;
@ -662,9 +662,14 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
#endif
#if WASM_ENABLE_JIT != 0
LOG_DEBUG("Start LLVM_JIT, opt_sz=%u, opt_lvl=%u, segue=%s, linux_perf=%s",
init_args->llvm_jit_size_level, init_args->llvm_jit_opt_level,
init_args->segue_flags ? "Yes" : "No",
init_args->linux_perf_support ? "Yes" : "No");
llvm_jit_options.size_level = init_args->llvm_jit_size_level;
llvm_jit_options.opt_level = init_args->llvm_jit_opt_level;
llvm_jit_options.segue_flags = init_args->segue_flags;
llvm_jit_options.linux_perf_support = init_args->linux_perf_support;
#endif
if (!wasm_runtime_env_init()) {

View File

@ -430,6 +430,7 @@ typedef struct LLVMJITOptions {
uint32 opt_level;
uint32 size_level;
uint32 segue_flags;
bool linux_perf_support;
} LLVMJITOptions;
#endif

View File

@ -653,6 +653,19 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, LLVMModuleRef module,
attr_no_jump_tables);
}
/* spread fp.all to every function */
if (comp_ctx->emit_frame_pointer) {
const char *key = "frame-pointer";
const char *val = "all";
LLVMAttributeRef no_omit_fp = LLVMCreateStringAttribute(
comp_ctx->context, key, strlen(key), val, strlen(val));
if (!no_omit_fp) {
aot_set_last_error("create LLVM attribute (frame-pointer) failed.");
goto fail;
}
LLVMAddAttributeAtIndex(func, LLVMAttributeFunctionIndex, no_omit_fp);
}
if (need_precheck) {
if (!comp_ctx->is_jit_mode)
LLVMSetLinkage(func, LLVMInternalLinkage);
@ -2160,7 +2173,7 @@ jit_stack_size_callback(void *user_data, const char *name, size_t namelen,
}
static bool
orc_jit_create(AOTCompContext *comp_ctx)
orc_jit_create(AOTCompContext *comp_ctx, bool linux_perf_support)
{
LLVMErrorRef err;
LLVMOrcLLLazyJITRef orc_jit = NULL;
@ -2200,6 +2213,14 @@ orc_jit_create(AOTCompContext *comp_ctx)
/* Ownership transfer: LLVMOrcLLJITBuilderRef -> LLVMOrcLLJITRef */
builder = NULL;
if (linux_perf_support) {
LOG_DEBUG("Enable linux perf support");
LLVMOrcObjectLayerRef obj_linking_layer =
(LLVMOrcObjectLayerRef)LLVMOrcLLLazyJITGetObjLinkingLayer(orc_jit);
LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(
obj_linking_layer, LLVMCreatePerfJITEventListener());
}
/* Ownership transfer: local -> AOTCompContext */
comp_ctx->orc_jit = orc_jit;
orc_jit = NULL;
@ -2298,6 +2319,17 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
goto fail;
}
if (option->linux_perf_support) {
/* FramePointerKind.All */
LLVMMetadataRef val =
LLVMValueAsMetadata(LLVMConstInt(LLVMInt32Type(), 2, false));
const char *key = "frame-pointer";
LLVMAddModuleFlag(comp_ctx->module, LLVMModuleFlagBehaviorWarning, key,
strlen(key), val);
comp_ctx->emit_frame_pointer = true;
}
if (BH_LIST_ERROR == bh_list_init(&comp_ctx->native_symbols)) {
goto fail;
}
@ -2401,7 +2433,7 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
goto fail;
/* Create LLJIT Instance */
if (!orc_jit_create(comp_ctx))
if (!orc_jit_create(comp_ctx, option->linux_perf_support))
goto fail;
}
else {

View File

@ -12,6 +12,7 @@
#include "llvm-c/Target.h"
#include "llvm-c/Core.h"
#include "llvm-c/Object.h"
#include "llvm-c/OrcEE.h"
#include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Analysis.h"
#include "llvm-c/BitWriter.h"
@ -422,6 +423,8 @@ typedef struct AOTCompContext {
char stack_usage_temp_file[64];
const char *llvm_passes;
const char *builtin_intrinsics;
bool emit_frame_pointer;
} AOTCompContext;
enum {
@ -431,6 +434,7 @@ enum {
AOT_LLVMIR_OPT_FILE,
};
/* always sync it with AOTCompOption in aot_export.h */
typedef struct AOTCompOption {
bool is_jit_mode;
bool is_indirect_mode;
@ -457,6 +461,7 @@ typedef struct AOTCompOption {
uint32 bounds_checks;
uint32 stack_bounds_checks;
uint32 segue_flags;
bool linux_perf_support;
char **custom_sections;
uint32 custom_sections_count;
const char *stack_usage_file;

View File

@ -12,11 +12,13 @@
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#endif
#include "llvm/ExecutionEngine/JITEventListener.h"
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Support/CBindingWrapping.h"
@ -108,6 +110,7 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectTransformLayer,
LLVMOrcObjectTransformLayerRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry,
LLVMOrcSymbolStringPoolEntryRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer, LLVMOrcObjectLayerRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool, LLVMOrcSymbolStringPoolRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef)
@ -322,3 +325,9 @@ LLVMOrcLLLazyJITGetObjTransformLayer(LLVMOrcLLLazyJITRef J)
{
return wrap(&unwrap(J)->getObjTransformLayer());
}
LLVMOrcObjectLayerRef
LLVMOrcLLLazyJITGetObjLinkingLayer(LLVMOrcLLLazyJITRef J)
{
return wrap(&unwrap(J)->getObjLinkingLayer());
}

View File

@ -76,5 +76,8 @@ LLVMOrcLLJITBuilderSetCompileFuncitonCreatorWithStackSizesCallback(
LLVMOrcLLLazyJITBuilderRef Builder,
void (*cb)(void *, const char *, size_t, size_t), void *cb_data);
LLVMOrcObjectLayerRef
LLVMOrcLLLazyJITGetObjLinkingLayer(LLVMOrcLLLazyJITRef J);
LLVM_C_EXTERN_C_END
#endif

View File

@ -70,6 +70,7 @@ typedef struct JitInterpSwitchInfo {
typedef struct JitCompOptions {
uint32 code_cache_size;
uint32 opt_level;
bool linux_perf_support;
} JitCompOptions;
bool

View File

@ -38,6 +38,7 @@ enum {
AOT_LLVMIR_OPT_FILE,
};
/* always sync it with AOTCompOption in compilation/aot_llvm.h */
typedef struct AOTCompOption {
bool is_jit_mode;
bool is_indirect_mode;
@ -64,6 +65,7 @@ typedef struct AOTCompOption {
uint32_t bounds_checks;
uint32_t stack_bounds_checks;
uint32_t segue_flags;
bool linux_perf_support;
char **custom_sections;
uint32_t custom_sections_count;
const char *stack_usage_file;

View File

@ -169,6 +169,15 @@ typedef struct RuntimeInitArgs {
uint32_t llvm_jit_size_level;
/* Segue optimization flags for LLVM JIT */
uint32_t segue_flags;
/**
* If enabled
* - llvm-jit will output a jitdump file for `perf inject`
* - aot. TBD
* - fast-jit. TBD
* - multi-tier-jit. TBD
* - interpreter. TBD
*/
bool linux_perf_support;
} RuntimeInitArgs;
#ifndef WASM_VALKIND_T_DEFINED
@ -945,7 +954,7 @@ wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
/**
* Set the memory bounds checks flag of a WASM module instance.
*
*
* @param module_inst the WASM module instance
* @param enable the flag to enable/disable the memory bounds checks
*/
@ -954,9 +963,8 @@ wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst,
bool enable);
/**
* Check if the memory bounds checks flag is enabled for a WASM module instance.
*
* @param module_inst the WASM module instance
*
* @param module_inst the WASM module instance
* @return true if the memory bounds checks flag is enabled, false otherwise
*/
WASM_RUNTIME_API_EXTERN bool

View File

@ -2878,6 +2878,7 @@ init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
option.opt_level = llvm_jit_options.opt_level;
option.size_level = llvm_jit_options.size_level;
option.segue_flags = llvm_jit_options.segue_flags;
option.linux_perf_support = llvm_jit_options.linux_perf_support;
#if WASM_ENABLE_BULK_MEMORY != 0
option.enable_bulk_memory = true;

View File

@ -1877,6 +1877,7 @@ init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
option.opt_level = llvm_jit_options.opt_level;
option.size_level = llvm_jit_options.size_level;
option.segue_flags = llvm_jit_options.segue_flags;
option.linux_perf_support = llvm_jit_options.linux_perf_support;
#if WASM_ENABLE_BULK_MEMORY != 0
option.enable_bulk_memory = true;