o
    Y۷i):                     @   sf  d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZ ddlmZ G dd deZG dd	 d	eZed
ZejZejZejZejZejZejZejZdd Zdd Zdd Zdd Zdd Zdd ZG dd deZ G dd deZ!d2ddZ"dd Z#dd  Z$d!d" Z%e&d#d$Z'ej(d%d& Z)d'd( Z*d)d* Z+d+d, Z,ej(d3d.d/Z-d0d1 Z.dS )4z<
Utilities to simplify the boilerplate for native lowering.
    N)Enum)typingtypesutilscgutils)BaseRegistryLoaderc                   @   sb   e Zd ZdZd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d ZdS )Registryz?
    A registry of function and attribute implementations.
    unspecifiedc                 C   s(   || _ g | _g | _g | _g | _g | _d S N)name	functionsgetattrssetattrscasts	constants)selfr    r   I/home/ubuntu/vllm_env/lib/python3.10/site-packages/numba/core/imputils.py__init__   s   
zRegistry.__init__c                        fdd}|S )a1  
        Decorate an implementation of *func* for the given argument types.
        *func* may be an actual global function object, or any
        pseudo-function supported by Numba, such as "getitem".

        The decorated implementation has the signature
        (context, builder, sig, args).
        c                    s   j |  f | S r
   )r   appendimplargtysfuncr   r   r   decorate%      z Registry.lower.<locals>.decorater   )r   r   r   r   r   r   r   lower   s   	zRegistry.lowerc                 C   s"   ||||}| |||jf |S r
   )r   	signature)r   r   tyattr	impl_list	decorator	real_implr   r   r   _decorate_attr*   s   zRegistry._decorate_attrc                    r   )z
        Decorate an implementation of __getattr__ for type *ty* and
        the attribute *attr*.

        The decorated implementation will have the signature
        (context, builder, typ, val).
        c                        |  jtS r
   )r%   r   _decorate_getattrr   r!   r   r    r   r   r   7      z(Registry.lower_getattr.<locals>.decorater   r   r    r!   r   r   r(   r   lower_getattr/      zRegistry.lower_getattrc                 C      |  |dS )a:  
        Decorate the fallback implementation of __getattr__ for type *ty*.

        The decorated implementation will have the signature
        (context, builder, typ, val, attr).  The implementation is
        called for attributes which haven't been explicitly registered
        with lower_getattr().
        N)r+   r   r    r   r   r   lower_getattr_generic<      	zRegistry.lower_getattr_genericc                    r   )z
        Decorate an implementation of __setattr__ for type *ty* and
        the attribute *attr*.

        The decorated implementation will have the signature
        (context, builder, sig, args).
        c                    r&   r
   )r%   r   _decorate_setattrr   r(   r   r   r   O   r)   z(Registry.lower_setattr.<locals>.decorater   r*   r   r(   r   lower_setattrG   r,   zRegistry.lower_setattrc                 C   r-   )a;  
        Decorate the fallback implementation of __setattr__ for type *ty*.

        The decorated implementation will have the signature
        (context, builder, sig, args, attr).  The implementation is
        called for attributes which haven't been explicitly registered
        with lower_setattr().
        N)r2   r.   r   r   r   lower_setattr_genericT   r0   zRegistry.lower_setattr_genericc                    r   )z
        Decorate the implementation of implicit conversion between
        *fromty* and *toty*.

        The decorated implementation will have the signature
        (context, builder, fromty, toty, val).
        c                    s   j |  ff | S r
   )r   r   r   fromtyr   totyr   r   r   g   s   z%Registry.lower_cast.<locals>.decorater   )r   r5   r6   r   r   r4   r   
lower_cast_   r,   zRegistry.lower_castc                    s    fdd}|S )z
        Decorate the implementation for creating a constant of type *ty*.

        The decorated implementation will have the signature
        (context, builder, ty, pyval).
        c                    s    j | ff | S r
   )r   r   r   r.   r   r   r   s   r   z)Registry.lower_constant.<locals>.decorater   )r   r    r   r   r.   r   lower_constantl   s   zRegistry.lower_constantc                 C   s   d| j  dS )NzLowering Registry<>)r   r   r   r   r   __repr__x      zRegistry.__repr__N)r	   )__name__
