
    xKg_n                     &   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e      ZFeFed<    G d de+      ZG e1       ZH G d d e&      ZI G d! d"e!      ZJ eJd      ZK G d# d$e      ZLeLeed   <    G d% d&e      ZMeMeed   <   eHj                  ej                        d'        ZPeHj                  ej                  ej                        d(        ZTeHj                  ej                        d)        ZUeHj                  ej                        d*        ZW ed+      d,        ZX eej                  d+      d-        ZZ G d. d/e      Z[ G d0 d1e      Z\e]d2k(  r ej                          yy)3a  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                       e Zd Zy)DPUN)__name__
__module____qualname__     e/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/tests/test_target_extension.pyr-   r-   3   s    r2   r-   dpuc                   (    e Zd ZeZd Zd Zd Zd Zy)JITDPUCodegenc                    | j                         |d<   t        j                  j                         j                  }|j                  d      rd}n|j                  d      rd}nd}||d<   d|d	<   | j                  |d
<   t        j                  t        j                  j                        }d|j                  v rd|d<   y y )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sigs        r3   _customize_tm_optionsz#JITDPUCodegen._customize_tm_optionsC   s    002yy,,.33??5!"K__U#K#K&+ #//
 		 ? ?@CNN"!GEN #r2   c                 "    | j                         S N)_get_host_cpu_featuresrL   s    r3   _customize_tm_featuresz$JITDPUCodegen._customize_tm_featuresZ   s    **,,r2   c                 :    | j                   j                  |       y rS   )_engine
add_module)rL   modules     r3   _add_modulezJITDPUCodegen._add_module^   s    'r2   c                     | j                   j                  |      }t        j                  dz  j	                  |      }t        j                  t        |            |d<   y)zrSet the environment address.

        Update the GlobalVariable named *env_name* to the address of *env*.
           r   N)rX   get_global_value_addressctypesc_void_pfrom_addressid)rL   env_nameenvgvaddrenvptrs        r3   set_envzJITDPUCodegen.set_enva   sG    
 66x@//A%33F;OOBsG,q	r2   N)	r.   r/   r0   r   _library_classrQ   rV   r[   rg   r1   r2   r3   r6   r6   >   s     $N".-(-r2   r6   c                       e Zd ZdZd Zed        Zd Zed        Z	d Z
ed        Zd Zd	 Zd
 Zd Z	 ddZd Zd Zy)
DPUContextTc                 8    | j                   j                  |      S rS   )_internal_codegen_create_empty_module)rL   rF   s     r3   create_modulezDPUContext.create_moduleu   s    %%::4@@r2   c                 n    t        d      | _        t        j                  |        | j	                          y )Nz
numba.exec)r6   rl   r%   
initializerefreshrU   s    r3   initzDPUContext.initx   s&    !.|!<r2   c                     t         }	 | j                  |   }| j	                  |       | j
                  j                          y # t        $ r t        |      }|| j                  |<   Y Qw xY wrS   )dpu_function_registry_registriesKeyErrorr    install_registrytyping_contextrq   )rL   registryloaders      r3   rq   zDPUContext.refresh   sk    (	0%%h/F 	h' 	##%  	0#H-F)/DX&	0s   A #A)(A)c                 .    | j                   j                  S rS   )rl   target_datarU   s    r3   r|   zDPUContext.target_data   s    %%111r2   c                     | j                   S rS   )rl   rU   s    r3   codegenzDPUContext.codegen   s    %%%r2   c                 ,    t        j                  |       S rS   )r   CPUCallConvrU   s    r3   	call_convzDPUContext.call_conv   s    ##D))r2   c                     t        j                  ||t        j                  d         }t	        j
                  | ||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)rL   builderrf   body_ptrs       r3   get_env_bodyzDPUContext.get_env_body   s>    
 &&VX001DE
 {{4hFFr2   c                 \   | j                  |j                  | j                  | j                              }|j	                  |      }| j                  |      }|j                  || j                  j                         | j                  ||      }|j                  | j                  ||      S )N)	debug_msg)declare_env_globalrZ   get_env_namefndescloadget_python_apiemit_environment_sentryrc   r   get_env_managerenvironment)rL   r   envgvenvargpyapienv_bodys         r3   r   zDPUContext.get_env_manager   s    ''NND--dkk:
 e$##G,%%dkk22 	& 	
 $$Wf5$$T%5%5xHHr2   c                 V    t        j                  ||t        j                  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   )rL   r   genptrr   s       r3   get_generator_statezDPUContext.get_generator_state   s/    
 "" :;#	
 	
