Skip to content

Commit 4c94625

Browse files
committed
Update Cranelift to 0.82
1 parent 3ad9552 commit 4c94625

File tree

25 files changed

+308
-462
lines changed

25 files changed

+308
-462
lines changed

Cargo.lock

+18-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/compiler-cranelift/Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ edition = "2018"
1515
wasmer-compiler = { path = "../compiler", version = "=2.2.1", features = ["translator"], default-features = false }
1616
wasmer-vm = { path = "../vm", version = "=2.2.1" }
1717
wasmer-types = { path = "../types", version = "=2.2.1", default-features = false, features = ["std"] }
18-
cranelift-entity = { version = "0.76", default-features = false }
19-
cranelift-codegen = { version = "0.76", default-features = false, features = ["x86", "arm64"] }
20-
cranelift-frontend = { version = "0.76", default-features = false }
18+
cranelift-entity = { version = "0.82", default-features = false }
19+
cranelift-codegen = { version = "0.82", default-features = false, features = ["x86", "arm64"] }
20+
cranelift-frontend = { version = "0.82", default-features = false }
2121
tracing = "0.1"
2222
hashbrown = { version = "0.11", optional = true }
2323
rayon = "1.5"
2424
more-asserts = "0.2"
25-
gimli = { version = "0.25", optional = true }
25+
gimli = { version = "0.26", optional = true }
2626
smallvec = "1.6"
2727
loupe = "0.1"
2828
target-lexicon = { version = "0.12.2", default-features = false }
2929

3030
[dev-dependencies]
31-
cranelift-codegen = { version = "0.76", features = ["all-arch"] }
31+
cranelift-codegen = { version = "0.82", features = ["all-arch"] }
3232
lazy_static = "1.4"
3333

3434
[badges]

lib/compiler-cranelift/src/address_map.rs

+10-28
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,26 @@
11
// This file contains code from external sources.
22
// Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md
33

4+
use cranelift_codegen::Context;
45
use cranelift_codegen::MachSrcLoc;
5-
use cranelift_codegen::{isa, Context};
66
use wasmer_compiler::{wasmparser::Range, FunctionAddressMap, InstructionAddressMap, SourceLoc};
77

