U
    hâËdº-  ã                   @   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   úU/home/sam/Atlas/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	   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   Ú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)r/   r   r   Ú
<listcomp>X   s    z#_adjust_timings.<locals>.<listcomp>)ÚuserÚsystemÚuser_systemÚwallc                 S   s   |   ¡ S ©N)Ú_asdict©r1   r   r   r   Ú<lambda>]   ó    z!_adjust_timings.<locals>.<lambda>c                    s   ˆ D ]}|| ƒ} qt f | ŽS r7   )r   )r$   Úfn)Úadj_fnsr   r   Úchained_   s    
z _adjust_timings.<locals>.chained)r   ÚAssertionErrorÚmapÚlist)ÚrecordsZdictsr>   r   )r=   r/   r*   r   Ú_adjust_timings<   s    

ÿrC   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 r7   ©Ú	_raw_data)r   Úraw_datar   r   r   Ú__init__p   s    zProcessedPassTimings.__init__c                 C   s
   t | jƒS r7   )ÚboolrE   r   r   r   r   Ú__bool__s   s    zProcessedPassTimings.__bool__c                 C   s   | j S )zWReturns the raw string data.

        Returns
        -------
        res: str
        rD   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   rK   ˆ   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    )rK   r,   r-   ÚheapqÚnlargest)r   ÚnrB   Ú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 r7   )Úappend)Úarg©ÚbufÚprefixr   r   Úapµ   s    z(ProcessedPassTimings.summary.<locals>.apzTotal ú.4fÚszTop timings:z  zs (Ú5z%) Ú
)rL   rR   r   r   r   Újoin)r   ÚtopnÚindentrZ   Úpr   rW   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   rM   ¾   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-+)+r3   r4   r5   r6   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)r0   Úk©Z
header_mapr   r   r2   ä   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 )Nrj   )Úfloat)r0   rg   Ú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?   rU   r   Ú_fieldsrA   ÚgroupsÚzipÚupdater   r_   Ú
ValueError)rF   ÚlinesZ	colheaderZmulticolheadersZ	line_iterÚlnÚmZraw_headersÚheadersÚattrsrP   Úpatrg   ÚmissingÚdatar   ÚrecÚ	remainingr   rh   r   ÚparseÌ   sl    ú



ÿ
ÿÿ

ÿz,ProcessedPassTimings._process.<locals>.parse)rA   rE   rC   )r   r‚   rB   r   r   r   rd   Æ   s    FzProcessedPassTimings._processN)rS   r   )r   r   r   r   rG   rI   rJ   rL   rK   rR   rc   r   rM   rd   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 r7   )Ú_nameÚ_records)r   r„   r   r   r   rG      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   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ˆ   rU   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 r7   ©r…   rL   )r0   Úrr   r   r   Ú	<genexpr>S  s     z7PassTimingsCollection.get_total_time.<locals>.<genexpr>N)rˆ   Úsumr   r   r   r   rL   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 r7   r‹   r9   r   r   r   r:   _  r;   z:PassTimingsCollection.list_longest_first.<locals>.<lambda>T)rQ   Ú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_emptyrS   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== #rT   éd   z
 Percent: z.1fú%é   )r`   ra   r^   N)
r“   rU   r‡   rL   Ú	enumeraterˆ   r„   r…   rc   r_   )r   r`   rX   rZ   Zoverall_timeÚirŒ   Úpercentr   r   r   rc   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 r7   )rc   r   r   r   r   Ú__str__˜  s    zPassTimingsCollection.__str__N)rS   )r   r   r   r   rG   r   rŠ   r‰   rL   r‘   Úpropertyr“   rc   rš   rœ   r�   r   r   r   r   r†     s   


r†   )rp   r,   rN   Úcollectionsr   Úcollections.abcr   Ú
contextlibr   Ú	functoolsr   Z
numba.corer   Zllvmlite.bindingZbindingr	   r   r   rC   r   rƒ   r†   r   r   r   r   Ú<module>   s8   öþ- .