a
    –§»aè>  ã                   @   sr  d dl Z d dlmZmZ d dlZd dlZd dlmZ d dlZ	d dl
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„ Ze d	¡G d
d„ dejƒƒZe
je je
je je
je jiZe
je j e
j!e j"e
j#e j$e
j%e j&e
j'e j(e
j)e j*e
j+e j,e
j-e j.e
j/e j0e
j1e j2e
j3e j4e
j5e j6iZ7e
j8e j9e
j:e j;e
j<e j=e
j>e j?e
j@e jAe
jBe jCiZDdd„ ZEejFdd„ ƒZGdd„ ZHdS )é    N)ÚdefaultdictÚOrderedDict)ÚSimpleNamespace)ÚtypesÚutilsÚirÚrewritesÚcompiler)Únpydecl)ÚDUFuncc                 C   s   t | tjtfƒS ©N)Ú
isinstanceÚnpÚufuncr   )Úfunc© r   úT/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/np/ufunc/array_exprs.pyÚ	_is_ufunc   s    r   zafter-inferencec                       sh   e Zd ZdZ‡ f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‡  ZS )ÚRewriteArrayExprszóThe RewriteArrayExprs class is responsible for finding array
    expressions in Numba intermediate representation code, and
    rewriting those expressions to a single operation that will expand
    into something similar to a ufunc call.
    c                    s:   t t| ƒj|g|¢R i |¤Ž |jj}d|vr6t|d< d S )NÚ	arrayexpr)Úsuperr   Ú__init__Z	targetctxÚspecial_opsÚ_lower_array_expr)ÚselfÚstateÚargsÚkwsr   ©Ú	__class__r   r   r      s    zRewriteArrayExprs.__init__c           	      C   s¢   t |ƒdkrdS || _|| _tƒ | _i | _| tj¡}|D ]X}|j	j
}|j}t|tjƒr|t| |d¡tjƒr||  |||¡ q:t|tjƒr:|| j|< q:t | jƒdkS )z±
        Using typing and a basic block, search the basic block for array
        expressions.
        Return True when one or more matches were found, False otherwise.
        r   FN)ÚlenÚ
crnt_blockÚtypemapr   Úarray_assignsÚconst_assignsZ
find_instsr   ÚAssignÚtargetÚnameÚvaluer   ÚExprÚgetr   ÚArrayÚ_match_array_exprÚConst)	r   Zfunc_irÚblockr"   Z	calltypesZassignmentsÚinstrÚtarget_nameÚexprr   r   r   Úmatch!   s"    ÿzRewriteArrayExprs.matchc                    sš   |j }ˆ j}|dv rD|jtjv rDt‡ fdd„| ¡ D ƒƒr–|||< nR|dkr–|jjˆ j	v r–ˆ j	|jj }t
|tjƒr–|j}t|ƒr–ˆ  ||¡s–|||< dS )z‘
        Find whether the given assignment (*instr*) of an expression (*expr*)
        to variable *target_name* is an array expression.
        ©ÚunaryÚbinopc                 3   s   | ]}ˆ j |j jV  qd S r   )r"   r'   Zis_internal©Ú.0Úvar©r   r   r   Ú	<genexpr>Q   s   ÿz6RewriteArrayExprs._match_array_expr.<locals>.<genexpr>ÚcallN)Úopr#   Úfnr
   Úsupported_array_operatorsÚallÚ	list_varsr   r'   r"   r   r   ZFunctionÚ
