@@ -210,7 +210,7 @@ type CompileInput<'a> = Box<dyn FnOnce(&dyn Compiler) -> Result<CompileOutput> +
210210/// A sortable, comparable key for a compilation output.
211211///
212212/// 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 ) ]
214214struct CompileKey {
215215 // The namespace field is bitpacked like:
216216 //
@@ -220,24 +220,73 @@ struct CompileKey {
220220 index : u32 ,
221221}
222222
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+
223253impl CompileKey {
224254 const KIND_BITS : u32 = 3 ;
225255 const KIND_OFFSET : u32 = 32 - Self :: KIND_BITS ;
226256 const KIND_MASK : u32 = ( ( 1 << Self :: KIND_BITS ) - 1 ) << Self :: KIND_OFFSET ;
227257
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 ! ( )
230284 }
231285
232286 fn module ( & self ) -> StaticModuleIndex {
233287 StaticModuleIndex :: from_u32 ( self . namespace & !Self :: KIND_MASK )
234288 }
235289
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-
241290 const fn new_kind ( kind : u32 ) -> u32 {
242291 assert ! ( kind < ( 1 << Self :: KIND_BITS ) ) ;
243292 kind << Self :: KIND_OFFSET
@@ -248,49 +297,46 @@ impl CompileKey {
248297 fn wasm_function ( module : StaticModuleIndex , index : DefinedFuncIndex ) -> Self {
249298 debug_assert_eq ! ( module. as_u32( ) & Self :: KIND_MASK , 0 ) ;
250299 Self {
251- namespace : Self :: WASM_FUNCTION_KIND | module. as_u32 ( ) ,
300+ namespace : u32 :: from ( CompileKind :: WasmFunction ) | module. as_u32 ( ) ,
252301 index : index. as_u32 ( ) ,
253302 }
254303 }
255304
256305 fn array_to_wasm_trampoline ( module : StaticModuleIndex , index : DefinedFuncIndex ) -> Self {
257306 debug_assert_eq ! ( module. as_u32( ) & Self :: KIND_MASK , 0 ) ;
258307 Self {
259- namespace : Self :: ARRAY_TO_WASM_TRAMPOLINE_KIND | module. as_u32 ( ) ,
308+ namespace : u32 :: from ( CompileKind :: ArrayToWasmTrampoline ) | module. as_u32 ( ) ,
260309 index : index. as_u32 ( ) ,
261310 }
262311 }
263312
264313 fn wasm_to_array_trampoline ( index : ModuleInternedTypeIndex ) -> Self {
265314 Self {
266- namespace : Self :: WASM_TO_ARRAY_TRAMPOLINE_KIND ,
315+ namespace : CompileKind :: WasmToArrayTrampoline . into ( ) ,
267316 index : index. as_u32 ( ) ,
268317 }
269318 }
270319
271320 fn wasm_to_builtin_trampoline ( index : BuiltinFunctionIndex ) -> Self {
272321 Self {
273- namespace : Self :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ,
322+ namespace : CompileKind :: WasmToBuiltinTrampoline . into ( ) ,
274323 index : index. index ( ) ,
275324 }
276325 }
277326}
278327
279328#[ cfg( feature = "component-model" ) ]
280329impl CompileKey {
281- const TRAMPOLINE_KIND : u32 = Self :: new_kind ( 4 ) ;
282- const RESOURCE_DROP_WASM_TO_ARRAY_KIND : u32 = Self :: new_kind ( 5 ) ;
283-
284330 fn trampoline ( index : wasmtime_environ:: component:: TrampolineIndex ) -> Self {
285331 Self {
286- namespace : Self :: TRAMPOLINE_KIND ,
332+ namespace : CompileKind :: Trampoline . into ( ) ,
287333 index : index. as_u32 ( ) ,
288334 }
289335 }
290336
291337 fn resource_drop_wasm_to_array_trampoline ( ) -> Self {
292338 Self {
293- namespace : Self :: RESOURCE_DROP_WASM_TO_ARRAY_KIND ,
339+ namespace : CompileKind :: ResourceDropWasmToArrayTrampoline . into ( ) ,
294340 index : 0 ,
295341 }
296342 }
@@ -585,7 +631,7 @@ the use case.
585631 compile_required_builtins ( engine, & mut raw_outputs) ?;
586632
587633 // 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 ( ) ;
589635 for output in raw_outputs {
590636 outputs. entry ( output. key . kind ( ) ) . or_default ( ) . push ( output) ;
591637 }
@@ -638,7 +684,7 @@ fn compile_required_builtins(engine: &Engine, raw_outputs: &mut Vec<CompileOutpu
638684#[ derive( Default ) ]
639685struct UnlinkedCompileOutputs {
640686 // A map from kind to `CompileOutput`.
641- outputs : BTreeMap < u32 , Vec < CompileOutput > > ,
687+ outputs : BTreeMap < CompileKind , Vec < CompileOutput > > ,
642688}
643689
644690impl UnlinkedCompileOutputs {
@@ -682,8 +728,8 @@ impl UnlinkedCompileOutputs {
682728 }
683729 } ;
684730
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
687733 {
688734 indices
689735 . compiled_func_index_to_module
@@ -730,7 +776,7 @@ struct FunctionIndices {
730776 start_srclocs : HashMap < CompileKey , FilePos > ,
731777
732778 // 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 > > > ,
734780}
735781
736782impl FunctionIndices {
@@ -766,12 +812,12 @@ impl FunctionIndices {
766812 . module
767813 . defined_func_index ( callee_index)
768814 . unwrap ( ) ;
769- self . indices [ & CompileKey :: WASM_FUNCTION_KIND ]
815+ self . indices [ & CompileKind :: WasmFunction ]
770816 [ & CompileKey :: wasm_function ( module, def_func_index) ]
771817 . unwrap_function ( )
772818 }
773819 RelocationTarget :: Builtin ( builtin) => self . indices
774- [ & CompileKey :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ]
820+ [ & CompileKind :: WasmToBuiltinTrampoline ]
775821 [ & CompileKey :: wasm_to_builtin_trampoline ( builtin) ]
776822 . unwrap_function ( ) ,
777823 RelocationTarget :: PulleyHostcall ( _) => {
@@ -786,7 +832,7 @@ impl FunctionIndices {
786832 & mut obj,
787833 & translations,
788834 & |module, func| {
789- let bucket = & self . indices [ & CompileKey :: WASM_FUNCTION_KIND ] ;
835+ let bucket = & self . indices [ & CompileKind :: WasmFunction ] ;
790836 let i = bucket[ & CompileKey :: wasm_function ( module, func) ] . unwrap_function ( ) ;
791837 ( symbol_ids_and_locs[ i] . 0 , & * compiled_funcs[ i] . 1 )
792838 } ,
@@ -802,8 +848,7 @@ impl FunctionIndices {
802848 // assert `self.indices` is empty, so this is acknowledgement that this
803849 // is a pure runtime implementation detail and not needed in any
804850 // metadata generated below.
805- self . indices
806- . remove ( & CompileKey :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ) ;
851+ self . indices . remove ( & CompileKind :: WasmToBuiltinTrampoline ) ;
807852
808853 // Finally, build our binary artifacts that map things like `FuncIndex`
809854 // to a function location and all of that using the indices we saved
@@ -812,7 +857,7 @@ impl FunctionIndices {
812857
813858 let mut wasm_functions = self
814859 . indices
815- . remove ( & CompileKey :: WASM_FUNCTION_KIND )
860+ . remove ( & CompileKind :: WasmFunction )
816861 . unwrap_or_default ( )
817862 . into_iter ( )
818863 . peekable ( ) ;
@@ -835,14 +880,14 @@ impl FunctionIndices {
835880
836881 let mut array_to_wasm_trampolines = self
837882 . indices
838- . remove ( & CompileKey :: ARRAY_TO_WASM_TRAMPOLINE_KIND )
883+ . remove ( & CompileKind :: ArrayToWasmTrampoline )
839884 . unwrap_or_default ( ) ;
840885
841886 // NB: unlike the above maps this is not emptied out during iteration
842887 // since each module may reach into different portions of this map.
843888 let wasm_to_array_trampolines = self
844889 . indices
845- . remove ( & CompileKey :: WASM_TO_ARRAY_TRAMPOLINE_KIND )
890+ . remove ( & CompileKind :: WasmToArrayTrampoline )
846891 . unwrap_or_default ( ) ;
847892
848893 artifacts. modules = translations
@@ -913,14 +958,14 @@ impl FunctionIndices {
913958 {
914959 artifacts. trampolines = self
915960 . indices
916- . remove ( & CompileKey :: TRAMPOLINE_KIND )
961+ . remove ( & CompileKind :: Trampoline )
917962 . unwrap_or_default ( )
918963 . into_iter ( )
919964 . map ( |( _id, x) | x. unwrap_all_call_func ( ) . map ( |i| symbol_ids_and_locs[ i] . 1 ) )
920965 . collect ( ) ;
921966 let map = self
922967 . indices
923- . remove ( & CompileKey :: RESOURCE_DROP_WASM_TO_ARRAY_KIND )
968+ . remove ( & CompileKind :: ResourceDropWasmToArrayTrampoline )
924969 . unwrap_or_default ( ) ;
925970 assert ! ( map. len( ) <= 1 ) ;
926971 artifacts. resource_drop_wasm_to_array_trampoline = map
0 commit comments