U
    9%em                     @   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 < eFNejOd(d) ZPeFQejRejSd*d+ ZTeFNejSd,d- ZUeFNejVd.d/ ZWedd0d1d2 ZXeejYdd0d3d4 ZZG d5d6 d6eZ[G d7d8 d8eZ\e]d9kre^  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-   `/var/www/html/Darija-Ai-API/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 )TestTargetHierarchySelectionzThis 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 )zChecks 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 )Nd   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_foo8Function 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>.foo1No 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	 qt
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 rd| 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
