U
    iâËdK-  ã                   @   s´  d dl Z d dlZd dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZmZ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!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/m,Z,m0Z0m1Z1 d d
l2m3Z3m4Z4m5Z5 d dl6Z6e	ej7fdd�dd„ ƒZ8e	ej7fdd�dd„ ƒZ9dd„ Z:dd„ Z;e
dd„ ƒZ<e5ddd�G dd„ de3ƒƒZ=dd„ Z>G dd„ dej?ƒZ@G dd „ d eƒZAeBd!k�r°e6 C¡  dS )"é    N)ÚTestCaseÚoverride_configÚcaptured_stdoutÚskip_parfors_unsupported)ÚjitÚnjit)ÚtypesÚirÚpostprocÚcompiler)ÚguardÚfind_callnameÚ
find_constÚget_definitionÚsimplify_CFG)ÚCPUDispatcher)Úinline_closure_call)
ÚExtractByteCodeÚTranslateByteCodeÚ	FixupArgsÚIRProcessingÚDeadBranchPruneÚRewriteSemanticConstantsÚGenericRewritesÚWithLiftingÚ
PreserveIRÚInlineClosureLikes)ÚNopythonTypeInferenceÚAnnotateTypesÚNopythonRewritesÚPreParforPassÚ
ParforPassÚDumpParforDiagnosticsÚNativeLoweringÚNativeParforLoweringÚIRLegalizationÚNoPythonBackendr#   ÚParforFusionPassÚParforPreLoweringPass)ÚFunctionPassÚPassManagerÚregister_passT©Znopythonc                 C   s   | d S ©Né   © ©Úar/   r/   úR/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/tests/test_inlining.pyÚinner   s    r3   c                 C   s   t t | ƒƒS ©N©r3   r0   r/   r/   r2   Úmore    s    r6   c                 C   s   t | ƒd S ©Né   r5   r0   r/   r/   r2   Úouter_simple$   s    r9   c                 C   s   t | ƒt| ƒ S r4   )r3   r6   r0   r/   r/   r2   Úouter_multiple'   s    r:   c                   C   s   d S r4   r/   r/   r/   r/   r2   Ú	__dummy__*   s    r;   F©Zanalysis_onlyZmutates_CFGc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚInlineTestPassZinline_test_passc                 C   s   t  | ¡ d S r4   ©r)   Ú__init__©Úselfr/   r/   r2   r?   2   s    zInlineTestPass.__init__c                 C   sœ   t |jjƒdkst‚t|jj ¡ ƒd }t|jƒD ]H\}}tt	|j|j
ƒd k	r2t|ji ||dd„ |j|jd|j|jƒ
  q|q2t |j¡}| ¡  | ¡  dS )Nr.   r   c                   S   s   d S r4   r/   r/   r/   r/   r2   Ú<lambda>;   ó    z)InlineTestPass.run_pass.<locals>.<lambda>r/   T)ÚlenÚfunc_irÚblocksÚAssertionErrorÚlistÚvaluesÚ	enumerateÚbodyr   r   Úvaluer   Ú	typingctxÚ	targetctxÚtypemapÚ	calltypesr
   ZPostProcessorÚrunZremove_dels)rA   ÚstateÚblockÚiÚstmtZ	post_procr/   r/   r2   Úrun_pass5   s        þzInlineTestPass.run_passN©Ú__name__Ú
__module__Ú__qualname__Ú_namer?   rV   r/   r/   r/   r2   r=   .   s   r=   c                 C   sJ  d}t |ƒ}| td¡ | td¡ | td¡ | td¡ | jjsh| td¡ | t	d¡ | t
d¡ | td	¡ | td
¡ | jjjr–| td¡ | jjsª| td¡ | jjjrØ| td¡ | td¡ | td¡ | |d¡ | td¡ | td¡ | td¡ | jjj�r"| td¡ n| td¡ | td¡ | td¡ |S )NZinline_testz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Preprocessing for parforszconvert to parforszfuse parforszparfor preloweringzinline testz$ensure IR is legal prior to loweringzannotate typeszpreserve IRznative parfor loweringznative loweringznopython mode backendzdump parfor diagnostics)r*   Zadd_passr   r   r   r   ÚflagsZno_rewritesr   r   r   r   r   Zauto_parallelÚenabledr    r   r!   r'   r(   r%   r   r   r$   r#   r&   r"   )rR   Z	test_passÚnameÚpmr/   r/   r2   Úgen_pipelineF   s@    ÿ

