a
    –§»a-  ã                   @   s´   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„ dƒZed	g d
¢ƒZdd„ ZG dd„ dƒZedddgƒZG dd„ deƒZdS )é    N)Ú
namedtuple)ÚSequence)Úcontextmanager)Úcached_property)Úconfigc                   @   s.   e Zd ZdZdgZdd„ Zdd„ Zdd„ Zd	S )
ÚRecordLLVMPassTimingsz9A helper context manager to track LLVM pass timings.
    Ú_datac                 C   s   t  d¡ | S )z)Enables the pass timing in LLVM.
        T)ÚllvmÚset_time_passes©Úself© r   úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/misc/llvm_pass_timings.pyÚ	__enter__   s    
zRecordLLVMPassTimings.__enter__c                 C   s   t  ¡ | _t  d¡ dS )z2Reset timings and save report internally.
        FN)r	   Zreport_and_reset_timingsr   r
   )r   Úexc_valÚexc_typeÚexc_tbr   r   r   Ú__exit__   s    

zRecordLLVMPassTimings.__exit__c                 C   s
   t | jƒS )ztRetrieve timing data for processing.

        Returns
        -------
        timings: ProcessedPassTimings
        )ÚProcessedPassTimingsr   r   r   r   r   Úget!   s    zRecordLLVMPassTimings.getN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r   r   r   r   r      s
   r   ÚPassTimingRecord)	Z	user_timeZuser_percentZsystem_timeZsystem_percentZuser_system_timeZuser_system_percentÚ	wall_timeÚwall_percentÚ	pass_namec                    s\   | d ‰ˆj dksJ ‚‡fdd„‰‡fdd„dD ƒ‰ tdd	„ | ƒ}‡ fd
d„}tt||ƒƒS )zÎAdjust timing records because of truncated information.

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

    Returns
    -------
    res: List[PassTimingRecord]
    éÿÿÿÿÚTotalc                    s4   | › d�‰| › d�‰ t  ˆ¡‰‡ ‡‡‡fdd„}|S )NÚ_timeÚ_percentc                    s$   ˆˆƒ}|| ˆ   d }|| ˆ< | S )z'Compute percent x total_time = adjustedg{®Gáz„?r   )ÚdÚtotalÚadjusted)Úpercent_attrÚ	time_attrÚtime_getterÚ	total_recr   r   ÚadjustM   s    z6_adjust_timings.<locals>.make_adjuster.<locals>.adjust)ÚoperatorÚ
attrgetter)Úattrr*   )r)   )r&   r'   r(   r   Úmake_adjusterH   s
    


z&_adjust_timings.<locals>.make_adjusterc                    s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0Úx)r.   r   r   Ú
<listcomp>W   s   z#_adjust_timings.<locals>.<listcomp>)ÚuserÚsystemÚuser_systemÚwallc                 S   s   |   ¡ S ©N)Ú_asdict©r0   r   r   r   Ú<lambda>\   ó    z!_adjust_timings.<locals>.<lambda>c                    s    ˆ D ]}|| ƒ} qt f i | ¤ŽS r6   )r   )r#   Úfn)Úadj_fnsr   r   Úchained^   s    
z _adjust_timings.<locals>.chained)r   ÚmapÚlist)ÚrecordsZdictsr=   r   )r<   r.   r)   r   Ú_adjust_timings;   s    

ÿrA   c                   @   s^   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	ddd„Z
edd„ ƒZdd„ ZdS )r   zœA class for processing raw timing report from LLVM.

    The processing is done lazily so we don't waste time processing unused
    timing information.
    c                 C   s
   || _ d S r6   ©Ú	_raw_data)r   Úraw_datar   r   r   Ú__init__o   s    zProcessedPassTimings.__init__c                 C   s
   t | jƒS r6   )ÚboolrC   r   r   r   r   Ú__bool__r   s    zProcessedPassTimings.__bool__c                 C   s   | j S )zWReturns the raw string data.

        Returns
        -------
        res: str
        rB   r   r   r   r   Úget_raw_datau   s    z!ProcessedPassTimings.get_raw_datac                 C   s   |   ¡ d jS )zhCompute the total time spend in all passes.

        Returns
        -------
        res: float
        r   )Úlist_recordsr   r   r   r   r   Úget_total_time~   s    z#ProcessedPassTimings.get_total_timec                 C   s   | j S )z{Get the processed data for the timing report.

        Returns
        -------
        res: List[PassTimingRecord]
        )Ú