r2   c                     | j                   r t        j                  || j                          |j                  t        j
                         y rS   )fastmathr   rewrite_moduleadd_linking_libraryr%   library)rL   modr   s      r3   post_loweringzDPUContext.post_lowering   s/    ==''T]];##EMM2r2   c           	      .   | j                  d      }| j                  j                  |j                  |j                        }t        j                  |||j                        }t        | ||||||      }	|	j                          |j                  |       y )Nwrapper)call_helperrelease_gil)rn   r   get_function_typerestypeargtypesllirFunctionllvm_func_namer   buildadd_ir_module)
rL   r   r   rd   r   r   wrapper_modulefntywrapper_calleer   s
             r3   create_cpython_wrapperz!DPUContext.create_cpython_wrapper   s     ++I6~~//PD&"7"7
  ##
 	n-r2   c                      y rS   r1   )rL   r   r   rd   r   s        r3   create_cfunc_wrapperzDPUContext.create_cfunc_wrapper   s    r2   c                 :   |j                  |j                        }d|j                  }t        j                  |j                         |j                  j                  d      d   ||||f      }|j                  j                  | 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 .)
get_pointer_to_functionllvm_cpython_wrapper_namequalnamer#   make_functionlookup_modulesplitr~   rg   r   )rL   r   r   rd   fnptrdoccfuncs          r3   get_executablezDPUContext.get_executable   s     //,,
 ,2??<&&  "OO!!#&r*J
 	 1 1& 93?r2   N)F)r.   r/   r0   allow_dynamic_globalsrn   r   rr   rq   propertyr|   r~   r   r   r   r   r   r   r   r   r   r1   r2   r3   rj   rj   q   s     A  
& 2 2& * *G
I

3 >C.(r2   rj   c                   d    e Zd Zej                  Zed        Zed        Ze	d        Z
e	d        Zy)	DPUTargetc                 B    t        | j                  | j                        S rS   )rj   rx   _target_namerU   s    r3   _toplevel_target_contextz"DPUTarget._toplevel_target_context   s     $--t/@/@AAr2   c                 *    t        j                         S rS   )r   ContextrU   s    r3   _toplevel_typing_contextz"DPUTarget._toplevel_typing_context  s     ~~r2   c                     | j                   S )z5
        The target context for DPU targets.
        )r   rU   s    r3   target_contextzDPUTarget.target_context      
 ,,,r2   c                     | j                   S )z5
        The typing context for CPU targets.
        )r   rU   s    r3   rx   zDPUTarget.typing_context  r   r2   N)r.   r/   r0   r   CPUTargetOptionsrM   r   r   r   r   r   rx   r1   r2   r3   r   r      s]    ""GB B     - - - -r2   r   c                   "     e Zd ZeZ fdZ xZS )DPUDispatcherc                 d    t        d      5  t        | 	  |      cd d d        S # 1 sw Y   y xY w)Nr4   )r   supercompile)rL   rP   	__class__s     r3   r   zDPUDispatcher.compile  s#    U#7?3' $##s   &/)r.   r/   r0   
dpu_targettargetdescrr   __classcell__)r   s   @r3   r   r     s    K( (r2   r   c                   $    e Zd Zd Zd Zd Zd Zy)djitc                      || _         || _        y rS   )_args_kwargs)rL   argskwargss      r3   __init__zdjit.__init__+  s    
r2   c                 ~    t        |      dk  sJ |r|d   }n| j                  d   }|| _        | j                         S )N   r   )lenr   py_funcdispatcher_wrapper)rL   r   funcs      r3   __call__zdjit.__call__/  sA    4y1}}7D::a=D&&((r2   c                 "    t         t        d      S )z(
        Returns the dispatcher
        r4   )r   r   rU   s    r3   get_dispatcherzdjit.get_dispatcher9  s     #?5#9::r2   c                     | j                         }i }d| j                  v rd|d<   t        j                  }d| j                  v r| j                  d   } || j                  ||      S )NnopythonTpipeline_class)r   targetoptionsr   )r   r   r&   Compilerr   )rL   disptoptr   s       r3   r   zdjit.dispatcher_wrapper?  sm    ""$%#D "**t||+!\\*:;NLL)
 	
r2   N)r.   r/   r0   r   r   r   r   r1   r2   r3   r   r   *  s    );
r2   r   c                 "    | j                         S rS   )get_dummy_value)contextr   typyvals       r3   constant_dummyr   [  s    ""$$r2   c                     | j                  ||j                  |j                        }| j                  |||j                  |      S rS   )get_constant_genericliteral_typeliteral_valuecast)r   r   fromtytotyvallits         r3   literal_int_to_numberr  a  sB    

