Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 32 additions & 37 deletions cranelift/codegen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,13 @@ fn write_instruction(
// Write out the result values, if any.
let mut has_results = false;
for r in func.dfg.inst_results(inst) {
let r = func.dfg.resolve_aliases(*r);
if !has_results {
has_results = true;
write!(w, "{}", r)?;
} else {
write!(w, ", {}", r)?;
}
if let Some(f) = &func.dfg.facts[r] {
if let Some(f) = &func.dfg.facts[*r] {
write!(w, " ! {}", f)?;
}
}
Expand Down Expand Up @@ -389,53 +388,43 @@ fn write_instruction(
pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt::Result {
let pool = &dfg.value_lists;
let jump_tables = &dfg.jump_tables;

// Resolve a value to its alias, if any.
let v = |v: Value| dfg.resolve_aliases(v);

// Resolve a list of values to their aliases and format them.
let vs = |vs: &[_]| {
let vs: Vec<_> = vs.iter().copied().map(v).collect();
DisplayValues(&vs).to_string()
};

use crate::ir::instructions::InstructionData::*;
match dfg.insts[inst] {
AtomicRmw { op, args, .. } => write!(w, " {} {}, {}", op, v(args[0]), v(args[1])),
AtomicCas { args, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), v(args[2])),
LoadNoOffset { flags, arg, .. } => write!(w, "{} {}", flags, v(arg)),
StoreNoOffset { flags, args, .. } => write!(w, "{} {}, {}", flags, v(args[0]), v(args[1])),
Unary { arg, .. } => write!(w, " {}", v(arg)),
AtomicRmw { op, args, .. } => write!(w, " {} {}, {}", op, args[0], args[1]),
AtomicCas { args, .. } => write!(w, " {}, {}, {}", args[0], args[1], args[2]),
LoadNoOffset { flags, arg, .. } => write!(w, "{} {}", flags, arg),
StoreNoOffset { flags, args, .. } => write!(w, "{} {}, {}", flags, args[0], args[1]),
Unary { arg, .. } => write!(w, " {}", arg),
UnaryImm { imm, .. } => write!(w, " {}", imm),
UnaryIeee32 { imm, .. } => write!(w, " {}", imm),
UnaryIeee64 { imm, .. } => write!(w, " {}", imm),
UnaryGlobalValue { global_value, .. } => write!(w, " {}", global_value),
UnaryConst {
constant_handle, ..
} => write!(w, " {}", constant_handle),
Binary { args, .. } => write!(w, " {}, {}", v(args[0]), v(args[1])),
BinaryImm8 { arg, imm, .. } => write!(w, " {}, {}", v(arg), imm),
BinaryImm64 { arg, imm, .. } => write!(w, " {}, {}", v(arg), imm),
Ternary { args, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), v(args[2])),
Binary { args, .. } => write!(w, " {}, {}", args[0], args[1]),
BinaryImm8 { arg, imm, .. } => write!(w, " {}, {}", arg, imm),
BinaryImm64 { arg, imm, .. } => write!(w, " {}, {}", arg, imm),
Ternary { args, .. } => write!(w, " {}, {}, {}", args[0], args[1], args[2]),
MultiAry { ref args, .. } => {
if args.is_empty() {
write!(w, "")
} else {
write!(w, " {}", vs(args.as_slice(pool)))
write!(w, " {}", DisplayValues(args.as_slice(pool)))
}
}
NullAry { .. } => write!(w, " "),
TernaryImm8 { imm, args, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), imm),
TernaryImm8 { imm, args, .. } => write!(w, " {}, {}, {}", args[0], args[1], imm),
Shuffle { imm, args, .. } => {
let data = dfg.immediates.get(imm).expect(
"Expected the shuffle mask to already be inserted into the immediates table",
);
write!(w, " {}, {}, {}", v(args[0]), v(args[1]), data)
write!(w, " {}, {}, {}", args[0], args[1], data)
}
IntCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, v(args[0]), v(args[1])),
IntCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, args[0], args[1]),
IntCompareImm { cond, arg, imm, .. } => write!(w, " {} {}, {}", cond, arg, imm),
IntAddTrap { args, code, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), code),
FloatCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, v(args[0]), v(args[1])),
IntAddTrap { args, code, .. } => write!(w, " {}, {}, {}", args[0], args[1], code),
FloatCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, args[0], args[1]),
Jump { destination, .. } => {
write!(w, " {}", destination.display(pool))
}
Expand All @@ -444,20 +433,26 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
blocks: [block_then, block_else],
..
} => {
write!(w, " {}, {}", v(arg), block_then.display(pool))?;
write!(w, " {}, {}", arg, block_then.display(pool))?;
write!(w, ", {}", block_else.display(pool))
}
BranchTable { arg, table, .. } => {
write!(w, " {}, {}", v(arg), jump_tables[table].display(pool))
write!(w, " {}, {}", arg, jump_tables[table].display(pool))
}
Call {
func_ref, ref args, ..
} => write!(w, " {}({})", func_ref, vs(args.as_slice(pool))),
} => write!(w, " {}({})", func_ref, DisplayValues(args.as_slice(pool))),
CallIndirect {
sig_ref, ref args, ..
} => {
let args = args.as_slice(pool);
write!(w, " {}, {}({})", sig_ref, args[0], vs(&args[1..]))
write!(
w,
" {}, {}({})",
sig_ref,
args[0],
DisplayValues(&args[1..])
)
}
FuncAddr { func_ref, .. } => write!(w, " {}", func_ref),
StackLoad {
Expand All @@ -468,7 +463,7 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
stack_slot,
offset,
..
} => write!(w, " {}, {}{}", v(arg), stack_slot, offset),
} => write!(w, " {}, {}{}", arg, stack_slot, offset),
DynamicStackLoad {
dynamic_stack_slot, ..
} => write!(w, " {}", dynamic_stack_slot),
Expand All @@ -479,15 +474,15 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
} => write!(w, " {}, {}", arg, dynamic_stack_slot),
Load {
flags, arg, offset, ..
} => write!(w, "{} {}{}", flags, v(arg), offset),
} => write!(w, "{} {}{}", flags, arg, offset),
Store {
flags,
args,
offset,
..
} => write!(w, "{} {}, {}{}", flags, v(args[0]), v(args[1]), offset),
} => write!(w, "{} {}, {}{}", flags, args[0], args[1], offset),
Trap { code, .. } => write!(w, " {}", code),
CondTrap { arg, code, .. } => write!(w, " {}, {}", v(arg), code),
CondTrap { arg, code, .. } => write!(w, " {}, {}", arg, code),
}?;

