OSDN Git Service

2013-07-08 Tobias Burnus <burnus@net-b.de>
[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 #
68 # profopt-cleanup -- remove profiling or performance results files.
69 #
70 # TESTCASE is the name of the test
71 # EXT is the extensions of files to remove
72 #
73 proc profopt-cleanup { testcase extlist } {
74     set basename [file tail $testcase]
75     set base [file rootname $basename]
76     foreach ext $extlist {
77         set files [glob -nocomplain $base.$ext]
78         if { $files != "" } {
79             eval "remote_file build delete $files"
80         }
81     }
82 }
83
84 #
85 # profopt-perf-value -- get performance value for a test
86 #
87 # TESTCASE is the name of the test
88 # PERF_EXT is the extension of the performance result file
89 # OPTSTR is the string of compiler options
90 #
91 proc profopt-perf-value { testcase perf_ext optstr } {
92     set basename [file tail $testcase]
93     set base [file rootname $basename]
94     set files [glob -nocomplain $base.$perf_ext]
95     # The file doesn't exist; let the caller decide if that's a problem.
96     if { $files == "" } {
97         return -2 
98     }
99     remote_upload host $base.$perf_ext $base.$perf_ext
100     set fd [open $base.$perf_ext r]
101     gets $fd line
102     set val -2
103     if [regexp "TIME" $line] {
104         if [regexp "TIME -1" $line] {
105             fail "$testcase perf check: no consistent time available, $optstr"
106             set val -1
107         } elseif ![regexp "(\[0-9\]+)" "$line" val] {
108             set val -2
109         }
110     }
111     # Report problems with an existing file.
112     if { $val == -2 } {
113         fail "$testcase perf check: file $base.$perf_ext has wrong format, $optstr"
114     }
115     close $fd
116     profopt-cleanup $testcase $perf_ext
117     return $val
118 }
119
120 #
121 # dg-final-generate -- process code to run after the profile-generate step
122 #
123 # ARGS is the line number of the directive followed by the commands.
124 #
125 proc dg-final-generate { args } {
126     global generate_final_code
127
128     if { [llength $args] > 2 } {
129         error "[lindex $args 0]: too many arguments"
130         return
131     }
132     append generate_final_code "[lindex $args 1]\n"
133 }
134
135 #
136 # dg-final-use -- process code to run after the profile-use step
137 #
138 # ARGS is the line number of the directive followed by the commands.
139 #
140 proc dg-final-use { args } {
141     global use_final_code
142
143     if { [llength $args] > 2 } {
144         error "[lindex $args 0]: too many arguments"
145         return
146     }
147     append use_final_code "[lindex $args 1]\n"
148 }
149
150 #
151 # profopt-final-code -- run final code
152 #
153 # WHICH is "generate" or "use".
154 # FINAL_CODE is the TCL code to run.
155 # TESTCASE is the name of the test, for error messages.
156 #
157 proc profopt-final-code { which final_code name } {
158     # This is copied from dg-test in dg.exp of DejaGnu.
159     regsub -all "\\\\(\[{}\])" $final_code "\\1" final_code
160     proc profopt-final-proc { args } $final_code
161     if [catch "profopt-final-proc $name" errmsg] {
162         perror "$name: error executing dg-final-${which}: $errmsg"
163         unresolved "$name: Error executing dg-final-${which}: $errmsg"
164     }
165 }
166
167 #
168 # profopt-get-options -- process test directives
169 #
170 # SRC is the full pathname of the testcase.
171 #
172 proc profopt-get-options { src } {
173     # dg-options sets a variable called dg-extra-tool-flags.
174     set dg-extra-tool-flags ""
175
176     # dg-require-* sets dg-do-what.
177     upvar dg-do-what dg-do-what 
178
179     # current_compiler_flags reads tool_flags from the same stack frame
180     # as dg-extra-tool-flags
181     set tool_flags ""
182
183     set tmp [dg-get-options $src]
184     foreach op $tmp {
185         set cmd [lindex $op 0]
186         if { ![string compare "dg-options" $cmd] \
187              || ![string compare "dg-skip-if" $cmd] \
188              || ![string compare "dg-final-generate" $cmd] \
189              || ![string compare "dg-final-use" $cmd] \
190              || ![string compare "dg-additional-sources" $cmd] \
191              || [string match "dg-require-*" $cmd]  } {
192             set status [catch "$op" errmsg]
193             if { $status != 0 } {
194                 perror "$src: $errmsg for \"$op\"\n"
195                 unresolved "$src: $errmsg for \"$op\""
196                 return
197             }
198         } else {
199             # Ignore unrecognized dg- commands, but warn about them.
200             warning "profopt.exp does not support $cmd"
201         }
202     }
203
204     # Return flags to use for compiling the primary source file and for
205     # linking.
206     return ${dg-extra-tool-flags}
207 }
208
209 #
210 # c-prof-execute -- compile for profiling and then feedback, then normal
211 #
212 # SRC is the full pathname of the testcase.
213 #
214 proc profopt-execute { src } {
215     global srcdir tmpdir
216     global PROFOPT_OPTIONS
217     global tool profile_option feedback_option prof_ext perf_ext perf_delta
218     global generate_final_code use_final_code
219     global verbose
220     global testname_with_flags
221
222     if ![info exists profile_option] {
223         error "No profile option specified for first compile."
224     }
225     if ![info exists feedback_option] {
226         error "No feedback option specified for second compile."
227     }
228
229     # Use the default option list or one defined for a set of tests.
230     if ![info exists PROFOPT_OPTIONS] {
231          error "PROFOPT_OPTIONS is not defined"
232     }
233     set prof_option_list $PROFOPT_OPTIONS
234
235     regsub "(?q)$srcdir/" $src "" testcase
236     # If we couldn't rip $srcdir out of `src' then just do the best we can.
237     # The point is to reduce the unnecessary noise in the logs.  Don't strip
238     # out too much because different testcases with the same name can confuse
239     # `test-tool'.
240     if [string match "/*" $testcase] {
241         set testcase "[file tail [file dirname $src]]/[file tail $src]"
242     }
243
244     # Several procedures access the name of the test with torture flags,
245     # normally defined in dg-test.  Profile optimization tests don't
246     # use dg-test, so define it here to make it accessible via
247     # testname-for-summary.
248     set testname_with_flags $testcase
249
250     set executable $tmpdir/[file tail [file rootname $src].x]
251     set basename [file tail $testcase]
252     set base [file rootname $basename]
253
254     set count 0
255     foreach option $prof_option_list {
256         set execname1 "${executable}${count}1"
257         set execname2 "${executable}${count}2"
258         set execname3 "${executable}${count}3"
259         incr count
260
261         remote_file build delete $execname1
262         remote_file build delete $execname2
263         remote_file build delete $execname3
264         verbose "Testing $testcase, $option" 1
265
266         # Remove old profiling and performance data files.
267         foreach ext $prof_ext {
268             remote_file target delete $tmpdir/$base.$ext
269         }
270         if [info exists perf_ext] {
271             profopt-cleanup $testcase $perf_ext
272         }
273
274         # Process test directives.
275
276         set generate_final_code ""
277         set use_final_code ""
278         set dg-do-what [list "run" "" P]
279         set extra_flags [profopt-get-options $src]
280         if { [lindex ${dg-do-what} 1 ] == "N" } {
281             unsupported "$src"
282             unset testname_with_flags
283             verbose "$src not supported on this target, skipping it" 3
284             return
285         }
286
287         set extra_options [dg-additional-files-options "" "$src"]
288
289         # Tree profiling requires TLS runtime support, which may need
290         # additional flags.
291         if { [string first "-fprofile-generate" $profile_option] >= 0 } {
292             set extra_flags [add_options_for_tls $extra_flags]
293         }
294
295         # Compile for profiling.
296
297         set options "$extra_options"
298         lappend options "additional_flags=$option $extra_flags $profile_option"
299         set optstr "$option $profile_option"
300         set comp_output [${tool}_target_compile "$src" "$execname1" executable $options]
301         if ![${tool}_check_compile "$testcase compilation" $optstr $execname1 $comp_output] {
302             unresolved "$testcase execution,   $optstr"
303             unresolved "$testcase compilation, $option $feedback_option"
304             unresolved "$testcase execution,   $option $feedback_option"
305             continue
306         }
307
308         # Run the profiled test.
309
310         set result [${tool}_load $execname1 "" ""]
311         set status [lindex $result 0]
312         set missing_file 0
313         # Make sure the profile data was generated, and fail if not.
314         if { $status == "pass" } {
315             foreach ext $prof_ext {
316                 remote_upload target $tmpdir/$base.$ext
317                 set files [glob -nocomplain $base.$ext]
318                 if { $files == "" } {
319                     set status "fail"
320                     set missing_file 1
321                     fail "$testcase execution: file $base.$ext does not exist, $option $profile_option"
322                 }
323             }
324         }
325         if { $missing_file == 0 } {
326             $status "$testcase execution,   $optstr"
327         }
328
329         # If there is dg-final code to execute for the generate step, do it
330         # even if it failed; it might clean up temporary files.
331         if ![string match $generate_final_code ""] {
332             profopt-final-code "generate" $generate_final_code $testcase
333         }
334
335         remote_file build delete $execname1
336
337         # Quit for this round if it failed
338         if { $status != "pass" } {
339             unresolved "$testcase compilation, $option $feedback_option"
340             unresolved "$testcase execution,   $option $feedback_option"
341             continue
342         }
343
344         # Compile with feedback-directed optimizations.
345
346         set options "$extra_options"
347         lappend options "additional_flags=$option $extra_flags $feedback_option"
348         set optstr "$option $feedback_option"
349         set comp_output [${tool}_target_compile "$src" "$execname2" "executable" $options]
350
351         # Prune warnings we know are unwanted.
352         set comp_output [prune_warnings $comp_output]
353
354         if ![${tool}_check_compile "$testcase compilation" $optstr $execname2 $comp_output] {
355             unresolved "$testcase execution,   $optstr"
356             continue
357         }
358
359         # Run the profile-directed optimized test.
360
361         set result [${tool}_load "$execname2" "" ""]
362         set status [lindex $result 0]
363         $status "$testcase execution,   $optstr"
364
365         # If there is dg-final code to execute for the use step, do it.
366         if ![string match $use_final_code ""] {
367             profopt-final-code "use" $use_final_code $testcase
368         }
369
370         # Remove the profiling data files.
371         foreach ext $prof_ext {
372             remote_file target delete $tmpdir/$base.$ext
373         }
374
375         if { $status != "pass" } {
376             continue
377         }
378
379         # If the test is not expected to produce performance data then
380         # we're done now.
381         if ![info exists perf_ext] {
382             remote_file build delete $execname2
383             continue
384         }
385
386         # Get the performance data from the test built with
387         # profile-directed optimization.  If the file doesn't exist or if
388         # the value is zero, skip the performance comparison.
389         set val2 [profopt-perf-value $testcase $perf_ext $optstr]
390         if { $val2 <= 0 } {
391             remote_file build delete $execname2
392             continue
393         }
394
395         # Compile with normal optimizations.
396
397         set options "$extra_options"
398         lappend options "additional_flags=$option"
399         set optstr "$option"
400         set comp_output [${tool}_target_compile "$src" "$execname3" "executable" $options]
401         if ![${tool}_check_compile "$testcase compilation" $optstr $execname3 $comp_output] {
402             unresolved "$testcase execution,   $optstr"
403             unresolved "$testcase perf check,  $optstr"
404             continue
405         }
406
407         # Run the test with normal optimizations.
408
409         set result [${tool}_load "$execname3" "" ""]
410         set status [lindex $result 0]
411         $status "$testcase execution,   $optstr"
412         if { $status != "pass" } {
413             unresolved "$testcase perf check, $optstr"
414             continue
415         }
416
417         # Get the performance data from the test built with normal
418         # optimization.
419         set val1 [profopt-perf-value $testcase $perf_ext $optstr]
420         if { $val1 < 0 } {
421             if { $val1 == -2 } {
422                 # The data file existed with the profile-directed
423                 # optimization so this one should, too.
424                 fail "$testcase perf check: file $base.$perf_ext does not exist, $optstr"
425             }
426             continue
427         }
428
429         # Compare results of the two runs and fail if the time with the
430         # profile-directed optimization is significantly more than the time
431         # without it.
432         set status "pass"
433         if { $val2 > $val1 } {
434             # Check for a performance degration outside of allowable limits.
435             if { [expr $val2 - $val1] > [expr [expr $val1 * $perf_delta] / 100] } {
436                 set status "fail"
437             }
438         }
439         if { $status == "fail" } {
440             fail "$testcase perf check: orig: $val1  new: $val2, $optstr"
441         } else {
442             $status "$testcase perf check,  $optstr"
443             verbose "$testcase orig: $val1  new: $val2, $optstr" 2
444             remote_file build delete $execname2
445             remote_file build delete $execname3
446         }
447     }
448     unset testname_with_flags
449 }