llvm-project
177 строк · 5.0 Кб
1#!/bin/bash
2#
3# findmisopt
4#
5# This is a quick and dirty hack to potentially find a misoptimization
6# problem. Mostly its to work around problems in bugpoint that prevent
7# it from finding a problem unless the set of failing optimizations are
8# known and given to it on the command line.
9#
10# Given a bitcode file that produces correct output (or return code),
11# this script will run through all the optimizations passes that gccas
12# uses (in the same order) and will narrow down which optimizations
13# cause the program either generate different output or return a
14# different result code. When the passes have been narrowed down,
15# bugpoint is invoked to further refine the problem to its origin. If a
16# release version of bugpoint is available it will be used, otherwise
17# debug.
18#
19# Usage:
20# findmisopt bcfile outdir progargs [match]
21#
22# Where:
23# bcfile
24# is the bitcode file input (the unoptimized working case)
25# outdir
26# is a directory into which intermediate results are placed
27# progargs
28# is a single argument containing all the arguments the program needs
29# proginput
30# is a file name from which stdin should be directed
31# match
32# if specified to any value causes the result code of the program to
33# be used to determine success/fail. If not specified success/fail is
34# determined by diffing the program's output with the non-optimized
35# output.
36#
37if [ "$#" -lt 3 ] ; then
38echo "usage: findmisopt bcfile outdir progargs [match]"
39exit 1
40fi
41
42dir="${0%%/utils/findmisopt}"
43if [ -x "$dir/Release/bin/bugpoint" ] ; then
44bugpoint="$dir/Release/bin/bugpoint"
45elif [ -x "$dir/Debug/bin/bugpoint" ] ; then
46bugpoint="$dir/Debug/bin/bugpoint"
47else
48echo "findmisopt: bugpoint not found"
49exit 1
50fi
51
52bcfile="$1"
53outdir="$2"
54args="$3"
55input="$4"
56if [ ! -f "$input" ] ; then
57input="/dev/null"
58fi
59match="$5"
60name=`basename $bcfile .bc`
61ll="$outdir/${name}.ll"
62s="$outdir/${name}.s"
63prog="$outdir/${name}"
64out="$outdir/${name}.out"
65optbc="$outdir/${name}.opt.bc"
66optll="$outdir/${name}.opt.ll"
67opts="$outdir/${name}.opt.s"
68optprog="$outdir/${name}.opt"
69optout="$outdir/${name}.opt.out"
70ldflags="-lstdc++ -lm -ldl -lc"
71
72echo "Test Name: $name"
73echo "Unoptimized program: $prog"
74echo " Optimized program: $optprog"
75
76# Define the list of optimizations to run. This comprises the same set of
77# optimizations that opt -O3 runs, in the same order.
78opt_switches=`llvm-as < /dev/null -o - | opt -O3 -disable-output -debug-pass=Arguments 2>&1 | sed 's/Pass Arguments: //'`
79all_switches="$opt_switches"
80echo "Passes : $all_switches"
81
82# Create output directory if it doesn't exist
83if [ -f "$outdir" ] ; then
84echo "$outdir is not a directory"
85exit 1
86fi
87
88if [ ! -d "$outdir" ] ; then
89mkdir "$outdir" || exit 1
90fi
91
92# Generate the disassembly
93llvm-dis "$bcfile" -o "$ll" -f || exit 1
94
95# Generate the non-optimized program and its output
96llc "$bcfile" -o "$s" -f || exit 1
97gcc "$s" -o "$prog" $ldflags || exit 1
98"$prog" $args > "$out" 2>&1 <$input
99ex1=$?
100
101# Current set of switches is empty
102function tryit {
103switches_to_use="$1"
104opt $switches_to_use "$bcfile" -o "$optbc" -f || exit
105llvm-dis "$optbc" -o "$optll" -f || exit
106llc "$optbc" -o "$opts" -f || exit
107gcc "$opts" -o "$optprog" $ldflags || exit
108"$optprog" $args > "$optout" 2>&1 <"$input"
109ex2=$?
110
111if [ -n "$match" ] ; then
112if [ "$ex1" -ne "$ex2" ] ; then
113echo "Return code not the same with these switches:"
114echo $switches
115echo "Unoptimized returned: $ex1"
116echo "Optimized returned: $ex2"
117return 0
118fi
119else
120diff "$out" "$optout" > /dev/null
121if [ $? -ne 0 ] ; then
122echo "Diff fails with these switches:"
123echo $switches
124echo "Differences:"
125diff "$out" "$optout" | head
126return 0;
127fi
128fi
129return 1
130}
131
132echo "Trying to find optimization that breaks program:"
133for sw in $all_switches ; do
134echo -n " $sw"
135switches="$switches $sw"
136if tryit "$switches" ; then
137break;
138fi
139done
140
141# Terminate the previous output with a newline
142echo ""
143
144# Determine if we're done because none of the optimizations broke the program
145if [ "$switches" == " $all_switches" ] ; then
146echo "The program did not miscompile"
147exit 0
148fi
149
150final=""
151while [ ! -z "$switches" ] ; do
152trimmed=`echo "$switches" | sed -e 's/^ *\(-[^ ]*\).*/\1/'`
153switches=`echo "$switches" | sed -e 's/^ *-[^ ]* *//'`
154echo "Trimmed $trimmed from left"
155tryit "$final $switches"
156if [ "$?" -eq "0" ] ; then
157echo "Still Failing .. continuing ..."
158continue
159else
160echo "Found required early pass: $trimmed"
161final="$final $trimmed"
162continue
163fi
164echo "Next Loop"
165done
166
167if [ "$final" == " $all_switches" ] ; then
168echo "findmisopt: All optimizations pass. Perhaps this isn't a misopt?"
169exit 0
170fi
171echo "Smallest Optimization list=$final"
172
173bpcmd="$bugpoint -run-llc -disable-loop-extraction --output "$out" --input /dev/null $bcfile $final --args $args"
174
175echo "Running: $bpcmd"
176$bpcmd
177echo "findmisopt finished."
178