88
pub fn get_function_address_map<'data>(
99
context: &Context,
1010
range: Range,
1111
body_len: usize,
12-
isa: &dyn isa::TargetIsa,
1312
) -> FunctionAddressMap {
1413
let mut instructions = Vec::new();
1514

16-
if let Some(ref mcr) = &context.mach_compile_result {
17-
// New-style backend: we have a `MachCompileResult` that will give us `MachSrcLoc` mapping
18-
// tuples.
19-
for &MachSrcLoc { start, end, loc } in mcr.buffer.get_srclocs_sorted() {
20-
instructions.push(InstructionAddressMap {
21-
srcloc: SourceLoc::new(loc.bits()),
22-
code_offset: start as usize,
23-
code_len: (end - start) as usize,
24-
});
25-
}
26-
} else {
27-
let func = &context.func;
28-
let mut blocks = func.layout.blocks().collect::<Vec<_>>();
29-
blocks.sort_by_key(|block| func.offsets[*block]); // Ensure inst offsets always increase
30-
31-
let encinfo = isa.encoding_info();
32-
for block in blocks {
33-
for (offset, inst, size) in func.inst_offsets(block, &encinfo) {
34-
let srcloc = func.srclocs[inst];
35-
instructions.push(InstructionAddressMap {
36-
srcloc: SourceLoc::new(srcloc.bits()),
37-
code_offset: offset as usize,
38-
code_len: size as usize,
39-
});
40-
}
41-
}
15+
// New-style backend: we have a `MachCompileResult` that will give us `MachSrcLoc` mapping
16+
// tuples.
17+
let mcr = context.mach_compile_result.as_ref().unwrap();
18+
for &MachSrcLoc { start, end, loc } in mcr.buffer.get_srclocs_sorted() {
19+
instructions.push(InstructionAddressMap {
20+
srcloc: SourceLoc::new(loc.bits()),
21+
code_offset: start as usize,
22+
code_len: (end - start) as usize,
23+
});
4224
}
4325

4426
// Generate artificial srcloc for function start/end to identify boundary

lib/compiler-cranelift/src/compiler.rs

+95-32
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,35 @@ use crate::config::Cranelift;
55
#[cfg(feature = "unwind")]
66
use crate::dwarf::WriterRelocate;
77
use crate::func_environ::{get_function_name, FuncEnvironment};
8-
use crate::sink::{RelocSink, TrapSink};
98
use crate::trampoline::{
109
make_trampoline_dynamic_function, make_trampoline_function_call, FunctionBuilderContext,
1110
};
1211
use crate::translator::{
13-
compiled_function_unwind_info, signature_to_cranelift_ir, transform_jump_table,
14-
CraneliftUnwindInfo, FuncTranslator,
12+
compiled_function_unwind_info, irlibcall_to_libcall, irreloc_to_relocationkind,
13+
signature_to_cranelift_ir, CraneliftUnwindInfo, FuncTranslator,
1514
};
16-
use cranelift_codegen::ir;
15+
use cranelift_codegen::ir::ExternalName;
1716
use cranelift_codegen::print_errors::pretty_error;
18-
use cranelift_codegen::{binemit, Context};
17+
use cranelift_codegen::{ir, MachReloc};
18+
use cranelift_codegen::{Context, MachTrap};
1919
#[cfg(feature = "unwind")]
2020
use gimli::write::{Address, EhFrame, FrameTable};
2121
use loupe::MemoryUsage;
2222
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
2323
use std::sync::Arc;
24-
use wasmer_compiler::CompileError;
25-
use wasmer_compiler::{CallingConvention, ModuleTranslationState, Target};
24+
use wasmer_compiler::{
25+
CallingConvention, ModuleTranslationState, RelocationTarget, Target, TrapInformation,
26+
};
2627
use wasmer_compiler::{
2728
Compilation, CompileModuleInfo, CompiledFunction, CompiledFunctionFrameInfo,
2829
CompiledFunctionUnwindInfo, Compiler, Dwarf, FunctionBinaryReader, FunctionBody,
2930
FunctionBodyData, MiddlewareBinaryReader, ModuleMiddleware, ModuleMiddlewareChain,
3031
SectionIndex,
3132
};
33+
use wasmer_compiler::{CompileError, Relocation};
3234
use wasmer_types::entity::{EntityRef, PrimaryMap};
33-
use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
35+
use wasmer_types::{FunctionIndex, LocalFunctionIndex, ModuleInfo, SignatureIndex};
36+
use wasmer_vm::TrapCode;
3437

3538
/// A compiler that compiles a WebAssembly module with Cranelift, translating the Wasm to Cranelift IR,
3639
/// optimizing it and then translating to assembly.
@@ -66,7 +69,10 @@ impl Compiler for CraneliftCompiler {
6669
module_translation_state: &ModuleTranslationState,
6770
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
6871
) -> Result<Compilation, CompileError> {
69-
let isa = self.config().isa(target);
72+
let isa = self
73+
.config()
74+
.isa(target)
75+
.map_err(|error| CompileError::Codegen(error.to_string()))?;
7076
let frontend_config = isa.frontend_config();
7177
let memory_styles = &compile_info.memory_styles;
7278
let table_styles = &compile_info.table_styles;
@@ -139,20 +145,24 @@ impl Compiler for CraneliftCompiler {
139145
)?;
140146

141147
let mut code_buf: Vec<u8> = Vec::new();
142-
let mut reloc_sink = RelocSink::new(&module, func_index);
143-
let mut trap_sink = TrapSink::new();
144-
let mut stackmap_sink = binemit::NullStackMapSink {};
145148
context
146-
.compile_and_emit(
147-
&*isa,
148-
&mut code_buf,
149-
&mut reloc_sink,
150-
&mut trap_sink,
151-
&mut stackmap_sink,
152-
)
153-
.map_err(|error| {
154-
CompileError::Codegen(pretty_error(&context.func, Some(&*isa), error))
155-
})?;
149+
.compile_and_emit(&*isa, &mut code_buf)
150+
.map_err(|error| CompileError::Codegen(pretty_error(&context.func, error)))?;
151+
152+
let result = context.mach_compile_result.as_ref().unwrap();
153+
let func_relocs = result
154+
.buffer
155+
.relocs()
156+
.into_iter()
157+
.map(|r| mach_reloc_to_reloc(module, r))
158+
.collect::<Vec<_>>();
159+
160+
let traps = result
161+
.buffer
162+
.traps()
163+
.into_iter()
164+
.map(mach_trap_to_trap)
165+
.collect::<Vec<_>>();
156166

157167
let (unwind_info, fde) = match compiled_function_unwind_info(&*isa, &context)? {
158168
#[cfg(feature = "unwind")]
@@ -182,23 +192,16 @@ impl Compiler for CraneliftCompiler {
182192
};
183193

184194
let range = reader.range();
185-
let address_map = get_function_address_map(&context, range, code_buf.len(), &*isa);
186-
187-
// We transform the Cranelift JumpTable's into compiler JumpTables
188-
let func_jt_offsets = transform_jump_table(context.func.jt_offsets);
195+
let address_map = get_function_address_map(&context, range, code_buf.len());
189196

190197
Ok((
191198
CompiledFunction {
192199
body: FunctionBody {
193200
body: code_buf,
194201
unwind_info,
195202
},
196-
jt_offsets: func_jt_offsets,
197-
relocations: reloc_sink.func_relocs,
198-
frame_info: CompiledFunctionFrameInfo {
199-
address_map,
200-
traps: trap_sink.traps,
201-
},
203+
relocations: func_relocs,
204+
frame_info: CompiledFunctionFrameInfo { address_map, traps },
202205
},
203206
fde,
204207
))
@@ -262,3 +265,63 @@ impl Compiler for CraneliftCompiler {
262265
))
263266
}
264267
}
268+
269+
fn mach_reloc_to_reloc(module: &ModuleInfo, reloc: &MachReloc) -> Relocation {
270+
let &MachReloc {
271+
offset,
272+
srcloc: _,
273+
kind,
274+
ref name,
275+
addend,
276+
} = reloc;
277+
let reloc_target = if let ExternalName::User { namespace, index } = *name {
278+
debug_assert_eq!(namespace, 0);
279+
RelocationTarget::LocalFunc(
280+
module
281+
.local_func_index(FunctionIndex::from_u32(index))
282+
.expect("The provided function should be local"),
283+
)
284+
} else if let ExternalName::LibCall(libcall) = *name {
285+
RelocationTarget::LibCall(irlibcall_to_libcall(libcall))
286+
} else {
287+
panic!("unrecognized external name")
288+
};
289+
Relocation {
290+
kind: irreloc_to_relocationkind(kind),
291+
reloc_target,
292+
offset,
293+
addend,
294+
}
295+
}
296+
297+
fn mach_trap_to_trap(trap: &MachTrap) -> TrapInformation {
298+
let &MachTrap {
299+
offset,
300+
srcloc: _,
301+
code,
302+
} = trap;
303+
TrapInformation {
304+
code_offset: offset,
305+
trap_code: translate_ir_trapcode(code),
306+
}
307+
}
308+
309+
/// Translates the Cranelift IR TrapCode into generic Trap Code
310+
fn translate_ir_trapcode(trap: ir::TrapCode) -> TrapCode {
311+
match trap {
312+
ir::TrapCode::StackOverflow => TrapCode::StackOverflow,
313+
ir::TrapCode::HeapOutOfBounds => TrapCode::HeapAccessOutOfBounds,
314+
ir::TrapCode::HeapMisaligned => TrapCode::HeapMisaligned,
315+
ir::TrapCode::TableOutOfBounds => TrapCode::TableAccessOutOfBounds,
316+
ir::TrapCode::IndirectCallToNull => TrapCode::IndirectCallToNull,
317+
ir::TrapCode::BadSignature => TrapCode::BadSignature,
318+
ir::TrapCode::IntegerOverflow => TrapCode::IntegerOverflow,
319+
ir::TrapCode::IntegerDivisionByZero => TrapCode::IntegerDivisionByZero,
320+
ir::TrapCode::BadConversionToInteger => TrapCode::BadConversionToInteger,
321+
ir::TrapCode::UnreachableCodeReached => TrapCode::UnreachableCodeReached,
322+
ir::TrapCode::Interrupt => unimplemented!("Interrupts not supported"),
323+
ir::TrapCode::User(_user_code) => unimplemented!("User trap code not supported"),
324+
// ir::TrapCode::Interrupt => TrapCode::Interrupt,
325+
// ir::TrapCode::User(user_code) => TrapCode::User(user_code),
326+
}
327+
}

0 commit comments

Comments
 (0)