
    ܙd                        d dl Z d dl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 d d
lmZmZmZmZmZ d                    edg          Z G d de          Z G d de          Z G d de          Z ee          Z  G d de          Z!eee<   e!eee         <   d Z"e"eee         <    G d de          Z# G d de j$                  Z%dS )    N)contextmanager)cached_property)njit)errorscputyping)TargetDescriptor)TargetConfigurationStack)BasicRetarget)overload)dispatcher_registryCPUDispatcherCPUtarget_registryjit_registry.	CustomCPUc                       e Zd ZdZdS )r   zExtend from the CPU target
    N)__name__
__module____qualname____doc__     <lib/python3.11/site-packages/numba/tests/test_retargeting.pyr   r      s         Dr   c                   ,    e Zd ZdZdZed             ZdS )_NestedContextNc              #      K   | j         | j        f}	 || _         || _        d V  |\  | _         | _        d S # |\  | _         | _        w xY wN)_typing_context_target_context)selftyping_contexttarget_context
old_nesteds       r   nestedz_NestedContext.nested$   sg      )4+??
	D#1D #1D EEE9C6D $"6"6"66D $"6CCCCs	   5 A)r   r   r   r    r!   r   r&   r   r   r   r   r       s<        OOD D ^D D Dr   r   c                       e Zd Zej        Z e            Zed             Z	ed             Z
ed             Zed             Zd ZdS )CustomTargetDescrc                 @    t          j        | j        | j                  S r   )r   
CPUContextr#   _target_namer"   s    r   _toplevel_target_contextz*CustomTargetDescr._toplevel_target_context4   s     ~d143DEEEr   c                 (    t          j                    S r   )r   Contextr,   s    r   _toplevel_typing_contextz*CustomTargetDescr._toplevel_typing_context9   s     ~r   c                 0    | j         j        }||S | j        S )z5
        The target context for DPU targets.
        )_nestedr!   r-   r"   r&   s     r   r$   z CustomTargetDescr.target_context>   "    
 -M00r   c                 0    | j         j        }||S | j        S )z5
        The typing context for CPU targets.
        )r2   r    r0   r3   s     r   r#   z CustomTargetDescr.typing_contextI   r4   r   c                 8    | j                             ||          S )z
        A context manager temporarily replacing the contexts with the
        given ones, for the current thread of execution.
        )r2   r&   )r"   r#   r$   s      r   nested_contextz CustomTargetDescr.nested_contextT   s    
 |"">>BBBr   N)r   r   r   r   CPUTargetOptionsoptionsr   r2   r   r-   r0   propertyr$   r#   r7   r   r   r   r(   r(   0   s        "GnGF F _F     _  1 1 X1 1 1 X1C C C C Cr   r(   c                       e Zd ZeZdS )CustomCPUDispatcherN)r   r   r   custom_targettargetdescrr   r   r   r<   r<   _   s        KKKr   r<   c                  @    d|vsJ d|vsJ t          | dt          i|S )Ntarget_target)r   CUSTOM_TARGET)argskwargss     r   
