@@ -2257,6 +2257,19 @@ const HostCallbackMap &get_host_callback_map() {
22572257#if WITH_WAMR
22582258void halide_print_native (wasm_exec_env_t exec_env, int32_t ucon, const char *str);
22592259void halide_error_native (wasm_exec_env_t exec_env, int32_t ucon, const char *str);
2260+ int32_t halide_trace_helper_native (wasm_exec_env_t exec_env,
2261+ int32_t ucon,
2262+ int32_t func_name,
2263+ int32_t value,
2264+ int32_t coordinates,
2265+ int32_t type_code,
2266+ int32_t type_bits,
2267+ int32_t type_lanes,
2268+ int32_t trace_code,
2269+ int32_t parent_id,
2270+ int32_t value_index,
2271+ int32_t dimensions,
2272+ int32_t trace_tag);
22602273
22612274int32_t malloc_native (wasm_exec_env_t exec_env, int32_t size) {
22622275 wasm_module_inst_t module_inst = wasm_runtime_get_module_inst (exec_env);
@@ -2564,9 +2577,8 @@ void wamr_extern_wrapper_n(wasm_exec_env_t exec_env, uint64_t *args) {
25642577
25652578template <size_t ... Is>
25662579constexpr auto make_wrapper_table (std::index_sequence<Is...>) {
2567- return std::array<void (*)(wasm_exec_env_t , uint64_t *), sizeof ...(Is)>{
2568- &wamr_extern_wrapper_n<Is>...
2569- };
2580+ return std::array<void (*)(wasm_exec_env_t , uint64_t *), sizeof ...(Is)>{
2581+ &wamr_extern_wrapper_n<Is>...};
25702582}
25712583
25722584constexpr size_t kMaxExternWrappers = 128 ;
@@ -2596,6 +2608,45 @@ void halide_error_native(wasm_exec_env_t exec_env, int32_t ucon, const char *str
25962608 }
25972609}
25982610
2611+ int32_t halide_trace_helper_native (wasm_exec_env_t exec_env,
2612+ int32_t ucon,
2613+ int32_t func_name_ptr,
2614+ int32_t value_ptr,
2615+ int32_t coordinates_ptr,
2616+ int32_t type_code,
2617+ int32_t type_bits,
2618+ int32_t type_lanes,
2619+ int32_t trace_code,
2620+ int32_t parent_id,
2621+ int32_t value_index,
2622+ int32_t dimensions,
2623+ int32_t trace_tag_ptr) {
2624+ wasm_module_inst_t instance = wasm_runtime_get_module_inst (exec_env);
2625+ WasmModuleContents *contents = (WasmModuleContents *)wasm_runtime_get_custom_data (instance);
2626+ JITUserContext *jit_user_context = contents ? contents->jit_user_context : nullptr ;
2627+
2628+ uint8_t *base = (uint8_t *)wasm_runtime_addr_app_to_native (instance, 0 );
2629+
2630+ halide_trace_event_t event;
2631+ event.func = (const char *)(base + func_name_ptr);
2632+ event.value = value_ptr ? ((void *)(base + value_ptr)) : nullptr ;
2633+ event.coordinates = coordinates_ptr ? ((int32_t *)(base + coordinates_ptr)) : nullptr ;
2634+ event.trace_tag = (const char *)(base + trace_tag_ptr);
2635+ event.type .code = (halide_type_code_t )type_code;
2636+ event.type .bits = (uint8_t )type_bits;
2637+ event.type .lanes = (uint16_t )type_lanes;
2638+ event.event = (halide_trace_event_code_t )trace_code;
2639+ event.parent_id = parent_id;
2640+ event.value_index = value_index;
2641+ event.dimensions = dimensions;
2642+
2643+ int32_t result = 0 ;
2644+ if (jit_user_context && jit_user_context->handlers .custom_trace != nullptr ) {
2645+ result = (*jit_user_context->handlers .custom_trace )(jit_user_context, &event);
2646+ }
2647+ return result;
2648+ }
2649+
25992650bool should_skip_extern_symbol (const std::string &name) {
26002651 static std::set<std::string> symbols = {
26012652 " halide_print" ,
@@ -2641,6 +2692,7 @@ WasmModuleContents::WasmModuleContents(
26412692 static NativeSymbol native_symbols[] = {
26422693 {" halide_print" , (void *)halide_print_native, " (i$)" , NULL },
26432694 {" halide_error" , (void *)halide_error_native, " (i$)" , NULL },
2695+ {" halide_trace_helper" , (void *)halide_trace_helper_native, " (iiiiiiiiiiii)i" , NULL },
26442696 {" malloc" , (void *)malloc_native, " (i)i" , NULL },
26452697 {" free" , (void *)free_native, " (i)" , NULL },
26462698 {" memcpy" , (void *)memcpy_native, " (iii)i" , NULL },
@@ -2715,7 +2767,7 @@ WasmModuleContents::WasmModuleContents(
27152767 internal_assert (success) << " Failed to build extern signature for " << fn_name << " \n " ;
27162768
27172769 internal_assert (current_extern_idx < kMaxExternWrappers ) << " Exceeded dynamic extern callback registration limit for WAMR" ;
2718-
2770+
27192771 wamr_extern_names.push_back (fn_name);
27202772 wamr_extern_arg_types.push_back (arg_types);
27212773 wamr_extern_trampoline_fns.push_back (trampoline_fn);
@@ -2999,7 +3051,7 @@ WasmModuleContents::WasmModuleContents(
29993051#if WITH_WAMR
30003052void WasmModuleContents::run_extern_callback (size_t index, wasm_exec_env_t exec_env, uint64_t *args) {
30013053 wasm_module_inst_t instance = wasm_runtime_get_module_inst (exec_env);
3002-
3054+
30033055 internal_assert (index < wamr_extern_names.size ());
30043056 const auto &arg_types = wamr_extern_arg_types[index];
30053057 TrampolineFn trampoline_fn = wamr_extern_trampoline_fns[index];
@@ -3028,7 +3080,7 @@ void WasmModuleContents::run_extern_callback(size_t index, wasm_exec_env_t exec_
30283080 buf_ptrs[i] = buf_ptr;
30293081 void *native_buf_ptr = wasm_runtime_addr_app_to_native (instance, buf_ptr);
30303082 internal_assert (native_buf_ptr);
3031-
3083+
30323084 // Fill buffer instance from interpreted wasm_halide_buffer_t
30333085 wasm_halide_buffer_t *src = (wasm_halide_buffer_t *)native_buf_ptr;
30343086 halide_buffer_t dst_tmp;
@@ -3047,7 +3099,7 @@ void WasmModuleContents::run_extern_callback(size_t index, wasm_exec_env_t exec_
30473099 void *src_host_native = wasm_runtime_addr_app_to_native (instance, src->host );
30483100 memcpy (buffers[i].raw_buffer ()->host , src_host_native, buffers[i].raw_buffer ()->size_in_bytes ());
30493101 }
3050-
3102+
30513103 trampoline_args[i] = buffers[i].raw_buffer ();
30523104 } else {
30533105 if (a.type .code == halide_type_int || a.type .code == halide_type_uint) {
0 commit comments