U
    ,d[i                     @   sR  d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZ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mZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d d
l*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z;m<Z<m=Z= G dd de;Z>e> Z?de?_@dddddddddddddddgZAG dd  d ed!eAZBed"ddd#d$gZCd%d& ZDd'd( ZEde?i fd)d*ZFdCd,d-ZGG d.d/ d/eHZIG d0d1 d1eJZKG d2d3 d3eLZMd4d5 ZNG d6d7 d7eHZOG d8d9 d9eOZPG d:d; d;eHZQdePfd<d=ZRd>dd+dePfd?d@ZSdAdB ZTdS )D    )
namedtupleN)event)	utilserrorstypinginterpreterbytecodepostprocconfigcallconvcpu)ParforDiagnostics)CompilerError)lookup_environment)PassManager)ExtractByteCodeTranslateByteCode	FixupArgsIRProcessingDeadBranchPruneRewriteSemanticConstantsInlineClosureLikesGenericRewritesWithLiftingInlineInlinablesFindLiterallyCallsMakeFunctionToJitFunctionCanonicalizeLoopExitCanonicalizeLoopEntryLiteralUnrollReconstructSSA!LiteralPropagationSubPipelinePass)NopythonTypeInferenceAnnotateTypesNopythonRewritesPreParforPass
ParforPassDumpParforDiagnosticsIRLegalizationNoPythonBackendInlineOverloadsPreLowerStripPhisNativeLowering"NoPythonSupportedFeatureValidation)ObjectModeFrontEndObjectModeBackEnd)TargetConfigOptionConfigStackc                   @   sX  e Zd ZeedddZeedddZeedddZeedddZeedddZ	eedd	dZ
eedd
dZeedd
dZeedd
dZeedddZeedd
dZeedd
dZeejedddZeedd
dZeedd
dZeedd
dZeejedd
dZeedd
dZeejedd
dZeedddZeedddZeedddZdS )FlagsFzEnable loop-lifting)typedefaultdocz!Enable pyobject mode (in general)z(Enable pyobject mode inside lifted loopsTz
Enable SSAz-Force pyobject mode inside the whole functionz&Release GIL inside the native functionZTODOz7Force inlining of the function. Overrides _dbg_optnone.zEnable automatic parallel optimization, can be fine-tuned by
taking a dictionary of sub-options instead of a boolean, see parfor.py for
detailpythonneverr   backendzTExtend variable lifetime for debugging. This automatically turns on with debug=True.z\Disable optimization for debug. Equivalent to adding optnone attribute in the LLVM Function.N) __name__
__module____qualname__r1   boolZenable_loopliftenable_pyobjectZenable_pyobject_looplift
enable_ssaforce_pyobjectZrelease_gilZ
no_compile	debuginfoboundscheckZforceinlineZno_cpython_wrapperZno_cfunc_wrapperr   ParallelOptionsauto_parallelnrtno_rewritesstrerror_modelZFastMathOptionsfastmathZnoaliasZInlineOptionsinlineZtarget_backendZdbg_extend_lifetimesZdbg_optnone rK   rK   7/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/compiler.pyr3   )   s   r3   Ttyping_contexttarget_contextentry_pointtyping_errortype_annotation	signature
objectmodeliftedfndesclibrarycall_helperenvironmentmetadatareload_initreferenced_envsc                   @   sF   e Zd ZdZdZdd Zdd Zedd Ze	d	d
 Z
dddZdS )CompileResultzI
    A structure holding results from the compilation of a function.
    rK   c              
   C   sT   | j  }t| j}| j}d |_|_|  }|| j| j| j	| j
| j|| jt|f	S )zA
        Reduce a CompileResult to picklable components.
        N)rV   Zserialize_using_object_coderG   rQ   rU   typemap	calltypes_find_referenced_environmentsrX   rR   rS   rT   rZ   tuple)selflibdatatypeannrU   r[   rK   rK   rL   _reduce   s    

   zCompileResult._reducec                 C   sN   | j j}g }|jD ]6}|j}|drt|}|dk	r| r|| q|S )z2Returns a list of referenced environments
        Z_ZN08NumbaEnvN)rV   Z_final_moduleZglobal_variablesname
