a
    
d!                     @   s   d Z zddlmZ W n ey&   Y n0 ddlmZ 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dd Zdd Zdd Zdd Zdd  ZG d!d" d"Zd#d$ Zd%d& Zed'kre  d(S ))zc
Contains tests and a prototype implementation for the fanout algorithm in
the LLVM refprune pass.
    )Digraph)defaultdictAc                  C   s   dgddgg ddgdgg ddgddgd	d
gddgg dgg ddgdgg d} t t}dg|d< dg|d< ddg|d< dddhi}|| |fS )NBCDEFGHIJKLMZ)r   OP)r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   increfdecrefr   listedgesnodesexpected r   f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/llvmlite/tests/refprune_proto.pycase1   s.    

r   c                  C   sJ   ddgdgg d} t t}dg|d< dg|d< dg|d< dd i}|| |fS Nr   r   )r   r   r   r   r   r   r   r   r   r   r   case22   s    


r    c                  C   s,   t  \} }}|d d dd i}| ||fS )Nr   r	   r   r   appendr   r   _r   r   r   r   case3@   s    r%   c                  C   s,   t  \} }}|d d dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   case4H   s    r&   c                  C   s,   t  \} }}|d d dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   case5P   s    r'   c                  C   s,   t  \} }}|d d dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   case6X   s    r(   c                  C   s,   t  \} }}|d d dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   case7`   s    r)   c                  C   sB   ddgdgg d} t t}dg|d< dg|d< ddhi}|| |fS r   r   r   r   r   r   case8h   s    


r*   c                  C   s,   t  \} }}|d d dd i}| ||fS )Nr   r   r   r*   r"   r#   r   r   r   case9u   s    r,   c                  C   s.   t  \} }}|d d ddhi}| ||fS )Nr   r   r+   r#   r   r   r   case10}   s    
r-   c                  C   s6   t  \} }}|d d g |d< ddhi}| ||fS Nr   r   r   r+   r#   r   r   r   case11   s
    
r/   c                  C   s8   t  \} }}|d d dg|d< ddhi}| ||fS r.   r+   r#   r   r   r   case12   s
    

r0   c                  C   s6   t  \} }}|d d dg|d< dd i}| ||fS )Nr   r   r   r   r+   r#   r   r   r   case13   s
    
r1   c                 C   s6   t t}|  D ] \}}|D ]}|| | qq|S N)r   setitemsadd)r   dsrcZ	outgoingsdstr   r   r   make_predecessor_map   s
    r9   c                   @   sh   e Zd Zd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dZdd ZdS )FanoutAlgorithmFc                 C   s*   || _ || _t|| _|rtn| j| _d S r2   )r   r   r9   	rev_edgesprint_null_print)selfr   r   verboser   r   r   __init__   s    
zFanoutAlgorithm.__init__c                 C   s   |   S r2   )find_fanout_in_function)r>   r   r   r   run   s    zFanoutAlgorithm.runc                 O   s   d S r2   r   )r>   argskwargsr   r   r   r=      s    zFanoutAlgorithm._null_printc                 C   sP   i }| j D ]@}dd | j| D D ]&}| |}| d|d| |||< q"q
|S )Nc                 s   s   | ]}|d kr|V  qdS )r   Nr   ).0xr   r   r   	<genexpr>       z:FanoutAlgorithm.find_fanout_in_function.<locals>.<genexpr>z>>z===)r   r   find_fanoutr<   )r>   gotcur_noder   decref_blocksr   r   r   rA      s    

z'FanoutAlgorithm.find_fanout_in_functionc                 C   s:   |  |}| d| |sd S | j||tds2d S t|S )N
candidates)entry)find_decref_candidatesr<   verify_non_overlappingENTRYr3   )r>   	head_noderL   r   r   r   rI      s    
zFanoutAlgorithm.find_fanoutc           	      C   s   |  ddd t|}|r| }t }|g}~|r| }|  d|d| ||v rZq4||krr|  d| dS || |  d| d	| |  | |D ].}||v r|  d
  dS ||kr|| qq4qdS )NrP   P   -rK   |z%!! failed because we arrived at entryFz   z preds z.!! reject because predecessor in decref_blocksT)r<   centerr   popr3   r5   get_predecessorsr"   )	r>   rR   rL   rN   todorK   visitedZ	workstackpredr   r   r   rP      s<    
z&FanoutAlgorithm.verify_non_overlappingc                 C   s   t | j| S r2   )tupler   r>   noder   r   r   get_successors   s    zFanoutAlgorithm.get_successorsc                 C   s   t | j| S r2   )r\   r;   r]   r   r   r   rX      s    z FanoutAlgorithm.get_predecessorsc                 C   s   d| j | v S )Nr   )r   r]   r   r   r   
has_decref   s    zFanoutAlgorithm.has_decref
   c                 C   s   dt | }| |d|| |dkr(dS ||v rD||d kr@dS dS | |rh|| | |d dS |d8 }||f7 }d}| |D ] }| |||sd} qqd}q| |d|  |S )	N walkr   FTzfound decref   zret )lenr<   r`   r5   r_   walk_child_for_decref)r>   rK   
path_stackrL   depthindentfoundchildr   r   r   rf      s0    


z%FanoutAlgorithm.walk_child_for_decrefc                 C   s`   |  ddd |f}d}t }| |D ] }| |||sHd} qNq,d}q,|sXt S |S d S )NrO   rS   rT   FT)r<   rV   r3   r_   rf   )r>   rK   rg   rj   rL   rk   r   r   r   rO     s    z&FanoutAlgorithm.find_decref_candidatesN)F)ra   )__name__
__module____qualname__r@   rB   r=   rA   rI   rP   r_   rX   r`   rf   rO   r   r   r   r   r:      s   
	( 
r:   c            	   	   C   s   t  \} }}t }|D ](}|j|d| dd| |  d q| D ]\}}|D ]}||| qTqH|  t| |dd}| }||ksJ d S )NZrect
z\l)shapelabelT)r?   )	r1   r   r^   joinr4   Zedgeviewr:   rB   )	r   r   r   r
   r^   childrenrk   algorJ   r   r   r   
check_once*  s    &rv   c                  C   sj   t t  D ]N\} }| drt| dd | \}}}t||}| }||ksJ qtd d S )NZcaserS   rT   z
ALL PASSED)r   globalsr4   
startswithr<   rV   r:   rB   )kfnr   r   r   ru   rJ   r   r   r   	check_all<  s    

r{   __main__N)__doc__Zgraphvizr   ImportErrorcollectionsr   rQ   r   r    r%   r&   r'   r(   r)   r*   r,   r-   r/   r0   r1   r9   r:   rv   r{   rl   r   r   r   r   <module>   s4   
 