1111#include < executorch/runtime/core/span.h>
1212#include < array>
1313#include < cstddef>
14+ #include < cstdint>
1415#include < cstring>
1516#include < variant>
1617
@@ -20,11 +21,15 @@ namespace runtime {
2021static constexpr size_t kMaxOptionKeyLength = 64 ;
2122static constexpr size_t kMaxOptionValueLength = 256 ;
2223
23- // All string keys and values must have static storage duration (string
24- // literals, static const char arrays, or global constants). The BackendOptions
25- // class does NOT take ownership of strings.
24+ // String keys and string values are COPIED into fixed-size internal buffers
25+ // (key into `BackendOption::key`, value into the `std::array<char, ...>`
26+ // variant arm) and truncated at kMaxOptionKeyLength / kMaxOptionValueLength
27+ // respectively. Callers do NOT need to keep the source strings alive after
28+ // the set_option() call returns.
29+ // The int64_t arm lets callers pass pointer-sized opaque handles (e.g.,
30+ // driver handles like CUgreenCtx) by reinterpret_cast to/from int64_t.
2631using OptionValue =
27- std::variant<bool , int , std::array<char , kMaxOptionValueLength >>;
32+ std::variant<bool , int , int64_t , std::array<char , kMaxOptionValueLength >>;
2833
2934struct BackendOption {
3035 // key is the name of the backend option, like num_threads, enable_profiling,
@@ -40,7 +45,9 @@ struct BackendOption {
4045 *
4146 * This class provides a type-safe way to store key-value pairs for backend
4247 * configuration, with compile-time capacity limits and runtime type checking.
43- * It supports bool, int, and const char* value types.
48+ * It supports bool, int, int64_t, and const char* value types. The int64_t
49+ * arm allows callers to pass pointer-sized opaque handles (e.g., driver
50+ * handles like CUgreenCtx) by reinterpret_cast to/from int64_t.
4451 *
4552 * @tparam MaxCapacity The maximum number of options that can be stored
4653 */
@@ -113,16 +120,34 @@ class BackendOptions {
113120 return set_option_impl (key, value);
114121 }
115122
123+ /* *
124+ * Sets an int64_t option value for the given key.
125+ * Useful for pointer-sized opaque handles (cast via reinterpret_cast).
126+ * If the key already exists, updates its value. Otherwise, adds a new option.
127+ *
128+ * @tparam N The length of the key string (automatically deduced)
129+ * @param key The option key (must be a string literal or array)
130+ * @param value The int64_t value to set
131+ * @return Error::Ok on success, Error::InvalidArgument if storage is full
132+ */
133+ template <size_t N>
134+ Error set_option (const char (&key)[N], int64_t value) noexcept {
135+ static_assert (N <= kMaxOptionKeyLength , " Option key is too long" );
136+ return set_option_impl (key, value);
137+ }
138+
116139 /* *
117140 * Sets a string option value for the given key.
118141 * If the key already exists, updates its value. Otherwise, adds a new option.
119142 *
120- * Note: The string value must have static storage duration. This class does
121- * NOT take ownership of the string - it only stores the pointer.
143+ * The string value is copied into an internal fixed-size buffer (truncated
144+ * at kMaxOptionValueLength - 1 characters and null-terminated). The caller
145+ * does NOT need to keep the source string alive after this call returns.
122146 *
123147 * @tparam N The length of the key string (automatically deduced)
124148 * @param key The option key (must be a string literal or array)
125- * @param value The string value to set (must have static storage duration)
149+ * @param value The string value to set (copied; truncated at
150+ * kMaxOptionValueLength - 1 characters)
126151 * @return Error::Ok on success, Error::InvalidArgument if storage is full
127152 */
128153 template <size_t N>
@@ -137,7 +162,8 @@ class BackendOptions {
137162 /* *
138163 * Retrieves an option value by key and type.
139164 *
140- * @tparam T The expected type of the option value (bool, int, or const char*)
165+ * @tparam T The expected type of the option value (bool, int, int64_t, or
166+ * const char*)
141167 * @tparam KeyLen The length of the key string (automatically deduced)
142168 * @param key The option key to look up
143169 * @param out Reference to store the retrieved value
@@ -157,7 +183,7 @@ class BackendOptions {
157183 return Error::Ok;
158184 }
159185 }
160- // Default handling for bool/int
186+ // Default handling for bool/int/int64_t
161187 else if (auto * val = std::get_if<T>(&options_[i].value )) {
162188 out = *val;
163189 return Error::Ok;
@@ -176,7 +202,7 @@ class BackendOptions {
176202 * Internal implementation for setting option values.
177203 * Handles both updating existing options and adding new ones.
178204 *
179- * @tparam T The type of the value (bool, int, or const char*)
205+ * @tparam T The type of the value (bool, int, int64_t, or const char*)
180206 * @param key The option key
181207 * @param value The value to set
182208 * @return Error::Ok on success, Error::InvalidArgument if storage is full
0 commit comments