U
    hâËdˆp  ã                   @   sb  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)m*Z* d d
l+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: d dl;m<Z<m=Z= d dl>m?Z?m@Z@mAZA G dd„ de?ƒZBeBƒ ZCdeC_DdddddddddddddddgZEG dd „ d ed!eEƒƒZFed"ddd#d$gƒZGd%d&„ ZHd'd(„ ZIdeCi fd)d*„ZJdCd,d-„ZKG d.d/„ d/eLƒZMG d0d1„ d1eNƒZOG d2d3„ d3ePƒZQd4d5„ ZRG d6d7„ d7eLƒZSG d8d9„ d9eSƒZTG d:d;„ d;eLƒZUdeTfd<d=„ZVd>dd+deTfd?d@„ZWdAdB„ ZXd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ÚRewriteDynamicRaisesÚ!LiteralPropagationSubPipelinePass)ÚNopythonTypeInferenceÚAnnotateTypesÚNopythonRewritesÚPreParforPassÚ
ParforPassÚDumpParforDiagnosticsÚIRLegalizationÚNoPythonBackendÚInlineOverloadsÚPreLowerStripPhisÚNativeLoweringÚNativeParforLoweringÚ"NoPythonSupportedFeatureValidationÚParforFusionPassÚParforPreLoweringPass)ÚObjectModeFrontEndÚObjectModeBackEnd)ÚTargetConfigÚOptionÚConfigStackc                   @   sf  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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.zŽEnable 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.zDMake debug emissions directives-only. Used when generating lineinfo.N)!Ú__name__Ú
__module__Ú__qualname__r5   Ú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_optnoneZdbg_directives_only© rP   rP   úL/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/core/compiler.pyr7   *   sæ   ýýýýýýýýýýýýýýýýýýýýýýýr7   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.
    rP   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)r[   Zserialize_using_object_coderL   rV   rZ   ÚtypemapÚ	calltypesÚ_find_referenced_environmentsr]   rW   rX   rY   r_   Útuple)ÚselfÚlibdataÚtypeannrZ   r`   rP   rP   rQ   Ú_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)r[   Z_final_moduleZglobal_variablesÚnameÚ
startswithr   Z	can_cacheÚappend)rf   Úmodr`   ÚgvZgvnÚenvrP   rP   rQ   rd   Ù   s    

z+CompileResult._find_referenced_environmentsc                 C   sv   |	r|	D ]
}|ƒ  q|  ¡  |¡}| |||¡}| ||j||||||||d d d |	|
d�}|
D ]}|j  |j|¡ q\|S )N)rS   rR   r[   r]   rT   rZ   rV   rW   rX   rY   rU   r\   r^   r_   r`   )ÚcodegenZunserialize_libraryÚget_executablerR   Zset_envÚenv_name)ÚclsrS   rg   rZ   ro   rW   rX   rY   rh   r_   r`   Úfnr[   ÚcfuncÚcrrP   rP   rQ   Ú_rebuildè   s0    òzCompileResult._rebuildc                 C   s
   | j  ¡ S ©N)rS   rp   ©rf   rP   rP   rQ   rp   
  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)Úprintr8   r?   rT   rW   Údump)rf   r|   rP   rP   rQ   r~     s     zCompileResult.dumpN)rz   )r?   r@   rA   Ú__doc__Ú	__slots__ri   rd   Úclassmethodrw   Úpropertyrp   r~   rP   rP   rP   rQ   ra   Â   s   
