o
    8ήc[i                     @   s4  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?_@g dZAG dd dedeAZBedg dZCdd ZDdd ZEde?i fddZFd4ddZGG dd  d eHZIG d!d" d"eJZKG d#d$ d$eLZMd%d& ZNG d'd( d(eHZOG d)d* d*eOZPG d+d, d,eHZQdePfd-d.ZRd/dddePfd0d1ZSd2d3 ZTdS )5    )
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 function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   boolenable_loopliftenable_pyobjectenable_pyobject_looplift
enable_ssaforce_pyobjectrelease_gil
no_compile	debuginfoboundscheckforceinlineno_cpython_wrapperno_cfunc_wrapperr   ParallelOptionsauto_parallelnrtno_rewritesstrerror_modelFastMathOptionsfastmathnoaliasInlineOptionsinlinetarget_backenddbg_extend_lifetimesdbg_optnone rY   rY   :/tmp/pip-target-vg8gfxp4/lib/python/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.
    rY   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)rd   serialize_using_object_coderO   r_   rc   typemap	calltypes_find_referenced_environmentsrf   r`   ra   rb   rh   tuple)selflibdatatypeannrc   ri   rY   rY   rZ   _reduce   s   

zCompileResult._reducec                 C   sN   | j j}g }|jD ]}|j}|dr$t|}|dur$| r$|| q	|S )z2Returns a list of referenced environments
        _ZN08NumbaEnvN)rd   _final_moduleglobal_variablesname
startswithr   	can_cacheappend)rp   modri   gvgvnenvrY   rY   rZ   rn      s   


z+CompileResult._find_referenced_environmentsc                 C   sv   |	r
|	D ]}|  q|  |}||||}| ||j||||||||d d d |	|
d}|
D ]
}|j |j| q.|S )N)r\   r[   rd   rf   r]   rc   r_   r`   ra   rb   r^   re   rg   rh   ri   )codegenunserialize_libraryget_executabler[   set_envenv_name)clsr\   rq   rc   r~   r`   ra   rb   rr   rh   ri   fnrd   cfunccrrY   rY   rZ   _rebuild   s0   zCompileResult._rebuildc                 C   s
   | j  S N)r\   r   rp   rY   rY   rZ   r     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;   r]   r`   dump)rp   r   rY   rY   rZ   r     s    zCompileResult.dumpN)r   )r;   r<   r=   __doc__	__slots__rs   rn   classmethodr   propertyr   r   rY   rY   rY   rZ   rj      s    
!
rj   _CompileResult_LowerResult)rc   re   r   r~   c                 C   sd   t |  }t t}|| }|rt| || }|D ]}d | |< q| d }|d ur0|d | d< | S )Nr^   )setkeys	CR_FIELDS	NameErrorwith_traceback)entriesr   fieldsetbadnamesmissingkerrrY   rY   rZ   sanitize_compile_result_entries  s   
r   c                  K   s   t | } tdi | S NrY   )r   rj   )r   rY   rY   rZ   compile_result$  s   r   c              	   C   sf   ddl m} t }tj|dd}||| t||| ||||W  d   S 1 s,w   Y  dS )zp
    Compile the function in an isolated environment (typing and target
    context).
    Good for testing.
    r   )
cpu_targetr   )targetN)numba.core.registryr   r   Contextr   
CPUContextnested_contextcompile_extra)funcargsreturn_typeflagslocalsr   	typingctx	targetctxrY   rY   rZ   compile_isolated)  s   $r   Fc           
      C   sp   t j| }t|}t j|d}||}|r,d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   InterpreterByteCode	interpretnumba.core.inline_closurecallr   r   rK   runr	   PostProcessor)
r   inline_closures	emit_delsr   interpbcfunc_irr   inline_pass	post_procrY   rY   rZ   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 r   )r   r   )rp   r   rY   rY   rZ   __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   rz   formatgetattrjoin)rp   valsr   rY   rY   rZ   __repr__Z  s   

z_CompileStatus.__repr__N)r;   r<   r=   r   r   r   r   rY   rY   rY   rZ   r   P  s
    r   c                   @      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 r   )result)rp   r   rY   rY   rZ   r   f  s   
z!_EarlyPipelineCompletion.__init__N)r;   r<   r=   r   r   rY   rY   rY   rZ   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 y   t|w r   )KeyErrorAttributeError)rp   attrrY   rY   rZ   __getattr__p  s
   
zStateDict.__getattr__c                 C   s   || |< d S r   rY   )rp   r   valuerY   rY   rZ   __setattr__v  s   zStateDict.__setattr__N)r;   r<   r=   r   r   r   rY   rY   rY   rZ   r   j  s    r   c                 C   st   i }|j r	d|d< |jrd|d< |jrd|d< |jr|j|d< |jr'|j|d< t|j| }||d< | jd	i |S )
zL
    Make a new target context from the given target context and flags.
    Tenable_debuginfoenable_boundscheck
enable_nrtrL   rR   rP   NrY   )	rF   rG   rM   rL   rR   r   create_error_modelrP   	subtarget)r   r   subtargetoptionsrP   rY   rY   rZ   _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_diagnosticsparfors)r   )r
   reload_configrefreshr   stater   r   r   rd   r   r   r   r   r   r   r   rb   lifted_fromrl   rm   r_   rg   rh   pipeliner   r   r   r@   status)rp   r   r   rd   r   r   r   r   rY   rY   rZ   r     s:   

