o
    ۾i-8                     @   s\  d dl Z d dlmZmZ d dlmZmZ d dlZd dlm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZ d dlmZmZ d dlm  mZ e ZG d	d
 d
eZG dd dedZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"dZ#dd Z$eddZ%G dd deZ&edd Z'G d!d" d"eZ(e( Z)[(	 e)j*Z+dS )#    N)abstractmethodABCMeta)
namedtupleOrderedDict)global_compiler_lock)errorsconfig
transformsutils)event)PostProcessor)enforce_no_delslegalize_single_scopec                   @   s    e Zd ZdZdd Zdd ZdS )SimpleTimerz(
    A simple context managed timer
    c                 C   s   t  | _| S N)timeitdefault_timertsself r   Q/home/ubuntu/.local/lib/python3.10/site-packages/numba/core/compiler_machinery.py	__enter__   s   
zSimpleTimer.__enter__c                 G   s   t  | j | _d S r   )r   r   r   elapsed)r   excr   r   r   __exit__   s   zSimpleTimer.__exit__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZedd Zedd Zedd Z	e	j
dd Z	ed	d
 Zej
dd
 Zdd Zedd Zdd Zdd Zdd ZdS )CompilerPassz- The base class for all compiler passes.
    c                 O   s   d | _ d | _d S r   )	_analysis_pass_idr   argskwargsr   r   r   __init__#   s   
zCompilerPass.__init__c                 C      | j S )z.
        Returns the name of the pass
        )_name)clsr   r   r   name(      zCompilerPass.namec                 C   r'   )z$
        The ID of the pass
        r"   r   r   r   r   pass_id/   r+   zCompilerPass.pass_idc                 C   
   || _ dS )z)
        Sets the ID of the pass
        Nr,   r   valr   r   r   r-   6      
c                 C   r'   )z,
        Analysis data for the pass
        r!   r   r   r   r   analysis=   r+   zCompilerPass.analysisc                 C   r.   )z4
        Set the analysis data for the pass
        Nr2   r/   r   r   r   r3   D   r1   c                 O      dS zd
        Runs the initialization sequence for the pass, will run before
        `run_pass`.
        Fr   r#   r   r   r   run_initializationK      zCompilerPass.run_initializationc                 O   r4   )z
        Runs the pass itself. Must return True/False depending on whether
        statement level modification took place.
        Nr   r#   r   r   r   run_passR   s   zCompilerPass.run_passc                 O   r4   r5   r   r#   r   r   r   run_finalizerZ   r7   zCompilerPass.run_finalizerc                 C   r4   )z( Override to set analysis usage
        Nr   )r   AUr   r   r   get_analysis_usagea   s   zCompilerPass.get_analysis_usagec                 C   s
   | j | S )z5
        Gets the analysis from a given pass
        r2   )r   	pass_namer   r   r   get_analysisf   s   
zCompilerPass.get_analysisN)r   r   r   r   r   r&   classmethodr*   propertyr-   setterr3   r6   r8   r9   r;   r=   r   r   r   r   r       s(    






r    )	metaclassc                   @      e Zd ZdZdS )SSACompliantMixinzn Mixin to indicate a pass is SSA form compliant. Nothing is asserted
    about this condition at present.
    Nr   r   r   r   r   r   r   r   rC   m   s    rC   c                   @   rB   )FunctionPassz$ Base class for function passes
    NrD   r   r   r   r   rE   t       rE   c                   @   rB   )AnalysisPasszD Base class for analysis passes (no modification made to state)
    NrD   r   r   r   r   rG   z   rF   rG   c                   @   rB   )LoweringPassz$ Base class for lowering passes
    NrD   r   r   r   r   rH      rF   rH   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S )AnalysisUsagezLThis looks and behaves like LLVM's AnalysisUsage because its like that.
    c                 C   s   t  | _t  | _d S r   )set	_required
_preservedr   r   r   r   r&      s   zAnalysisUsage.__init__c                 C   r'   r   rK   r   r   r   r   get_required_set      zAnalysisUsage.get_required_setc                 C   r'   r   )rL   r   r   r   r   get_preserved_set   rO   zAnalysisUsage.get_preserved_setc                 C      | j | d S r   )rK   addr   pssr   r   r   add_required      zAnalysisUsage.add_requiredc                 C   rQ   r   )rL   rR   rS   r   r   r   add_preserved   rV   zAnalysisUsage.add_preservedc                 C   s
   d| j  S )Nzrequired: %s
