o
    8ήc                     @   sT   d Z ddlZddlZddlmZ G dd dZG dd dejZG d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                   @   s>   e Zd ZdZejZdd Zdd Zdd Z	dd	 Z
d
d ZdS )RetargetCachez^Cache for retargeted dispatchers.

    The cache uses the original dispatcher as the key.
    c                 C   s   |   | _d| _d| _d S )Nr   )container_type_cache	_stat_hit
_stat_missself r
   :/tmp/pip-target-vg8gfxp4/lib/python/numba/core/retarget.py__init__   s   

zRetargetCache.__init__c                 C   s   || j |< dS )z9Save a dispatcher associated with the given key.
        N)r   )r	   	orig_dispnew_dispr
   r
   r   
save_cache      zRetargetCache.save_cachec                 C   s8   | j |}|du r|  jd7  _|S |  jd7  _|S )z9Load a dispatcher associated with the given key.
        N   )r   getr   r   )r	   r   outr
   r
   r   
load_cache   s   zRetargetCache.load_cachec                 C   s
   | j  S )z+Returns the contents of the cache.
        )r   itemsr   r
   r
   r   r   (   s   
zRetargetCache.itemsc                 C   s   | j | jdS )z0Returns stats regarding cache hit/miss.
        )hitmiss)r   r   r   r
   r
   r   stats-   r   zRetargetCache.statsN)__name__
__module____qualname____doc__weakrefWeakKeyDictionaryr   r   r   r   r   r   r
   r
   r
   r   r      s    
r   c                   @   s,   e Zd ZdZejdd Zejdd ZdS )BaseRetargetz/Abstract base class for retargeting logic.
    c                 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   r
   r
   r   check_compatible6   s   zBaseRetarget.check_compatiblec                 C   r    )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!   r
   r
   r   retarget?      zBaseRetarget.retargetN)r   r   r   r   abcabstractmethodr"   r#   r
   r
   r
   r   r   3   s    
r   c                   @   sN   e Zd ZdZeZdd Zejde	fddZ
ejdd Zd	d
 Zd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                 C   s   |   | _d S )N)
cache_typecacher   r
   r
   r   r   V   s   zBasicRetarget.__init__returnc                 C   r    )zhReturns the output target name.

        See numba/tests/test_retargeting.py for example usage.
        Nr
   r   r
   r
   r   output_targetY   r$   zBasicRetarget.output_targetc                 C   r    )zkReturns the retargeted dispatcher.

        See numba/tests/test_retargeting.py for example usage.
        Nr
   r!   r
   r
   r   compile_retargeta   r$   zBasicRetarget.compile_retargetc                 C   s@   |j }| j}|dur||krd| d| d}t|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_backendr+   r   CompilerError)r	   r   required_targetr+   mr
   r
   r   r"   i   s   
zBasicRetarget.check_compatiblec                 C   sT   | j }|j}|d| jkr|S ||}|du r&| |}||| |S |}|S )zgApply retargeting to orig_disp.

        The retargeted dispatchers are cached for future use.
        target_backendN)r)   targetoptionsr   r+   r   r,   r   )r	   r   r)   optscachedr   r
   r
   r   r#   v   s   

zBasicRetarget.retargetN)r   r   r   r   r   r(   r   r%   abstractpropertystrr+   r&   r,   r"   r#   r
   r
   r
   r   r'   H   s    
r'   )	r   r%   r   
numba.corer   r   ABCr   r'   r
   r
   r
   r   <module>   s    &