B
      ›\  ã               @   sÔ   d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ d	Zd
ZdZe ejejej¡ZG dd„ deƒZedkrÐe  ¡  dS )z"
Tests for numba.targets.codegen.
é    )Úprint_functionN)Úutils)ÚJITCPUCodegen)Úglobal_compiler_locké   )ÚTestCasezb
    define i32 @sum(i32 %.1, i32 %.2) {
      %.3 = add i32 %.1, %.2
      ret i32 %.3
    }
    z›
    define i32 @"__main__.ising_element_update$1.array(int8,_2d,_C).int64.int64"(i32 %.1, i32 %.2) {
      %.3 = add i32 %.1, %.2
      ret i32 %.3
    }
a  
    declare i32 @"__main__.ising_element_update$1.array(int8,_2d,_C).int64.int64"(i32 %.1, i32 %.2)

    define i32 @sum(i32 %.1, i32 %.2) {
      %.3 = call i32 @"__main__.ising_element_update$1.array(int8,_2d,_C).int64.int64"(i32 %.1, i32 %.2)
      ret i32 %.3
    }
c               @   sŒ   e Zd ZdZdd„ Zdd„ Zddd„Zed	d
„ ƒZdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zejdd„ ƒZdS ) ÚJITCPUCodegenTestCasez'
    Test the JIT code generation.
    c             C   s   t  ¡  tdƒ| _d S )NZtest_codegen)r   Úacquirer   Úcodegen)Úself© r   ú7lib/python3.7/site-packages/numba/tests/test_codegen.pyÚsetUp:   s    zJITCPUCodegenTestCase.setUpc             C   s   | ` t ¡  d S )N)r
   r   Úrelease)r   r   r   r   ÚtearDown>   s    zJITCPUCodegenTestCase.tearDownNc             C   sb   | j  d¡}t |¡}| ¡  | |¡ |r^| j  d¡}t |¡}| ¡  | |¡ | |¡ |S )NZcompiled_moduleZlinking_module)r
   Zcreate_libraryÚllZparse_assemblyZverifyZadd_llvm_moduleZadd_linking_library)r   ZasmZlinking_asmÚlibraryZ	ll_moduleZlinking_libraryr   r   r   Úcompile_moduleB   s    




z$JITCPUCodegenTestCase.compile_modulec             C   sN   t dƒ}| |¡}| d¡}|s(t|ƒ‚t|ƒ}|ddƒ}|dksJt|ƒ‚d S )NÚother_codegenÚsumé   é   é   )r   Úunserialize_libraryÚget_pointer_to_functionÚAssertionErrorÚctypes_sum_ty)ÚclsÚstater
   r   ÚptrÚcfuncZresr   r   r   Ú_check_unserialize_sumO   s    


z,JITCPUCodegenTestCase._check_unserialize_sumc             C   s~   |   t¡}| d¡}|  |tj¡ t|ƒ}|  |ddƒd¡ |   tt	¡}| d¡}|  |tj¡ t|ƒ}|  |ddƒd¡ d S )Nr   r   r   r   )
r   Úasm_sumr   ZassertIsInstancer   Zinteger_typesr   ÚassertEqualÚasm_sum_outerÚasm_sum_inner)r   r   r   r    Zlibrary2r   r   r   Útest_get_pointer_to_functionY   s    


z2JITCPUCodegenTestCase.test_get_pointer_to_functionc             C   s0   | j  ¡ }t |¡ tdƒ}|  | ¡ |¡ d S )NZxxx)r
   Zmagic_tupleÚpickleÚdumpsr   r#   )r   ÚtupZcg2r   r   r   Útest_magic_tuplek   s    

z&JITCPUCodegenTestCase.test_magic_tuplec             C   s   |   |¡ d S )N)r!   )r   r   r   r   r   Ú_check_serialize_unserializes   s    z2JITCPUCodegenTestCase._check_serialize_unserializec             C   s@   t  t |d¡¡}dt| jjd }t t	j
d|| ¡ g¡ d S )Néÿÿÿÿa  if 1:
            import base64
            import pickle
            import sys
            from numba.tests.test_codegen import %(test_class)s

            state = pickle.loads(base64.b64decode(sys.argv[1]))
            %(test_class)s._check_unserialize_sum(state)
            )Z
test_classz-c)Úbase64Z	b64encoder'   r(   ÚdictÚ	__class__Ú__name__Ú
subprocessZ
check_callÚsysÚ
executableÚdecode)r   r   ÚargÚcoder   r   r   Ú _check_unserialize_other_processv   s    	z6JITCPUCodegenTestCase._check_unserialize_other_processc             C   s"   |   tt¡}| ¡ }|  |¡ d S )N)r   r$   r%   Úserialize_using_bitcoder+   )r   r   r   r   r   r   Ú"test_serialize_unserialize_bitcodeƒ   s    z8JITCPUCodegenTestCase.test_serialize_unserialize_bitcodec             C   s"   |   tt¡}| ¡ }|  |¡ d S )N)r   r$   r%   r8   r7   )r   r   r   r   r   r   Ú&test_unserialize_other_process_bitcodeˆ   s    z<JITCPUCodegenTestCase.test_unserialize_other_process_bitcodec             C   s*   |   tt¡}| ¡  | ¡ }|  |¡ d S )N)r   r$   r%   Úenable_object_cachingÚserialize_using_object_coder+   )r   r   r   r   r   r   Ú&test_serialize_unserialize_object_code   s    z<JITCPUCodegenTestCase.test_serialize_unserialize_object_codec             C   s*   |   tt¡}| ¡  | ¡ }|  |¡ d S )N)r   r$   r%   r;   r<   r7   )r   r   r   r   r   r   Ú*test_unserialize_other_process_object_code“   s    z@JITCPUCodegenTestCase.test_unserialize_other_process_object_codec          
   C   sž  |   tt¡}| ¡  | ¡ }tjdd }| ¡ }| ¡ }| 	d¡ W dQ R X |  
t|ƒd¡ tdƒ}| |¡}tjdd }t d¡ |  || ¡ ¡ W dQ R X |  
t|ƒd¡ |  d	t|d jƒ¡ tjdd }t d¡ |  | ¡ |¡ W dQ R X |  
t|ƒd¡ |  d	t|d jƒ¡ tjdd0}t d¡ |  t¡}| 	d¡ W dQ R X W dQ R X |  
t|ƒd¡ |  d	t|d jƒ¡ |  dt|jƒ¡ dS )
z	
        T)Úrecordr   Nr   r   Úalwaysr   zInspection disabled)r   r$   r%   r;   r<   ÚwarningsÚcatch_warningsZget_llvm_strZget_asm_strZget_function_cfgr#   Úlenr   r   ÚsimplefilterZassertNotEqualZassertInÚstrÚmessageZassertRaisesÚ	NameErrorZ	exception)r   r   r   ÚwZold_llvmZold_asmr
   Zraisesr   r   r   Útest_cache_disabled_inspection™   s6    



z4JITCPUCodegenTestCase.test_cache_disabled_inspectionc             C   s\   |   tt¡}| ¡  | ¡  | ¡  t |¡}t |j¡}~|  	|ƒ d ¡ |  	|ƒ d ¡ d S )N)
r   r$   r%   r;   r8   r<   ÚweakrefÚrefZ_final_moduleZassertIs)r   r   ÚuÚvr   r   r   Útest_library_lifetimeÂ   s    
z+JITCPUCodegenTestCase.test_library_lifetime)N)r0   Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úclassmethodr!   r&   r*   r+   r7   r9   r:   r=   r>   rI   ÚunittestZexpectedFailurerN   r   r   r   r   r   5   s   

)r   Ú__main__)!rQ   Z
__future__r   rA   r-   Zctypesr'   r1   r2   rJ   Zllvmlite.bindingZbindingr   Znumba.unittest_supportZunittest_supportrS   Znumbar   Znumba.targets.codegenr   Znumba.compiler_lockr   Zsupportr   r"   r%   r$   Z	CFUNCTYPEZc_intr   r   r0   Úmainr   r   r   r   Ú<module>   s,   
	 