Skip to content

Commit f9d892a

Browse files
authored
Cleaning up iree_hal_module_debug_sink_t destroy. (#20841)
It was not properly added to the inline HAL, did not handle the status, and was misnamed for what it was doing (destroy = delete). It's still not great that this grew this method but it was confusing as it was. If debug sinks need lifetime management they should be promoted to a full ref counted type instead of an on-stack by-value struct as was the original intent.
1 parent 51d6412 commit f9d892a

File tree

6 files changed

+38
-42
lines changed

6 files changed

+38
-42
lines changed

runtime/bindings/python/hal.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,10 @@ iree_hal_module_debug_sink_t HalModuleDebugSink::AsIreeHalModuleDebugSink()
11371137
const {
11381138
iree_hal_module_debug_sink_t res;
11391139
memset(&res, 0, sizeof(res));
1140+
res.release.fn = HalModuleDebugSink::ReleaseCallback;
1141+
res.release.user_data = const_cast<HalModuleDebugSink*>(this);
11401142
res.buffer_view_trace.fn = HalModuleDebugSink::IreeHalModuleBufferViewTrace;
11411143
res.buffer_view_trace.user_data = const_cast<HalModuleDebugSink*>(this);
1142-
res.destroy.fn = HalModuleDebugSink::DestroyCallback;
1143-
res.destroy.user_data = const_cast<HalModuleDebugSink*>(this);
11441144
return res;
11451145
}
11461146

@@ -1161,11 +1161,10 @@ static std::vector<HalBufferView> CreateHalBufferViewVector(
11611161
return res;
11621162
}
11631163

1164-
iree_status_t HalModuleDebugSink::DestroyCallback(void* user_data) {
1164+
void HalModuleDebugSink::ReleaseCallback(void* user_data) {
11651165
HalModuleDebugSink* debug_sink =
11661166
reinterpret_cast<HalModuleDebugSink*>(user_data);
11671167
debug_sink->dec_ref();
1168-
return iree_ok_status();
11691168
}
11701169

11711170
iree_status_t HalModuleDebugSink::IreeHalModuleBufferViewTrace(

runtime/bindings/python/hal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class HalModuleDebugSink : public py::intrusive_base {
334334
private:
335335
HalModuleBufferViewTraceCallback buffer_view_trace_callback_;
336336

337-
static iree_status_t DestroyCallback(void* user_data);
337+
static void ReleaseCallback(void* user_data);
338338

339339
static iree_status_t IreeHalModuleBufferViewTrace(
340340
void* user_data, iree_string_view_t key,

runtime/src/iree/modules/check/check_test.cc

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,27 +233,6 @@ iree_vm_instance_t* CheckTest::instance_ = nullptr;
233233
iree_vm_module_t* CheckTest::check_module_ = nullptr;
234234
iree_vm_module_t* CheckTest::hal_module_ = nullptr;
235235

236-
TEST_F(CheckTest, HalModuleDebugSinkDestroyCallbackIsCalled) {
237-
struct UserData {
238-
bool is_callback_called = false;
239-
};
240-
241-
iree_hal_module_debug_sink_t sink = {};
242-
sink.destroy.fn = [](void* user_data) {
243-
reinterpret_cast<UserData*>(user_data)->is_callback_called = true;
244-
return iree_ok_status();
245-
};
246-
UserData user_data;
247-
sink.destroy.user_data = &user_data;
248-
iree_vm_module_t* hal_module;
249-
IREE_ASSERT_OK(iree_hal_module_create(
250-
instance(), /*device_count=*/1, &device(), IREE_HAL_MODULE_FLAG_NONE,
251-
sink, iree_allocator_system(), &hal_module));
252-
IREE_ASSERT_FALSE(user_data.is_callback_called);
253-
iree_vm_module_release(hal_module);
254-
IREE_ASSERT_TRUE(user_data.is_callback_called);
255-
}
256-
257236
TEST_F(CheckTest, ExpectTrueSuccess) {
258237
IREE_ASSERT_OK(InvokeValue("expect_true", {iree_vm_value_make_i32(1)}));
259238
}

runtime/src/iree/modules/hal/debugging.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ extern "C" {
1919
// Debug Sink
2020
//===----------------------------------------------------------------------===//
2121

22-
// Receives a
22+
typedef void(IREE_API_PTR* iree_hal_module_debug_sink_release_callback_fn_t)(
23+
void* user_data);
24+
25+
typedef struct iree_hal_module_debug_sink_release_callback_t {
26+
iree_hal_module_debug_sink_release_callback_fn_t fn;
27+
void* user_data;
28+
} iree_hal_module_debug_sink_release_callback_t;
29+
2330
typedef iree_status_t(
2431
IREE_API_PTR* iree_hal_module_buffer_view_trace_callback_fn_t)(
2532
void* user_data, iree_string_view_t key, iree_host_size_t buffer_view_count,
@@ -30,23 +37,16 @@ typedef struct iree_hal_module_buffer_view_trace_callback_t {
3037
void* user_data;
3138
} iree_hal_module_buffer_view_trace_callback_t;
3239

33-
typedef iree_status_t(
34-
IREE_API_PTR* iree_hal_module_debug_sink_destroy_callback_fn_t)(
35-
void* user_data);
36-
37-
// Called by the runtime when the HAL module no longer needs the debug sink.
38-
typedef struct iree_hal_module_debug_sink_destroy_callback_t {
39-
iree_hal_module_debug_sink_destroy_callback_fn_t fn;
40-
void* user_data;
41-
} iree_hal_module_debug_sink_destroy_callback_t;
42-
4340
// Interface for a HAL module debug event sink.
44-
// Any referenced user data must remain live for the lifetime of the HAL module
45-
// the sink is provided to.
41+
// Any referenced user data must remain live until all users of the sink have
42+
// released it via the release callback.
4643
typedef struct iree_hal_module_debug_sink_t {
44+
// Called once per HAL module that has been provided the debug sink when it
45+
// will no longer be used. Optional if there's no lifetime management of user
46+
// data required.
47+
iree_hal_module_debug_sink_release_callback_t release;
4748
// Called on each hal.buffer_view.trace.
4849
iree_hal_module_buffer_view_trace_callback_t buffer_view_trace;
49-
iree_hal_module_debug_sink_destroy_callback_t destroy;
5050
} iree_hal_module_debug_sink_t;
5151

5252
// Returns a default debug sink that outputs nothing.

runtime/src/iree/modules/hal/inline/module.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,18 @@ typedef struct iree_hal_inline_module_state_t {
152152
} iree_hal_inline_module_state_t;
153153

154154
static void IREE_API_PTR iree_hal_inline_module_destroy(void* base_module) {
155+
IREE_TRACE_ZONE_BEGIN(z0);
156+
155157
iree_hal_inline_module_t* module = IREE_HAL_INLINE_MODULE_CAST(base_module);
158+
159+
if (module->debug_sink.release.fn) {
160+
module->debug_sink.release.fn(module->debug_sink.release.user_data);
161+
}
162+
156163
iree_hal_allocator_release(module->device_allocator);
157164
module->device_allocator = NULL;
165+
166+
IREE_TRACE_ZONE_END(z0);
158167
}
159168

160169
static iree_status_t IREE_API_PTR

runtime/src/iree/modules/hal/module.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,24 @@ typedef struct iree_hal_module_t {
4747
(iree_hal_module_t*)((uint8_t*)(module) + iree_vm_native_module_size());
4848

4949
static void IREE_API_PTR iree_hal_module_destroy(void* base_module) {
50+
IREE_TRACE_ZONE_BEGIN(z0);
51+
5052
iree_hal_module_t* module = IREE_HAL_MODULE_CAST(base_module);
5153

52-
if (module->debug_sink.destroy.fn) {
53-
module->debug_sink.destroy.fn(module->debug_sink.destroy.user_data);
54+
// Release the debug sink prior to releasing devices as it may be caching
55+
// device-specific information that will be unavailable once the devices are
56+
// released.
57+
if (module->debug_sink.release.fn) {
58+
module->debug_sink.release.fn(module->debug_sink.release.user_data);
5459
}
5560

61+
// Release all devices. The module may be the last retainer and the devices
62+
// (and their corresponding drivers) may be immediately unloaded.
5663
for (iree_host_size_t i = 0; i < module->device_count; ++i) {
5764
iree_hal_device_release(module->devices[i]);
5865
}
66+
67+
IREE_TRACE_ZONE_END(z0);
5968
}
6069

6170
typedef struct iree_hal_module_state_t {

0 commit comments

Comments
 (0)