Skip to content

Commit 1740792

Browse files
sjain-stanfordcodex
andcommitted
Keep IREE device groups alive per handle
Moves HAL device group ownership to Handle so IREE topology and frontier state stay valid for the lifetime of the device. Co-Authored-By: GPT 5 <codex@openai.com> Signed-off-by: Sambhav Jain <sambhav@alumni.stanford.edu>
1 parent b616148 commit 1740792

3 files changed

Lines changed: 44 additions & 13 deletions

File tree

include/fusilli/backend/backend.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,14 @@ struct IreeHalDeviceDeleter {
377377
}
378378
};
379379

380+
// Custom deleter for IREE HAL device group.
381+
struct IreeHalDeviceGroupDeleter {
382+
void operator()(iree_hal_device_group_t *deviceGroup) const {
383+
if (deviceGroup)
384+
iree_hal_device_group_release(deviceGroup);
385+
}
386+
};
387+
380388
// Custom deleter for IREE VM context.
381389
struct IreeVmContextDeleter {
382390
void operator()(iree_vm_context_t *context) const {
@@ -413,6 +421,8 @@ struct IreeHalBufferViewDeleter {
413421
using IreeVmInstanceSharedPtrType = std::shared_ptr<iree_vm_instance_t>;
414422
using IreeHalDeviceUniquePtrType =
415423
std::unique_ptr<iree_hal_device_t, IreeHalDeviceDeleter>;
424+
using IreeHalDeviceGroupUniquePtrType =
425+
std::unique_ptr<iree_hal_device_group_t, IreeHalDeviceGroupDeleter>;
416426
using IreeVmContextUniquePtrType =
417427
std::unique_ptr<iree_vm_context_t, IreeVmContextDeleter>;
418428
using IreeAsyncFrontierTrackerUniquePtrType =

include/fusilli/backend/handle.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class Handle {
6060
"Handle::create got an unknown backend");
6161
}
6262

63+
FUSILLI_CHECK_ERROR(handle.createDeviceGroup());
64+
6365
return ok(std::move(handle));
6466
}
6567

@@ -104,6 +106,7 @@ class Handle {
104106
// Lazy create handle-specific IREE HAL device and populate the handle.
105107
FUSILLI_CHECK_ERROR(
106108
handle.createAMDGPUDevice(/*deviceId=*/deviceId, /*stream=*/stream));
109+
FUSILLI_CHECK_ERROR(handle.createDeviceGroup());
107110

108111
return ok(std::move(handle));
109112
}
@@ -121,8 +124,7 @@ class Handle {
121124

122125
Backend getBackend() const { return backend_; }
123126

124-
// Allow Graph and Buffer to access private Handle methods `getDevice()`
125-
// and `getInstance()`.
127+
// Allow Graph and Buffer to access private Handle methods.
126128
friend class Graph;
127129
friend class Buffer;
128130

@@ -139,6 +141,10 @@ class Handle {
139141
// Definition in `fusilli/backend/runtime.h`.
140142
ErrorObject createAMDGPUDevice(int deviceId, uintptr_t stream);
141143

144+
// Creates IREE HAL device group for this handle's device. Definition in
145+
// `fusilli/backend/runtime.h`.
146+
ErrorObject createDeviceGroup();
147+
142148
// Private constructor (use factory `create` method for handle creation).
143149
Handle(Backend backend, IreeVmInstanceSharedPtrType instance)
144150
: backend_(backend), instance_(std::move(instance)) {}
@@ -149,6 +155,12 @@ class Handle {
149155
// valid as long as this handle exists.
150156
iree_hal_device_t *getDevice() const { return device_.get(); }
151157

158+
// Returns a raw pointer to the underlying IREE HAL device group.
159+
// WARNING: The returned raw pointer is not safe to store since
160+
// its lifetime is tied to the `Handle` object and only
161+
// valid as long as this handle exists.
162+
iree_hal_device_group_t *getDeviceGroup() const { return deviceGroup_.get(); }
163+
152164
// Returns a raw pointer to the underlying IREE VM instance.
153165
// WARNING: The returned raw pointer is not safe to store since
154166
// its lifetime is tied to the `Handle` objects and only
@@ -160,6 +172,7 @@ class Handle {
160172
Backend backend_;
161173
IreeVmInstanceSharedPtrType instance_;
162174
IreeHalDeviceUniquePtrType device_;
175+
IreeHalDeviceGroupUniquePtrType deviceGroup_;
163176
};
164177

165178
} // namespace fusilli

include/fusilli/backend/runtime.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ inline ErrorObject Handle::createCPUDevice() {
159159
return ok();
160160
}
161161

162+
inline ErrorObject Handle::createDeviceGroup() {
163+
FUSILLI_LOG_LABEL_ENDL("INFO: Creating per-handle IREE HAL device group");
164+
iree_allocator_t allocator = iree_allocator_system();
165+
166+
iree_async_frontier_tracker_t *rawFrontierTracker = nullptr;
167+
FUSILLI_CHECK_ERROR(iree_async_frontier_tracker_create(
168+
iree_async_frontier_tracker_options_default(), allocator,
169+
&rawFrontierTracker));
170+
IreeAsyncFrontierTrackerUniquePtrType frontierTracker(rawFrontierTracker);
171+
172+
iree_hal_device_group_t *rawDeviceGroup = nullptr;
173+
FUSILLI_CHECK_ERROR(iree_hal_device_group_create_from_device(
174+
getDevice(), frontierTracker.get(), allocator, &rawDeviceGroup));
175+
deviceGroup_ = IreeHalDeviceGroupUniquePtrType(rawDeviceGroup);
176+
177+
return ok();
178+
}
179+
162180
// Copied from the IREE runtime code.
163181
#define HIP_DEVICE_ID_TO_IREE_DEVICE_ID(device) \
164182
(iree_hal_device_id_t)((device) + 1)
@@ -239,21 +257,11 @@ inline ErrorObject Graph::createVmContext(const Handle &handle,
239257

240258
// Create HAL module and register it with the context.
241259
{
242-
iree_async_frontier_tracker_t *rawFrontierTracker = nullptr;
243-
FUSILLI_CHECK_ERROR(iree_async_frontier_tracker_create(
244-
iree_async_frontier_tracker_options_default(), allocator,
245-
&rawFrontierTracker));
246-
IreeAsyncFrontierTrackerUniquePtrType frontierTracker(rawFrontierTracker);
247-
248-
iree_hal_device_group_t *deviceGroup = nullptr;
249-
FUSILLI_CHECK_ERROR(iree_hal_device_group_create_from_device(
250-
handle.getDevice(), frontierTracker.get(), allocator, &deviceGroup));
251260
iree_vm_module_t *halModule = nullptr;
252261
FUSILLI_CHECK_ERROR(iree_hal_module_create(
253262
handle.getInstance(), iree_hal_module_device_policy_default(),
254-
deviceGroup, IREE_HAL_MODULE_FLAG_NONE,
263+
handle.getDeviceGroup(), IREE_HAL_MODULE_FLAG_NONE,
255264
iree_hal_module_debug_sink_null(), allocator, &halModule));
256-
iree_hal_device_group_release(deviceGroup);
257265
iree_status_t status = iree_vm_context_register_modules(
258266
vmContext_.get(), /*module_count=*/1, &halModule);
259267
iree_vm_module_release(halModule);

0 commit comments

Comments
 (0)