@@ -210,7 +210,7 @@ type CompileInput<'a> = Box<dyn FnOnce(&dyn Compiler) -> Result<CompileOutput> +
210
210
/// A sortable, comparable key for a compilation output.
211
211
///
212
212
/// Two `u32`s to align with `cranelift_codegen::ir::UserExternalName`.
213
- #[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
213
+ #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
214
214
struct CompileKey {
215
215
// The namespace field is bitpacked like:
216
216
//
@@ -220,24 +220,73 @@ struct CompileKey {
220
220
index : u32 ,
221
221
}
222
222
223
+ #[ repr( u32 ) ]
224
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
225
+ enum CompileKind {
226
+ WasmFunction = CompileKey :: new_kind ( 0 ) ,
227
+ ArrayToWasmTrampoline = CompileKey :: new_kind ( 1 ) ,
228
+ WasmToArrayTrampoline = CompileKey :: new_kind ( 2 ) ,
229
+ WasmToBuiltinTrampoline = CompileKey :: new_kind ( 3 ) ,
230
+
231
+ #[ cfg( feature = "component-model" ) ]
232
+ Trampoline = CompileKey :: new_kind ( 4 ) ,
233
+ #[ cfg( feature = "component-model" ) ]
234
+ ResourceDropWasmToArrayTrampoline = CompileKey :: new_kind ( 5 ) ,
235
+ }
236
+
237
+ impl From < CompileKind > for u32 {
238
+ fn from ( kind : CompileKind ) -> Self {
239
+ kind as u32
240
+ }
241
+ }
242
+
243
+ impl core:: fmt:: Debug for CompileKey {
244
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
245
+ f. debug_struct ( "CompileKey" )
246
+ . field ( "kind" , & self . kind ( ) )
247
+ . field ( "module" , & self . module ( ) )
248
+ . field ( "index" , & self . index )
249
+ . finish ( )
250
+ }
251
+ }
252
+
223
253
impl CompileKey {
224
254
const KIND_BITS : u32 = 3 ;
225
255
const KIND_OFFSET : u32 = 32 - Self :: KIND_BITS ;
226
256
const KIND_MASK : u32 = ( ( 1 << Self :: KIND_BITS ) - 1 ) << Self :: KIND_OFFSET ;
227
257
228
- fn kind ( & self ) -> u32 {
229
- self . namespace & Self :: KIND_MASK
258
+ fn kind ( & self ) -> CompileKind {
259
+ let k = self . namespace & Self :: KIND_MASK ;
260
+ if k == u32:: from ( CompileKind :: WasmFunction ) {
261
+ return CompileKind :: WasmFunction ;
262
+ }
263
+ if k == u32:: from ( CompileKind :: ArrayToWasmTrampoline ) {
264
+ return CompileKind :: ArrayToWasmTrampoline ;
265
+ }
266
+ if k == u32:: from ( CompileKind :: WasmToArrayTrampoline ) {
267
+ return CompileKind :: WasmToArrayTrampoline ;
268
+ }
269
+ if k == u32:: from ( CompileKind :: WasmToBuiltinTrampoline ) {
270
+ return CompileKind :: WasmToBuiltinTrampoline ;
271
+ }
272
+
273
+ #[ cfg( feature = "component-model" ) ]
274
+ {
275
+ if k == u32:: from ( CompileKind :: Trampoline ) {
276
+ return CompileKind :: Trampoline ;
277
+ }
278
+ if k == u32:: from ( CompileKind :: ResourceDropWasmToArrayTrampoline ) {
279
+ return CompileKind :: ResourceDropWasmToArrayTrampoline ;
280
+ }
281
+ }
282
+
283
+ unreachable ! ( )
230
284
}
231
285
232
286
fn module ( & self ) -> StaticModuleIndex {
233
287
StaticModuleIndex :: from_u32 ( self . namespace & !Self :: KIND_MASK )
234
288
}
235
289
236
- const WASM_FUNCTION_KIND : u32 = Self :: new_kind ( 0 ) ;
237
- const ARRAY_TO_WASM_TRAMPOLINE_KIND : u32 = Self :: new_kind ( 1 ) ;
238
- const WASM_TO_ARRAY_TRAMPOLINE_KIND : u32 = Self :: new_kind ( 2 ) ;
239
- const WASM_TO_BUILTIN_TRAMPOLINE_KIND : u32 = Self :: new_kind ( 3 ) ;
240
-
241
290
const fn new_kind ( kind : u32 ) -> u32 {
242
291
assert ! ( kind < ( 1 << Self :: KIND_BITS ) ) ;
243
292
kind << Self :: KIND_OFFSET
@@ -248,49 +297,46 @@ impl CompileKey {
248
297
fn wasm_function ( module : StaticModuleIndex , index : DefinedFuncIndex ) -> Self {
249
298
debug_assert_eq ! ( module. as_u32( ) & Self :: KIND_MASK , 0 ) ;
250
299
Self {
251
- namespace : Self :: WASM_FUNCTION_KIND | module. as_u32 ( ) ,
300
+ namespace : u32 :: from ( CompileKind :: WasmFunction ) | module. as_u32 ( ) ,
252
301
index : index. as_u32 ( ) ,
253
302
}
254
303
}
255
304
256
305
fn array_to_wasm_trampoline ( module : StaticModuleIndex , index : DefinedFuncIndex ) -> Self {
257
306
debug_assert_eq ! ( module. as_u32( ) & Self :: KIND_MASK , 0 ) ;
258
307
Self {
259
- namespace : Self :: ARRAY_TO_WASM_TRAMPOLINE_KIND | module. as_u32 ( ) ,
308
+ namespace : u32 :: from ( CompileKind :: ArrayToWasmTrampoline ) | module. as_u32 ( ) ,
260
309
index : index. as_u32 ( ) ,
261
310
}
262
311
}
263
312
264
313
fn wasm_to_array_trampoline ( index : ModuleInternedTypeIndex ) -> Self {
265
314
Self {
266
- namespace : Self :: WASM_TO_ARRAY_TRAMPOLINE_KIND ,
315
+ namespace : CompileKind :: WasmToArrayTrampoline . into ( ) ,
267
316
index : index. as_u32 ( ) ,
268
317
}
269
318
}
270
319
271
320
fn wasm_to_builtin_trampoline ( index : BuiltinFunctionIndex ) -> Self {
272
321
Self {
273
- namespace : Self :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ,
322
+ namespace : CompileKind :: WasmToBuiltinTrampoline . into ( ) ,
274
323
index : index. index ( ) ,
275
324
}
276
325
}
277
326
}
278
327
279
328
#[ cfg( feature = "component-model" ) ]
280
329
impl CompileKey {
281
- const TRAMPOLINE_KIND : u32 = Self :: new_kind ( 4 ) ;
282
- const RESOURCE_DROP_WASM_TO_ARRAY_KIND : u32 = Self :: new_kind ( 5 ) ;
283
-
284
330
fn trampoline ( index : wasmtime_environ:: component:: TrampolineIndex ) -> Self {
285
331
Self {
286
- namespace : Self :: TRAMPOLINE_KIND ,
332
+ namespace : CompileKind :: Trampoline . into ( ) ,
287
333
index : index. as_u32 ( ) ,
288
334
}
289
335
}
290
336
291
337
fn resource_drop_wasm_to_array_trampoline ( ) -> Self {
292
338
Self {
293
- namespace : Self :: RESOURCE_DROP_WASM_TO_ARRAY_KIND ,
339
+ namespace : CompileKind :: ResourceDropWasmToArrayTrampoline . into ( ) ,
294
340
index : 0 ,
295
341
}
296
342
}
@@ -585,7 +631,7 @@ the use case.
585
631
compile_required_builtins ( engine, & mut raw_outputs) ?;
586
632
587
633
// Bucket the outputs by kind.
588
- let mut outputs: BTreeMap < u32 , Vec < CompileOutput > > = BTreeMap :: new ( ) ;
634
+ let mut outputs: BTreeMap < CompileKind , Vec < CompileOutput > > = BTreeMap :: new ( ) ;
589
635
for output in raw_outputs {
590
636
outputs. entry ( output. key . kind ( ) ) . or_default ( ) . push ( output) ;
591
637
}
@@ -638,7 +684,7 @@ fn compile_required_builtins(engine: &Engine, raw_outputs: &mut Vec<CompileOutpu
638
684
#[ derive( Default ) ]
639
685
struct UnlinkedCompileOutputs {
640
686
// A map from kind to `CompileOutput`.
641
- outputs : BTreeMap < u32 , Vec < CompileOutput > > ,
687
+ outputs : BTreeMap < CompileKind , Vec < CompileOutput > > ,
642
688
}
643
689
644
690
impl UnlinkedCompileOutputs {
@@ -682,8 +728,8 @@ impl UnlinkedCompileOutputs {
682
728
}
683
729
} ;
684
730
685
- if output. key . kind ( ) == CompileKey :: WASM_FUNCTION_KIND
686
- || output. key . kind ( ) == CompileKey :: ARRAY_TO_WASM_TRAMPOLINE_KIND
731
+ if output. key . kind ( ) == CompileKind :: WasmFunction
732
+ || output. key . kind ( ) == CompileKind :: ArrayToWasmTrampoline
687
733
{
688
734
indices
689
735
. compiled_func_index_to_module
@@ -730,7 +776,7 @@ struct FunctionIndices {
730
776
start_srclocs : HashMap < CompileKey , FilePos > ,
731
777
732
778
// The index of each compiled function, bucketed by compile key kind.
733
- indices : BTreeMap < u32 , BTreeMap < CompileKey , CompiledFunction < usize > > > ,
779
+ indices : BTreeMap < CompileKind , BTreeMap < CompileKey , CompiledFunction < usize > > > ,
734
780
}
735
781
736
782
impl FunctionIndices {
@@ -766,12 +812,12 @@ impl FunctionIndices {
766
812
. module
767
813
. defined_func_index ( callee_index)
768
814
. unwrap ( ) ;
769
- self . indices [ & CompileKey :: WASM_FUNCTION_KIND ]
815
+ self . indices [ & CompileKind :: WasmFunction ]
770
816
[ & CompileKey :: wasm_function ( module, def_func_index) ]
771
817
. unwrap_function ( )
772
818
}
773
819
RelocationTarget :: Builtin ( builtin) => self . indices
774
- [ & CompileKey :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ]
820
+ [ & CompileKind :: WasmToBuiltinTrampoline ]
775
821
[ & CompileKey :: wasm_to_builtin_trampoline ( builtin) ]
776
822
. unwrap_function ( ) ,
777
823
RelocationTarget :: PulleyHostcall ( _) => {
@@ -786,7 +832,7 @@ impl FunctionIndices {
786
832
& mut obj,
787
833
& translations,
788
834
& |module, func| {
789
- let bucket = & self . indices [ & CompileKey :: WASM_FUNCTION_KIND ] ;
835
+ let bucket = & self . indices [ & CompileKind :: WasmFunction ] ;
790
836
let i = bucket[ & CompileKey :: wasm_function ( module, func) ] . unwrap_function ( ) ;
791
837
( symbol_ids_and_locs[ i] . 0 , & * compiled_funcs[ i] . 1 )
792
838
} ,
@@ -802,8 +848,7 @@ impl FunctionIndices {
802
848
// assert `self.indices` is empty, so this is acknowledgement that this
803
849
// is a pure runtime implementation detail and not needed in any
804
850
// metadata generated below.
805
- self . indices
806
- . remove ( & CompileKey :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ) ;
851
+ self . indices . remove ( & CompileKind :: WasmToBuiltinTrampoline ) ;
807
852
808
853
// Finally, build our binary artifacts that map things like `FuncIndex`
809
854
// to a function location and all of that using the indices we saved
@@ -812,7 +857,7 @@ impl FunctionIndices {
812
857
813
858
let mut wasm_functions = self
814
859
. indices
815
- . remove ( & CompileKey :: WASM_FUNCTION_KIND )
860
+ . remove ( & CompileKind :: WasmFunction )
816
861
. unwrap_or_default ( )
817
862
. into_iter ( )
818
863
. peekable ( ) ;
@@ -835,14 +880,14 @@ impl FunctionIndices {
835
880
836
881
let mut array_to_wasm_trampolines = self
837
882
. indices
838
- . remove ( & CompileKey :: ARRAY_TO_WASM_TRAMPOLINE_KIND )
883
+ . remove ( & CompileKind :: ArrayToWasmTrampoline )
839
884
. unwrap_or_default ( ) ;
840
885
841
886
// NB: unlike the above maps this is not emptied out during iteration
842
887
// since each module may reach into different portions of this map.
843
888
let wasm_to_array_trampolines = self
844
889
. indices
845
- . remove ( & CompileKey :: WASM_TO_ARRAY_TRAMPOLINE_KIND )
890
+ . remove ( & CompileKind :: WasmToArrayTrampoline )
846
891
. unwrap_or_default ( ) ;
847
892
848
893
artifacts. modules = translations
@@ -913,14 +958,14 @@ impl FunctionIndices {
913
958
{
914
959
artifacts. trampolines = self
915
960
. indices
916
- . remove ( & CompileKey :: TRAMPOLINE_KIND )
961
+ . remove ( & CompileKind :: Trampoline )
917
962
. unwrap_or_default ( )
918
963
. into_iter ( )
919
964
. map ( |( _id, x) | x. unwrap_all_call_func ( ) . map ( |i| symbol_ids_and_locs[ i] . 1 ) )
920
965
. collect ( ) ;
921
966
let map = self
922
967
. indices
923
- . remove ( & CompileKey :: RESOURCE_DROP_WASM_TO_ARRAY_KIND )
968
+ . remove ( & CompileKind :: ResourceDropWasmToArrayTrampoline )
924
969
. unwrap_or_default ( ) ;
925
970
assert ! ( map. len( ) <= 1 ) ;
926
971
artifacts. resource_drop_wasm_to_array_trampoline = map
0 commit comments