o
    i24                     @   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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 )
RecordLLVMPassTimingsLegacy9A helper context manager to track LLVM pass timings.
    _datac                 C   s   t d | S ))Enables the pass timing in LLVM.
        T)llvmset_time_passesself r   Z/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/numba/misc/llvm_pass_timings.py	__enter__      
z%RecordLLVMPassTimingsLegacy.__enter__c                 C   s   t  | _t d dS )2Reset timings and save report internally.
        FN)r   report_and_reset_timingsr	   r   r   exc_valexc_typeexc_tbr   r   r   __exit__   s   

z$RecordLLVMPassTimingsLegacy.__exit__c                 C   
   t | jS ztRetrieve timing data for processing.

        Returns
        -------
        timings: ProcessedPassTimings
        ProcessedPassTimingsr	   r   r   r   r   get!      
zRecordLLVMPassTimingsLegacy.getN)__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   r      s    r   c                   @   s8   e Zd ZdZddgZdd Zdd Zdd	 Zd
d ZdS )RecordLLVMPassTimingsr   r	   _pbc                 C   s   || _ d | _d S N)r&   r	   )r   pbr   r   r   __init__1      
zRecordLLVMPassTimings.__init__c                 C   s   | j   | S )r
   )r&   start_pass_timingr   r   r   r   r   5   r   zRecordLLVMPassTimings.__enter__c                 C   s   | j  | _dS )r   N)r&   finish_pass_timingr	   r   r   r   r   r   ;   s   zRecordLLVMPassTimings.__exit__c                 C   r   r   r   r   r   r   r   r   A   r   zRecordLLVMPassTimings.getN)	r    r!   r"   r#   r$   r)   r   r   r   r   r   r   r   r%   +   s    r%   PassTimingRecord)
	user_timeuser_percentsystem_timesystem_percentuser_system_timeuser_system_percent	wall_timewall_percent	pass_nameinstructionc                    s\   | d j dksJ fddfdddD  tdd	 | } fd
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                    s4   |  d|  d t  fdd}|S )N_time_percentc                    s$   }||    d }|| < | S )z'Compute percent x total_time = adjustedg{Gz?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)attrrC   )rB   )r?   r@   rA   r   make_adjusteri   s
   


z&_adjust_timings.<locals>.make_adjusterc                    s   g | ]} |qS r   r   ).0x)rG   r   r   
<listcomp>x   s    z#_adjust_timings.<locals>.<listcomp>)usersystemuser_systemwallc                 S      |   S r'   )_asdictrI   r   r   r   <lambda>}   s    z!_adjust_timings.<locals>.<lambda>c                    s     D ]}|| } qt di | S )Nr   )r-   )r<   fn)adj_fnsr   r   chained   s   
z _adjust_timings.<locals>.chained)r6   maplist)recordsdictsrU   r   )rT   rG   rB   r   _adjust_timings\   s   

rZ   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   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                 C   s
   || _ d S r'   	_raw_data)r   raw_datar   r   r   r)         
zProcessedPassTimings.__init__c                 C   r   r'   )boolr\   r   r   r   r   __bool__   r^   zProcessedPassTimings.__bool__c                 C      | j S )zWReturns the raw string data.

        Returns
        -------
        res: str
        r[   r   r   r   r   get_raw_data      z!ProcessedPassTimings.get_raw_datac                 C   s   |   d jS )zhCompute the total time spend in all passes.

        Returns
        -------
        res: float
        r8   )list_recordsr4   r   r   r   r   get_total_time   s   z#ProcessedPassTimings.get_total_timec                 C   ra   )z{Get the processed data for the timing report.

        Returns
        -------
        res: List[PassTimingRecord]
        )
_processedr   r   r   r   rd      rc   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.
        r4   Nr8   )rd   rD   rE   heapqnlargest)r   nrX   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  q!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                    s      |   d S r'   )append)argbufprefixr   r   ap   s   z(ProcessedPassTimings.summary.<locals>.apzTotal .4fszTop timings:z  zs (5z%) 
)re   rk   r4   r5   r6   join)r   topnindentrs   pr   rp   r   summary   s   &
zProcessedPassTimings.summaryc                 C   rO   )ztA cached property for lazily processing the data and returning it.

        See ``_process()`` for details.
        )_processr   r   r   r   rf      s   zProcessedPassTimings._processedc                 C   s   d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   s   |   }d}d| d}t|}dddddd	d
 |D ]}t||}|r7td|} fdd|D } nq|d d	ks@J g }	d}
d}|dd D ]&}|dkrW||
7 }qL|	| d |	| d |d|
 d|
 d7 }qLi }tjD ]}||	vr|d	krd||< qx|d7 }|D ]8}t||}|durt|	 } dd t