let mut sep = " ; ";
Expand All @@ -502,7 +497,7 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
} => constant_handle.to_string(),
_ => continue,
};
write!(w, "{}{} = {}", sep, v(arg), imm)?;
write!(w, "{}{} = {}", sep, arg, imm)?;
sep = ", ";
}
}
Expand Down Expand Up @@ -608,7 +603,7 @@ mod tests {
}
assert_eq!(
func.to_string(),
"function u0:0() fast {\nblock0(v3: i32):\n v0 -> v3\n v2 -> v0\n v4 = iconst.i32 42\n v5 = iadd v3, v3\n v1 -> v5\n v6 = iconst.i32 23\n v7 = iadd v5, v5\n}\n"
"function u0:0() fast {\nblock0(v3: i32):\n v0 -> v3\n v2 -> v0\n v4 = iconst.i32 42\n v5 = iadd v0, v0\n v1 -> v5\n v6 = iconst.i32 23\n v7 = iadd v1, v1\n}\n"
);
}

Expand Down
40 changes: 20 additions & 20 deletions cranelift/frontend/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,8 +1294,8 @@ block0:
v1 -> v4
v3 = iconst.i64 0
v0 -> v3
v2 = call fn0(v4, v3, v4) ; v4 = 0, v3 = 0, v4 = 0
return v4 ; v4 = 0
v2 = call fn0(v1, v0, v1) ; v1 = 0, v0 = 0, v1 = 0
return v1 ; v1 = 0
}
",
);
Expand Down Expand Up @@ -1347,9 +1347,9 @@ block0:
v1 -> v4
v3 = iconst.i64 0
v0 -> v3
v2 = load.i64 aligned v3 ; v3 = 0
store aligned v2, v4 ; v4 = 0
return v4 ; v4 = 0
v2 = load.i64 aligned v0 ; v0 = 0
store aligned v2, v1 ; v1 = 0
return v1 ; v1 = 0
}
",
);
Expand Down Expand Up @@ -1405,8 +1405,8 @@ block0:
v4 = iconst.i64 0
v0 -> v4
v2 = iconst.i64 8192
v3 = call fn0(v5, v4, v2) ; v5 = 0, v4 = 0, v2 = 8192
return v5 ; v5 = 0
v3 = call fn0(v1, v0, v2) ; v1 = 0, v0 = 0, v2 = 8192
return v1 ; v1 = 0
}
",
);
Expand Down Expand Up @@ -1445,8 +1445,8 @@ block0:
v2 = iconst.i64 0
v0 -> v2
v1 = iconst.i64 0x0101_0101_0101_0101
store aligned v1, v2 ; v1 = 0x0101_0101_0101_0101, v2 = 0
return v2 ; v2 = 0
store aligned v1, v0 ; v1 = 0x0101_0101_0101_0101, v0 = 0
return v0 ; v0 = 0
}
",
);
Expand Down Expand Up @@ -1490,8 +1490,8 @@ block0:
v1 = iconst.i8 1
v2 = iconst.i64 8192
v3 = uextend.i32 v1 ; v1 = 1
v4 = call fn0(v5, v3, v2) ; v5 = 0, v2 = 8192
return v5 ; v5 = 0
v4 = call fn0(v0, v3, v2) ; v0 = 0, v2 = 8192
return v0 ; v0 = 0
}
",
);
Expand Down Expand Up @@ -1555,7 +1555,7 @@ block0:
v1 -> v5
v4 = iconst.i64 0
v0 -> v4
v3 = call fn0(v4, v5, v6) ; v4 = 0, v5 = 0, v6 = 0
v3 = call fn0(v0, v1, v2) ; v0 = 0, v1 = 0, v2 = 0
return v3
}
",
Expand Down Expand Up @@ -1599,8 +1599,8 @@ block0:
v1 -> v6
v5 = iconst.i64 0
v0 -> v5
v2 = load.i8 aligned v5 ; v5 = 0
v3 = load.i8 aligned v6 ; v6 = 0
v2 = load.i8 aligned v0 ; v0 = 0
v3 = load.i8 aligned v1 ; v1 = 0
v4 = icmp ugt v2, v3
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1628,8 +1628,8 @@ block0:
v1 -> v6
v5 = iconst.i64 0
v0 -> v5
v2 = load.i32 aligned v5 ; v5 = 0
v3 = load.i32 aligned v6 ; v6 = 0
v2 = load.i32 aligned v0 ; v0 = 0
v3 = load.i32 aligned v1 ; v1 = 0
v4 = icmp eq v2, v3
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1657,8 +1657,8 @@ block0:
v1 -> v6
v5 = iconst.i64 0
v0 -> v5
v2 = load.i128 v5 ; v5 = 0
v3 = load.i128 v6 ; v6 = 0
v2 = load.i128 v0 ; v0 = 0
v3 = load.i128 v1 ; v1 = 0
v4 = icmp ne v2, v3
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1690,7 +1690,7 @@ block0:
v5 = iconst.i64 0
v0 -> v5
v2 = iconst.i64 3
v3 = call fn0(v5, v6, v2) ; v5 = 0, v6 = 0, v2 = 3
v3 = call fn0(v0, v1, v2) ; v0 = 0, v1 = 0, v2 = 3
v4 = icmp_imm sge v3, 0
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1801,7 +1801,7 @@ block0:
v1 -> v4
v3 = vconst.i8x16 const0
v0 -> v3
return v3, v4, v6 ; v3 = const0, v4 = const0
return v0, v1, v2 ; v0 = const0, v1 = const0
}
",
);
Expand Down
6 changes: 4 additions & 2 deletions tests/disas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ fn run_test(path: &Path) -> Result<()> {
UserFuncName::Testcase(_) => unreachable!(),
});

