U
    Ô¾|eº-  ã                   @   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	d
dddddddddg
ƒ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   úY/var/www/website-v5/atlas_env/lib/python3.8/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	   Ú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   ÚPassTimingRecordZ	user_timeZuser_percentZsystem_timeZsystem_percentZuser_system_timeZuser_system_percentÚ	wall_timeÚwall_percentÚ	pass_nameÚinstructionc                    s\   | d ‰ˆj dkst‚‡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   ÚadjustN   s    z6_adjust_timings.<locals>.make_adjuster.<locals>.adjust)ÚoperatorÚ
attrgetter)Úattrr,   )r+   )r(   r)   r*   r   Úmake_adjusterI   s
    


z&_adjust_timings.<locals>.make_adjusterc                    s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0Úx)r0   r   r   Ú
<listcomp>X   s    z#_adjust_timings.<locals>.<listcomp>)ÚuserÚsystemÚuser_systemÚwallc                 S   s   |   ¡ S ©N)Ú_asdict©r2   r   r   r   Ú<lambda>]   ó    z!_adjust_timings.<locals>.<lambda>c                    s   ˆ D ]}|| ƒ} qt f | ŽS r8   )r   )r%   Úfn)Úadj_fnsr   r   Úchained_   s    
z _adjust_timings.<locals>.chained)r   ÚAssertionErrorÚmapÚlist)ÚrecordsÚdictsr?   r   )r>   r0   r+   r   Ú_adjust_timings<   s    

ÿrE   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 r8   ©Ú	_raw_data)r   Úraw_datar   r   r   Ú__init__p   s    zProcessedPassTimings.__init__c                 C   s
   t | jƒS r8   )ÚboolrG   r   r   r   r   Ú__bool__s   s    zProcessedPassTimings.__bool__c                 C   s   | j S )zWReturns the raw string data.

        Returns
        -------
        res: str
        rF   r   r   r   r   Úget_raw_datav   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   rM   ˆ   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!   )rM   r-   r.   ÚheapqÚnlargest)r   ÚnrC   Ú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 r8   )Úappend)Úarg©ÚbufÚprefixr   r   Úapµ   s    z(ProcessedPassTimings.summary.<locals>.apzTotal ú.4fÚszTop timings:z  zs (Ú5z%) Ú
)rN   rT   r   r   r   Újoin)r   ÚtopnÚindentr\   Úpr   rY   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   rO   ¾   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
œ‰ |D ]6}t ||¡}|r6t d|¡}‡ fdd„|D ƒ} qnq6|d d	ks~t‚g }	d}
d}|dd… D ]L}|dkr¬||
7 }q–|	 |› d�¡ |	 |› d�¡ |d|
› d|
› d�7 }q–i }tjD ]}||	krî|d	krîd||< qî|d7 }|D ]x}t ||¡}|dk	�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-+)+r4   r5   r6   r7   r    r   )z	User TimezSystem TimezUser+Systemz	Wall TimeZInstrÚNamez[a-zA-Z][a-zA-Z+ ]+c                    s   g | ]}ˆ |  ¡  ‘qS r   )Ústrip)r1   Úk©Z
header_mapr   r   r3   ä   s     z@ProcessedPassTimings._process.<locals>.parse.<locals>.<listcomp>r!   z\s*((?:[0-9]+\.)?[0-9]+)Ú Nr#   r$   z\s+(?:z\s*\(z%\)|-+)ç        z\s*(.*)c                 S   s&   i | ]\}}||d k	rt |ƒnd“qS )Nrl   )Úfloat)r1   ri   Úvr   r   r   Ú
<dictcomp>ÿ   s   ÿ z@ProcessedPassTimings._process.<locals>.parse.<locals>.<dictcomp>r"   r`   z'unexpected text after parser finished:
)Ú
splitlinesÚiterÚreÚmatchÚfindallr@   rW   r   Ú_fieldsrB   ÚgroupsÚzipÚupdater   ra   Ú
ValueError)rH   ÚlinesZ	colheaderZmulticolheadersZ	line_iterÚlnÚmZraw_headersÚheadersÚattrsrR   Úpatri   ÚmissingÚdatar   ÚrecÚ	remainingr   rj   r   ÚparseÌ   sl    ú



ÿ
ÿÿ

ÿz,ProcessedPassTimings._process.<locals>.parse)rB   rG   rE   )r   r„   rC   r   r   r   rf   Æ   s    FzProcessedPassTimings._processN)rU   r   )r   r   r   r   rI   rK   rL   rN   rM   rT   re   r   rO   rf   r   r   r   r   r   i   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 r8   )Ú_nameÚ_records)r   r†   r   r   r   rI      s    zPassTimingsCollection.__init__c              	   c   sB   t jr8tƒ �}dV  W 5 Q R X | ¡ }|r>|  ||¡ 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   Ú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Š   rW   r…   )r   r†   r‡   r   r   r   rŒ   =  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 r8   ©r‡   rN   )r1   Úrr   r   r   Ú	<genexpr>S  s     z7PassTimingsCollection.get_total_time.<locals>.<genexpr>N)rŠ   Úsumr   r   r   r   rN   I  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 r8   rŽ   r:   r   r   r   r;   _  r<   z:PassTimingsCollection.list_longest_first.<locals>.<lambda>T)rS   Úreverse)ÚsortedrŠ   r   r   r   r   Úlist_longest_firstW  s    þz(PassTimingsCollection.list_longest_firstc                 C   s   | j  S )z	
        ©rŠ   r   r   r   r   Úis_emptyb  s    zPassTimingsCollection.is_emptyrU   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: r]   z== #rV   éd   z
 Percent: z.1fú%é   )rb   rc   r`   N)
r–   rW   r‰   rN   Ú	enumeraterŠ   r†   r‡   re   ra   )r   rb   rZ   r\   Zoverall_timeÚir�   Úpercentr   r   r   re   h  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.
        )ÚlenrŠ   r   r   r   r   Ú__len__“  s    zPassTimingsCollection.__len__c                 C   s   |   ¡ S r8   )re   r   r   r   r   Ú__str__˜  s    zPassTimingsCollection.__str__N)rU   )r   r   r   r   rI   r   r�   rŒ   rN   r”   Úpropertyr–   re   r�   rŸ   r    r   r   r   r   rˆ     s   


rˆ   )rr   r-   rP   Úcollectionsr   Úcollections.abcr   Ú
contextlibr   Ú	functoolsr   Ú
numba.corer   Úllvmlite.bindingÚbindingr	   r   r   rE   r   r…   rˆ   r   r   r   r   Ú<module>   s8   öþ- .