B
      ›\  ã               @   s  d dl mZmZ d dlZd dlZddlmZmZm	Z	 d dl
Z
d dl
mZ d dl
mZmZmZmZmZ d dlmZmZmZ d dlmZ dd	lmZ eejfd
ddd„ ƒZeejfd
ddd„ ƒZdd„ Zdd„ Zedd„ ƒZG dd„ de
jj ƒZ!G dd„ deƒZ"e#dkre $¡  dS )é    )Úprint_functionÚabsolute_importNé   )ÚTestCaseÚoverride_configÚcaptured_stdout)Úunittest_support)ÚjitÚnjitÚtypesÚirÚcompiler)ÚguardÚfind_callnameÚ
find_const)Úinline_closure_call)Úskip_unsupportedT)Únopythonc             C   s   | d S )Nr   © )Úar   r   ú8lib/python3.7/site-packages/numba/tests/test_inlining.pyÚinner   s    r   c             C   s   t t | ƒƒS )N)r   )r   r   r   r   Úmore   s    r   c             C   s   t | ƒd S )Né   )r   )r   r   r   r   Úouter_simple   s    r   c             C   s   t | ƒt| ƒ S )N)r   r   )r   r   r   r   Úouter_multiple   s    r   c               C   s   d S )Nr   r   r   r   r   Ú	__dummy__   s    r   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚInlineTestPipelinez>compiler pipeline for testing inlining after optimization
    c             C   s¦   d}|  |¡ |  |¡ |  |¡ |  |¡ |  |¡ | | jd¡ | jjsZ| | j	d¡ | jj
jrr| | jd¡ | | jd¡ | | jd¡ |  |¡ |  |¡ d S )NZinline_testzPreprocessing for parforsznopython rewriteszconvert to parforszinline testz$ensure IR is legal prior to lowering)Zcreate_pipelineZadd_preprocessing_stageZadd_with_handling_stageZadd_pre_typing_stageZadd_typing_stageZ	add_stageZstage_pre_parfor_passÚflagsZno_rewritesZstage_nopython_rewritesZauto_parallelZenabledZstage_parfor_passÚstage_inline_test_passZstage_ir_legalizationZadd_lowering_stageZadd_cleanup_stage)ÚselfZpmÚnamer   r   r   Údefine_pipelines#   s     






z#InlineTestPipeline.define_pipelinesc             C   s~   t | jjƒdkst‚t| jj ¡ ƒd }xPt|jƒD ]B\}}tt	| j|j
ƒd k	r4t| ji ||dd„ | jd| j| jƒ	 P q4W d S )Nr   r   c               S   s   d S )Nr   r   r   r   r   Ú<lambda>;   s    z;InlineTestPipeline.stage_inline_test_pass.<locals>.<lambda>r   )ÚlenÚfunc_irÚblocksÚAssertionErrorÚlistÚvaluesÚ	enumerateÚbodyr   r   Úvaluer   Z	typingctxZtypemapZ	calltypes)r    ÚblockÚiÚstmtr   r   r   r   5   s    z)InlineTestPipeline.stage_inline_test_passN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r   r   r   r   r   r       s   r   c               @   sP   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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>M   s    z-TestInlining.make_pattern.<locals>.<listcomp>)ÚsplitÚjoin)r    ÚfullnameÚpartsr   r   r   Úmake_patternH   s    
zTestInlining.make_patternc             C   s*   |   |¡}| jt ||¡d |¡d d S )Nzexpected {})Úmsg)r?   ZassertIsNotNoneÚreÚsearchr7   )r    r=   ÚtextÚpatr   r   r   Úassert_has_patternO   s    
zTestInlining.assert_has_patternc             C   s*   |   |¡}| jt ||¡d |¡d d S )Nzunexpected {})r@   )r?   ZassertIsNonerA   rB   r7   )r    r=   rC   rD   r   r   r   Úassert_not_has_patternT   s    
z#TestInlining.assert_not_has_patternc          
   C   s|   t ddƒ, tƒ }ttjfddtƒ}W d Q R X W d Q R X |  |dƒd¡ | ¡ }t}|  	d| |¡ |  
d| |¡ d S )NÚDUMP_ASSEMBLYT)r   r   é   z%s.outer_simplez%s.inner)r   r   r	   r   Úint32r   ÚassertPreciseEqualÚgetvaluer0   rE   rF   )r    ÚoutÚcfuncÚasmÚprefixr   r   r   Útest_inner_functionY   s    (z TestInlining.test_inner_functionc          
   C   sŒ   t ddƒ, tƒ }ttjfddtƒ}W d Q R X W d Q R X |  |dƒd¡ | ¡ }t}|  	d| |¡ |  
d| |¡ |  
d| |¡ d S )	NrG   T)r   r   é   z%s.outer_multiplez%s.morez%s.inner)r   r   r	   r   rI   r   rJ   rK   r0   rE   rF   )r    rL   rM   rN   rO   r   r   r   Útest_multiple_inner_functionse   s    (z*TestInlining.test_multiple_inner_functionsc             C   s:   dd„ }t dtd|ƒ}t d¡}|  ||ƒ||ƒ¡ d S )Nc             S   s   t ƒ  |  ¡ S )N)r   Úsum)ÚAr   r   r   Ú	test_implw   s    z=TestInlining.test_inline_call_after_parfor.<locals>.test_implT)ZparallelZpipeline_classé
   )r
   r   ÚnpZarangeÚassertEqual)r    rU   Zj_funcrT   r   r   r   Útest_inline_call_after_parfors   s
    

z*TestInlining.test_inline_call_after_parforc          	   C   sä   dd„ }t  |¡}t|j ¡ ƒ}x¨|D ] }xšt|jƒD ]Œ\}}t|tj	ƒr6t|j
tjƒr6tt||j
ƒdkr6|j|jj  |j
¡ tj d dd|j¡|_
|j|jj  |j
¡ t|i ||dd„ ƒ P q6W q&W |  t|jd ƒd¡ d S )Nc             S   s   | dkrd}nd}|S )Nr   r   é   r   )r   Úbr   r   r   rU   ‚   s    z=TestInlining.test_inline_update_target_def.<locals>.test_implr   r   c               S   s   dS )Nr   r   r   r   r   r   r#   •   s    z<TestInlining.test_inline_update_target_def.<locals>.<lambda>r[   )r   Zrun_frontendr(   r&   r)   r*   r+   Ú
isinstancer   ZAssignr,   ZVarr   r   Z_definitionsÚtargetr!   ÚremoveZExprZcallZlocÚappendr   rX   r$   )r    rU   r%   r&   r-   r.   r/   r   r   r   Útest_inline_update_target_def   s    


z*TestInlining.test_inline_update_target_defN)r0   r1   r2   r3   r?   rE   rF   rP   rR   r   rY   r`   r   r   r   r   r4   @   s   r4   Ú__main__)%Z
__future__r   r   rA   ZnumpyrW   Zsupportr   r   r   Znumbar   Zunittestr	   r
   r   r   r   Znumba.ir_utilsr   r   r   Znumba.inline_closurecallr   Ztest_parforsr   rI   r   r   r   r   r   ZBasePipeliner   r4   r0   Úmainr   r   r   r   Ú<module>   s$    [
