#!/usr/bin/env bash12# Runs Souper on the LHSes that were harvested by `clif-util3# souper-harvest`.4#5# This script takes two inputs:6#7# 1. The `souper-check` binary, and8# 2. The directory of harvested left-hand sides (aka the `-o $directory` you9# specified to `clif-util souper-harvest`).10#11# For a left-hand side file `foo` that Souper successfully synthesized a12# right-hand side for, this script will write the whole optimization to a13# sibling file named `foo.result`.14#15# The left-hand sides are processed in smallest-to-largest order. This helps16# give you initial results more quickly, but does mean that progress will slow17# down as we encounter larger and larger left-hand sides.18#19# Usage:20#21# run-souper.sh path/to/souper-check path/to/left-hand-sides2223set -e2425# Knobs for configuring how large of right-hand sides Souper should try to26# generate and how much time we give it to synthesize a result. Feel free to27# play with these!28MAX_INSTS=329TIMEOUT=5s3031# Run Souper on one left-hand side.32function run_one {33local souper=$134local lhs=$235local rhs="$lhs".result3637if [[ -f "$rhs" ]]; then38return39fi4041local temp=$(mktemp)42local cmd="taskset --cpu-list 0-3 $souper --infer-rhs -souper-enumerative-synthesis-max-instructions=$MAX_INSTS $lhs > $temp"4344set +e45$(which timeout) --foreground --kill-after=1s $TIMEOUT bash -c "$cmd"46local exit_code="$?"47set -e4849case "$exit_code" in50"0")51# Success! Copy the RHS to its final destination.52cp $lhs $rhs53cat "$temp" >> "$rhs"54;;5556# SIGINT. Exit this whole script.57"130")58exit 159;;6061# Timeout (regular).62"124")63return64;;6566# Timeout (with SIGKILL).67"137")68return69;;7071# Something else.72*)73exit 174esac7576}7778# Run Souper on all the left-hand sides.79function main {80local souper=$181local lhs_dir=$282local lhs_count=$(ls -1 $lhs_dir | grep -v result | wc -l)8384echo "Processing $lhs_count left-hand sides."8586cd "$lhs_dir"8788local i=0;89for lhs in $(ls -1S $lhs_dir); do90# Ignore '.result' files.91if $(echo "$lhs" | grep -q result); then92continue;93fi9495i=$(( $i + 1 ))96if (( $i % 5 == 0 )); then97local percent=$(( $i * 100 / $lhs_count ))98echo "$i / $lhs_count ($percent%)"99fi100101run_one "$souper" "$lhs"102done103104echo "Done!"105}106107# Kick everything off!108main $1 $2109110111