startswithr   Z	can_cacheappend)ra   modr[   gvZgvnenvrK   rK   rL   r_      s    

z+CompileResult._find_referenced_environmentsc                 C   sv   |	r|	D ]
}|  q|  |}||||}| ||j||||||||d d d |	|
d}|
D ]}|j |j| q\|S )N)rN   rM   rV   rX   rO   rU   rQ   rR   rS   rT   rP   rW   rY   rZ   r[   )codegenZunserialize_libraryget_executablerM   Zset_envenv_name)clsrN   rb   rU   rj   rR   rS   rT   rc   rZ   r[   fnrV   cfunccrrK   rK   rL   _rebuild   s0    zCompileResult._rebuildc                 C   s
   | j  S N)rN   rk   ra   rK   rK   rL   rk     s    zCompileResult.codegen c                 C   sD   t | dt| j d| j  | jj|d d t | d d S )NzDUMP  z  )tabzEND DUMP)printr4   r:   rO   rR   dump)ra   rw   rK   rK   rL   ry     s     zCompileResult.dumpN)ru   )r:   r;   r<   __doc__	__slots__rd   r_   classmethodrr   propertyrk   ry   rK   rK   rK   rL   r\      s   
!
r\   Z_CompileResult_LowerResultrp   rj   c                 C   sd   t |  }t t}|| }|r(t| || }|D ]}d | |< q4| d }|d k	r`|d | d< | S )NrP   )setkeys	CR_FIELDS	NameErrorwith_traceback)entriesr   fieldsetZbadnamesmissingkerrrK   rK   rL   sanitize_compile_result_entries  s    
r   c                  K   s   t | } tf | S rs   )r   r\   )r   rK   rK   rL   compile_result$  s    r   c              
   C   sZ   ddl m} t }tj|dd}|||" t||| ||||W  5 Q R  S Q R X dS )zp
    Compile the function in an isolated environment (typing and target
    context).
    Good for testing.
    r   )
cpu_targetr   )targetN)Znumba.core.registryr   r   Contextr   Z
CPUContextZnested_contextcompile_extra)funcargsreturn_typeflagslocalsr   	typingctx	targetctxrK   rK   rL   compile_isolated)  s     r   Fc           
      C   sp   t j| }t|}t j|d}||}|rXddlm} ||t	
di d}|  t|}	|	| |S )a  
    Run the compiler frontend over the given Python function, and return
    the function's canonical Numba IR.

    If inline_closures is Truthy then closure inlining will be run
    If emit_dels is Truthy the ir.Del nodes will be emitted appropriately
    )func_idr   )InlineClosureCallPassF)r   FunctionIdentityfrom_functionr   ZInterpreterZByteCodeZ	interpretZnumba.core.inline_closurecallr   r   rC   runr	   ZPostProcessor)
r   Zinline_closuresZ	emit_delsr   Zinterpbcfunc_irr   Zinline_passZ	post_procrK   rK   rL   run_frontend9  s    	

 

r   c                   @   s(   e Zd ZdZddgZdd Zdd ZdS )	_CompileStatuszC
    Describes the state of compilation. Used like a C record.
    fail_reasoncan_fallbackc                 C   s   d | _ || _d S rs   )r   r   )ra   r   rK   rK   rL   __init__V  s    z_CompileStatus.__init__c                 C   s4   g }| j D ]}|dj|t| |d q
d|S )Nz{k}={v})r   vz, )r{   rg   formatgetattrjoin)ra   valsr   rK   rK   rL   __repr__Z  s    
z_CompileStatus.__repr__N)r:   r;   r<   rz   r{   r   r   rK   rK   rK   rL   r   P  s   r   c                   @   s   e Zd ZdZdd ZdS )_EarlyPipelineCompletionz@
    Raised to indicate that a pipeline has completed early
    c                 C   s
   || _ d S rs   )result)ra   r   rK   rK   rL   r   f  s    z!_EarlyPipelineCompletion.__init__N)r:   r;   r<   rz   r   rK   rK   rK   rL   r   a  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )	StateDictz
    A dictionary that has an overloaded getattr and setattr to permit getting
    and setting key/values through the use of attributes.
    c                 C   s,   z
| | W S  t k
r&   t|Y nX d S rs   )KeyErrorAttributeError)ra   attrrK   rK   rL   __getattr__p  s    
zStateDict.__getattr__c                 C   s   || |< d S rs   rK   )ra   r   valuerK   rK   rL   __setattr__v  s    zStateDict.__setattr__N)r:   r;   r<   rz   r   r   rK   rK   rK   rL   r   j  s   r   c                 C   sp   i }|j rd|d< |jr d|d< |jr.d|d< |jr>|j|d< |jrN|j|d< t|j| }||d< | jf |S )zL
    Make a new target context from the given target context and flags.
    TZenable_debuginfoZenable_boundscheckZ
enable_nrtrD   rI   rH   )	rA   rB   rE   rD   rI   r   Zcreate_error_modelrH   Z	subtarget)r   r   ZsubtargetoptionsrH   rK   rK   rL   _make_subtargetz  s    

r   c                   @   sJ   e Zd ZdZdd Zdd Zddd	Zd
d Zdd Zdd Z	dd Z
dS )CompilerBasez4
    Stores and manages states for the compiler
    c                 C   s   t   |  |  t | _|| j_t||| j_|| j_|| j_	|| j_
|| j_|| j_d | j_d | j_d | j_d | j_d | j_d | j_d | j_d | j_i | j_g | j_| | j_t | j_| jj| jjd< i | jjd< t| jjjd| j_d S )Nparfor_diagnosticsZparfors)r   )r
   Zreload_configZrefreshr   stater   r   r   rV   r   r   r   r   r   r   r   rT   lifted_fromr]   r^   rQ   rY   rZ   pipeliner   r   r   r>   status)ra   r   r   rV   r   r   r   r   rK   rK   rL   r     s:    

zCompilerBase.__init__c                 C   s6   t j|| j_t | j d| j_d | j_| 	 S )NrK   )
r   r   r   r   r   r   run_passrT   r   _compile_bytecode)ra   r   rK   rK   rL   r     s
    zCompilerBase.compile_extrarK   Nc                 C   sF   |j | j_ || j_|| j_|| j_| jjj| j_t | j | 	 S rs   )
r   r   rT   r   r   	arg_countnargsr   r   _compile_ir)ra   r   rT   r   rK   rK   rL   
compile_ir  s    
zCompilerBase.compile_irc                 C   s
   t  dS )zGChild classes override this to customize the pipelines in use.
        N)NotImplementedErrorrt   rK   rK   rL   define_pipelines  s    zCompilerBase.define_pipelinesc                 C   s  t  | jj ` |  }|D ]}|j}d| jjj| jjj	f }t