r`   c                   @   s   e Zd ZdZdd„ ZdS )ÚInlineTestPipelinez>compiler pipeline for testing inlining after optimization
    c                 C   s   t | jtƒ}| ¡  |gS r4   )r`   rR   r=   Úfinalize©rA   r_   r/   r/   r2   Údefine_pipeliness   s    z#InlineTestPipeline.define_pipelinesN)rX   rY   rZ   Ú__doc__rd   r/   r/   r/   r2   ra   p   s   ra   c                   @   sh   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	dd„ ƒZdS )ÚTestInliningzÕ
    Check that jitted inner functions are inlined into outer functions,
    in nopython mode.
    Note that not all inner functions are guaranteed to be inlined.
    We just trust LLVM's inlining heuristics.
    c                 C   s"   |  d¡}dd dd„ |D ƒ¡ S )z4
        Make regexpr to match mangled name
        Ú.z_ZN?Ú c                 S   s   g | ]}d   |¡‘qS )z\d+{})Úformat)Ú.0Úpr/   r/   r2   Ú
<listcomp>…   s     z-TestInlining.make_pattern.<locals>.<listcomp>)ÚsplitÚjoin)rA   ÚfullnameÚpartsr/   r/   r2   Úmake_pattern€   s    
zTestInlining.make_patternc                 C   s*   |   |¡}| jt ||¡d |¡d� d S )Nzexpected {}©Úmsg)rq   ZassertIsNotNoneÚreÚsearchri   ©rA   ro   ÚtextÚpatr/   r/   r2   Úassert_has_pattern‡   s    
ÿzTestInlining.assert_has_patternc                 C   s*   |   |¡}| jt ||¡d |¡d� d S )Nzunexpected {}rr   )rq   ZassertIsNonert   ru   ri   rv   r/   r/   r2   Úassert_not_has_patternŒ   s    
ÿz#TestInlining.assert_not_has_patternc              
   C   s|   t ddƒ�, tƒ �}ttjfdd�tƒ}W 5 Q R X W 5 Q R X |  |dƒd¡ | ¡ }t}|  	d| |¡ |  
d| |¡ d S )NÚDUMP_ASSEMBLYTr,   r.   é   z%s.outer_simpleú%s.inner)r   r   r   r   Úint32r9   ÚassertPreciseEqualÚgetvaluerX   ry   rz   ©rA   ÚoutZcfuncÚasmÚprefixr/   r/   r2   Útest_inner_function‘   s    (z TestInlining.test_inner_functionc              
   C   sŒ   t ddƒ�, tƒ �}ttjfdd�tƒ}W 5 Q R X W 5 Q R X |  |dƒd¡ | ¡ }t}|  	d| |¡ |  
d| |¡ |  
d| |¡ d S )	Nr{   Tr,   r.   é   z%s.outer_multiplez%s.morer}   )r   r   r   r   r~   r:   r   r€   rX   ry   rz   r�   r/   r/   r2   Útest_multiple_inner_functions�   s    (z*TestInlining.test_multiple_inner_functionsc                 C   s:   dd„ }t dtd�|ƒ}t d¡}|  ||ƒ||ƒ¡ d S )Nc                 S   s   t ƒ  |  ¡ S r4   )r;   Úsum©ÚAr/   r/   r2   Ú	test_impl¯   s    z=TestInlining.test_inline_call_after_parfor.<locals>.test_implT)ÚparallelÚpipeline_classé
   )r   ra   ÚnpZarangeÚassertEqual)rA   r‹   Új_funcrŠ   r/   r/   r2   Útest_inline_call_after_parfor«   s    
ÿ
z*TestInlining.test_inline_call_after_parforc              	   C   sî   dd„ }t  |¡}t|j ¡ ƒ}|D ]®}t|jƒD ]ž\}}t|tj	ƒr2t|j
tjƒr2tt||j
ƒdkr2|j|jj  |j
¡ tj tj|jd|jd�dd|j¡|_
|j|jj  |j
¡ t|i ||dd„ ƒ  q$q2q$|  t|jd	 ƒd¡ d S )
Nc                 S   s   | dkrd}nd}|S )Nr.   r8   é   r/   ©r1   Úbr/   r/   r2   r‹   º   s    z=TestInlining.test_inline_update_target_def.<locals>.test_implr8   Zmyvar)Úlocr/   c                   S   s   dS r7   r/   r/   r/   r/   r2   rB   Í   rC   z<TestInlining.test_inline_update_target_def.<locals>.<lambda>r•   )r   Úrun_frontendrH   rF   rI   rJ   rK   Ú
isinstancer	   ÚAssignrL   ZVarr   r   Z_definitionsÚtargetr^   ÚremoveÚExprÚcallÚscoper–   Úappendr   r�   rD   )rA   r‹   rE   rF   rS   rT   rU   r/   r/   r2   Útest_inline_update_target_def·   s    
ÿ&z*TestInlining.test_inline_update_target_defc                    sâ   t dtjid�dd„ ƒ‰ ‡ fdd„}t |¡}t|j ¡ ƒ}|D ]Ž}t|j	ƒD ]~\}}t
|tjƒrNt
|jtjƒrN|jjdkrNtt||jjƒ}t
|tjtjfƒrNt
|jtƒrN|jj}t||j|||ƒ\}	}
 q@qNq@|  d|
