Skip to content

Commit bb2aa73

Browse files
authored
Add a bunch of trace logging for CLIF and Wasm-to-CLIF translation (#10191)
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 5ca7892 commit bb2aa73

File tree

7 files changed

+164
-212
lines changed

7 files changed

+164
-212
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(

crates/cranelift/src/gc/disabled.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! `GcCompiler` implementation when GC support is disabled.
22
33
use super::GcCompiler;
4-
use crate::func_environ::FuncEnvironment;
4+
use crate::func_environ::{Extension, FuncEnvironment};
55
use cranelift_codegen::ir;
66
use cranelift_frontend::FunctionBuilder;
77
use wasmtime_environ::{wasm_unsupported, TypeIndex, WasmRefType, WasmResult};
@@ -41,26 +41,7 @@ pub fn translate_struct_get(
4141
_struct_type_index: TypeIndex,
4242
_field_index: u32,
4343
_struct_ref: ir::Value,
44-
) -> WasmResult<ir::Value> {
45-
disabled()
46-
}
47-
48-
pub fn translate_struct_get_s(
49-
_func_env: &mut FuncEnvironment<'_>,
50-
_builder: &mut FunctionBuilder<'_>,
51-
_struct_type_index: TypeIndex,
52-
_field_index: u32,
53-
_struct_ref: ir::Value,
54-
) -> WasmResult<ir::Value> {
55-
disabled()
56-
}
57-
58-
pub fn translate_struct_get_u(
59-
_func_env: &mut FuncEnvironment<'_>,
60-
_builder: &mut FunctionBuilder<'_>,
61-
_struct_type_index: TypeIndex,
62-
_field_index: u32,
63-
_struct_ref: ir::Value,
44+
_extension: Option<Extension>,
6445
) -> WasmResult<ir::Value> {
6546
disabled()
6647
}
@@ -130,26 +111,7 @@ pub fn translate_array_get(
130111
_array_type_index: TypeIndex,
131112
_array: ir::Value,
132113
_index: ir::Value,
133-
) -> WasmResult<ir::Value> {
134-
disabled()
135-
}
136-
137-
pub fn translate_array_get_s(
138-
_func_env: &mut FuncEnvironment<'_>,
139-
_builder: &mut FunctionBuilder,
140-
_array_type_index: TypeIndex,
141-
_array: ir::Value,
142-
_index: ir::Value,
143-
) -> WasmResult<ir::Value> {
144-
disabled()
145-
}
146-
147-
pub fn translate_array_get_u(
148-
_func_env: &mut FuncEnvironment<'_>,
149-
_builder: &mut FunctionBuilder,
150-
_array_type_index: TypeIndex,
151-
_array: ir::Value,
152-
_index: ir::Value,
114+
_extension: Option<Extension>,
153115
) -> WasmResult<ir::Value> {
154116
disabled()
155117
}

0 commit comments

Comments
 (0)