
    J/Ph@                        d Z ddlZddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZ ddlmZmZ dd	lmZ  ej        e          Zd
 Z G d d          Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z# G d d          Z$ G d de$          Z% G d d          Z& G d de$          Z' G d  d!e$          Z(d" Z)dS )#a  
Implement Dominance-Fronter-based SSA by Choi et al described in Inria SSA book

References:

- Static Single Assignment Book by Inria
  http://ssabook.gforge.inria.fr/latest/book.pdf
- Choi et al. Incremental computation of static single assignment form.
    N)reduce)copy)defaultdict)config)irir_utilserrors)
OrderedSet_lazy_pformat)compute_cfg_from_blocksc                 8    t          | j                  | _        | S )znApply SSA reconstruction algorithm on the given IR.

    Produces minimal SSA using Choi et al algorithm.
    )_run_ssablocks)func_irs    N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/core/ssa.pyreconstruct_ssar      s    
 gn--GNN    c                       e Zd Zd Zd ZdS )_CacheListVarsc                     i | _         d S N)_savedselfs    r   __init__z_CacheListVars.__init__%   s    r   c                 z    | j                             |          }||                                x| j         |<   }|S r   )r   get	list_vars)r   instgots      r   r   z_CacheListVars.get(   s:    kood##;&*nn&6&66DK
r   N)__name__
__module____qualname__r   r    r   r   r   r   $   s2              r   r   c           	         | si S t          |           }t          |          }t          | |          }t                      }|D ]l}t                              d|           t          | |          \  } }t                              dt          |                     t          | |||||          } mt          |           }||k    rt          j
        d          | S )z7Run SSA reconstruction on IR blocks of a function.
    zFix SSA violator on var %szReplaced assignments: %szCFG mutated in SSA pass)r   _iterated_domfronts_find_defs_violatorsr   _loggerdebug_fresh_varsr   _fix_ssa_varsr	   CompilerError)r   cfgdf_plus	violatorscache_list_varsvarnamedefmapcfg_posts           r   r   r   /   s      	
!&
)
)C!#&&G$VS11I$&&O  0 0('	
 	
 	

 %VW550-2G2GHHH vwW.0 0
 'v..H3"#<===Mr   c                 8   t          |           }||d<   ||d<   t          t                    x|d<   }||d<   t          ||          |d<   t	          | |t          |                    }|                                D ]\  }	}
||	         }|
|j        z   |_        |S )z=Rewrite all uses to ``varname`` given the definition map
    r1   r2   phimapr-   phi_locations)_make_statesr   list_compute_phi_locations_run_block_rewrite_FixSSAVarsitemsbody)r   r1   r2   r-   r.   r0   statesr5   	newblockslabelphilistcurblks               r   r+   r+   S   s     &!!FF9F8 +D 1 11F8vF5M4WfEEF?"66;3O3OPPI ,,.. , ,w5!+r   c                 J   d |                                                                  D             d}|rmd}                                D ]T\  }}t          t          j        fd|D             t                                }|                    |          r||z  }d}U|mS )zCompute the iterated dominance frontiers (DF+ in literatures).

    Returns a dictionary which maps block label to the set of labels of its
    iterated dominance frontiers.
    c                 4    i | ]\  }}|t          |          S r$   )set.0kvss      r   
<dictcomp>z'_iterated_domfronts.<locals>.<dictcomp>k   s$    JJJ2CGGJJJr   TFc                      g | ]
}|         S r$   r$   )rG   v	domfrontss     r   
<listcomp>z'_iterated_domfronts.<locals>.<listcomp>p   s    )C)C)C1)A,)C)C)Cr   )dominance_frontierr<   r   operatoror_rE   
difference)r-   
keep_goingrH   rI   innerrM   s        @r   r&   r&   e   s     KJ)?)?)A)A)G)G)I)IJJJIJ
 "
__&& 	" 	"EAr8<)C)C)C)C)C)C)CSUUKKE## "e!
  " r   c                 p    t                      }|                                D ]\  }}|r|| |         z  }|S r   )rE   r<   )iterated_dfr2   r6   deflabeldefstmtss        r   r9   r9   w   sG     EEM$llnn 3 3( 	3[22Mr   c                     t          |           }||d<   t          t                    x|d<   }t          | |t	                                }||fS )z(Rewrite to put fresh variable names
    r1   r2   )r7   r   r8   r:   _FreshVarHandler)r   r1   r>   r2   r?   s        r   r*   r*      sU     &!!FF9 +D 1 11F8v"663C3E3EFFIfr   c                 <    |                                  ^}}|j        S r   )valuesscope)r   first_s      r   
