U
    iâËdÈm  ã                   @   sÔ  d Z ddlZddlmZ ddlZddlZddlZddlmZ ddl	Z
ddlmZmZ ddlmZmZmZ ddlmZmZ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! ddlm"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddlm)Z) ddl*m+Z+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 ddlm2Z2 ddl3m4Z5 ddl6m7Z8 ddl9m:Z: ddlm;Z; ddl<m=Z=m>Z> ddl?m@Z@mAZA ddlBmCZC G dd„ deƒZDeDed< G dd„ de+ƒZEe1ƒ ZFG dd„ de&ƒZGG d d!„ d!eHƒZIG d"d#„ d#e!ƒZJeJdƒZKG d$d%„ d%eƒZLeLeed < G d&d'„ d'eƒZMeMeed < eF NejO¡d(d)„ ƒZPeF QejRejS¡d*d+„ ƒZTeF NejS¡d,d-„ ƒZUeF NejV¡d.d/„ ƒZWedd0�d1d2„ ƒZXeejYdd0�d3d4„ ƒZZG d5d6„ d6eƒZ[G d7d8„ d8eƒZ\e]d9k�rÐe ^¡  dS ):a�  This tests the target extension API to ensure that rudimentary expected
behaviours are present and correct. It uses a piece of fake hardware as a
target, the Dummy Processing Unit (DPU), to do this. The DPU borrows a lot from
the CPU but is part of the GPU class of target. The DPU target has deliberately
strange implementations of fundamental operations so as to make it identifiable
in testing.é    N)ÚTestCase)Úcached_property)ÚnjitÚtypes)ÚoverloadÚ	intrinsicÚoverload_classmethod)ÚJitDecoratorÚtarget_registryÚdispatcher_registryÚjit_registryÚtarget_overrideÚGPUÚresolve_dispatcher_from_str)ÚutilsÚfastmathpassÚerrors)Ú
Dispatcher)ÚTargetDescriptor)ÚcpuÚtypingÚcgutils)ÚBaseContext)Úglobal_compiler_lock)Úcallconv)Ú
CPUCodegenÚJITCodeLibrary)ÚPyCallWrapper)ÚRegistryLoaderÚRegistry)Ú_dynfunc)Úir)Úrtsys)Úcompiler)ÚCompilerBaseÚDefaultPassBuilder)ÚFunctionPassÚregister_pass)ÚPreLowerStripPhisc                   @   s   e Zd ZdS )ÚDPUN)Ú__name__Ú
__module__Ú__qualname__© r-   r-   úZ/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/tests/test_target_extension.pyr)   2   s   r)   Údpuc                   @   s0   e Zd ZeZdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚJITDPUCodegenc                 C   sz   |   ¡ |d< tj ¡ j}| d¡r(d}n| d¡r8d}nd}||d< d|d	< | j|d
< t tjj	¡}d|j
krvd|d< d S )Nr   Úx86ZstaticÚppcZpicÚdefaultZrelocZ
jitdefaultZ	codemodelÚfeaturesZjitT)Z_get_host_cpu_nameÚllZTargetZfrom_default_tripleÚnameÚ
startswithZ_tm_featuresr   ZpysignatureZcreate_target_machineÚ
parameters)ÚselfÚoptionsÚarchZreloc_modelÚsigr-   r-   r.   Ú_customize_tm_optionsB   s    



