o
    1&ip                     @   s,  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 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'm(Z( d d
l)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 d dl9m:Z:m;Z; d dl<m=Z=m>Z>m?Z? G dd de=Z@e@ ZAdeA_Bg dZCG dd dedeCZDedg dZEdd ZFdd ZGd2ddZHG dd deIZJG dd  d eKZLG d!d" d"eMZNd#d$ ZOG d%d& d&eIZPG d'd( d(ePZQG d)d* d*eIZRdeQfd+d,ZSd-dddeQfd.d/ZTd0d1 ZUdS )3    )
namedtupleN)event)errors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RewriteDynamicRaises!LiteralPropagationSubPipelinePass)NopythonTypeInferenceAnnotateTypesNopythonRewritesPreParforPass
ParforPassDumpParforDiagnosticsIRLegalizationNoPythonBackendInlineOverloadsPreLowerStripPhisNativeLoweringNativeParforLowering"NoPythonSupportedFeatureValidationParforFusionPassParforPreLoweringPass)ObjectModeFrontEndObjectModeBackEnd)TargetConfigOptionConfigStackc                   @   s\  e Zd 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 )Flags Fz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neverzTExtend 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.zDMake debug emissions directives-only. Used when generating lineinfo.N)!__name__
__module____qualname__	__slots__r3   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dbg_extend_lifetimesZdbg_optnoneZdbg_directives_onlyr6   r6   r6   >C:\wamp64\www\opt\env\Lib\site-packages\numba/core/compiler.pyr5   *   s    
r5   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.
    r6   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)rY   Zserialize_using_object_coderK   rT   rX   typemap	calltypes_find_referenced_environmentsr[   rU   rV   rW   r]   tuple)selflibdatatypeannrX   r^   r6   r6   rO   _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
        Z_ZN08NumbaEnvN)rY   Z_final_moduleZglobal_variablesname
startswithr   Z	can_cacheappend)rd   modr^   gvZgvnenvr6   r6   rO   rb      s   


z+CompileResult._find_referenced_environmentsc                 C   sv   |	r
|	D ]}|  q|  |}||||}| ||j||||||||d d d |	|
d}|
D ]
}|j |j| q.|S )N)rQ   rP   rY   r[   rR   rX   rT   rU   rV   rW   rS   rZ   r\   r]   r^   )codegenZunserialize_libraryget_executablerP   Zset_envenv_name)clsrQ   re   rX   rm   rU   rV   rW   rf   r]   r^   fnrY   cfunccrr6   r6   rO   _rebuild   s0   zCompileResult._rebuildc                 C   s
   | j  S N)rQ   rn   rd   r6   r6   rO   rn     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)printr7   r=   rR   rU   dump)rd   rz   r6   r6   rO   r|   	  s    zCompileResult.dumpN)rx   )r=   r>   r?   __doc__r@   rg   rb   classmethodru   propertyrn   r|   r6   r6   r6   rO   r_      s    
!
r_   Z_CompileResult_LowerResult)rX   rZ   rs   rm   c                 C   sd   t |  }t t}|| }|rt| || }|D ]}d | |< q| d }|d ur0|d | d< | S )NrS   )setkeys	CR_FIELDS	NameErrorwith_traceback)entriesr   ZfieldsetZbadnamesmissingkerrr6   r6   rO   sanitize_compile_result_entries  s   
r   c                  K   s   t | } tdi | S Nr6   )r   r_   )r   r6   r6   rO   compile_result'  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   ZInterpreterZByteCodeZ	interpretZnumba.core.inline_closurecallr   r
   rG   runr   ZPostProcessor)
funcZinline_closuresZ	emit_delsr   Zinterpbcfunc_irr   Zinline_passZ	post_procr6   r6   rO   run_frontend,  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 rv   )r   r   )rd   r   r6   r6   rO   __init__I  s   
z_CompileStatus.__init__c                 C   s4   g }| j D ]}|dj|t| |d qd|S )Nz{k}={v})r   vz, )r@   rj   formatgetattrjoin)rd   valsr   r6   r6   rO   __repr__M  s   

z_CompileStatus.__repr__N)r=   r>   r?   r}   r@   r   r   r6   r6   r6   rO   r   C  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 rv   )result)rd   r   r6   r6   rO   r   Y  s   
z!_EarlyPipelineCompletion.__init__N)r=   r>   r?   r}   r   r6   r6   r6   rO   r   T  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 rv   )KeyErrorAttributeError)rd   attrr6   r6   rO   __getattr__c  s
   
zStateDict.__getattr__c                 C   s   || |< d S rv   r6   )rd   r   valuer6   r6   rO   __setattr__i  s   zStateDict.__setattr__N)r=   r>   r?   r}   r   r   r6   r6   r6   rO   r   ]  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.
    TZenable_debuginfoZenable_boundscheckZ
