U
    ,d+                     @   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, d d
l/m0Z0m1Z1m2Z2 d dl3Z3e	ej4fdddd Z5e	ej4fdddd Z6dd Z7dd Z8e
dd Z9e2dddG dd de0Z:dd Z;G dd dej<Z=G dd  d eZ>e?d!kre3@  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IRLegalizationNoPythonBackendr#   )FunctionPassPassManagerregister_passTZnopythonc                 C   s   | d S N    ar,   r,   =/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_inlining.pyinner   s    r0   c                 C   s   t t | S Nr0   r-   r,   r,   r/   more   s    r3   c                 C   s   t | d S N   r2   r-   r,   r,   r/   outer_simple"   s    r6   c                 C   s   t | t|  S r1   )r0   r3   r-   r,   r,   r/   outer_multiple%   s    r7   c                   C   s   d S r1   r,   r,   r,   r,   r/   	__dummy__(   s    r8   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 r1   r&   __init__selfr,   r,   r/   r<   0   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 r1   r,   r,   r,   r,   r/   <lambda>9       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)r>   stateblockistmtZ	post_procr,   r,   r/   run_pass3   s        zInlineTestPass.run_passN__name__
__module____qualname___namer<   rS   r,   r,   r,   r/   r:   ,   s   r:   c                 C   s  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 ||d |td |td |td |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inline testz$ensure IR is legal prior to loweringzannotate typeszpreserve IRznative loweringznopython mode backendzdump parfor diagnostics)r'   Zadd_passr   r   r   r   flagsZno_rewritesr   r   r   r   r   Zauto_parallelZenabledr    r   r!   r$   r   r   r#   r%   r"   )rO   Z	test_passnamepmr,   r,   r/   gen_pipelineD   s8    

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 r1   )r\   rO   r:   finalizer>   r[   r,   r,   r/   define_pipelinesl   s    z#InlineTestPipeline.define_pipelinesN)rU   rV   rW   __doc__r`   r,   r,   r,   r/   r]   i   s   r]   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,   r/   
<listcomp>~   s     z-TestInlining.make_pattern.<locals>.<listcomp>)splitjoin)r>   fullnamepartsr,   r,   r/   make_patterny   s    
zTestInlining.make_patternc                 C   s*   |  |}| jt||d|d d S )Nzexpected {}msg)rm   ZassertIsNotNoneresearchre   r>   rk   textpatr,   r,   r/   assert_has_pattern   s    
zTestInlining.assert_has_patternc                 C   s*   |  |}| jt||d|d d S )Nzunexpected {}rn   )rm   ZassertIsNonerp   rq   re   rr   r,   r,   r/   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   int32r6   assertPreciseEqualgetvaluerU   ru   rv   r>   outZcfuncZasmprefixr,   r,   r/   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 )	Nrw   Tr)   r+      z%s.outer_multiplez%s.morery   )r   r   r   r   rz   r7   r{   r|   rU   ru   rv   r}   r,   r,   r/   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 r1   )r8   sumAr,   r,   r/   	test_impl   s    z=TestInlining.test_inline_call_after_parfor.<locals>.test_implT)parallelpipeline_class
   )r   r]   npZarangeassertEqual)r>   r   j_funcr   r,   r,   r/   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+   r5      r,   r.   br,   r,   r/   r      s    z=TestInlining.test_inline_update_target_def.<locals>.test_implr5   Zmyvar)locr,   c                   S   s   dS r4   r,   r,   r,   r,   r/   r?      r@   z<TestInlining.test_inline_update_target_def.<locals>.<lambda>r   )r   run_frontendrE   rC   rF   rG   rH   
isinstancer	   AssignrI   ZVarr   r   Z_definitionstargetrZ   removeExprcallZscoper   appendr   r   rA   )r>   r   rB   rC   rP   rQ   rR   r,   r,   r/   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,   r/   g   s    z0TestInlining.test_inline_var_dict_ret.<locals>.gc                      s    dS r*   r,   r,   r   r,   r/   r      s    z8TestInlining.test_inline_var_dict_ret.<locals>.test_implr   )r   r   Zfloat64r   r   rE   rC   rF   rG   rH   r   r	   r   rI   r   opr   r   funcZGlobalZFreeVarr   py_funcr   __globals__
assertTrue)r>   r   rB   rC   rP   rQ   rR   Zfunc_defr   _Zvar_mapr,   r   r/   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 r4   r,   r   r,   r,   r/   foo   s    z9TestInlining.test_inline_call_branch_pruning.<locals>.fooc                    s    | S r1   r,   r   r   r,   r/   r      s    z?TestInlining.test_inline_call_branch_pruning.<locals>.test_implFTr9   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 r1   r;   r=   r,   r,   r/   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
  qq2dS )Nr+   r   T)rA   rB   rC   rD   rE   rF   rG   rH   r   r   rI   r   r   rJ   rK   rL   argsrZ   rM   )r>   rO   rP   rQ   rR   r   r,   r/   rS      s        zTTestInlining.test_inline_call_branch_pruning.<locals>.PruningInlineTestPass.run_passNrT   r,   r   r,   r/   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 r1   )r\   rO   r^   r_   r   r,   r/   r`     s    z^TestInlining.test_inline_call_branch_pruning.<locals>.InlineTestPipelinePrune.define_pipelinesN)rU   rV   rW   r`   r,   r   r,   r/   InlineTestPipelinePrune  s   r   )r   r   Zpreserved_irr+   )N)N)r   r(   r&   r   CompilerBaser   Z	overloadsr   ZOmittedmetadatar   rC   rA   )r>   r   r   r   r   Zfirr,   )r   r   r/   test_inline_call_branch_pruning   s    
z,TestInlining.test_inline_call_branch_pruningN)rU   rV   rW   ra   rm   ru   rv   r   r   r   r   r   r   r   r,   r,   r,   r/   rb   q   s   


rb   __main__)Arp   Z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%   Znumba.core.compiler_machineryr&   r'   r(   Zunittestrz   r0   r3   r6   r7   r8   r:   r\   r   r]   rb   rU   mainr,   r,   r,   r/   <module>   s6   00



% -