_get_scoper`      s    IEA;r   c                    t          t                    }t          t                    }t          ||          }t	          | |t                                 t                              dt          |                     t          d |
                                D                       }|                                }|
                                D ]T\  }}||vrK|D ]H}	||	         }
d ||         D             }|                    |
          s|                    |            nIUt                              dt          |                     |S )zm
    Returns
    -------
    res : Set[str]
        The SSA violators in a dictionary of variable names.
    )defsuseszdefs %sc                 >    g | ]\  }}t          |          d k    |S )   )lenrF   s      r   rN   z(_find_defs_violators.<locals>.<listcomp>   s'    GGG%!R3r77Q;;A;;;r   c                     h | ]\  }}|S r$   r$   )rG   _assignr@   s      r   	<setcomp>z'_find_defs_violators.<locals>.<setcomp>   s    CCCeCCCr   zSSA violators %s)r   r8   rE   dict_run_block_analysis_GatherDefsHandlerr(   r)   r   r
   r<   
dominatorsintersectionadd)r   r-   rb   rc   r>   r/   domsrH   
use_blocksr@   dom
def_labelss               r   r'   r'      sG    tDsDt$'''F(:(<(<===MM)]400111 GG4::<<GGGHHI>>D  :I#  5kCC$q'CCC
!..s33 MM!$$$E MM$mI&>&>???r   c                     |                                  D ]9\  }}t                              d|           ||d<   t          |||          D ]}:d S )Nz"==== SSA block analysis pass on %sr@   )r<   r(   r)   _run_ssa_block_pass)r   r>   handlerr@   blkr_   s         r   rk   rk      sh    llnn  
s:EBBBw$VS':: 	 	A	 r   c                 >   i }|                                  D ]\  }}t                              d|           t          j        |j        |j                  }g }||d<   ||d<   t          |||          D ]}|J |                    |           ||_	        |||<   |S )Nz!==== SSA block rewrite pass on %s)r]   locr@   block)
r<   r(   r)   r   Blockr]   ry   ru   appendr=   )	r   r>   rv   r?   r@   rw   newblknewbodystmts	            r   r:   r:      s    Illnn " "
s95AAA	sw777ww'W== 	! 	!D###NN4    !	%r   c                 <    t          t          |                     S )N)r]   )rj   r`   )r   s    r   r7   r7      s$         r   c              #   b  K   t                               d|           |j        D ]}t                               d|           t          |t          j                  r|                    | |          }n|                    | |          }||ur|t                               d|           |V  d S )Nz
Running %szon stmt: %szreplaced with: %s)r(   r)   r=   
isinstancer   Assign	on_assignon_other)r>   rw   rv   r   rets        r   ru   ru      s      MM,(((  mT***dBI&& 	1##FD11CC""6400Cd??sMM-s333				 r   c                       e Zd ZdZd Zd ZdS )_BaseHandlerzGA base handler for all the passes used here for the SSA algorithm.
    c                     dS )a  
        Called when the pass sees an ``ir.Assign``.

        Subclasses should override this for custom behavior

        Parameters
        -----------
        states : dict
        assign : numba.ir.Assign

        Returns
        -------
        stmt : numba.ir.Assign or None
            For rewrite passes, the return value is used as the replacement
            for the given statement.
        Nr$   )r   r>   assigns      r   r   z_BaseHandler.on_assign         r   c                     dS )a  
        Called when the pass sees an ``ir.Stmt`` that's not an assignment.

        Subclasses should override this for custom behavior

        Parameters
        -----------
        states : dict
        assign : numba.ir.Stmt

        Returns
        -------
        stmt : numba.ir.Stmt or None
            For rewrite passes, the return value is used as the replacement
            for the given statement.
        Nr$   r   r>   r   s      r   r   z_BaseHandler.on_other   r   r   Nr!   r"   r#   __doc__r   r   r$   r   r   r   r      s<           $    r   r   c                       e Zd ZdZd Zd ZdS )rl   aZ  Find all defs and uses of variable in each block

    ``states["label"]`` is a int; label of the current block
    ``states["defs"]`` is a Mapping[str, List[Tuple[ir.Assign, int]]]:
        - a mapping of the name of the assignee variable to the assignment
          IR node and the block label.
    ``states["uses"]`` is a Mapping[Set[int]]
    c                    |d         |j         j                                     ||d         f           |                                D ]@}|j        }||j         j        k    r'|d         |                             |d                    Ad S )Nrb   r@   rc   )targetnamer|   r   ro   )r   r>   r   varrH   s        r   r   z_GatherDefsHandler.on_assign  s    vv})*1166'?2KLLL##%% 	7 	7CAFM&&&vq!%%fWo666	7 	7r   c                     |                                 D ]0}|j        }|d         |                             |d                    1d S )Nrc   r@   )r   r   ro   )r   r>   r   r   rH   s        r   r   z_GatherDefsHandler.on_other  sP    >>## 	3 	3CA6N1!!&/2222	3 	3r   Nr   r$   r   r   rl   rl     s<         7 7 73 3 3 3 3r   rl   c                   "    e Zd Zd Zej        ZdS )UndefinedVariablec                      t          d          )NzNot intended for instantiation)NotImplementedErrorr   s    r   r   zUndefinedVariable.__init__  s    !"BCCCr   N)r!   r"   r#   r   r   	UNDEFINEDr   r$   r   r   r   r     s)        D D D \FFFr   r   c                       e Zd ZdZd Zd ZdS )rZ   z9Replaces assignment target with new fresh variables.
    c                    |j         j        |d         k    r|d         }|d         }t          |          dk    ri|j         }t                              d|           |j        |j        vr8d|j        d}t          j        t          j	        ||j
                             n&|                    |j         j        |j
                  }t          j        ||j        |j
        	          }||d
                                      |           |S )Nr1   r]   r2   r   zfirst assign: %sz	variable z is not in scope.ry   r   valuery   r@   )r   r   rf   r(   r)   	localvarswarningswarnr	   NumbaIRAssumptionWarningry   redefiner   r   r   r|   )r   r>   r   r]   r2   	newtargetwmsgs          r   r   z_FreshVarHandler.on_assign&  s   =	!2227OEH%F6{{a"M	0)<<<>88Jy~JJJDM&"A$&,j#2 #2 #2 3 3 3 "NN6=+=6:NNN	Y lJ  F
 6'?#**6222r   c                     |S r   r$   r   s      r   r   z_FreshVarHandler.on_other<  s    r   Nr   r$   r   r   rZ   rZ   #  s<           ,    r   rZ   c                   D    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
dd
ZdS )r;   aF  Replace variable uses in IR nodes to the correct reaching variable
    and introduce Phi nodes if necessary. This class contains the core of
    the SSA reconstruction algorithm.

    See Ch 5 of the Inria SSA book for reference. The method names used here
    are similar to the names used in the pseudocode in the book.
    c                     || _         d S r   )_cache_list_vars)r   r0   s     r   r   z_FixSSAVars.__init__I  s     /r   c                    |j         }t          |t          j                  r|                     ||| j                            |j                             }|}|j        t          j        urj|d         |j        j	        k    rT|d         |j        i}t          |          }t          j        ||           t          j        |j        ||j                  S nt          |t          j                  ri|                     |||g          }|O|j        t          j        ur<|d         |j        j	        k    r&t          j        |j        |j        |j                  S |S )Nr1   r   )r   r   r   Inst_fix_varr   r   r   r   r   r   r   replace_vars_innerr   ry   Var)r   r>   r   rhsnewdefreplmaps         r   r   z_FixSSAVars.on_assignL  sP   lc27## 	]] 5 9 9&, G G F !fm2<&G&G)$(:::%i0&-@Gs))C/W===9%}!"J   
 RV$$ 		]]66C599F!fm2<&G&G)$(:::9%}$m"J    r   c                     |                      ||| j                            |                    }|\|j        t          j        urI|d         |j        j        k    r3|d         |j        i}t          |          }t          j	        ||           |S )Nr1   )
