File:  [gforth] / gforth / prof-inline.fs
Revision 1.7: download - view: text, annotated - select for diffs
Mon Sep 13 07:32:37 2004 UTC (16 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
more prof-inline.fs work
improved CASE...ENDCASE documentation

    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: \ how much does inlining called-once words help?
   49: \ how much does inlining words without control flow help?
   50: \ how much does partial inlining help?
   51: \ what's the overlap?
   52: \ optimizing return-to-returns (tail calls), return-to-calls, call-to-calls
   53: 
   54: struct
   55:     cell% field list-next
   56: end-struct list%
   57: 
   58: list%
   59:     cell% 2* field profile-count \ how often this profile point is performed
   60:     cell% 2* field profile-sourcepos
   61:     cell% field profile-char \ character position in line
   62:     cell% field profile-bblen \ number of primitives in BB
   63:     cell% field profile-bblenpi \ bblen after partial inlining
   64:     cell% field profile-callee-postlude \ 0 or (for calls) callee postlude len
   65:     cell% field profile-tailof \ 0 or (for tail bbs) pointer to coldef bb
   66:     cell% field profile-colondef? \ is this a colon definition start
   67:     cell% field profile-calls \ static calls to the colon def (calls%)
   68:     cell% field profile-straight-line \ may contain calls, but no other CF
   69:     cell% field profile-calls-from \ static calls in the colon def
   70:     cell% field profile-exits \ number of exits in this colon def
   71:     cell% 2* field profile-execs \ number of EXECUTEs etc. of this colon def
   72:     cell% field profile-prelude \ first BB-len of colon def (incl. callee)
   73:     cell% field profile-postlude \ last BB-len of colon def (incl. callee)
   74: end-struct profile% \ profile point 
   75: 
   76: list%
   77:     cell% field calls-call \ ptr to profile point of bb containing the call
   78: end-struct calls%
   79: 
   80: variable profile-points \ linked list of profile%
   81: 0 profile-points !
   82: variable next-profile-point-p \ the address where the next pp will be stored
   83: profile-points next-profile-point-p !
   84: variable last-colondef-profile \ pointer to the pp of last colon definition
   85: variable current-profile-point
   86: variable library-calls 0 library-calls ! \ list of calls to library colon defs
   87: variable in-compile,? in-compile,? off
   88: variable all-bbs 0 all-bbs ! \ list of all basic blocks
   89: 
   90: \ list stuff
   91: 
   92: : map-list ( ... list xt -- ... )
   93:     { xt } begin { list }
   94: 	list while
   95: 	    list xt execute
   96: 	    list list-next @
   97:     repeat ;
   98: 
   99: : drop-1+ drop 1+ ;
  100: 
  101: : list-length ( list -- u )
  102:     0 swap ['] drop-1+ map-list ;
  103: 
  104: : insert-list ( listp listpp -- )
  105:     \ insert list node listp into list pointed to by listpp in front
  106:     tuck @ over list-next !
  107:     swap ! ;
  108: 
  109: : insert-list-end ( listp listppp -- )
  110:     \ insert list node listp into list, with listppp indicating the
  111:     \ position to insert at, and indicating the position behind the
  112:     \ new element afterwards.
  113:     2dup @ insert-list
  114:     swap list-next swap ! ;
  115: 
  116: \ calls
  117: 
  118: : new-call ( profile-point -- call )
  119:     calls% %alloc tuck calls-call ! ;
  120: 
  121: \ profile-point stuff   
  122: 
  123: : new-profile-point ( -- addr )
  124:     profile% %alloc >r
  125:     0. r@ profile-count 2!
  126:     current-sourcepos r@ profile-sourcepos 2!
  127:     >in @ r@ profile-char !
  128:     0 r@ profile-callee-postlude !
  129:     0 r@ profile-tailof !
  130:     r@ profile-colondef? off
  131:     0 r@ profile-bblen !
  132:     -100000000 r@ profile-bblenpi !
  133:     current-profile-point @ profile-bblenpi @ -100000000 = if
  134: 	current-profile-point @ dup profile-bblen @ swap profile-bblenpi !
  135:     endif
  136:     0 r@ profile-calls !
  137:     r@ profile-straight-line on
  138:     0 r@ profile-calls-from !
  139:     0 r@ profile-exits !
  140:     0. r@ profile-execs 2!
  141:     0 r@ profile-prelude !
  142:     0 r@ profile-postlude !
  143:     r@ next-profile-point-p insert-list-end
  144:     r@ current-profile-point !
  145:     r@ new-call all-bbs insert-list
  146:     r> ;
  147: 
  148: : print-profile ( -- )
  149:     profile-points @ begin
  150: 	dup while
  151: 	    dup >r
  152: 	    r@ profile-sourcepos 2@ .sourcepos ." :"
  153: 	    r@ profile-char @ 0 .r ." : "
  154: 	    r@ profile-count 2@ 0 d.r cr
  155: 	    r> list-next @
  156:     repeat
  157:     drop ;
  158: 
  159: : print-profile-coldef ( -- )
  160:     profile-points @ begin
  161: 	dup while
  162: 	    dup >r
  163: 	    r@ profile-colondef? @ if
  164: 		r@ profile-sourcepos 2@ .sourcepos ." :"
  165: 		r@ profile-char @ 3 .r ." : "
  166: 		r@ profile-count 2@ 10 d.r
  167: 		r@ profile-straight-line @ space 2 .r
  168: 		r@ profile-calls @ list-length 4 .r
  169: 		cr
  170: 	    endif
  171: 	    r> list-next @
  172:     repeat
  173:     drop ;
  174: 
  175: : 1= ( u -- f )
  176:     1 = ;
  177: 
  178: : 2= ( u -- f )
  179:     2 = ;
  180: 
  181: : 3= ( u -- f )
  182:     3 = ;
  183: 
  184: : 1u> ( u -- f )
  185:     1 u> ;
  186: 
  187: : call-count+ ( ud1 callp -- ud2 )
  188:     calls-call @ profile-count 2@ d+ ;
  189: 
  190: : count-dyncalls ( calls -- ud )
  191:     0. rot ['] call-count+ map-list ;
  192: 
  193: : add-calls ( statistics1 xt-test profpp -- statistics2 xt-test )
  194:     \ add statistics for callee profpp up, if the number of static
  195:     \ calls to profpp satisfies xt-test ( u -- f ); see below for what
  196:     \ statistics are computed.
  197:     { xt-test p }
  198:     p profile-colondef? @ if
  199: 	p profile-calls @ { calls }
  200: 	calls list-length { stat }
  201: 	stat xt-test execute if
  202: 	    { d: ud-dyn-callee d: ud-dyn-caller u-stat u-exec-callees u-callees }
  203: 	    ud-dyn-callee p profile-count 2@ 2dup { d: de } d+
  204: 	    ud-dyn-caller calls count-dyncalls 2dup { d: dr } d+
  205: 	    u-stat stat +
  206: 	    u-exec-callees de dr d<> -
  207: 	    u-callees 1+
  208: 	endif
  209:     endif
  210:     xt-test ;
  211: 
  212: : print-stat-line ( xt -- )
  213:     >r 0. 0. 0 0 0 r> profile-points @ ['] add-calls map-list drop
  214:     ( ud-dyn-callee ud-dyn-caller u-stat )
  215:     6 u.r 7 u.r 7 u.r 12 ud.r 12 ud.r space ;
  216: 
  217: : print-library-stats ( -- )
  218:     library-calls @ list-length 20 u.r \ static callers
  219:     library-calls @ count-dyncalls 12 ud.r \ dynamic callers
  220:     13 spaces ;
  221: 
  222: : bblen+ ( u1 callp -- u2 )
  223:     calls-call @ profile-bblen @ + ;
  224: 
  225: : dyn-bblen+ ( ud1 callp -- ud2 )
  226:     calls-call @ dup profile-count 2@ rot profile-bblen @ 1 m*/ d+ ;
  227:     
  228: : print-bb-statistics ( -- )
  229:     ." static     dynamic" cr
  230:     all-bbs @ list-length 6 u.r all-bbs @ count-dyncalls 12 ud.r ."  basic blocks" cr
  231:     0 all-bbs @ ['] bblen+ map-list 6 u.r
  232:     0. all-bbs @ ['] dyn-bblen+ map-list 12 ud.r ."  primitives" cr
  233:     ;
  234: 
  235: : print-statistics ( -- )
  236:     ." callee exec'd static  dyn-caller  dyn-callee   condition" cr
  237:     ['] 0=  print-stat-line ." calls to coldefs with 0 callers" cr
  238:     ['] 1=  print-stat-line ." calls to coldefs with 1 callers" cr
  239:     ['] 2=  print-stat-line ." calls to coldefs with 2 callers" cr
  240:     ['] 3=  print-stat-line ." calls to coldefs with 3 callers" cr
  241:     ['] 1u> print-stat-line ." calls to coldefs with >1 callers" cr
  242:     print-library-stats     ." library calls" cr
  243:     print-bb-statistics
  244:     ;
  245: 
  246: : dinc ( profilep -- )
  247:     \ increment double pointed to by d-addr
  248:     profile-count dup 2@ 1. d+ rot 2! ;
  249: 
  250: : profile-this ( -- )
  251:     in-compile,? @ in-compile,? on
  252:     new-profile-point POSTPONE literal POSTPONE dinc
  253:     in-compile,? ! ;
  254: 
  255: \ Various words trigger PROFILE-THIS.  In order to avoid getting
  256: \ several calls to PROFILE-THIS from a compiling word (like ?EXIT), we
  257: \ just wait until the next word is parsed by the text interpreter (in
  258: \ compile state) and call PROFILE-THIS only once then.  The whole
  259: \ BEFORE-WORD hooking etc. is there for this.
  260: 
  261: \ The reason that we do this is because we use the source position for
  262: \ the profiling information, and there's only one source position for
  263: \ ?EXIT.  If we used the threaded code position instead, we would see
  264: \ that ?EXIT compiles to several threaded-code words, and could use
  265: \ different profile points for them.  However, usually dealing with
  266: \ the source is more practical.
  267: 
  268: \ Another benefit is that we can ask for profiling anywhere in a
  269: \ control-flow word (even before it compiles its own stuff).
  270: 
  271: \ Potential problem: Consider "COMPILING ] [" where COMPILING compiles
  272: \ a whole colon definition (and triggers our profiler), but during the
  273: \ compilation of the colon definition there is no parsing.  Afterwards
  274: \ you get interpret state at first (no profiling, either), but after
  275: \ the "]" you get parsing in compile state, and PROFILE-THIS gets
  276: \ called (and compiles code that is never executed).  It would be
  277: \ better if we had a way of knowing whether we are in a colon def or
  278: \ not (and used that knowledge instead of STATE).
  279: 
  280: Defer before-word-profile ( -- )
  281: ' noop IS before-word-profile
  282: 
  283: : before-word1 ( -- )
  284:     before-word-profile defers before-word ;
  285: 
  286: ' before-word1 IS before-word
  287: 
  288: : profile-this-compiling ( -- )
  289:     state @ if
  290: 	profile-this
  291: 	['] noop IS before-word-profile
  292:     endif ;
  293: 
  294: : cock-profiler ( -- )
  295:     \ as in cock the gun - pull the trigger
  296:     ['] profile-this-compiling IS before-word-profile
  297:     [ count-calls? ] [if] \ we are at a non-colondef profile point
  298: 	last-colondef-profile @ profile-straight-line off
  299:     [endif]
  300: ;
  301: 
  302: : hook-profiling-into ( "name" -- )
  303:     \ make (deferred word) "name" call cock-profiler, too
  304:     ' >body >r :noname
  305:     POSTPONE cock-profiler
  306:     r@ @ compile, \ old hook behaviour
  307:     POSTPONE ;
  308:     r> ! ; \ change hook behaviour
  309: 
  310: : note-execute ( -- )
  311:     \ end of BB due to execute, dodefer, perform
  312:     profile-this \ should actually happen after the word, but the
  313:                  \ error is probably small
  314: ;
  315: 
  316: : note-call ( addr -- )
  317:     \ addr is the body address of a called colon def or does handler
  318:     dup ['] (does>2) >body = if \ adjust does handler address
  319: 	4 cells here 1 cells - +!
  320:     endif
  321:     { addr }
  322:     current-profile-point @ { lastbb }
  323:     profile-this
  324:     current-profile-point @ { thisbb }
  325:     thisbb new-call { call-node }
  326:     over 3 cells + @ ['] dinc >body = if
  327: 	\ non-library call
  328:     !! update profile-bblenpi of last and current pp
  329: 	addr cell+ @ { callee-pp }
  330: 	callee-pp profile-postlude @ thisbb profile-callee-postlude !
  331: 	call-node callee-pp profile-calls insert-list
  332:     else ( addr call-prof-point )
  333: 	call-node library-calls insert-list
  334:     endif ;
  335: 
  336: : prof-compile, ( xt -- )
  337:     in-compile,? @ if
  338: 	DEFERS compile, EXIT
  339:     endif
  340:     1 current-profile-point @ profile-bblen +!
  341:     dup CASE
  342: 	['] execute of note-execute endof
  343: 	['] perform of note-execute endof
  344: 	dup >does-code if
  345: 	    dup >does-code note-call
  346: 	then
  347: 	dup >code-address CASE
  348: 	    docol:   OF dup >body note-call ENDOF
  349: 	    dodefer: OF note-execute ENDOF
  350: 	    \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
  351: 	    \ code words and ;code-defined words (code words could be optimized):
  352: 	ENDCASE
  353:     ENDCASE
  354:     DEFERS compile, ;
  355: 
  356: : :-hook-profile ( -- )
  357:     defers :-hook
  358:     next-profile-point-p @
  359:     profile-this
  360:     @ dup last-colondef-profile ! ( current-profile-point )
  361:     1 over profile-bblenpi !
  362:     profile-colondef? on ;
  363: 
  364: : exit-hook-profile ( -- )
  365:     defers exit-hook
  366:     1 last-colondef-profile @ profile-exits +! ;
  367: 
  368: : ;-hook-profile ( -- )
  369:     \ ;-hook is called before the POSTPONE EXIT
  370:     defers ;-hook
  371:     last-colondef-profile @ { col }
  372:     current-profile-point @ { bb }
  373:     col profile-bblen @ col profile-prelude +!
  374:     col profile-exits @ 0= if
  375: 	col bb profile-tailof !
  376: 	bb profile-bblen @ bb profile-callee-postlude @ +
  377: 	col profile-postlude !
  378: 	1 bb profile-bblenpi !
  379: 	\ not counting the EXIT
  380:     endif ;
  381: 
  382: hook-profiling-into then-like
  383: \ hook-profiling-into if-like    \ subsumed by other-control-flow
  384: \ hook-profiling-into ahead-like \ subsumed by other-control-flow
  385: hook-profiling-into other-control-flow
  386: hook-profiling-into begin-like
  387: hook-profiling-into again-like
  388: hook-profiling-into until-like
  389: ' :-hook-profile IS :-hook
  390: ' prof-compile, IS compile,
  391: ' exit-hook-profile IS exit-hook
  392: ' ;-hook-profile IS ;-hook

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>