a
    —§»aÃ*  ã                   @   s~  d dl Z d dlZ d dlmZmZ d dlmZmZ d dl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mZmZmZmZmZmZ d dlmZ d d	lmZmZ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) d dlm*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m0Z0 d dl3Z4d dl5m6Z6m7Z7 d dl8Z8dd„ Z9dd„ Z:e j;dd„ ƒZ<dd„ Z=dd„ Z>G dd„ de8j?ƒZ@eAdk�rze8 B¡  dS )é    N)Úir_utilsÚcpu)Úcompile_isolatedÚFlags)ÚtypesÚtypingÚirÚconfigÚcompiler)Ú
cpu_target)Útype_annotations)Úcopy_propagateÚapply_copy_propagateÚget_name_var_tableÚremove_delsÚremove_deadÚremove_call_handlersÚalias_func_extensions)Útype_inference_stage)ÚFunctionPassÚregister_passÚPassManager)
ÚExtractByteCodeÚTranslateByteCodeÚ	FixupArgsÚIRProcessingÚDeadBranchPruneÚRewriteSemanticConstantsÚGenericRewritesÚWithLiftingÚ
PreserveIRÚInlineClosureLikes)
ÚNopythonTypeInferenceÚAnnotateTypesÚNopythonRewritesÚPreParforPassÚ
ParforPassÚDumpParforDiagnosticsÚNativeLoweringÚIRLegalizationÚNoPythonBackendr(   )Úskip_parfors_unsupportedÚ
needs_blasc                 C   s.   d}|}| dkr|| }nd}d| }|| k S )Né   r   é   © )ÚbÚzÚwÚx1ÚxÚyÚar/   r/   úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/tests/test_remove_dead.pyÚtest_will_propagate    s    
r8   c                 C   s   d S ©Nr/   )r6   r0   ÚcÚdr/   r/   r7   Ú	null_func*   s    r<   c                 C   s   | S r9   r/   )ÚAr/   r/   r7   Údummy_aliased_func-   s    r>   c                 C   s   t  | |d j||¡ d S )Nr   )r   Z
_add_aliasÚname)Zlhs_nameÚargsZ	alias_mapZarg_aliasesr/   r/   r7   Úalias_ext_dummy_func1   s    rA   c                 C   sL   | j  ¡ D ]<\}}t|jƒD ](\}}t|tjƒr|jj|kr  dS qq
dS )NTF)	ÚblocksÚitemsÚ	enumerateÚbodyÚ
isinstancer   ÚAssignÚtargetr?   )Úfunc_irÚvarÚlabelÚblockÚiÚinstr/   r/   r7   ÚfindLhsAssign4   s
    rO   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d„ Z
dd„ Zdd„ Zeedd„ ƒƒZdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZd S )!ÚTestRemoveDeadFc                 C   s4   t ƒ }t d¡|_d|_t d¡|_t|||d�jS )NT)Úflags)	r   r   ZParallelOptionsÚauto_parallelZnrtZFastMathOptionsZfastmathr   Úentry_point)ÚselfÚfuncZ	arg_typesZfast_pflagsr/   r/   r7   Úcompile_parallel@   s
    zTestRemoveDead.compile_parallelc                 C   sò   t  ¡ }t |¡}t t¡}t ||¡�¶ | 	¡  | 	¡  t
jt
jt
jf}t||||d ƒ\}}}}tj|||dd ||tjd�}	t|jƒ t|j|ƒ\}
}t|j|
t|jƒ||ƒ t|j|j|ƒ |  t|dƒ¡ W d   ƒ n1 sä0    Y  d S )Nr/   )rI   ÚtypemapÚ	calltypesZliftedZlifted_fromr@   Úreturn_typeZhtml_outputr4   )r   ÚContextr   Z
CPUContextr
   Úrun_frontendr8   r   Znested_contextZrefreshr   Úint64r   r   ZTypeAnnotationr	   ZHTMLr   rB   r   r   r   r   Ú	arg_namesZassertFalserO   )rT   Ú	typingctxZ	targetctxÚtest_irr@   rW   rY   rX   Ú_Útype_annotationZin_cpsZout_cpsr/   r/   r7   Útest1G   s.    

ø	
zTestRemoveDead.test1c                 C   s<   dd„ }dd„ }t  |¡}t|j|j|ƒ |  ||ƒ¡ d S )Nc                   S   s   t j d¡ d S )Nr.   )ÚnpÚrandomÚseedr/   r/   r/   r7   Úcall_np_random_seeda   s    z1TestRemoveDead.test2.<locals>.call_np_random_seedc                 S   sV   | j d jD ]D}t|tjƒrt|jtjƒr|jjdkr|  |jj	¡j
dkr dS qdS )Nr   Úcallre   TF)rB   rE   rF   r   rG   ÚvalueÚExprÚopZget_definitionrU   Úattr)rI   rN   r/   r/   r7   Úseed_call_existsd   s    ÿ
þýz.TestRemoveDead.test2.<locals>.seed_call_exists)r
   r[   r   rB   r]   Ú
