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 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::TAIL_CALL, features.tail_call);
137 wasm_features.set(WasmFeatures::MUTABLE_GLOBAL, true);
138 wasm_features.set(WasmFeatures::SATURATING_FLOAT_TO_INT, true);
139 wasm_features.set(WasmFeatures::FLOATS, true);
140 wasm_features.set(WasmFeatures::SIGN_EXTENSION, true);
141 wasm_features.set(WasmFeatures::GC_TYPES, true);
142
143 let mut validator = Validator::new_with_features(wasm_features);
144 validator
145 .validate_all(data)
146 .map_err(|e| CompileError::Validate(format!("{e}")))?;
147 Ok(())
148 }
149
150 fn compile_module(
154 &self,
155 target: &Target,
156 module: &CompileModuleInfo,
157 module_translation: &ModuleTranslationState,
158 function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
160 progress_callback: Option<&CompilationProgressCallback>,
161 ) -> Result<Compilation, CompileError>;
162
163 fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>];
165
166 fn get_cpu_features_used(&self, cpu_features: &EnumSet<CpuFeature>) -> EnumSet<CpuFeature> {
168 *cpu_features
169 }
170
171 fn get_perfmap_enabled(&self) -> bool {
173 false
174 }
175}
176
177pub struct FunctionBucket<'a> {
179 functions: Vec<(LocalFunctionIndex, &'a FunctionBodyData<'a>)>,
180 pub size: usize,
182}
183
184impl<'a> FunctionBucket<'a> {
185 pub fn new() -> Self {
187 Self {
188 functions: Vec::new(),
189 size: 0,
190 }
191 }
192}
193
194pub fn build_function_buckets<'a>(
196 function_body_inputs: &'a PrimaryMap<LocalFunctionIndex, FunctionBodyData<'a>>,
197 bucket_threshold_size: u64,
198) -> Vec<FunctionBucket<'a>> {
199 let mut function_bodies = function_body_inputs
200 .iter()
201 .sorted_by_key(|(id, body)| Reverse((body.data.len(), id.as_u32())))
202 .collect_vec();
203
204 let mut buckets = Vec::new();
205
206 while !function_bodies.is_empty() {
207 let mut next_function_body = Vec::with_capacity(function_bodies.len());
208 let mut bucket = FunctionBucket::new();
209
210 for (fn_index, fn_body) in function_bodies.into_iter() {
211 if bucket.size + fn_body.data.len() <= bucket_threshold_size as usize
212 || bucket.size == 0
214 {
215 bucket.size += fn_body.data.len();
216 bucket.functions.push((fn_index, fn_body));
217 } else {
218 next_function_body.push((fn_index, fn_body));
219 }
220 }
221
222 function_bodies = next_function_body;
223 buckets.push(bucket);
224 }
225
226 buckets
227}
228
229pub trait CompiledFunction {}
231
232pub trait FuncTranslator {}
234
235#[allow(clippy::too_many_arguments)]
237pub fn translate_function_buckets<'a, C, T, F, G>(
238 pool: &rayon::ThreadPool,
239 func_translator_builder: F,
240 translate_fn: G,
241 progress: Option<ProgressContext>,
242 buckets: &[FunctionBucket<'a>],
243) -> Result<Vec<C>, CompileError>
244where
245 T: FuncTranslator,
246 C: CompiledFunction + Send + Sync,
247 F: Fn() -> T + Send + Sync + Copy,
248 G: Fn(&mut T, &LocalFunctionIndex, &FunctionBodyData) -> Result<C, CompileError>
249 + Send
250 + Sync
251 + Copy,
252{
253 let progress = progress.as_ref();
254
255 let functions = pool.install(|| {
256 let (bucket_tx, bucket_rx) = unbounded::<&FunctionBucket<'a>>();
257 for bucket in buckets {
258 bucket_tx.send(bucket).map_err(|e| {
259 CompileError::Resource(format!("cannot allocate crossbeam channel item: {e}"))
260 })?;
261 }
262 drop(bucket_tx);
263
264 let (result_tx, result_rx) =
265 unbounded::<Result<Vec<(LocalFunctionIndex, C)>, CompileError>>();
266
267 pool.scope(|s| {
268 let worker_count = pool.current_num_threads().max(1);
269 for _ in 0..worker_count {
270 let bucket_rx = bucket_rx.clone();
271 let result_tx = result_tx.clone();
272 s.spawn(move |_| {
273 let mut func_translator = func_translator_builder();
274
275 while let Ok(bucket) = bucket_rx.recv() {
276 let bucket_result = (|| {
277 let mut translated_functions = Vec::new();
278 for (i, input) in bucket.functions.iter() {
279 let translated = translate_fn(&mut func_translator, i, input)?;
280 if let Some(progress) = progress {
281 progress.notify_steps(input.data.len() as u64)?;
282 }
283 translated_functions.push((*i, translated));
284 }
285 Ok(translated_functions)
286 })();
287
288 if result_tx.send(bucket_result).is_err() {
289 break;
290 }
291 }
292 });
293 }
294 });
295
296 drop(result_tx);
297 let mut functions = Vec::with_capacity(buckets.iter().map(|b| b.functions.len()).sum());
298 for _ in 0..buckets.len() {
299 match result_rx.recv().map_err(|e| {
300 CompileError::Resource(format!("cannot allocate crossbeam channel item: {e}"))
301 })? {
302 Ok(bucket_functions) => functions.extend(bucket_functions),
303 Err(err) => return Err(err),
304 }
305 }
306 Ok(functions)
307 })?;
308
309 Ok(functions
310 .into_iter()
311 .sorted_by_key(|x| x.0)
312 .map(|(_, body)| body)
313 .collect_vec())
314}
315
316pub const WASM_LARGE_FUNCTION_THRESHOLD: u64 = 100_000;
318
319pub const WASM_TRAMPOLINE_ESTIMATED_BODY_SIZE: u64 = 1_000;