1use std::cmp::Reverse;
5
6use crate::progress::ProgressContext;
7use crate::types::module::CompileModuleInfo;
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 enable_readonly_funcref_table(&mut self) {}
60
61 fn canonicalize_nans(&mut self, _enable: bool) {
66 }
69
70 fn compiler(self: Box<Self>) -> Box<dyn Compiler>;
72
73 fn default_features_for_target(&self, target: &Target) -> Features {
75 self.supported_features_for_target(target)
76 }
77
78 fn supported_features_for_target(&self, _target: &Target) -> Features {
80 Features::default()
81 }
82
83 fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>);
85}
86
87impl<T> From<T> for Box<dyn CompilerConfig + 'static>
88where
89 T: CompilerConfig + 'static,
90{
91 fn from(other: T) -> Self {
92 Box::new(other)
93 }
94}
95
96pub trait Compiler: Send + std::fmt::Debug {
98 fn name(&self) -> &str;
102
103 fn deterministic_id(&self) -> String;
106
107 fn with_opts(
115 &mut self,
116 suggested_compiler_opts: &UserCompilerOptimizations,
117 ) -> Result<(), CompileError> {
118 _ = suggested_compiler_opts;
119 Ok(())
120 }
121
122 #[cfg(feature = "translator")]
126 fn validate_module(&self, features: &Features, data: &[u8]) -> Result<(), CompileError> {
127 let mut wasm_features = WasmFeatures::empty();
128 wasm_features.set(WasmFeatures::BULK_MEMORY, features.bulk_memory);
129 wasm_features.set(WasmFeatures::THREADS, features.threads);
130 wasm_features.set(WasmFeatures::REFERENCE_TYPES, features.reference_types);
131 wasm_features.set(WasmFeatures::MULTI_VALUE, features.multi_value);
132 wasm_features.set(WasmFeatures::SIMD, features.simd);
133 wasm_features.set(WasmFeatures::TAIL_CALL, features.tail_call);
134 wasm_features.set(WasmFeatures::MULTI_MEMORY, features.multi_memory);
135 wasm_features.set(WasmFeatures::MEMORY64, features.memory64);
136 wasm_features.set(WasmFeatures::EXCEPTIONS, features.exceptions);
137 wasm_features.set(WasmFeatures::EXTENDED_CONST, features.extended_const);
138 wasm_features.set(WasmFeatures::RELAXED_SIMD, features.relaxed_simd);
139 wasm_features.set(WasmFeatures::WIDE_ARITHMETIC, features.wide_arithmetic);
140 wasm_features.set(WasmFeatures::TAIL_CALL, features.tail_call);
141 wasm_features.set(WasmFeatures::MUTABLE_GLOBAL, true);
142 wasm_features.set(WasmFeatures::SATURATING_FLOAT_TO_INT, true);
143 wasm_features.set(WasmFeatures::FLOATS, true);
144 wasm_features.set(WasmFeatures::SIGN_EXTENSION, true);
145 wasm_features.set(WasmFeatures::GC_TYPES, true);
146
147 let mut validator = Validator::new_with_features(wasm_features);
148 validator
149 .validate_all(data)
150 .map_err(|e| CompileError::Validate(format!("{e}")))?;
151 Ok(())
152 }
153
154 fn compile_module(
158 &self,
159 target: &Target,
160 module: &CompileModuleInfo,
161 module_translation: &ModuleTranslationState,
162 function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
164 progress_callback: Option<&CompilationProgressCallback>,
165 ) -> Result<Compilation, CompileError>;
166
167 fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>];
169
170 fn enable_readonly_funcref_table(&self) -> bool {
172 false
173 }
174
175 fn get_cpu_features_used(&self, cpu_features: &EnumSet<CpuFeature>) -> EnumSet<CpuFeature> {
177 *cpu_features
178 }
179
180 fn get_perfmap_enabled(&self) -> bool {
182 false
183 }
184}
185
186pub struct FunctionBucket<'a> {
188 functions: Vec<(LocalFunctionIndex, &'a FunctionBodyData<'a>)>,
189 pub size: usize,
191}
192
193impl<'a> FunctionBucket<'a> {
194 pub fn new() -> Self {
196 Self {
197 functions: Vec::new(),
198 size: 0,
199 }
200 }
201}
202
203pub fn build_function_buckets<'a>(
205 function_body_inputs: &'a PrimaryMap<LocalFunctionIndex, FunctionBodyData<'a>>,
206 bucket_threshold_size: u64,
207) -> Vec<FunctionBucket<'a>> {
208 let mut function_bodies = function_body_inputs
209 .iter()
210 .sorted_by_key(|(id, body)| Reverse((body.data.len(), id.as_u32())))
211 .collect_vec();
212
213 let mut buckets = Vec::new();
214
215 while !function_bodies.is_empty() {
216 let mut next_function_body = Vec::with_capacity(function_bodies.len());
217 let mut bucket = FunctionBucket::new();
218
219 for (fn_index, fn_body) in function_bodies.into_iter() {
220 if bucket.size + fn_body.data.len() <= bucket_threshold_size as usize
221 || bucket.size == 0
223 {
224 bucket.size += fn_body.data.len();
225 bucket.functions.push((fn_index, fn_body));
226 } else {
227 next_function_body.push((fn_index, fn_body));
228 }
229 }
230
231 function_bodies = next_function_body;
232 buckets.push(bucket);
233 }
234
235 buckets
236}
237
238pub trait CompiledFunction {}
240
241pub trait FuncTranslator {}
243
244#[allow(clippy::too_many_arguments)]
246pub fn translate_function_buckets<'a, C, T, F, G>(
247 pool: &rayon::ThreadPool,
248 func_translator_builder: F,
249 translate_fn: G,
250 progress: Option<ProgressContext>,
251 buckets: &[FunctionBucket<'a>],
252) -> Result<Vec<C>, CompileError>
253where
254 T: FuncTranslator,
255 C: CompiledFunction + Send + Sync,
256 F: Fn() -> T + Send + Sync + Copy,
257 G: Fn(&mut T, &LocalFunctionIndex, &FunctionBodyData) -> Result<C, CompileError>
258 + Send
259 + Sync
260 + Copy,
261{
262 let progress = progress.as_ref();
263
264 let functions = pool.install(|| {
265 let (bucket_tx, bucket_rx) = unbounded::<&FunctionBucket<'a>>();
266 for bucket in buckets {
267 bucket_tx.send(bucket).map_err(|e| {
268 CompileError::Resource(format!("cannot allocate crossbeam channel item: {e}"))
269 })?;
270 }
271 drop(bucket_tx);
272
273 let (result_tx, result_rx) =
274 unbounded::<Result<Vec<(LocalFunctionIndex, C)>, CompileError>>();
275
276 pool.scope(|s| {
277 let worker_count = pool.current_num_threads().max(1);
278 for _ in 0..worker_count {
279 let bucket_rx = bucket_rx.clone();
280 let result_tx = result_tx.clone();
281 s.spawn(move |_| {
282 let mut func_translator = func_translator_builder();
283
284 while let Ok(bucket) = bucket_rx.recv() {
285 let bucket_result = (|| {
286 let mut translated_functions = Vec::new();
287 for (i, input) in bucket.functions.iter() {
288 let translated = translate_fn(&mut func_translator, i, input)?;
289 if let Some(progress) = progress {
290 progress.notify_steps(input.data.len() as u64)?;
291 }
292 translated_functions.push((*i, translated));
293 }
294 Ok(translated_functions)
295 })();
296
297 if result_tx.send(bucket_result).is_err() {
298 break;
299 }
300 }
301 });
302 }
303 });
304
305 drop(result_tx);
306 let mut functions = Vec::with_capacity(buckets.iter().map(|b| b.functions.len()).sum());
307 for _ in 0..buckets.len() {
308 match result_rx.recv().map_err(|e| {
309 CompileError::Resource(format!("cannot allocate crossbeam channel item: {e}"))
310 })? {
311 Ok(bucket_functions) => functions.extend(bucket_functions),
312 Err(err) => return Err(err),
313 }
314 }
315 Ok(functions)
316 })?;
317
318 Ok(functions
319 .into_iter()
320 .sorted_by_key(|x| x.0)
321 .map(|(_, body)| body)
322 .collect_vec())
323}
324
325pub const WASM_LARGE_FUNCTION_THRESHOLD: u64 = 100_000;
327
328pub const WASM_TRAMPOLINE_ESTIMATED_BODY_SIZE: u64 = 1_000;