rM   r   r   r   r   __str__   s   
zAnalysisUsage.__str__N)
r   r   r   r   r&   rN   rP   rU   rW   rX   r   r   r   r   rI      s    rI   Fc                  O   s   t rt| i | d S d S r   )_DEBUGprint)r$   r%   r   r   r   debug_print   s   r[   pass_timingszinit run finalizec                   @   sv   e Zd ZdZdZdd Zdd Zddd	Zd
d Zdd Z	dd Z
edd Zdd Zedd Zdd Zdd ZdS )PassManagerzR
    The PassManager is a named instance of a particular compilation pipeline
    Fc                 C   s*   g | _ t | _d| _d| _d| _|| _dS )zA
        Create a new pipeline with name "pipeline_name"
        FN)passesr   
exec_times
_finalizedr!   _print_afterpipeline_name)r   rb   r   r   r   r&      s   
zPassManager.__init__c                 C   s`   t |tst|rt|tsd| }t|t |tr#t|}d S t	|s.t
d| d S )NzKPass must be referenced by name or be a subclass of a CompilerPass. Have %szPass %s is not registered)
isinstancestrinspectisclass
issubclassr    	TypeError_pass_registryfind_by_nameis_registered
ValueError)r   pass_clsmsgr   r   r   _validate_pass   s   


zPassManager._validate_pass c                 C   s(   |  | ||f}| j| d| _dS )zI
        Append a pass to the PassManager's compilation pipeline
        FN)ro   r^   appendr`   )r   rT   descriptionfunc_desc_tupler   r   r   add_pass   s   

zPassManager.add_passc                 C   sr   | j sJ | | | | t| j D ]\}\}}||kr  nqtd| | j |d |t|f d| _dS )zt
        Add a pass `pass_cls` to the PassManager's compilation pipeline after
        the pass `location`.
        zCould not find pass %s   FN)r^   ro   	enumeraterl   insertrd   r`   )r   rm   locationidxx_r   r   r   add_pass_after   s   



zPassManager.add_pass_afterc                    s,    fdd}|t j|t j|t jf}|S )Nc                    sD   g }| dkr | dkrdd  j D }|S | d}dd |D }|S )Nnoneallc                 S   s   g | ]\}}|  qS r   )r*   ).0rz   r{   r   r   r   
<listcomp>   s    z:PassManager._debug_init.<locals>.parse.<locals>.<listcomp>,c                 S   s   g | ]}|  qS r   )strip)r   rz   r   r   r   r      s    )r^   split)	conf_itemprint_passessplittedr   r   r   parse   s   
z&PassManager._debug_init.<locals>.parse)r   DEBUG_PRINT_AFTERDEBUG_PRINT_BEFOREDEBUG_PRINT_WRAP)r   r   retr   r   r   _debug_init   s   zPassManager._debug_initc                 C   s(   |   | _|  \| _| _| _d| _dS )zt
        Finalize the PassManager, after which no more passes may be added
        without re-finalization.
        TN)dependency_analysisr!   r   ra   _print_before_print_wrapr`   r   r   r   r   finalize   s   

zPassManager.finalizec                 C   r'   r   )r`   r   r   r   r   	finalized   s   zPassManager.finalizedc                 C   s   dj ||d}|f|_|S )zG
        Patches the error to show the stage that it arose in.
        z{desc}
{exc})descr   )formatr$   )r   r   r   newmsgr   r   r   _patch_error  s   zPassManager._patch_errorc              
      s<  d}fdd} fdd}|  jj d j_ jj}t   d| d| jjt	
 j t jt jd	}td
   }	tjd|dk |	Y t }
||j O }W d    n1 snw   Y  t }||j O }W d    n1 sw   Y  t }||j O }W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  ttrt j jrtjj r|rt! j"  n	t#$ jj% j_%t& jj%st'd t(|
j)|j)|j)}|j*d|  f < |  j+j d d S )NFc                    s(   | |}|dvrd}t |   |S )N)TFz[CompilerPass implementations should return True/False. CompilerPass with name '%s' did not.)rl   r*   )funccompiler_statemangledrn   )rT   r   r   check  s
   z#PassManager._runPass.<locals>.checkc                    s^   | |v r- j }|j|jj|| f}td| dd  jd ur' j  d S td d S d S )Nz%s.%s: %s: %s %sx   -zfunc_ir is None)func_idmodnamefunc_qualnamerb   rZ   centerfunc_irdump)r<   print_conditionprintable_conditionfidr$   )internal_stater   r   r   r[     s   
