Skip to content

Commit b63e97f

Browse files
committed
Use a proper enum type for the compilation kind in our CompileKey bitpacking
On top of being a general tidiness thing, this allows for better print debugging and such.
1 parent 968952a commit b63e97f

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

crates/wasmtime/src/compile.rs

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
214214
struct CompileKey {
215215
// The namespace field is bitpacked like:
216216
//
@@ -220,24 +220,60 @@ 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+
223253
impl 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+
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+
}
230271
}
231272

232273
fn module(&self) -> StaticModuleIndex {
233274
StaticModuleIndex::from_u32(self.namespace & !Self::KIND_MASK)
234275
}
235276

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-
241277
const fn new_kind(kind: u32) -> u32 {
242278
assert!(kind < (1 << Self::KIND_BITS));
243279
kind << Self::KIND_OFFSET
@@ -248,49 +284,46 @@ impl CompileKey {
248284
fn wasm_function(module: StaticModuleIndex, index: DefinedFuncIndex) -> Self {
249285
debug_assert_eq!(module.as_u32() & Self::KIND_MASK, 0);
250286
Self {
251-
namespace: Self::WASM_FUNCTION_KIND | module.as_u32(),
287+
namespace: u32::from(CompileKind::WasmFunction) | module.as_u32(),
252288
index: index.as_u32(),
253289
}
254290
}
255291

256292
fn array_to_wasm_trampoline(module: StaticModuleIndex, index: DefinedFuncIndex) -> Self {
257293
debug_assert_eq!(module.as_u32() & Self::KIND_MASK, 0);
258294
Self {
259-
namespace: Self::ARRAY_TO_WASM_TRAMPOLINE_KIND | module.as_u32(),
295+
namespace: u32::from(CompileKind::ArrayToWasmTrampoline) | module.as_u32(),
260296
index: index.as_u32(),
261297
}
262298
}
263299

264300
fn wasm_to_array_trampoline(index: ModuleInternedTypeIndex) -> Self {
265301
Self {
266-
namespace: Self::WASM_TO_ARRAY_TRAMPOLINE_KIND,
302+
namespace: CompileKind::WasmToArrayTrampoline.into(),
267303
index: index.as_u32(),
268304
}
269305
}
270306

271307
fn wasm_to_builtin_trampoline(index: BuiltinFunctionIndex) -> Self {
272308
Self {
273-
namespace: Self::WASM_TO_BUILTIN_TRAMPOLINE_KIND,
309+
namespace: CompileKind::WasmToBuiltinTrampoline.into(),
274310
index: index.index(),
275311
}
276312
}
277313
}
278314

279315
#[cfg(feature = "component-model")]
280316
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-
284317
fn trampoline(index: wasmtime_environ::component::TrampolineIndex) -> Self {
285318
Self {
286-
namespace: Self::TRAMPOLINE_KIND,
319+
namespace: CompileKind::Trampoline.into(),
287320
index: index.as_u32(),
288321
}
289322
}
290323

291324
fn resource_drop_wasm_to_array_trampoline() -> Self {
292325
Self {
293-
namespace: Self::RESOURCE_DROP_WASM_TO_ARRAY_KIND,
326+
namespace: CompileKind::ResourceDropWasmToArrayTrampoline.into(),
294327
index: 0,
295328
}
296329
}
@@ -585,7 +618,7 @@ the use case.
585618
compile_required_builtins(engine, &mut raw_outputs)?;
586619

587620
// 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();
589622
for output in raw_outputs {
590623
outputs.entry(output.key.kind()).or_default().push(output);
591624
}
@@ -638,7 +671,7 @@ fn compile_required_builtins(engine: &Engine, raw_outputs: &mut Vec<CompileOutpu
638671
#[derive(Default)]
639672
struct UnlinkedCompileOutputs {
640673
// A map from kind to `CompileOutput`.
641-
outputs: BTreeMap<u32, Vec<CompileOutput>>,
674+
outputs: BTreeMap<CompileKind, Vec<CompileOutput>>,
642675
}
643676

644677
impl UnlinkedCompileOutputs {
@@ -682,8 +715,8 @@ impl UnlinkedCompileOutputs {
682715
}
683716
};
684717

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
687720
{
688721
indices
689722
.compiled_func_index_to_module
@@ -730,7 +763,7 @@ struct FunctionIndices {
730763
start_srclocs: HashMap<CompileKey, FilePos>,
731764

732765
// 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>>>,
734767
}
735768

736769
impl FunctionIndices {
@@ -766,12 +799,12 @@ impl FunctionIndices {
766799
.module
767800
.defined_func_index(callee_index)
768801
.unwrap();
769-
self.indices[&CompileKey::WASM_FUNCTION_KIND]
802+
self.indices[&CompileKind::WasmFunction]
770803
[&CompileKey::wasm_function(module, def_func_index)]
771804
.unwrap_function()
772805
}
773806
RelocationTarget::Builtin(builtin) => self.indices
774-
[&CompileKey::WASM_TO_BUILTIN_TRAMPOLINE_KIND]
807+
[&CompileKind::WasmToBuiltinTrampoline]
775808
[&CompileKey::wasm_to_builtin_trampoline(builtin)]
776809
.unwrap_function(),
777810
RelocationTarget::PulleyHostcall(_) => {
@@ -786,7 +819,7 @@ impl FunctionIndices {
786819
&mut obj,
787820
&translations,
788821
&|module, func| {
789-
let bucket = &self.indices[&CompileKey::WASM_FUNCTION_KIND];
822+
let bucket = &self.indices[&CompileKind::WasmFunction];
790823
let i = bucket[&CompileKey::wasm_function(module, func)].unwrap_function();
791824
(symbol_ids_and_locs[i].0, &*compiled_funcs[i].1)
792825
},
@@ -802,8 +835,7 @@ impl FunctionIndices {
802835
// assert `self.indices` is empty, so this is acknowledgement that this
803836
// is a pure runtime implementation detail and not needed in any
804837
// metadata generated below.
805-
self.indices
806-
.remove(&CompileKey::WASM_TO_BUILTIN_TRAMPOLINE_KIND);
838+
self.indices.remove(&CompileKind::WasmToBuiltinTrampoline);
807839

808840
// Finally, build our binary artifacts that map things like `FuncIndex`
809841
// to a function location and all of that using the indices we saved
@@ -812,7 +844,7 @@ impl FunctionIndices {
812844

813845
let mut wasm_functions = self
814846
.indices
815-
.remove(&CompileKey::WASM_FUNCTION_KIND)
847+
.remove(&CompileKind::WasmFunction)
816848
.unwrap_or_default()
817849
.into_iter()
818850
.peekable();
@@ -835,14 +867,14 @@ impl FunctionIndices {
835867

836868
let mut array_to_wasm_trampolines = self
837869
.indices
838-
.remove(&CompileKey::ARRAY_TO_WASM_TRAMPOLINE_KIND)
870+
.remove(&CompileKind::ArrayToWasmTrampoline)
839871
.unwrap_or_default();
840872

841873
// NB: unlike the above maps this is not emptied out during iteration
842874
// since each module may reach into different portions of this map.
843875
let wasm_to_array_trampolines = self
844876
.indices
845-
.remove(&CompileKey::WASM_TO_ARRAY_TRAMPOLINE_KIND)
877+
.remove(&CompileKind::WasmToArrayTrampoline)
846878
.unwrap_or_default();
847879

848880
artifacts.modules = translations
@@ -913,14 +945,14 @@ impl FunctionIndices {
913945
{
914946
artifacts.trampolines = self
915947
.indices
916-
.remove(&CompileKey::TRAMPOLINE_KIND)
948+
.remove(&CompileKind::Trampoline)
917949
.unwrap_or_default()
918950
.into_iter()
919951
.map(|(_id, x)| x.unwrap_all_call_func().map(|i| symbol_ids_and_locs[i].1))
920952
.collect();
921953
let map = self
922954
.indices
923-
.remove(&CompileKey::RESOURCE_DROP_WASM_TO_ARRAY_KIND)
955+
.remove(&CompileKind::ResourceDropWasmToArrayTrampoline)
924956
.unwrap_or_default();
925957
assert!(map.len() <= 1);
926958
artifacts.resource_drop_wasm_to_array_trampoline = map

0 commit comments

Comments
 (0)