o
    ۾i_n                     @   s  d Z ddlZddlm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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l2m3Z3 ddl
m4Z4 ddl5m6Z7 ddl8m9Z: ddl;m<Z< ddlm=Z= ddl>m?Z?m@Z@ ddlAmBZBmCZC ddlDmEZE G dd deZFeFed< G dd de+ZGe1 ZHG dd  d e&ZIG d!d" d"e!ZJeJdZKG d#d$ d$eZLeLeed < G d%d& d&eZMeMeed < eHNejOd'd( ZPeHQejRejSd)d* ZTeHNejSd+d, ZUeHNejVd-d. ZWedd/d0d1 ZXeejYdd/d2d3 ZZG d4d5 d5eZ[G d6d7 d7eZ\e]d8krfe^  dS dS )9a  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overload_attributeoverload_classmethod	intrinsic)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)typeof)_dynfunc)ir)rtsys)compiler)CompilerBaseDefaultPassBuilder)FunctionPassregister_pass)PreLowerStripPhisc                   @   s   e Zd ZdS )DPUN)__name__
__module____qualname__ r/   r/   U/home/ubuntu/.local/lib/python3.10/site-packages/numba/tests/test_target_extension.pyr+   3   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   s~   |   |d< tj j}|drd}n
|drd}nd}||d< d|d	< | j|d
< ttjj	}d|j
v r=d|d< d S d S )Nr   x86staticppcpicdefaultreloc
jitdefault	codemodelfeaturesjitT)_get_host_cpu_namellTargetfrom_default_triplename
startswith_tm_featuresr   pysignaturecreate_target_machine
parameters)selfoptionsarchreloc_modelsigr/   r/   r0   _customize_tm_optionsC   s   



z#JITDPUCodegen._customize_tm_optionsc                 C      |   S N)_get_host_cpu_featuresrG   r/   r/   r0   _customize_tm_featuresZ      z$JITDPUCodegen._customize_tm_featuresc                 C   s   | j | d S rN   )_engine
add_module)rG   moduler/   r/   r0   _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)rS   get_global_value_addressctypesc_void_pfrom_addressid)rG   env_nameenvgvaddrenvptrr/   r/   r0   set_enva   s   zJITDPUCodegen.set_envN)	r,   r-   r.   r   _library_classrL   rQ   rV   ra   r/   r/   r/   r0   r2   >   s    r2   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 rN   )_internal_codegen_create_empty_module)rG   rA   r/   r/   r0   create_moduleu   s   zDPUContext.create_modulec                 C   s    t d| _t|  |   d S )Nz
numba.exec)r2   rd   r$   
initializerefreshrP   r/   r/   r0   initx   s   

zDPUContext.initc                 C   sP   t }z| j| }W n ty   t|}|| j|< Y nw | | | j  d S rN   )dpu_function_registry_registriesKeyErrorr   install_registrytyping_contextrh   )rG   registryloaderr/   r/   r0   rh      s   
zDPUContext.refreshc                 C   s   | j jS rN   )rd   target_datarP   r/   r/   r0   rq      rR   zDPUContext.target_datac                 C      | j S rN   )rd   rP   r/   r/   r0   codegen   s   zDPUContext.codegenc                 C   
   t | S rN   )r   CPUCallConvrP   r/   r/   r0   	call_conv      
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.
        offsetof_env_bodyT)refcast_ref)r   pointer_addr"   
