Commit 2f3b912
authored
[Backend] Enable default device sharing across handles for multi threaded handle+device creation (#114)
### Issue
The `Multi-threaded Handle creation` test was failing flakily on my
recent local runs:
```
/home/srajeshk/fusilli/tests/test_handle.cpp:83: FAILED:
REQUIRE( !creationFailed.load() )
with expansion:
false
```
The test spawns 32 threads that simultaneously attempt to create CPU
handles, and at least one handle creation was failing.
### Root Cause
IREE's HAL drivers (both `local-task` for CPU and HIP for GPU) provide a
**single default device** per configuration. When multiple threads
concurrently called `iree_runtime_instance_try_create_default_device()`,
each attempting to create its own device:
1. The first thread succeeded in creating the device
2. Subsequent threads failed because the default device already existed
IREE's default device model seems to support one "default" device per
driver, not one per handle.
### Fix
Implement **device sharing** across handles using a `weak_ptr` caching
pattern (similar to the existing `createSharedInstance()` pattern for
the runtime instance):
**For CPU (`createCPUDevice`):**
- Single shared device across all CPU handles
- Uses `static std::weak_ptr<iree_hal_device_t>` with mutex protection
- First handle creates the device; subsequent handles reuse it
**For AMDGPU (`createAMDGPUDevice`):**
- Devices cached by `(deviceId, stream)` configuration
- Uses `static std::map<key, std::weak_ptr<iree_hal_device_t>>` with
mutex protection
- Handles with the same configuration share the device; different
configurations get separate devices
### Changes
| File | Change |
|------|--------|
| `backend.h` | Added `IreeHalDeviceSharedPtrType`, removed unused
`IreeHalDeviceUniquePtrType` |
| `handle.h` | Changed `device_` from `unique_ptr` to `shared_ptr` |
| `runtime.h` | Implemented device sharing in `createCPUDevice()` and
`createAMDGPUDevice()` |
| `test_handle.cpp` | Added multi-threaded test for AMDGPU backend |
### Test Plan
- [x] `Multi-threaded Handle creation` test passes (CPU backend)
- [x] `Multi-threaded Handle creation AMDGPU` test passes (GPU backend)
### Disclaimer
- PR description and code fixes generated with assistance from Claude
4.5 Opus under my supervision.
---------
Signed-off-by: Sambhav Jain <[email protected]>1 parent dc4810f commit 2f3b912
File tree
4 files changed
+117
-16
lines changed- include/fusilli/backend
- tests
4 files changed
+117
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
356 | 356 | | |
357 | 357 | | |
358 | 358 | | |
359 | | - | |
360 | | - | |
| 359 | + | |
361 | 360 | | |
362 | 361 | | |
363 | 362 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
164 | 167 | | |
165 | 168 | | |
166 | 169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| |||
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
64 | | - | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
75 | | - | |
76 | | - | |
77 | | - | |
| 76 | + | |
78 | 77 | | |
79 | 78 | | |
80 | 79 | | |
| |||
106 | 105 | | |
107 | 106 | | |
108 | 107 | | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
| 108 | + | |
| 109 | + | |
113 | 110 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
117 | 138 | | |
| 139 | + | |
118 | 140 | | |
119 | 141 | | |
120 | 142 | | |
| |||
130 | 152 | | |
131 | 153 | | |
132 | 154 | | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
133 | 185 | | |
134 | 186 | | |
135 | 187 | | |
| |||
149 | 201 | | |
150 | 202 | | |
151 | 203 | | |
152 | | - | |
| 204 | + | |
153 | 205 | | |
154 | | - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
155 | 212 | | |
156 | 213 | | |
157 | 214 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
87 | 129 | | |
88 | 130 | | |
89 | 131 | | |
| |||
0 commit comments