
    xKg-                         d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ  G d d      Z ed	g d
      Zd Z G d d      Z edddg      Z G d de      Zy)    N)
namedtuple)Sequence)contextmanager)cached_property)configc                   (    e Zd ZdZdgZd Zd Zd Zy)RecordLLVMPassTimingsz9A helper context manager to track LLVM pass timings.
    _datac                 0    t        j                  d       | S )z)Enables the pass timing in LLVM.
        T)llvmset_time_passesselfs    `/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/misc/llvm_pass_timings.py	__enter__zRecordLLVMPassTimings.__enter__   s     	T"    c                 `    t        j                         | _        t        j                  d       y)z2Reset timings and save report internally.
        FN)r   report_and_reset_timingsr
   r   )r   exc_valexc_typeexc_tbs       r   __exit__zRecordLLVMPassTimings.__exit__   s%     224
U#r   c                 ,    t        | j                        S )ztRetrieve timing data for processing.

        Returns
        -------
        timings: ProcessedPassTimings
        )ProcessedPassTimingsr
   r   s    r   getzRecordLLVMPassTimings.get!   s     $DJJ//r   N)__name__
__module____qualname____doc__	__slots__r   r   r    r   r   r	   r	      s     	I0r   r	   PassTimingRecord)
	user_timeuser_percentsystem_timesystem_percentuser_system_timeuser_system_percent	wall_timewall_percent	pass_nameinstructionc                     | d   j                   dk(  sJ fd}dD cg c]
  } ||       c}t        d |       }fd}t        t        ||            S c c}w )zAdjust timing records because of truncated information.

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

    Returns
    -------
    res: List[PassTimingRecord]
    Totalc                 \    |  d|  dt        j                        fd}|S )N_time_percentc                 8           }||    z  dz  }|| <   | S )z'Compute percent x total_time = adjustedg{Gz?r!   )dtotaladjustedpercent_attr	time_attrtime_getter	total_recs      r   adjustz6_adjust_timings.<locals>.make_adjuster.<locals>.adjustN   s.    	*Eq.5H#AiLHr   )operator
attrgetter)attrr;   r7   r8   r9   r:   s     @@@r   make_adjusterz&_adjust_timings.<locals>.make_adjusterI   s6    fEN	x()))4	 r   )usersystemuser_systemwallc                 "    | j                         S N)_asdictxs    r   <lambda>z!_adjust_timings.<locals>.<lambda>]   s
    !))+r   c                 8    D ]
  } ||       }  t        di | S )Nr!   )r"   )r4   fnadj_fnss     r   chainedz _adjust_timings.<locals>.chained_   s&    B1A   $!$$r   )r+   maplist)recordsr?   rH   dictsrM   rL   r:   s        @@r   _adjust_timingsrR   <   sv     I')))  #L"KQa"KG
 %w/E% GU#$$s   Ac                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	ddZ
ed	        Zd
 Zy)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                     || _         y rE   	_raw_data)r   raw_datas     r   __init__zProcessedPassTimings.__init__p   s	    !r   c                 ,    t        | j                        S rE   )boolrV   r   s    r   __bool__zProcessedPassTimings.__bool__s   s    DNN##r   c                     | j                   S )zWReturns the raw string data.

        Returns
        -------
        res: str
        rU   r   s    r   get_raw_dataz!ProcessedPassTimings.get_raw_datav   s     ~~r   c                 <    | j                         d   j                  S )zhCompute the total time spend in all passes.

        Returns
        -------
        res: float
        r.   )list_recordsr)   r   s    r   get_total_timez#ProcessedPassTimings.get_total_time   s       "2&000r   c                     | j                   S )z{Get the processed data for the timing report.

        Returns
        -------
        res: List[PassTimingRecord]
        )
_processedr   s    r   r_   z!ProcessedPassTimings.list_records   s     r   c                     | j                         }t        j                  d      }t        j                  ||dd |      S )a  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.
        r)   Nr.   )r_   r<   r=   heapqnlargest)r   nrP   keys       r   list_topzProcessedPassTimings.list_top   s;     ##%!!+.~~a"s33r   c           	         g d|z  fd} |d| j                         dd        |d       | j                  |      D ]3  } |d|j                  dd|j                  d	d