_impl_infor   EnvBody)rG   builderr`   body_ptrr/   r/   r0   get_env_body   s   zDPUContext.get_env_bodyc                 C   sX   |  |j| | j}||}| |}|j|| jjd | ||}|	| j
||S )N)	debug_msg)declare_env_globalrU   get_env_namefndescloadget_python_apiemit_environment_sentryr]   r   get_env_managerenvironment)rG   r~   envgvenvargpyapienv_bodyr/   r/   r0   r      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.
        offsetof_generator_state)return_type)r   r{   r"   r|   )rG   r~   genptrr   r/   r/   r0   get_generator_state   s   zDPUContext.get_generator_statec                 C   s$   | j r
t|| j  |tj d S rN   )fastmathr   rewrite_moduleadd_linking_libraryr$   library)rG   modr   r/   r/   r0   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)rf   rv   get_function_typerestypeargtypesllirFunctionllvm_func_namer   buildadd_ir_module)
rG   r   r   r^   r   r   wrapper_modulefntywrapper_calleer~   r/   r/   r0   create_cpython_wrapper   s    
	z!DPUContext.create_cpython_wrapperc                 C      d S rN   r/   )rG   r   r   r^   r   r/   r/   r0   create_cfunc_wrapper      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.)
get_pointer_to_functionllvm_cpython_wrapper_namequalnamer"   make_functionlookup_modulesplitrs   ra   r   )rG   r   r   r^   fnptrdoccfuncr/   r/   r0   get_executable   s   	zDPUContext.get_executableN)F)r,   r-   r.   allow_dynamic_globalsrf   r   ri   rh   propertyrq   rs   r   rv   r   r   r   r   r   r   r   r/   r/   r/   r0   rc   q   s&    




rc   c                   @   sB   e Zd ZejZedd Zedd Ze	dd Z
e	dd Zd	S )
	DPUTargetc                 C   s   t | j| jS rN   )rc   rn   _target_namerP   r/   r/   r0   _toplevel_target_context   s   z"DPUTarget._toplevel_target_contextc                 C   s   t  S rN   )r   ContextrP   r/   r/   r0   _toplevel_typing_context  s   z"DPUTarget._toplevel_typing_contextc                 C   rr   )z5
        The target context for DPU targets.
        )r   rP   r/   r/   r0   target_context     zDPUTarget.target_contextc                 C   rr   )z5
        The typing context for CPU targets.
        )r   rP   r/   r/   r0   rn     r   zDPUTarget.typing_contextN)r,   r-   r.   r   CPUTargetOptionsrH   r   r   r   r   r   rn   r/   r/   r/   r0   r      s    


r   c                       s    e Zd ZeZ fddZ  ZS )DPUDispatcherc                    s8   t d t |W  d    S 1 sw   Y  d S )Nr1   )r   supercompile)rG   rK   	__class__r/   r0   r     s   

$zDPUDispatcher.compile)r,   r-   r.   
dpu_targettargetdescrr   __classcell__r/   r/   r   r0   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 rN   )_args_kwargs)rG   argskwargsr/   r/   r0   __init__+  s   
zdjit.__init__c                 G   s6   t |dk sJ |r|d }n| jd }|| _|  S )N   r   )lenr   py_funcdispatcher_wrapper)rG   r   funcr/   r/   r0   __call__/  s   

zdjit.__call__c                 C   s   t td  S )z(
        Returns the dispatcher
        r1   )r   r   rP   r/   r/   r0   get_dispatcher9  s   zdjit.get_dispatcherc                 C   sH   |   }i }d| jv rd|d< tj}d| jv r| jd }|| j||dS )NnopythonTpipeline_class)r   targetoptionsr   )r   r   r%   Compilerr   )rG   disptoptr   r/   r/   r0   r   ?  s   


zdjit.dispatcher_wrapperN)r,   r-   r.   r   r   r   r   r/   r/   r/   r0   r   *  s
    
r   c                 C   rM   rN   )get_dummy_value)contextr~   typyvalr/   r/   r0   constant_dummy[  rR   r   c                 C   s$   |  ||j|j}| |||j|S rN   )get_constant_genericliteral_typeliteral_valuecast)r   r~   fromtytotyvallitr/   r/   r0   literal_int_to_numbera  s   
r   c                 C      |  |}||S rN   get_value_typer   r~   r   r   ltyr/   r/   r0   	const_intj     
r   c                 C   r   rN   r   r   r/   r/   r0   const_floatq  r   r   targetc                 C      |||}dd }||fS )Nc                 S   
   |j | S rN   subcgctxr~   tyargsllargsr/   r/   r0   rs   |     
zintrin_add.<locals>.codegenr/   tyctxxyrK   rs   r/   r/   r0   
intrin_addx     
r   c                 C   s,   t | tjrt |tjrdd }|S d S d S )Nc                 S   s
   t | |S rN   )r   )r   r   r/   r/   r0   impl  r   zol_add.<locals>.impl)
isinstancer   Integer)r   r   r  r/   r/   r0   ol_add  s   r  c                   @   sX   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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.
        rW      N)assertFalserj   	functionsgetattrsassertEqualr   casts	constantsrP   r/   r/   r0   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   r   rN   r/   r   r/   r/   r0   my_func     zATestTargetHierarchySelection.test_specialise_gpu.<locals>.my_funcgenericr   c                 S      dd }|S )Nc                 S      d|  S NrW   r/   r  r/   r/   r0   r       zSTestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func1.<locals>.implr/   r   r  r/   r/   r0   ol_my_func1     zETestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func1gpuc                 S   r  )Nc                 S   r  N
   r/   r  r/   r/   r0   r    r  zSTestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func2.<locals>.implr/   r  r/   r/   r0   ol_my_func2  r  zETestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func2c                          dS N   r/   r/   r  r/   r0   dpu_foo  rR   zATestTargetHierarchySelection.test_specialise_gpu.<locals>.dpu_fooc                      r  r  r/   r/   r!  r/   r0   cpu_foo  rR   zATestTargetHierarchySelection.test_specialise_gpu.<locals>.cpu_foor     r   r   r   assertPreciseEqual)rG   r  r  r"  r#  r/   r!  r0   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   r   rN   r/   r  r/   r/   r0   r    r  zATestTargetHierarchySelection.test_specialise_dpu.<locals>.my_funcr  r   c                 S   r  )Nc                 S   r  r  r/   r  r/   r/   r0   r    r  zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func1.<locals>.implr/   r  r/   r/   r0   r    r  zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func1r  c                 S   r  )Nc                 S   r  r  r/   r  r/   r/   r0   r    r  zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func2.<locals>.implr/   r  r/   r/   r0   r    r  zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func2r1   c                 S   r  )Nc                 S   r  )Nd   r/   r  r/   r/   r0   r    r  zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func3.<locals>.implr/   r  r/   r/   r0   ol_my_func3  r  zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func3c                      r  r  r/   r/   r!  r/   r0   r"    rR   zATestTargetHierarchySelection.test_specialise_dpu.<locals>.dpu_fooc                      r  r  r/   r/   r!  r/   r0   r#    rR   zATestTargetHierarchySelection.test_specialise_dpu.<locals>.cpu_foo]   r$  r%  )rG   r  r  r)  r"  r#  r/   r!  r0   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 d    n1 s2w   Y  g d}|D ]}| |t|j q=d S )Nc                 S   r   rN   r/   r  r/   r/   r0   r    r  zJTestTargetHierarchySelection.test_no_specialisation_found.<locals>.my_funccudar   c                 S      dd S )Nc                 S   r   rN   r/   r  r/   r/   r0   <lambda>      zdTestTargetHierarchySelection.test_no_specialisation_found.<locals>.ol_my_func_cuda.<locals>.<lambda>r/   r  r/   r/   r0   ol_my_func_cuda  rR   zRTestTargetHierarchySelection.test_no_specialisation_found.<locals>.ol_my_func_cudaTr   c                      s    d d S r  r/   r/   r!  r/   r0   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)rG   r0  r"  acceptraisesmsgsmsgr/   r!  r0   test_no_specialisation_found  s   

z9TestTargetHierarchySelection.test_no_specialisation_foundc                 C   s^   |  tj}tdddd }|  W d    n1 sw   Y  d}| |t|j d S )Ninvalid_silicon)_targetc                   S   r   rN   r/   r/   r/   r/   r0   foo
  r   zATestTargetHierarchySelection.test_invalid_target_jit.<locals>.foo1No target is registered against 'invalid_silicon')r5  r   NonexistentTargetErrorr   r6  r7  r8  )rG   r:  r@  r<  r/   r/   r0   test_invalid_target_jit  s   
z4TestTargetHierarchySelection.test_invalid_target_jitc                    sx   dd  |  tj}t dddd }t fdd}|  W d    n1 s*w   Y  d	}| |t|j d S )
Nc                   S   r   rN   r/   r/   r/   r/   r0   bar  r  zFTestTargetHierarchySelection.test_invalid_target_overload.<locals>.barr>  r   c                   S   r-  )Nc                   S   r   rN   r/   r/   r/   r/   r0   r.    r/  z[TestTargetHierarchySelection.test_invalid_target_overload.<locals>.ol_bar.<locals>.<lambda>r/   r/   r/   r/   r0   ol_bar  rR   zITestTargetHierarchySelection.test_invalid_target_overload.<locals>.ol_barc                      s
      d S rN   r/   r/   rD  r/   r0   r@    rw   zFTestTargetHierarchySelection.test_invalid_target_overload.<locals>.foorA  )r5  r   rB  r   r   r6  r7  r8  )rG   r:  rE  r@  r<  r/   rF  r0   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 d   n1 sZw   Y  g d}|D ]}| |t|j	 qet
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 d   n1 sw   Y  g d}|D ]}| |t|j	 qd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   r   )Nc                 S   r   rN   )mulr   r/   r/   r0   rs   3  r   zcTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_generic.<locals>.codegenr/   r   r/   r/   r0   intrin_math_generic/  r  zRTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_genericr1   c                 S   r   )Nc                 S   r   rN   r   r   r/   r/   r0   rs   <  r   z_TestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_dpu.<locals>.codegenr/   r   r/   r/   r0   intrin_math_dpu8  r  zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_dpur   c                 S   r   )Nc                 S   r   rN   )addr   r/   r/   r0   rs   E  r   z_TestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_cpu.<locals>.codegenr/   r   r/   r/   r0   intrin_math_cpuA  r  zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_cpuc                      
    ddS Nr     r/   r/   rL  r/   r0   cpu_foo_specificK  rw   zOTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_specificr   c                      rM  rN  r/   r/   rI  r/   r0   cpu_foo_genericR  rw   zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_generic   c                      rM  rN  r/   r/   rJ  r/   r0   cpu_foo_dpuY  rw   zJTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_dpuN)r2  zintrinsic intrin_math_dpufor the current targetTr1  c                      rM  rN  r/   r/   rU  r/   r0   dpu_foo_specifich  rw   zOTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_specificr   c                      rM  rN  r/   r/   rR  r/   r0   dpu_foo_generico  rw   zNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_genericc                      rM  rN  r/   r/   rP  r/   r0   dpu_foo_cpuv  rw   zJTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_cpu)r2  zintrinsic intrin_math_cpurW  )r	   r   r  r   r3  r4  r5  r6  r7  r8  r   )rG   rQ  rS  rV  r9  r:  r;  r<  rX  rY  rZ  r/   )rL  rJ  rI  r0   test_intrinsic_selection(  sJ   


	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 d    n1 sbw   Y  ddl
m} | ||j d S )Nc                 S   sX   |j |j kr|S |j |j k r||| |S |jr#||| |S ||| |S rN   )bitwidthtruncr   signedsextzext)r   r~   r   r   r   r/   r/   r0   cast_integer  s   zKTestTargetHierarchySelection.test_overload_allocation.<locals>.cast_integerr1   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   uint32nrtmeminfo_alloc_aligned)r   r~   	signaturer   	allocsizealign	align_u32meminfora  r/   r0   rs     s   
z\TestTargetHierarchySelection.test_overload_allocation.<locals>.intrin_alloc.<locals>.codegenr   )re  )numba.core.typingre  r   MemInfoPointervoidptr)	typingctxrf  rg  rs   re  miprK   rj  r/   r0   intrin_alloc  s
   zKTestTargetHierarchySelection.test_overload_allocation.<locals>.intrin_alloc	_allocater   T)r   jit_optionsc                    s    fdd}|S )Nc                    s
    ||S rN   r/   )clsrf  rg  rp  r/   r0   r    r   zaTestTargetHierarchySelection.test_overload_allocation.<locals>._ol_arr_allocate_dpu.<locals>.implr/   )rs  rf  rg  r  rt  r/   r0   _ol_arr_allocate_dpu  s   zSTestTargetHierarchySelection.test_overload_allocation.<locals>._ol_arr_allocate_dpuc                 S   r  )Nc                 S   s   t j| dS r  )r   Arrayrq  )nr/   r/   r0   r       zZTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_empty_impl.<locals>.implr/   )rw  r  r/   r/   r0   ol_empty_impl     zLTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_empty_implc                   S   r   rN   r/   r/   r/   r/   r0   buffer_func  r  zJTestTargetHierarchySelection.test_overload_allocation.<locals>.buffer_funcc                  S   s   dd } | S )Nc                   S   s
   t dS r  )npemptyr/   r/   r/   r0   r    r   z`TestTargetHierarchySelection.test_overload_allocation.<locals>.ol_buffer_func_impl.<locals>.implr/   )r  r/   r/   r0   ol_buffer_func_impl  rz  zRTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_buffer_func_implr   )r   r1  c                      s     S rN   r/   r/   )r{  r/   r0   r@       zBTestTargetHierarchySelection.test_overload_allocation.<locals>.foo)rc  )r	   r   r   rv  r   r|  r}  numba.core.target_extensionr   r   numba.core.runtimerc  assertIsInstanceMemInfo)rG   ru  ry  r~  r   r@  rrc  r/   )r{  ra  rp  r0   test_overload_allocation  s(   



z5TestTargetHierarchySelection.test_overload_allocationc                 C   s   |   \}}t| }t|ddddd }| tjd tt|dd }W d    n1 s3w   Y  t	t
tjd d d	 |d
d }d S )Ndpu_onlyr1   r   c                 S   r  )Nc                 S      dS )N*   r/   )objr/   r/   r0   imp  r  zcTestTargetHierarchySelection.test_overload_attribute_target.<locals>.ov_dummy_dpu_attr.<locals>.impr/   )r  r  r/   r/   r0   ov_dummy_dpu_attr  r  zVTestTargetHierarchySelection.test_overload_attribute_target.<locals>.ov_dummy_dpu_attrzUnknown attribute 'dpu_only'c                 S   rr   rN   r  r  r/   r/   r0   illegal_target_attr_use  r  z\TestTargetHierarchySelection.test_overload_attribute_target.<locals>.illegal_target_attr_userW   c                 S   s   |j | d< d S )Nr   r  )resdummyr/   r/   r0   cuda_target_attr_use  s   zYTestTargetHierarchySelection.test_overload_attribute_target.<locals>.cuda_target_attr_use)make_dummy_typer!   r   assertRaisesRegexr   r4  r   r   int64r   void)rG   MyDummyMyDummyTypemydummy_typer  r  r  r/   r/   r0   test_overload_attribute_target  s   

	z;TestTargetHierarchySelection.test_overload_attribute_targetN)r,   r-   r.   __doc__r  r'  r+  r=  rC  rG  r[  r  r  r/   r/   r/   r0   r    s    "*\Er  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 )NFr1   r   c                 S   r  )Nc                 S   r  )N    ,Ar/   r  r/   r/   r0   dpu_sin_impl  r  zQTestTargetOffload.test_basic_offload.<locals>.ol_np_sin_DPU.<locals>.dpu_sin_implr/   )r   r  r/   r/   r0   ol_np_sin_DPU  r  z;TestTargetOffload.test_basic_offload.<locals>.ol_np_sin_DPUTr1  c                 S   rt   rN   r|  sinr  r/   r/   r0   r@    rw   z1TestTargetOffload.test_basic_offload.<locals>.foo   r  c                 S   rt   rN   r  r  r/   r/   r0   r@    rw   )mutates_CFG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 rN   )r(   r   rP   r/   r/   r0   r     rx  zITestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcher.__init__c                    s  |j }d}|j D ]w}|dD ]o}|j|jj }d}t|X z||j	|j
