@@ -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,60 @@ 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
+ CompileKind :: WasmFunction
262
+ } else if k == u32:: from ( CompileKind :: ArrayToWasmTrampoline ) {
263
+ CompileKind :: ArrayToWasmTrampoline
264
+ } else if k == u32:: from ( CompileKind :: WasmToArrayTrampoline ) {
265
+ CompileKind :: WasmToArrayTrampoline
266
+ } else if k == u32:: from ( CompileKind :: WasmToBuiltinTrampoline ) {
267
+ CompileKind :: WasmToBuiltinTrampoline
268
+ } else {
269
+ unreachable ! ( )
270
+ }
230
271
}
231
272
232
273
fn module ( & self ) -> StaticModuleIndex {
233
274
StaticModuleIndex :: from_u32 ( self . namespace & !Self :: KIND_MASK )
234
275
}
235
276
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
277
const fn new_kind ( kind : u32 ) -> u32 {
242
278
assert ! ( kind < ( 1 << Self :: KIND_BITS ) ) ;
243
279
kind << Self :: KIND_OFFSET
@@ -248,49 +284,46 @@ impl CompileKey {
248
284
fn wasm_function ( module : StaticModuleIndex , index : DefinedFuncIndex ) -> Self {
249
285
debug_assert_eq ! ( module. as_u32( ) & Self :: KIND_MASK , 0 ) ;
250
286
Self {
251
- namespace : Self :: WASM_FUNCTION_KIND | module. as_u32 ( ) ,
287
+ namespace : u32 :: from ( CompileKind :: WasmFunction ) | module. as_u32 ( ) ,
252
288
index : index. as_u32 ( ) ,
253
289
}
254
290
}
255
291
256
292
fn array_to_wasm_trampoline ( module : StaticModuleIndex , index : DefinedFuncIndex ) -> Self {
257
293
debug_assert_eq ! ( module. as_u32( ) & Self :: KIND_MASK , 0 ) ;
258
294
Self {
259
- namespace : Self :: ARRAY_TO_WASM_TRAMPOLINE_KIND | module. as_u32 ( ) ,
295
+ namespace : u32 :: from ( CompileKind :: ArrayToWasmTrampoline ) | module. as_u32 ( ) ,
260
296
index : index. as_u32 ( ) ,
261
297
}
262
298
}
263
299
264
300
fn wasm_to_array_trampoline ( index : ModuleInternedTypeIndex ) -> Self {
265
301
Self {
266
- namespace : Self :: WASM_TO_ARRAY_TRAMPOLINE_KIND ,
302
+ namespace : CompileKind :: WasmToArrayTrampoline . into ( ) ,
267
303
index : index. as_u32 ( ) ,
268
304
}
269
305
}
270
306
271
307
fn wasm_to_builtin_trampoline ( index : BuiltinFunctionIndex ) -> Self {
272
308
Self {
273
- namespace : Self :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ,
309
+ namespace : CompileKind :: WasmToBuiltinTrampoline . into ( ) ,
274
310
index : index. index ( ) ,
275
311
}
276
312
}
277
313
}
278
314
279
315
#[ cfg( feature = "component-model" ) ]
280
316
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
317
fn trampoline ( index : wasmtime_environ:: component:: TrampolineIndex ) -> Self {
285
318
Self {
286
- namespace : Self :: TRAMPOLINE_KIND ,
319
+ namespace : CompileKind :: Trampoline . into ( ) ,
287
320
index : index. as_u32 ( ) ,
288
321
}
289
322
}
290
323
291
324
fn resource_drop_wasm_to_array_trampoline ( ) -> Self {
292
325
Self {
293
- namespace : Self :: RESOURCE_DROP_WASM_TO_ARRAY_KIND ,
326
+ namespace : CompileKind :: ResourceDropWasmToArrayTrampoline . into ( ) ,
294
327
index : 0 ,
295
328
}
296
329
}
@@ -585,7 +618,7 @@ the use case.
585
618
compile_required_builtins ( engine, & mut raw_outputs) ?;
586
619
587
620
// Bucket the outputs by kind.
588
- let mut outputs: BTreeMap < u32 , Vec < CompileOutput > > = BTreeMap :: new ( ) ;
621
+ let mut outputs: BTreeMap < CompileKind , Vec < CompileOutput > > = BTreeMap :: new ( ) ;
589
622
for output in raw_outputs {
590
623
outputs. entry ( output. key . kind ( ) ) . or_default ( ) . push ( output) ;
591
624
}
@@ -638,7 +671,7 @@ fn compile_required_builtins(engine: &Engine, raw_outputs: &mut Vec<CompileOutpu
638
671
#[ derive( Default ) ]
639
672
struct UnlinkedCompileOutputs {
640
673
// A map from kind to `CompileOutput`.
641
- outputs : BTreeMap < u32 , Vec < CompileOutput > > ,
674
+ outputs : BTreeMap < CompileKind , Vec < CompileOutput > > ,
642
675
}
643
676
644
677
impl UnlinkedCompileOutputs {
@@ -682,8 +715,8 @@ impl UnlinkedCompileOutputs {
682
715
}
683
716
} ;
684
717
685
- if output. key . kind ( ) == CompileKey :: WASM_FUNCTION_KIND
686
- || output. key . kind ( ) == CompileKey :: ARRAY_TO_WASM_TRAMPOLINE_KIND
718
+ if output. key . kind ( ) == CompileKind :: WasmFunction
719
+ || output. key . kind ( ) == CompileKind :: ArrayToWasmTrampoline
687
720
{
688
721
indices
689
722
. compiled_func_index_to_module
@@ -730,7 +763,7 @@ struct FunctionIndices {
730
763
start_srclocs : HashMap < CompileKey , FilePos > ,
731
764
732
765
// The index of each compiled function, bucketed by compile key kind.
733
- indices : BTreeMap < u32 , BTreeMap < CompileKey , CompiledFunction < usize > > > ,
766
+ indices : BTreeMap < CompileKind , BTreeMap < CompileKey , CompiledFunction < usize > > > ,
734
767
}
735
768
736
769
impl FunctionIndices {
@@ -766,12 +799,12 @@ impl FunctionIndices {
766
799
. module
767
800
. defined_func_index ( callee_index)
768
801
. unwrap ( ) ;
769
- self . indices [ & CompileKey :: WASM_FUNCTION_KIND ]
802
+ self . indices [ & CompileKind :: WasmFunction ]
770
803
[ & CompileKey :: wasm_function ( module, def_func_index) ]
771
804
. unwrap_function ( )
772
805
}
773
806
RelocationTarget :: Builtin ( builtin) => self . indices
774
- [ & CompileKey :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ]
807
+ [ & CompileKind :: WasmToBuiltinTrampoline ]
775
808
[ & CompileKey :: wasm_to_builtin_trampoline ( builtin) ]
776
809
. unwrap_function ( ) ,
777
810
RelocationTarget :: PulleyHostcall ( _) => {
@@ -786,7 +819,7 @@ impl FunctionIndices {
786
819
& mut obj,
787
820
& translations,
788
821
& |module, func| {
789
- let bucket = & self . indices [ & CompileKey :: WASM_FUNCTION_KIND ] ;
822
+ let bucket = & self . indices [ & CompileKind :: WasmFunction ] ;
790
823
let i = bucket[ & CompileKey :: wasm_function ( module, func) ] . unwrap_function ( ) ;
791
824
( symbol_ids_and_locs[ i] . 0 , & * compiled_funcs[ i] . 1 )
792
825
} ,
@@ -802,8 +835,7 @@ impl FunctionIndices {
802
835
// assert `self.indices` is empty, so this is acknowledgement that this
803
836
// is a pure runtime implementation detail and not needed in any
804
837
// metadata generated below.
805
- self . indices
806
- . remove ( & CompileKey :: WASM_TO_BUILTIN_TRAMPOLINE_KIND ) ;
838
+ self . indices . remove ( & CompileKind :: WasmToBuiltinTrampoline ) ;
807
839
808
840
// Finally, build our binary artifacts that map things like `FuncIndex`
809
841
// to a function location and all of that using the indices we saved
@@ -812,7 +844,7 @@ impl FunctionIndices {
812
844
813
845
let mut wasm_functions = self
814
846
. indices
815
- . remove ( & CompileKey :: WASM_FUNCTION_KIND )
847
+ . remove ( & CompileKind :: WasmFunction )
816
848
. unwrap_or_default ( )
817
849
. into_iter ( )
818
850
. peekable ( ) ;
@@ -835,14 +867,14 @@ impl FunctionIndices {
835
867
836
868
let mut array_to_wasm_trampolines = self
837
869
. indices
838
- . remove ( & CompileKey :: ARRAY_TO_WASM_TRAMPOLINE_KIND )
870
+ . remove ( & CompileKind :: ArrayToWasmTrampoline )
839
871
. unwrap_or_default ( ) ;
840
872
841
873
// NB: unlike the above maps this is not emptied out during iteration
842
874
// since each module may reach into different portions of this map.
843
875
let wasm_to_array_trampolines = self
844
876
. indices
845
- . remove ( & CompileKey :: WASM_TO_ARRAY_TRAMPOLINE_KIND )
877
+ . remove ( & CompileKind :: WasmToArrayTrampoline )
846
878
. unwrap_or_default ( ) ;
847
879
848
880
artifacts. modules = translations
@@ -913,14 +945,14 @@ impl FunctionIndices {
913
945
{
914
946
artifacts. trampolines = self
915
947
. indices
916
- . remove ( & CompileKey :: TRAMPOLINE_KIND )
948
+ . remove ( & CompileKind :: Trampoline )
917
949
. unwrap_or_default ( )
918
950
. into_iter ( )
919
951
. map ( |( _id, x) | x. unwrap_all_call_func ( ) . map ( |i| symbol_ids_and_locs[ i] . 1 ) )
920
952
. collect ( ) ;
921
953
let map = self
922
954
. indices
923
- . remove ( & CompileKey :: RESOURCE_DROP_WASM_TO_ARRAY_KIND )
955
+ . remove ( & CompileKind :: ResourceDropWasmToArrayTrampoline )
924
956
. unwrap_or_default ( ) ;
925
957
assert ! ( map. len( ) <= 1 ) ;
926
958
artifacts. resource_drop_wasm_to_array_trampoline = map
0 commit comments