Skip to content

Commit 6905fa5

Browse files
committed
Add a bunch of trace logging for CLIF and Wasm-to-CLIF translation
This adds trace logging for: * `InstructionBuilder` methods * Switching `FunctionBuilder`s between blocks * A ton of GC-related Wasm-to-CLIF translation bits The result is that it is wayyyyyyyy easier to tell what CLIF is generated for what purpose when staring at trace logs, particularly for Wasm GC things where a single Wasm instruction might become many blocks of CLIF. At the same time, this consolidates some `translate_{array,struct}_get{,_s,_u}` helpers so that there is less code duplication (purely mechanical; should not change any actual translations or instructions we emit) just so that there were fewer places to add trace logs to.
1 parent a727985 commit 6905fa5

File tree

6 files changed

+161
-171
lines changed

6 files changed

+161
-171
lines changed

cranelift/codegen/meta/src/gen_inst.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,12 +1178,17 @@ fn gen_inst_builder(inst: &Instruction, format: &InstructionFormat, fmt: &mut Fo
11781178
// Call to the format constructor,
11791179
let fcall = format!("self.{}({})", format.name, args.join(", "));
11801180

1181+
fmtln!(fmt, "let (inst, dfg) = {};", fcall);
1182+
fmtln!(
1183+
fmt,
1184+
"crate::trace!(\"inserted {inst:?}: {}\", dfg.display_inst(inst));"
1185+
);
1186+
11811187
if inst.value_results.is_empty() {
1182-
fmtln!(fmt, "{}.0", fcall);
1188+
fmtln!(fmt, "inst");
11831189
return;
11841190
}
11851191

1186-
fmtln!(fmt, "let (inst, dfg) = {};", fcall);
11871192
if inst.value_results.len() == 1 {
11881193
fmt.line("dfg.first_result(inst)");
11891194
} else {

cranelift/frontend/src/frontend.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ impl<'a> FunctionBuilder<'a> {
357357
/// successor), the block will be declared filled and it will not be possible to append
358358
/// instructions to it.
359359
pub fn switch_to_block(&mut self, block: Block) {
360+
log::trace!("switch to {block:?}");
361+
360362
// First we check that the previous block has been filled.
361363
debug_assert!(
362364
self.position.is_none()

crates/cranelift/src/func_environ.rs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ use wasmtime_environ::{
2828
};
2929
use wasmtime_environ::{FUNCREF_INIT_BIT, FUNCREF_MASK};
3030

31+
#[derive(Debug)]
32+
pub(crate) enum Extension {
33+
Sign,
34+
Zero,
35+
}
36+
3137
/// A struct with an `Option<ir::FuncRef>` member for every builtin
3238
/// function, to de-duplicate constructing/getting its function.
3339
pub(crate) struct BuiltinFunctions {
@@ -1955,28 +1961,16 @@ impl FuncEnvironment<'_> {
19551961
struct_type_index: TypeIndex,
19561962
field_index: u32,
19571963
struct_ref: ir::Value,
1964+
extension: Option<Extension>,
19581965
) -> WasmResult<ir::Value> {
1959-
gc::translate_struct_get(self, builder, struct_type_index, field_index, struct_ref)
1960-
}
1961-
1962-
pub fn translate_struct_get_s(
1963-
&mut self,
1964-
builder: &mut FunctionBuilder,
1965-
struct_type_index: TypeIndex,
1966-
field_index: u32,
1967-
struct_ref: ir::Value,
1968-
) -> WasmResult<ir::Value> {
1969-
gc::translate_struct_get_s(self, builder, struct_type_index, field_index, struct_ref)
1970-
}
1971-
1972-
pub fn translate_struct_get_u(
1973-
&mut self,
1974-
builder: &mut FunctionBuilder,
1975-
struct_type_index: TypeIndex,
1976-
field_index: u32,
1977-
struct_ref: ir::Value,
1978-
) -> WasmResult<ir::Value> {
1979-
gc::translate_struct_get_u(self, builder, struct_type_index, field_index, struct_ref)
1966+
gc::translate_struct_get(
1967+
self,
1968+
builder,
1969+
struct_type_index,
1970+
field_index,
1971+
struct_ref,
1972+
extension,
1973+
)
19801974
}
19811975

19821976
pub fn translate_struct_set(
@@ -2181,28 +2175,9 @@ impl FuncEnvironment<'_> {
21812175
array_type_index: TypeIndex,
21822176
array: ir::Value,
21832177
index: ir::Value,
2178+
extension: Option<Extension>,
21842179
) -> WasmResult<ir::Value> {
2185-
gc::translate_array_get(self, builder, array_type_index, array, index)
2186-
}
2187-
2188-
pub fn translate_array_get_s(
2189-
&mut self,
2190-
builder: &mut FunctionBuilder,
2191-
array_type_index: TypeIndex,
2192-
array: ir::Value,
2193-
index: ir::Value,
2194-
) -> WasmResult<ir::Value> {
2195-
gc::translate_array_get_s(self, builder, array_type_index, array, index)
2196-
}
2197-
2198-
pub fn translate_array_get_u(
2199-
&mut self,
2200-
builder: &mut FunctionBuilder,
2201-
array_type_index: TypeIndex,
2202-
array: ir::Value,
2203-
index: ir::Value,
2204-
) -> WasmResult<ir::Value> {
2205-
gc::translate_array_get_u(self, builder, array_type_index, array, index)
2180+
gc::translate_array_get(self, builder, array_type_index, array, index, extension)
22062181
}
22072182

22082183
pub fn translate_array_set(

0 commit comments

Comments
 (0)