assertTrue)rT   rf   rl   r_   r/   r/   r7   Útest2`   s
    	
zTestRemoveDead.test2c                 C   s`   t  d¡ dd¡}| ¡ }d}|  |t |¡t |¡f¡}|||ƒ |||ƒ t j ||¡ d S )Né   r.   r-   r   )	rc   ZarangeÚreshapeÚcopyrV   ÚnumbaÚtypeofÚtestingÚassert_array_equal)rT   rU   ÚA1ÚA2rM   Úpfuncr/   r/   r7   Úrun_array_index_testq   s    

z#TestRemoveDead.run_array_index_testc                 C   s   dd„ }|   |¡ d S )Nc                 S   s   |   ¡ }d||< d S ©Nr-   )Zravel©r=   rM   ÚBr/   r/   r7   rU   |   s    z-TestRemoveDead.test_alias_ravel.<locals>.func©ry   ©rT   rU   r/   r/   r7   Útest_alias_ravel{   s    zTestRemoveDead.test_alias_ravelc                 C   s   dd„ }|   |¡ d S )Nc                 S   s   | j }d||< d S rz   )Zflatr{   r/   r/   r7   rU   ƒ   s    z,TestRemoveDead.test_alias_flat.<locals>.funcr}   r~   r/   r/   r7   Útest_alias_flat‚   s    zTestRemoveDead.test_alias_flatc                 C   s   dd„ }|   |¡ d S )Nc                 S   s   | j }d||df< d S ©Nr-   r   )ÚTr{   r/   r/   r7   rU   Š   s    z2TestRemoveDead.test_alias_transpose1.<locals>.funcr}   r~   r/   r/   r7   Útest_alias_transpose1‰   s    z$TestRemoveDead.test_alias_transpose1c                 C   s   dd„ }|   |¡ d S )Nc                 S   s   |   ¡ }d||df< d S r�   )Ú	transposer{   r/   r/   r7   rU   ‘   s    z2TestRemoveDead.test_alias_transpose2.<locals>.funcr}   r~   r/   r/   r7   Útest_alias_transpose2�   s    z$TestRemoveDead.test_alias_transpose2c                 C   s   dd„ }|   |¡ d S )Nc                 S   s   t  | ¡}d||df< d S r�   )rc   r„   r{   r/   r/   r7   rU   ˜   s    
z2TestRemoveDead.test_alias_transpose3.<locals>.funcr}   r~   r/   r/   r7   Útest_alias_transpose3—   s    z$TestRemoveDead.test_alias_transpose3c              
      s¼   ddl m} |ƒ  tj¡‰ ‡ fdd„}td d … }t |¡ ‡ fdd„}t d¡}| 	¡ }z:|  
|t |¡f¡}t |¡|ƒ ||ƒ W |td d …< n|td d …< 0 |  |d |d ¡ d S )Nr   )Ú_BLASc                    s   |ˆ gkr| j d j|vS dS )Né   F)r@   r?   )ÚrhsZlivesÚ	call_list©Zxxnrm2r/   r7   Úremove_dead_xxnrm2¥   s    
z<TestRemoveDead.test_alias_ctypes.<locals>.remove_dead_xxnrm2c                    s"   t  d¡}ˆ dd|jd| jƒ d S )Nrˆ   éd   é   )rc   ZonesÚctypes)Úretr6   r‹   r/   r7   rU   ¯   s    
z.TestRemoveDead.test_alias_ctypes.<locals>.funcrŽ   )Znumba.np.linalgr‡   Znumba_xxnrm2r   Úfloat64r   Úappendrc   Úzerosrq   rV   rr   rs   ÚnjitÚassertEqual)rT   r‡   rŒ   Zold_remove_handlersrU   rv   rw   rx   r/   r‹   r7   Útest_alias_ctypesž   s    