k¡ d S )Nr•   )Úlocalsc                 S   s   | d }|S r-   r/   r”   r/   r/   r2   ÚgÖ   s    z0TestInlining.test_inline_var_dict_ret.<locals>.gc                      s   ˆ dƒS r-   r/   r/   ©r¢   r/   r2   r‹   Û   s    z8TestInlining.test_inline_var_dict_ret.<locals>.test_implr�   )r   r   Zfloat64r   r—   rH   rF   rI   rJ   rK   r˜   r	   r™   rL   rœ   Úopr   r   ÚfuncÚGlobalZFreeVarr   Úpy_funcr   Ú__globals__Ú
assertTrue)rA   r‹   rE   rF   rS   rT   rU   Zfunc_defr§   Ú_Zvar_mapr/   r£   r2   Útest_inline_var_dict_retÒ   s4    

ÿ
þ
ÿ    ÿz%TestInlining.test_inline_var_dict_retc                    sÂ   t ddd„ƒ‰d‡fdd„	}tddd�G ‡fdd	„d	tƒƒ‰ G ‡ fd
d„dtjƒ}t |d�|ƒ}d}|  ||ƒ||ƒ¡ |  |ƒ |ƒ ¡ |jt d ¡f j	d }t
|jƒ|_|  t|jƒd¡ d S )Nc                 S   s   | d krdS | S d S r7   r/   r‰   r/   r/   r2   Úfooó   s    z9TestInlining.test_inline_call_branch_pruning.<locals>.fooc                    s   ˆ | ƒS r4   r/   r‰   ©r¬   r/   r2   r‹   ú   s    z?TestInlining.test_inline_call_branch_pruning.<locals>.test_implFTr<   c                       s$   e Zd ZdZdd„ Z‡ fdd„ZdS )zKTestInlining.test_inline_call_branch_pruning.<locals>.PruningInlineTestPassZpruning_inline_test_passc                 S   s   t  | ¡ d S r4   r>   r@   r/   r/   r2   r?     s    zTTestInlining.test_inline_call_branch_pruning.<locals>.PruningInlineTestPass.__init__c                    s�   t |jjƒdkst‚t|jj ¡ ƒd }t|jƒD ]X\}}tt	|j|j
ƒd k	r2t|ji ||ˆ j|j|j|j|j
jd j f|j|jƒ
  qŒq2dS )Nr.   r   T)rD   rE   rF   rG   rH   rI   rJ   rK   r   r   rL   r   r§   rM   rN   rO   Úargsr^   rP   )rA   rR   rS   rT   rU   r­   r/   r2   rV     s     ÿ   ýzTTestInlining.test_inline_call_branch_pruning.<locals>.PruningInlineTestPass.run_passNrW   r/   r­   r/   r2   ÚPruningInlineTestPassý   s   r¯   c                       s   e Zd Z‡ fdd„ZdS )zMTestInlining.test_inline_call_branch_pruning.<locals>.InlineTestPipelinePrunec                    s   t | jˆ ƒ}| ¡  |gS r4   )r`   rR   rb   rc   ©r¯   r/   r2   rd     s    z^TestInlining.test_inline_call_branch_pruning.<locals>.InlineTestPipelinePrune.define_pipelinesN)rX   rY   rZ   rd   r/   r°   r/   r2   ÚInlineTestPipelinePrune  s   r±   )r�   r“   Zpreserved_irr.   )N)N)r   r+   r)   r   ÚCompilerBaser�   Z	overloadsr   ZOmittedÚmetadatar   rF   rD   )rA   r‹   r±   r‘   rŠ   Zfirr/   )r¯   r¬   r2   Útest_inline_call_branch_pruningï   s    
z,TestInlining.test_inline_call_branch_pruningN)rX   rY   rZ   re   rq   ry   rz   r…   r‡   r   r’   r    r«   r´   r/   r/   r/   r2   rf   x   s   


rf   Ú__main__)Drt   Únumpyr�   Znumba.tests.supportr   r   r   r   Znumbar   r   Z
numba.corer   r	   r
   r   Znumba.core.ir_utilsr   r   r   r   r   Znumba.core.registryr   Znumba.core.inline_closurecallr   Znumba.core.untyped_passesr   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.compiler_machineryr)   r*   r+   Zunittestr~   r3   r6   r9   r:   r;   r=   r`   r²   ra   rf   rX   Úmainr/   r/   r/   r2   Ú<module>   s6   0<



* -
