
    0;ji                         d Z ddlZddlmZmZ ddlmZ ddlm	Z	  ej
        d          Z ej
        d          Z ej
        d                    g d	                    Zd
 Zd ZdS )z
NRT specific optimizations
    N)defaultdictdeque)binding)cgutilsz,\s*(?:tail)?\s*call void @NRT_incref\((.*)\)z,\s*(?:tail)?\s*call void @NRT_decref\((.*)\)|)z[0-9]+:z,[\'"]?[-a-zA-Z$._0-9][-a-zA-Z$._0-9]*[\'"]?:z^definez^;\s*<label>c                    	
 d }	fd}d 
fd	d fd
d g } ||           D ]\  }}|r ||          }||z  }d                     |          S )	Nc              3   `  K   g }t          |                                           D ]}|                    d          r|rJ |                    |           1|                    d          r"|sJ |                    |           d|fV  g }h|r|                    |           d|gfV  d S )Ndefine}TF)str
splitlines
startswithappend)modulecurlines      S/root/voice-cloning/.venv/lib/python3.11/site-packages/numba/core/runtime/nrtopt.py_extract_functionsz7_remove_redundant_nrt_refct.<locals>._extract_functions   s      KK**,, 	$ 	$Dx(( $w

4    %% 	$

s

4   Ci $

4    dVm####	$ 	$    c                 V    g } |           D ]\  }}|r|r |          }||z  }|S N )
func_linesoutis_bbbb_lines_extract_basic_blocks_process_basic_blocks       r   _process_functionz6_remove_redundant_nrt_refct.<locals>._process_function/   sU    44Z@@ 	 	OE8 : ://998OCC
r   c              3   b  K   | d                              d          sJ | d                              d          sJ d| d         gfV  g }| dd         D ]E}t                              |          }|d|fV  g }d|gfV  .|r|                    |           Fd|fV  d| d         gfV  d S )Nr   r
   r   F   T)r   	_regex_bbmatchr   )r   r   lnms       r   r   z:_remove_redundant_nrt_refct.<locals>._extract_basic_blocks7   s      !}''11111"~((-----jm_$$$$QrT" 	 	B##A}CibTk!!!! 

2Cijn%%%%%%%r   c                 4     |           }  |           } | S r   r   )r   (_move_and_group_decref_after_all_increfs_prune_redundant_refct_opss    r   r   z9_remove_redundant_nrt_refct.<locals>._process_basic_blockJ   s(    ;;HEE--h77r   c              3     K   t          |           D ]z\  }}t                              |          }|||                    d          d fV  <t                              |          }||d |                    d          fV  s|d d fV  {d S )Nr"   )	enumerate_regex_increfr$   group_regex_decref)r   numr%   r&   s       r   _examine_refct_opz6_remove_redundant_nrt_refct.<locals>._examine_refct_opO   s       ** 	! 	!GC##B''A}1771::t++++##B''A}4++++dD.    	! 	!r   c                    t          t                    }t          t                    }t                       |           D ]\  }}}|r|rJ |r8|dk    r                    |           *||                             |           F|r7|dk    r                    |           d||                             |           |                                D ]\  }}||         }t          t          |          t          |                    }	t          |	          D ]P}
                    |	                                                               |
                                           Qfdt          |           D             S )Nzi8* nullc                 "    g | ]\  }}|v	|S r   r   ).0r/   r%   	to_removes      r   
<listcomp>zS_remove_redundant_nrt_refct.<locals>._prune_redundant_refct_ops.<locals>.<listcomp>u   s1     ) ) )wsBi'' '''r   )r   r   setaddr   itemsminlenrangepoppopleftr+   )r   
incref_map
decref_mapr/   
incref_var
decref_varvardecopsincopsct_r4   r0   s              @r   r)   z?_remove_redundant_nrt_refct.<locals>._prune_redundant_refct_ops]   s    ''
 ''
EE	+<+<X+F+F 	7 	7'CZ"2z222 	7++MM#&&&&z*11#6666 7++MM#&&&&z*11#666%++-- 	0 	0KC_FS[[#f++..B2YY 0 0fjjll+++fnn..////0) ) ) ))H"5"5 ) ) ) 	)r   c                    d}t          |           D ]$\  }}t                              |          |dz   }%d}t          |           D ]$\  }}t                              |          |dz   }%t	          ||          }g }g }| d |         D ]G}t                              |          |                    |           2|                    |           H||z   | |d          z   S )Nr   r"   )r+   r,   r$   r.   maxr   )r   last_incref_posposr%   last_decref_poslast_posdecrefsheads           r   r(   zM_remove_redundant_nrt_refct.<locals>._move_and_group_decref_after_all_increfsx   s    ** 	* 	*GC""2&&2"%'  ** 	* 	*GC""2&&2"%'88 9H9% 	  	 B""2&&2r""""B g~ 333r   
)join)llvmirr   r   	processedis_funclinesr0   r   r(   r   r)   s         @@@@@r   _remove_redundant_nrt_refctrU      s    $ $ $$     & & &&     
! ! !) ) ) ) )64 4 48 I,,V44   	-%%e,,EU		99Yr   c                     	 |                      d           n# t          $ r | cY S w xY w| j        }t          t	          |                     }t          j        |          }t          j        |          |_        |S )a  
    Remove redundant reference count operations from the
    `llvmlite.binding.ModuleRef`. This parses the ll_module as a string and
    line by line to remove the unnecessary nrt refct pairs within each block.
    Decref calls are moved after the last incref call in the block to avoid
    temporarily decref'ing to zero (which can happen due to hidden decref from
    alias).

    Note: non-threadsafe due to usage of global LLVMcontext
    
NRT_incref)	get_function	NameErrornamerU   r   llparse_assemblyr   normalize_ir_text)	ll_modulerZ   newllnew_mods       r   remove_redundant_nrt_refctra      s    |,,,,   
 >D'I77E&&G,T22GLNs    '')__doc__recollectionsr   r   llvmliter   r[   
numba.corer   compiler,   r.   rP   r#   rU   ra   r   r   r   <module>rh      s     
			 * * * * * * * * " " " " " "      
JKK
JKKBJII 	 	 	 	 	 	C  C  C L    r   