d||f  ||ji| jjd< ||d k}d}z$|| j | jjdk	rW  q*W q$ tk
r } z|j}W Y 
 q*W 5 d}~X Y q$ tk
r } z0t rt|tjs||| jj_|r|W 5 d}~X Y q$X q$tdd| j_|dk	rL|W  5 Q R  S | jjdk	s^t| jjW  5 Q R  S W 5 Q R X dS )z4
        Populate and run compiler pipeline
        z%s.%szPipeline: %s for %sZpipeline_timesNz!All available pipelines exhausted)r2   Zenterr   r   copyr   pipeline_namer   modnameZfunc_qualnamer   Z
exec_timesrY   r   rq   r   r   	Exceptionr   Zuse_new_style_errors
isinstancer   Z
NumbaErrorr   r   r   r   AssertionError)ra   pmspmr   	func_nameZis_final_pipelinereserK   rK   rL   _compile_core  sD    



zCompilerBase._compile_corec                 C   s   | j jdkst|  S )z>
        Populate and run pipeline for bytecode input
        Nr   r   r   r   rt   rK   rK   rL   r     s    zCompilerBase._compile_bytecodec                 C   s   | j jdk	st|  S )z8
        Populate and run pipeline for IR input
        Nr   rt   rK   rK   rL   r   
  s    zCompilerBase._compile_ir)rK   N)r:   r;   r<   rz   r   r   r   r   r   r   r   rK   rK   rK   rL   r     s   *

.r   c                   @   s   e Zd ZdZdd ZdS )CompilerzThe default compiler
    c                 C   sJ   g }| j jjs |t| j  | j jjs4| j jjrF|t| j  |S rs   )	r   r   r@   rg   DefaultPassBuilderdefine_nopython_pipeliner   r   define_objectmode_pipeline)ra   r   rK   rK   rL   r     s    