&
&$$f&:&:C <<f&9&94@@r2   c                 4    | j                  |      } ||      S rS   get_value_typer   r   r   r   ltys        r3   	const_intr
  j      

 
 
$Cu:r2   c                 4    | j                  |      } ||      S rS   r  r  s        r3   const_floatr  q  r  r2   targetc                 "     |||      }d }||fS )Nc                       |j                   | S rS   subcgctxr   tyargsllargss       r3   r~   zintrin_add.<locals>.codegen|  s    w{{F##r2   r1   tyctxxyrP   r~   s        r3   
intrin_addr  x  s    
Aq'C$ <r2   c                 x    t        | t        j                        r t        |t        j                        rd }|S y y )Nc                     t        | |      S rS   )r  )r  r  s     r3   implzol_add.<locals>.impl  s    a##r2   )
isinstancer   Integer)r  r  r  s      r3   ol_addr"    s2    !U]]#
1emm(D	$  )E#r2   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)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                 $   | j                  t        j                         | j                  t        j                         | j	                  t        t        j                        d       | j	                  t        t        j                        d       y)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.
        r]      N)assertFalsert   	functionsgetattrsassertEqualr   casts	constantsrU   s    r3   test_0_dpu_registryz0TestTargetHierarchySelection.test_0_dpu_registry  sf     	.889.77828891=2<<=qAr2   c                     d t        d      d        }t        d      d        }t               fd       }t               fd       }| j                   |       d	       | j                   |       d
       y )Nc                      y rS   r1   r  s    r3   my_funczATestTargetHierarchySelection.test_specialise_gpu.<locals>.my_func      r2   genericr  c                     d }|S )Nc                     d| z   S Nr]   r1   r0  s    r3   r  zSTestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func1.<locals>.impl      1ur2   r1   r  r  s     r3   ol_my_func1zETestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func1       Kr2   gpuc                     d }|S )Nc                     d| z   S N
   r1   r0  s    r3   r  zSTestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func2.<locals>.impl      Avr2   r1   r8  s     r3   ol_my_func2zETestTargetHierarchySelection.test_specialise_gpu.<locals>.ol_my_func2       Kr2   c                        d      S N   r1   r1  s   r3   dpu_foozATestTargetHierarchySelection.test_specialise_gpu.<locals>.dpu_foo      1:r2   c                        d      S rD  r1   rF  s   r3   cpu_foozATestTargetHierarchySelection.test_specialise_gpu.<locals>.cpu_foo  rH  r2   r&     r   r   r   assertPreciseEqual)rL   r9  rA  rG  rJ  r1  s        @r3   test_specialise_gpuz0TestTargetHierarchySelection.test_specialise_gpu  s    	 
')	,	 
-	 
'%	(	 
)	 
	 
	 
	 
	
 		1-	1-r2   c                 "   d t        d      d        }t        d      d        }t        d      d        }t               fd	       }t               fd
       }| j                   |       d       | j                   |       d       y )Nc                      y rS   r1   r0  s    r3   r1  zATestTargetHierarchySelection.test_specialise_dpu.<locals>.my_func  r2  r2   r3  r  c                     d }|S )Nc                     d| z   S r6  r1   r0  s    r3   r  zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func1.<locals>.impl  r7  r2   r1   r8  s     r3   r9  zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func1  r:  r2   r;  c                     d }|S )Nc                     d| z   S r>  r1   r0  s    r3   r  zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func2.<locals>.impl  r@  r2   r1   r8  s     r3   rA  zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func2  rB  r2   r4   c                     d }|S )Nc                     d| z   S )Nd   r1   r0  s    r3   r  zSTestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func3.<locals>.impl  s    Qwr2   r1   r8  s     r3   ol_my_func3zETestTargetHierarchySelection.test_specialise_dpu.<locals>.ol_my_func3  s     Kr2   c                        d      S rD  r1   rF  s   r3   rG  zATestTargetHierarchySelection.test_specialise_dpu.<locals>.dpu_foo  rH  r2   c                        d      S rD  r1   rF  s   r3   rJ  zATestTargetHierarchySelection.test_specialise_dpu.<locals>.cpu_foo  rH  r2   ]   rK  rL  )rL   r9  rA  rX  rG  rJ  r1  s         @r3   test_specialise_dpuz0TestTargetHierarchySelection.test_specialise_dpu  s    	 
