o
    8ήc1                     @   s  d dl Z d dlmZ d dl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mZmZ d d
lmZmZ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#m$Z$ d dl%m&Z& edddddZ'G dd de'eZ(G dd de	Z)e) Z*G dd dej+Z,e,eed < dd Z-dd  Z.d!d" Z/ej0ej1ej2ej3d#Z4d$d% Z5G d&d' d'e6Z7G d(d) d)e7Z8G d*d+ d+e7Z9dS ),    N)contextmanager)configtargetconfig)jit)TargetDescriptor)	is_jitted)TargetOptionsinclude_default_options)
cpu_target)dispatcher_registrytarget_registry)utilstypes	serializecompilersigutils)as_dtype)	_internal)parse_signature)build_ufunc_wrapperbuild_gufunc_wrapper)FunctionCache	NullCache)global_compiler_locknopythonforceobjboundscheckfastmathtarget_backendc                   @   s   e Zd Zdd ZdS )UFuncTargetOptionsc                 C   sf   | dsd|_| dsd|_|jddd | ds tj|_| ds)|j|_d|_|d d S )	Nenable_pyobjectTenable_loopliftnrt)default	debuginfor   r   )	is_setr    r!   inherit_if_not_setr   DEBUGINFO_DEFAULTr$   r   enable_pyobject_looplift)selfflagsoptions r,   B/tmp/pip-target-vg8gfxp4/lib/python/numba/np/ufunc/ufuncbuilder.pyfinalize!   s   



zUFuncTargetOptions.finalizeN)__name__
__module____qualname__r.   r,   r,   r,   r-   r      s    r   c                       s8   e Zd ZeZ fddZedd Zedd Z  Z	S )UFuncTargetc                    s   t  d d S )Nufunc)super__init__r)   	__class__r,   r-   r5   8      zUFuncTarget.__init__c                 C      t jS N)r
   typing_contextr6   r,   r,   r-   r<   ;      zUFuncTarget.typing_contextc                 C   r:   r;   )r
   target_contextr6   r,   r,   r-   r>   ?   r=   zUFuncTarget.target_context)
r/   r0   r1   r   r+   r5   propertyr<   r>   __classcell__r,   r,   r7   r-   r2   5   s    
r2   c                   @   sR   e Zd ZdZeZi i fddZdd Zedd Z	dd	 Z
i fd
dZdd ZdS )UFuncDispatcherzK
    An object handling compilation of various signatures for a ufunc.
    c                 C   s(   || _ t | _|| _|| _t | _d S r;   )py_funcr   
UniqueDict	overloadstargetoptionslocalsr   cache)r)   rB   rF   rE   r,   r,   r-   r5   M   s
   
zUFuncDispatcher.__init__c                 C   s   t | j| j| jdS )4
        NOTE: part of ReduceMixin protocol
        )pyfuncrF   rE   )dictrB   rF   rE   r6   r,   r,   r-   _reduce_statesT   s
   zUFuncDispatcher._reduce_statesc                 C   s   | |||dS )rH   )rB   rF   rE   r,   )clsrI   rF   rE   r,   r,   r-   _rebuild^   s   zUFuncDispatcher._rebuildc                 C   s   t | j| _d S r;   )r   rB   rG   r6   r,   r,   r-   enable_cachinge   r9   zUFuncDispatcher.enable_cachingc                 K   s`   | j  }|| | j }|| t }| jj|| d|_	d|_
d|_| |||S )NTnumpyF)rF   copyupdaterE   r   Flagstargetdescrr+   parse_as_flagsno_cpython_wrappererror_modelr!   _compile_core)r)   sigrF   rE   locstoptr*   r,   r,   r-   compileh   s   



zUFuncDispatcher.compilec           	         sN  j j}j j}t fdd}t t | h | U j	
||  durD W  d   W  d   W  d   S t|\}}tj||j||||d j	|   W  d   W  d   W  d   S 1 syw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z
        Trigger the compiler on the core function or load a previously
        compiled version from the cache.  Returns the CompileResult.
        c                  3   sH    zd V  W n t y    w j j} | d u r" j j< d S d S r;   )	ExceptionrD   get	signature)existscresr)   r,   r-   store_overloads_on_success   s   
zAUFuncDispatcher._compile_core.<locals>.store_overloads_on_successN)argsreturn_typer*   rF   )rS   r<   r>   r   r   r   ConfigStackenterrP   rG   load_overloadr   normalize_signaturer   compile_extrarB   save_overload)	r)   rX   r*   rF   	typingctx	targetctxrb   rc   rd   r,   r`   r-   rW   {   s:   	"zUFuncDispatcher._compile_coreN)r/   r0   r1   __doc__ufunc_targetrS   r5   rK   classmethodrM   rN   r[   rW   r,   r,   r,   r-   rA   G   s    

rA   npyufuncc                 C   s*   | j |fi |}t|\}}|||fS r;   )r[   r   rh   )nb_funcrE   rX   ra   rc   rd   r,   r,   r-   _compile_element_wise_function   s   
rr   c                 C   s4   |du r| j rtd| jj}|tjksJ || S )zGiven a compilation result, argument types, and a return type,
    build a valid Numba signature after validating that it doesn't
    violate the constraints for the compilation mode.
    Nz-return type must be specified for object mode)
objectmode	TypeErrorr^   rd   r   pyobject)ra   rc   rd   r,   r,   r-   _finalize_ufunc_signature   s   rv   c                 C   s   | j }| j}| jj}t t||||| j| }|j|j}W d   n1 s(w   Y  dd |j	D }|
t|jj ||| jfS )zBuild a wrapper for the ufunc loop entry point given by the
    compilation result object, using the element-wise signature.
    Nc                 S   s   g | ]}t |jqS r,   )r   num).0ar,   r,   r-   