// And finally, use `cranelift_filetests` to perform the rest of the test.
run_functions(
&test.path,
&test.contents,
Expand Down Expand Up @@ -271,7 +270,7 @@ pub enum TestKind {
}

/// Assert that `wat` contains the test expectations necessary for `funcs`.
pub fn run_functions(
fn run_functions(
path: &Path,
wat: &str,
isa: &dyn TargetIsa,
Expand All @@ -294,9 +293,12 @@ pub fn run_functions(
let mut ctx = cranelift_codegen::Context::for_function(func.clone());
ctx.optimize(isa, &mut Default::default())
.map_err(|e| codegen_error_to_anyhow_error(&ctx.func, e))?;
ctx.func.dfg.resolve_all_aliases();
writeln!(&mut actual, "{}", ctx.func.display()).unwrap();
}
TestKind::Clif => {
let mut func = func.clone();
func.dfg.resolve_all_aliases();
writeln!(&mut actual, "{}", func.display()).unwrap();
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/disas/dead-code.wat
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
;; stack_limit = gv2
;;
;; block0(v0: i64, v1: i64, v2: i32):
;; v3 -> v2
;; @0023 jump block2
;;
;; block2:
Expand Down
10 changes: 0 additions & 10 deletions tests/disas/duplicate-loads-dynamic-memory.wat
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
;; stack_limit = gv2
;;
;; block0(v0: i64, v1: i64, v2: i32):
;; v21 -> v0
;; v22 -> v0
;; v23 -> v0
;; v24 -> v0
;; @0057 v6 = load.i64 notrap aligned v0+88
;; @0057 v8 = load.i64 notrap aligned checked v0+80
;; @0057 v5 = uextend.i64 v2
Expand All @@ -44,7 +40,6 @@
;; @0057 v9 = iadd v8, v5
;; @0057 v11 = select_spectre_guard v7, v10, v9 ; v10 = 0
;; @0057 v12 = load.i32 little heap v11
;; v3 -> v12
;; @005f jump block1
;;
;; block1:
Expand All @@ -61,10 +56,6 @@
;; stack_limit = gv2
;;
;; block0(v0: i64, v1: i64, v2: i32):
;; v25 -> v0
;; v26 -> v0
;; v27 -> v0
;; v28 -> v0
;; @0064 v6 = load.i64 notrap aligned v0+88
;; @0064 v8 = load.i64 notrap aligned checked v0+80
;; @0064 v5 = uextend.i64 v2
Expand All @@ -75,7 +66,6 @@
;; @0064 v11 = iadd v9, v10 ; v10 = 1234
;; @0064 v13 = select_spectre_guard v7, v12, v11 ; v12 = 0
;; @0064 v14 = load.i32 little heap v13
;; v3 -> v14
;; @006e jump block1
;;
;; block1:
Expand Down
6 changes: 0 additions & 6 deletions tests/disas/duplicate-loads-static-memory.wat
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
;; stack_limit = gv2
;;
;; block0(v0: i64, v1: i64, v2: i32):
;; v13 -> v0
;; v14 -> v0
;; @0057 v6 = load.i64 notrap aligned readonly checked v0+80
;; @0057 v5 = uextend.i64 v2
;; @0057 v7 = iadd v6, v5
;; @0057 v8 = load.i32 little heap v7
;; v3 -> v8
;; @005f jump block1
;;
;; block1:
Expand All @@ -48,15 +45,12 @@
;; stack_limit = gv2
;;
;; block0(v0: i64, v1: i64, v2: i32):
;; v17 -> v0
;; v18 -> v0
;; @0064 v6 = load.i64 notrap aligned readonly checked v0+80
;; @0064 v5 = uextend.i64 v2
;; @0064 v7 = iadd v6, v5
;; @0064 v8 = iconst.i64 1234
;; @0064 v9 = iadd v7, v8 ; v8 = 1234
;; @0064 v10 = load.i32 little heap v9
;; v3 -> v10
;; @006e jump block1
;;
;; block1:
Expand Down
Loading