custom_jitrE   g   s<    6!!!!F""""7}7777r   c                   *    e Zd Zed             Zd ZdS )CustomCPURetargetc                     t           S r   )rB   r,   s    r   output_targetzCustomCPURetarget.output_targets   s    r   c                 L     t          t                    |j                  }|S )NrA   )r   rB   py_func)r"   cpu_dispkernels      r   compile_retargetz"CustomCPURetarget.compile_retargetw   s$    ,m,,,X-=>>r   N)r   r   r   r:   rI   rO   r   r   r   rG   rG   r   s<          X    r   rG   c                   Z    e Zd Zd Zd Zed             Zd Zd Zd Z	d Z
d Zd	 Zd
 ZdS )TestRetargetingc                     t          d          d             t           fd            }t           d             }t                      | _        t                      | _        d S )Nr   rK   c                     | dz   S )zo
            This has a fixed target to "cpu".
            Cannot be used in CUSTOM_TARGET target.
            
   r   xs    r   fixed_targetz+TestRetargeting.setUp.<locals>.fixed_target   s     r6Mr   c                       |           dz   S )z
            This has a flexible target, but uses a fixed target function.
            Cannot be used in CUSTOM_TARGET target.
            d   r   )rV   rW   s    r   flex_call_fixedz.TestRetargeting.setUp.<locals>.flex_call_fixed   s      <??S((r   c                     | dz   S )zf
            This has a flexible target.
            Can be used in CUSTOM_TARGET target.
            i  r   rU   s    r   flex_targetz*TestRetargeting.setUp.<locals>.flex_target   s     t8Or   )r   locals	functionsrG   retarget)r"   rZ   r\   rW   s      @r   setUpzTestRetargeting.setUp}   s     
e				 	 
		 
	) 	) 	) 	) 
	) 
	 	 
	  )++r   c                 4    t          j        | j                  S r   )r
   switch_targetr_   r,   s    r   rb   zTestRetargeting.switch_target   s    '5dmDDDr   c              #      K   |                      t          j                  5 }d V  d d d            n# 1 swxY w Y   |                     t           dt          |j                             d S )Nz != cpu)assertRaisesr   
NumbaErrorassertInrB   str	exception)r"   raisess     r   check_retarget_errorz$TestRetargeting.check_retarget_error   s      v011 	VEEE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	///V5E1F1FGGGGGs   377c                     | j         j                                        }|                     |d         |d         z   d           d S )Nhitmissr   )r_   cachestatsassertGreater)r"   ro   s     r   check_non_empty_cachez%TestRetargeting.check_non_empty_cache   sC    #))++ 	5<%-7;;;;;r   c                 ,   | j         d         | j         d         t          fd            } |d          }|                     |d           | j        j                                        }|                     |t          dd                     d S )NrW   r\   c                 4     |           }  |           } | S r   r   rV   rW   r\   s    r   fooz'TestRetargeting.test_case0.<locals>.foo   "    QAAAHr   {   im  r   )rl   rm   )r^   r   assertEqualr_   rn   ro   dict)r"   ru   rro   rW   r\   s       @@r   
test_case0zTestRetargeting.test_case0   s    ~n5n]3		 	 	 	 	 
	
 CHHO,,,#))++ 3 3 344444r   c                    | j         d         t          fd            }|                                 5   |d          }d d d            n# 1 swxY w Y   |                     |d           |                                  d S )Nr\   c                      |           } | S r   r   )rV   r\   s    r   ru   z'TestRetargeting.test_case1.<locals>.foo   s    AAHr   rw   ic  )r^   r   rb   rx   rq   )r"   ru   rz   r\   s      @r   
test_case1zTestRetargeting.test_case1   s    n]3		 	 	 	 
	 !! 	 	CA	 	 	 	 	 	 	 	 	 	 	 	 	 	 	J'''""$$$$$s   AAAc                 ,   | j         d         | j         d         t          fd            }|                                 5  |                                 5   |d           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zK
        The non-nested call into fixed_target should raise error.
        rW   r\   c                 4     |           }  |           } | S r   r   rt   s    r   ru   z'TestRetargeting.test_case2.<locals>.foo   rv   r   rw   Nr^   r   rj   rb   )r"   ru   rW   r\   s     @@r   
test_case2zTestRetargeting.test_case2   s2    ~n5n]3		 	 	 	 	 
	
 &&(( 	 	##%%  C              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	6   B	A1%B	1A5	5B	8A5	9B		BBc                 ,   | j         d         | j         d         t          fd            }|                                 5  |                                 5   |d           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zF
        The nested call into fixed_target should raise error
        r\   rZ   c                 4     |           }  |           } | S r   r   rV   rZ   r\   s    r   ru   z'TestRetargeting.test_case3.<locals>.foo   $    ""AAAHr   rw   Nr   )r"   ru   rZ   r\   s     @@r   
test_case3zTestRetargeting.test_case3   s3    n]3.):;		 	 	 	 	 
	
 &&(( 	 	##%%  C              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r   c                 n   | j         d         | j         d          d          }|                     |d           t          fd            }|                                 5  |                                 5   |d           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z
        Same as case2 but flex_call_fixed() is invoked outside of CUSTOM_TARGET
        target before the switch_target.
        r\   rZ   rw      c                 4     |           }  |           } | S r   r   r   s    r   ru   z'TestRetargeting.test_case4.<locals>.foo   r   r   N)r^   rx   r   rj   rb   )r"   rz   ru   rZ   r\   s      @@r   
test_case4zTestRetargeting.test_case4   sX   
 n]3.):;OC  N+++		 	 	 	 	 
	
 &&(( 	 	##%%  C              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s6   %B*:BB*B	B*B	B**B.1B.c                    d t          t                    d             }t          d          d             }t          d             }t          fd            } |d          }|                     |d	           |                                 5   |d          }|                     |d
           ddd           n# 1 swxY w Y   |                                  dS )zA
        Tests overload resolution with target switching
        c                     d S r   r   rU   s    r   overloaded_funcz3TestRetargeting.test_case5.<locals>.overloaded_func  s    Dr   )r@   c                     d }|S )Nc                     dS )Nin  r   rU   s    r   implzRTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_target.<locals>.impl      ur   r   rV   r   s     r    ol_overloaded_func_custom_targetzDTestRetargeting.test_case5.<locals>.ol_overloaded_func_custom_target	        Kr   r   c                     d }|S )Nc                     dS )Niz  r   rU   s    r   r   zHTestRetargeting.test_case5.<locals>.ol_overloaded_func_cpu.<locals>.impl  r   r   r   r   s     r   ol_overloaded_func_cpuz:TestRetargeting.test_case5.<locals>.ol_overloaded_func_cpu  r   r   c                     d S r   r   rU   s    r   flex_resolve_overloadz9TestRetargeting.test_case5.<locals>.flex_resolve_overload  s    Fr   c                      |  |           z   S r   r   )rV   r   s    r   ru   z'TestRetargeting.test_case5.<locals>.foo  s    q))))r   rw   i2{  i  N)r   rB   r   rx   rb   rq   )r"   r   r   r   ru   rz   r   s         @r   
test_case5zTestRetargeting.test_case5  sm   
	 	 	 
/-	8	8	8	 	 
9	8	
 
/%	0	0	0	 	 
1	0	
 
	 	 
	 
	* 	* 	* 	* 
	* CHHK(((!! 	- 	-CAQ,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	""$$$$$s   "CC
CN)r   r   r   r`   rb   r   rj   rq   r{   r~   r   r   r   r   r   r   r   rQ   rQ   |   s        , , ,@E E E H H ^H
< < <5 5 5 % % %  "  "  *#% #% #% #% #%r   rQ   )&unittest
contextlibr   	functoolsr   numbar   
numba.corer   r   r   numba.core.descriptorsr	   numba.core.dispatcherr
   numba.core.retargetr   numba.core.extendingr   numba.core.target_extensionr   r   r   r   r   joinr   rB   r   objectr   r(   r=   r<   rE   rG   TestCaserQ   r   r   r   <module>r      s{    % % % % % % % % % % % %       * * * * * * * * * * 3 3 3 3 3 3 : : : : : : - - - - - - ) ) ) ) ) )              (K011	 	 	 	 	 	 	 	D D D D DV D D D )C )C )C )C )C( )C )C )CX "!-00         -       "+ 6I OM2 38 8 8 0:_]+ ,
       h% h% h% h% h%h' h% h% h% h% h%r   