typing_keyr   Ú_has_explicit_output)r   r/   r1   r0   Zexpr_opr#   Ú	func_typeZfunc_keyr   r9   r   r,   B   s     
ÿÿ
z#RewriteArrayExprs._match_array_exprc                 C   s,   t |jƒt |jƒ }|jdur"dS ||jkS )zr
        Return whether the *expr* call to *func* (a ufunc) features an
        explicit output argument.
        NT)r    r   r   ÚvarargÚnin)r   r1   r   Únargsr   r   r   rB   `   s    
z&RewriteArrayExprs._has_explicit_outputc                 C   s>   |j }|dv r|jS |dkr,| j|jj jS td |¡ƒ‚d S )Nr3   r;   z:Don't know how to find the operator for '{0}' expressions.)r<   r=   r"   r   r'   rA   ÚNotImplementedErrorÚformat©r   Úir_exprÚir_opr   r   r   Ú_get_array_operatorl   s    ÿÿz%RewriteArrayExprs._get_array_operatorc                 C   sJ   |j }|dkr|j|jfS |dkr*| ¡ S |dkr8|jS td |¡ƒ‚dS )z{Given a Numba IR expression, return the operands to the expression
        in order they appear in the expression.
        r5   r4   r;   z:Don't know how to find the operands for '{0}' expressions.N)r<   ÚlhsÚrhsr@   r   rG   rH   rI   r   r   r   Ú_get_operandsv   s    ÿÿzRewriteArrayExprs._get_operandsc                    s:   |j }|dkr|jS ‡ fdd„ˆ  |¡D ƒ}ˆ  |¡|fS )zZTranslate the given expression from Numba IR to an array expression
        tree.
        r   c                    s   g | ]}ˆ j  |j|¡‘qS r   )r$   r*   r'   )r7   Zop_varr9   r   r   Ú
<listcomp>Œ   s   ÿz5RewriteArrayExprs._translate_expr.<locals>.<listcomp>)r<   r1   rO   rL   )r   rJ   rK   Zoperands_or_argsr   r9   r   Ú_translate_expr…   s    
ÿz!RewriteArrayExprs._translate_exprc                 C   sV  i }t ƒ }ttƒ}| j ¡ D �],}|j}g }|  |¡|f}tjd|j	|| j
|jj d�}t ||j|j	¡}	|	||< |	| j|jj< |  |¡D ]¾}
|
j}|
j�r|| jv �r| j| }|j}| ¡ }|D ]}
||
j  d7  < qÄ| |  |¡¡ |jj�rH| |jj¡ d||< qŠ|| jv �r,| | j| ¡ qŠ||
j  d7  < | |
¡ qŠq|||fS )ztIterate over the matches, trying to find which instructions should
        be rewritten, deleted, or moved.
        r   )r<   Úlocr1   Útyé   N)Úsetr   Úintr#   Úvaluesr(   rL   r   r)   rR   r"   r&   r'   r%   rO   Zis_tempr@   ÚappendrQ   Úaddr$   )r   Úreplace_mapÚ	dead_varsÚ	used_varsr/   r1   Zarr_inpsZarr_exprZnew_exprZ	new_instrÚoperandZoperand_nameZchild_assignZ
child_exprZchild_operandsr   r   r   Ú_handle_matches�   s@    ý


z!RewriteArrayExprs._handle_matchesc                 C   s   || }||v r|| }q|S )z¯Find the final replacement instruction for a given initial
        instruction by chasing instructions in a map from instructions
        to replacement instructions.
        r   )r   Zreplacement_mapr/   Úreplacementr   r   r   Ú_get_final_replacementµ   s    
z(RewriteArrayExprs._get_final_replacementc                 C   s6  |   ¡ \}}}| j ¡ }| ¡  i }| jjD ]ä}t|tjƒr¶||v rª|  ||¡}|r´| 	|¡ |j
 ¡ D ]>}|j}	|	|v rŠ| 	| |	¡¡ ||	 dkrh||	  d8  < qhn
| 	|¡ q,t|tjƒ�r|j
}
||
 dkrð||
  d8  < |||
< n|
|v�r| 	|¡ q,| 	|¡ q,|�r2| ¡ D ]}| |¡ �q |S )z}When we've found array expressions in a basic block, rewrite that
        block, returning a new, transformed block.
        r   rT   )r^   r!   ÚcopyÚclearÚbodyr   r   r%   r`   rX   r(   r@   r'   ÚpopÚDelrW   Zinsert_before_terminator)r   rZ   r[   r\   ÚresultZ
delete_mapr/   r_   r8   Úvar_nameZinstr_valuer   r   r   Úapply¿   s>    
ÿ


zRewriteArrayExprs.apply)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r2   r,   rB   rL   rO   rQ   r^   r`   rh   Ú__classcell__r   r   r   r   r      s   !
%
r   c                 C   sž  t | tƒ�r.| \}}g }i }|D ]$}t|ƒ\}}| |¡ | |¡ q |tjv rÚt|ƒdkr²|tv r„t	 
|d t| ƒ |d ¡|fS |tv rØt	 |d t| ƒ g|d g¡|fS n&|tv s¾J ‚t	 t| ƒ |d ¡|fS nRt|ƒ�rŒd tt|ƒƒ dd¡¡}t	 |t	 ¡ ¡}	|||< t	 |	|g ¡}
|
|fS n^t | tjƒ�rnt	j| jt	 ¡ | jj| jj�rb| jjndd�i fS t | tjƒ�rŒt	 | j¡i fS t d| f ƒ‚d	S )
z[Build a Python expression AST from an array expression built by
    RewriteArrayExprs.
    é   r   rT   z__ufunc_or_dufunc_{0}ú-Ú_)ÚlinenoÚ
col_offsetz1Don't know how to translate array expression '%r'N)!r   ÚtupleÚ_arr_expr_to_astrX   Úupdater
   r>   r    Ú_binopsÚastÚBinOpÚ_cmpopsÚCompareÚ	_unaryopsÚUnaryOpr   rH   ÚhexÚhashÚreplaceÚNameÚLoadÚCallr   ZVarr'   rR   ÚlineÚcolr-   ÚNumr(   rG   )r1   r<   Zarr_expr_argsÚast_argsÚenvÚargZast_argZ	child_envÚfn_nameZfn_ast_nameZast_callr   r   r   rt   
  sX    

ÿÿÿÿ
ÿ
þþÿrt   c              	   c   s°   t ƒ }| D ]Z}|j}|jj||jd�j}| dd¡ dd¡}||v rRt|›d�ƒ‚||f||< ||_q
t|ƒ}z"|V  W | ¡ D ]\}}||_q€n| ¡ D ]\}}||_qš0 dS )za
    Legalize names in the variable list for use as a Python function's
    parameter names.
    )rR   ú$rp   Ú.z not uniqueN)	r   r'   ZscopeZredefinerR   r   ÚAssertionErrorÚlistrW   )Zvar_listZvar_mapr8   Zold_nameÚnew_nameZparam_namesr   r   r   Ú_legalize_parameter_names2  s     
ÿr�   c                    s(  dt t|ƒƒ dd¡ }|jj}| ¡ }tt|ƒdd„ d�}dd„ |D ƒ}t|ƒ�€}d	d„ |D ƒ}t	 
d
 |¡|d¡}	t|	dƒrŽt|	jƒdks’J ‚|	jd }
||
j_t|jƒ\|
jd _}t	 |	¡ W d  ƒ n1 sØ0    Y  t|	|dƒ}t||ƒ || }ˆj}ˆj‰ |j‡fdd„|D ƒŽ }g }|jD ]@}t|tjƒ�rF|j}t|tjƒ�rb| |j¡ n
| |¡ �q.|j j|Ž ‰t! "¡  #¡ }|du �ršt$ %¡ n| &¡ }d|_'|j(ˆ |ˆ|dd�‰ddl)m*} G ‡ ‡‡fdd„d|j+ƒ}t,t|ƒd|d�}|j-|j. |_/‡fdd„|D ƒ}| 0|ˆ ||||¡S )z:Lower an array expression built by RewriteArrayExprs.
    z__numba_array_expr_%sro   rp   c                 S   s   | j S r   ©r'   )r8   r   r   r   Ú<lambda>T  ó    z#_lower_array_expr.<locals>.<lambda>)Úkeyc                 S   s   g | ]
}|j ‘qS r   r�   r6   r   r   r   rP   W  r’   z%_lower_array_expr.<locals>.<listcomp>c                 S   s   g | ]}t  |d ¡‘qS r   )rw   rˆ   )r7   Ú
param_namer   r   r   rP   [  s   ÿzdef {0}(): returnÚexecrc   rT   r   Nc                 3   s   | ]}ˆ   |¡V  qd S r   )Ztypeof©r7   r'   ©Úlowererr   r   r:   p  r’   z$_lower_array_expr.<locals>.<genexpr>ÚnumpyF)ÚflagsZcaching)Únpyimplc                       s   e Zd Z‡ ‡‡fdd„ZdS )z%_lower_array_expr.<locals>.ExprKernelc                    sL   t |ˆ jjˆjƒ}‡ fdd„|D ƒ}ˆ j ˆˆjˆ|¡}ˆ  |ˆjˆ jj¡S )Nc                    s    g | ]\}}}ˆ   |||¡‘qS r   )Úcast)r7   ÚvalZintyZouttyr9   r   r   rP   ‰  s   ÿzB_lower_array_expr.<locals>.ExprKernel.generate.<locals>.<listcomp>)ÚzipÚ	outer_sigr   ÚcontextZcall_internalZfndescrœ   Úreturn_type)r   r   Zarg_zipZ	cast_argsrf   ©ÚbuilderÚcresÚ	inner_sigr9   r   Úgenerate‡  s    
ÿ
ÿ
ÿz._lower_array_expr.<locals>.ExprKernel.generateN)ri   rj   rk   r¦   r   r¢   r   r   Ú
ExprKernel†  s   r§   )rE   Únoutri   c                    s   g | ]}ˆ   |¡‘qS r   )Zloadvarr–   r—   r   r   rP   ”  r’   )1r}   r~   r   rR   Úfilenamer@   ÚsortedrU   r�   rw   ÚparserH   Úhasattrr    rc   r   rt   r1   r(   Úfix_missing_locationsÚcompiler•   r    r£   rS   r   r   ÚOptionalÚtyper+   rX   Zdtyper¡   r   ZConfigStackZtop_or_noner	   ZFlagsra   Zerror_modelZcompile_subroutineZnumba.npr›   Z_Kernelr   rE   r¨   rF   Znumpy_ufunc_kernel)r˜   r1   Z	expr_nameZexpr_filenameZexpr_var_listZexpr_var_uniqueZ	expr_argsZexpr_paramsr†   Z
ast_moduleZast_fnÚ	namespaceZcode_objÚimplr    rŸ   Zinner_sig_argsZargtyrš   r›   r§   r   r   r   )r£   r¤   r¥   r˜   r   r   L  sZ    
ÿÿ
(

ÿÿr   )Irw   Úcollectionsr   r   Ú
contextlibÚsysr   r   r™   r   ÚoperatorZ
numba.corer   r   r   r	   Znumba.core.typingr
   Znumba.np.ufunc.dufuncr   r   Zregister_rewriteZRewriter   ÚposÚUAddÚnegÚUSubÚinvertÚInvertr{   rY   ÚAddÚsubÚSubÚmulÚMultÚtruedivÚDivÚmodÚModÚor_ÚBitOrÚrshiftÚRShiftÚxorÚBitXorÚlshiftÚLShiftÚand_ÚBitAndÚpowÚPowÚfloordivÚFloorDivrv   ÚeqÚEqÚneÚNotEqÚltÚLtÚleÚLtEÚgtÚGtÚgeÚGtEry   rt   Úcontextmanagerr�   r   r   r   r   r   Ú<module>   sR    Xýôú
(