| ji }t|}	|	jj}
|
|| W n) tyf } z rVd| d| d| }t| W Y d }~W d    qd }~ww |jj|
_||_d}W d    n1 s{w   Y  qq
|S )NFcallr1   z+Failed to find and compile an overload for z for z due to T)func_irblocksvalues
find_exprstypemapr   rA   r   get_call_typern  	calltypesr   r   r   r   get_function	Exceptionprint	targetctx_codelib_stackr   )rG   stater  mutatedblkr  functiontnamerK   r   hw_ctxer<  _DEBUGr/   r0   run_pass  sN   


	*zITestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcher.run_passN)r,   r-   r.   _namer   r  r/   r  r/   r0   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 rN   )r'   define_nopython_pipeliner  add_pass_afterr*   finalize)rG   pmr  r/   r0   define_pipelinesC  s   zQTestTargetOffload.test_basic_offload.<locals>.DPUOffloadCompiler.define_pipelinesN)r,   r-   r.   r  r/   r  r/   r0   DPUOffloadCompilerB  s    r  )r   c                 S   s   t | t | fS rN   )r|  r  cosr  r/   r/   r0   r@  K  s   )
r   r|  r  r   r&  r   r)   r(   r&   r  )rG   r  r@  r  r/   )r  r  r0   test_basic_offload  s   



9	
z$TestTargetOffload.test_basic_offloadN)r,   r-   r.   r  r  r/   r/   r/   r0   r    s    r  __main__)_r  unittestnumba.tests.supportr   rY   operator	functoolsr   numpyr|  numbar   r   numba.extendingr   r   r   r	   r  r
   r   r   r   r   r   r   
numba.corer   r   r   numba.core.dispatcherr   numba.core.descriptorsr   r   r   r   numba.core.baser   numba.core.compiler_lockr   r   numba.core.codegenr   r   numba.core.callwrapperr   numba.core.imputilsr   r    numba.core.typing.typeofr!   r"   llvmlite.bindingbindingr>   llvmliter#   r   r  r$   r%   numba.core.compilerr&   r'   numba.core.compiler_machineryr(   r)   numba.core.typed_passesr*   r+   r2   rj   rc   r   r   r   r   lower_constantDummyr   
lower_castIntegerLiteralr  r   r   Floatr   r   rK  r  r  r  r,   mainr/   r/   r/   r0   <module>   sx    $	. 

*









	  Y
n