<listcomp>   s    z5_build_element_wise_ufunc_wrapper.<locals>.<listcomp>)r>   libraryfndescllvm_func_namer   r   rs   get_pointer_to_functionnamerc   appendr   rd   rw   environment)ra   r^   ctxr{   fnameinfoptr	dtypenumsr,   r,   r-   !_build_element_wise_ufunc_wrapper   s   
r   )r      Nreorderablec                 C   s,   zt |  } W | S  ty   td| f w )z]
    Parse an identity value and return the corresponding low-level value
    for Numpy.
    zInvalid identity value %r)_identitiesKeyError
ValueError)identityr,   r,   r-   parse_identity   s   
r   c                   @   s   e Zd ZdddZdd ZdS )_BaseUFuncBuilderNc                 C   sV   t | dr	| j}n| jj}t| j||\}}}| |||}| j| || j|< |S )NrE   )hasattrrE   rq   rr   _finalize_signature_sigsr   _cres)r)   rX   rE   ra   rc   rd   r,   r,   r-   add   s   


z_BaseUFuncBuilder.addc                 C   s   dS )zI
        Disable the compilation of new signatures at call time.
        Nr,   r6   r,   r,   r-   disable_compile   s    z!_BaseUFuncBuilder.disable_compiler;   )r/   r0   r1   r   r   r,   r,   r,   r-   r      s    
r   c                   @   s4   e Zd Zddi fddZdd Zdd Zd	d
 ZdS )UFuncBuilderNFc                 C   sH   t |r|j}|| _t|| _tdd|d||| _g | _i | _d S )Nrp   _targetrG   r,   )r   rB   r   r   r   rq   r   r   )r)   rB   r   rG   rE   r,   r,   r-   r5     s   

zUFuncBuilder.__init__c                 C   s   t |||S )z^Slated for deprecation, use ufuncbuilder._finalize_ufunc_signature()
        instead.
        )rv   r)   ra   rc   rd   r,   r,   r-   r     s   z UFuncBuilder._finalize_signaturec                 C   s   t r g }g }| jstdg }d }| jD ]$}| j| }| ||\}}}|| |t| ||j|f qd gt	| }	|d u rQt
| j}
t	|
j}nt	|jj}d}t| jj| jj|||||	|| j	}|W  d    S 1 sww   Y  d S )NNo definitionr   )r   rq   rt   r   r   buildr   intr{   leninspectgetfullargspecrB   rc   r^   r   fromfuncr/   rm   r   )r)   	dtypelistptrlist	keepalivera   rX   r   r   envdatlistargspecinctoutctr3   r,   r,   r-   build_ufunc  s4   



$zUFuncBuilder.build_ufuncc                 C   s
   t ||S )z^Slated for deprecation, use
        ufuncbuilder._build_element_wise_ufunc_wrapper().
        )r   )r)   ra   r^   r,   r,   r-   r   9  s   
zUFuncBuilder.build)r/   r0   r1   r5   r   r   r   r,   r,   r,   r-   r      s
    'r   c                   @   s8   e Zd Zddi fddZdd Zedd Zd	d
 ZdS )GUFuncBuilderNFc                 C   sT   || _ t|| _td|d|| _|| _t|\| _| _|| _	|| _
g | _i | _d S )Nrp   r   )rB   r   r   r   rq   r^   r   sinsoutrE   rG   r   r   )r)   rB   r^   r   rG   rE   r,   r,   r-   r5   C  s   

zGUFuncBuilder.__init__c                 C   s2   |j s|jjtjkrtd|d u rtj}|| S )Nz(gufunc kernel must have void return type)rs   r^   rd   r   voidrt   r   r,   r,   r-   r   O  s
   z!GUFuncBuilder._finalize_signaturec                 C   s   g }g }| j stdg }| jD ]#}| j| }| |\}}}|| |t| ||j|f qd gt| }	t| j	}
t| j
}t| jj| jj|||
||	|| j| j
}|S )Nr   )rq   rt   r   r   r   r   r   r{   r   r   r   r   r   rB   r/   rm   r   r^   )r)   	type_list	func_listr   rX   ra   r   r   r   datalistninnoutr3   r,   r,   r-   r   X  s(   






zGUFuncBuilder.build_ufuncc           	      C   sx   |j }t| j|| j| j| jdd}|j}|j|j	}g }|j
D ]}t|tjr,|j}n|}|t|j q |||fS )zJ
        Returns (dtype numbers, function ptr, EnvironmentObject)
        F)rG   
is_parfors)r^   r   rB   r   r   rG   r   r{   r~   r   rc   
isinstancer   Arraydtyper   r   rw   )	r)   ra   r^   r   r   r   r   ry   tyr,   r,   r-   r   u  s   

zGUFuncBuilder.build)r/   r0   r1   r5   r   r   r   r   r,   r,   r,   r-   r   @  s    
	
r   ):r   
contextlibr   
numba.corer   r   numba.core.decoratorsr   numba.core.descriptorsr   numba.core.extendingr   numba.core.optionsr   r	   numba.core.registryr
   numba.core.target_extensionr   r   r   r   r   r   r   numba.np.numpy_supportr   numba.np.ufuncr   numba.np.ufunc.sigparser   numba.np.ufunc.wrappersr   r   numba.core.cachingr   r   numba.core.compiler_lockr   _options_mixinr   r2   rn   ReduceMixinrA   rr   rv   r   PyUFunc_ZeroPyUFunc_OnePyUFunc_NonePyUFunc_ReorderableNoner   r   objectr   r   r   r,   r,   r,   r-   <module>   sP   	^A