o
    8ήc/8                     @   sh  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 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)pformat)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   D/tmp/pip-target-vg8gfxp4/lib/python/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_id0   r,   zCompilerPass.pass_idc                 C   
   || _ dS )z)
        Sets the ID of the pass
        Nr-   r   valr   r   r   r.   7      
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
        Nr3   r0   r   r   r   r4   E   r2   c                 O      dS zd
        Runs the initialization sequence for the pass, will run before
        `run_pass`.
        Fr   r$   r   r   r   run_initializationL      zCompilerPass.run_initializationc                 O   r5   )z
        Runs the pass itself. Must return True/False depending on whether
        statement level modification took place.
        Nr   r$   r   r   r   run_passS   s   zCompilerPass.run_passc                 O   r5   r6   r   r$   r   r   r   run_finalizer[   r8   zCompilerPass.run_finalizerc                 C   r5   )z( Override to set analysis usage
        Nr   )r   AUr   r   r   get_analysis_usageb   s   zCompilerPass.get_analysis_usagec                 C   s
   | j | S )z5
        Gets the analysis from a given pass
        r3   )r   	pass_namer   r   r   get_analysisg   s   
zCompilerPass.get_analysisN)r   r   r   r    r   r'   classmethodr+   propertyr.   setterr4   r7   r9   r:   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   rD   n   s    rD   c                   @   rC   )FunctionPassz$ Base class for function passes
    NrE   r   r   r   r   rF   u       rF   c                   @   rC   )AnalysisPasszD Base class for analysis passes (no modification made to state)
    NrE   r   r   r   r   rH   {   rG   rH   c                   @   rC   )LoweringPassz$ Base class for lowering passes
    NrE   r   r   r   r   rI      rG   rI   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   rL   r   r   r   r   get_required_set      zAnalysisUsage.get_required_setc                 C   r(   r   )rM   r   r   r   r   get_preserved_set   rP   zAnalysisUsage.get_preserved_setc                 C      | j | d S r   )rL   addr   pssr   r   r   add_required      zAnalysisUsage.add_requiredc                 C   rR   r   )rM   rS   rT   r   r   r   add_preserved   rW   zAnalysisUsage.add_preservedc                 C   s
   d| j  S )Nzrequired: %s
rN   r   r   r   r   __str__   s   
zAnalysisUsage.__str__N)
r   r   r   r    r'   rO   rQ   rV   rX   rY   r   r   r   r   rJ      s    rJ   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   rc   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)rp   r_   appendra   )r   rU   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_   rp   	enumeraterm   insertre   ra   )r   rn   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+   ).0r{   r|   r   r   r   
<listcomp>   s    z:PassManager._debug_init.<locals>.parse.<locals>.<listcomp>,c                 S   s   g | ]}|  qS r   )strip)r   r{   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   rb   _print_before_print_wrapra   r   r   r   r   finalize   s   

zPassManager.finalizec                 C   r(   r   )ra   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jd
|dY t }	||j O }W d    n1 s`w   Y  t }
||j O }W d    n1 s{w   Y  t }||j O }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.)rm   r+   )funccompiler_statemangledro   )rU   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_qualnamerc   r[   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numba:run_pass)dataz&multiple scope in func_ir detected in z%s_%sAFTER)*r+   r   r   r"   r4   r   r   dictr   r   r   valuesre   r%   r   evtrigger_eventr   r7   r9   r:   rd   rF   r   r   
_ENFORCINGrj   get	__class__mutates_CFGr   runr
   canonicalize_cfgblocksr   r   CompilerErrorr]   r   r`   rb   )r   indexrU   r   mutatedr   r\   r   
ev_details	init_time	pass_timefinalize_timeptr   )r   rU   r   r   _runPass  sX   



zPassManager._runPassc           
      C   s   ddl m} | jstdt| jD ]Y\}\}}ztd|  t|j	}t
|tr2| ||| ntdW q |yE } z|d}~w tyk } zt rXt
|tjsX|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   RuntimeErrorrw   r_   r   rj   r   	pass_instrd   r!   r   BaseException	Exceptionr   use_new_style_errorsr   
NumbaErrorrc   r   )
r   stater   rz   rU   	pass_descr   ero   patched_exceptionr   r   r   r   V  s6   

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   rK   )lkeyrmapdep_setr{   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_   rj   r   r   rJ   r<   typeitemsrO   rK   )r   depsrU   r|   r{   aurequires_mapr   vr   	dep_chainr   r   r   r   r  s   
zPassManager.dependency_analysisN)rq   )r   r   r   r    r   r'   rp   ru   r}   r   r   r@   r   r   r   r   r   r   r   r   r   r   r^      s     
	


I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 )Nrv   )rl   _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   rl     s   zPassRegistry.is_registeredc                 C   s   |  |sJ | j| S r   )rl   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)rd   re   r   r   r   r+   rm   )r   
class_namer   r   r   r   r   rk     s   zPassRegistry.find_by_namec                 C   s(   | j  D ]\}}td||f  qd S )Nz%s: %s)r   r   r[   )r   r   r   r   r   r   r     s   zPassRegistry.dumpN)r   r   r   r    r   r   r   r   rl   r   r   rk   r   r   r   r   r   r     s    r   ).r   abcr   r   collectionsr   r   rf   pprintr   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!   rD   rF   rH   rI   rJ   rZ   r\   r]   r^   r   r   rj   r   register_passr   r   r   r   <module>   s<    N
 
m.