r   r   r   r   r   r   r   r   r   replace_vars_stmt)r   r>   r   r   r   s        r   r   z_FixSSAVars.on_otherk  s    D$/33D99
 
 &-r|"C"Ci FM$666!),fm<Dzz*4999r   c                 b    d |D             }|d         }||v r|                      ||          S dS )z0Fix all variable uses in ``used_vars``.
        c                     g | ]	}|j         
S r$   )r   )rG   rH   s     r   rN   z(_FixSSAVars._fix_var.<locals>.<listcomp>y  s    ...qAF...r   r1   N)	_find_def)r   r>   r   	used_varsvarnamesphivars         r   r   z_FixSSAVars._fix_varv  sH     /.I...	"X>>&$/// r   c                    t                               d|d         |           d}|d         }|d         |         }|d         |         }|d         }|                     ||          }t          |          D ]2}	|                     |	||          }
|
|k     r|	} n|	|v r
|d	         } n3||                     |||j        
          }|S )z?Find definition of ``stmt`` for the statement ``stmt``
        zfind_def var=%r stmt=%sr1   Nr@   r2   r5   rz   )stopr   )r(   r)   _stmt_indexreversed_find_def_from_topry   )r   r>   r   selected_defr@   
local_defs
local_phisrz   cur_posdefstmtdef_poss              r   r   z_FixSSAVars._find_def~  s    	/	1BDIIIwH%e,
H%e,
w""4//
++ 		 		G&&wG&DDG  &J&&)"~ ' 2248 3  L r   c                    t                               d|           |d         }|d         }|d         }|d         }||v rM|d         }|d         j        }|                    |d         |	          }	t	          j        |	t          j                            |	          |
          }
