Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / testsuite / lib / profopt.exp
bloba6e4d890318cbd58b5665afd2e986d2826208622
1 # Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
17 # This script was submitted by Janis Johnson <janis187@us.ibm.com>.
19 # Test the functionality and optionally, performance improvement, of
20 # programs compiled with profile-directed optimizations. Compile and
21 # run a test with profile options, compile it with options using the
22 # profile feedback, and then run the test again. Optionally compile
23 # and run a third time without the profile-directed optimization and
24 # compare timing results of the program with normal optimization and
25 # with the profile-directed optimization. Each test is run using
26 # multiple sets of optimization and/or code generation options in
27 # addition to the profiling and feedback options.
29 # If perf_ext is defined and the performance value for the
30 # profile-directed test run is nonzero then the performance check will
31 # be done.
33 load_lib dg.exp
34 load_lib gcc-dg.exp
36 global PROFOPT_OPTIONS perf_delta
38 # The including .exp file must define these.
39 global tool profile_option feedback_option prof_ext
40 if ![info exists tool] {
41 error "Tools is not specified."
43 if ![info exists prof_ext] {
44 error "No profile data file extensions specified."
47 # The maximum perforance degradation can be defined in the including file.
48 if ![info exists perf_delta] {
49 set perf_delta 4
52 # The default option list can be overridden by
53 # PROFOPT_OPTIONS="{ { list1 } ... { list2 } }"
55 if ![info exists PROFOPT_OPTIONS] {
56 set PROFOPT_OPTIONS [list \
57 { -g } \
58 { -O0 } \
59 { -O1 } \
60 { -O2 } \
61 { -O3 } \
62 { -O3 -g } \
63 { -Os } ]
67 # profopt-cleanup -- remove profiling or performance results files.
69 # TESTCASE is the name of the test
70 # EXT is the extensions of files to remove
72 proc profopt-cleanup { testcase extlist } {
73 set basename [file tail $testcase]
74 set base [file rootname $basename]
75 foreach ext $extlist {
76 set files [glob -nocomplain $base.$ext]
77 if { $files != "" } {
78 eval "remote_file build delete $files"
84 # profopt-perf-value -- get performance value for a test
86 # TESTCASE is the name of the test
87 # PERF_EXT is the extension of the performance result file
88 # OPTSTR is the string of compiler options
90 proc profopt-perf-value { testcase perf_ext optstr } {
91 set basename [file tail $testcase]
92 set base [file rootname $basename]
93 set files [glob -nocomplain $base.$perf_ext]
94 # The file doesn't exist; let the caller decide if that's a problem.
95 if { $files == "" } {
96 return -2
98 remote_upload host $base.$perf_ext $base.$perf_ext
99 set fd [open $base.$perf_ext r]
100 gets $fd line
101 set val -2
102 if [regexp "TIME" $line] {
103 if [regexp "TIME -1" $line] {
104 fail "$testcase perf check: no consistent time available, $optstr"
105 set val -1
106 } elseif ![regexp "(\[0-9\]+)" "$line" val] {
107 set val -2
110 # Report problems with an existing file.
111 if { $val == -2 } {
112 fail "$testcase perf check: file $base.$perf_ext has wrong format, $optstr"
114 close $fd
115 profopt-cleanup $testcase $perf_ext
116 return $val
120 # dg-final-generate -- process code to run after the profile-generate step
122 # ARGS is the line number of the directive followed by the commands.
124 proc dg-final-generate { args } {
125 global generate_final_code
127 if { [llength $args] > 2 } {
128 error "[lindex $args 0]: too many arguments"
129 return
131 append generate_final_code "[lindex $args 1]\n"
135 # dg-final-use -- process code to run after the profile-use step
137 # ARGS is the line number of the directive followed by the commands.
139 proc dg-final-use { args } {
140 global use_final_code
142 if { [llength $args] > 2 } {
143 error "[lindex $args 0]: too many arguments"
144 return
146 append use_final_code "[lindex $args 1]\n"
150 # profopt-final-code -- run final code
152 # WHICH is "generate" or "use".
153 # FINAL_CODE is the TCL code to run.
154 # TESTCASE is the name of the test, for error messages.
156 proc profopt-final-code { which final_code name } {
157 # This is copied from dg-test in dg.exp of DejaGnu.
158 regsub -all "\\\\(\[{}\])" $final_code "\\1" final_code
159 proc profopt-final-proc { args } $final_code
160 if [catch "profopt-final-proc $name" errmsg] {
161 perror "$name: error executing dg-final-${which}: $errmsg"
162 unresolved "$name: Error executing dg-final-${which}: $errmsg"
167 # profopt-get-options -- process test directives
169 # SRC is the full pathname of the testcase.
171 proc profopt-get-options { src } {
172 # dg-options sets a variable called dg-extra-tool-flags.
173 set dg-extra-tool-flags ""
175 # dg-require-* sets dg-do-what.
176 upvar dg-do-what dg-do-what
178 # current_compiler_flags reads tool_flags from the same stack frame
179 # as dg-extra-tool-flags
180 set tool_flags ""
182 set tmp [dg-get-options $src]
183 foreach op $tmp {
184 set cmd [lindex $op 0]
185 if { ![string compare "dg-options" $cmd] \
186 || ![string compare "dg-skip-if" $cmd] \
187 || ![string compare "dg-final-generate" $cmd] \
188 || ![string compare "dg-final-use" $cmd] \
189 || ![string compare "dg-additional-sources" $cmd] \
190 || [string match "dg-require-*" $cmd] } {
191 set status [catch "$op" errmsg]
192 if { $status != 0 } {
193 perror "$src: $errmsg for \"$op\"\n"
194 unresolved "$src: $errmsg for \"$op\""
195 return
197 } else {
198 # Ignore unrecognized dg- commands, but warn about them.
199 warning "profopt.exp does not support $cmd"
203 # Return flags to use for compiling the primary source file and for
204 # linking.
205 return ${dg-extra-tool-flags}
209 # c-prof-execute -- compile for profiling and then feedback, then normal
211 # SRC is the full pathname of the testcase.
213 proc profopt-execute { src } {
214 global srcdir tmpdir
215 global PROFOPT_OPTIONS
216 global tool profile_option feedback_option prof_ext perf_ext perf_delta
217 global generate_final_code use_final_code
218 global verbose
219 global testname_with_flags
221 if ![info exists profile_option] {
222 error "No profile option specified for first compile."
224 if ![info exists feedback_option] {
225 error "No feedback option specified for second compile."
228 # Use the default option list or one defined for a set of tests.
229 if ![info exists PROFOPT_OPTIONS] {
230 error "PROFOPT_OPTIONS is not defined"
232 set prof_option_list $PROFOPT_OPTIONS
234 regsub "(?q)$srcdir/" $src "" testcase
235 # If we couldn't rip $srcdir out of `src' then just do the best we can.
236 # The point is to reduce the unnecessary noise in the logs. Don't strip
237 # out too much because different testcases with the same name can confuse
238 # `test-tool'.
239 if [string match "/*" $testcase] {
240 set testcase "[file tail [file dirname $src]]/[file tail $src]"
243 # Several procedures access the name of the test with torture flags,
244 # normally defined in dg-test. Profile optimization tests don't
245 # use dg-test, so define it here to make it accessible via
246 # testname-for-summary.
247 set testname_with_flags $testcase
249 set executable $tmpdir/[file tail [file rootname $src].x]
250 set basename [file tail $testcase]
251 set base [file rootname $basename]
252 set dir [file dirname $src]
253 # multiple file test base
254 set mbase [file rootname $basename]
255 regsub "_0" $mbase "" mbase
256 regsub "/" $mbase "" mbase
257 set src_list $src
258 set i 1
259 set done 0
260 while { !$done } {
261 set names [glob -nocomplain -types f -- "${dir}/${mbase}_${i}.*"]
262 if { [llength ${names}] > 1 } {
263 warning "profopt-execute: more than one file matched ${dir}/${mbase}_${i}.*"
265 if { [llength ${names}] == 1 } {
266 lappend src_list [lindex ${names} 0]
267 incr i
268 } else {
269 set num_srcs ${i}
270 set done 1
274 set count 0
275 foreach option $prof_option_list {
276 set execname1 "${executable}${count}1"
277 set execname2 "${executable}${count}2"
278 set execname3 "${executable}${count}3"
279 incr count
281 remote_file build delete $execname1
282 remote_file build delete $execname2
283 remote_file build delete $execname3
284 verbose "Testing $testcase, $option" 1
286 # Remove old profiling and performance data files.
287 foreach ext $prof_ext {
288 remote_file target delete $tmpdir/$base.$ext
290 if [info exists perf_ext] {
291 profopt-cleanup $testcase $perf_ext
294 # Process test directives.
296 set generate_final_code ""
297 set use_final_code ""
298 set dg-do-what [list "run" "" P]
299 set extra_flags [profopt-get-options $src]
300 if { [lindex ${dg-do-what} 1 ] == "N" } {
301 unsupported "$testcase"
302 unset testname_with_flags
303 verbose "$src not supported on this target, skipping it" 3
304 return
307 set extra_options [dg-additional-files-options "" "$src"]
309 # Tree profiling requires TLS runtime support, which may need
310 # additional flags.
311 if { [string first "-fprofile-generate" $profile_option] >= 0 } {
312 set extra_flags [add_options_for_tls $extra_flags]
315 # Compile for profiling.
317 set options "$extra_options"
318 lappend options "additional_flags=$option $extra_flags $profile_option"
319 set optstr "$option $profile_option"
320 set comp_output [${tool}_target_compile "$src_list" "$execname1" executable $options]
321 if ![${tool}_check_compile "$testcase compilation" $optstr $execname1 $comp_output] {
322 unresolved "$testcase execution, $optstr"
323 unresolved "$testcase compilation, $option $feedback_option"
324 unresolved "$testcase execution, $option $feedback_option"
325 continue
328 # Run the profiled test.
330 set result [${tool}_load $execname1 "" ""]
331 set status [lindex $result 0]
332 set missing_file 0
333 # Make sure the profile data was generated, and fail if not.
334 if { $status == "pass" } {
335 set i 0
336 if { $num_srcs > 1 } {
337 foreach s $src_list {
338 foreach ext $prof_ext {
339 remote_upload target $tmpdir/${mbase}_${i}.$ext
340 set files [glob -nocomplain ${mbase}_${i}.$ext]
341 if { $files == "" } {
342 set status "fail"
343 set missing_file 1
344 fail "$testcase execution: file ${mbase}_${i}.$ext does not exist, $option $profile_option"
347 incr i
349 } else {
350 foreach ext $prof_ext {
351 remote_upload target $tmpdir/$base.$ext
352 set files [glob -nocomplain $base.$ext]
353 if { $files == "" } {
354 set status "fail"
355 set missing_file 1
356 fail "$testcase execution: file $base.$ext does not exist, $option $profile_option"
362 if { $missing_file == 0 } {
363 $status "$testcase execution, $optstr"
366 # If there is dg-final code to execute for the generate step, do it
367 # even if it failed; it might clean up temporary files.
368 if ![string match $generate_final_code ""] {
369 profopt-final-code "generate" $generate_final_code $testcase
372 remote_file build delete $execname1
374 # Quit for this round if it failed
375 if { $status != "pass" } {
376 unresolved "$testcase compilation, $option $feedback_option"
377 unresolved "$testcase execution, $option $feedback_option"
378 continue
381 # Compile with feedback-directed optimizations.
383 set options "$extra_options"
384 lappend options "additional_flags=$option $extra_flags $feedback_option"
385 set optstr "$option $feedback_option"
386 set comp_output [${tool}_target_compile "$src_list" "$execname2" "executable" $options]
388 # Prune warnings we know are unwanted.
389 set comp_output [prune_warnings $comp_output]
391 if ![${tool}_check_compile "$testcase compilation" $optstr $execname2 $comp_output] {
392 unresolved "$testcase execution, $optstr"
393 continue
396 # Run the profile-directed optimized test.
398 set result [${tool}_load "$execname2" "" ""]
399 set status [lindex $result 0]
400 $status "$testcase execution, $optstr"
402 # If there is dg-final code to execute for the use step, do it.
403 if ![string match $use_final_code ""] {
404 profopt-final-code "use" $use_final_code $testcase
407 # Remove the profiling data files.
408 if { $num_srcs > 1 } {
409 set i 0
410 foreach s $src_list {
411 foreach ext $prof_ext {
412 remote_file target delete $tmpdir/${mbase}_${i}.$ext
414 incr i
416 } else {
417 foreach ext $prof_ext {
418 remote_file target delete $tmpdir/$base.$ext
422 if { $status != "pass" } {
423 continue
426 # If the test is not expected to produce performance data then
427 # we're done now.
428 if ![info exists perf_ext] {
429 remote_file build delete $execname2
430 continue
433 # Get the performance data from the test built with
434 # profile-directed optimization. If the file doesn't exist or if
435 # the value is zero, skip the performance comparison.
436 set val2 [profopt-perf-value $testcase $perf_ext $optstr]
437 if { $val2 <= 0 } {
438 remote_file build delete $execname2
439 continue
442 # Compile with normal optimizations.
444 set options "$extra_options"
445 lappend options "additional_flags=$option"
446 set optstr "$option"
447 set comp_output [${tool}_target_compile "$src_list" "$execname3" "executable" $options]
448 if ![${tool}_check_compile "$testcase compilation" $optstr $execname3 $comp_output] {
449 unresolved "$testcase execution, $optstr"
450 unresolved "$testcase perf check, $optstr"
451 continue
454 # Run the test with normal optimizations.
456 set result [${tool}_load "$execname3" "" ""]
457 set status [lindex $result 0]
458 $status "$testcase execution, $optstr"
459 if { $status != "pass" } {
460 unresolved "$testcase perf check, $optstr"
461 continue
464 # Get the performance data from the test built with normal
465 # optimization.
466 set val1 [profopt-perf-value $testcase $perf_ext $optstr]
467 if { $val1 < 0 } {
468 if { $val1 == -2 } {
469 # The data file existed with the profile-directed
470 # optimization so this one should, too.
471 fail "$testcase perf check: file $base.$perf_ext does not exist, $optstr"
473 continue
476 # Compare results of the two runs and fail if the time with the
477 # profile-directed optimization is significantly more than the time
478 # without it.
479 set status "pass"
480 if { $val2 > $val1 } {
481 # Check for a performance degration outside of allowable limits.
482 if { [expr $val2 - $val1] > [expr [expr $val1 * $perf_delta] / 100] } {
483 set status "fail"
486 if { $status == "fail" } {
487 fail "$testcase perf check: orig: $val1 new: $val2, $optstr"
488 } else {
489 $status "$testcase perf check, $optstr"
490 verbose "$testcase orig: $val1 new: $val2, $optstr" 2
491 remote_file build delete $execname2
492 remote_file build delete $execname3
495 unset testname_with_flags