Commit fc70db4
Support int64_t in BackendOption variant for pointer-sized opaque handles
Summary:
Add an `int64_t` alternative to the `OptionValue` variant in
`executorch/runtime/backend/options.h`, plus a matching `set_option(int64_t)`
overload on `BackendOptions` and an extended `get_runtime_spec<int64_t>`
template in `BackendInitContext`.
## Motivation
Backends increasingly need to receive pointer-sized opaque handles at
`Method::load_method()` time via the existing `runtime_specs` channel:
- NVIDIA CUDA driver handles (e.g., `CUgreenCtx` for green-context-aware
TensorRT inference, where the runtime user pre-creates a green context
and binds the engine's stream to it via `cuGreenCtxStreamCreate`).
- Externally-created CUDA streams shared across delegates.
- Vulkan/Metal command queue handles, etc.
The current `OptionValue` variant supports `bool`, `int`, and a 256-byte
char array. None of these safely round-trips a 64-bit pointer on LP64
platforms — `int` is 32-bit and would silently truncate the upper half of
a pointer, producing use-after-free or segfaults at the consumer side.
A workaround using the `const char*` arm (stringify the pointer to hex,
parse with `strtoull` in the backend) is possible but ugly, error-prone,
and unnecessary given how trivial the additive fix is.
## Scope of change
- Add `int64_t` to the `OptionValue` variant alternatives.
- Add a `set_option(const char (&key)[N], int64_t value)` overload on
`BackendOptions`.
- Extend `BackendInitContext::get_runtime_spec<T>` template's
`static_assert` to allow `T = int64_t` in addition to `bool`, `int`,
`const char*`. The internal `std::get_if<T>` already works with the new
variant alternative — no other body changes needed.
- Add `<cstdint>` include in both headers.
## Properties
This change is purely additive:
- **Existing consumers see no behavior change.** The `bool`, `int`, and
`const char*` paths are untouched. Variant alternatives are appended,
not reordered, so `std::variant::index()` semantics for existing values
are preserved.
- **No serialized format changes.** `runtime_specs` are passed in-process
at `Method::load_method` and are never serialized into the .pte file.
AOT `CompileSpec` lives in a separate Python schema (flatbuffer-backed)
and is unchanged here.
- **No public API removed or renamed.** The new `int64_t` overload is
selected only when callers explicitly pass `int64_t`-typed values;
integer literals continue to bind to the `int` overload.
## Caller usage
The intended usage pattern, illustrated for the green-context case:
// 1) User creates the opaque resource and owns its lifetime
CUgreenCtx green = nullptr;
cuGreenCtxCreate(&green, desc, device, CU_GREEN_CTX_DEFAULT_STREAM);
// 2) Pass as a runtime spec at Method::load_method time
BackendOption opt{};
std::strcpy(opt.key, "green_context");
opt.value =
static_cast<int64_t>(reinterpret_cast<intptr_t>(green));
Span<const BackendOption> specs(&opt, 1);
// ... thread `specs` through Module::load_program / load_method
// 3) In the backend's init():
auto h = context.get_runtime_spec<int64_t>("green_context");
if (h.ok()) {
auto green = reinterpret_cast<CUgreenCtx>(h.get());
CUstream raw_stream;
cuGreenCtxStreamCreate(
&raw_stream, green, CU_STREAM_NON_BLOCKING, /*priority=*/0);
// bind raw_stream to the engine for enqueueV3...
}
Differential Revision: D1032418651 parent e84a418 commit fc70db4
3 files changed
Lines changed: 52 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
100 | | - | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
110 | | - | |
111 | | - | |
| 111 | + | |
| 112 | + | |
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| 27 | + | |
| 28 | + | |
26 | 29 | | |
27 | | - | |
| 30 | + | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
| |||
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
43 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
| |||
113 | 118 | | |
114 | 119 | | |
115 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
116 | 137 | | |
117 | 138 | | |
118 | 139 | | |
| |||
137 | 158 | | |
138 | 159 | | |
139 | 160 | | |
140 | | - | |
| 161 | + | |
| 162 | + | |
141 | 163 | | |
142 | 164 | | |
143 | 165 | | |
| |||
176 | 198 | | |
177 | 199 | | |
178 | 200 | | |
179 | | - | |
| 201 | + | |
180 | 202 | | |
181 | 203 | | |
182 | 204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
64 | 86 | | |
65 | 87 | | |
66 | 88 | | |
| |||
0 commit comments