__module____qualname____doc__r   r   r%   r+   r/   r2   r3   r7   r8   r;   r   r   r   r   r      s    
r   c                   @   s   e Zd ZdZdZdS )RegistryLoaderz6
    An incremental loader for a target registry.
    )r   r   r   r   r   N)r=   r>   r?   r@   registry_itemsr   r   r   r   rA   |   s    rA   builtin_registryc                    s8   |  |d ur fdd}n fdd}|f|_ ||_|S )Nc                        | |||S r
   r   contextbuildertypvaluer!   r$   r   r   res   r<   z_decorate_getattr.<locals>.resc                        | ||||S r
   r   rE   rJ   r   r   rK         )r   r!   r   r    r!   rK   r   rJ   r   r'      s   r'   c                    s<   |  |d ur fdd}n fdd}|t jf|_||_|S )Nc                    rD   r
   r   rF   rG   sigargsr!   rJ   r   r   rK      r<   z_decorate_setattr.<locals>.resc                    rL   r
   r   rO   rJ   r   r   rK      rM   )r   Anyr   r!   rN   r   rJ   r   r1      s   r1   c           	      C   s   t |jtjrB|jj}| ||}t||}||	|j
 | |||}||| W d    n1 s8w   Y  ||}|S r
   )
isinstancereturn_typer   Optionaltypemake_optional_noner   alloca_once_valueif_thennot_is_nonemake_optional_valuestoreload)	rF   rG   rP   statusretval
value_typeoptional_none	retvalptroptional_valuer   r   r   fix_returning_optional   s   
re   c                    s"    fdd} j |_t||_|S )zC
    A wrapper inserting code calling Numba-compiled *fndesc*.
    c                    s   |  |j }| j|| j j|\}}t||j | j	|| W d    n1 s.w   Y  |j
 jks;J t| ||||}|j| |j
krXd}t||j|j
t| | j|S )Nz$function returned {0} but expect {1})declare_functionmodule	call_convcall_functionrestypeargtypesr   if_unlikelyis_errorreturn_status_propagaterT   re   rV   get_value_type	TypeErrorformatimpl_ret_new_ref)rF   rG   rP   rQ   r   r_   r`   msgfndescr   r   imp   s   zuser_function.<locals>.imp)rk   r   tuplelibs)ru   rx   rv   r   rt   r   user_function   s   
ry   c                    s    fdd}t ||_|S )zD
    A wrapper inserting code calling Numba-compiled *gendesc*.
    c                    s2   |  |j }| j|| j j|\}}||fS r
   )rf   rg   rh   ri   rj   rk   )rF   rG   rP   rQ   r   r_   r`   gendescr   r   rv      s
   zuser_generator.<locals>.imp)rw   rx   )r{   rx   rv   r   rz   r   user_generator   s   
r|   c                    s    fdd}|S )zl
    Decorator a given class as implementing *iterator_type*
    (by providing an `iternext()` method).
    c                    s0    j ttj fdd}td|  S )Nc                    s    |\} | ||}|| ||S r
   r   )rF   rG   rP   rQ   resultrI   iterobjclsiternextr   r   iternext_wrapper   s   z8iterator_impl.<locals>.wrapper.<locals>.iternext_wrapperr   )r   iternext_implRefTypeBORROWEDlower_builtin)r   r   iterator_typer   r   wrapper   s
   ziterator_impl.<locals>.wrapperr   )iterable_typer   r   r   r   r   iterator_impl   s   r   c                   @   sF   e Zd ZdZdZdd Zdd Zddd	Zd
d Zdd Z	dd Z
dS )_IternextResultzb
    A result wrapper for iteration, passed by iternext_impl() into the
    wrapped function.
    _context_builder_pairobjc                 C   s   || _ || _|| _d S r
   r   )r   rF   rG   pairobjr   r   r   r     s   
z_IternextResult.__init__c                 C   s   | j tjd| j_dS )z1
        Mark the iterator as exhausted.
        FNr   get_constantr   booleanr   secondr:   r   r   r   set_exhausted	  s   z_IternextResult.set_exhaustedTc                 C   s$   |dv r| j tj|}|| j_dS )z
        Mark the iterator as valid according to *is_valid* (which must
        be either a Python boolean or a LLVM inst).
        )FTNr   )r   is_validr   r   r   	set_valid  s   z_IternextResult.set_validc                 C   s   || j _dS )zP
        Mark the iterator as yielding the given *value* (a LLVM inst).
        Nr   first)r   rI   r   r   r   yield_  s   z_IternextResult.yield_c                 C   s   | j | jtj| jjS )z>
        Return whether the iterator is marked valid.
        )r   get_argument_valuer   r   r   r   r   r:   r   r   r   r     s   
