1use std::cmp::Reverse;
5
6use crate::progress::ProgressContext;
7use crate::types::{module::CompileModuleInfo, symbols::SymbolRegistry};
8use crate::{
9 FunctionBodyData, ModuleTranslationState,
10 lib::std::{boxed::Box, sync::Arc},
11 translator::ModuleMiddleware,
12 types::function::Compilation,
13};
14use crossbeam_channel::unbounded;
15use enumset::EnumSet;
16use itertools::Itertools;
17use wasmer_types::{
18 CompilationProgressCallback, Features, LocalFunctionIndex,
19 entity::PrimaryMap,
20 error::CompileError,
21 target::{CpuFeature, Target, UserCompilerOptimizations},
22};
23#[cfg(feature = "translator")]
24use wasmparser::{Validator, WasmFeatures};
25
26pub trait CompilerConfig {
28 fn enable_pic(&mut self) {
34 }
37
38 fn enable_verifier(&mut self) {
43 }
46
47 fn enable_perfmap(&mut self) {
49 }
52
53 fn enable_non_volatile_memops(&mut self) {}
56
57 fn canonicalize_nans(&mut self, _enable: bool) {
62 }
65
66 fn compiler(self: Box<Self>) -> Box<dyn Compiler>;
68
69 fn default_features_for_target(&self, target: &Target) -> Features {
71 self.supported_features_for_target(target)
72 }
73
74 fn supported_features_for_target(&self, _target: &Target) -> Features {
76 Features::default()
77 }
78
79 fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>);
81}
82
83impl<T> From<T> for Box<dyn CompilerConfig + 'static>
84where
85 T: CompilerConfig + 'static,
86{
87 fn from(other: T) -> Self {
88 Box::new(other)
89 }
90}
91
92pub trait Compiler: Send + std::fmt::Debug {
94 fn name(&self) -> &str;
98
99 fn deterministic_id(&self) -> String;
102
103 fn with_opts(
111 &mut self,
112 suggested_compiler_opts: &UserCompilerOptimizations,
113 ) -> Result<(), CompileError> {
114 _ = suggested_compiler_opts;
115 Ok(())
116 }
117
118 #[cfg(feature = "translator")]
122 fn validate_module(&self, features: &Features, data: &[u8]) -> Result<(), CompileError> {
123 let mut wasm_features = WasmFeatures::empty();
124 wasm_features.set(WasmFeatures::BULK_MEMORY, features.bulk_memory);
125 wasm_features.set(WasmFeatures::THREADS, features.threads);
126 wasm_features.set(WasmFeatures::REFERENCE_TYPES, features.reference_types);
127 wasm_features.set(WasmFeatures::MULTI_VALUE, features.multi_value);
128 wasm_features.set(WasmFeatures::SIMD, features.simd);
129 wasm_features.set(WasmFeatures::TAIL_CALL, features.tail_call);
130 wasm_features.set(WasmFeatures::MULTI_MEMORY, features.multi_memory);
131 wasm_features.set(WasmFeatures::MEMORY64, features.memory64);
132 wasm_features.set(WasmFeatures::EXCEPTIONS, features.exceptions);
133 wasm_features.set(WasmFeatures::EXTENDED_CONST, features.extended_const);
134 wasm_features.set(WasmFeatures::RELAXED_SIMD, features.relaxed_simd);
135 wasm_features.set(WasmFeatures::WIDE_ARITHMETIC, features.wide_arithmetic);
136 wasm_features.set(WasmFeatures::MUTABLE_GLOBAL, true);
137 wasm_features.set(WasmFeatures::SATURATING_FLOAT_TO_INT, true);
138 wasm_features.set(WasmFeatures::FLOATS, true);
139 wasm_features.set(WasmFeatures::SIGN_EXTENSION, true);
140 wasm_features.set(WasmFeatures::GC_TYPES, true);
141
142 let mut validator = Validator::new_with_features(wasm_features);
143 validator
144 .validate_all(data)
145 .map_err(|e| CompileError::Validate(format!("{e}")))?;
146 Ok(())
147 }
148
149 fn compile_module(
153 &self,
154 target: &Target,
155 module: &CompileModuleInfo,
156 module_translation: &ModuleTranslationState,
157 function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
159 progress_callback: Option<&CompilationProgressCallback>,
160 ) -> Result<Compilation, CompileError>;
161
162 fn experimental_native_compile_module(
166 &self,
167 _target: &Target,
168 _module: &CompileModuleInfo,
169 _module_translation: &ModuleTranslationState,
170 _function_body_inputs: &PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
172 _symbol_registry: &dyn SymbolRegistry,
173 _wasmer_metadata: &[u8],
175 ) -> Option<Result<Vec<u8>, CompileError>> {
176 None
177 }
178
179 fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>];
181
182 fn get_cpu_features_used(&self, cpu_features: &EnumSet<CpuFeature>) -> EnumSet<CpuFeature> {
184 *cpu_features
185 }
186
187 fn get_perfmap_enabled(&self) -> bool {
189 false
190 }
191}
192
193pub struct FunctionBucket<'a> {
195 functions: Vec<(LocalFunctionIndex, &'a FunctionBodyData<'a>)>,
196 pub size: usize,
198}
199
200impl<'a> FunctionBucket<'a> {
201 pub fn new() -> Self {
203 Self {
204 functions: Vec::new(),
205 size: 0,
206 }
207 }
208}
209
210pub fn build_function_buckets<'a>(
212 function_body_inputs: &'a PrimaryMap<LocalFunctionIndex, FunctionBodyData<'a>>,
213 bucket_threshold_size: u64,
214) -> Vec<FunctionBucket<'a>> {
215 let mut function_bodies = function_body_inputs
216 .iter()
217 .sorted_by_key(|(id, body)| Reverse((body.data.len(), id.as_u32())))
218 .collect_vec();
219
220 let mut buckets = Vec::new();
221
222 while !function_bodies.is_empty() {
223 let mut next_function_body = Vec::with_capacity(function_bodies.len());
224 let mut bucket = FunctionBucket::new();
225
226 for (fn_index, fn_body) in function_bodies.into_iter() {
227 if bucket.size + fn_body.data.len() <= bucket_threshold_size as usize
228 || bucket.size == 0
230 {
231 bucket.size += fn_body.data.len();
232 bucket.functions.push((fn_index, fn_body));
233 } else {
234 next_function_body.push((fn_index, fn_body));
235 }
236 }
237
238 function_bodies = next_function_body;
239 buckets.push(bucket);
240 }
241
242 buckets
243}
244
245pub trait CompiledFunction {}
247
248pub trait FuncTranslator {}
250
251#[allow(clippy::too_many_arguments)]
253pub fn translate_function_buckets<'a, C, T, F, G>(
254 pool: &rayon::ThreadPool,
255 func_translator_builder: F,
256 translate_fn: G,
257 progress: Option<ProgressContext>,
258 buckets: &[FunctionBucket<'a>],
259) -> Result<Vec<C>, CompileError>
260where
261 T: FuncTranslator,
262 C: CompiledFunction + Send + Sync,
263 F: Fn() -> T + Send + Sync + Copy,
264 G: Fn(&mut T, &LocalFunctionIndex, &FunctionBodyData) -> Result<C, CompileError>
265 + Send
266 + Sync
267 + Copy,
268{
269 let progress = progress.as_ref();
270
271 let functions = pool.install(|| {
272 let (bucket_tx, bucket_rx) = unbounded::<&FunctionBucket<'a>>();
273 for bucket in buckets {
274 bucket_tx.send(bucket).map_err(|e| {
275 CompileError::Resource(format!("cannot allocate crossbeam channel item: {e}"))
276 })?;
277 }
278 drop(bucket_tx);
279
280 let (result_tx, result_rx) =
281 unbounded::<Result<Vec<(LocalFunctionIndex, C)>, CompileError>>();
282
283 pool.scope(|s| {
284 let worker_count = pool.current_num_threads().max(1);
285 for _ in 0..worker_count {
286 let bucket_rx = bucket_rx.clone();
287 let result_tx = result_tx.clone();
288 s.spawn(move |_| {
289 let mut func_translator = func_translator_builder();
290
291 while let Ok(bucket) = bucket_rx.recv() {
292 let bucket_result = (|| {
293 let mut translated_functions = Vec::new();
294 for (i, input) in bucket.functions.iter() {
295 let translated = translate_fn(&mut func_translator, i, input)?;
296 if let Some(progress) = progress {
297 progress.notify_steps(input.data.len() as u64)?;
298 }
299 translated_functions.push((*i, translated));
300 }
301 Ok(translated_functions)
302 })();
303
304 if result_tx.send(bucket_result).is_err() {
305 break;
306 }
307 }
308 });
309 }
310 });
311
312 drop(result_tx);
313 let mut functions = Vec::with_capacity(buckets.iter().map(|b| b.functions.len()).sum());
314 for _ in 0..buckets.len() {
315 match result_rx.recv().map_err(|e| {
316 CompileError::Resource(format!("cannot allocate crossbeam channel item: {e}"))
317 })? {
318 Ok(bucket_functions) => functions.extend(bucket_functions),
319 Err(err) => return Err(err),
320 }
321 }
322 Ok(functions)
323 })?;
324
325 Ok(functions
326 .into_iter()
327 .sorted_by_key(|x| x.0)
328 .map(|(_, body)| body)
329 .collect_vec())
330}
331
332pub const WASM_LARGE_FUNCTION_THRESHOLD: u64 = 100_000;
334
335pub const WASM_TRAMPOLINE_ESTIMATED_BODY_SIZE: u64 = 1_000;