
    J/Ph-                         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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 )RecordLLVMPassTimingsz9A helper context manager to track LLVM pass timings.
    _datac                 .    t          j        d           | S )z)Enables the pass timing in LLVM.
        T)llvmset_time_passesselfs    \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/misc/llvm_pass_timings.py	__enter__zRecordLLVMPassTimings.__enter__   s     	T"""    c                 ^    t          j                    | _        t          j        d           dS )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44
U###r   c                 *    t          | j                  S )ztRetrieve timing data for processing.

        Returns
        -------
        timings: ProcessedPassTimings
        )ProcessedPassTimingsr
   r   s    r   getzRecordLLVMPassTimings.get!   s     $DJ///r   N)__name__
__module____qualname____doc__	__slots__r   r   r    r   r   r	   r	      sT          	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)attrr;   r7   r8   r9   r:   s     @@@r   make_adjusterz&_adjust_timings.<locals>.make_adjusterI   s^    NNN	((())44	 	 	 	 	 	 	 	 r   c                 &    g | ]} |          S r!   r!   ).0xr?   s     r   
<listcomp>z#_adjust_timings.<locals>.<listcomp>X   s0       a  r   )usersystemuser_systemwallc                 *    |                                  S N)_asdictrB   s    r   <lambda>z!_adjust_timings.<locals>.<lambda>]   s    !))++ r   c                 <    D ]} ||           } t          di | S )Nr!   )r"   )r4   fnadj_fnss     r   chainedz _adjust_timings.<locals>.chained_   s5     	 	B1AA$$!$$$r   )r+   maplist)recordsdictsrP   rO   r?   r:   s      @@@r   _adjust_timingsrU   <   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 rI   	_raw_data)r   raw_datas     r   __init__zProcessedPassTimings.__init__p   s    !r   c                 *    t          | j                  S rI   )boolrY   r   s    r   __bool__zProcessedPassTimings.__bool__s   s    DN###r   c                     | j         S )zWReturns the raw string data.

        Returns
        -------
        res: str
        rX   r   s    r   get_raw_dataz!ProcessedPassTimings.get_raw_datav   s     ~r   c                 @    |                                  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0r   c                     | j         S )z{Get the processed data for the timing report.

        Returns
        -------
        res: List[PassTimingRecord]
        )
_processedr   s    r   rb   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)   Nr.   )rb   r<   r=   heapqnlargest)r   nrS   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 rI   )append)argbufprefixs    r   apz(ProcessedPassTimings.summary.<locals>.ap   s&    JJ&'#''(((((r   zTotal .4fszTop timings:z  zs (5z%) 
)rc   rk   r)   r*   r+   join)r   topnindentrt   prr   rs   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   re   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                    |          }|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-+)+rD   rE   rF   rG   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)rA   k
header_maps     r   rC   z@ProcessedPassTimings._process.<locals>.parse.<locals>.<listcomp>   s&    JJJz!''))4JJJr   r.   z\s*((?:[0-9]+\.)?[0-9]+) Nr1   r2   z\s+(?:z\s*\(z%\)|-+)        z\s*(.*)c                 <    i | ]\  }}||t          |          ndS )Nr   )float)rA   r   vs      r   
<dictcomp>z@ProcessedPassTimings._process.<locals>.parse.<locals>.<dictcomp>   s<     > > > $1 1=uQxxxc > > >r   r/   rx   z'unexpected text after parser finished:
r!   )
splitlinesiterrematchfindallrp   r"   _fieldsrR   groupszipupdater+   ry   
ValueError)rZ   lines	colheadermulticolheaders	line_iterlnmraw_headersheadersattrsri   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  JyJJ   r   )rR   rY   rU   )r   r   rS   s      r   r   zProcessedPassTimings._process   s>    C	 C	 C	L uuT^,,--w'''r   N)rl   r   )r   r   r   r   r[   r^   r`   rc   rb   rk   r}   r   re   r   r!   r   r   r   r   i   s         " " "$ $ $  1 1 1  4 4 4$   6   _M( M( M( M( M(r   r   NamedTimingsnametimingsc                   p    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dS )PassTimingsCollectionzA collection of pass timings.

    This class implements the ``Sequence`` protocol for accessing the
    individual timing records.
    c                 "    || _         g | _        d S rI   )_name_records)r   r   s     r   r[   zPassTimingsCollection.__init__   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 ``RecordLLVMPassTimings``

        Parameters
        ----------
        name: str
            Name for the records.
        N)r   LLVM_PASS_TIMINGSr	   r   _append)r   r   r   r   s       r   recordzPassTimingsCollection.record$  s       # 
	&(( G              ++--C (T3'''''( ( EEEEEs   .22c                 V    | j                             t          ||                     dS )zAppend timing records

        Parameters
        ----------
        name: str
            Name for the records.
        timings: ProcessedPassTimings
            the timing records.
        N)r   rp   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 rI   r   rc   )rA   rs     r   	<genexpr>z7PassTimingsCollection.get_total_time.<locals>.<genexpr>S  s0      IIaqy//11IIIIIIr   N)r   sumr   s    r   rc   z$PassTimingsCollection.get_total_timeI  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 rI   r   rK   s    r   rL   z:PassTimingsCollection.list_longest_first.<locals>.<lambda>_  s    AI$<$<$>$> r   T)rj   reverse)sortedr   r   s    r   list_longest_firstz(PassTimingsCollection.list_longest_firstW  s(     dm>>"$ $ $ 	$r   c                     | j          S )z	
        r   r   s    r   is_emptyzPassTimingsCollection.is_emptyb  s     =  r   rl   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: ru   z== #rn   d   z
 Percent: z.1f%   )rz   r{   rx   )
r   rp   r   rc   	enumerater   r   r   r}   ry   )r   rz   rr   rt   overall_timeir   percents           r   r}   zPassTimingsCollection.summaryh  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 rI   )r}   r   s    r   __str__zPassTimingsCollection.__str__  s    ||~~r   N)rl   )r   r   r   r   r[   r   r   r   rc   r   propertyr   r}   r   r   r   r!   r   r   r   r     s              ^0
: 
: 
:  	$ 	$ 	$ ! ! X!
" " " "<     " " "
    r   r   )r   r<   rg   collectionsr   collections.abcr   
contextlibr   	functoolsr   
numba.corer   llvmlite.bindingbindingr   r	   r"   rU   r   r   r   r!   r   r   <module>r      sq   				   " " " " " " $ $ $ $ $ $ % % % % % % % % % % % %            0 0 0 0 0 0 0 0: :    "*% *% *%Zj( j( j( j( j( j( j( j(Z z.69*=>>@ @ @ @ @H @ @ @ @ @r   