
    J/PhF                     T    d Z ddlmZ ddlmZ  G d de          Zd ZdZd Zd	 Z	d
S )zW
Implement a rewrite pass on a LLVM module to remove unnecessary 
refcount operations.
    )CallVisitor)typesc                       e Zd ZdZd Zd ZdS )_MarkNrtCallVisitorz7
    A pass to mark all NRT_incref and NRT_decref.
    c                 ,    t                      | _        d S N)setmarked)selfs    Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/core/removerefctpass.py__init__z_MarkNrtCallVisitor.__init__   s    ee    c                 x    t          |j        dd          t          v r| j                            |           d S d S )Nname )getattrcallee_accepted_nrtfnsr
   add)r   instrs     r   
visit_Callz_MarkNrtCallVisitor.visit_Call   s?    5<,,0@@@KOOE""""" A@r   N)__name__
__module____qualname____doc__r   r    r   r   r   r      s<           # # # # #r   r   c                     t                      }|                    |            | j        D ]<}t          |j                  D ]%}||j        v r|j                            |           &=d S r   )r   visit_Functionbasic_blockslistinstructionsr
   remove)functionmarkpassbbinsts       r   _rewrite_functionr'      s    "$$HH%%%# - -)) 	- 	-Dx&&&&t,,,	-- -r   )
NRT_incref
NRT_decrefc                    fdfd}	 |                      d          }t          |j                  dk    rdS n# t          $ r Y nw xY w|j        }|j        }|j        }|D ]} ||          s dS  |          sdS |                                D ]}	|	 |	j                  s dS | j	        D ]-}
|
j
                            d          r|
j
        t          vr dS .d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 )zG
        Valid output are any type that does not need refcount
        )contains_nrt_meminfo)tymodeldmms     r   valid_outputz_legalize.<locals>.valid_output,   s#     B--////r   c                 N     |           pt          | t          j                  S )zT
        Valid input are any type that does not need refcount except Array.
        )
isinstancer   Array)r-   r0   s    r   valid_inputz_legalize.<locals>.valid_input3   s&     |B>:b%+#>#>>r   numba_args_may_always_need_nrtr   FNNRT_T)get_named_metadatalenoperandsKeyErrorargtypesrestype	calltypesvaluesreturn_type	functionsr   
startswithr   )moduler/   fndescr4   nmdr;   r<   r=   argtycalltyfnr0   s    `         @r   	_legalizerH   %   sv   0 0 0 0 0? ? ? ? ?	''(HII s|q  5 !     HnG I   {5!! 	55	 <   u ""$$  ll63E&F&F55   7f%% 	w...uu4s   > 
A
Ac                 b    |j         }t          | j        ||          rt          |            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.
    TF)data_model_managerrH   rB   r'   )r#   contextrC   r/   s       r   remove_unnecessary_nrt_usagerL   c   s:      
$C#v.. (###tur   N)
r   llvmlite.ir.transformsr   
numba.corer   r   r'   r   rH   rL   r   r   r   <module>rO      s    
 / . . . . .      	# 	# 	# 	# 	#+ 	# 	# 	#- - - . ; ; ;|    r   