')	,	 
-	 
'%	(	 
)	 
'%	(	 
)	 
	 
	 
	 
	
 		2.	1-r2   c                 T   d t        d      d        }t        d      fd       }t        j                  t        j                  f}| j                  |      5 } |        d d d        g d}|D ]'  }| j                  |t        j                               ) y # 1 sw Y   :xY w)	Nc                      y rS   r1   r0  s    r3   r1  zJTestTargetHierarchySelection.test_no_specialisation_found.<locals>.my_func  r2  r2   cudar  c                     d S )Nc                      y rS   r1   r0  s    r3   <lambda>zdTestTargetHierarchySelection.test_no_specialisation_found.<locals>.ol_my_func_cuda.<locals>.<lambda>  s    Tr2   r1   r0  s    r3   ol_my_func_cudazRTestTargetHierarchySelection.test_no_specialisation_found.<locals>.ol_my_func_cuda  s    !!r2   Tr   c                        d       y r6  r1   rF  s   r3   rG  zJTestTargetHierarchySelection.test_no_specialisation_found.<locals>.dpu_foo  s
    AJr2   )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)rL   rc  rG  acceptraisesmsgsmsgr1  s          @r3   test_no_specialisation_foundz9TestTargetHierarchySelection.test_no_specialisation_found  s    	 
'&	)	" 
*	" 
t		 
	
 ))6+=+=>v&&I ';
 CMM#s6#3#345  '&s   BB'c                     | j                  t        j                        5 }t        d      d        } |        d d d        d}| j	                  |t        j                               y # 1 sw Y   1xY w)Ninvalid_silicon)_targetc                       y rS   r1   r1   r2   r3   foozATestTargetHierarchySelection.test_invalid_target_jit.<locals>.foo
  s    r2   1No target is registered against 'invalid_silicon')ri  r   NonexistentTargetErrorr   rj  rk  rl  )rL   rn  rv  rp  s       r3   test_invalid_target_jitz4TestTargetHierarchySelection.test_invalid_target_jit  sf    v<<=+, - E > Bc3v//01 >=s   A**A3c                    d | j                  t        j                        5 }t        d      d        }t        fd       } |        d d d        d}| j                  |t        j                               y # 1 sw Y   1xY w)Nc                       y rS   r1   r1   r2   r3   barzFTestTargetHierarchySelection.test_invalid_target_overload.<locals>.bar  r2  r2   rs  r  c                      d S )Nc                       y rS   r1   r1   r2   r3   rb  z[TestTargetHierarchySelection.test_invalid_target_overload.<locals>.ol_bar.<locals>.<lambda>  s    r2   r1   r1   r2   r3   ol_barzITestTargetHierarchySelection.test_invalid_target_overload.<locals>.ol_bar  s    $$r2   c                                y rS   r1   )r|  s   r3   rv  zFTestTargetHierarchySelection.test_invalid_target_overload.<locals>.foo  s    r2   rw  )ri  r   rx  r   r   rj  rk  rl  )rL   rn  r  rv  rp  r|  s        @r3   test_invalid_target_overloadz9TestTargetHierarchySelection.test_invalid_target_overload  s    	
 v<<=c"34% 5%   E > Bc3v//01 >=s   )A==Bc                    t        d      d        t        d      d        t        d      d        t        fd       }| j                   |       d	       t        fd
       }| j                   |       d       t        fd       }t        j                  t        j
                  f}| j                  |      5 } |        ddd       g d}|D ]'  }| j                  |t        j                               ) t        d      fd       }| j                   |       d       t        d      fd       }	| j                   |	       d       t        d      fd       }
