Annotation of gforth/prof-inline.fs, revision 1.1
1.1 ! anton 1: \ get some data on potential (partial) inlining
! 2:
! 3: \ Copyright (C) 2004 Free Software Foundation, Inc.
! 4:
! 5: \ This file is part of Gforth.
! 6:
! 7: \ Gforth is free software; you can redistribute it and/or
! 8: \ modify it under the terms of the GNU General Public License
! 9: \ as published by the Free Software Foundation; either version 2
! 10: \ of the License, or (at your option) any later version.
! 11:
! 12: \ This program is distributed in the hope that it will be useful,
! 13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
! 14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 15: \ GNU General Public License for more details.
! 16:
! 17: \ You should have received a copy of the GNU General Public License
! 18: \ along with this program; if not, write to the Free Software
! 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
! 20:
! 21:
! 22: \ relies on some Gforth internals
! 23:
! 24: \ !! assumption: each file is included only once; otherwise you get
! 25: \ the counts for just one of the instances of the file. This can be
! 26: \ fixed by making sure that every source position occurs only once as
! 27: \ a profile point.
! 28:
! 29: true constant count-calls? \ do some profiling of colon definitions etc.
! 30:
! 31: \ for true COUNT-CALLS?:
! 32:
! 33: \ What data do I need for evaluating the effectiveness of (partial) inlining?
! 34:
! 35: \ static and dynamic counts of everything:
! 36:
! 37: \ original BB length (histogram and average)
! 38: \ BB length with partial inlining (histogram and average)
! 39: \ since we cannot partially inline library calls, we use a parameter
! 40: \ that represents the amount of partial inlining we can expect there.
! 41: \ number of tail calls (original and after partial inlining)
! 42: \ number of calls (original and after partial inlining)
! 43: \ reason for BB end: call, return, execute, branch
! 44:
! 45: \ how many static calls are there to a word? How many of the dynamic
! 46: \ calls call just a single word?
! 47:
! 48: struct
! 49: cell% field profile-next
! 50: cell% 2* field profile-count
! 51: cell% 2* field profile-sourcepos
! 52: cell% field profile-char \ character position in line
! 53: count-calls? [if]
! 54: cell% field profile-colondef? \ is this a colon definition start
! 55: cell% field profile-calls \ static calls to the colon def
! 56: cell% field profile-straight-line \ may contain calls, but no other CF
! 57: cell% field profile-calls-from \ static calls in the colon def
! 58: [endif]
! 59: end-struct profile% \ profile point
! 60:
! 61: variable profile-points \ linked list of profile%
! 62: 0 profile-points !
! 63: variable next-profile-point-p \ the address where the next pp will be stored
! 64: profile-points next-profile-point-p !
! 65: count-calls? [if]
! 66: variable last-colondef-profile \ pointer to the pp of last colon definition
! 67: [endif]
! 68:
! 69: : new-profile-point ( -- addr )
! 70: profile% %alloc >r
! 71: 0. r@ profile-count 2!
! 72: current-sourcepos r@ profile-sourcepos 2!
! 73: >in @ r@ profile-char !
! 74: [ count-calls? ] [if]
! 75: r@ profile-colondef? off
! 76: 0 r@ profile-calls !
! 77: r@ profile-straight-line on
! 78: 0 r@ profile-calls-from !
! 79: [endif]
! 80: 0 r@ profile-next !
! 81: r@ next-profile-point-p @ !
! 82: r@ profile-next next-profile-point-p !
! 83: r> ;
! 84:
! 85: : print-profile ( -- )
! 86: profile-points @ begin
! 87: dup while
! 88: dup >r
! 89: r@ profile-sourcepos 2@ .sourcepos ." :"
! 90: r@ profile-char @ 0 .r ." : "
! 91: r@ profile-count 2@ 0 d.r cr
! 92: r> profile-next @
! 93: repeat
! 94: drop ;
! 95:
! 96: : print-profile-coldef ( -- )
! 97: profile-points @ begin
! 98: dup while
! 99: dup >r
! 100: r@ profile-colondef? @ if
! 101: r@ profile-sourcepos 2@ .sourcepos ." :"
! 102: r@ profile-char @ 3 .r ." : "
! 103: r@ profile-count 2@ 10 d.r
! 104: r@ profile-straight-line @ space 2 .r
! 105: r@ profile-calls @ 4 .r
! 106: cr
! 107: endif
! 108: r> profile-next @
! 109: repeat
! 110: drop ;
! 111:
! 112: : dinc ( profilep -- )
! 113: \ increment double pointed to by d-addr
! 114: profile-count dup 2@ 1. d+ rot 2! ;
! 115:
! 116: : profile-this ( -- )
! 117: new-profile-point POSTPONE literal POSTPONE dinc ;
! 118:
! 119: \ Various words trigger PROFILE-THIS. In order to avoid getting
! 120: \ several calls to PROFILE-THIS from a compiling word (like ?EXIT), we
! 121: \ just wait until the next word is parsed by the text interpreter (in
! 122: \ compile state) and call PROFILE-THIS only once then. The whole
! 123: \ BEFORE-WORD hooking etc. is there for this.
! 124:
! 125: \ The reason that we do this is because we use the source position for
! 126: \ the profiling information, and there's only one source position for
! 127: \ ?EXIT. If we used the threaded code position instead, we would see
! 128: \ that ?EXIT compiles to several threaded-code words, and could use
! 129: \ different profile points for them. However, usually dealing with
! 130: \ the source is more practical.
! 131:
! 132: \ Another benefit is that we can ask for profiling anywhere in a
! 133: \ control-flow word (even before it compiles its own stuff).
! 134:
! 135: \ Potential problem: Consider "COMPILING ] [" where COMPILING compiles
! 136: \ a whole colon definition (and triggers our profiler), but during the
! 137: \ compilation of the colon definition there is no parsing. Afterwards
! 138: \ you get interpret state at first (no profiling, either), but after
! 139: \ the "]" you get parsing in compile state, and PROFILE-THIS gets
! 140: \ called (and compiles code that is never executed). It would be
! 141: \ better if we had a way of knowing whether we are in a colon def or
! 142: \ not (and used that knowledge instead of STATE).
! 143:
! 144: Defer before-word-profile ( -- )
! 145: ' noop IS before-word-profile
! 146:
! 147: : before-word1 ( -- )
! 148: before-word-profile defers before-word ;
! 149:
! 150: ' before-word1 IS before-word
! 151:
! 152: : profile-this-compiling ( -- )
! 153: state @ if
! 154: profile-this
! 155: ['] noop IS before-word-profile
! 156: endif ;
! 157:
! 158: : cock-profiler ( -- )
! 159: \ as in cock the gun - pull the trigger
! 160: ['] profile-this-compiling IS before-word-profile
! 161: [ count-calls? ] [if] \ we are at a non-colondef profile point
! 162: last-colondef-profile @ profile-straight-line off
! 163: [endif]
! 164: ;
! 165:
! 166: : hook-profiling-into ( "name" -- )
! 167: \ make (deferred word) "name" call cock-profiler, too
! 168: ' >body >r :noname
! 169: POSTPONE cock-profiler
! 170: r@ @ compile, \ old hook behaviour
! 171: POSTPONE ;
! 172: r> ! ; \ change hook behaviour
! 173:
! 174: : note-execute ( -- )
! 175: \ end of BB due to execute
! 176: ;
! 177:
! 178: : note-call ( addr -- )
! 179: \ addr is the body address of a called colon def or does handler
! 180: dup 3 cells + @ ['] dinc >body = if
! 181: 1 over cell+ @ profile-calls +!
! 182: endif
! 183: drop ;
! 184:
! 185: : prof-compile, ( xt -- )
! 186: dup >does-code if
! 187: dup >does-code note-call
! 188: then
! 189: dup >code-address CASE
! 190: docol: OF dup >body note-call ENDOF
! 191: dodefer: OF note-execute ENDOF
! 192: dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
! 193: \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
! 194: \ code words and ;code-defined words (code words could be optimized):
! 195: dup in-dictionary? IF drop POSTPONE literal ['] execute peephole-compile, EXIT THEN
! 196: ENDCASE
! 197: DEFERS compile, ;
! 198:
! 199: \ hook-profiling-into then-like
! 200: \ \ hook-profiling-into if-like \ subsumed by other-control-flow
! 201: \ \ hook-profiling-into ahead-like \ subsumed by other-control-flow
! 202: \ hook-profiling-into other-control-flow
! 203: \ hook-profiling-into begin-like
! 204: \ hook-profiling-into again-like
! 205: \ hook-profiling-into until-like
! 206:
! 207: : :-hook-profile ( -- )
! 208: defers :-hook
! 209: next-profile-point-p @
! 210: profile-this
! 211: @ dup last-colondef-profile !
! 212: profile-colondef? on ;
! 213:
! 214: ' :-hook-profile IS :-hook
! 215: ' prof-compile, IS compile,
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>