z)PassManager._runPass.<locals>.debug_printBEFOREz [])r*   qualnamemoduleflagsr$   return_typezPass znumba:run_pass)dataz&multiple scope in func_ir detected in z%s_%sAFTER),r*   r   r   r!   r3   r   r   dictr   r
   _lazy_pformatr   valuesrd   r$   r   r   new_error_contextevtrigger_eventr   r6   r8   r9   rc   rE   r   r   
_ENFORCINGri   get	__class__mutates_CFGr   runr	   canonicalize_cfgblocksr   CompilerErrorr\   r   r_   ra   )r   indexrT   r   mutatedr   r[   r   
ev_detailserrctx	init_time	pass_timefinalize_timeptr   )r   rT   r   r   _runPass  s^    



zPassManager._runPassc           
      C   s   ddl m} | jstdt| jD ]U\}\}}ztd|  t|j	}t
|tr2| ||| ntdW q |yE } z|d}~w tyg } zt
|tjsT|d| j|f }| ||}	|	d}~ww dS )z9
        Run the defined pipelines on the state.
        r   )_EarlyPipelineCompletionz!Cannot run non-finalised pipelinez-- %szLegacy pass in useNz%Failed in %s mode pipeline (step: %s))numba.core.compilerr   r   RuntimeErrorrv   r^   r   ri   r   	pass_instrc   r    r   BaseException	Exceptionr   
NumbaErrorrb   r   )
r   stater   ry   rT   	pass_descr   ern   patched_exceptionr   r   r   r   V  s2   
zPassManager.runc                 C   s   t  }| jD ]\}}t|j}t }|| ||t|< qt  }| D ]
\}}|	 ||< q&dd }	t  }
| D ]\}}t
||	||B |
|< q<|
S )z.
        Computes dependency analysis
        c                    s.    fdd t  }| D ]	}| ||O }q|S )Nc                    s<   | |v r||  nt  }|r|D ]	}| ||O }q|S t  S r   rJ   )lkeyrmapdep_setrz   walkr   r   r     s   zGPassManager.dependency_analysis.<locals>.resolve_requires.<locals>.walkr   )keyr   r   kr   r   r   resolve_requires  s
   z9PassManager.dependency_analysis.<locals>.resolve_requires)r   r^   ri   r   r   rI   r;   typeitemsrN   rJ   )r   depsrT   r{   rz   aurequires_mapr   vr   	dep_chainr   r   r   r   q  s   
zPassManager.dependency_analysisN)rp   )r   r   r   r   r   r&   ro   rt   r|   r   r   r?   r   r   r   r   r   r   r   r   r   r   r]      s     
	


Jr]   	pass_infoz#pass_inst mutates_CFG analysis_onlyc                   @   sJ   e Zd ZdZdZe Zdd Zdd Zdd Z	d	d
 Z
dd Zdd ZdS )PassRegistryz(
    Pass registry singleton class.
    r   c                    s    fdd}|S )Nc                    sN    | rJ |  rJ j| _ jd7  _t|   j| < | S )Nru   )rk   _does_pass_name_aliasr*   _idr-   r   	_registry)
pass_classanalysis_onlyr   r   r   r   make_festive  s   
z+PassRegistry.register.<locals>.make_festiver   )r   r   r   r   r   r   r   register  s   zPassRegistry.registerc                 C   s   || j  v S r   )r   keysr   clazzr   r   r   rk     s   zPassRegistry.is_registeredc                 C   s   |  |sJ | j| S r   )rk   r   r   r   r   r   r     s   
zPassRegistry.getc                 C   s*   | j  D ]\}}|jj|kr dS qdS )NTF)r   r   r   r*   )r   r   r   r   r   r   r   r     s
   z"PassRegistry._does_pass_name_aliasc                 C   sB   t |tsJ | j D ]\}}|jj|kr|  S qtd| )Nz"No pass with name %s is registered)rc   rd   r   r   r   r*   rl   )r   
class_namer   r   r   r   r   rj     s   zPassRegistry.find_by_namec                 C   s(   | j  D ]\}}td||f  qd S )Nz%s: %s)r   r   rZ   )r   r   r   r   r   r   r     s   zPassRegistry.dumpN)r   r   r   r   r   r   r   r   rk   r   r   rj   r   r   r   r   r   r     s    r   ),r   abcr   r   collectionsr   r   re   numba.core.compiler_lockr   
numba.corer   r   r	   r
   numba.core.tracingr   numba.core.postprocr   numba.core.ir_utilsr   r   numba.core.eventcorer   	termcolor
_termcolorobjectr   r    rC   rE   rG   rH   rI   rY   r[   r\   r]   r   r   ri   r   register_passr   r   r   r   <module>   s:    N
 
m.
