o
    i8                     @   sr   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r7e   dS 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}||B td}t	t
jt
j}|j|j|j}t||d}|j|}|js<J |d}	t|	}
||
||fV  W d    d S 1 sXw   Y  d S )Ntestingtest_moduletest_fnentry)r	   typing_contexttarget_contextcodegencreate_librarypush_code_libraryr   Moduler   	signaturer   int32	call_convget_function_typereturn_typeargsr   get_or_insert_functionget_argumentsis_declarationappend_basic_block	IRBuilder)selfr   contextlibmodulesig	llvm_fntyfunctionr   entry_blockbuilder r)   \/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/numba/tests/test_compile_cache.py_context_builder_sig_args   s&   




"z*TestCompileCache._context_builder_sig_argsc                 C   sZ  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s~J |
d	}t|}||||| | |d
 t|j W d    d S 1 sw   Y  d S )Nc                 S   s   d|  S )N   r)   ir)   r)   r*   times2%      z+TestCompileCache.test_cache.<locals>.times2c                 S   s   | d S )N   r)   r-   r)   r)   r*   times3(   r0   z+TestCompileCache.test_cache.<locals>.times3r      r,   	test_fn_2r   r1   )r+   lencached_internal_funcassertEqualcompile_internalr   r   r   float64r   r   r   r   r   r   r#   r   r   r   r   r   )r    r/   r2   r!   r(   r$   r   initial_cache_sizesig2
llvm_fnty2	function2args2entry_block2builder2r)   r)   r*   
test_cache$   sH   










"zTestCompileCache.test_cachec                 C   s   dd }|   Z\}}}}|dd}|dd}|dd}t|j}	|||||}
| |	d t|j |||||}| |	d t|j |||||}| |	d t|j W d   dS 1 sew   Y  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   r0   z?TestCompileCache.test_closures.<locals>.make_closure.<locals>.fr)   )xrE   rF   r)   rD   r*   make_closureX   s   z4TestCompileCache.test_closures.<locals>.make_closurer3   r,   N)r+   r5   r6   r8   r7   )r    rH   r!   r(   r$   r   clo11clo12clo22r:   res1res2res3r)   r)   r*   test_closuresT   s(   







"zTestCompileCache.test_closuresc                    s  dd t tjtj fdd}|  h\} }}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 d   dS 1 sw   Y  dS )zA
        Caching must not mix up different error models.
        c                 S   s   d|  S )Ng      ?r)   )rG   r)   r)   r*   invt   r0   z.TestCompileCache.test_error_model.<locals>.invc                    s   |   S rB   )compile_subroutine)r!   r(   rP   inv_sigr)   r*   compile_invy   s   z6TestCompileCache.test_error_model.<locals>.compile_invpythonnumpy)error_modelr   r3   r,   N)r   r   r   r9   r+   r   create_error_model	subtargetr5   r6   r7   )r    rT   r!   r$   r   py_error_modelnp_error_modelpy_context1py_context2
np_contextr:   r)   rR   r*   test_error_modelp   s6   





"z!TestCompileCache.test_error_modelN)	__name__
__module____qualname____doc__r   r+   rA   rO   r_   r)   r)   r)   r*   r
   
   s    
0r
   __main__)unittest
contextlibr   llvmliter   
numba.corer   r   r   r   r   numba.core.registryr	   TestCaser
   r`   mainr)   r)   r)   r*   <module>   s     