zCompilerBase.__init__c                 C   s6   t j|| j_t | j d| j_d | j_| 	 S r   )
r   r   r   r   r   r   run_passrb   r   _compile_bytecode)rp   r   rY   rY   rZ   r     s
   zCompilerBase.compile_extrarY   Nc                 C   sF   |j | j_ || j_|| j_|| j_| jjj| j_t | j | 	 S r   )
r   r   rb   r   r   	arg_countnargsr   r   _compile_ir)rp   r   rb   r   rY   rY   rZ   
compile_ir  s   
zCompilerBase.compile_irc                 C   s   t  )zGChild classes override this to customize the pipelines in use.
        )NotImplementedErrorr   rY   rY   rZ   define_pipelines  s   zCompilerBase.define_pipelinesc                 C   sv  t  | jj  |  }|D ]w}|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urKW  nBW q tyb } z
|j}W Y d}~ n/d}~w ty } zt rut|tjsu||| jj_|r~|W Y d}~qd}~ww tdd| j_|dur|W  d   S | jjdusJ | jjW  d   S 1 sw   Y  dS )z4
        Populate and run compiler pipeline
        z%s.%szPipeline: %s for %spipeline_timesNz!All available pipelines exhausted)r2   enterr   r   copyr   pipeline_namer   modnamefunc_qualnamer   
exec_timesrg   r   r   r   r   	Exceptionr   use_new_style_errors
isinstancer   
NumbaErrorr   r   r   r   )rp   pmspmr   	func_nameis_final_pipelinereserY   rY   rZ   _compile_core  sR   


	'$zCompilerBase._compile_corec                 C   s   | j jdu sJ |  S )z>
        Populate and run pipeline for bytecode input
        Nr   r   r  r   rY   rY   rZ   r        zCompilerBase._compile_bytecodec                 C   s   | j jdusJ |  S )z8
        Populate and run pipeline for IR input
        Nr  r   rY   rY   rZ   r   
  r  zCompilerBase._compile_ir)rY   N)r;   r<   r=   r   r   r   r   r   r  r   r   rY   rY   rY   rZ   r     s    *

.r   c                   @   r   )CompilerzThe default compiler
    c                 C   sJ   g }| j jjs|t| j  | j jjs| j jjr#|t| j  |S r   )	r   r   rC   rz   DefaultPassBuilderdefine_nopython_pipeliner   r   define_objectmode_pipeline)rp   r   rY   rY   rZ   r     s   

zCompiler.define_pipelinesN)r;   r<   r=   r   r   rY   rY   rY   rZ   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_pipelinepassesextenddefine_typed_pipeline!define_nopython_lowering_pipelinefinalize)r   rw   dpbr   untyped_passestyped_passeslowering_passesrY   rY   rZ   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   rw   r   rY   rY   rZ   r  @  s   z4DefaultPassBuilder.define_nopython_lowering_pipelinetypedc                 C   sx   t |}|td |td |td | jjjr!|td | jj	s+|t
d | jjjr6|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   rL   enabledr%   rN   r$   r&   r  r  rY   rY   rZ   r  Q  s   

z(DefaultPassBuilder.define_typed_pipelineuntypedc                 C   s   t |}| jdu r|td |td |td |td |td | jj	s=|t
d |td |td	 |td
 |td | jj	sS|td |td |td | jjri|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_unrollssazLiteral propagation)r   r   r  r   r   r   r   r   r   rN   r   r   r   r   r   r   r   rB   r    r!   r  r  rY   rY   rZ   r  g  s4   
z*DefaultPassBuilder.define_untyped_pipelineobjectc                 C   s   t |}| jdu r|td |td n|td |td tjdkr3|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   	PYVERSIONr   r   r.   r   r   r(   r#   r/   r  r  rY   rY   rZ   r	    s*   

z-DefaultPassBuilder.define_objectmode_pipelineN)r
  )r  )r  )r  )r%  )
r;   r<   r=   r   staticmethodr  r  r  r  r	  rY   rY   rY   rZ   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   rd   pipeline_classr   rY   rY   rZ   r     s   
r   rY   c              	      s   |	r^t |}d|_ f	dd}||  |}d}|jsRt " tdtj z	||  |}W n	 tyB   Y nw W d   n1 sMw   Y  |durZ|}|S |}|S  |}|j	|dS )zK
    Compile a function with the given IR.

    For internal use only.
    Tc                    s$    |}|j | dS )Nr   rb   r   )r   )the_ir	the_flagsr   	r   rd   rb   r   r   r*  r   r   r   rY   rZ   compile_local  s   z!compile_ir.<locals>.compile_localNignorer+  )
r   deepcopyrN   warningscatch_warningssimplefilterr   NumbaWarningr   r   )r   r   r   r   r   r   r   rb   r   is_lifted_looprd   r*  
norw_flagsr/  	norw_cresrw_crescresr   rY   r.  rZ   r     s6   


r   c           	      C   s   t | ||||||}||S )z 
    For internal use only.
    )r  r   )	r   r   rd   r   r   r   r   r   r   rY   rY   rZ   compile_internal  s   
r;  )FF)Ucollectionsr   r   r2  numba.core.tracingr   
numba.corer   r   r   r   r   r	   r
   r   r   numba.parfors.parforr   numba.core.errorsr   numba.core.environmentr   numba.core.compiler_machineryr   numba.core.untyped_passesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   numba.core.typed_passesr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   numba.core.object_mode_passesr.   r/   numba.core.targetconfigr0   r1   r2   r3   DEFAULT_FLAGSrM   r   rj   r   r   r   r   r   r%  r   r   r   dictr   r   r   r  r  r   r   r;  rY   rY   rY   rZ   <module>   sP    ,L8	yR

	  

@