annotate llvm/utils/findmisopt @ 266:00f31e85ec16 default tip

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