|	| D }|| | d }tdd	|i|}|V  |jdkr nqd|}d|v rdS |rt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-+)+rK   rL   rM   rN   r7   r6   )z	User TimezSystem TimezUser+Systemz	Wall TimeInstrNamez[a-zA-Z][a-zA-Z+ ]+c                    s   g | ]} |   qS r   )strip)rH   k
header_mapr   r   rJ     s    z@ProcessedPassTimings._process.<locals>.parse.<locals>.<listcomp>r8   z\s*((?:[0-9]+\.)?[0-9]+) Nr:   r;   z\s+(?:z\s*\(z%\)|-+)        z\s*(.*)c                 S   s&   i | ]\}}||d urt |ndqS )Nr   )float)rH   r   vr   r   r   
<dictcomp>  s    z@ProcessedPassTimings._process.<locals>.parse.<locals>.<dictcomp>r9   rw   z Analysis execution timing reportz'unexpected text after parser finished:
r   )
splitlinesiterrematchfindallrn   r-   _fieldsrW   groupszipupdater6   rx   
ValueError)r]   lines	colheadermulticolheaders	line_iterlnmraw_headersheadersattrsri   patr   missingdatar6   rec	remainingr   r   r   parse   sz   




z,ProcessedPassTimings._process.<locals>.parse)rW   r\   rZ   )r   r   rX   r   r   r   r}      s   KzProcessedPassTimings._processN)rl   r   )r    r!   r"   r#   r)   r`   rb   re   rd   rk   r|   r   rf   r}   r   r   r   r   r      s    			

r   NamedTimingsnametimingsc                   @   sv   e Zd ZdZdd Ze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 )PassTimingsCollectionzA collection of pass timings.

    This class implements the ``Sequence`` protocol for accessing the
    individual timing records.
    c                 C   s   || _ g | _d S r'   )_name_records)r   r   r   r   r   r)   E  r*   zPassTimingsCollection.__init__c                 c   s^    t jr*t }dV  W d   n1 s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   r   r   r   record_legacyI  s   
z#PassTimingsCollection.record_legacyc                 c   s`    t jr+t|}dV  W d   n1 s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   r   r   r   recordb  s   

zPassTimingsCollection.recordc                 C   s   | j t|| dS )zAppend timing records

        Parameters
        ----------
        name: str
            Name for the records.
        timings: ProcessedPassTimings
            the timing records.
        N)r   rn   r   )r   r   r   r   r   r   r   {  s   
zPassTimingsCollection._appendc                 C   s   | j rtd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                 s   s    | ]}|j  V  qd S r'   r   re   )rH   rr   r   r   	<genexpr>  s    z7PassTimingsCollection.get_total_time.<locals>.<genexpr>N)r   sumr   r   r   r   re     s   	z$PassTimingsCollection.get_total_timec                 C   s   t | jdd ddS )zReturns the timings in descending order of total time duration.

        Returns
        -------
        res: List[ProcessedPassTimings]
        c                 S   s
   | j  S r'   r   rQ   r   r   r   rR     s   
 z:PassTimingsCollection.list_longest_first.<locals>.<lambda>T)rj   reverse)sortedr   r   r   r   r   list_longest_first  s   z(PassTimingsCollection.list_longest_firstc                 C   s   | j  S )z	
        r   r   r   r   r   is_empty  s   zPassTimingsCollection.is_emptyrl   c                 C   s   | j rdS g }|j}|d| j  |  }|d|d t| jD ]+\}}|d| d|j  |j | d }|d|d	d
 ||jj|dd q#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: rt   z== #rm   d   z
 Percent: z.1f%   )ry   rz   rw   )
r   rn   r   re   	enumerater   r   r   r|   rx   )r   ry   rq   rs   overall_timeir   percentr   r   r   r|     s   
zPassTimingsCollection.summaryc                 C   s
   | 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   r   r   r   __getitem__  s   
z!PassTimingsCollection.__getitem__c                 C   r   )z#Length of this collection.
        )lenr   r   r   r   r   __len__  s   
zPassTimingsCollection.__len__c                 C   rO   r'   )r|   r   r   r   r   __str__  s   zPassTimingsCollection.__str__N)rl   )r    r!   r"   r#   r)   r   r   r   r   re   r   propertyr   r|   r   r   r   r   r   r   r   r   >  s     



r   )r   rD   rg   collectionsr   collections.abcr   
contextlibr   	functoolsr   
numba.corer   llvmlite.bindingbindingr   r   r%   r-   rZ   r   r   r   r   r   r   r   <module>   s(     - 3