
    1;ji24                        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 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dS )    N)
namedtuple)Sequence)contextmanager)cached_property)configc                   *    e Zd ZdZdgZd Zd Zd ZdS )RecordLLVMPassTimingsLegacy9A helper context manager to track LLVM pass timings.
    _datac                 .    t          j        d           | S ))Enables the pass timing in LLVM.
        T)llvmset_time_passesselfs    V/root/voice-cloning/.venv/lib/python3.11/site-packages/numba/misc/llvm_pass_timings.py	__enter__z%RecordLLVMPassTimingsLegacy.__enter__   s     	T"""    c                 ^    t          j                    | _        t          j        d           dS )2Reset timings and save report internally.
        FN)r   report_and_reset_timingsr   r   r   exc_valexc_typeexc_tbs       r   __exit__z$RecordLLVMPassTimingsLegacy.__exit__   s*     244
U###r   c                 *    t          | j                  S ztRetrieve timing data for processing.

        Returns
        -------
        timings: ProcessedPassTimings
        ProcessedPassTimingsr   r   s    r   getzRecordLLVMPassTimingsLegacy.get!        $DJ///r   N)__name__
__module____qualname____doc__	__slots__r   r   r!    r   r   r	   r	      sT          	I    0 0 0 0 0r   r	   c                   2    e Zd ZdZddgZd Zd Zd Zd ZdS )	RecordLLVMPassTimingsr
   r   _pbc                 "    || _         d | _        d S N)r+   r   )r   pbs     r   __init__zRecordLLVMPassTimings.__init__1   s    


r   c                 8    | j                                          | S )r   )r+   start_pass_timingr   s    r   r   zRecordLLVMPassTimings.__enter__5   s     	""$$$r   c                 B    | j                                         | _        dS )r   N)r+   finish_pass_timingr   r   s       r   r   zRecordLLVMPassTimings.__exit__;   s     X0022
r   c                 *    t          | j                  S r   r   r   s    r   r!   zRecordLLVMPassTimings.getA   r"   r   N)	r#   r$   r%   r&   r'   r/   r   r   r!   r(   r   r   r*   r*   +   sf          % I      0 0 0 0 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fddD             t          d |           }fd}t          t          ||                    S )zAdjust timing records because of truncated information.

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

    Returns
    -------
    res: List[PassTimingRecord]
    Totalc                 Z    |  d|  dt          j                  fd}|S )N_time_percentc                 D               }||          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   s3    K	**Eq.5H#AiLHr   )operator
attrgetter)attrrN   rJ   rK   rL   rM   s     @@@r   make_adjusterz&_adjust_timings.<locals>.make_adjusteri   s^    NNN	((())44	 	 	 	 	 	 	 	 r   c                 &    g | ]} |          S r(   r(   ).0xrR   s     r   
<listcomp>z#_adjust_timings.<locals>.<listcomp>x   s0       a  r   )usersystemuser_systemwallc                 *    |                                  S r-   )_asdictrU   s    r   <lambda>z!_adjust_timings.<locals>.<lambda>}   s    !))++ r   c                 <    D ]} ||           } t          di | S )Nr(   )r5   )rG   fnadj_fnss     r   chainedz _adjust_timings.<locals>.chained   s5     	 	B1AA$$!$$$r   )r>   maplist)recordsdictsrb   ra   rR   rM   s      @@@r   _adjust_timingsrg   \   s     I'))))       "K  G
 %%w//E% % % % % GU##$$$r   c                   Z    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dS )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                     || _         d S r-   	_raw_data)r   raw_datas     r   r/   zProcessedPassTimings.__init__   s    !r   c                 *    t          | j                  S r-   )boolrk   r   s    r   __bool__zProcessedPassTimings.__bool__   s    DN###r   c                     | j         S )zWReturns the raw string data.

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

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

        Returns
        -------
        res: List[PassTimingRecord]
        )
_processedr   s    r   rs   z!ProcessedPassTimings.list_records   s     r   c                     |                                  }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<   NrA   )rs   rO   rP   heapqnlargest)r   nre   keys       r   list_topzProcessedPassTimings.list_top   sA     ##%%!+..~a"s333r      r   c           	         g d|z  fd} |d|                                  dd            |d           |                     |          D ]'} |d|j        dd|j        d	d
|j                    (d                              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                 :                          |             d S r-   )append)argbufprefixs    r   apz(ProcessedPassTimings.summary.<locals>.ap   s&    JJ&'#''(((((r   zTotal .4fszTop timings:z  zs (5z%) 
)rt   r|   r<   r=   r>   join)r   topnindentr   pr   r   s        @@r   summaryzProcessedPassTimings.summary   s     v	) 	) 	) 	) 	) 	) 	0D''))0000111
>t$$ 	L 	LABJAKJJJJJJQ[JJKKKKyy~~r   c                 *    |                                  S )ztA cached property for lazily processing the data and returning it.

        See ``_process()`` for details.
        )_processr   s    r   rv   zProcessedPassTimings._processed   s     }}r   c                 `    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   |                                  }d}d| d}t          |          }dddddd	d
|D ]>}t          j        ||          }|r%t          j        d|          }fd|D             } n?|d         d	k    sJ g }	d}
d}|dd         D ]J}|dk    r||
z  }|	                    | d           |	                    | d           |d|
 d|
 dz  }Ki }t          j        D ]}||	vr|d	k    rd||<   |dz  }|D ]}t          j        ||          }|wt          |	                                          } d t          |	|           D             }|                    |           | d         }t          dd	|i|}|V  |j        dk    r nd                    |          }d|v rdS |rt          d|           dS )zwA generator that parses the raw_data line-by-line to extract
            timing information for each pass.
            z[a-zA-Z+ ]+z(?:\s*-+z-+)+rW   rX   rY   rZ   r?   r>   )z	User TimezSystem TimezUser+Systemz	Wall TimeInstrNamez[a-zA-Z][a-zA-Z+ ]+c                 D    g | ]}|                                          S r(   )strip)rT   k
header_maps     r   rV   z@ProcessedPassTimings._process.<locals>.parse.<locals>.<listcomp>  s&    JJJz!''))4JJJr   rA   z\s*((?:[0-9]+\.)?[0-9]+) NrD   rE   z\s+(?:z\s*\(z%\)|-+)        z\s*(.*)c                 <    i | ]\  }}||t          |          ndS )Nr   )float)rT   r   vs      r   
<dictcomp>z@ProcessedPassTimings._process.<locals>.parse.<locals>.<dictcomp>  s<     > > > $1 1=uQxxxc > > >r   rB   r   z Analysis execution timing reportz'unexpected text after parser finished:
r(   )
splitlinesiterrematchfindallr   r5   _fieldsrd   groupszipupdater>   r   
ValueError)rl   lines	colheadermulticolheaders	line_iterlnmraw_headersheadersattrsrz   patr   missingdatar>   rec	remainingr   s                     @r   parsez,ProcessedPassTimings._process.<locals>.parse   s      ''))E&I9)999OUI $',#&# J    H_b11 "$*-CR"H"HKJJJJkJJJGE	 2;+----E+ACSbS\ 8 8%%1HCCLLA---LLA0007Q77Q7777CC G%- % %E>>a;&6&6!$GAJ:C  HS"%%=#AHHJJ//H> >(+E8(<(<> > >DKK((( (I*  "+/3 C III}//		),,I 2Y>>  JyJJ   r   )rd   rk   rg   )r   r   re   s      r   r   zProcessedPassTimings._process   s>    H	 H	 H	V uuT^,,--w'''r   N)r}   r   )r#   r$   r%   r&   r/   ro   rq   rt   rs   r|   r   r   rv   r   r(   r   r   r    r       s         " " "$ $ $  1 1 1  4 4 4$   6   _R( R( R( R( R(r   r    NamedTimingsnametimingsc                       e Zd ZdZd Zed             Zed             Zd Zd Z	d Z
ed             Zdd
Zd Zd Zd ZdS )PassTimingsCollectionzA collection of pass timings.

    This class implements the ``Sequence`` protocol for accessing the
    individual timing records.
    c                 "    || _         g | _        d S r-   )_name_records)r   r   s     r   r/   zPassTimingsCollection.__init__E  s    
r   c              #      K   t           j        rZt                      5 }dV  ddd           n# 1 swxY w Y   |                                }|r|                     ||           dS dS dV  dS )a  Record 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_legacyz#PassTimingsCollection.record_legacyI  s       # 
	,.. '              ++--C (T3'''''( ( EEEEEs   .22c              #      K   t           j        r[t          |          5 }dV  ddd           n# 1 swxY w Y   |                                }|r|                     ||           dS dS dV  dS )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   r   r*   r!   r   )r   r   r.   r   r   s        r   recordzPassTimingsCollection.recordb  s       # 
	&r** g              ++--C (T3'''''( ( EEEEEs   /33c                 V    | j                             t          ||                     dS )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   zPassTimingsCollection._append{  s*     	\$8899999r   c                 P    | j         rt          d | j         D                       S dS )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   H   K   | ]}|j                                         V  d S r-   r   rt   )rT   rs     r   	<genexpr>z7PassTimingsCollection.get_total_time.<locals>.<genexpr>  s0      IIaqy//11IIIIIIr   N)r   sumr   s    r   rt   z$PassTimingsCollection.get_total_time  s2     = 	II4=IIIIII4r   c                 2    t          | j        d d          S )zReturns the timings in descending order of total time duration.

        Returns
        -------
        res: List[ProcessedPassTimings]
        c                 4    | j                                         S r-   r   r]   s    r   r^   z:PassTimingsCollection.list_longest_first.<locals>.<lambda>  s    AI$<$<$>$> r   T)r{   reverse)sortedr   r   s    r   list_longest_firstz(PassTimingsCollection.list_longest_first  s(     dm>>"$ $ $ 	$r   c                     | j          S )z	
        r   r   s    r   is_emptyzPassTimingsCollection.is_empty  s     =  r   r}   c                    | j         rdS g }|j        } |d| j                    |                                 } |d|d           t	          | j                  D ]o\  }} |d| d|j                    |j                                        |z  dz  } |d|d	d
            ||j                            |d                     pd	                    |          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: r   z== #r   d   z
 Percent: z.1f%   )r   r   r   )
r   r   r   rt   	enumerater   r   r   r   r   )r   r   r   r   overall_timeir   percents           r   r   zPassTimingsCollection.summary  s    = 	"22CBB8DJ88999..00LB0l000111!$-00 ; ;1&!&&af&&''')2244|CcI.....///19$$$q$99::::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                 *    |                                  S r-   )r   r   s    r   __str__zPassTimingsCollection.__str__  s    ||~~r   N)r}   )r#   r$   r%   r&   r/   r   r   r   r   rt   r   propertyr   r   r   r   r   r(   r   r   r   r   >  s              ^0   ^0
: 
: 
:  	$ 	$ 	$ ! ! X!
" " " "<     " " "
    r   r   )r   rO   rx   collectionsr   collections.abcr   
contextlibr   	functoolsr   
numba.corer   llvmlite.bindingbindingr   r	   r*   r5   rg   r    r   r   r(   r   r   <module>r      s   				   " " " " " " $ $ $ $ $ $ % % % % % % % % % % % %            0 0 0 0 0 0 0 0:0 0 0 0 0 0 0 0@ :    "*% *% *%Zo( o( o( o( o( o( o( o(d z.69*=>>Y Y Y Y YH Y Y Y Y Yr   