U
    9%eF                     @   sL   d Z ddlmZ ddlmZ G dd deZdd ZdZd	d
 Zdd Z	dS )zW
Implement a rewrite pass on a LLVM module to remove unnecessary 
refcount operations.
    )CallVisitor)typesc                   @   s    e Zd ZdZdd Zdd ZdS )_MarkNrtCallVisitorz7
    A pass to mark all NRT_incref and NRT_decref.
    c                 C   s   t  | _d S N)setmarked)self r	   Y/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/core/removerefctpass.py__init__   s    z_MarkNrtCallVisitor.__init__c                 C   s"   t |jddtkr| j| d S )Nname )getattrZcallee_accepted_nrtfnsr   add)r   instrr	   r	   r
   
visit_Call   s    z_MarkNrtCallVisitor.visit_CallN)__name__
__module____qualname____doc__r   r   r	   r	   r	   r
   r      s   r   c                 C   sF   t  }||  | jD ]*}t|jD ]}||jkr$|j| q$qd S r   )r   Zvisit_FunctionZbasic_blockslistZinstructionsr   remove)functionZmarkpassZbbinstr	   r	   r
   _rewrite_function   s    


r   )Z
NRT_increfZ
NRT_decrefc                    s    fddfdd}z|  d}W n tk
r:   Y nX t|jdkrNdS |j}|j}|j}|D ]}||sd dS qd|sdS | D ]}	|	dk	r|	js dS q| j	D ] }
|
j
d	r|
j
tkr dS qd
S )z
    Legalize the code in the module.
    Returns True if the module is legal for the rewrite pass that removes
    unnecessary refcounts.
    c                    s    |  }|   S )zG
        Valid output are any type that does not need refcount
        )Zcontains_nrt_meminfo)tymodel)dmmr	   r
   valid_output,   s    z_legalize.<locals>.valid_outputc                    s    | pt | tjS )zT
        Valid input are any type that does not need refcount except Array.
        )
isinstancer   ZArray)r   )r   r	   r
   valid_input3   s    z_legalize.<locals>.valid_inputZnumba_args_may_always_need_nrtr   FNZNRT_T)Zget_named_metadataKeyErrorlenZoperandsargtypesrestype	calltypesvaluesreturn_typeZ	functionsr   
startswithr   )moduler   fndescr!   Znmdr$   r%   r&   ZargtyZcalltyfnr	   )r   r   r
   	_legalize%   s0    	

r-   c                 C   s(   |j }t| j||r t|  dS dS dS )ac  
    Remove unnecessary NRT incref/decref in the given LLVM function.
    It uses highlevel type info to determine if the function does not need NRT.
    Such a function does not:

    - return array object(s);
    - take arguments that need refcounting except array;
    - call function(s) that return refcounted object.

    In effect, the function will not capture or create references that extend
    the lifetime of any refcounted objects beyond the lifetime of the function.

    The rewrite is performed in place.
    If rewrite has happened, this function returns True, otherwise, it returns False.
    TFN)Zdata_model_managerr-   r*   r   )r   contextr+   r   r	   r	   r
   remove_unnecessary_nrt_usagec   s
    r/   N)
r   Zllvmlite.ir.transformsr   Z
numba.corer   r   r   r   r-   r/   r	   r	   r	   r
   <module>   s   >