§
    ƒÛgº-  ã                   óô   — 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    úR/granary/edev/quantum/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   € õ $ D¤JÑ/Ô/Ð/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 )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                 óZ   •‡‡‡— | › d�Š| › d�Št          j        ‰¦  «        Šˆˆˆˆfd„}|S )NÚ_timeÚ_percentc                 óD   •—  ‰‰¦  «        }|| ‰         z  dz  }|| ‰<   | S )z'Compute percent x total_time = adjustedg{®Gáz„?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 œÑ.°Ñ5ˆHØ#ˆAˆi‰LØˆHr   )ÚoperatorÚ
attrgetter)Úattrr;   r7   r8   r9   r:   s     @@@€r   Úmake_adjusterz&_adjust_timings.<locals>.make_adjusterI   s^   øøøø€ Ø�N�N�Nˆ	ØÐ(Ð(Ð(ˆÝÔ)¨)Ñ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‘”ˆAˆAåÐ$Ð$ !Ð$Ð$Ð$r   )r+   ÚmapÚlist)ÚrecordsÚdictsrP   rO   r?   r:   s      @@@r   Ú_adjust_timingsrU   <   s­   øøø€ ð ˜”€IØÔ 'Ò)Ð)Ð)Ð)ðð ð ð ð ðð ð ð Ø"Kðñ ô €Gõ
 Ð%Ð% wÑ/Ô/€Eð%ð %ð %ð %ð %õ •�G˜UÑ#Ô#Ñ$Ô$Ð$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   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                 ó   — || _         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   € Ý�D”NÑ#Ô#Ð#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&   ø€ Ø�JŠJ˜&Ð' #Ð'Ð'Ñ(Ô(Ð(Ð(Ð(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‘ˆð	)ð 	)ð 	)ð 	)ð 	)ð 	)ð 	ˆÐ0�D×'Ò'Ñ)Ô)Ð0Ð0Ð0Ð0Ñ1Ô1Ð1Ø
ˆˆ>ÑÔÐØ—’˜tÑ$Ô$ð 	Lð 	LˆAØˆBÐJ�A”KÐJÐJÐJ¨¬ÐJÐJÐJ¸Q¼[ÐJÐJÑKÔKÐKÐKØ�yŠy˜‰~Œ~Ð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 )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   óŠ  ‡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¨=�u Q™xœx˜x¸cð >ð >ð >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Ð9ˆOå˜U™œˆIð $Ø'Ø,Ø#Ø&Ø#ðð ˆJð  ð ð �Ý”H˜_¨bÑ1Ô1�Øð å"$¤*Ð-CÀRÑ"HÔ"H�KØJÐJÐJÐJ¸kÐJÑJÔJ�GØ�Eð	ð ˜2”; +Ò-Ð-Ð-Ð-àˆEØ+ˆAØˆCØ˜S˜b˜S”\ð ;ð ;�Ø˜Ò%Ð%Ø˜1‘H�C�Cà—L’L A   Ñ-Ô-Ð-Ø—L’L A   Ñ0Ô0Ð0ØÐ: QÐ:Ð:¨qÐ:Ð:Ð:Ñ:�C�Cð ˆGÝ%Ô-ð %ð %�Ø˜E�>�> a¨;Ò&6Ð&6Ø!$�G˜A‘Jøà�:ÑˆCØð ð �Ý”H˜S "Ñ%Ô%�Ø�=Ý# A§H¢H¡J¤JÑ/Ô/�Hð>ð >Ý(+¨E°8Ñ(<Ô(<ð>ñ >ô >�Dà—K’K Ñ(Ô(Ð(Ø (¨¤�IÝ*ð ð Ø"+ðØ/3ðð �Cð �I�I�IØ”}¨Ò/Ð/à˜øàŸ	š	 )Ñ,Ô,ˆIØð Ý ØJ¸yÐJÐJñô ð ðð r   )rR   rY   rU   )r   r¦   rS   s      r   r   zProcessedPassTimings._processÆ   s>   € ðC	ð C	ð C	õL �u�u˜Tœ^Ñ,Ô,Ñ-Ô-ˆÝ˜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 )ÚPassTimingsCollectionzˆA 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àð (Ø—’˜T 3Ñ'Ô'Ð'Ð'Ð'ð(ð (ð ˆEˆEˆEˆEˆEs   �.®2µ2c                 óV   — | j                              t          ||¦  «        ¦  «         dS )z¿Append 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 )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              3   óH   K  — | ]}|j                              ¦   «         V — Œd S rI   ©r©   rc   )rA   Úrs     r   ú	<genexpr>z7PassTimingsCollection.get_total_time.<locals>.<genexpr>S  s0   è è € ÐIÐI°a�q”y×/Ò/Ñ1Ô1ÐIÐIÐIÐIÐIÐIr   N)r®   Úsumr   s    r   rc   z$PassTimingsCollection.get_total_timeI  s2   € ð Œ=ð 	ÝÐIÐI¸4¼=ÐIÑIÔIÑIÔIÐIà�4r   c                 ó2   — t          | j        d„ d¬¦  «        S )z‘Returns 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   €  A¤I×$<Ò$<Ñ$>Ô$>€ r   T)rj   Úreverse)Úsortedr®   r   s    r   Úlist_longest_firstz(PassTimingsCollection.list_longest_firstW  s(   € õ �d”mØ>Ð>Ø"ð$ñ $ô $ð 	$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Ð8¨D¬JÐ8Ð8Ñ9Ô9Ð9Ø×.Ò.Ñ0Ô0ˆLØˆBÐ0˜lÐ0Ð0Ð0Ñ1Ô1Ð1Ý! $¤-Ñ0Ô0ð ;ð ;‘��1Ø�Ð&˜!Ð&Ð&˜aœfÐ&Ð&Ñ'Ô'Ð'Øœ)×2Ò2Ñ4Ô4°|ÑCÀcÑI�Ø�Ð. Ð.Ð.Ð.Ð.Ñ/Ô/Ð/Ø��1”9×$Ò$¨$°qÐ$Ñ9Ô9Ñ:Ô:Ð:Ð:Ø—9’9˜S‘>”>Ð!r   c                 ó   — | 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È   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˜.¨6°9Ð*=Ñ>Ô>€ð@ð @ð @ð @ð @˜Hñ @ô @ð @ð @ð @r   