@@ -1403,7 +1403,8 @@ def walk_(stmts):
14031403
14041404
14051405def pressure (body : Tuple [Stmt , ...], in_bytes : bool = False ,
1406- explicit_simd : bool = True ) -> int :
1406+ explicit_simd : bool = True ,
1407+ profile : Optional [List [Tuple [Stmt , int ]]] = None ) -> int :
14071408 """Peak simultaneously live SSA values, or the bytes they occupy.
14081409
14091410 `in_bytes` is what a caller comparing against a register budget has to
@@ -1539,13 +1540,40 @@ def reach(d: int, i: int) -> int:
15391540 if isinstance (v .type , BufferType ))
15401541 events += [(0 , floor ), (len (order ), - floor )] if floor else []
15411542 events .sort ()
1543+ if profile is not None :
1544+ # The same sweep, sampled at every statement: what is live across
1545+ # statement `i` is every interval that began at or before it and has
1546+ # not ended, which is the prefix sum of the events up to `i`.
1547+ delta = [0 ] * (len (order ) + 1 )
1548+ for p , w in events :
1549+ delta [min (p , len (order ))] += w
1550+ live = 0
1551+ for st , d in zip (order , delta ):
1552+ live += d
1553+ profile .append ((st , live ))
15421554 live = peak = 0
15431555 for _ , w in events :
15441556 live += w
15451557 peak = max (peak , live )
15461558 return peak
15471559
15481560
1561+ def pressure_profile (body : Tuple [Stmt , ...], in_bytes : bool = True ,
1562+ explicit_simd : bool = True ) -> List [Tuple [Stmt , int ]]:
1563+ """`pressure` at every statement rather than at its worst one.
1564+
1565+ Pre-order, as `pressure` numbers the statements, each with the values --
1566+ or the bytes -- live across it. The peak of it is `pressure`'s answer;
1567+ where the figure rises and falls is the question a per-section register
1568+ budget asks (`setmaxnreg` on NVIDIA, dynamic VGPRs on gfx12): which part of
1569+ a body needs the registers, and which parts could give them back.
1570+ """
1571+ profile : List [Tuple [Stmt , int ]] = []
1572+ pressure (body , in_bytes = in_bytes , explicit_simd = explicit_simd ,
1573+ profile = profile )
1574+ return profile
1575+
1576+
15491577def _is_integer (t ) -> bool :
15501578 return (isinstance (t , ScalarType ) and not t .is_vector
15511579 and (t .base .name .startswith (('I' , 'U' )) or t .base is Datatype .SIZE ))
0 commit comments