Skip to content

Commit a5db8bc

Browse files
committed
fix winch trampoline compilation
1 parent dd21c00 commit a5db8bc

File tree

1 file changed

+152
-2
lines changed

1 file changed

+152
-2
lines changed

crates/winch/src/compiler.rs

Lines changed: 152 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct CompilationContext {
2525

2626
pub(crate) struct Compiler {
2727
isa: Box<dyn TargetIsa>,
28-
trampolines: Box<dyn wasmtime_environ::Compiler>,
28+
trampolines: NoInlineCompiler,
2929
contexts: Mutex<Vec<CompilationContext>>,
3030
tunables: Tunables,
3131
}
@@ -38,7 +38,7 @@ impl Compiler {
3838
) -> Self {
3939
Self {
4040
isa,
41-
trampolines,
41+
trampolines: NoInlineCompiler(trampolines),
4242
contexts: Mutex::new(Vec::new()),
4343
tunables,
4444
}
@@ -227,3 +227,153 @@ impl wasmtime_environ::Compiler for Compiler {
227227
self.trampolines.compiled_function_relocation_targets(func)
228228
}
229229
}
230+
231+
/// A wrapper around another `Compiler` implementation that may or may not be an
232+
/// inlining compiler and turns it into a non-inlining compiler.
233+
struct NoInlineCompiler(Box<dyn wasmtime_environ::Compiler>);
234+
235+
impl wasmtime_environ::Compiler for NoInlineCompiler {
236+
fn inlining_compiler(&self) -> Option<&dyn wasmtime_environ::InliningCompiler> {
237+
None
238+
}
239+
240+
fn compile_function(
241+
&self,
242+
translation: &ModuleTranslation<'_>,
243+
index: DefinedFuncIndex,
244+
data: FunctionBodyData<'_>,
245+
types: &ModuleTypesBuilder,
246+
symbol: &str,
247+
) -> Result<CompiledFunctionBody, CompileError> {
248+
let input = data.body.clone();
249+
let mut body = self
250+
.0
251+
.compile_function(translation, index, data, types, symbol)?;
252+
if let Some(c) = self.0.inlining_compiler() {
253+
c.finish_compiling(&mut body, Some(input), symbol)
254+
.map_err(|e| CompileError::Codegen(e.to_string()))?;
255+
}
256+
Ok(body)
257+
}
258+
259+
fn compile_array_to_wasm_trampoline(
260+
&self,
261+
translation: &ModuleTranslation<'_>,
262+
types: &ModuleTypesBuilder,
263+
index: DefinedFuncIndex,
264+
symbol: &str,
265+
) -> Result<CompiledFunctionBody, CompileError> {
266+
let mut body =
267+
self.0
268+
.compile_array_to_wasm_trampoline(translation, types, index, symbol)?;
269+
if let Some(c) = self.0.inlining_compiler() {
270+
c.finish_compiling(&mut body, None, symbol)
271+
.map_err(|e| CompileError::Codegen(e.to_string()))?;
272+
}
273+
Ok(body)
274+
}
275+
276+
fn compile_wasm_to_array_trampoline(
277+
&self,
278+
wasm_func_ty: &wasmtime_environ::WasmFuncType,
279+
symbol: &str,
280+
) -> Result<CompiledFunctionBody, CompileError> {
281+
let mut body = self
282+
.0
283+
.compile_wasm_to_array_trampoline(wasm_func_ty, symbol)?;
284+
if let Some(c) = self.0.inlining_compiler() {
285+
c.finish_compiling(&mut body, None, symbol)
286+
.map_err(|e| CompileError::Codegen(e.to_string()))?;
287+
}
288+
Ok(body)
289+
}
290+
291+
fn compile_wasm_to_builtin(
292+
&self,
293+
index: BuiltinFunctionIndex,
294+
symbol: &str,
295+
) -> Result<CompiledFunctionBody, CompileError> {
296+
let mut body = self.0.compile_wasm_to_builtin(index, symbol)?;
297+
if let Some(c) = self.0.inlining_compiler() {
298+
c.finish_compiling(&mut body, None, symbol)
299+
.map_err(|e| CompileError::Codegen(e.to_string()))?;
300+
}
301+
Ok(body)
302+
}
303+
304+
fn compiled_function_relocation_targets<'a>(
305+
&'a self,
306+
func: &'a dyn Any,
307+
) -> Box<dyn Iterator<Item = RelocationTarget> + 'a> {
308+
self.0.compiled_function_relocation_targets(func)
309+
}
310+
311+
fn append_code(
312+
&self,
313+
obj: &mut Object<'static>,
314+
funcs: &[(String, Box<dyn Any + Send + Sync>)],
315+
resolve_reloc: &dyn Fn(usize, RelocationTarget) -> usize,
316+
) -> Result<Vec<(SymbolId, FunctionLoc)>> {
317+
self.0.append_code(obj, funcs, resolve_reloc)
318+
}
319+
320+
fn triple(&self) -> &target_lexicon::Triple {
321+
self.0.triple()
322+
}
323+
324+
fn flags(&self) -> Vec<(&'static str, wasmtime_environ::FlagValue<'static>)> {
325+
self.0.flags()
326+
}
327+
328+
fn isa_flags(&self) -> Vec<(&'static str, wasmtime_environ::FlagValue<'static>)> {
329+
self.0.isa_flags()
330+
}
331+
332+
fn is_branch_protection_enabled(&self) -> bool {
333+
self.0.is_branch_protection_enabled()
334+
}
335+
336+
#[cfg(feature = "component-model")]
337+
fn component_compiler(&self) -> &dyn wasmtime_environ::component::ComponentCompiler {
338+
self
339+
}
340+
341+
fn append_dwarf<'a>(
342+
&self,
343+
obj: &mut Object<'_>,
344+
translations: &'a PrimaryMap<StaticModuleIndex, ModuleTranslation<'a>>,
345+
get_func: &'a dyn Fn(
346+
StaticModuleIndex,
347+
DefinedFuncIndex,
348+
) -> (SymbolId, &'a (dyn Any + Send + Sync)),
349+
dwarf_package_bytes: Option<&'a [u8]>,
350+
tunables: &'a Tunables,
351+
) -> Result<()> {
352+
self.0
353+
.append_dwarf(obj, translations, get_func, dwarf_package_bytes, tunables)
354+
}
355+
}
356+
357+
#[cfg(feature = "component-model")]
358+
impl wasmtime_environ::component::ComponentCompiler for NoInlineCompiler {
359+
fn compile_trampoline(
360+
&self,
361+
component: &wasmtime_environ::component::ComponentTranslation,
362+
types: &wasmtime_environ::component::ComponentTypesBuilder,
363+
trampoline: wasmtime_environ::component::TrampolineIndex,
364+
tunables: &Tunables,
365+
symbol: &str,
366+
) -> Result<wasmtime_environ::component::AllCallFunc<CompiledFunctionBody>> {
367+
let mut body = self
368+
.0
369+
.component_compiler()
370+
.compile_trampoline(component, types, trampoline, tunables, symbol)?;
371+
if let Some(c) = self.0.inlining_compiler() {
372+
c.finish_compiling(&mut body.array_call, None, symbol)
373+
.map_err(|e| CompileError::Codegen(e.to_string()))?;
374+
c.finish_compiling(&mut body.wasm_call, None, symbol)
375+
.map_err(|e| CompileError::Codegen(e.to_string()))?;
376+
}
377+
Ok(body)
378+
}
379+
}

0 commit comments

Comments
 (0)