Skip to content

Commit 82e3c45

Browse files
committed
wip
1 parent 03a14ac commit 82e3c45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1661
-509
lines changed

Cargo.lock

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

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ members = [
116116
"examples/fib-debug/wasm",
117117
"examples/wasi/wasm",
118118
"examples/tokio/wasm",
119+
"examples/min-platform",
120+
"examples/min-platform/embedding",
119121
"fuzz",
120122
"winch",
121123
"winch/codegen",

cranelift/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ harness = false
2323

2424
[dependencies]
2525
cfg-if = { workspace = true }
26-
cranelift-codegen = { workspace = true, features = ["disas", "trace-log"] }
26+
cranelift-codegen = { workspace = true, features = ["disas", "trace-log", "timing"] }
2727
cranelift-entity = { workspace = true }
2828
cranelift-interpreter = { workspace = true }
2929
cranelift-reader = { workspace = true }

cranelift/codegen/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ cranelift-codegen-meta = { path = "meta", version = "0.103.0" }
4848
cranelift-isle = { path = "../isle/isle", version = "=0.103.0" }
4949

5050
[features]
51-
default = ["std", "unwind", "host-arch"]
51+
default = ["std", "unwind", "host-arch", "timing"]
5252

5353
# The "std" feature enables use of libstd. The "core" feature enables use
5454
# of some minimal std-like replacement libraries. At least one of these two
@@ -114,6 +114,11 @@ isle-errors = ["cranelift-isle/fancy-errors"]
114114
# inspection, rather than inside of target/.
115115
isle-in-source-tree = []
116116

117+
# Enable tracking how long passes take in Cranelift.
118+
#
119+
# Enabled by default.
120+
timing = []
121+
117122
[[bench]]
118123
name = "x64-evex-encoding"
119124
harness = false

cranelift/codegen/src/timing.rs

+74-48
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use core::fmt;
66
use std::any::Any;
77
use std::boxed::Box;
8-
use std::cell::{Cell, RefCell};
8+
use std::cell::RefCell;
99
use std::mem;
10-
use std::time::{Duration, Instant};
10+
use std::time::Duration;
1111

1212
// Each pass that can be timed is predefined with the `define_passes!` macro. Each pass has a
1313
// snake_case name and a plain text description used when printing out the timing report.
@@ -130,22 +130,6 @@ fn start_pass(pass: Pass) -> Box<dyn Any> {
130130
PROFILER.with(|profiler| profiler.borrow().start_pass(pass))
131131
}
132132

133-
/// A timing token is responsible for timing the currently running pass. Timing starts when it
134-
/// is created and ends when it is dropped.
135-
///
136-
/// Multiple passes can be active at the same time, but they must be started and stopped in a
137-
/// LIFO fashion.
138-
struct DefaultTimingToken {
139-
/// Start time for this pass.
140-
start: Instant,
141-
142-
// Pass being timed by this token.
143-
pass: Pass,
144-
145-
// The previously active pass which will be restored when this token is dropped.
146-
prev: Pass,
147-
}
148-
149133
/// Accumulated timing information for a single pass.
150134
#[derive(Default, Copy, Clone)]
151135
struct PassTime {
@@ -215,49 +199,91 @@ impl fmt::Display for PassTimes {
215199

216200
// Information about passes in a single thread.
217201
thread_local! {
218-
static CURRENT_PASS: Cell<Pass> = const { Cell::new(Pass::None) };
219202
static PASS_TIME: RefCell<PassTimes> = RefCell::new(Default::default());
220203
}
221204

222205
/// The default profiler. You can get the results using [`take_current`].
223206
pub struct DefaultProfiler;
224207

225-
impl Profiler for DefaultProfiler {
226-
fn start_pass(&self, pass: Pass) -> Box<dyn Any> {
227-
let prev = CURRENT_PASS.with(|p| p.replace(pass));
228-
log::debug!("timing: Starting {}, (during {})", pass, prev);
229-
Box::new(DefaultTimingToken {
230-
start: Instant::now(),
231-
pass,
232-
prev,
233-
})
234-
}
235-
}
236-
237-
/// Dropping a timing token indicated the end of the pass.
238-
impl Drop for DefaultTimingToken {
239-
fn drop(&mut self) {
240-
let duration = self.start.elapsed();
241-
log::debug!("timing: Ending {}: {}ms", self.pass, duration.as_millis());
242-
let old_cur = CURRENT_PASS.with(|p| p.replace(self.prev));
243-
debug_assert_eq!(self.pass, old_cur, "Timing tokens dropped out of order");
244-
PASS_TIME.with(|rc| {
245-
let mut table = rc.borrow_mut();
246-
table.pass[self.pass.idx()].total += duration;
247-
if let Some(parent) = table.pass.get_mut(self.prev.idx()) {
248-
parent.child += duration;
249-
}
250-
})
251-
}
252-
}
253-
254208
/// Take the current accumulated pass timings and reset the timings for the current thread.
255209
///
256210
/// Only applies when [`DefaultProfiler`] is used.
257211
pub fn take_current() -> PassTimes {
258212
PASS_TIME.with(|rc| mem::take(&mut *rc.borrow_mut()))
259213
}
260214

215+
#[cfg(feature = "timing")]
216+
mod enabled {
217+
use super::{DefaultProfiler, Pass, Profiler, PASS_TIME};
218+
use std::any::Any;
219+
use std::boxed::Box;
220+
use std::cell::Cell;
221+
use std::time::Instant;
222+
223+
// Information about passes in a single thread.
224+
thread_local! {
225+
static CURRENT_PASS: Cell<Pass> = const { Cell::new(Pass::None) };
226+
}
227+
228+
impl Profiler for DefaultProfiler {
229+
fn start_pass(&self, pass: Pass) -> Box<dyn Any> {
230+
let prev = CURRENT_PASS.with(|p| p.replace(pass));
231+
log::debug!("timing: Starting {}, (during {})", pass, prev);
232+
Box::new(DefaultTimingToken {
233+
start: Instant::now(),
234+
pass,
235+
prev,
236+
})
237+
}
238+
}
239+
240+
/// A timing token is responsible for timing the currently running pass. Timing starts when it
241+
/// is created and ends when it is dropped.
242+
///
243+
/// Multiple passes can be active at the same time, but they must be started and stopped in a
244+
/// LIFO fashion.
245+
struct DefaultTimingToken {
246+
/// Start time for this pass.
247+
start: Instant,
248+
249+
// Pass being timed by this token.
250+
pass: Pass,
251+
252+
// The previously active pass which will be restored when this token is dropped.
253+
prev: Pass,
254+
}
255+
256+
/// Dropping a timing token indicated the end of the pass.
257+
impl Drop for DefaultTimingToken {
258+
fn drop(&mut self) {
259+
let duration = self.start.elapsed();
260+
log::debug!("timing: Ending {}: {}ms", self.pass, duration.as_millis());
261+
let old_cur = CURRENT_PASS.with(|p| p.replace(self.prev));
262+
debug_assert_eq!(self.pass, old_cur, "Timing tokens dropped out of order");
263+
PASS_TIME.with(|rc| {
264+
let mut table = rc.borrow_mut();
265+
table.pass[self.pass.idx()].total += duration;
266+
if let Some(parent) = table.pass.get_mut(self.prev.idx()) {
267+
parent.child += duration;
268+
}
269+
})
270+
}
271+
}
272+
}
273+
274+
#[cfg(not(feature = "timing"))]
275+
mod disabled {
276+
use super::{DefaultProfiler, Pass, Profiler};
277+
use std::any::Any;
278+
use std::boxed::Box;
279+
280+
impl Profiler for DefaultProfiler {
281+
fn start_pass(&self, _pass: Pass) -> Box<dyn Any> {
282+
Box::new(())
283+
}
284+
}
285+
}
286+
261287
#[cfg(test)]
262288
mod tests {
263289
use super::*;

crates/cranelift/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ anyhow = { workspace = true }
1818
log = { workspace = true }
1919
wasmtime-environ = { workspace = true }
2020
cranelift-wasm = { workspace = true }
21-
cranelift-codegen = { workspace = true, features = ["default"] }
21+
cranelift-codegen = { workspace = true, features = ["host-arch"] }
2222
cranelift-frontend = { workspace = true }
2323
cranelift-entity = { workspace = true }
2424
cranelift-native = { workspace = true }

crates/jit/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ ittapi = { version = "0.4.0", optional = true }
4646
[features]
4747
profiling = ['dep:wasmtime-jit-debug', 'dep:ittapi']
4848
demangle = ['dep:rustc-demangle', 'dep:cpp_demangle']
49+
debug-builtins = []

crates/jit/src/code_memory.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
//! Memory management for executable code.
22
33
use crate::subslice_range;
4-
use crate::unwind::UnwindRegistration;
54
use anyhow::{anyhow, bail, Context, Result};
65
use object::read::{File, Object, ObjectSection};
76
use object::ObjectSymbol;
87
use std::mem::ManuallyDrop;
98
use std::ops::Range;
109
use wasmtime_environ::obj;
1110
use wasmtime_jit_icache_coherence as icache_coherence;
12-
use wasmtime_runtime::libcalls;
13-
use wasmtime_runtime::MmapVec;
11+
use wasmtime_runtime::{libcalls, MmapVec, UnwindRegistration};
1412

1513
/// Management of executable memory within a `MmapVec`
1614
///

crates/jit/src/instantiate.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
//! steps.
55
66
use crate::code_memory::CodeMemory;
7-
use crate::debug::create_gdbjit_image;
87
use crate::profiling::ProfilingAgent;
9-
use anyhow::{bail, Context, Error, Result};
8+
use anyhow::{bail, Error, Result};
109
use object::write::{Object, SectionId, StandardSegment, WritableBuffer};
1110
use object::SectionKind;
1211
use serde_derive::{Deserialize, Serialize};
@@ -19,9 +18,7 @@ use wasmtime_environ::{
1918
DefinedFuncIndex, FuncIndex, FunctionLoc, MemoryInitialization, Module, ModuleTranslation,
2019
PrimaryMap, SignatureIndex, StackMapInformation, Tunables, WasmFunctionInfo,
2120
};
22-
use wasmtime_runtime::{
23-
CompiledModuleId, CompiledModuleIdAllocator, GdbJitImageRegistration, MmapVec,
24-
};
21+
use wasmtime_runtime::{CompiledModuleId, CompiledModuleIdAllocator, MmapVec};
2522

2623
/// Secondary in-memory results of function compilation.
2724
#[derive(Serialize, Deserialize)]
@@ -426,7 +423,8 @@ pub struct CompiledModule {
426423
wasm_to_native_trampolines: Vec<(SignatureIndex, FunctionLoc)>,
427424
meta: Metadata,
428425
code_memory: Arc<CodeMemory>,
429-
dbg_jit_registration: Option<GdbJitImageRegistration>,
426+
#[cfg(feature = "debug-builtins")]
427+
dbg_jit_registration: Option<wasmtime_runtime::GdbJitImageRegistration>,
430428
/// A unique ID used to register this module with the engine.
431429
unique_id: CompiledModuleId,
432430
func_names: Vec<FunctionName>,
@@ -459,6 +457,7 @@ impl CompiledModule {
459457
module: Arc::new(info.module),
460458
funcs: info.funcs,
461459
wasm_to_native_trampolines: info.wasm_to_native_trampolines,
460+
#[cfg(feature = "debug-builtins")]
462461
dbg_jit_registration: None,
463462
code_memory,
464463
meta: info.meta,
@@ -471,11 +470,17 @@ impl CompiledModule {
471470
}
472471

473472
fn register_debug_and_profiling(&mut self, profiler: &dyn ProfilingAgent) -> Result<()> {
473+
#[cfg(feature = "debug-builtins")]
474474
if self.meta.native_debug_info_present {
475+
use anyhow::Context;
476+
475477
let text = self.text();
476-
let bytes = create_gdbjit_image(self.mmap().to_vec(), (text.as_ptr(), text.len()))
477-
.context("failed to create jit image for gdb")?;
478-
let reg = GdbJitImageRegistration::register(bytes);
478+
let bytes = crate::debug::create_gdbjit_image(
479+
self.mmap().to_vec(),
480+
(text.as_ptr(), text.len()),
481+
)
482+
.context("failed to create jit image for gdb")?;
483+
let reg = wasmtime_runtime::GdbJitImageRegistration::register(bytes);
479484
self.dbg_jit_registration = Some(reg);
480485
}
481486
profiler.register_module(&self.code_memory, &|addr| {
@@ -669,6 +674,7 @@ impl CompiledModule {
669674
/// what filename and line number a wasm pc comes from.
670675
#[cfg(feature = "addr2line")]
671676
pub fn symbolize_context(&self) -> Result<Option<SymbolizeContext<'_>>> {
677+
use anyhow::Context;
672678
use gimli::EndianSlice;
673679
if !self.meta.has_wasm_debuginfo {
674680
return Ok(None);

crates/jit/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#![deny(missing_docs)]
44

55
mod code_memory;
6+
#[cfg(feature = "debug-builtins")]
67
mod debug;
78
mod demangling;
89
mod instantiate;
910
pub mod profiling;
10-
mod unwind;
1111

1212
pub use crate::code_memory::CodeMemory;
1313
#[cfg(feature = "addr2line")]

crates/jit/src/unwind.rs

-14
This file was deleted.

0 commit comments

Comments
 (0)