t        j                  t        j
                  f}| j                  |      5 } |
        ddd       g d}|D ]'  }| j                  |t        j                               ) y# 1 sw Y   xY w# 1 sw Y   GxY w)z
        Test to make sure that targets can share generic implementations and
        cannot reach implementations that are not in their target hierarchy.
        r3  r  c                 "     |||      }d }||fS )Nc                       |j                   | S rS   )mulr  s       r3   r~   zcTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_generic.<locals>.codegen3      "w{{F++r2   r1   r  s        r3   intrin_math_genericzRTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_generic/      Aq'C, <r2   r4   c                 "     |||      }d }||fS )Nc                       |j                   | S rS   r  r  s       r3   r~   z_TestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_dpu.<locals>.codegen<  r  r2   r1   r  s        r3   intrin_math_dpuzNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_dpu8  r  r2   r   c                 "     |||      }d }||fS )Nc                       |j                   | S rS   )addr  s       r3   r~   z_TestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_cpu.<locals>.codegenE  r  r2   r1   r  s        r3   intrin_math_cpuzNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.intrin_math_cpuA  r  r2   c                        dd      S Nr&     r1   r  s   r3   cpu_foo_specificzOTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_specificK      "1a((r2   rE  c                        dd      S r  r1   r  s   r3   cpu_foo_genericzNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_genericR      &q!,,r2      c                        dd      S r  r1   r  s   r3   cpu_foo_dpuzJTestTargetHierarchySelection.test_intrinsic_selection.<locals>.cpu_foo_dpuY  r  r2   N)rf  zintrinsic intrin_math_dpufor the current targetTrd  c                        dd      S r  r1   r  s   r3   dpu_foo_specificzOTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_specifich  r  r2   r   c                        dd      S r  r1   r  s   r3   dpu_foo_genericzNTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_generico  r  r2   c                        dd      S r  r1   r  s   r3   dpu_foo_cpuzJTestTargetHierarchySelection.test_intrinsic_selection.<locals>.dpu_foo_cpuv  r  r2   )rf  zintrinsic intrin_math_cpur  )r
   r   r*  r   rg  rh  ri  rj  rk  rl  r   )rL   r  r  r  rm  rn  ro  rp  r  r  r  r  r  r  s              @@@r3   test_intrinsic_selectionz5TestTargetHierarchySelection.test_intrinsic_selection(  s    
)	$	  
%	  
%	 	  
!	  
%	 	  
!	  
	) 
	) 	)+Q/ 
	- 
	- 	*B/ 
	) 
	) ))6+=+=>v&&M '+ CMM#s6#3#345  
t		) 
	) 	)+R0 
t		- 
	- 	*B/ 
t		) 
	) ))6+=+=>v&&M '+ CMM#s6#3#345 G '&: '&s   GG,G),G5c                   	
 d 	t        d      	fd       
t        t        j                  ddddi      
fd	       }t	        t
        j                  dddi      d
        }d t	        dddi      d        }ddlm}  |d      5  t        d      fd       } |       }d d d        ddl
