@@ -69,76 +69,89 @@ static iree_status_t iree_hal_hip_driver_factory_enumerate(
6969 return iree_ok_status ();
7070}
7171
72- // Option key constants.
73- static const iree_string_view_t key_hip_dylib_path =
74- iree_string_view_literal ("hip_dylib_path" );
75- static const iree_string_view_t key_hip_use_streams =
76- iree_string_view_literal ("hip_use_streams" );
77- static const iree_string_view_t key_hip_allow_inline_execution =
78- iree_string_view_literal ("hip_allow_inline_execution" );
79- static const iree_string_view_t key_hip_async_allocations =
80- iree_string_view_literal ("hip_async_allocations" );
81- static const iree_string_view_t key_hip_async_caching =
82- iree_string_view_literal ("hip_async_caching" );
83- static const iree_string_view_t key_hip_tracing =
84- iree_string_view_literal ("hip_tracing" );
85- static const iree_string_view_t key_hip_default_index =
86- iree_string_view_literal ("hip_default_index" );
87-
8872// Parses flags and environment variables into a string pair builder.
8973static iree_status_t iree_hal_hip_driver_parse_flags (
9074 iree_string_pair_builder_t * builder ) {
9175 const iree_flag_string_list_t dylib_path_list = FLAG_hip_dylib_path_list ();
9276
9377 // hip_dylib_path
78+ // TODO: make this a single key-value pair (semicolon separated).
79+ // Repeated fields don't work in things like python dictionaries/JSON and we
80+ // want to match the environment variable formatting.
9481 for (iree_host_size_t i = 0 ; i < dylib_path_list .count ; ++ i ) {
9582 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add (
96- builder ,
97- iree_make_string_pair ( key_hip_dylib_path , dylib_path_list .values [i ])));
83+ builder , iree_make_string_pair ( IREE_SV ( "hip_dylib_path" ),
84+ dylib_path_list .values [i ])));
9885 }
9986
10087 // bool and int flags
10188 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add_int32 (
102- builder , key_hip_use_streams , FLAG_hip_use_streams ));
89+ builder , IREE_SV ( "hip_use_streams" ) , FLAG_hip_use_streams ));
10390 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add_int32 (
104- builder , key_hip_allow_inline_execution ,
91+ builder , IREE_SV ( "hip_allow_inline_execution" ) ,
10592 FLAG_hip_allow_inline_execution ));
10693 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add_int32 (
107- builder , key_hip_async_allocations , FLAG_hip_async_allocations ));
94+ builder , IREE_SV ( "hip_async_allocations" ) , FLAG_hip_async_allocations ));
10895 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add_int32 (
109- builder , key_hip_async_caching , FLAG_hip_async_caching ));
96+ builder , IREE_SV ( "hip_async_caching" ) , FLAG_hip_async_caching ));
11097 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add_int32 (
111- builder , key_hip_tracing , FLAG_hip_tracing ));
98+ builder , IREE_SV ( "hip_tracing" ) , FLAG_hip_tracing ));
11299 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add_int32 (
113- builder , key_hip_default_index , FLAG_hip_default_index ));
100+ builder , IREE_SV ( "hip_default_index" ) , FLAG_hip_default_index ));
114101
115102 // If there were no flag-based dylib paths, consult the environment
116103 // variable.
117104 // Read from the IREE_HIP_DYLIB_PATH env var and split by ';' (regardless
118105 // of platform).
106+ //
107+ // TODO: make this a single key-value pair (semicolon separated).
108+ // Repeated fields don't work in things like python dictionaries/JSON and we
109+ // want to match the environment variable formatting.
119110 if (dylib_path_list .count == 0 ) {
120111 char * raw_dylib_path_env = getenv ("IREE_HIP_DYLIB_PATH" );
121112 if (raw_dylib_path_env ) {
122113 iree_string_view_t dylib_path_env =
123114 iree_make_cstring_view (raw_dylib_path_env );
124115 IREE_RETURN_IF_ERROR (
125116 iree_string_pair_builder_emplace_string (builder , & dylib_path_env ));
126- while (true) {
127- iree_string_view_t first , rest ;
128- intptr_t index =
129- iree_string_view_split (dylib_path_env , ';' , & first , & rest );
117+ intptr_t split_index = 0 ;
118+ do {
119+ iree_string_view_t value ;
120+ split_index = iree_string_view_split (dylib_path_env , ';' , & value ,
121+ & dylib_path_env );
130122 IREE_RETURN_IF_ERROR (iree_string_pair_builder_add (
131- builder , iree_make_string_pair (key_hip_dylib_path , first )));
123+ builder , iree_make_string_pair (IREE_SV ("hip_dylib_path" ), value )));
124+ } while (split_index != -1 );
125+ }
126+ }
132127
133- if (index < 0 ) {
134- break ;
135- }
128+ return iree_ok_status ();
129+ }
136130
137- dylib_path_env = rest ;
138- }
139- }
131+ static iree_status_t iree_hal_hip_try_parse_int32_option (
132+ iree_string_view_t name , iree_string_view_t value , int32_t * out_value ) {
133+ if (!iree_string_view_atoi_int32 (value , out_value )) {
134+ return iree_make_status (
135+ IREE_STATUS_FAILED_PRECONDITION ,
136+ "option '%*.s' expected an int32 value, got: '%.*s'" , (int )name .size ,
137+ name .data , (int )value .size , value .data );
140138 }
139+ return iree_ok_status ();
140+ }
141141
142+ static iree_status_t iree_hal_hip_try_parse_bool_option (
143+ iree_string_view_t name , iree_string_view_t value , bool * out_value ) {
144+ if (iree_string_view_equal (value , IREE_SV ("true" ))) {
145+ * out_value = 1 ;
146+ return iree_ok_status ();
147+ } else if (iree_string_view_equal (value , IREE_SV ("false" ))) {
148+ * out_value = 0 ;
149+ return iree_ok_status ();
150+ }
151+ int32_t int_value = 0 ;
152+ IREE_RETURN_IF_ERROR (
153+ iree_hal_hip_try_parse_int32_option (name , value , & int_value ));
154+ * out_value = int_value != 0 ;
142155 return iree_ok_status ();
143156}
144157
@@ -152,72 +165,43 @@ static iree_status_t iree_hal_hip_driver_populate_options(
152165 for (iree_host_size_t i = 0 ; i < pairs_size ; ++ i ) {
153166 iree_string_view_t key = pairs [i ].key ;
154167 iree_string_view_t value = pairs [i ].value ;
155- int32_t ivalue ;
156-
157- if (iree_string_view_equal (key , key_hip_dylib_path )) {
168+ if (iree_string_view_equal (key , IREE_SV ("hip_dylib_path" ))) {
158169 ++ dylib_path_count ;
159- } else if (iree_string_view_equal (key , key_hip_use_streams )) {
160- if (!iree_string_view_atoi_int32 (value , & ivalue )) {
161- return iree_make_status (
162- IREE_STATUS_FAILED_PRECONDITION ,
163- "Option 'hip_use_streams' expected to be int. Got: '%.*s'" ,
164- (int )value .size , value .data );
165- }
170+ } else if (iree_string_view_equal (key , IREE_SV ("hip_use_streams" ))) {
171+ bool use_streams = false;
172+ IREE_RETURN_IF_ERROR (
173+ iree_hal_hip_try_parse_bool_option (key , value , & use_streams ));
166174 device_params -> command_buffer_mode =
167- ivalue ? IREE_HAL_HIP_COMMAND_BUFFER_MODE_STREAM
168- : IREE_HAL_HIP_COMMAND_BUFFER_MODE_GRAPH ;
169- } else if (iree_string_view_equal (key , key_hip_allow_inline_execution )) {
170- if (!iree_string_view_atoi_int32 (value , & ivalue )) {
171- return iree_make_status (
172- IREE_STATUS_FAILED_PRECONDITION ,
173- "Option 'hip_allow_inline_execution' expected to be "
174- "int. Got: '%.*s'" ,
175- (int )value .size , value .data );
176- }
177- if (ivalue ) {
178- device_params -> allow_inline_execution = ivalue ? true : false;
179- }
180- } else if (iree_string_view_equal (key , key_hip_async_allocations )) {
181- if (!iree_string_view_atoi_int32 (value , & ivalue )) {
182- return iree_make_status (
183- IREE_STATUS_FAILED_PRECONDITION ,
184- "Option 'hip_async_allocations' expected to be int Got: '%.*s'" ,
185- (int )value .size , value .data );
186- }
187- device_params -> async_allocations = ivalue ? true : false;
188- } else if (iree_string_view_equal (key , key_hip_async_caching )) {
189- if (!iree_string_view_atoi_int32 (value , & ivalue )) {
190- return iree_make_status (
191- IREE_STATUS_FAILED_PRECONDITION ,
192- "Option 'hip_async_caching' expected to be int Got: '%.*s'" ,
193- (int )value .size , value .data );
194- }
195- device_params -> async_caching = ivalue ? true : false;
196- } else if (iree_string_view_equal (key , key_hip_tracing )) {
197- if (!iree_string_view_atoi_int32 (value , & ivalue )) {
198- return iree_make_status (
199- IREE_STATUS_FAILED_PRECONDITION ,
200- "Option 'hip_tracing' expected to be int. Got: '%.*s'" ,
201- (int )value .size , value .data );
202- }
203- device_params -> stream_tracing = ivalue ;
204- } else if (iree_string_view_equal (key , key_hip_default_index )) {
205- if (!iree_string_view_atoi_int32 (value , & ivalue )) {
206- return iree_make_status (
207- IREE_STATUS_FAILED_PRECONDITION ,
208- "Option 'hip_default_index' expected to be int. Got: '%.*s'" ,
209- (int )value .size , value .data );
210- ;
211- }
212- driver_options -> default_device_index = ivalue ;
175+ use_streams ? IREE_HAL_HIP_COMMAND_BUFFER_MODE_STREAM
176+ : IREE_HAL_HIP_COMMAND_BUFFER_MODE_GRAPH ;
177+ } else if (iree_string_view_equal (key ,
178+ IREE_SV ("hip_allow_inline_execution" ))) {
179+ IREE_RETURN_IF_ERROR (iree_hal_hip_try_parse_bool_option (
180+ key , value , & device_params -> allow_inline_execution ));
181+ } else if (iree_string_view_equal (key , IREE_SV ("hip_async_allocations" ))) {
182+ IREE_RETURN_IF_ERROR (iree_hal_hip_try_parse_bool_option (
183+ key , value , & device_params -> async_allocations ));
184+ } else if (iree_string_view_equal (key , IREE_SV ("hip_async_caching" ))) {
185+ IREE_RETURN_IF_ERROR (iree_hal_hip_try_parse_bool_option (
186+ key , value , & device_params -> async_caching ));
187+ } else if (iree_string_view_equal (key , IREE_SV ("hip_tracing" ))) {
188+ IREE_RETURN_IF_ERROR (iree_hal_hip_try_parse_int32_option (
189+ key , value , & device_params -> stream_tracing ));
190+ } else if (iree_string_view_equal (key , IREE_SV ("hip_default_index" ))) {
191+ IREE_RETURN_IF_ERROR (iree_hal_hip_try_parse_int32_option (
192+ key , value , & driver_options -> default_device_index ));
213193 } else {
214194 return iree_make_status (IREE_STATUS_FAILED_PRECONDITION ,
215- "Unrecognized options: %.*s" , (int )key .size ,
195+ "unrecognized option: ' %.*s' " , (int )key .size ,
216196 key .data );
217197 }
218198 }
219199
220200 // Populate dynamic sized values.
201+ //
202+ // TODO: make this a single key-value pair (semicolon separated).
203+ // Repeated fields don't work in things like python dictionaries/JSON and we
204+ // want to match the environment variable formatting.
221205 if (dylib_path_count > 0 ) {
222206 IREE_RETURN_IF_ERROR (iree_allocator_malloc (
223207 host_allocator ,
@@ -226,7 +210,7 @@ static iree_status_t iree_hal_hip_driver_populate_options(
226210 for (iree_host_size_t i = 0 ; i < pairs_size ; ++ i ) {
227211 iree_string_view_t key = pairs [i ].key ;
228212 iree_string_view_t value = pairs [i ].value ;
229- if (iree_string_view_equal (key , key_hip_dylib_path )) {
213+ if (iree_string_view_equal (key , IREE_SV ( "hip_dylib_path" ) )) {
230214 driver_options -> hip_lib_search_paths
231215 [driver_options -> hip_lib_search_path_count ++ ] = value ;
232216 }
0 commit comments