t                               d|
|           ||                             d|
           ||         	                    |
           |
                    |          D ]{\  }}|                     |||	          }t                               d|           |
j        j        	                    |j                   |
j        j        	                    |           ||
S |                                |         }||k    rt#          |d         |           t$          S t                               d||           |                     |||	          S )zFind definition reaching block of ``label``.

        This method would look at all dominance frontiers.
        Insert phi node if necessary.
        zfind_def_from_top label %rr-   r2   r5   r6   r]   rz   r1   r   r   zinsert phi node %s at %sr   zincoming_def %szidom %s from label %s)r(   r)   ry   r   r   r   Exprphiinsertr|   predecessors_find_def_from_bottomr   incoming_valuesr   incoming_blocksimmediate_dominators"_warn_about_uninitialized_variabler   )r   r>   r@   ry   r-   r2   r5   r6   r]   freshvarphinodepredr_   incoming_defidoms                  r   r   z_FixSSAVars._find_def_from_top  s    	2E:::Um!!/M!!7OE/%C~~fY&7S~AAHigkkck**  G
 MM4guEEE5M  G,,,5M  )))++E22 ; ;a#99Dc  :     />>>-44\5HIII-44T::::N++--e4Du}} 36)3DcJJJ((MM14???--fd-DDDr   c                     t                               d|           |d         }||         }|r
|d         }|S |                     |||          S )z<Find definition from within the block at ``label``.
        zfind_def_from_bottom label %rr2   r   r   )r(   r)   r   )r   r>   r@   ry   r2   rb   lastdefs          r   r   z!_FixSSAVars._find_def_from_bottom  s^     	5u===!e} 	C2hGN**65c*BBBr   r   c                     t          t          |j                            d|         D ]}|j        |         |u r|c S t          |j                  S )zFind the positional index of the statement at ``block``.

        Assumptions:
        - no two statements can point to the same object.
        N)rangerf   r=   )r   r   rz   r   is        r   r   z_FixSSAVars._stmt_index  sY     s5:''. 	 	Az!}'' (5:r   N)r   )r!   r"   r#   r   r   r   r   r   r   r   r   r   r$   r   r   r;   r;   @  s         0 0 0  >	 	 	0 0 0  8,E ,E ,E\
C 
C 
C     r   r;   c                 x    t           j        r-t          j        t	          j        d|  |                     d S d S )Nz Detected uninitialized variable r   )r   ALWAYS_WARN_UNINIT_VARr   r   r	   NumbaWarning)r1   ry   s     r   r   r     sX    $ 
<7<<  	
 	
 	
 	
 	

 
r   )*r   loggingrP   r   	functoolsr   r   collectionsr   numbar   
numba.corer   r   r	   numba.core.utilsr
   r   numba.core.analysisr   	getLoggerr!   r(   r   r   r   r+   r&   r9   r*   r`   r'   rk   r:   r7   ru   r   rl   r   rZ   r;   r   r$   r   r   <module>r      s                   # # # # # #       + + + + + + + + + + 6 6 6 6 6 6 6 6 7 7 7 7 7 7 '
H
%
%         ! ! !H  $  $      
  <    "  
 
 
% % % % % % % %P3 3 3 3 3 3 3 32           |   :` ` ` ` `, ` ` `F
 
 
 
 
r   