OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / profopt.exp
1 # Copyright (C) 2001, 2004, 2005, 2007, 2008, 2010, 2011
2 # Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with GCC; see the file COPYING3.  If not see
16 # <http://www.gnu.org/licenses/>.
17 #
18 # This script was submitted by Janis Johnson <janis187@us.ibm.com>.
19
20 # Test the functionality and optionally, performance improvement, of
21 # programs compiled with profile-directed optimizations.  Compile and
22 # run a test with profile options, compile it with options using the
23 # profile feedback, and then run the test again.  Optionally compile
24 # and run a third time without the profile-directed optimization and
25 # compare timing results of the program with normal optimization and
26 # with the profile-directed optimization. Each test is run using
27 # multiple sets of optimization and/or code generation options in
28 # addition to the profiling and feedback options.
29
30 # If perf_ext is defined and the performance value for the
31 # profile-directed test run is nonzero then the performance check will
32 # be done.
33
34 load_lib dg.exp
35 load_lib gcc-dg.exp
36
37 global PROFOPT_OPTIONS perf_delta
38
39 # The including .exp file must define these.
40 global tool profile_option feedback_option prof_ext
41 if ![info exists tool] {
42     error "Tools is not specified."
43 }
44 if ![info exists prof_ext] {
45     error "No profile data file extensions specified."
46 }
47
48 # The maximum perforance degradation can be defined in the including file.
49 if ![info exists perf_delta] {
50     set perf_delta 4
51 }
52
53 # The default option list can be overridden by
54 # PROFOPT_OPTIONS="{ { list1 } ... { list2 } }"
55
56 if ![info exists PROFOPT_OPTIONS] {
57     set PROFOPT_OPTIONS [list \
58         { -g } \
59         { -O0 } \
60         { -O1 } \
61         { -O2 } \
62         { -O3 } \
63         { -O3 -g } \
64         { -Os } ]
65 }
66
67 set prof_option_list $PROFOPT_OPTIONS
68
69 #
70 # profopt-cleanup -- remove profiling or performance results files.
71 #
72 # TESTCASE is the name of the test
73 # EXT is the extensions of files to remove
74 #
75 proc profopt-cleanup { testcase extlist } {
76     set basename [file tail $testcase]
77     set base [file rootname $basename]
78     foreach ext $extlist {
79         set files [glob -nocomplain $base.$ext]
80         if { $files != "" } {
81             eval "remote_file build delete $files"
82         }
83     }
84 }
85
86 #
87 # profopt-perf-value -- get performance value for a test
88 #
89 # TESTCASE is the name of the test
90 # PERF_EXT is the extension of the performance result file
91 # OPTSTR is the string of compiler options
92 #
93 proc profopt-perf-value { testcase perf_ext optstr } {
94     set basename [file tail $testcase]
95     set base [file rootname $basename]
96     set files [glob -nocomplain $base.$perf_ext]
97     # The file doesn't exist; let the caller decide if that's a problem.
98     if { $files == "" } {
99         return -2 
100     }
101     remote_upload host $base.$perf_ext $base.$perf_ext
102     set fd [open $base.$perf_ext r]
103     gets $fd line
104     set val -2
105     if [regexp "TIME" $line] {
106         if [regexp "TIME -1" $line] {
107             fail "$testcase perf check: no consistent time available, $optstr"
108             set val -1
109         } elseif ![regexp "(\[0-9\]+)" "$line" val] {
110             set val -2
111         }
112     }
113     # Report problems with an existing file.
114     if { $val == -2 } {
115         fail "$testcase perf check: file $base.$perf_ext has wrong format, $optstr"
116     }
117     close $fd
118     profopt-cleanup $testcase $perf_ext
119     return $val
120 }
121
122 #
123 # dg-final-generate -- process code to run after the profile-generate step
124 #
125 # ARGS is the line number of the directive followed by the commands.
126 #
127 proc dg-final-generate { args } {
128     global generate_final_code
129
130     if { [llength $args] > 2 } {
131         error "[lindex $args 0]: too many arguments"
132         return
133     }
134     append generate_final_code "[lindex $args 1]\n"
135 }
136
137 #
138 # dg-final-use -- process code to run after the profile-use step
139 #
140 # ARGS is the line number of the directive followed by the commands.
141 #
142 proc dg-final-use { args } {
143     global use_final_code
144
145     if { [llength $args] > 2 } {
146         error "[lindex $args 0]: too many arguments"
147         return
148     }
149     append use_final_code "[lindex $args 1]\n"
150 }
151
152 #
153 # profopt-final-code -- run final code
154 #
155 # WHICH is "generate" or "use".
156 # FINAL_CODE is the TCL code to run.
157 # TESTCASE is the name of the test, for error messages.
158 #
159 proc profopt-final-code { which final_code name } {
160     # This is copied from dg-test in dg.exp of DejaGnu.
161     regsub -all "\\\\(\[{}\])" $final_code "\\1" final_code
162     proc profopt-final-proc { args } $final_code
163     if [catch "profopt-final-proc $name" errmsg] {
164         perror "$name: error executing dg-final-${which}: $errmsg"
165         unresolved "$name: Error executing dg-final-${which}: $errmsg"
166     }
167 }
168
169 #
170 # profopt-get-options -- process test directives
171 #
172 # SRC is the full pathname of the testcase.
173 #
174 proc profopt-get-options { src } {
175     # dg-options sets a variable called dg-extra-tool-flags.
176     set dg-extra-tool-flags ""
177
178     # dg-require-* sets dg-do-what.
179     upvar dg-do-what dg-do-what 
180
181     # current_compiler_flags reads tool_flags from the same stack frame
182     # as dg-extra-tool-flags
183     set tool_flags ""
184
185     set tmp [dg-get-options $src]
186     foreach op $tmp {
187         set cmd [lindex $op 0]
188         if { ![string compare "dg-options" $cmd] \
189              || ![string compare "dg-skip-if" $cmd] \
190              || ![string compare "dg-final-generate" $cmd] \
191              || ![string compare "dg-final-use" $cmd] \
192              || ![string compare "dg-additional-sources" $cmd] \
193              || [string match "dg-require-*" $cmd]  } {
194             set status [catch "$op" errmsg]
195             if { $status != 0 } {
196                 perror "$src: $errmsg for \"$op\"\n"
197                 unresolved "$src: $errmsg for \"$op\""
198                 return
199             }
200         } else {
201             # Ignore unrecognized dg- commands, but warn about them.
202             warning "profopt.exp does not support $cmd"
203         }
204     }
205
206     # Return flags to use for compiling the primary source file and for
207     # linking.
208     return ${dg-extra-tool-flags}
209 }
210
211 #
212 # c-prof-execute -- compile for profiling and then feedback, then normal
213 #
214 # SRC is the full pathname of the testcase.
215 #
216 proc profopt-execute { src } {
217     global srcdir tmpdir
218     global prof_option_list
219     global tool profile_option feedback_option prof_ext perf_ext perf_delta
220     global generate_final_code use_final_code
221     global verbose
222
223     if ![info exists profile_option] {
224         error "No profile option specified for first compile."
225     }
226     if ![info exists feedback_option] {
227         error "No feedback option specified for second compile."
228     }
229
230     regsub "(?q)$srcdir/" $src "" testcase
231     # If we couldn't rip $srcdir out of `src' then just do the best we can.
232     # The point is to reduce the unnecessary noise in the logs.  Don't strip
233     # out too much because different testcases with the same name can confuse
234     # `test-tool'.
235     if [string match "/*" $testcase] {
236         set testcase "[file tail [file dirname $src]]/[file tail $src]"
237     }
238
239     set executable $tmpdir/[file tail [file rootname $src].x]
240     set basename [file tail $testcase]
241     set base [file rootname $basename]
242
243     set count 0
244     foreach option $prof_option_list {
245         set execname1 "${executable}${count}1"
246         set execname2 "${executable}${count}2"
247         set execname3 "${executable}${count}3"
248         incr count
249
250         remote_file build delete $execname1
251         remote_file build delete $execname2
252         remote_file build delete $execname3
253         verbose "Testing $testcase, $option" 1
254
255         # Remove old profiling and performance data files.
256         foreach ext $prof_ext {
257             remote_file target delete $tmpdir/$base.$ext
258         }
259         if [info exists perf_ext] {
260             profopt-cleanup $testcase $perf_ext
261         }
262
263         # Process test directives.
264
265         set generate_final_code ""
266         set use_final_code ""
267         set dg-do-what [list "run" "" P]
268         set extra_flags [profopt-get-options $src]
269         if { [lindex ${dg-do-what} 1 ] == "N" } {
270             unsupported "$src"
271             verbose "$src not supported on this target, skipping it" 3
272             return
273         }
274
275         set extra_options [dg-additional-files-options "" "$src"]
276
277         # Tree profiling requires TLS runtime support, which may need
278         # additional flags.
279         if { [string first "-fprofile-generate" $profile_option] >= 0 } {
280             set extra_flags [add_options_for_tls $extra_flags]
281         }
282
283         # Compile for profiling.
284
285         set options "$extra_options"
286         lappend options "additional_flags=$option $extra_flags $profile_option"
287         set optstr "$option $profile_option"
288         set comp_output [${tool}_target_compile "$src" "$execname1" executable $options]
289         if ![${tool}_check_compile "$testcase compilation" $optstr $execname1 $comp_output] {
290             unresolved "$testcase execution,   $optstr"
291             unresolved "$testcase compilation, $option $feedback_option"
292             unresolved "$testcase execution,   $option $feedback_option"
293             continue
294         }
295
296         # Run the profiled test.
297
298         set result [${tool}_load $execname1 "" ""]
299         set status [lindex $result 0]
300         set missing_file 0
301         # Make sure the profile data was generated, and fail if not.
302         if { $status == "pass" } {
303             foreach ext $prof_ext {
304                 remote_upload target $tmpdir/$base.$ext
305                 set files [glob -nocomplain $base.$ext]
306                 if { $files == "" } {
307                     set status "fail"
308                     set missing_file 1
309                     fail "$testcase execution: file $base.$ext does not exist, $option $profile_option"
310                 }
311             }
312         }
313         if { $missing_file == 0 } {
314             $status "$testcase execution,   $optstr"
315         }
316
317         # If there is dg-final code to execute for the generate step, do it
318         # even if it failed; it might clean up temporary files.
319         if ![string match $generate_final_code ""] {
320             profopt-final-code "generate" $generate_final_code $testcase
321         }
322
323         remote_file build delete $execname1
324
325         # Quit for this round if it failed
326         if { $status != "pass" } {
327             unresolved "$testcase compilation, $option $feedback_option"
328             unresolved "$testcase execution,   $option $feedback_option"
329             continue
330         }
331
332         # Compile with feedback-directed optimizations.
333
334         set options "$extra_options"
335         lappend options "additional_flags=$option $extra_flags $feedback_option"
336         set optstr "$option $feedback_option"
337         set comp_output [${tool}_target_compile "$src" "$execname2" "executable" $options]
338
339         # Prune warnings we know are unwanted.
340         set comp_output [prune_warnings $comp_output]
341
342         if ![${tool}_check_compile "$testcase compilation" $optstr $execname2 $comp_output] {
343             unresolved "$testcase execution,   $optstr"
344             continue
345         }
346
347         # Run the profile-directed optimized test.
348
349         set result [${tool}_load "$execname2" "" ""]
350         set status [lindex $result 0]
351         $status "$testcase execution,   $optstr"
352
353         # If there is dg-final code to execute for the use step, do it.
354         if ![string match $use_final_code ""] {
355             profopt-final-code "use" $use_final_code $testcase
356         }
357
358         # Remove the profiling data files.
359         foreach ext $prof_ext {
360             remote_file target delete $tmpdir/$base.$ext
361         }
362
363         if { $status != "pass" } {
364             continue
365         }
366
367         # If the test is not expected to produce performance data then
368         # we're done now.
369         if ![info exists perf_ext] {
370             remote_file build delete $execname2
371             continue
372         }
373
374         # Get the performance data from the test built with
375         # profile-directed optimization.  If the file doesn't exist or if
376         # the value is zero, skip the performance comparison.
377         set val2 [profopt-perf-value $testcase $perf_ext $optstr]
378         if { $val2 <= 0 } {
379             remote_file build delete $execname2
380             continue
381         }
382
383         # Compile with normal optimizations.
384
385         set options "$extra_options"
386         lappend options "additional_flags=$option"
387         set optstr "$option"
388         set comp_output [${tool}_target_compile "$src" "$execname3" "executable" $options]
389         if ![${tool}_check_compile "$testcase compilation" $optstr $execname3 $comp_output] {
390             unresolved "$testcase execution,   $optstr"
391             unresolved "$testcase perf check,  $optstr"
392             continue
393         }
394
395         # Run the test with normal optimizations.
396
397         set result [${tool}_load "$execname3" "" ""]
398         set status [lindex $result 0]
399         $status "$testcase execution,   $optstr"
400         if { $status != "pass" } {
401             unresolved "$testcase perf check, $optstr"
402             continue
403         }
404
405         # Get the performance data from the test built with normal
406         # optimization.
407         set val1 [profopt-perf-value $testcase $perf_ext $optstr]
408         if { $val1 < 0 } {
409             if { $val1 == -2 } {
410                 # The data file existed with the profile-directed
411                 # optimization so this one should, too.
412                 fail "$testcase perf check: file $base.$perf_ext does not exist, $optstr"
413             }
414             continue
415         }
416
417         # Compare results of the two runs and fail if the time with the
418         # profile-directed optimization is significantly more than the time
419         # without it.
420         set status "pass"
421         if { $val2 > $val1 } {
422             # Check for a performance degration outside of allowable limits.
423             if { [expr $val2 - $val1] > [expr [expr $val1 * $perf_delta] / 100] } {
424                 set status "fail"
425             }
426         }
427         if { $status == "fail" } {
428             fail "$testcase perf check: orig: $val1  new: $val2, $optstr"
429         } else {
430             $status "$testcase perf check,  $optstr"
431             verbose "$testcase orig: $val1  new: $val2, $optstr" 2
432             remote_file build delete $execname2
433             remote_file build delete $execname3
434         }
435     }
436 }