U
    ,d8                     @   sn   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ G dd de jZedkrje   dS )	    N)contextmanager)ir)typestypingcallconvcpucgutils)
cpu_targetc                   @   s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )TestCompileCachez
    Tests that the caching in BaseContext.compile_internal() works correctly by
    checking the state of the cache when it is used by the CPUContext.
    c              	   c   s   t j}t j}| d}||x td}t	t
jt
j}|j|j|j}t||d}|j|}|jsvt|d}	t|	}
||
||fV  W 5 Q R X d S )NZtestingZtest_moduleZtest_fnentry)r	   typing_contextZtarget_contextZcodegenZcreate_libraryZpush_code_libraryr   Moduler   	signaturer   Zint32	call_convget_function_typereturn_typeargsr   get_or_insert_functionget_argumentsis_declarationAssertionErrorappend_basic_block	IRBuilder)selfr   contextlibmodulesigZ	llvm_fntyfunctionr   Zentry_blockbuilder r    B/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_compile_cache.py_context_builder_sig_args   s"    




z*TestCompileCache._context_builder_sig_argsc              	   C   sF  dd }dd }|   "\}}}}t|j}| |d t|j ||||| | |d t|j ||||| | |d t|j ||||| | |d t|j ttjtj}|j	
|j|j}	t|j|	d}
|j	|
}|
jst|
d	}t|}||||| | |d
 t|j W 5 Q R X d S )Nc                 S   s   d|  S )N   r    ir    r    r!   times2%   s    z+TestCompileCache.test_cache.<locals>.times2c                 S   s   | d S )N   r    r$   r    r    r!   times3(   s    z+TestCompileCache.test_cache.<locals>.times3r      r#   Z	test_fn_2r   r'   )r"   lencached_internal_funcassertEqualcompile_internalr   r   r   float64r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r&   r(   r   r   r   r   initial_cache_sizeZsig2Z
llvm_fnty2Z	function2Zargs2Zentry_block2Zbuilder2r    r    r!   
test_cache$   sN       





 



zTestCompileCache.test_cachec              	   C   s   dd }|   \}}}}|dd}|dd}|dd}t|j}	|||||}
| |	d t|j |||||}| |	d t|j |||||}| |	d t|j W 5 Q R X dS )zP
        Caching must not mix up closures reusing the same code object.
        c                    s    fdd}|S )Nc                    s    |  S Nr    )zyr    r!   fY   s    z?TestCompileCache.test_closures.<locals>.make_closure.<locals>.fr    )xr4   r5   r    r3   r!   make_closureX   s    z4TestCompileCache.test_closures.<locals>.make_closurer)   r#   N)r"   r*   r+   r-   r,   )r   r7   r   r   r   r   Zclo11Zclo12Zclo22r/   Zres1Zres2Zres3r    r    r!   test_closuresT   s,    
   






zTestCompileCache.test_closuresc              	      s   dd t tjtj fdd}|  \} }}td|}td|}|j|d}|j|d}|j|d}	t|j	}
| 
|
d t|j	 || | 
|
d	 t|j	 || | 
|
d	 t|j	 ||	 | 
|
d
 t|j	 W 5 Q R X dS )zA
        Caching must not mix up different error models.
        c                 S   s   d|  S )Ng      ?r    )r6   r    r    r!   invt   s    z.TestCompileCache.test_error_model.<locals>.invc                    s   |   S r1   )Zcompile_subroutine)r   r   r9   Zinv_sigr    r!   compile_invy   s    z6TestCompileCache.test_error_model.<locals>.compile_invpythonZnumpy)Zerror_modelr   r)   r#   N)r   r   r   r.   r"   r   Zcreate_error_modelZ	subtargetr*   r+   r,   )r   r;   r   r   r   Zpy_error_modelZnp_error_modelZpy_context1Zpy_context2Z
np_contextr/   r    r:   r!   test_error_modelp   s:    
   




z!TestCompileCache.test_error_modelN)	__name__
__module____qualname____doc__r   r"   r0   r8   r=   r    r    r    r!   r
   
   s   
0r
   __main__)Zunittest
contextlibr   Zllvmliter   Z
numba.corer   r   r   r   r   Znumba.core.registryr	   ZTestCaser
   r>   mainr    r    r    r!   <module>   s    