
    |h24                         S SK r S SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr   " S S5      r " S	 S
5      r\" S/ SQ5      rS r " S S5      r\" SSS/5      r " S S\5      rg)    N)
namedtuple)Sequence)contextmanager)cached_property)configc                   0    \ rS rSrSrS/rS rS rS rSr	g)	RecordLLVMPassTimingsLegacy   9A helper context manager to track LLVM pass timings.
    _datac                 2    [         R                  " S5        U $ ))Enables the pass timing in LLVM.
        T)llvmset_time_passesselfs    V/home/james-whalen/.local/lib/python3.13/site-packages/numba/misc/llvm_pass_timings.py	__enter__%RecordLLVMPassTimingsLegacy.__enter__   s     	T"    c                 d    [         R                  " 5       U l        [         R                  " S5        g)2Reset timings and save report internally.
        FN)r   report_and_reset_timingsr   r   r   exc_valexc_typeexc_tbs       r   __exit__$RecordLLVMPassTimingsLegacy.__exit__   s%     224
U#r   c                 ,    [        U R                  5      $ zTRetrieve timing data for processing.

Returns
-------
timings: ProcessedPassTimings
ProcessedPassTimingsr   r   s    r   getRecordLLVMPassTimingsLegacy.get!        $DJJ//r   )r   N)
__name__
__module____qualname____firstlineno____doc__	__slots__r   r   r$   __static_attributes__ r   r   r	   r	      s     	I0r   r	   c                   8    \ rS rSrSrSS/rS rS rS rS r	S	r
g
)RecordLLVMPassTimings+   r   r   _pbc                     Xl         S U l        g N)r2   r   )r   pbs     r   __init__RecordLLVMPassTimings.__init__1   s    
r   c                 :    U R                   R                  5         U $ )r   )r2   start_pass_timingr   s    r   r   RecordLLVMPassTimings.__enter__5   s     	""$r   c                 B    U R                   R                  5       U l        g)r   N)r2   finish_pass_timingr   r   s       r   r   RecordLLVMPassTimings.__exit__;   s     XX002
r   c                 ,    [        U R                  5      $ r!   r"   r   s    r   r$   RecordLLVMPassTimings.getA   r&   r   )r   r2   N)r'   r(   r)   r*   r+   r,   r6   r   r   r$   r-   r.   r   r   r0   r0   +   s'     % I0r   r0   PassTimingRecord)
	user_timeuser_percentsystem_timesystem_percentuser_system_timeuser_system_percent	wall_timewall_percent	pass_nameinstructionc                    ^^ U S   mTR                   S:X  d   eU4S jnS Vs/ s H
  o!" U5      PM     snm[        S U 5      nU4S jn[        [        XC5      5      $ s  snf )zAdjust timing records because of truncated information.

Details: The percent information can be used to improve the timing
information.

Returns
-------
res: List[PassTimingRecord]
Totalc                 `   >^^^ U  S3mU  S3m[         R                  " T5      mUUUU4S jnU$ )N_time_percentc                 4   > T" T5      nXT   -  S-  nX T'   U $ )z'Compute percent x total_time = adjustedg{Gz?r.   )dtotaladjustedpercent_attr	time_attrtime_getter	total_recs      r   adjust6_adjust_timings.<locals>.make_adjuster.<locals>.adjustn   s*    	*E.5H#iLHr   )operator
attrgetter)attrrY   rU   rV   rW   rX   s     @@@r   make_adjuster&_adjust_timings.<locals>.make_adjusteri   s;    fEN	x()))4	 	 r   )usersystemuser_systemwallc                 "    U R                  5       $ r4   )_asdictxs    r   <lambda>!_adjust_timings.<locals>.<lambda>}   s
    !))+r   c                 <   > T H  nU" U 5      n M     [        S0 U D6$ )Nr.   )r@   )rR   fnadj_fnss     r   chained _adjust_timings.<locals>.chained   s&    B1A   $!$$r   )rI   maplist)recordsr^   rg   dictsrm   rl   rX   s        @@r   _adjust_timingsrs   \   st     I')))  #L"KQa"KG
 %w/E% G#$$s   Ac                   \    \ rS rSrSrS rS rS rS rS r	S r
SS	 jr\S
 5       rS rSrg)r#      zA class for processing raw timing report from LLVM.

The processing is done lazily so we don't waste time processing unused
timing information.
c                     Xl         g r4   	_raw_data)r   raw_datas     r   r6   ProcessedPassTimings.__init__   s    !r   c                 ,    [        U R                  5      $ r4   )boolrx   r   s    r   __bool__ProcessedPassTimings.__bool__   s    DNN##r   c                     U R                   $ )z7Returns the raw string data.

Returns
-------
res: str
rw   r   s    r   get_raw_data!ProcessedPassTimings.get_raw_data   s     ~~r   c                 <    U R                  5       S   R                  $ )zHCompute the total time spend in all passes.

Returns
-------
res: float
rL   )list_recordsrG   r   s    r   get_total_time#ProcessedPassTimings.get_total_time   s       "2&000r   c                     U R                   $ )z[Get the processed data for the timing report.

Returns
-------
res: List[PassTimingRecord]
)
_processedr   s    r   r   !ProcessedPassTimings.list_records   s     r   c                     U R                  5       n[        R                  " S5      n[        R                  " XSS U5      $ )aG  Returns the top(n) most time-consuming (by wall-time) passes.

Parameters
----------
n: int
    This limits the maximum number of items to show.
    This function will show the ``n`` most time-consuming passes.

Returns
-------
res: List[PassTimingRecord]
    Returns the top(n) most time-consuming passes in descending order.
rG   NrL   )r   r[   r\   heapqnlargest)r   nrq   keys       r   list_topProcessedPassTimings.list_top   s9     ##%!!+.~~a"s33r   c           	        ^^ / mSU-  mUU4S jnU" SU R                  5       S S35        U" S5        U R                  U5       H4  nU" SUR                  S SUR                  S	 S
UR                   35        M6     SR                  T5      $ )aJ  Return a string summarizing the timing information.

Parameters
----------
topn: int; optional
    This limits the maximum number of items to show.
    This function will show the ``topn`` most time-consuming passes.
indent: int; optional
    Set the indentation level. Defaults to 0 for no indentation.

Returns
-------
res: str
 c                 0   > TR                  T U  35        g r4   )append)argbufprefixs    r   ap(ProcessedPassTimings.summary.<locals>.ap   s    JJ&#'(r   zTotal .4fszTop timings:z  zs (5z%) 
)r   r   rG   rH   rI   join)r   topnindentr   pr   r   s        @@r   summaryProcessedPassTimings.summary   s     v	) 	VD'')#.a01
>t$AAKK$Cq'9Q[[MJK %yy~r   c                 "    U R                  5       $ )zdA cached property for lazily processing the data and returning it.

See ``_process()`` for details.
)_processr   s    r   r   ProcessedPassTimings._processed   s     }}r   c                 T    S n[        U" U R                  5      5      n[        U5      $ )zParses the raw string data from LLVM timing report and attempts
to improve the data by recomputing the times
(See `_adjust_timings()``).
c           	   3     #    U R                  5       nSnSU S3n[        U5      nSSSSSS	S
.nU H[  n[        R                  " X65      nU(       d  M"  [        R                  " SU5      nU V	s/ s H  oU	R                  5          PM     n
n	  O   W
S   S	:X  d   e/ nSnSnU
SS  HC  n	U	S:X  a  X-  nM  UR                  U	 S35        UR                  U	 S35        USU SU S3-  nME     0 n[        R                   H  n	X;  d  M
  U	S	:w  d  M  SX'   M     US-  nU H  n[        R                  " X5      nUc  M  [        UR                  5       5      n [        X5       V	Vs0 s H  u  pXb  [        U5      OS_M     nn	nUR                  U5        U S   n[        SS	U0UD6nUv   UR                  S:X  d  M    O   SR                  U5      nSU;   a  gU(       a  [!        SU 35      egs  sn	f s  snn	f 7f)z_A generator that parses the raw_data line-by-line to extract
timing information for each pass.
z[a-zA-Z+ ]+z(?:\s*-+z-+)+r`   ra   rb   rc   rJ   rI   )z	User TimezSystem TimezUser+Systemz	Wall TimeInstrNamez[a-zA-Z][a-zA-Z+ ]+rL   z\s*((?:[0-9]+\.)?[0-9]+) NrO   rP   z\s+(?:z\s*\(z%\)|-+)g        z\s*(.*)rM   r   z Analysis execution timing reportz'unexpected text after parser finished:
r.   )
splitlinesiterrematchfindallstripr   r@   _fieldsrp   groupszipfloatupdaterI   r   
ValueError)ry   lines	colheadermulticolheaders	line_iter
header_maplnmraw_headerskheadersattrsr   patmissingvdatarI   rec	remainings                       r   parse,ProcessedPassTimings._process.<locals>.parse   s?     '')E&I!))D9OUI $',#&#J  HH_11"$**-CR"HK>IJk!'')4kGJ   2;+---E+ACSb\%HCLLA3e-LLA3h0fQCuQCw77C " G%-->a;&6!$GJ . :CHHS%=#AHHJ/H(+E(<>(< =uQxcA(<  >KK( (I* "+/3C I}}/    		),I 2Y> >ykJ  [ K6>s=   AG:G:.G/
A;G:	G:)G:>'G:%G48G:==G:)rp   rx   rs   )r   r   rq   s      r   r   ProcessedPassTimings._process   s)    H	V uT^^,-w''r   rw   N)   r   )r'   r(   r)   r*   r+   r6   r}   r   r   r   r   r   r   r   r   r-   r.   r   r   r#   r#      sD    "$14$6  R(r   r#   NamedTimingsnametimingsc                   |    \ rS rSrSrS r\S 5       r\S 5       rS r	S r
S r\S	 5       rSS
 jrS rS rS rSrg)PassTimingsCollectioni>  z|A collection of pass timings.

This class implements the ``Sequence`` protocol for accessing the
individual timing records.
c                     Xl         / U l        g r4   _name_records)r   r   s     r   r6   PassTimingsCollection.__init__E  s    
r   c              #      #    [         R                  (       aA  [        5        nSv   SSS5        WR                  5       nU(       a  U R	                  X5        ggSv   g! , (       d  f       N== f7f)zRecord new timings and append to this collection.

Note: this is mainly for internal use inside the compiler pipeline.

See also ``RecordLLVMPassTimingsLegacy``

Parameters
----------
name: str
    Name for the records.
N)r   LLVM_PASS_TIMINGSr	   r$   _append)r   r   r   r   s       r   record_legacy#PassTimingsCollection.record_legacyI  sL      ##,.' /++-CT'   /.s    A/A7A/
A,(A/c              #      #    [         R                  (       aB  [        U5       nSv   SSS5        WR                  5       nU(       a  U R	                  X5        ggSv   g! , (       d  f       N== f7f)zRecord new timings and append to this collection.

Note: this is mainly for internal use inside the compiler pipeline.

See also ``RecordLLVMPassTimings``

Parameters
----------
name: str
    Name for the records.
N)r   r   r0   r$   r   )r   r   r5   r   r   s        r   recordPassTimingsCollection.recordb  sN      ##&r*g +++-CT'   +*s   !A0A7A0
A-)A0c                 L    U R                   R                  [        X5      5        g)zAppend timing records

Parameters
----------
name: str
    Name for the records.
timings: ProcessedPassTimings
    the timing records.
N)r   r   r   )r   r   r   s      r   r   PassTimingsCollection._append{  s     	\$89r   c                 ^    U R                   (       a  [        S U R                    5       5      $ g)zComputes the sum of the total time across all contained timings.

Returns
-------
res: float or None
    Returns the total number of seconds or None if no timings were
    recorded
c              3   T   #    U  H  oR                   R                  5       v   M      g 7fr4   r   r   ).0rs     r   	<genexpr>7PassTimingsCollection.get_total_time.<locals>.<genexpr>  s     I=ayy//11=s   &(N)r   sumr   s    r   r   $PassTimingsCollection.get_total_time  s#     ==I4==IIIr   c                 .    [        U R                  S SS9$ )zqReturns the timings in descending order of total time duration.

Returns
-------
res: List[ProcessedPassTimings]
c                 6    U R                   R                  5       $ r4   r   rf   s    r   rh   :PassTimingsCollection.list_longest_first.<locals>.<lambda>  s    AII$<$<$>r   T)r   reverse)sortedr   r   s    r   list_longest_first(PassTimingsCollection.list_longest_first  s     dmm>"$ 	$r   c                 $    U R                   (       + $ )z	
        r   r   s    r   is_emptyPassTimingsCollection.is_empty  s     ==  r   c                    U R                   (       a  g/ nUR                  nU" SU R                   35        U R                  5       nU" SUS 35        [	        U R
                  5       Hj  u  pVU" SU SUR                   35        UR                  R                  5       U-  S-  nU" SUS	 S
35        U" UR                  R                  USS95        Ml     SR                  U5      $ )a1  Return a string representing the summary of the timings.

Parameters
----------
topn: int; optional, default=5.
    This limits the maximum number of items to show.
    This function will show the ``topn`` most time-consuming passes.

Returns
-------
res: str

See also ``ProcessedPassTimings.summary()``
zNo pass timings were recordedzPrinting pass timings for zTotal time: r   z== #r   d   z
 Percent: z.1f%   )r   r   r   )
r   r   r   r   	enumerater   r   r   r   r   )r   r   r   r   overall_timeir   percents           r   r   PassTimingsCollection.summary  s     ==2CB+DJJ<89..0Ll3/01!$--0T!AaffX&'))224|CcIZ}A./199$$$q$9:	 1
 99S>!r   c                      U R                   U   $ )zGet the i-th timing record.

Returns
-------
res: (name, timings)
    A named tuple with two fields:

    - name: str
    - timings: ProcessedPassTimings
r   )r   r   s     r   __getitem__!PassTimingsCollection.__getitem__  s     }}Qr   c                 ,    [        U R                  5      $ )z#Length of this collection.
        )lenr   r   s    r   __len__PassTimingsCollection.__len__  s     4==!!r   c                 "    U R                  5       $ r4   )r   r   s    r   __str__PassTimingsCollection.__str__  s    ||~r   r   N)r   )r'   r(   r)   r*   r+   r6   r   r   r   r   r   r   propertyr   r   r   r  r  r-   r.   r   r   r   r   >  sk      0  0
:	$ ! !
"< "
r   r   )r   r[   r   collectionsr   collections.abcr   
contextlibr   	functoolsr   
numba.corer   llvmlite.bindingbindingr   r	   r0   r@   rs   r#   r   r   r.   r   r   <module>r     s~    	   " $ % %  0 0:0 0@  "*%Zo( o(d .69*=>YH Yr   