OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [testsuite/] [lib/] [profopt.exp] - Blame information for rev 514

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.