
    ܙd                         d Z ddlZddlZddlmZ  G d d          Z G d dej                  Z G d d	e          ZdS )
z
Implement utils for supporting retargeting of dispatchers.

WARNING: Features defined in this file are experimental. The API may change
         without notice.
    N)errorsc                   >    e Zd ZdZej        Zd Zd Zd Z	d Z
d ZdS )RetargetCachez^Cache for retargeted dispatchers.

    The cache uses the original dispatcher as the key.
    c                 T    |                                  | _        d| _        d| _        d S )Nr   )container_type_cache	_stat_hit
_stat_missselfs    3lib/python3.11/site-packages/numba/core/retarget.py__init__zRetargetCache.__init__   s'    ))++    c                     || j         |<   dS )z9Save a dispatcher associated with the given key.
        N)r   )r   	orig_dispnew_disps      r   
save_cachezRetargetCache.save_cache   s     "*Ir   c                     | j                             |          }|| xj        dz  c_        n| xj        dz  c_        |S )z9Load a dispatcher associated with the given key.
        N   )r   getr
   r	   )r   r   outs      r   
load_cachezRetargetCache.load_cache   sC     kooi((;OOq OOONNaNN
r   c                 4    | j                                         S )z+Returns the contents of the cache.
        )r   itemsr   s    r   r   zRetargetCache.items(   s     {  """r   c                      | j         | j        dS )z0Returns stats regarding cache hit/miss.
        )hitmiss)r	   r
   r   s    r   statszRetargetCache.stats-   s     ~t???r   N)__name__
__module____qualname____doc__weakrefWeakKeyDictionaryr   r   r   r   r   r    r   r   r   r      sx          .N  
* * *
  # # #
@ @ @ @ @r   r   c                   R    e Zd ZdZej        d             Zej        d             ZdS )BaseRetargetz/Abstract base class for retargeting logic.
    c                     dS )zCheck that the retarget is compatible.

        This method does not return anything meaningful (e.g. None)
        Incompatibility is signalled via raising an exception.
        Nr%   r   r   s     r   check_compatiblezBaseRetarget.check_compatible6   s	     	r   c                     dS )zRetargets the given dispatcher and returns a new dispatcher-like
        callable. Or, returns the original dispatcher if the the target_backend
        will not change.
        Nr%   r)   s     r   retargetzBaseRetarget.retarget?   	     	r   N)r   r    r!   r"   abcabstractmethodr*   r,   r%   r   r   r'   r'   3   sZ            	    r   r'   c                   n    e Zd ZdZeZd Zej        de	fd            Z
ej        d             Zd Zd ZdS )	BasicRetargeta  A basic retargeting implementation for a single output target.

    This class has two abstract methods/properties that subclasses must define.

    - `output_target` must return output target name.
    - `compile_retarget` must define the logic to retarget the given dispatcher.

    By default, this class uses `RetargetCache` as the internal cache. This
    can be modified by overriding the `.cache_type` class attribute.

    c                 8    |                                  | _        d S )N)
cache_typecacher   s    r   r   zBasicRetarget.__init__V   s    __&&


r   returnc                     dS )zhReturns the output target name.

        See numba/tests/test_retargeting.py for example usage.
        Nr%   r   s    r   output_targetzBasicRetarget.output_targetY   r-   r   c                     dS )zkReturns the retargeted dispatcher.

        See numba/tests/test_retargeting.py for example usage.
        Nr%   r)   s     r   compile_retargetzBasicRetarget.compile_retargeta   r-   r   c                 p    |j         }| j        }|#||k    rd| d| d}t          j        |          dS dS )zt
        This implementation checks that
        `self.output_target == orig_disp._required_target_backend`
        Nz2The output target does match the required target: z != .)_required_target_backendr7   r   CompilerError)r   r   required_targetr7   ms        r   r*   zBasicRetarget.check_compatiblei   sj    
 $<*&//>%> >+:> > >*1---	 '&//r   c                     | j         }|j        }|                    d          | j        k    r|S |                    |          }|,|                     |          }|                    ||           n|}|S )zgApply retargeting to orig_disp.

        The retargeted dispatchers are cached for future use.
        target_backend)r4   targetoptionsr   r7   r   r9   r   )r   r   r4   optscachedr   s         r   r,   zBasicRetarget.retargetv   s    
 
&88$%%);;;!!),,>''	22CY,,,,C
r   N)r   r    r!   r"   r   r3   r   r.   abstractpropertystrr7   r/   r9   r*   r,   r%   r   r   r1   r1   H   s        
 
 J' ' ' 	s     	  . . .    r   r1   )	r"   r.   r#   
numba.corer   r   ABCr'   r1   r%   r   r   <module>rI      s     


       #@ #@ #@ #@ #@ #@ #@ #@L    37   *? ? ? ? ?L ? ? ? ? ?r   