m} | j                  |j                         y # 1 sw Y   ,xY w)Nc                 H   |j                   |j                   k(  r|S |j                   |j                   k  r!|j                  || j                  |            S |j                  r!|j	                  || j                  |            S |j                  || j                  |            S rS   )bitwidthtruncr  signedsextzext)r   r   r  r   r  s        r3   cast_integerzKTestTargetHierarchySelection.test_overload_allocation.<locals>.cast_integer  s    }}/
0}}S'*@*@*FGG||C)?)?)EFF ||C)?)?)EFFr2   r4   r  c                 |    fd}ddl m} t        j                  t        j                        } ||||      }||fS )z;Intrinsic to call into the allocator for Array
            c                     |\  }} | |||j                   d   t        j                        }| j                  j	                  |||      }|S r6  )r   r   uint32nrtmeminfo_alloc_aligned)	r   r   	signaturer   	allocsizealign	align_u32meminfor  s	           r3   r~   z\TestTargetHierarchySelection.test_overload_allocation.<locals>.intrin_alloc.<locals>.codegen  sS    %)"E )'5)2):ELLJ	!++;;GY<EGr2   r   )r  )numba.core.typingr  r   MemInfoPointervoidptr)	typingctxr  r  r~   r  miprP   r  s          r3   intrin_alloczKTestTargetHierarchySelection.test_overload_allocation.<locals>.intrin_alloc  s9    	 4&&u}}5CCE2C<r2   	_allocater   T)r  jit_optionsc                     fd}|S )Nc                      ||      S rS   r1   )clsr  r  r  s      r3   r  zaTestTargetHierarchySelection.test_overload_allocation.<locals>._ol_arr_allocate_dpu.<locals>.impl  s    #Iu55r2   r1   )r  r  r  r  r  s       r3   _ol_arr_allocate_dpuzSTestTargetHierarchySelection.test_overload_allocation.<locals>._ol_arr_allocate_dpu  s    6Kr2   c                     d }|S )Nc                 B    t         j                  j                  | d      S rD  )r   Arrayr  )ns    r3   r  zZTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_empty_impl.<locals>.impl  s    {{,,Q22r2   r1   )r  r  s     r3   ol_empty_implzLTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_empty_impl  s    3Kr2   c                       y rS   r1   r1   r2   r3   buffer_funczJTestTargetHierarchySelection.test_overload_allocation.<locals>.buffer_func  r2  r2   c                      d } | S )Nc                  ,    t        j                  d      S r>  )npemptyr1   r2   r3   r  z`TestTargetHierarchySelection.test_overload_allocation.<locals>.ol_buffer_func_impl.<locals>.impl  s    xx|#r2   r1   )r  s    r3   ol_buffer_func_implzRTestTargetHierarchySelection.test_overload_allocation.<locals>.ol_buffer_func_impl  s    $Kr2   r   )r   rd  c                               S rS   r1   )r  s   r3   rv  zBTestTargetHierarchySelection.test_overload_allocation.<locals>.foo  s    "}$r2   )r  )r
   r	   r   r  r   r  r  numba.core.target_extensionr   r   numba.core.runtimer  assertIsInstanceMemInfo)rL   r  r  r  r   rv  rr  r  r  r  s           @@@r3   test_overload_allocationz5TestTargetHierarchySelection.test_overload_allocation  s    	G 
%	 	  
!	 & 
ekk;u+5d*;
=	
=	
 
"((5z$6G	H	 
I	
	 
+e*T9J	K	 
L	
 	@ U#4 % !%A	 $
 	+a- $#s   CCc                    | j                         \  }}t         |             }t        |dd      d        }| j                  t        j
                  d      5  t        t        j                  |            d        }d d d        t        t        j                  t        j                  d d d   |            d        }y # 1 sw Y   CxY w)	Ndpu_onlyr4   r  c                     d }|S )Nc                      y)N*   r1   )objs    r3   impzcTestTargetHierarchySelection.test_overload_attribute_target.<locals>.ov_dummy_dpu_attr.<locals>.imp  s    r2   r1   )r  r  s     r3   ov_dummy_dpu_attrzVTestTargetHierarchySelection.test_overload_attribute_target.<locals>.ov_dummy_dpu_attr  s     Jr2   zUnknown attribute 'dpu_only'c                     | j                   S rS   r  r0  s    r3   illegal_target_attr_usez\TestTargetHierarchySelection.test_overload_attribute_target.<locals>.illegal_target_attr_use  s    zz!r2   r]   c                 "    |j                   | d<   y )Nr   r  )resdummys     r3   cuda_target_attr_usezYTestTargetHierarchySelection.test_overload_attribute_target.<locals>.cuda_target_attr_use  s    ^^CFr2   )make_dummy_typer"   r   assertRaisesRegexr   rh  r   r   int64r   void)rL   MyDummyMyDummyTypemydummy_typer  r  r  s          r3   test_overload_attribute_targetz;TestTargetHierarchySelection.test_overload_attribute_target  s    #335gi(	KE	B	 