zCompiler.define_pipelinesN)r:   r;   r<   rz   r   rK   rK   rK   rL   r     s   r   c                   @   sV   e Zd ZdZedddZedddZedd	d
ZedddZedddZ	dS )r   z
    This is the default pass builder, it contains the "classic" default
    pipelines as pre-canned PassManager instances:
      - nopython
      - objectmode
      - interpreted
      - typed
      - untyped
      - nopython lowering
    nopythonc                 C   s`   t }t|}|| }|j|j || }|j|j || }|j|j |  |S )z<Returns an nopython mode pipeline based PassManager
        )r   r   define_untyped_pipelineZpassesextenddefine_typed_pipeline!define_nopython_lowering_pipelinefinalize)r   re   Zdpbr   Zuntyped_passesZtyped_passesZlowering_passesrK   rK   rL   r   -  s    


z+DefaultPassBuilder.define_nopython_pipelinenopython_loweringc                 C   s\   t |}|td |td |td |td |td |td |  |S )Nz3ensure features that are in use are in a valid form$ensure IR is legal prior to loweringannotate typesznative loweringznopython mode backendzdump parfor diagnostics)	r   add_passr-   r(   r#   r,   r)   r'   r   r   re   r   rK   rK   rL   r   @  s    z4DefaultPassBuilder.define_nopython_lowering_pipelinetypedc                 C   sx   t |}|td |td |td | jjjrB|td | jj	sV|t
d | jjjrl|td |  |S )z/Returns the typed part of the nopython pipelineznopython frontendremove phis nodeszinline overloaded functionszPreprocessing for parforsnopython rewriteszconvert to parfors)r   r   r"   r+   r*   r   rD   Zenabledr%   rF   r$   r&   r   r   rK   rK   rL   r   Q  s    

z(DefaultPassBuilder.define_typed_pipelineuntypedc                 C   s   t |}| jdkr*|td |td |td |td |td | jj	sz|t
d |td |td	 |td
 |td | jj	s|td |td |td | jjr|td |td |  |S )z0Returns an untyped part of the nopython pipelineNanalyzing bytecodefix up argsprocessing IRzHandle with contexts(inline calls to locally defined closureszrewrite semantic constantszdead branch pruningr   (convert make_function into JIT functionszinline inlinable functionszfind literally callszhandles literal_unrollZssazLiteral propagation)r   r   r   r   r   r   r   r   r   rF   r   r   r   r   r   r   r   r?   r    r!   r   r   rK   rK   rL   r   g  s4    
z*DefaultPassBuilder.define_untyped_pipelineobjectc                 C   s   t |}| jdkr,|td |td n|td |td tjdkrf|t	d |t
d |td	 |td
 |td |td |td |td |  |S )z:Returns an object-mode pipeline based PassManager
        Nr   r   r   r   )      zcanonicalize loop entryzcanonicalize loop exitzobject mode frontendr   r   r   r   zobject mode backend)r   r   r   r   r   r+   r   r   Z	PYVERSIONr   r   r.   r   r   r(   r#   r/   r   r   rK   rK   rL   r     s*    

z-DefaultPassBuilder.define_objectmode_pipelineN)r   )r   )r   )r   )r   )
r:   r;   r<   rz   staticmethodr   r   r   r   r   rK   rK   rK   rL   r   "  s   
*r   c	           
      C   s   || ||||||}	|	 |S )aD  Compiler entry point

    Parameter
    ---------
    typingctx :
        typing context
    targetctx :
        target context
    func : function
        the python function to be compiled
    args : tuple, list
        argument types
    return_type :
        Use ``None`` to indicate void return
    flags : numba.compiler.Flags
        compiler flags
    library : numba.codegen.CodeLibrary
        Used to store the compiled code.
        If it is ``None``, a new CodeLibrary is used.
    pipeline_class : type like numba.compiler.CompilerBase
        compiler pipeline
    )r   )
r   r   r   r   r   r   r   rV   pipeline_classr   rK   rK   rL   r     s       r   rK   c              	      s   |	rt |}d|_ f	dd}||  |}d}|jst < tdtj z||  |}W n tk
r   Y nX W 5 Q R X |dk	r|}n|}|S  |}|j	|dS dS )zK
    Compile a function with the given IR.

    For internal use only.
    Tc                    s$    |}|j | dS )Nr   rT   r   )r   )Zthe_irZ	the_flagsr   	r   rV   rT   r   r   r   r   r   r   rK   rL   compile_local  s       z!compile_ir.<locals>.compile_localNignorer   )
r   deepcopyrF   warningscatch_warningssimplefilterr   ZNumbaWarningr   r   )r   r   r   r   r   r   r   rT   r   Zis_lifted_looprV   r   Z
norw_flagsr   Z	norw_cresZrw_cresZcresr   rK   r   rL   r     s4    


   r   c           	      C   s   t | ||||||}||S )z 
    For internal use only.
    )r   r   )	r   r   rV   r   r   r   r   r   r   rK   rK   rL   compile_internal  s       r   )FF)Ucollectionsr   r   r   Znumba.core.tracingr   Z
numba.corer   r   r   r   r   r	   r
   r   r   Znumba.parfors.parforr   Znumba.core.errorsr   Znumba.core.environmentr   Znumba.core.compiler_machineryr   Znumba.core.untyped_passesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   Znumba.core.typed_passesr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   Znumba.core.object_mode_passesr.   r/   Znumba.core.targetconfigr0   r1   r2   r3   ZDEFAULT_FLAGSrE   r   r\   r~   r   r   r   r   r   r   r   r   dictr   r   r   r   r   r   r   r   rK   rK   rK   rL   <module>   s~   ,L8	yR

	   
   
@