|j                          5 dj                        S )a  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    j                   |         y rE   )append)argbufprefixs    r   apz(ProcessedPassTimings.summary.<locals>.ap   s    JJ&#'(r   zTotal .4fszTop timings:z  zs (5z%) 
)r`   rh   r)   r*   r+   join)r   topnindentrp   prn   ro   s        @@r   summaryzProcessedPassTimings.summary   s     v	) 	VD'')#.a01
>t$AAKK$Cq'9Q[[MJK %yy~r   c                 "    | j                         S )ztA cached property for lazily processing the data and returning it.

        See ``_process()`` for details.
        )_processr   s    r   rb   zProcessedPassTimings._processed   s     }}r   c                 T    d }t         || j                              }t        |      S )zParses the raw string data from LLVM timing report and attempts
        to improve the data by recomputing the times
        (See `_adjust_timings()``).
        c           	   3     K   | j                         }d}d| d}t        |      }dddddd	d
}|D ]R  }t        j                  ||      }|st        j                  d|      }|D 	cg c]  }	||	j                             }
}	 n 
d   d	k(  sJ g }d}d}|
dd D ]A  }	|	dk(  r||z  }|j                  |	 d       |j                  |	 d       |d| d| dz  }C i }t        j                  D ]  }	|	|vs|	d	k7  sd||	<    |dz  }|D ]  }t        j                  ||      }|t        |j                               } t        ||       D 	ci c]  \  }	}|	|t        |      nd }}	}|j                  |       | d   }t        dd	|i|}| |j                  dk(  s n dj                  |      }|rt!        d|       yc c}	w c c}}	w w)zwA 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   r,   r+   )z	User TimezSystem TimezUser+Systemz	Wall TimeInstrNamez[a-zA-Z][a-zA-Z+ ]+r.   z\s*((?:[0-9]+\.)?[0-9]+) Nr1   r2   z\s+(?:z\s*\(z%\)|-+)g        z\s*(.*)r/   rt   z'unexpected text after parser finished:
r!   )
splitlinesiterrematchfindallstriprl   r"   _fieldsrO   groupszipfloatupdater+   ru   
ValueError)rW   lines	colheadermulticolheaders	line_iter
header_maplnmraw_headerskheadersattrsrf   patmissingvdatar+   rec	remainings                       r   parsez,ProcessedPassTimings._process.<locals>.parse   sC     '')E&I!))D9OUI $',#&#J  HH_b1"$**-CR"HK>IJkz!'')4kGJ   2;+---E+ACSb\%1HCLLA3e-LLA3h0fQCuQCw77C " G%--E>a;&6!$GAJ . :CHHS"%=#AHHJ/H(+E8(<>(<1 1=uQxcA(<  >KK( (I* "+/3C I}}/    		),I >ykJ  Q K6>s=   A
GG'G
A7G9G?)G)(GG+9G%0G)rO   rV   rR   )r   r   rP   s      r   r{   zProcessedPassTimings._process   s)    C	L uT^^,-w''r   N)   r   )r   r   r   r   rX   r[   r]   r`   r_   rh   ry   r   rb   r{   r!   r   r   r   r   i   sD    "$14$6  M(r   r   NamedTimingsnametimingsc                   b    e Zd ZdZd Zed        Zd Zd Zd Z	e
d        ZddZd	 Zd
 Zd Zy)PassTimingsCollectionzA collection of pass timings.

    This class implements the ``Sequence`` protocol for accessing the
    individual timing records.
    c                      || _         g | _        y rE   )_name_records)r   r   s     r   rX   zPassTimingsCollection.__init__   s    
r   c              #      K   t         j                  r=t               5 }d ddd       j                         }|r| j	                  ||       yyd y# 1 sw Y   4xY ww)a  Record 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   LLVM_PASS_TIMINGSr	   r   _append)r   r   r   r   s       r   recordzPassTimingsCollection.record$  sO      ##&(G )++-CT3'   )(s   A!A3A!AA!c                 N    | j                   j                  t        ||             y)zAppend timing records

        Parameters
        ----------
        name: str
            Name for the records.
        timings: ProcessedPassTimings
            the timing records.
        N)r   rl   r   )r   r   r   s      r   r   zPassTimingsCollection._append=  s     	\$89r   c                 T    | j                   rt        d | j                   D              S y)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   P   K   | ]  }|j                   j                            y wrE   r   r`   ).0rs     r   	<genexpr>z7PassTimingsCollection.get_total_time.<locals>.<genexpr>S  s     I=aqyy//1=s   $&N)r   sumr   s    r   r`   z$PassTimingsCollection.get_total_timeI  s#     ==I4==IIIr   c                 4    t        | j                  d d      S )zReturns the timings in descending order of total time duration.

        Returns
        -------
        res: List[ProcessedPassTimings]
        c                 6    | j                   j                         S rE   r   rG   s    r   rI   z:PassTimingsCollection.list_longest_first.<locals>.<lambda>_  s    AII$<$<$>r   T)rg   reverse)sortedr   r   s    r   list_longest_firstz(PassTimingsCollection.list_longest_firstW  s     dmm>"$ 	$r   c                     | j                    S )z	
        r   r   s    r   is_emptyzPassTimingsCollection.is_emptyb  s     ==  r   c                    | j                   ryg }|j                  } |d| j                          | j                         } |d|d       t	        | j
                        D ]m  \  }} |d| d|j                          |j                  j                         |z  dz  } |d|d	d
        ||j                  j                  |d             o dj                  |      S )a  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: rq   z== #rj   d   z
 Percent: z.1f%   )rv   rw   rt   )
r   rl   r   r`   	enumerater   r   r   ry   ru   )r   rv   rn   rp   overall_timeir   percents           r   ry   zPassTimingsCollection.summaryh  s     ==2CB+DJJ<89..0Ll3/01!$--01T!AaffX&'))224|CcIZ}A./199$$$q$9:	 1
 99S>!r   c                      | j                   |   S )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__z!PassTimingsCollection.__getitem__  s     }}Qr   c                 ,    t        | j                        S )z#Length of this collection.
        )lenr   r   s    r   __len__zPassTimingsCollection.__len__  s     4==!!r   c                 "    | j                         S rE   )ry   r   s    r   __str__zPassTimingsCollection.__str__  s    ||~r   N)r   )r   r   r   r   rX   r   r   r   r`   r   propertyr   ry   r   r   r   r!   r   r   r   r     sW      0
:	$ ! !
"< "
r   r   )r   r<   rd   collectionsr   collections.abcr   
contextlibr   	functoolsr   
numba.corer   llvmlite.bindingbindingr   r	   r"   rR   r   r   r   r!   r   r   <module>r      ss    	   " $ % %  0 0:  "*%Zj( j(Z .69*=>@H @r   