Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix souper harvest (#13225)

Bongjun Jang15517502a1a7
diff --git a/cranelift/codegen/src/souper_harvest.rs b/cranelift/codegen/src/souper_harvest.rs
index 02958ed56a..f31abd1f3b 100644
--- a/cranelift/codegen/src/souper_harvest.rs
+++ b/cranelift/codegen/src/souper_harvest.rs
@@ -113,6 +113,7 @@ fn harvest_candidate_lhs(
| ir::Opcode::BorImm
| ir::Opcode::Bxor
| ir::Opcode::BxorImm
+ | ir::Opcode::Bnot
| ir::Opcode::Ishl
| ir::Opcode::IshlImm
| ir::Opcode::Sshr
@@ -334,6 +335,15 @@ fn harvest_candidate_lhs(
.into();
ast::Instruction::Xor { a, b }.into()
}
+ (ir::Opcode::Bnot, _) => {
+ let a = arg(allocs, 0);
+ let b = ast::Constant {
+ value: -1,
+ r#type: souper_type_of(&func.dfg, val),
+ }
+ .into();
+ ast::Instruction::Xor { a, b }.into()
+ }
(ir::Opcode::Ishl, _) => {
let a = arg(allocs, 0);
let b = arg(allocs, 1);
@@ -434,7 +444,7 @@ fn harvest_candidate_lhs(
| (ir::Opcode::IcmpImm, ir::InstructionData::IntCompare { cond, .. }) => {
let a = arg(allocs, 0);
let b = arg(allocs, 1);
- match cond {
+ let cmp = match cond {
ir::condcodes::IntCC::Equal => ast::Instruction::Eq { a, b }.into(),
ir::condcodes::IntCC::NotEqual => ast::Instruction::Ne { a, b }.into(),
ir::condcodes::IntCC::UnsignedLessThan => {
@@ -444,12 +454,22 @@ fn harvest_candidate_lhs(
ast::Instruction::Slt { a, b }.into()
}
ir::condcodes::IntCC::UnsignedLessThanOrEqual => {
- ast::Instruction::Sle { a, b }.into()
+ ast::Instruction::Ule { a, b }.into()
}
ir::condcodes::IntCC::SignedLessThanOrEqual => {
ast::Instruction::Sle { a, b }.into()
}
_ => ast::AssignmentRhs::Var,
+ };
+
+ match cmp {
+ ast::AssignmentRhs::Var => ast::AssignmentRhs::Var,
+ cmp => {
+ let cmp = lhs
+ .assignment(None, Some(ast::Type { width: 1 }), cmp, vec![])
+ .into();
+ ast::Instruction::Zext { a: cmp }.into()
+ }
}
}
(ir::Opcode::Popcnt, _) => {
@@ -534,15 +554,7 @@ fn souper_type_of(dfg: &ir::DataFlowGraph, val: ir::Value) -> Option<ast::Type>
let ty = dfg.value_type(val);
assert!(ty.is_int());
assert_eq!(ty.lane_count(), 1);
- let width = match dfg.value_def(val).inst() {
- Some(inst)
- if dfg.insts[inst].opcode() == ir::Opcode::IcmpImm
- || dfg.insts[inst].opcode() == ir::Opcode::Icmp =>
- {
- 1
- }
- _ => ty.bits().try_into().unwrap(),
- };
+ let width = ty.bits().try_into().unwrap();
Some(ast::Type { width })
}
diff --git a/cranelift/run-souper.sh b/cranelift/run-souper.sh
index 8643379f0f..8ce6b8f317 100755
--- a/cranelift/run-souper.sh
+++ b/cranelift/run-souper.sh
@@ -36,6 +36,12 @@ function run_one {
local rhs="$lhs".result
if [[ -f "$rhs" ]]; then
+ if grep -q "^result " "$rhs"; then
+ echo "success"
+ else
+ echo "warning: existing result file for $lhs has no RHS; counting as failed" >&2
+ echo "failed"
+ fi
return
fi
@@ -49,9 +55,15 @@ function run_one {
case "$exit_code" in
"0")
- # Success! Copy the RHS to its final destination.
- cp $lhs $rhs
- cat "$temp" >> "$rhs"
+ if grep -q "^result " "$temp"; then
+ # Success! Copy the RHS to its final destination.
+ cp $lhs $rhs
+ cat "$temp" >> "$rhs"
+ echo "success"
+ else
+ echo "warning: Souper did not infer an RHS for $lhs; skipping" >&2
+ echo "failed"
+ fi
;;
# SIGINT. Exit this whole script.
@@ -61,17 +73,23 @@ function run_one {
# Timeout (regular).
"124")
+ echo "timed out: $lhs" >&2
+ echo "timeout"
return
;;
# Timeout (with SIGKILL).
"137")
+ echo "timed out: $lhs" >&2
+ echo "timeout"
return
;;
# Something else.
*)
- exit 1
+ echo "warning: Souper failed on $lhs with exit code $exit_code; skipping" >&2
+ echo "failed"
+ return
esac
}
@@ -87,6 +105,9 @@ function main {
cd "$lhs_dir"
local i=0;
+ local succeeded=0;
+ local timed_out=0;
+ local failed=0;
for lhs in $(ls -1S $lhs_dir); do
# Ignore '.result' files.
if $(echo "$lhs" | grep -q result); then
@@ -99,10 +120,28 @@ function main {
echo "$i / $lhs_count ($percent%)"
fi
- run_one "$souper" "$lhs"
+ local result=$(run_one "$souper" "$lhs")
+ case "$result" in
+ "success")
+ succeeded=$(( $succeeded + 1 ))
+ ;;
+ "timeout")
+ timed_out=$(( $timed_out + 1 ))
+ ;;
+ "failed")
+ failed=$(( $failed + 1 ))
+ ;;
+ *)
+ echo "warning: unexpected result from $lhs: $result" >&2
+ failed=$(( $failed + 1 ))
+ ;;
+ esac
done
echo "Done!"
+ echo "Succeeded: $succeeded / $lhs_count"
+ echo "Timed out: $timed_out / $lhs_count"
+ echo "Failed: $failed / $lhs_count"
}
# Kick everything off!