C	 ##F$6$6$BD%++l+," -"D 
ejjSqS)<8	9	$ 
:	$D Ds   %B??CN)r.   r/   r0   __doc__r-  rN  r\  rq  ry  r  r  r  r  r1   r2   r3   r$  r$    s;    9B .D(.T68
22*Z6xC.J$r2   r$  c                       e Zd ZdZd Zy)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                    dt        t        j                  d      d        }t        d      d        }| j	                   |d      d	       t
        d
        }| j	                   |d      t        j                  d             t        dd       G fddt                      G fddt              }t        |      d        }| j	                   |d      d	t        j                  d      f       y )NFr4   r  c                     d }|S )Nc                      y)N    ,Ar1   r0  s    r3   dpu_sin_implzQTestTargetOffload.test_basic_offload.<locals>.ol_np_sin_DPU.<locals>.dpu_sin_impl  s    r2   r1   )r  r  s     r3   ol_np_sin_DPUz;TestTargetOffload.test_basic_offload.<locals>.ol_np_sin_DPU  s       r2   Trd  c                 ,    t        j                  |       S rS   r  sinr0  s    r3   rv  z1TestTargetOffload.test_basic_offload.<locals>.foo      66!9r2      r  c                 ,    t        j                  |       S rS   r  r0  s    r3   rv  z1TestTargetOffload.test_basic_offload.<locals>.foo  r  r2   )mutates_CFGanalysis_onlyc                   "    e Zd ZdZd Z fdZy)@TestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcherDispatcherSwitcherc                 .    t        j                  |        y rS   )r)   r   rU   s    r3   r   zITestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcher.__init__  s    %%d+r2   c           
         |j                   }d}|j                  j                         D ]  }|j                  d      D ]  }|j                  |j
                  j                     }d}t        |      5  	 |j                  |j                  |j                  |   j                  i       }t        |      }	|	j                  j                  }
|
j                  ||       |j$                  j&                  |
_        ||_        d}d d d          |S # t         $ r*}rd| d| d| }t#        |       Y d }~d d d        d }~ww xY w# 1 sw Y   xY w)NFcallr4   z+Failed to find and compile an overload for z for z due to T)func_irblocksvalues
find_exprstypemapr   rF   r   get_call_typer  	calltypesr   r   r   r   get_function	Exceptionprint	targetctx_codelib_stackr  )rL   stater  mutatedblkr  functiontnamerP   r   hw_ctxerp  _DEBUGs                r3   run_passzITestTargetOffload.test_basic_offload.<locals>.DispatcherSwitcher.run_pass  sN   --">>002C !$v 6#(==#@ % -U3)&.&<&<$)OO$)OOD$9$>$>$&'"
 (C5'I)-)9)9)H)H & 3 3Hc B !& > > #1 +0DK&*GE 43 !7 3Z 3 $- )#)+88@zug N223)6 %(
 %*#J (' 43) 43s7   1E3A'D$E	D?	D:	-E:D?	?EEN)r.   r/   r0   _namer   r  )r  s   r3   r  r     s    (E,0r2   r  c                       e Zd Z fdZy)@TestTargetOffload.test_basic_offload.<locals>.DPUOffloadCompilerc                     t        j                  | j                        }|j                  t               |j                          |gS rS   )r(   define_nopython_pipeliner  add_pass_afterr+   finalize)rL   pmr  s     r3   define_pipelineszQTestTargetOffload.test_basic_offload.<locals>.DPUOffloadCompiler.define_pipelinesC  s9    '@@L!!"46GHtr2   N)r.   r/   r0   r"  )r  s   r3   DPUOffloadCompilerr  B  s    r2   r#  )r   c                 V    t        j                  |       t        j                  |       fS rS   )r  r  cosr0  s    r3   rv  z1TestTargetOffload.test_basic_offload.<locals>.fooK  s    66!9bffQi''r2   )
r   r  r  r   rM  r   r*   r)   r'   r%  )rL   r  rv  r#  r  r  s       @@r3   test_basic_offloadz$TestTargetOffload.test_basic_offload  s     
"&&	'	  
(	  
t		 
	 	A1 
	 
	 	Aq	2	5	>6	 6	 
?6	r	 	 
/	0	( 
1	( 	A266!9(=>r2   N)r.   r/   r0   r  r&  r1   r2   r3   r  r    s    b?r2   r  __main__)_r  unittestnumba.tests.supportr   r_   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bindingrC   llvmliter$   r   r  r%   r&   numba.core.compilerr'   r(   numba.core.compiler_machineryr)   r*   numba.core.typed_passesr+   r-   r6   rt   rj   r   r   r   r   lower_constantDummyr   
lower_castIntegerLiteralr!  r  r
  Floatr  r  r  r"  r$  r  r.   mainr1   r2   r3   <module>rD     s    (   %  > >   3 2 , 3 + + ' 9  9 0 8 +    $  @ E 5#   
*-J *-\ !
 
E ER-  -: u
(J ( /< OE* +
%
< %
T (,_U# $ %%ekk2% 3%
 !!%"6"6FA GA %%emm4 5 %%ekk2 3 %  
(,,u% &T$8 T$n
k? k?\ zHMMO r2   