!
ra   Z_CompileResultÚ_LowerResultru   ro   c                 C   sd   t |  ¡ ƒ}t tƒ}|| }|r(t|Ž ‚|| }|D ]}d | |< q4| d }|d k	r`| d ¡| d< | S )NrU   )ÚsetÚkeysÚ	CR_FIELDSÚ	NameErrorÚwith_traceback)Úentriesr…   ZfieldsetZbadnamesÚmissingÚkÚerrrP   rP   rQ   Úsanitize_compile_result_entries  s    
r�   c                  K   s   t | ƒ} tf | ŽS rx   )r�   ra   )r‰   rP   rP   rQ   Ú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Ú	targetctxrP   rP   rQ   Úcompile_isolated1  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   rH   Úrunr	   ZPostProcessor)
r“   Zinline_closuresZ	emit_delsr›   ZinterpÚbcÚfunc_irrœ   Zinline_passZ	post_procrP   rP   rQ   Úrun_frontendA  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 rx   )r¤   r¥   )rf   r¥   rP   rP   rQ   Ú__init__^  s    z_CompileStatus.__init__c                 C   s4   g }| j D ]}| dj|t| |ƒd�¡ q
d |¡S )Nz{k}={v})r‹   Úvz, )r€   rl   ÚformatÚgetattrÚjoin)rf   Úvalsr‹   rP   rP   rQ   Ú__repr__b  s    
z_CompileStatus.__repr__N)r?   r@   rA   r   r€   r¦   r¬   rP   rP   rP   rQ   r£   X  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 rx   )Úresult)rf   r®   rP   rP   rQ   r¦   n  s    z!_EarlyPipelineCompletion.__init__N)r?   r@   rA   r   r¦   rP   rP   rP   rQ   r­   i  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 rx   )ÚKeyErrorÚAttributeError)rf   ÚattrrP   rP   rQ   Ú__getattr__x  s    
zStateDict.__getattr__c                 C   s   || |< d S rx   rP   )rf   r²   ÚvaluerP   rP   rQ   Ú__setattr__~  s    zStateDict.__setattr__N)r?   r@   rA   r   r³   rµ   rP   rP   rP   rQ   r¯   r  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_nrtrI   rN   rM   )	rF   rG   rJ   rI   rN   r   Zcreate_error_modelrM   Z	subtarget)r™   r–   ZsubtargetoptionsrM   rP   rP   rQ   Ú_make_subtarget‚  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¯   Ústater˜   r¶   r™   r[   r”   r•   r–   r—   r    r›   r¡   rY   Úlifted_fromrb   rc   rV   r^   r_   Úpipeliner   r¸   r£   rC   Ústatus)rf   r˜   r™   r[   r”   r•   r–   r—   rP   rP   rQ   r¦   œ  s:    
ÿ
ÿzCompilerBase.__init__c                 C   s6   t j |¡| j_tƒ  | j¡ d| j_d | j_|  	¡ S )NrP   )
r   r�   rž   rº   r›   r   Úrun_passrY   r»   Ú_compile_bytecode)rf   r“   rP   rP   rQ   r’   Æ  s
    zCompilerBase.compile_extrarP   Nc                 C   sF   |j | j_ || j_|| j_|| j_| jjj| j_tƒ  | j¡ |  	¡ S rx   )
r›   rº   rY   r»   r¡   Ú	arg_countÚnargsr   r¾   Ú_compile_ir)rf   r¡   rY   r»   rP   rP   rQ   Ú
compile_irÎ  s    
zCompilerBase.compile_irc                 C   s
   t ƒ ‚dS )zGChild classes override this to customize the pipelines in use.
        N)ÚNotImplementedErrorry   rP   rP   rQ   Ú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	ršW  �q*W q$ tk
rÐ } z|j}W Y ¢
 �q*W 5 d}~X Y q$ tk
�r } z0t ¡ rút|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_timeséÿÿÿÿNz!All available pipelines exhausted)r6   Zenterrº   r–   ÚcopyrÅ   Úpipeline_namer›   ÚmodnameZfunc_qualnamer   Z
exec_timesr^   rŸ   rv   r­   r®   Ú	Exceptionr   Zuse_new_style_errorsÚ
isinstancer   Z
NumbaErrorr½   r¤   r   r¼   ÚAssertionError)rf   ÚpmsÚpmrÈ   Ú	func_nameZis_final_pipelineÚresÚerP   rP   rQ   Ú_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Ò   ry   rP   rP   rQ   r¿     s    zCompilerBase._compile_bytecodec                 C   s   | j jdk	st‚|  ¡ S )z8
        Populate and run pipeline for IR input
        NrÓ   ry   rP   rP   rQ   rÂ     s    zCompilerBase._compile_ir)rP   N)r?   r@   rA   r   r¦   r’   rÃ   rÅ   rÒ   r¿   rÂ   rP   rP   rP   rQ   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 rx   )	rº   r–   rE   rl   ÚDefaultPassBuilderÚdefine_nopython_pipeliner½   r¥   Údefine_objectmode_pipeline)rf   rÍ   rP   rP   rQ   rÅ     s    

ÿzCompiler.define_pipelinesN)r?   r@   rA   r   rÅ   rP   rP   rP   rQ   rÔ     s   rÔ   c                   @   sr   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º   rj   ZdpbrÎ   Zuntyped_passesZtyped_passesZlowering_passesrP   rP   rQ   rÖ   5  s    


z+DefaultPassBuilder.define_nopython_pipelineÚnopython_loweringc                 C   st   t |ƒ}| td¡ | td¡ | td¡ | jjjrD| td¡ n| t	d¡ | t
d¡ | td¡ | ¡  |S )Nú3ensure 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–   rI   Úenabledr.   r-   r*   r(   rÝ   ©rº   rj   rÎ   rP   rP   rQ   rÜ   H  s    ÿÿ
z4DefaultPassBuilder.define_nopython_lowering_pipelineÚparfor_gufunc_nopython_loweringc                 C   sh   t |ƒ}| td¡ | td¡ | td¡ | jjjrD| td¡ n| t	d¡ | t
d¡ | ¡  |S )Nrß   rà   rá   râ   rã   rä   )r   rå   r/   r)   r$   r–   rI   ræ   r.   r-   r*   rÝ   rç   rP   rP   rQ   Ú/define_parfor_gufunc_nopython_lowering_pipeline\  s    ÿÿ
zBDefaultPassBuilder.define_parfor_gufunc_nopython_lowering_pipelineÚtypedc                 C   s�   t |ƒ}| td¡ | td¡ | td¡ | jjjrB| td¡ | jj	sV| t
d¡ | jjjr„| 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–   rI   ræ   r&   rK   r%   r'   r0   r1   rÝ   rç   rP   rP   rQ   rÛ   p  s    

z(DefaultPassBuilder.define_typed_pipelineÚparfor_gufunc_typedc                 C   sB   t |ƒ}| jst‚| td¡ | td¡ | td¡ | ¡  |S )rë   úprocessing IRrì   rï   )r   r¡   rÌ   rå   r   r#   r1   rÝ   rç   rP   rP   rQ   Údefine_parfor_gufunc_pipelineˆ  s    
z0DefaultPassBuilder.define_parfor_gufunc_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¡ | 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 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–   rK   r   r   r   r!   r   r   r   r   rD   r    r"   rÝ   rç   rP   rP   rQ   rÙ   ”  s6    
ÿÿz*DefaultPassBuilder.define_untyped_pipelineÚobjectc                 C   s°   t |ƒ}| jdk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   r2   r   r   r)   r$   r3   rÝ   rç   rP   rP   rQ   r×   Á  s(    
ÿÿz-DefaultPassBuilder.define_objectmode_pipelineN)rØ   )rÞ   )rè   )rê   )rð   )ró   )rø   )r?   r@   rA   r   ÚstaticmethodrÖ   rÜ   ré   rÛ   rò   rÙ   r×   rP   rP   rP   rQ   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—   r[   Úpipeline_classr¼   rP   rP   rQ   r’   à  s       ÿr’   rP   c              	      sÐ   |	r¨t  |¡}d|_‡ ‡‡‡‡‡‡‡‡f	dd„}||  ¡ |ƒ}d}|js’t ¡ �< 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¡   rY   r»   )rÃ   )Zthe_irZ	the_flagsr¼   ©	r”   r[   rY   r»   r—   rú   r•   r™   r˜   rP   rQ   Úcompile_local  s       ÿÿz!compile_ir.<locals>.compile_localNÚignorerû   )
rÇ   ÚdeepcopyrK   ÚwarningsÚcatch_warningsÚsimplefilterr   ZNumbaWarningrÊ   rÃ   )r˜   r™   r¡   r”   r•   r–   r—   rY   r»   Zis_lifted_loopr[   rú   Z
norw_flagsrý   Z	norw_cresZrw_cresZcresr¼   rP   rü   rQ   rÃ   ý  s4    


   ÿÿrÃ   c           	      C   s   t | ||||||ƒ}| |¡S )z 
    For internal use only.
    )rÔ   r’   )	r˜   r™   r[   r“   r”   r•   r–   r—   r¼   rP   rP   rQ   Úcompile_internal=  s       ÿr  )FF)YÚ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!   r"   Znumba.core.typed_passesr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   Znumba.core.object_mode_passesr2   r3   Znumba.core.targetconfigr4   r5   r6   r7   ZDEFAULT_FLAGSrJ   r†   ra   rƒ   r�   rŽ   rš   r¢   rø   r£   rÊ   r­   Údictr¯   r¶   r·   rÔ   rÕ   r’   rÃ   r  rP   rP   rP   rQ   Ú<module>   s€   ,PD
 ðRüÿ

	  8 ÿ
   þ
@