Skip to content

Commit 1551750

Browse files
bongjunjCopilot
andauthored
Fix souper harvest (#13225)
* display souper timeouts and failures * harvest not operation correctly Co-authored-by: Copilot <copilot@github.com> * zero-extend icmp result * fix ule --------- Co-authored-by: Copilot <copilot@github.com>
1 parent f48ee88 commit 1551750

2 files changed

Lines changed: 67 additions & 16 deletions

File tree

cranelift/codegen/src/souper_harvest.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ fn harvest_candidate_lhs(
113113
| ir::Opcode::BorImm
114114
| ir::Opcode::Bxor
115115
| ir::Opcode::BxorImm
116+
| ir::Opcode::Bnot
116117
| ir::Opcode::Ishl
117118
| ir::Opcode::IshlImm
118119
| ir::Opcode::Sshr
@@ -334,6 +335,15 @@ fn harvest_candidate_lhs(
334335
.into();
335336
ast::Instruction::Xor { a, b }.into()
336337
}
338+
(ir::Opcode::Bnot, _) => {
339+
let a = arg(allocs, 0);
340+
let b = ast::Constant {
341+
value: -1,
342+
r#type: souper_type_of(&func.dfg, val),
343+
}
344+
.into();
345+
ast::Instruction::Xor { a, b }.into()
346+
}
337347
(ir::Opcode::Ishl, _) => {
338348
let a = arg(allocs, 0);
339349
let b = arg(allocs, 1);
@@ -434,7 +444,7 @@ fn harvest_candidate_lhs(
434444
| (ir::Opcode::IcmpImm, ir::InstructionData::IntCompare { cond, .. }) => {
435445
let a = arg(allocs, 0);
436446
let b = arg(allocs, 1);
437-
match cond {
447+
let cmp = match cond {
438448
ir::condcodes::IntCC::Equal => ast::Instruction::Eq { a, b }.into(),
439449
ir::condcodes::IntCC::NotEqual => ast::Instruction::Ne { a, b }.into(),
440450
ir::condcodes::IntCC::UnsignedLessThan => {
@@ -444,12 +454,22 @@ fn harvest_candidate_lhs(
444454
ast::Instruction::Slt { a, b }.into()
445455
}
446456
ir::condcodes::IntCC::UnsignedLessThanOrEqual => {
447-
ast::Instruction::Sle { a, b }.into()
457+
ast::Instruction::Ule { a, b }.into()
448458
}
449459
ir::condcodes::IntCC::SignedLessThanOrEqual => {
450460
ast::Instruction::Sle { a, b }.into()
451461
}
452462
_ => ast::AssignmentRhs::Var,
463+
};
464+
465+
match cmp {
466+
ast::AssignmentRhs::Var => ast::AssignmentRhs::Var,
467+
cmp => {
468+
let cmp = lhs
469+
.assignment(None, Some(ast::Type { width: 1 }), cmp, vec![])
470+
.into();
471+
ast::Instruction::Zext { a: cmp }.into()
472+
}
453473
}
454474
}
455475
(ir::Opcode::Popcnt, _) => {
@@ -534,15 +554,7 @@ fn souper_type_of(dfg: &ir::DataFlowGraph, val: ir::Value) -> Option<ast::Type>
534554
let ty = dfg.value_type(val);
535555
assert!(ty.is_int());
536556
assert_eq!(ty.lane_count(), 1);
537-
let width = match dfg.value_def(val).inst() {
538-
Some(inst)
539-
if dfg.insts[inst].opcode() == ir::Opcode::IcmpImm
540-
|| dfg.insts[inst].opcode() == ir::Opcode::Icmp =>
541-
{
542-
1
543-
}
544-
_ => ty.bits().try_into().unwrap(),
545-
};
557+
let width = ty.bits().try_into().unwrap();
546558
Some(ast::Type { width })
547559
}
548560

cranelift/run-souper.sh

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ function run_one {
3636
local rhs="$lhs".result
3737

3838
if [[ -f "$rhs" ]]; then
39+
if grep -q "^result " "$rhs"; then
40+
echo "success"
41+
else
42+
echo "warning: existing result file for $lhs has no RHS; counting as failed" >&2
43+
echo "failed"
44+
fi
3945
return
4046
fi
4147

@@ -49,9 +55,15 @@ function run_one {
4955

5056
case "$exit_code" in
5157
"0")
52-
# Success! Copy the RHS to its final destination.
53-
cp $lhs $rhs
54-
cat "$temp" >> "$rhs"
58+
if grep -q "^result " "$temp"; then
59+
# Success! Copy the RHS to its final destination.
60+
cp $lhs $rhs
61+
cat "$temp" >> "$rhs"
62+
echo "success"
63+
else
64+
echo "warning: Souper did not infer an RHS for $lhs; skipping" >&2
65+
echo "failed"
66+
fi
5567
;;
5668

5769
# SIGINT. Exit this whole script.
@@ -61,17 +73,23 @@ function run_one {
6173

6274
# Timeout (regular).
6375
"124")
76+
echo "timed out: $lhs" >&2
77+
echo "timeout"
6478
return
6579
;;
6680

6781
# Timeout (with SIGKILL).
6882
"137")
83+
echo "timed out: $lhs" >&2
84+
echo "timeout"
6985
return
7086
;;
7187

7288
# Something else.
7389
*)
74-
exit 1
90+
echo "warning: Souper failed on $lhs with exit code $exit_code; skipping" >&2
91+
echo "failed"
92+
return
7593
esac
7694

7795
}
@@ -87,6 +105,9 @@ function main {
87105
cd "$lhs_dir"
88106

89107
local i=0;
108+
local succeeded=0;
109+
local timed_out=0;
110+
local failed=0;
90111
for lhs in $(ls -1S $lhs_dir); do
91112
# Ignore '.result' files.
92113
if $(echo "$lhs" | grep -q result); then
@@ -99,10 +120,28 @@ function main {
99120
echo "$i / $lhs_count ($percent%)"
100121
fi
101122

102-
run_one "$souper" "$lhs"
123+
local result=$(run_one "$souper" "$lhs")
124+
case "$result" in
125+
"success")
126+
succeeded=$(( $succeeded + 1 ))
127+
;;
128+
"timeout")
129+
timed_out=$(( $timed_out + 1 ))
130+
;;
131+
"failed")
132+
failed=$(( $failed + 1 ))
133+
;;
134+
*)
135+
echo "warning: unexpected result from $lhs: $result" >&2
136+
failed=$(( $failed + 1 ))
137+
;;
138+
esac
103139
done
104140

105141
echo "Done!"
142+
echo "Succeeded: $succeeded / $lhs_count"
143+
echo "Timed out: $timed_out / $lhs_count"
144+
echo "Failed: $failed / $lhs_count"
106145
}
107146

108147
# Kick everything off!

0 commit comments

Comments
 (0)