z#JITDPUCodegen._customize_tm_optionsc                 C   s   |   ¡ S ©N)Z_get_host_cpu_features©r9   r-   r-   r.   Ú_customize_tm_featuresY   s    z$JITDPUCodegen._customize_tm_featuresc                 C   s   | j  |¡ d S r>   )Ú_engineZ
add_module)r9   Úmoduler-   r-   r.   Ú_add_module]   s    zJITDPUCodegen._add_modulec                 C   s2   | j  |¡}tjd  |¡}t t|ƒ¡|d< dS )zrSet the environment address.

        Update the GlobalVariable named *env_name* to the address of *env*.
        é   r   N)rA   Zget_global_value_addressÚctypesZc_void_pZfrom_addressÚid)r9   Úenv_nameÚenvZgvaddrÚenvptrr-   r-   r.   Úset_env`   s    zJITDPUCodegen.set_envN)	r*   r+   r,   r   Z_library_classr=   r@   rC   rJ   r-   r-   r-   r.   r0   =   s
   r0   c                   @   s†   e Zd ZdZdd„ Zedd„ ƒZdd„ Zedd	„ ƒZ	d
d„ Z
edd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ ZdS )Ú
DPUContextTc                 C   s   | j  |¡S r>   )Ú_internal_codegenZ_create_empty_module)r9   r6   r-   r-   r.   Úcreate_modulet   s    zDPUContext.create_modulec                 C   s    t dƒ| _t | ¡ |  ¡  d S )Nz
numba.exec)r0   rL   r"   Z
initializeÚrefreshr?   r-   r-   r.   Úinitw   s    

zDPUContext.initc                 C   sR   t }z| j| }W n& tk
r8   t|ƒ}|| j|< Y nX |  |¡ | j ¡  d S r>   )Údpu_function_registryÚ_registriesÚKeyErrorr   Zinstall_registryÚtyping_contextrN   )r9   ÚregistryÚloaderr-   r-   r.   rN   ~   s    
zDPUContext.refreshc                 C   s   | j jS r>   )rL   Útarget_datar?   r-   r-   r.   rV   Š   s    zDPUContext.target_datac                 C   s   | j S r>   )rL   r?   r-   r-   r.   ÚcodegenŽ   s    zDPUContext.codegenc                 C   s
   t  | ¡S r>   )r   ZCPUCallConvr?   r-   r-   r.   Ú	call_conv’   s    zDPUContext.call_convc                 C   s&   t  ||tjd ¡}tj| ||dd�S )zŸ
        From the given *envptr* (a pointer to a _dynfunc.Environment object),
        get a EnvBody allowing structured access to environment fields.
        Zoffsetof_env_bodyT)ÚrefZcast_ref)r   Úpointer_addr    Ú
_impl_infor   ZEnvBody)r9   ÚbuilderrI   Zbody_ptrr-   r-   r.   Úget_env_body–   s      ÿzDPUContext.get_env_bodyc                 C   sX   |   |j|  | j¡¡}| |¡}|  |¡}|j|| jjd� |  ||¡}| 	| j
||¡S )N)Z	debug_msg)Zdeclare_env_globalrB   Úget_env_nameÚfndescÚloadZget_python_apiZemit_environment_sentryrG   r]   Úget_env_managerÚenvironment)r9   r\   ZenvgvZenvargZpyapiZenv_bodyr-   r-   r.   ra       s     
ÿ

 ÿzDPUContext.get_env_managerc                 C   s   t j||tjd |d�S )z~
        From the given *genptr* (a pointer to a _dynfunc.Generator object),
        get a pointer to its state area.
        Zoffsetof_generator_state)Úreturn_type)r   rZ   r    r[   )r9   r\   Zgenptrrc   r-   r-   r.   Úget_generator_state¬   s    üzDPUContext.get_generator_statec                 C   s$   | j rt || j ¡ | tj¡ d S r>   )Zfastmathr   Zrewrite_moduleZadd_linking_libraryr"   Úlibrary)r9   Úmodre   r-   r-   r.   Úpost_lowering¸   s    zDPUContext.post_loweringFc           
   	   C   sX   |   d¡}| j |j|j¡}t |||j¡}t| ||||||d�}	|	 	¡  | 
|¡ d S )NÚwrapper)Úcall_helperÚrelease_gil)rM   rX   Zget_function_typeÚrestypeZargtypesÚllirÚFunctionZllvm_func_namer   ÚbuildZadd_ir_module)
r9   re   r_   rH   ri   rj   Zwrapper_moduleZfntyZwrapper_calleer\   r-   r-   r.   Úcreate_cpython_wrapper¾   s$    
  ÿù	z!DPUContext.create_cpython_wrapperc                 C   s   d S r>   r-   )r9   re   r_   rH   ri   r-   r-   r.   Úcreate_cfunc_wrapperÒ   s    zDPUContext.create_cfunc_wrapperc                 C   sV   |  |j¡}d|jf }t | ¡ |j d¡d ||||f¡}|j |  	|¡|¡ |S )zö
        Returns
        -------
        (cfunc, fnptr)

        - cfunc
            callable function (Can be None)
        - fnptr
            callable function address
        - env
            an execution environment (from _dynfunc)
        zcompiled wrapper for %rÚ.éÿÿÿÿ)
Zget_pointer_to_functionZllvm_cpython_wrapper_nameÚqualnamer    Zmake_functionZlookup_moduleÚsplitrW   rJ   r^   )r9   re   r_   rH   ZfnptrÚdocZcfuncr-   r-   r.   Úget_executableÖ   s    ÿù	zDPUContext.get_executableN)F)r*   r+   r,   Zallow_dynamic_globalsrM   r   rO   rN   ÚpropertyrV   rW   r   rX   r]   ra   rd   rg   ro   rp   rv   r-   r-   r-   r.   rK   p   s$   



 ÿ
rK   c                   @   s"   e Zd ZdZdZejdd„ ƒZdS )Ú_NestedContextNc              	   c   s6   | j | jf}z|| _ || _d V  W 5 |\| _ | _X d S r>   )Ú_typing_contextÚ_target_context)r9   rS   Útarget_contextZ
old_nestedr-   r-   r.   Únestedý   s    
z_NestedContext.nested)r*   r+   r,   ry   rz   Ú
contextlibÚcontextmanagerr|   r-   r-   r-   r.   rx   ù   s   rx   c                   @   sP   e Zd ZejZeƒ Zedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZd	d
„ ZdS )Ú	DPUTargetc                 C   s   t | j| jƒS r>   )rK   rS   Z_target_namer?   r-   r-   r.   Ú_toplevel_target_context  s    z"DPUTarget._toplevel_target_contextc                 C   s   t  ¡ S r>   )r   ÚContextr?   r-   r-   r.   Ú_toplevel_typing_context  s    z"DPUTarget._toplevel_typing_contextc                 C   s   | j j}|dk	r|S | jS dS )z5
        The target context for DPU targets.
        N)Ú_nestedrz   r€   ©r9   r|   r-   r-   r.   r{     s    zDPUTarget.target_contextc                 C   s   | j j}|dk	r|S | jS dS )z5
        The typing context for CPU targets.
        N)rƒ   ry   r‚   r„   r-   r-   r.   rS   "  s    zDPUTarget.typing_contextc                 C   s   | j  ||¡S )zˆ
        A context manager temporarily replacing the contexts with the
        given ones, for the current thread of execution.
        )rƒ   r|   )r9   rS   r{   r-   r-   r.   Únested_context-  s    zDPUTarget.nested_contextN)r*   r+   r,   r   ZCPUTargetOptionsr:   rx   rƒ   r   r€   r‚   rw   r{   rS   r…   r-   r-   r-   r.   r   	  s   





r   c                   @   s   e Zd ZeZdS )ÚDPUDispatcherN)r*   r+   r,   Ú
dpu_targetÚtargetdescrr-   r-   r-   r.   r†   :  s   r†   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Údjitc                 O   s   || _ || _d S r>   )Ú_argsÚ_kwargs)r9   ÚargsÚkwargsr-   r-   r.   Ú__init__F  s    zdjit.__init__c                 G   s6   t |ƒdk st‚|r|d }n
| jd }|| _|  ¡ S )Né   r   )ÚlenÚAssertionErrorrŠ   Úpy_funcÚdispatcher_wrapper)r9   rŒ   Úfuncr-   r-   r.   Ú__call__J  s    

zdjit.__call__c                 C   s   t td  S )z(
        Returns the dispatcher
        r/   )r   r
   r?   r-   r-   r.   Úget_dispatcherT  s    zdjit.get_dispatcherc                 C   sH   |   ¡ }i }d| jkrd|d< tj}d| jkr8| jd }|| j||d�S )NÚnopythonTÚpipeline_class)r’   Ztargetoptionsr˜   )r–   r‹   r#   ZCompilerr’   )r9   ÚdispZtoptr˜   r-   r-   r.   r“   Z  s    


ýzdjit.dispatcher_wrapperN)r*   r+   r,   rŽ   r•   r–   r“   r-   r-   r-   r.   r‰   E  s   
r‰   c                 C   s   |   ¡ S r>   )Zget_dummy_value)Úcontextr\   ÚtyÚpyvalr-   r-   r.   Úconstant_dummyv  s    r�   c                 C   s$   |   ||j|j¡}|  |||j|¡S r>   )Zget_constant_genericZliteral_typeZliteral_valueÚcast)rš   r\   ÚfromtyÚtotyÚvalZlitr-   r-   r.   Úliteral_int_to_number|  s      ÿr¢   c                 C   s   |   |¡}||ƒS r>   ©Úget_value_type©rš   r\   r›   rœ   Zltyr-   r-   r.   Ú	const_int…  s    
r¦   c                 C   s   |   |¡}||ƒS r>   r£   r¥   r-   r-   r.   Úconst_floatŒ  s    
r§   ©Útargetc                 C   s   |||ƒ}dd„ }||fS )Nc                 S   s
   |j |Ž S r>   ©Úsub©Zcgctxr\   ZtyargsZllargsr-   r-   r.   rW   —  s    zintrin_add.<locals>.codegenr-   ©ZtyctxÚxÚyr<   rW   r-   r-   r.   Ú
intrin_add“  s    
r°   c                 C   s(   t | tjƒr$t |tjƒr$dd„ }|S d S )Nc                 S   s
   t | |ƒS r>   )r°   )r®   r¯   r-   r-   r.   Úimpl¢  s    zol_add.<locals>.impl)Ú
isinstancer   ÚInteger)r®   r¯   r±   r-   r-   r.   Úol_addž  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dd„ Z	dd„ Z
dd„ ZdS )ÚTestTargetHierarchySelectionzÈThis tests that the target hierarchy is scanned in the right order,
    that appropriate functions are selected based on what's available and that
    the DPU target is distinctly different to the CPUc                 C   s@   |   tj¡ |   tj¡ |  ttjƒd¡ |  ttjƒd¡ dS )zËChecks that the DPU registry only contains the things added

        This test must be first to execute among all tests in this file to
        ensure the no lazily loaded entries are added yet.
        rD   é   N)ZassertFalserP   Z	functionsZgetattrsÚassertEqualr�   ZcastsÚ	constantsr?   r-   r-   r.   Útest_0_dpu_registry­  s    z0TestTargetHierarchySelection.test_0_dpu_registryc                    st   dd„ ‰ t ˆ dd�dd„ ƒ}t ˆ dd�dd	„ ƒ}tƒ ‡ fd
d„ƒ}tƒ ‡ fdd„ƒ}|  |ƒ d¡ |  |ƒ d¡ d S )Nc                 S   s   d S r>   r-   ©r®   r-   r-   r.   Úmy_func»  s    zATestTargetHierarchySelection.test_specialise_gpu.<locals>.my_funcÚgenericr¨   c                 S   s   dd„ }|S )Nc                 S   s   d|  S ©NrD   r-   rº   r-   r-   r.   r±   Á  s    zSTestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func1.<locals>.implr-   ©r®   r±   r-   r-   r.   Úol_my_func1¿  s    zETestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func1Úgpuc                 S   s   dd„ }|S )Nc                 S   s   d|  S ©Né
   r-   rº   r-   r-   r.   r±   É  s    zSTestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func2.<locals>.implr-   r¾   r-   r-   r.   Úol_my_func2Ç  s    zETestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func2c                      s   ˆ dƒS ©Né   r-   r-   ©r»   r-   r.   Údpu_fooÎ  s    zATestTargetHierarchySelection.test_specialise_gpu.<locals>.dpu_fooc                      s   ˆ dƒS rÄ   r-   r-   rÆ   r-   r.   Úcpu_fooÒ  s    zATestTargetHierarchySelection.test_specialise_gpu.<locals>.cpu_foor¶   é   ©r   r‰   r   ÚassertPreciseEqual)r9   r¿   rÃ   rÇ   rÈ   r-   rÆ   r.   Útest_specialise_gpuº  s    



z0TestTargetHierarchySelection.test_specialise_gpuc                    sˆ   dd„ ‰ t ˆ dd�dd„ ƒ}t ˆ dd�dd	„ ƒ}t ˆ d
d�dd„ ƒ}tƒ ‡ fdd„ƒ}tƒ ‡ fdd„ƒ}|  |ƒ d¡ |  |ƒ d¡ d S )Nc                 S   s   d S r>   r-   rº   r-   r-   r.   r»   Ý  s    zATestTargetHierarchySelection.test_specialise_dpu.<locals>.my_funcr¼   r¨   c                 S   s   dd„ }|S )Nc                 S   s   d|  S r½   r-   rº   r-   r-   r.   r±   ã  s    zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func1.<locals>.implr-   r¾   r-   r-   r.   r¿   á  s    zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func1rÀ   c                 S   s   dd„ }|S )Nc                 S   s   d|  S rÁ   r-   rº   r-   r-   r.   r±   ë  s    zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func2.<locals>.implr-   r¾   r-   r-   r.   rÃ   é  s    zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func2r/   c                 S   s   dd„ }|S )Nc                 S   s   d|  S )Néd   r-   rº   r-   r-   r.   r±   ó  s    zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func3.<locals>.implr-   r¾   r-   r-   r.   Úol_my_func3ñ  s    zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func3c                      s   ˆ dƒS rÄ   r-   r-   rÆ   r-   r.   rÇ   ø  s    zATestTargetHierarchySelection.test_specialise_dpu.<locals>.dpu_fooc                      s   ˆ dƒS rÄ   r-   r-   rÆ   r-   r.   rÈ   ü  s    zATestTargetHierarchySelection.test_specialise_dpu.<locals>.cpu_fooé]   rÉ   rÊ   )r9   r¿   rÃ   rÎ   rÇ   rÈ   r-   rÆ   r.   Útest_specialise_dpuÜ  s    





z0TestTargetHierarchySelection.test_specialise_dpuc              	      s†   dd„ ‰ t ˆ dd�dd„ ƒ}tdd�‡ fd	d
„ƒ}tjtjf}|  |¡�}|ƒ  W 5 Q R X ddddg}|D ]}|  |t|jƒ¡ qjd S )Nc                 S   s   d S r>   r-   rº   r-   r-   r.   r»     s    zJTestTargetHierarchySelection.test_no_specialisation_found.<locals>.my_funcÚcudar¨   c                 S   s   dd„ S )Nc                 S   s   d S r>   r-   rº   r-   r-   r.   Ú<lambda>  ó    zdTestTargetHierarchySelection.test_no_specialisation_found.<locals>.ol_my_func_cuda.<locals>.<lambda>r-   rº   r-   r-   r.   Úol_my_func_cuda  s    zRTestTargetHierarchySelection.test_no_specialisation_found.<locals>.ol_my_func_cudaT©r—   c                      s   ˆ dƒ d S r½   r-   r-   rÆ   r-   r.   rÇ     s    zJTestTargetHierarchySelection.test_no_specialisation_found.<locals>.dpu_fooú8Function resolution cannot find any matches for functionz-test_no_specialisation_found.<locals>.my_funczfor the current target:z''numba.tests.test_target_extension.DPU')	r   r‰   r   ÚUnsupportedErrorÚTypingErrorÚassertRaisesÚassertInÚstrÚ	exception)r9   rÔ   rÇ   ÚacceptÚraisesÚmsgsÚmsgr-   rÆ   r.   Útest_no_specialisation_found  s    

ýz9TestTargetHierarchySelection.test_no_specialisation_foundc              	   C   sJ   |   tj¡�}tdd�dd„ ƒ}|ƒ  W 5 Q R X d}|  |t|jƒ¡ d S )NÚinvalid_silicon)Ú_targetc                   S   s   d S r>   r-   r-   r-   r-   r.   Úfoo%  s    zATestTargetHierarchySelection.test_invalid_target_jit.<locals>.fooú1No target is registered against 'invalid_silicon')rÙ   r   ZNumbaValueErrorr   rÚ   rÛ   rÜ   )r9   rÞ   rä   rà   r-   r-   r.   Útest_invalid_target_jit"  s    
z4TestTargetHierarchySelection.test_invalid_target_jitc              	      sd   dd„ ‰ |   tj¡�0}tˆ dd�dd„ ƒ}t‡ fdd„ƒ}|ƒ  W 5 Q R X d	}|  |t|jƒ¡ d S )
Nc                   S   s   d S r>   r-   r-   r-   r-   r.   Úbar0  s    zFTestTargetHierarchySelection.test_invalid_target_overload.<locals>.barrâ   r¨   c                   S   s   dd„ S )Nc                   S   s   d S r>   r-   r-   r-   r-   r.   rÒ   8  rÓ   z[TestTargetHierarchySelection.test_invalid_target_overload.<locals>.ol_bar.<locals>.<lambda>r-   r-   r-   r-   r.   Úol_bar6  s    zITestTargetHierarchySelection.test_invalid_target_overload.<locals>.ol_barc                      s
   ˆ ƒ  d S r>   r-   r-   ©rç   r-   r.   rä   :  s    zFTestTargetHierarchySelection.test_invalid_target_overload.<locals>.foorå   )rÙ   r   rØ   r   r   rÚ   rÛ   rÜ   )r9   rÞ   rè   rä   rà   r-   ré   r.   Útest_invalid_target_overload.  s    

z9TestTargetHierarchySelection.test_invalid_target_overloadc              	      s‚  t dd�dd„ ƒ‰t dd�dd„ ƒ‰t dd�d	d
„ ƒ‰ t‡ fdd„ƒ}|  |ƒ d¡ t‡fdd„ƒ}|  |ƒ d¡ t‡fdd„ƒ}tjtjf}|  |¡�}|ƒ  W 5 Q R X dddg}|D ]}|  |t|j	ƒ¡ q¸t
dd�‡fdd„ƒ}|  |ƒ d¡ t
dd�‡fdd„ƒ}	|  |	ƒ d¡ t
dd�‡ fdd„ƒ}
tjtjf}|  |¡�}|
ƒ  W 5 Q R X dddg}|D ]}|  |t|j	ƒ¡ �qdd S )!z£
        Test to make sure that targets can share generic implementations and
        cannot reach implementations that are not in their target hierarchy.
        r¼   r¨   c                 S   s   |||ƒ}dd„ }||fS )Nc                 S   s
   |j |Ž S r>   )Úmulr¬   r-   r-   r.   rW   N  s    zcTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_generic.<locals>.codegenr-   r­   r-   r-   r.   Úintrin_math_genericJ  s    
zRTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_genericr/   c                 S   s   |||ƒ}dd„ }||fS )Nc                 S   s
   |j |Ž S r>   rª   r¬   r-   r-   r.   rW   W  s    z_TestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_dpu.<locals>.codegenr-   r­   r-   r-   r.   Úintrin_math_dpuS  s    
zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_dpur   c                 S   s   |||ƒ}dd„ }||fS )Nc                 S   s
   |j |Ž S r>   )Úaddr¬   r-   r-   r.   rW   `  s    z_TestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_cpu.<locals>.codegenr-   r­   r-   r-   r.   Úintrin_math_cpu\  s    
zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_cpuc                      s
   ˆ ddƒS ©Nr¶   é   r-   r-   ©rï   r-   r.   Úcpu_foo_specificf  s    zOTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_specificrÅ   c                      s
   ˆ ddƒS rð   r-   r-   ©rì   r-   r.   Úcpu_foo_genericm  s    zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_genericé   c                      s
   ˆ ddƒS rð   r-   r-   ©rí   r-   r.   Úcpu_foo_dput  s    zJTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_dpurÖ   zintrinsic intrin_math_dpuzfor the current targetTrÕ   c                      s
   ˆ ddƒS rð   r-   r-   r÷   r-   r.   Údpu_foo_specificƒ  s    zOTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_specificrr   c                      s
   ˆ ddƒS rð   r-   r-   rô   r-   r.   Údpu_foo_genericŠ  s    zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_genericc                      s
   ˆ ddƒS rð   r-   r-   rò   r-   r.   Údpu_foo_cpu‘  s    zJTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_cpuzintrinsic intrin_math_cpuN)r   r   r·   r   r×   rØ   rÙ   rÚ   rÛ   rÜ   r‰   )r9   ró   rõ   rø   rÝ   rÞ   rß   rà   rù   rú   rû   r-   )rï   rí   rì   r.   Útest_intrinsic_selectionC  sP    


	þþz5TestTargetHierarchySelection.test_intrinsic_selectionc              	      sØ   dd„ ‰t dd�‡fdd„ƒ‰ttjdddd	id