_processedr   r   r   r   rI   ‡   s    z!ProcessedPassTimings.list_recordsc                 C   s(   |   ¡ }t d¡}t ||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   )rI   r+   r,   ÚheapqÚnlargest)r   Únr@   Úkeyr   r   r   Úlist_top�   s    
zProcessedPassTimings.list_topé   r   c                    sv   g ‰ d| ‰‡ ‡fdd„}|d|   ¡ d›d�ƒ |dƒ |  |¡D ](}|d|jd›d	|jd
›d|j› �ƒ qBd ˆ ¡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                    s   ˆ   ˆ› | › �¡ d S r6   )Úappend)Úarg©ÚbufÚprefixr   r   Úap´   s    z(ProcessedPassTimings.summary.<locals>.apzTotal ú.4fÚszTop timings:z  zs (Ú5z%) Ú
)rJ   rP   r   r   r   Újoin)r   ÚtopnÚindentrX   Úpr   rU   r   Úsummary¢   s    &zProcessedPassTimings.summaryc                 C   s   |   ¡ S )ztA cached property for lazily processing the data and returning it.

        See ``_process()`` for details.
        )Ú_processr   r   r   r   rK   ½   s    zProcessedPassTimings._processedc                 C   s   dd„ }t || jƒƒ}t|ƒS )z¡Parses the raw string data from LLVM timing report and attempts
        to improve the data by recomputing the times
        (See `_adjust_timings()``).
        c                 3   s¤  |   ¡ }d}d|› d�}t|ƒ}dddddd	œ‰ |D ]6}t ||¡}|r4t d
|¡}‡ fdd„|D ƒ} qlq4|d dks|J ‚g }	|dd… D ]$}
|	 |
› d�¡ |	 |
› d�¡ qŒi }tjD ]}
|
|	vr¼|
dkr¼d||
< q¼d}d|› d|› d�t|ƒd  }|d7 }|D ]x}t ||¡}|du�rt	| 
¡ ƒ} dd„ t|	| ƒD ƒ}| |¡ | d }tf d|i|¤Ž}|V  |jdk�r �q‚�q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-+)+r2   r3   r4   r5   r   )z	User TimezSystem TimezUser+Systemz	Wall TimeÚNamez[a-zA-Z][a-zA-Z+ ]+c                    s   g | ]}ˆ |  ¡  ‘qS r   )Ústrip)r/   Úk©Z
header_mapr   r   r1   â   r:   z@ProcessedPassTimings._process.<locals>.parse.<locals>.<listcomp>r   Nr!   r"   ç        z\s*((?:[0-9]+\.)?[0-9]+)z\s+(?:z\s*\(z%\)|-+)é   z\s*(.*)c                 S   s&   i | ]\}}||d urt |ƒnd“qS )Nrg   )Úfloat)r/   re   Úvr   r   r   Ú
<dictcomp>ø   s   ÿz@ProcessedPassTimings._process.<locals>.parse.<locals>.<dictcomp>r    r\   z'unexpected text after parser finished:
)Ú
splitlinesÚiterÚreÚmatchÚfindallrS   r   Ú_fieldsÚlenr?   ÚgroupsÚzipÚupdater   r]   Ú
ValueError)rD   ÚlinesZ	colheaderZmulticolheadersZ	line_iterÚlnÚmZraw_headersÚheadersÚattrsre   ÚmissingrN   ÚpatÚdatar   ÚrecÚ	remainingr   rf   r   ÚparseË   sd    û


ÿ
ÿÿ

ÿz,ProcessedPassTimings._process.<locals>.parse)r?   rC   rA   )r   r�   r@   r   r   r   rb   Å   s    @zProcessedPassTimings._processN)rQ   r   )r   r   r   r   rE   rG   rH   rJ   rI   rP   ra   r   rK   rb   r   r   r   r   r   h   s   			

r   ÚNamedTimingsÚnameÚtimingsc                   @   sj   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	e
dd„ ƒZddd„Zdd„ Zdd„ Zdd„ ZdS )ÚPassTimingsCollectionzˆA collection of pass timings.

    This class implements the ``Sequence`` protocol for accessing the
    individual timing records.
    c                 C   s   || _ g | _d S r6   )Ú_nameÚ_records)r   rƒ   r   r   r   rE     s    zPassTimingsCollection.__init__c                 c   sV   t jrLtƒ �}dV  W d  ƒ n1 s(0    Y  | ¡ }|rR|  ||¡ n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   ZLLVM_PASS_TIMINGSr   r   Ú_append)r   rƒ   r„   r   r   r   r   Úrecord  s    $zPassTimingsCollection.recordc                 C   s   | j  t||ƒ¡ dS )z¿Append timing records

        Parameters
        ----------
        name: str
            Name for the records.
        timings: ProcessedPassTimings
            the timing records.
        N)r‡   rS   r‚   )r   rƒ   r„   r   r   r   rˆ   6  s    
zPassTimingsCollection._appendc                 C   s"   | j rtdd„ | j D ƒƒS dS dS )zåComputes 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                 s   s   | ]}|j  ¡ V  qd S r6   ©r„   rJ   )r/   Úrr   r   r   Ú	<genexpr>L  r:   z7PassTimingsCollection.get_total_time.<locals>.<genexpr>N)r‡   Úsumr   r   r   r   rJ   B  s    	z$PassTimingsCollection.get_total_timec                 C   s   t | jdd„ dd�S )z‘Returns the timings in descending order of total time duration.

        Returns
        -------
        res: List[ProcessedPassTimings]
        c                 S   s
   | j  ¡ S r6   rŠ   r8   r   r   r   r9   X  r:   z:PassTimingsCollection.list_longest_first.<locals>.<lambda>T)rO   Úreverse)Úsortedr‡   r   r   r   r   Úlist_longest_firstP  s    þz(PassTimingsCollection.list_longest_firstc                 C   s   | j  S )z	
        ©r‡   r   r   r   r   Úis_empty[  s    zPassTimingsCollection.is_emptyrQ   c                 C   s¬   | j r
dS g }|j}|d| j› �ƒ |  ¡ }|d|d›�ƒ t| jƒD ]V\}}|d|› d|j› �ƒ |j ¡ | d }|d|d	›d
�ƒ ||jj|dd�ƒ qFd 	|¡S d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: rY   z== #rR   éd   z
 Percent: z.1fú%rh   )r^   r_   r\   N)
r’   rS   r†   rJ   Ú	enumerater‡   rƒ   r„   ra   r]   )r   r^   rV   rX   Zoverall_timeÚir‹   Úpercentr   r   r   ra   a  s    zPassTimingsCollection.summaryc                 C   s
   | j | S )zÒGet the i-th timing record.

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

            - name: str
            - timings: ProcessedPassTimings
        r‘   )r   r–   r   r   r   Ú__getitem__  s    z!PassTimingsCollection.__getitem__c                 C   s
   t | jƒS )z#Length of this collection.
        )rr   r‡   r   r   r   r   Ú__len__Œ  s    zPassTimingsCollection.__len__c                 C   s   |   ¡ S r6   )ra   r   r   r   r   Ú__str__‘  s    zPassTimingsCollection.__str__N)rQ   )r   r   r   r   rE   r   r‰   rˆ   rJ   r�   Úpropertyr’   ra   r˜   r™   rš   r   r   r   r   r…     s   


r…   )rn   r+   rL   Úcollectionsr   Úcollections.abcr   Ú
contextlibr   Znumba.core.utilsr   Z
numba.corer   Zllvmlite.bindingZbindingr	   r   r   rA   r   r‚   r…   r   r   r   r   Ú<module>   s$   þ- (