z_IternextResult.is_validc                 C   s   | j jS )z>
        Return the iterator's yielded value, if any.
        r   r:   r   r   r   yielded_value&     z_IternextResult.yielded_valueN)T)r=   r>   r?   r@   	__slots__r   r   r   r   r   r   r   r   r   r   r      s    
	r   c                   @   s"   e Zd ZdZ	 dZ	 dZ	 dZdS )r   z&
    Enumerate the reference type
             N)r=   r>   r?   r@   NEWr   	UNTRACKEDr   r   r   r   r   ,  s    r   c                    s*    dd t D vrtd fdd}|S )a  
    Wrap the given iternext() implementation so that it gets passed
    an _IternextResult() object easing the returning of the iternext()
    result pair.

    ref_type: a numba.targets.imputils.RefType value, the reference type used is
    that specified through the RefType enum.

    The wrapped function will be called with the following signature:
        (context, builder, sig, args, iternext_result)
    c                 S   s   g | ]}|qS r   r   ).0xr   r   r   
<listcomp>I  s    z!iternext_impl.<locals>.<listcomp>z3ref_type must be an enum member of imputils.RefTypec                    s    fdd}|S )Nc              	      st   |j }| ||} | |||t| || tjkrt}ntjkr%t}ntjkr-t	}nt
d|| ||| S )NzUnknown ref_type encountered)rT   make_helperr   r   r   rr   r   impl_ret_borrowedr   impl_ret_untracked
ValueError	_getvalue)rF   rG   rP   rQ   	pair_typer   impl_ret)r   ref_typer   r   r   M  s   




z-iternext_impl.<locals>.outer.<locals>.wrapperr   )r   r   r   )r   r   outerL  s   ziternext_impl.<locals>.outer)r   r   )r   r   r   r   r   r   =  s   r   c                 C   s&   t |j|}| d|}|||fS )z
    Call the `getiter()` implementation for the given *iterable_type*
    of value *val*, and return the corresponding LLVM inst.
    getiter)r   r   r   get_function)rF   rG   r   valgetiter_siggetiter_implr   r   r   call_getiter`  s   r   c           	      C   sR   |j }t|tj}t||}| d|}|||f}| |||}t| ||S )z
    Call the `iternext()` implementation for the given *iterator_type*
    of value *val*, and return a convenience _IternextResult() object
    reflecting the results.
    r   )	
yield_typer   Pairr   r   r   r   r   r   )	rF   rG   r   r   itemtyr   iternext_sigr   r   r   r   r   call_iternextj  s   r   c                 C   s<   z|  tttj|}W n
 ty   Y dS w |||fS )z^
    Call len() on the given value.  Return None if len() isn't defined on
    this type.
    N)r   lenr   r   r   intpNotImplementedError)rF   rG   r    r   len_implr   r   r   call_leny  s   r   _ForIterLoop)rI   do_breakc           	   	   #   s    |j }t| ||}d}d  fdd}| |= t| ||}j| dd   W d   n1 sJw   Y  t	|
 |V  | W d   n1 sfw   Y    | jr}| j|| dS dS )z
    Simulate a for loop on the given iterable.  Yields a namedtuple with
    the given members:
    - `value` is the value being yielded
    - `do_break` is a callable to early out of the loop
    zfor_iter.bodyzfor_iter.endc                      s      d S r
   )branchr   bb_endrG   r   r   r     r<   zfor_iter.<locals>.do_breakF)likelyN)r   r   append_basic_blockr   
goto_blockr   rY   rZ   r   r   r   position_at_end
enable_nrtnrtdecref)	rF   rG   r   r   r   itervalbb_bodyr   rK   r   r   r   for_iter  s&   



r   c                 C      |S )z5
    The implementation returns a new reference.
    r   ctxrG   rettyretr   r   r   rr        rr   c                 C   s   | j r| j||| |S )z
    The implementation returns a borrowed reference.
    This function automatically incref so that the implementation is
    returning a new reference.
    )r   r   increfr   r   r   r   r     s   r   c                 C   r   )z.
    The return type is not a NRT object.
    r   r   r   r   r   r     r   r   numpyc                 c   s>    ddl m} | j}||| | _z	dV  W || _dS || _w )z7
    Temporarily change the context's error model.
    r   )callconvN)
numba.corer   error_modelcreate_error_model)rF   
model_namer   old_error_modelr   r   r   force_error_model  s   r   c                  O   s   t d)zPA stub for use internally by Numba when a call is emitted
    on a TypeRef.
    z%This function should not be executed.)r   )rQ   kwargsr   r   r   numba_typeref_ctor  r   r   r
   )r   )/r@   collections
contextlibinspect	functoolsenumr   r   r   r   r   r   numba.core.typing.templatesr   objectr   rA   rC   r   r   r+   r/   r2   r3   r7   r8   r'   r1   re   ry   r|   r   r   r   r   r   r   r   
namedtupler   contextmanagerr   rr   r   r   r   r   r   r   r   r   <module>   sR    l	/
#