enable_nrtrH   rM   rL   Nr6   )	rE   rF   rI   rH   rM   r	   Zcreate_error_modelrL   Z	subtarget)	targetctxflagsZsubtargetoptionsrL   r6   r6   rO   _make_subtargetm  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_configrefreshr   state	typingctxr   r   rY   argsreturn_typer   localsr   r   r   rW   lifted_fromr`   ra   rT   r\   r]   pipeliner   r   r   rB   status)rd   r   r   rY   r   r   r   r   r6   r6   rO   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_passrW   r   _compile_bytecode)rd   r   r6   r6   rO   compile_extra  s
   zCompilerBase.compile_extrar6   Nc                 C   sF   |j | j_ || j_|| j_|| j_| jjj| j_t | j | 	 S rv   )
r   r   rW   r   r   Z	arg_countnargsr   r   _compile_ir)rd   r   rW   r   r6   r6   rO   
compile_ir  s   
zCompilerBase.compile_irc                 C   s   t  )zGChild classes override this to customize the pipelines in use.
        )NotImplementedErrorrw   r6   r6   rO   define_pipelines  s   zCompilerBase.define_pipelinesc                 C   sn  t  | jj  |  }|D ]s}|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  n>W q tyb } z
|j}W Y d}~ n+d}~w ty } zt|tjsq||| jj_|rz|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 %sZpipeline_timesNz!All available pipelines exhausted)r4   Zenterr   r   copyr   pipeline_namer   modnameZfunc_qualnamer   Z
exec_timesr\   r   rt   r   r   	Exception
isinstancer   Z
NumbaErrorr   r   r   r   )rd   Zpmspmr   	func_nameZis_final_pipelinereser6   r6   rO   _compile_core  sN   

%$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   rw   r6   r6   rO   r        zCompilerBase._compile_bytecodec                 C   s   | j jdusJ |  S )z8
        Populate and run pipeline for IR input
        Nr   rw   r6   r6   rO   r     r   zCompilerBase._compile_ir)r6   N)r=   r>   r?   r}   r   r   r   r   r   r   r   r6   r6   r6   rO   r     s    *

,r   c                   @   r   )CompilerzThe default compiler
    c                 C   s&   | j jjrt| j gS t| j gS rv   )r   r   rD   DefaultPassBuilderdefine_objectmode_pipelinedefine_nopython_pipelinerw   r6   r6   rO   r     s   
zCompiler.define_pipelinesN)r=   r>   r?   r}   r   r6   r6   r6   rO   r     s    r   c                   @   st   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	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   rh   Zdpbr   Zuntyped_passesZtyped_passesZlowering_passesr6   r6   rO   r     s   


z+DefaultPassBuilder.define_nopython_pipelinenopython_loweringc                 C   st   t |}|td |td |td | jjjr"|td n|t	d |t
d |td |  |S )N3ensure features that are in use are in a valid form$ensure IR is legal prior to loweringannotate typesnative parfor loweringnative loweringnopython mode backendzdump parfor diagnostics)r   add_passr-   r'   r"   r   rH   enabledr,   r+   r(   r&   r   r   rh   r   r6   r6   rO   r   .  s   
z4DefaultPassBuilder.define_nopython_lowering_pipelineparfor_gufunc_nopython_loweringc                 C   sh   t |}|td |td |td | jjjr"|td n|t	d |t
d |  |S )Nr   r   r   r   r   r   )r   r   r-   r'   r"   r   rH   r   r,   r+   r(   r   r   r6   r6   rO   /define_parfor_gufunc_nopython_lowering_pipelineB  s   
zBDefaultPassBuilder.define_parfor_gufunc_nopython_lowering_pipelinetypedc                 C   s   t |}|td |td |td | jjjr!|td | jj	s+|t
d | jjjrB|td |td |td |  |S )	/Returns the typed part of the nopython pipelinenopython frontendremove phis nodeszinline overloaded functionszPreprocessing for parforsnopython rewriteszconvert to parforszfuse parforsparfor prelowering)r   r   r!   r*   r)   r   rH   r   r$   rJ   r#   r%   r.   r/   r   r   r6   r6   rO   r   V  s   

z(DefaultPassBuilder.define_typed_pipelineparfor_gufunc_typedc                 C   sB   t |}| js	J |td |td |td |  |S )r   processing IRr   r   )r   r   r   r   r!   r/   r   r   r6   r6   rO   define_parfor_gufunc_pipelinen  s   
z0DefaultPassBuilder.define_parfor_gufunc_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 |td | jj	sY|td |td |td | jjro|td | jj	sy|td |td |  |S )z0Returns an untyped part of the nopython pipelineNanalyzing bytecodefix up argsr   zHandle with contexts(inline calls to locally defined closureszrewrite semantic constantszdead branch pruningr   zrewrite dynamic raises(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   rJ   r   r   r   r   r   r   r   r   rC   r   r    r   r   r6   r6   rO   r   z  s:   
z*DefaultPassBuilder.define_untyped_pipelineobjectc                 C   s   t |}| jdu r|td |td n|td |td |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   r   r0   r   r   r'   r"   r1   r   r   r6   r6   rO   r     s(   
z-DefaultPassBuilder.define_objectmode_pipelineN)r   )r   )r   )r   )r   )r   )r   )r=   r>   r?   r}   staticmethodr   r   r   r   r   r   r   r6   r6   r6   rO   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   rY   pipeline_classr   r6   r6   rO   r     s   
r   r6   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   rW   r   )r   )Zthe_irZ	the_flagsr   	r   rY   rW   r   r   r   r   r   r   r6   rO   compile_local  s   z!compile_ir.<locals>.compile_localNignorer   )
r   deepcopyrJ   warningscatch_warningssimplefilterr   ZNumbaWarningr   r   )r   r   r   r   r   r   r   rW   r   Zis_lifted_looprY   r   Z
norw_flagsr   Z	norw_cresZrw_cresZcresr   r6   r   rO   r     s6   


r   c           	      C   s   t | ||||||}||S )z 
    For internal use only.
    )r   r   )	r   r   rY   r   r   r   r   r   r   r6   r6   rO   compile_internal&  s   
r   )FF)Vcollectionsr   r   r   Znumba.core.tracingr   Z
numba.corer   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   r    Znumba.core.typed_passesr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   Znumba.core.object_mode_passesr0   r1   Znumba.core.targetconfigr2   r3   r4   r5   ZDEFAULT_FLAGSrI   r   r_   r   r   r   r   r   r   r   r   dictr   r   r   r   r   r   r   r   r6   r6   r6   rO   <module>   sJ    $PD
{R
	  ;

@