�‡fdd„ƒ}ttjddd	id
�dd„ ƒ}dd„ ‰ tˆ ddd	id
�dd„ ƒ}ddlm} |dƒ�" t	d	d�‡ fdd„ƒ}|ƒ }W 5 Q R X ddl
m} |  ||j¡ d S )Nc                 S   s\   |j |j kr|S |j |j k r.| ||  |¡¡S |jrF| ||  |¡¡S | ||  |¡¡S d S r>   )ZbitwidthÚtruncr¤   ÚsignedZsextZzext)rš   r\   r¡   rŸ   r    r-   r-   r.   Úcast_integer   s    zKTestTargetHierarchySelection.test_overload_allocation.<locals>.cast_integerr/   r¨   c                    s8   ‡ fdd„}ddl m} t tj¡}||||ƒ}||fS )z;Intrinsic to call into the allocator for Array
            c                    s4   |\}}ˆ | |||j d tjƒ}| j |||¡}|S r½   )rŒ   r   Zuint32ÚnrtZmeminfo_alloc_aligned)rš   r\   Ú	signaturerŒ   Ú	allocsizeÚalignZ	align_u32Zmeminfo©rÿ   r-   r.   rW   ³  s     ÿ
ÿz\TestTargetHierarchySelection.test_overload_allocation.<locals>.intrin_alloc.<locals>.codegenr   )r  )Znumba.core.typingr  r   ZMemInfoPointerZvoidptr)Ú	typingctxr  r  rW   r  Zmipr<   r  r-   r.   Úintrin_alloc¯  s
    zKTestTargetHierarchySelection.test_overload_allocation.<locals>.intrin_allocÚ	_allocater—   T)r©   Zjit_optionsc                    s   ‡ fdd„}|S )Nc                    s
   ˆ ||ƒS r>   r-   )Úclsr  r  ©r  r-   r.   r±   Æ  s    zaTestTargetHierarchySelection.test_overload_allocation.<locals>._ol_arr_allocate_dpu.<locals>.implr-   )r  r  r  r±   r	  r-   r.   Ú_ol_arr_allocate_dpuÃ  s    zSTestTargetHierarchySelection.test_overload_allocation.<locals>._ol_arr_allocate_dpuc                 S   s   dd„ }|S )Nc                 S   s   t j | d¡S rÄ   )r   ÚArrayr  )Únr-   r-   r.   r±   Ì  s    zZTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_empty_impl.<locals>.implr-   )r  r±   r-   r-   r.   Úol_empty_implÊ  s    zLTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_empty_implc                   S   s   d S r>   r-   r-   r-   r-   r.   Úbuffer_funcÐ  s    zJTestTargetHierarchySelection.test_overload_allocation.<locals>.buffer_funcc                  S   s   dd„ } | S )Nc                   S   s
   t  d¡S rÁ   )ÚnpÚemptyr-   r-   r-   r.   r±   Õ  s    z`TestTargetHierarchySelection.test_overload_allocation.<locals>.ol_buffer_func_impl.<locals>.implr-   )r±   r-   r-   r.   Úol_buffer_func_implÓ  s    zRTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_buffer_func_implr   )r   rÕ   c                      s   ˆ ƒ S r>   r-   r-   )r  r-   r.   rä   Ý  s    zBTestTargetHierarchySelection.test_overload_allocation.<locals>.foo)r   )r   r   r   r  r   r  r  Únumba.core.target_extensionr   r‰   Únumba.core.runtimer   ZassertIsInstanceZMemInfo)r9   r
  r  r  r   rä   Úrr   r-   )r  rÿ   r  r.   Útest_overload_allocationŸ  s&    
ÿ


z5TestTargetHierarchySelection.test_overload_allocationN)r*   r+   r,   Ú__doc__r¹   rÌ   rÐ   rá   ræ   rê   rü   r  r-   r-   r-   r.   rµ   ¨  s   "*\rµ   c                   @   s   e Zd ZdZdd„ ZdS )ÚTestTargetOffloada   In this use case the CPU compilation pipeline is extended with a new
     compilation pass that runs just prior to lowering. The pass looks for
     function calls and when it finds one it sees if there's a DPU function
     available that is a valid overload for the function call. If there is one
     then it swaps the CPU implementation out for a DPU implementation. This
     producing an "offload" effect.
    c                    sÂ   d‰t tjdd�dd„ ƒ}tdd�dd	„ ƒ}|  |d
ƒd¡ tdd	„ ƒ}|  |d
ƒt d
¡¡ tddd�G ‡fdd„dtƒƒ‰ G ‡ fdd„dtƒ}t|d�dd	„ ƒ}|  |d
ƒdt 	d
¡f¡ d S )NFr/   r¨   c                 S   s   dd„ }|S )Nc                 S   s   dS )Nç    ¼,Ar-   rº   r-   r-   r.   Údpu_sin_implõ  s    zQTestTargetOffload.test_basic_offload.<locals>.ol_np_sin_DPU.<locals>.dpu_sin_implr-   )r®   r  r-   r-   r.   Úol_np_sin_DPUó  s    z;TestTargetOffload.test_basic_offload.<locals>.ol_np_sin_DPUTrÕ   c                 S   s
   t  | ¡S r>   ©r  Úsinrº   r-   r-   r.   rä   û  s    z1TestTargetOffload.test_basic_offload.<locals>.fooé   r  c                 S   s
   t  | ¡S r>   r  rº   r-   r-   r.   rä     s    )Zmutates_CFGZanalysis_onlyc                       s$   e Zd ZdZdd„ Z‡ fdd„ZdS )z@TestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcherÚDispatcherSwitcherc                 S   s   t  | ¡ d S r>   )r&   rŽ   r?   r-   r-   r.   rŽ     s    zITestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcher.__init__c                    sø   |j }d}|j ¡ D ]Þ}| d¡D ]Î}|j|jj }d}t|ƒ�ª z8| |j	|j
| ji ¡}t|ƒ}	|	jj}
|
 ||¡ W nV tk
rÐ } z8ˆ r®d|› d|› d|› �}t|ƒ W Y ¢W 5 Q R £ q"W 5 d }~X Y nX |jj|
_||_d}W 5 Q R X q"q|S )NFÚcallr/   z+Failed to find and compile an overload for z for z due to T)Úfunc_irÚblocksÚvaluesZ
find_exprsÚtypemapr”   r6   r   Zget_call_typer  Z	calltypesrŒ   r   rˆ   r{   Zget_functionÚ	ExceptionÚprintZ	targetctxZ_codelib_stackr©   )r9   Ústater   ZmutatedZblkr  ÚfunctionZtnamer<   r™   Zhw_ctxÚerà   ©Ú_DEBUGr-   r.   Úrun_pass  s6    

ýÿ$ÿ	zITestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcher.run_passN)r*   r+   r,   Ú_namerŽ   r+  r-   r)  r-   r.   r  	  s   r  c                       s   e Zd Z‡ fdd„ZdS )z@TestTargetOffload.test_basic_offload.<locals>.DPUOffloadCompilerc                    s&   t  | j¡}| ˆ t¡ | ¡  |gS r>   )r%   Zdefine_nopython_pipeliner&  Zadd_pass_afterr(   Úfinalize)r9   Úpm©r  r-   r.   Údefine_pipelinesD  s    zQTestTargetOffload.test_basic_offload.<locals>.DPUOffloadCompiler.define_pipelinesN)r*   r+   r,   r0  r-   r/  r-   r.   ÚDPUOffloadCompilerC  s   r1  )r˜   c                 S   s   t  | ¡t  | ¡fS r>   )r  r  Úcosrº   r-   r-   r.   rä   L  s    )
r   r  r  r‰   rË   r   r'   r&   r$   r2  )r9   r  rä   r1  r-   )r  r*  r.   Útest_basic_offloadî  s    



9	
z$TestTargetOffload.test_basic_offloadN)r*   r+   r,   r  r3  r-   r-   r-   r.   r  å  s   r  Ú__main__)_r  ZunittestZnumba.tests.supportr   r}   rE   ÚoperatorÚ	functoolsr   Únumpyr  Znumbar   r   Znumba.extendingr   r   r   r  r	   r
   r   r   r   r   r   Z
numba.corer   r   r   Znumba.core.dispatcherr   Znumba.core.descriptorsr   r   r   r   Znumba.core.baser   Znumba.core.compiler_lockr   r   Znumba.core.codegenr   r   Znumba.core.callwrapperr   Znumba.core.imputilsr   r   r    Zllvmlite.bindingZbindingr5   Zllvmliter!   rl   r  r"   r#   Znumba.core.compilerr$   r%   Znumba.core.compiler_machineryr&   r'   Znumba.core.typed_passesr(   r)   r0   rP   rK   Úobjectrx   r   r‡   r†   r‰   Zlower_constantZDummyr�   Z
lower_castZIntegerLiteralr³   r¢   r¦   ÚFloatr§   r°   rî   r´   rµ   r  r*   Úmainr-   r-   r-   r.   Ú<module>   sv   $	. 
-*









	  ?n