z TestRemoveDead.test_alias_ctypesc                 C   s   dd„ }|   |¡ d S )Nc                 S   s   t  | d¡}d||df< d S )N)r-   r.   r-   r   )rc   rp   r{   r/   r/   r7   rU   Á   s    z0TestRemoveDead.test_alias_reshape1.<locals>.funcr}   r~   r/   r/   r7   Útest_alias_reshape1À   s    z"TestRemoveDead.test_alias_reshape1c                 C   s   dd„ }|   |¡ d S )Nc                 S   s   |   dd¡}d||df< d S )Nr-   r.   r   )rp   r{   r/   r/   r7   rU   È   s    z0TestRemoveDead.test_alias_reshape2.<locals>.funcr}   r~   r/   r/   r7   Útest_alias_reshape2Ç   s    z"TestRemoveDead.test_alias_reshape2c                 C   s:   dd„ }t  ¡ }ztt d< |  |¡ W |t_ n|t_ 0 d S )Nc                 S   s   t | ƒ}d||df< d S r�   )r>   r{   r/   r/   r7   rU   Ï   s    z0TestRemoveDead.test_alias_func_ext.<locals>.func)r>   znumba.tests.test_remove_dead)r   rq   rA   ry   r   )rT   rU   Zold_ext_handlersr/   r/   r7   Útest_alias_func_extÎ   s    ÿz"TestRemoveDead.test_alias_func_extc                 C   s"   dd„ }|   |ƒ t |¡ƒ ¡ dS )zimake sure lhs variable of assignment is considered live if used in
        rhs (test for #6715).
        c                  S   s(   t dƒD ]} dd„ | ƒ}t |¡}q|S )Nr-   c                 S   s   | S r9   r/   )Újr/   r/   r7   Ú<lambda>ã   ó    zDTestRemoveDead.test_rm_dead_rhs_vars.<locals>.func.<locals>.<lambda>)Úrangerc   Úarray)rM   r6   r/   r/   r7   rU   á   s    z2TestRemoveDead.test_rm_dead_rhs_vars.<locals>.funcN)r•   rr   r”   r~   r/   r/   r7   Útest_rm_dead_rhs_varsÝ   s    z$TestRemoveDead.test_rm_dead_rhs_varsc                 C   s`   dd„ }t ddd�G dd„ dtƒƒ}G dd	„ d	tjƒ}tj|d
�|ƒƒ }|ƒ }tj ||¡ dS )zWMake sure aliases are considered in remove dead extension for
        parfors.
        c                  S   s:   d} t jj ¡  t | ¡}|}t  | ¡D ]}|||< q(|S )Né   )rr   ÚparforsÚparforZinit_prangerc   ÚemptyZprange)Únr=   r|   rM   r/   r/   r7   rU   î   s    

z8TestRemoveDead.test_alias_parfor_extension.<locals>.funcFT)Zanalysis_onlyZmutates_CFGc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )zATestRemoveDead.test_alias_parfor_extension.<locals>.LimitedParforZlimited_parforc                 S   s   t  | ¡ d S r9   )r   Ú__init__)rT   r/   r/   r7   r¥   ü   s    zJTestRemoveDead.test_alias_parfor_extension.<locals>.LimitedParfor.__init__c                 S   sœ   t jj |j|jj|jj|j|j	|j
j|j
|j|j¡	}t|jjƒ |j |jj¡ | |jj¡ t|jj|jj|j|jjƒ t jj |jj|jj|j¡ dS )NT)rr   r¡   r¢   r&   rI   ra   rW   rX   rY   r^   rQ   rR   ÚmetadataZparfor_diagnosticsr   rB   Zarray_analysisÚrunZ_convert_loopr   r]   Zget_parfor_paramsÚoptionsZfusionZnested_fusion_info)rT   ÚstateZparfor_passr/   r/   r7   Úrun_passÿ   s0    ÷ýþzJTestRemoveDead.test_alias_parfor_extension.<locals>.LimitedParfor.run_passN)Ú__name__Ú
__module__Ú__qualname__Ú_namer¥   rª   r/   r/   r/   r7   ÚLimitedParforø   s   r¯   c                   @   s   e Zd ZdZdd„ ZdS )z@TestRemoveDead.test_alias_parfor_extension.<locals>.TestPipelinezÀTest pipeline that just converts prange() to parfor and calls
            remove_dead(). Copy propagation can replace B in the example code
            which this pipeline avoids.
            c                 S   s´   d}t |ƒ}| td¡ | td¡ | td¡ | td¡ | jjjsj| t	d¡ | t
d¡ | td¡ | td	¡ | td
¡ | td¡ | td¡ | td¡ | ¡  |gS )Nztest parfor aliasingzanalyzing bytecodezfix up argszprocessing IRzHandle with contextsznopython rewriteszrewrite semantic constantszdead branch pruningz(inline calls to locally defined closuresznopython frontendzannotate typesznative loweringznopython mode backend)r   Zadd_passr   r   r   r   r©   rQ   Zno_rewritesr   r   r   r!   r"   r#   r(   r*   Úfinalize)rT   r?   Zpmr/   r/   r7   Údefine_pipelines  s&    
ÿzQTestRemoveDead.test_alias_parfor_extension.<locals>.TestPipeline.define_pipelinesN)r«   r¬   r­   Ú__doc__r±   r/   r/   r/   r7   ÚTestPipeline  s   r³   )Zpipeline_classN)	r   r   r
   ZCompilerrr   Zjitrc   rt   ru   )rT   rU   r¯   r³   Ztest_resZpy_resr/   r/   r7   Útest_alias_parfor_extensioné   s    

z*TestRemoveDead.test_alias_parfor_extensionN)r«   r¬   r­   Z_numba_parallel_test_rV   rb   rn   ry   r   r€   rƒ   r…   r†   r+   r,   r–   r—   r˜   r™   rŸ   r´   r/   r/   r/   r7   rP   <   s&   
 rP   Ú__main__)Crr   Znumba.parfors.parforZ
numba.corer   r   Znumba.core.compilerr   r   r   r   r   r	   r
   Znumba.core.registryr   Znumba.core.annotationsr   Znumba.core.ir_utilsr   r   r   r   r   r   r   Znumba.core.typed_passesr   Znumba.core.compiler_machineryr   r   r   Znumba.core.untyped_passesr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   Únumpyrc   Znumba.tests.supportr+   r,   Zunittestr8   r<   r”   r>   rA   rO   ZTestCaserP   r«   Úmainr/   r/   r/   r7   Ú<module>   s0   $00

 ~
