Skip to content

Commit 4693d9a

Browse files
authored
chore(cranelift): unify rayon and non-rayon code with closure (#5896)
1 parent 9600a16 commit 4693d9a

File tree

2 files changed

+33
-132
lines changed

2 files changed

+33
-132
lines changed

examples/throw_exception.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
122122
let instance = Instance::new(&mut store, &module, &import_object)?;
123123

124124
// Here we go.
125-
let f: TypedFunction<(), ()> = instance.exports.get_function("f")?.typed(&mut store)?;
125+
let f: TypedFunction<(), ()> = instance.exports.get_function("f")?.typed(&store)?;
126126

127127
println!("Calling `f` function...");
128128
let result = f.call(&mut store);

lib/compiler-cranelift/src/compiler.rs

Lines changed: 32 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -118,133 +118,9 @@ impl CraneliftCompiler {
118118
}
119119
};
120120

121-
#[cfg_attr(not(feature = "unwind"), allow(unused_mut))]
122-
let mut custom_sections = PrimaryMap::new();
123-
124-
#[cfg(not(feature = "rayon"))]
125-
let mut func_translator = FuncTranslator::new();
126-
#[cfg(not(feature = "rayon"))]
127-
#[cfg_attr(not(feature = "unwind"), allow(unused_variables))]
128-
let (functions, fdes): (Vec<CompiledFunction>, Vec<_>) = function_body_inputs
129-
.iter()
130-
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
131-
.into_iter()
132-
.map(|(i, input)| {
133-
let func_index = module.func_index(i);
134-
let mut context = Context::new();
135-
let mut func_env = FuncEnvironment::new(
136-
isa.frontend_config(),
137-
module,
138-
&signatures,
139-
&memory_styles,
140-
table_styles,
141-
);
142-
context.func.name = match get_function_name(&mut context.func, func_index) {
143-
ExternalName::User(nameref) => {
144-
if context.func.params.user_named_funcs().is_valid(nameref) {
145-
let name = &context.func.params.user_named_funcs()[nameref];
146-
UserFuncName::User(name.clone())
147-
} else {
148-
UserFuncName::default()
149-
}
150-
}
151-
ExternalName::TestCase(testcase) => UserFuncName::Testcase(testcase),
152-
_ => UserFuncName::default(),
153-
};
154-
context.func.signature = signatures[module.functions[func_index]].clone();
155-
// if generate_debug_info {
156-
// context.func.collect_debug_info();
157-
// }
158-
let mut reader =
159-
MiddlewareBinaryReader::new_with_offset(input.data, input.module_offset);
160-
reader.set_middleware_chain(
161-
self.config
162-
.middlewares
163-
.generate_function_middleware_chain(i),
164-
);
165-
166-
func_translator.translate(
167-
module_translation_state,
168-
&mut reader,
169-
&mut context.func,
170-
&mut func_env,
171-
i,
172-
)?;
173-
174-
let mut code_buf: Vec<u8> = Vec::new();
175-
let mut ctrl_plane = Default::default();
176-
let func_name_map = context.func.params.user_named_funcs().clone();
177-
let result = context
178-
.compile(&*isa, &mut ctrl_plane)
179-
.map_err(|error| CompileError::Codegen(error.inner.to_string()))?;
180-
code_buf.extend_from_slice(result.code_buffer());
181-
182-
let func_relocs = result
183-
.buffer
184-
.relocs()
185-
.into_iter()
186-
.map(|r| mach_reloc_to_reloc(module, &func_name_map, r))
187-
.collect::<Vec<_>>();
188-
189-
let traps = result
190-
.buffer
191-
.traps()
192-
.into_iter()
193-
.map(mach_trap_to_trap)
194-
.collect::<Vec<_>>();
195-
196-
let (unwind_info, fde) = match compiled_function_unwind_info(&*isa, &context)? {
197-
#[cfg(feature = "unwind")]
198-
CraneliftUnwindInfo::Fde(fde) => {
199-
if dwarf_frametable.is_some() {
200-
let fde = fde.to_fde(Address::Symbol {
201-
// The symbol is the kind of relocation.
202-
// "0" is used for functions
203-
symbol: WriterRelocate::FUNCTION_SYMBOL,
204-
// We use the addend as a way to specify the
205-
// function index
206-
addend: i.index() as _,
207-
});
208-
// The unwind information is inserted into the dwarf section
209-
(Some(CompiledFunctionUnwindInfo::Dwarf), Some(fde))
210-
} else {
211-
(None, None)
212-
}
213-
}
214-
#[cfg(feature = "unwind")]
215-
other => (other.maybe_into_to_windows_unwind(), None),
216-
217-
// This is a bit hacky, but necessary since gimli is not
218-
// available when the "unwind" feature is disabled.
219-
#[cfg(not(feature = "unwind"))]
220-
other => (other.maybe_into_to_windows_unwind(), None::<()>),
221-
};
222-
223-
let range = reader.range();
224-
let address_map = get_function_address_map(&context, range, code_buf.len());
225-
226-
Ok((
227-
CompiledFunction {
228-
body: FunctionBody {
229-
body: code_buf,
230-
unwind_info,
231-
},
232-
relocations: func_relocs,
233-
frame_info: CompiledFunctionFrameInfo { address_map, traps },
234-
},
235-
fde,
236-
))
237-
})
238-
.collect::<Result<Vec<_>, CompileError>>()?
239-
.into_iter()
240-
.unzip();
241-
#[cfg(feature = "rayon")]
242-
#[cfg_attr(not(feature = "unwind"), allow(unused_variables))]
243-
let (functions, fdes): (Vec<CompiledFunction>, Vec<_>) = function_body_inputs
244-
.iter()
245-
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
246-
.par_iter()
247-
.map_init(FuncTranslator::new, |func_translator, (i, input)| {
121+
let compile_function =
122+
|func_translator: &mut FuncTranslator,
123+
(i, input): (&LocalFunctionIndex, &FunctionBodyData)| {
248124
let func_index = module.func_index(*i);
249125
let mut context = Context::new();
250126
let mut func_env = FuncEnvironment::new(
@@ -368,6 +244,31 @@ impl CraneliftCompiler {
368244
},
369245
fde,
370246
))
247+
};
248+
249+
#[cfg_attr(not(feature = "unwind"), allow(unused_mut))]
250+
let mut custom_sections = PrimaryMap::new();
251+
252+
#[cfg(not(feature = "rayon"))]
253+
let mut func_translator = FuncTranslator::new();
254+
#[cfg(not(feature = "rayon"))]
255+
#[cfg_attr(not(feature = "unwind"), allow(unused_variables))]
256+
let (functions, fdes): (Vec<CompiledFunction>, Vec<_>) = function_body_inputs
257+
.iter()
258+
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
259+
.into_iter()
260+
.map(|(i, input)| compile_function(&mut func_translator, (&i, input)))
261+
.collect::<Result<Vec<_>, CompileError>>()?
262+
.into_iter()
263+
.unzip();
264+
#[cfg(feature = "rayon")]
265+
#[cfg_attr(not(feature = "unwind"), allow(unused_variables))]
266+
let (functions, fdes): (Vec<CompiledFunction>, Vec<_>) = function_body_inputs
267+
.iter()
268+
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
269+
.par_iter()
270+
.map_init(FuncTranslator::new, |func_translator, &(i, input)| {
271+
compile_function(func_translator, (&i, input))
371272
})
372273
.collect::<Result<Vec<_>, CompileError>>()?
373274
.into_iter()
@@ -402,7 +303,7 @@ impl CraneliftCompiler {
402303
.map(|sig| make_trampoline_function_call(&self.config().callbacks, &*isa, &mut cx, sig))
403304
.collect::<Result<Vec<FunctionBody>, CompileError>>()?
404305
.into_iter()
405-
.collect::<PrimaryMap<SignatureIndex, FunctionBody>>();
306+
.collect();
406307
#[cfg(feature = "rayon")]
407308
let function_call_trampolines = module
408309
.signatures
@@ -414,7 +315,7 @@ impl CraneliftCompiler {
414315
})
415316
.collect::<Result<Vec<FunctionBody>, CompileError>>()?
416317
.into_iter()
417-
.collect::<PrimaryMap<SignatureIndex, FunctionBody>>();
318+
.collect();
418319

419320
use wasmer_types::VMOffsets;
420321
let offsets = VMOffsets::new_for_trampolines(frontend_config.pointer_bytes());
@@ -437,7 +338,7 @@ impl CraneliftCompiler {
437338
})
438339
.collect::<Result<Vec<_>, CompileError>>()?
439340
.into_iter()
440-
.collect::<PrimaryMap<FunctionIndex, FunctionBody>>();
341+
.collect();
441342
#[cfg(feature = "rayon")]
442343
let dynamic_function_trampolines = module
443344
.imported_function_types()
@@ -454,7 +355,7 @@ impl CraneliftCompiler {
454355
})
455356
.collect::<Result<Vec<_>, CompileError>>()?
456357
.into_iter()
457-
.collect::<PrimaryMap<FunctionIndex, FunctionBody>>();
358+
.collect();
458359

459360
let got = wasmer_compiler::types::function::GOT::empty();
460361

0 commit comments

Comments
 (0)