
    |h                    P   S SK rS SKrS SKrS SKrS SKJ r JrJrJ	r	J
r
JrJrJr  S SKJr  S SKJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!J"r"  S SKJ#r#J$r$J%r%  S SK&J'r'  S SK(J)r)  S SK*J+r+  S S	K,J-r-  S S
K.J/r/  S SK0J1r1  S SKJ2r2  S SK3J4r5  S SK6r7S SK8r8S SK9r Sr:S r;S r< " S S\=5      r>S r? " S S\=5      r@    S*S jrAS rBS rCS rDS rES rFS rGS rHS rIS rJS  rK\)S! 5       rL  S+S" jrMS# rNS$ rOS% rP\
R                  " S&5       " S' S(\
R                  5      5       rSS) rTg),    N)typestypingerrorsirrewritesconfigir_utilscgutils)internal_prange)
next_labeladd_offset_to_labelsreplace_varsremove_delsrename_labelsfind_topo_ordermerge_adjacent_blocksGuardExceptionrequireguardget_definitionfind_callnamefind_build_sequenceget_np_ufunc_typget_ir_of_codesimplify_CFGcanonicalize_array_mathdead_code_elimination)compute_cfg_from_blockscompute_use_defscompute_live_variables)impl_ret_untracked)	intrinsic)	signatureListIterInstance)range_impl_map)
make_array)postproc)empty_inferredTc                 $   U R                   R                  5        Hr  nUR                  [        R                  5       HL  n[        UR                  [        R                  5      (       d  M.  Sn[        R                  " X2R                  S9e   Mt     g)z8Checks the IR of a callee is supported for inlining
    z-The use of yield in a closure is unsupported.locN)blocksvalues
find_instsr   Assign
isinstancevalueYieldr   UnsupportedErrorr,   )func_irblkstmtmsgs       W/home/james-whalen/.local/lib/python3.13/site-packages/numba/core/inline_closurecall.pycallee_ir_validatorr:   4   sa     ~~$$&NN299-D$**bhh//E--cxx@@ . '    c                     U  SU 3nUR                  SS5      R                  SS5      nUR                  SS5      R                  SS5      nU$ )a  Creates a name for an inlined variable based on the function name and the
variable name. It does this "safely" to avoid the use of characters that are
illegal in python variable names as there are occasions when function
generation needs valid python name tokens..<_>$_v)replace)function_namevar_nameinlined_namenew_names       r9   _created_inlined_var_namerH   >   sY    
 $_AhZ0L##C-55c3?H S)11#t<HOr;   c                   F    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rSrg)InlineClosureCallPassL   zInlineClosureCallPass class looks for direct calls to locally defined
closures, and inlines the body of the closure function to the call site.
Nc                 L    Uc  0 nXl         X l        X0l        / U l        X@l        g N)r5   parallel_optionsswapped_processed_stencilstyped)selfr5   rN   rO   rQ   s        r9   __init__InlineClosureCallPass.__init__Q   s)    ?G 0#% 
r;   c                    [         R                  " U R                  5      nUR                  S5        Sn[	        U R                  R
                  R                  5       5      n[        S5      nU" SU R                  R                  R                   35        U(       Ga>  UR                  5       u  pV[        UR                  5       GH  u  px[        U[        R                  5      (       d  M'  UR                   n	[        U	[        R"                  5      (       d  MT  U	R$                  S:X  d  Mf  ['        [(        U R                  U	5      n
['        [*        U R                  U	R,                  5      n['        U R.                  X6XyU
5      (       a  Sn  OF['        U R0                  X6X{5      (       a  Sn  O&['        U R2                  XU5      (       d  GM  SnGM     U(       a  GM>  [4        (       Ga6  U(       a  [7        U R                  R
                  5        [9        U R                  R
                  5      nU" S5        [;        U5        UR=                  5       nUR?                  5        Vs/ s H  o[A        X   R                  5      4PM     nn/ n[C        US SS9 Hj  u  nnURE                  U5        ['        [F        U R                  UUX   U RH                  U RJ                  RL                  U RN                  5      (       d  Mh  SnMl     U(       a  [Q        U R                  5        U(       a  [9        U R                  R
                  5      nURS                  5        H  nU R                  R
                  U	 M     [U        U R                  5        [W        U R                  R
                  5      U R                  l        [Y        U R                  R
                  5        U" S	5        g
s  snf )z&Run inline closure call pass.
        TFrJ   zSTART callzstart inline arraycallc                     U S   $ N    )tups    r9   <lambda>+InlineClosureCallPass.run.<locals>.<lambda>   s    CFr;   )keyreverseENDN)-r(   PostProcessorr5   runlistr-   items_make_debug_printfunc_idfunc_qualnamepop	enumeratebodyr1   r   r0   r2   Expropr   r   r   func_inline_reduction_inline_closure_inline_stencilenable_inline_arraycallr   r   _debug_dumploopskeyslensortedappend_inline_arraycallrO   rN   comprehensionrQ   _fix_nested_array
dead_nodesr   r   r   )rR   ppmodified	work_listdebug_print_labelblockiinstrexpr	call_namefunc_defcfgrs   ksized_loopsvisitedsdeads                      r9   rb   InlineClosureCallPass.runZ   s    ##DLL1
t,,2245	'(?@fT\\11??@AB%MMOMF%ejj1eRYY// ;;D!$00TWW5F$)-t$L	#()-$4 !!7!7!*1IG G'+H! !5!5!*1@ @'+H! !5!5!&8= ='+H) 2 i0 #"%dll&9&9:)$,,*=*=>C01IIKE<AJJLILqs58==12LKIG{0B'+-1q!*DLL#w4<<..<<djjJ J  $H- !$,,/ *$,,*=*=>C(LL''- ) "$,,/"/0C0C"DDLL 	DLL''(E; Js   !"Oc           
         [        U R                  R                  (       + 5        [        US:H  =(       d    US:H  5        [        UR                  5      S;  a  [        S5      e[        U R                  UR                  S   5        S	S jn[        U R                  U R                  R                  R                  R                  X#Xa[        S9  g)
N)reducebuiltins)r   
_functools)      z^invalid reduce call, two arguments are required (optional initial value can also be specified)r   c                 `    [        U5      nUb  UnO[        U5      nU H  nU " XE5      nM     U$ rM   )iternext)fAvitr   as         r9   reduce_func<InlineClosureCallPass._inline_reduction.<locals>.reduce_func   s6    aB}HaG Hr;   r~   callee_validatorTrM   )r   rN   	reductionru   args	TypeErrorcheck_reduce_funcr5   inline_closure_callrf   rm   __globals__r:   )rR   r~   r   r   r   r   r   s          r9   rn   'InlineClosureCallPass._inline_reduction   s     	D))3334	33 655	7tyy>' ; < < 	$,,		!5	 	LL$,,..33??k0	

 r;   c                 N   SSK Jn  UR                  nUR                  n[	        U[
        R                  5      (       a  UR                  S:X  ar  [	        UR                  U5      (       aW  UR                  (       a*  U=R                  UR                  R                  -  sl        gUR                  R                  Ul        g[        US:H  =(       d    US:H  5        [        X`R                  ;  5        U R                  R                  U5        [        UR                  5      S:X  d  [        S5      e[        [         U R"                  UR                  S   5      n[        [	        U[
        R$                  5      =(       a    UR&                  S	:H  5        [)        U R"                  R*                  R,                  R.                  UR0                  5      n[3        UR                  5      n	S
U	;   a(  [        U R4                  U	5      n
U
(       d  [        S5      eSU	;   a(  [        U R6                  U	5      n
U
(       d  [        S5      eU" USU	5      nUR                  Ul        [
        R                  " SXR8                  5      nU/U R"                  R:                  UR                  '   Xl        g)Nr   )StencilFuncstencilT)r   znumba.stencils.stencil)r   numbarY   z5As a minimum Stencil requires a kernel as an argumentmake_functionneighborhoodzXstencil neighborhood option should be a tuple with constant structure such as ((-w, w),)index_offsetszYstencil index_offsets option should be a tuple with constant structure such as (offset, )constant)numba.stencils.stencilr   targetr2   r1   r   Globalnamekwsr   rP   rw   ru   r   
ValueErrorr   r   r5   rk   rl   r   rf   rm   r   codedict_fix_stencil_neighborhood_fix_stencil_index_offsetsr,   _definitions)rR   r   r   r   r   lhsr   stencil_def	kernel_iroptionsfixedsf	sf_globals                r9   rp   %InlineClosureCallPass._inline_stencil   s   6ll{{ x++*8>>;77xxHNN...  $>>--	BB 211	34445  ''-499~" 8 9 9NDLL$))A,G
;0 2/1	3"4<<#7#7#<#<#H#H#.#3#35	txx.W$$88'BE B  g%$997CE B  J8IIiXX6	/8k!!#((+r;   c                 D   [        U R                  US   5      n[        [        US5      5        / nUR                   HR  n[        U R                  U5      n[        [        US5      5        UR                  [        UR                  5      5        MT     [        U5      US'   g)zz
Extract the two-level tuple representing the stencil neighborhood
from the program IR to provide a tuple to StencilFunc.
r   rd   T)r   r5   r   hasattrrd   rw   tuple)rR   r   dims_build_tupleres
window_varwin_build_tuples         r9   r   /InlineClosureCallPass._fix_stencil_neighborhood   s     *$,,8OP('23*00J,T\\:FOGOW56JJu_2234 1 #(*r;   c                     [        U R                  US   5      n[        [        US5      5        [	        UR
                  5      US'   g)zi
Extract the tuple representing the stencil index offsets
from the program IR to provide to StencilFunc.
r   rd   T)r   r5   r   r   r   rd   )rR   r   offset_tuples      r9   r   0InlineClosureCallPass._fix_stencil_index_offsets  s@    
 &dllGO4LMg./#(););#< r;   c           
          [        [        U[        R                  5      =(       a    UR                  S:H  5        [        U R                  U R                  R                  R                  R                  X#XA[        S9  g)Nr   r   T)r   r1   r   rk   rl   r   r5   rf   rm   r   r:   )rR   r~   r   r   r   s        r9   ro   %InlineClosureCallPass._inline_closure  s]    
8RWW- /.	0DLL LL0055AA!h-@	B r;   )rP   r5   rN   rO   rQ   NF)__name__
__module____qualname____firstlineno____doc__rS   rb   rn   rp   r   r   ro   __static_attributes__rZ   r;   r9   rJ   rJ   L   s-    IV:0d r;   rJ   c                 @   [        [        X5      nUc  [        S5      e[        U[        R
                  [        R                  45      (       a_  [        UR                  [        R                  R                  R                  5      (       d  [        S5      eUR                  R                  nO-[        US5      (       d  [        US5      (       d  [        S5      e[        US5      (       a  UR                  OUR                  nUR                   S:X  d  [#        S5      eU$ )zoChecks the function at func_var in func_ir to make sure it's amenable
for inlining. Returns the function itselfzMReduce function cannot be found for njit                             analysiszInvalid reduction functionr   __code__r   z*Reduction function should take 2 arguments)r   r   r   r1   r   FreeVarr   r2   r   coreregistryCPUDispatcherpy_funcr   r   r   co_argcountr   )r5   func_varr   f_codes       r9   r   r     s     :K & ' 	'+

BII677+++**--;;= =9::!''//k6**j11566f-- ''  "DEEr;   c                   X    \ rS rSrSrSSSSS\SS4S jr S
S jrS
S jrSS jr	S r
S	rg)InlineWorkeri0  a(  A worker class for inlining, this is a more advanced version of
`inline_closure_call` in that it permits inlining from function type, Numba
IR and code object. It also, runs the entire untyped compiler pipeline on
the inlinee to ensure that it is transformed as though it were compiled
directly.
Nc	                 l   S n	SSK Jn
  X#XE4nU Vs/ s H  oSLPM     nn[        U5      (       a5  [        U5      (       d%  U	" US5        U	" US5        U	" US5        U	" US5        O[        U5      (       a	  U	" US	5        U
R                  U l        Xl        X l        X0l        X@l	        XPl
        X`l        [        S
5      U l        Xx4nU Vs/ s H  oSL PM     nn[        U5      (       a   [        U5      (       d  Sn[        UU-  5      e[        U5      (       + U l        Xpl        Xl        gs  snf s  snf )a  
Instantiate a new InlineWorker, all arguments are optional though some
must be supplied together for certain use cases. The methods will refuse
to run if the object isn't configured in the manner needed. Args are the
same as those in a numba.core.Compiler.state, except the validator which
is a function taking Numba IR and validating it for use when inlining
(this is optional and really to just provide better error messages about
things which the inliner cannot handle like yield in closure).
c                 >    U c  [        SR                  U5      5      eg )Nz{} must not be None)r   format)argr   s     r9   check$InlineWorker.__init__.<locals>.checkJ  s#    { 5 < <T BCC r;   r   )DefaultPassBuilderN	targetctxlocalspipelineflags	typingctxr   zKtypemap and calltypes must both be either None or have a value, got: %s, %s)numba.core.compilerr   anyalldefine_untyped_pipeline_compiler_pipeliner   r   r   r   r   	validatorre   r   r   !_permit_update_type_and_call_mapstypemap	calltypes)rR   r   r   r   r   r   r   r   r   r   r   compiler_argsxcompiler_grouppairpair_is_noner8   s                    r9   rS   InlineWorker.__init__8  s*   $	D 	; #H<1>?A4-?~s>':':)[)&(#(J'%!  )[)"4"L"L"" 
",^< #+/04aT	40|S%6%6(CC$J''585F1F."; @, 1s   D,D1c                 V	   S nU" U5      nU R                   b  U R                  U5        U" U5      nUR                  n	UR                  U   n
U
R                  nUR                  n[        [        R                  R                  5       [        UR                  R                  5       5      5      n[        XS-   5      n[        U5      nXl        [        UR                  5       5      n[        UR                  5       5      n[        R                  R                  U5        U R                  S5        [        U5        [!        U5      nU R                  SU5        [#        U5      S:X  d   eUS   n0 n[%        UR&                  R(                  R+                  5       5       H  nUR,                  U;  d  M  [/        UR0                  R2                  UR,                  5      nU	R5                  UUR6                  S9nUR5                  UUR6                  S9  UUUR,                  '   M     U R                  SU5        [9        UU5        U R                  S	5        [        U5        UR0                  R:                  n[=        UUUR                  U   R6                  U5      nU R>                  (       a+  Uc  [A        S
5      eU RC                  XF5        UR                  nU R                  S5        [        U5        [E        UU5        / n[F        RH                  " XR6                  5      nUR                  US-   S Ul        [K        5       nUUR                  U'   URM                  UU45        UR                  SU Ul        UR                  RM                  [F        RN                  " XR6                  5      5        [Q        U5      n[S        XRT                  U5        U
RT                  R,                  URV                  ;   aX  XRV                  U
RT                  R,                     ;   a2  URV                  U
RT                  R,                     RY                  U5        U H:  nUU   nXl        [[        X5        X!R                  U'   URM                  UU45        M<     U R                  S5        [        U5        XUU4$ )a  Inlines the callee_ir in the caller_ir at statement index i of block
`block`, callee_freevars are the free variables for the callee_ir. If
the callee_ir is derived from a function `func` then this is
`func.__code__.co_freevars`. If `arg_typs` is given and the InlineWorker
instance was initialized with a typemap and calltypes then they will be
appropriately updated based on the arg_typs.
c                     U R                  5       n0 Ul        U R                  R                  5        H6  u  p#[         R                  " U R                  U   5      nXAR                  U'   M8     U$ rM   )copyr-   rd   deepcopy)the_irkernel_copyblock_labelr   	new_blocks        r9   copy_ir'InlineWorker.inline_ir.<locals>.copy_ir}  s\     ++-K!#K&,mm&9&9&;" MM&--*DE	2;"";/ '< r;   NrY   After relabelcallee_scopes = r   r+   var_dict = After local var renamez%arg_typs should have a value not NoneAfter arguments rename: After merge in).r   scoperj   r2   r-   maxr	   _the_max_labelr   rt   r   r   minupdater   rr   _get_all_scopesru   r   	localvars_conr.   r   rH   rf   unique_nameredefiner,   r   rm   _get_callee_argsr   r   update_type_and_call_maps_replace_args_withr   Blockr   rw   Jumpr   _replace_returnsr   r   remove_add_definitions)rR   	caller_irr   r   	callee_ircallee_freevarsarg_typsr  callee_ir_originalr  r   	call_exprcallee_blocks	max_label	min_labelcallee_scopescallee_scopevar_dictvarrF   new_varcallee_funcr   
new_blocksr  	new_label
topo_orderlabels                               r9   	inline_irInlineWorker.inline_irr  s   	 I&	 >>%NN9% %Y/

1KK	!(( ##((*	  %%'(
	 -]MJ$]3(**,-	**,-	&&y1)I (6+];M"a'('$Q'//44;;=>CHH/8%%11388 =  ..377.C
 %%l%@%," ? 	1]H-12I  '',,	;

18I8I )+ 11 GHH**9?%,,M34I=$/
HHUII.	AEF+	L	&/	#9i01ZZ^


"'')YY78 %]3
i@ LL!7!77!7!78I8I!JJ""5<<#4#45<<YG  E!%(EKY.&+U#uen-   	)*I!(JFFr;   c           	      t    U R                  U5      nUR                  R                  nU R                  XX6UUS9$ )zInlines the function in the caller_ir at statement index i of block
`block`. If `arg_typs` is given and the InlineWorker instance was
initialized with a typemap and calltypes then they will be appropriately
updated based on the arg_typs.
)r"  )run_untyped_passesr   co_freevarsr2  )rR   r  r   r   functionr"  r   freevarss           r9   inline_functionInlineWorker.inline_function  sD     ++H5	$$00~~ih'/  1 	1r;   c                    SSK JnJn  SSKJn  SSKJn  SSKJn  U" 5       nSUl	        U R                  Ul
        U R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        X(R                  l        UR                   R#                  U5      Ul        SUl        SUl        SUl        U" S5      Ul        SUl        U" 5       Ul        0 Ul        U" 5       R5                  U5        [7        UR8                  R$                  R:                  R<                  5      [>        R@                  4-  Ul!        U RE                  U5      n	U	RG                  5         U	RI                  U5        UR                  $ )z
Run the compiler frontend's untyped passes over the given Python
function, and return the function's canonical Numba IR.

Disable SSA transformation by default, since the call site won't be in
SSA form and self.inline_ir depends on this being the case.
r   )	StateDict_CompileStatus)ExtractByteCode)bytecode)ParforDiagnosticsNF)%r   r<  r=  numba.core.untyped_passesr>  
numba.corer?  numba.parfors.parforr@  r5   r   r   r   r   r   
enable_ssaFunctionIdentityfrom_functionrf   r   r   type_annotationstatusreturn_typeparfor_diagnosticsmetadatarun_passru   bcpysig
parametersr   pyobjectr   r   finalizerb   )
rR   rm   rD  r<  r=  r>  r?  r@  statepms
             r9   r5  InlineWorker.run_untyped_passes  s#    	B=':....{{jj!+ 11??E $%e, #4#6 ""5) ))//::;u~~>OO
$$U+

u}}r;   c                 P   SSK Jn  SSKJn  U R                  (       d  Sn[        U5      eSSKJn  [        R                  " UR                  5      Ul        [        R                  R                  R                  X5        U" U5      n[        R                  " UR                  5      Ul        UR!                  U R"                  U R$                  XS5      u  nnpU" 5       R'                  U5      n[        R                  " UR                  5      Ul        [)        XXR"                  5        U Vs/ s H  oR+                  S5      (       d  M  UPM     nnU H  nUR-                  U5        M     U R.                  R1                  U5        U R2                  R1                  U	5        gs  snf )zVUpdates the type and call maps based on calling callee_ir with
arguments from arg_typsr   )reconstruct_ssa)PreLowerStripPhisz_InlineWorker instance not configured correctly, typemap or calltypes missing in initialization.typed_passesNarg.)numba.core.ssarV  numba.core.typed_passesrW  r   r   rB  rY  r	   build_definitionsr-   r   r   r   analysisdead_branch_prunetype_inference_stager   r   _strip_phi_nodesr   
startswithrh   r   r  r   )rR   r   r"  rV  rW  r8   rY  	f_typemap_f_return_typef_calltypesr?   vname	arg_namesr   s                 r9   r  &InlineWorker.update_type_and_call_maps"  sI    	3=55:CS/!+!)!;!;I<L<L!M	

--iB#I.	!)!;!;I<L<L!M	 (<<NNDNNI
			 &'88C	!)!;!;I<L<L!M		 +^^	= )2N	u5E5Ef5MU		NAMM! I&k*	 Os   -F#
F#)r   r   r   r   r   r   r   r   r   r   r   rM   )F)r   r   r   r   r   r:   rS   r2  r9  r5  r  r   rZ   r;   r9   r   r   0  sD      .8#v  yGv	1(T+r;   r   c                   ^. UR                   nUR                  U   nUR                  n[        S5      nU" SUSU5        [	        US5      (       a  UR
                  OUR                  n[	        US5      (       a  UR                  OUR                  n[        U[        R                  5      (       a  SSKJn  UR                  USS	9nO[        UU5      nUb  U" U5        UR                   n[#        [$        R&                  R)                  5       [#        U R                   R+                  5       5      5      n[-        UUS-   5      n[/        U5      nUUl        [1        UR+                  5       5      n[#        UR+                  5       5      n[$        R&                  R3                  U5        U" S5        [5        U5        [7        U5      nU" SU5        [9        U5      S:X  d   eUS   n0 nUR:                  R<                  R?                  5        Hr  nUR@                  URB                  ;  d  M  [E        URF                  RH                  UR@                  5      nURK                  UURL                  S9nUUUR@                  '   Mt     U" SU5        [O        UU5        U" S5        [5        U5        [Q        XUR                  U   RL                  U 5      nU" S5        [5        U5        U(       Ga  U(       Ga
  U RS                  U5      nU" SU5        [        U[T        5      (       a[  [V        RX                  RZ                  m.[V        R\                  T.l/        [V        R\                  4T.l0        [U        U.4S jU 5       5      n O=[        U[b        Rd                  5      (       a  URf                  S:X  d   eURh                  n [9        URB                  5      [9        U 5      :X  d   e[k        UU 5        U" S5        [5        U5        U(       a  SSKJ6n!  [$        Rn                  " UR                   5      Ul8        [r        Rt                  Rv                  Ry                  UU5         U!R{                  XVUUS
5      u  n"n#n$n%[        UU"U$U5        U" V&s/ s H  n&U&R                  S5      (       d  M  U&PM     n'n&U' H  n(U"R                  U(5        M     UR3                  U"5        U	R3                  U$5        [        UU5        / n)[b        R                  " XRL                  5      n*UR                  US-   S
 U*l        [        5       n+U*U R                   U+'   U)R                  U+U*45        UR                  S
U Ul        UR                  R                  [b        R                  " UURL                  5      5        [        U5      n,[        UUR                  U+5        UR                  R@                  U Rp                  ;   aX  XRp                  UR                  R@                     ;   a2  U Rp                  UR                  R@                     R                  U5        U, H:  n-UU-   nXl         [        X5        X R                   U-'   U)R                  U-U45        M<     U" S5        [5        U 5        U
b  U) H  nU
R                  U5        M     UU4$ ! [|         a    U!R{                  XVUUS
5      u  n"n#n$n% GNkf = fs  sn&f )ak  Inline the body of `callee` at its callsite (`i`-th instruction of
`block`)

`func_ir` is the func_ir object of the caller function and `glbls` is its
global variable environment (func_ir.func_id.func.__globals__).
`block` is the IR block of the callsite and `i` is the index of the
callsite's node. `callee` is either the called function or a
make_function node. `typingctx`, `typemap` and `calltypes` are typing
data structures of the caller, available if we are in a typed pass.
`arg_typs` includes the types of the arguments at the callsite.
`callee_validator` is an optional callable which can be used to validate the
IR of the callee to ensure that it contains IR supported for inlining, it
takes one argument, the func_ir of the callee

Returns IR blocks of the callee and the variable renaming dictionary used
for them to facilitate further processing of new blocks.
r   zFound closure call: z with callee = r   closurer   )compilerT)inline_closuresNrY   r  r  r+   r	  r
  r  zcallee's closure = c              3   4   >#    U  H  nT" U5      v   M     g 7frM   rZ   ).0r   cellgets     r9   	<genexpr>&inline_closure_call.<locals>.<genexpr>  s     6g'!**gs   build_tuplezAfter closure renamerX  rZ  r  )Lr  rj   r2   re   r   r   r   rj  __closure__r1   pytypesFunctionTyperB  rk  run_frontendr   r-   r  r	   r  r   rt   r   r   r  r  rr   r  ru   r  r  r.   r   r6  rH   rf   r  r  r,   r   r  r   r   ctypes	pythonapi
PyCell_Get	py_objectrestypeargtypesr   rk   rl   rd   _replace_freevarsrY  r]  r   r   r   r^  r_  r`  	Exceptionr   rb  rh   r  r  r   rw   r  r   r  r   r  r  )/r5   glblsr   r   calleer   r   r"  r   r   r~   r   replace_freevarsr  r   r$  r   callee_codecallee_closurerk  r   r%  r&  r'  r(  r)  r*  r+  rF   r,  r   rj  rd   rY  rc  f_return_typere  r?   rf  rg  r   r.  r  r/  r0  r1  ro  s/                                                 @r9   r   r   D  s   * KKEJJqMEI#$9:K&/@&I!(!8!8&++fooK 33 nn9?9K9K  &'..//'))&$)G	"5+6	 ##$$M H++002C8K8K8M4NOI(	AFM /M$IM&&()IM&&()I""9- 	 $M2M"M2!#$# #LH%%**113K3334!!--sxx9Lnn\swwn?G!(HSXX 4 x()()	 Iuzz!}/@/@'JD*+	 *((8)73gu%%&&11G$..GO & 0 02G6g66Ew00

m35 4MMEK++,E
:;:-/*+I+!)!;!;I<L<L!M	

--iB	A+@@i4 AY! 	 	9 +Y	8 )2N	uU5E5Ef5MU		NAMM! y!%}d+J		*IZZA'INI )GNN9y),-BQEJ	JJbggi34 !/J]ELL)< 	W11111%,,2C2CDDU\\../66yA e$( %u5%.)   !EU#  (""a  	A+@@i4 AY!	A Os   Z2 ?[[2#[[c                   ^ U R                   S:X  aI  [        U R                  5      nU R                  (       a"  Sn[        R
                  " XPR                  5      eOnU R                   S:X  a  U R                  /nOP[        R                  " U 5      (       a  U R                  5       nO$[        SR                  U R                   5      5      e[        S5      n[        U[        R                   5      (       a  ["        R$                  R&                  R)                  U5      nS nU4S jn	S n
U R                   S:X  a  [+        U R,                  5      nO0 n["        R$                  R.                  R1                  XtXU	U
5      $ [3        US	5      (       a  UR4                  OUR6                  nU(       GaI  U" S
U5        [        U[8        5      (       ag  / nU HY  n[        U[:        R<                  5      (       a  UR?                  U5        M5  UR?                  [:        R@                  " UTS95        M[     XM-   nU$ [        U[:        R<                  5      (       d  [        U[B        5      (       av  URE                  U5      n[        U[:        RF                  5      (       d   eUR                   S:X  d   eURH                   Vs/ s H  nURE                  U5      PM     nnUU-   nU$ [K        SR                  U5      5      eU$ s  snf )zGet arguments for calling 'callee', including the default arguments.
keyword arguments are currently only handled when 'callee' is a function.
rV   z,Calling a closure with *args is unsupported.getattrzUnsupported ir.Expr.{}z$inline_closure_call default handlingc                     U$ rM   rZ   indexparamdefaults      r9   r\   "_get_callee_args.<locals>.<lambda>  s    wr;   c                 2   > [         R                  " UT5      $ rM   )r   Const)r  r  r  r,   s      r9   r\   r    s    #8Nr;   c                 6    [        SR                  X5      5      e)Nz.Stararg not supported in inliner for arg {} {})NotImplementedErrorr   r  s      r9   stararg_handler)_get_callee_args.<locals>.stararg_handler  s     %@GG"# #r;   defaultszdefaults = r2   r,   rr  z)Unsupported defaults to make_function: {})&rl   rc   r   varargr   r4   r,   r2   r	   is_operator_or_getitem	list_varsr   r   re   r1   rt  ru  r   r   utilspysignaturer   r   r   fold_argumentsr   r  __defaults__r   r   Varrw   r  strr   rk   rd   r  )r$  r  r,   r5   r   r8   r   rN  normal_handlerdefault_handlerr  r   callee_defaultsdefaults_listr   default_tuple
const_valss     `              r9   r  r    s    ||vINN#@C))#}}==  
	" 		(	(	3	3""$077	EFF#$JKK &'..//

  ,,V4>N	# <<6!y}}%CCzz  //o 	 /6fj.I.I6?? & 3 3 	7/511 "(A!!RVV,,%,,Q/ &,,RXXA3-GH ) +  _bff55!/377 ' 6 6 G"="''::;:%((M9:9#0#6#68#6a &44Q7#6  8j(
  *?FF')* * 8s   8K8c                    ^  U 4S jnU$ )Nc                     > [         R                  (       a(  [        TS-   SR                  S U  5       5      -   5        g g )Nz:  c              3   8   #    U  H  n[        U5      v   M     g 7frM   )r  )rn  r   s     r9   rp  9_make_debug_print.<locals>.debug_print.<locals>.<genexpr>+  s     )?$Q#a&&$s   )r   DEBUG_INLINE_CLOSUREprintjoin)r   prefixs    r9   r   &_make_debug_print.<locals>.debug_print)  s1    &&&4-"'')?$)?"??@ 'r;   rZ   )r  r   s   ` r9   re   re   (  s    A r;   c                 P    [         R                  (       a  U R                  5         g g rM   )r   r  dump)r5   s    r9   rr   rr   /  s    "" #r;   c                     / nU R                  5        H2  u  p#UR                  U;  d  M  UR                  UR                  5        M4     U$ )z+Get all block-local scopes from an IR.
    )rd   r  rw   )r-   
all_scopesr1  r   s       r9   r  r  4  sA     Jz)ekk* ' r;   c                 <   U R                  5        H  u  p#UR                  [        R                  5      nU H^  n[	        UR
                  [        R                  5      (       d  M.  UR
                  R                  nU[        U5      :  d   eX   Ul        M`     M     g)z8
Replace ir.Arg(...) with real arguments from call site
N)	rd   r/   r   r0   r1   r2   Argr  ru   r-   r   r1  r   assignsr7   idxs          r9   r  r  >  sq     ""299-D$**bff--jj&&c$i(!Y
	  'r;   c                    U R                  5        H  u  p#UR                  [        R                  5      nU H  n[	        UR
                  [        R                  5      (       d  M.  UR
                  R                  nU[        U5      :  d   e[	        X   [        R                  5      (       a  X   Ul        M  [        R                  " X   UR                  5      Ul        M     M     g)zB
Replace ir.FreeVar(...) with real variables from parent function
N)rd   r/   r   r0   r1   r2   r   r  ru   r  r  r,   r  s          r9   r}  r}  K  s     ""299-D$**bjj11jj&&c$i(di00!%DJ!#$)TXX!>DJ  'r;   c                    U R                  5        GH  u  p4/ n[        [        UR                  5      5       GH  nUR                  U   n[	        U[
        R                  5      (       a  US-   [        UR                  5      :X  d   e[
        R                  " UR                  XR                  5      UR                  U'   UR                  R                  [
        R                  " X'R                  5      5        U HN  nUR                  R                  UR                  R                  :X  d  M3  UR                  R                  Ul        MP     GM  [	        U[
        R                  5      (       d  GM:  [	        UR                  [
        R                  5      (       d  GMf  UR                  R                  S:X  d  GM  UR                  U5        GM     GM     g)zF
Return return statement by assigning directly to target, and a jump.
rY   castN)rd   rangeru   rj   r1   r   Returnr0   r2   r,   rw   r  r   r   rk   rl   )	r-   r   return_labelr1  r   castsr   r7   r  s	            r9   r  r  [  s"    s5::'A::a=D$		**AUZZ010 "		$**fhh G

1

!!"'',"AB!D{{''4::??:%)ZZ%5%5
 " T299--tzz27733JJMMV+T" ( 'r;   c                     U R                   nUR                  [        R                  5      nU H4  nX$R                  R
                     R                  UR                  5        M6     g)z>
Add variable definitions found in a block to parent func_ir.
N)r   r/   r   r0   r   r   rw   r2   )r5   r   definitionsr  r7   s        r9   r  r  q  sL     &&Kryy)GKK$$%,,TZZ8 r;   c                    SnSnSnSnU[        UR                  5      :  Ga  UR                  U   n[        U[        R                  5      (       a.  U(       a%  U(       a  UR
                  UR                  :X  a  SnGOTGO1[        U[        R                  5      (       a  UR                  nUR
                  n[        [        X5      S:X  aT  [        UR                  S   [        R                  5      (       a(  UR                  S   nUnUn	[        UR                  5      n
O[        U[        R                  5      (       aq  [        UR
                  [        R                  5      (       aH  U(       dA  UR
                  nUR                  n[!        X5      n[#        [        [$        X5      5        Un	0 n
OO!US-   nU[        UR                  5      :  a  GM  [#        U=(       a    U5        ['        S5      " UR                  W	   5        XIW
4$ )zLook for statement like "x = numpy.array(y)" or "x[..] = y"
immediately after the closure call that creates list y (the i-th
statement in block).  Return the statement index if found, or
raise GuardException.
NFr   T)arraynumpyrY   find_array_call)ru   rj   r1   r   Delr2   r   r0   r   r   r   r   r  r   r   SetItemr   r   _find_unsafe_empty_inferredre   )r5   r   	array_varlist_var_dead_after_array_calllist_varr   r   r   r   array_stmt_index	array_kws	array_defs               r9   _find_arraycallr  {  s    I%*"H	A
c%**o


1eRVV$$I%++*F15.ryy)),,C;;DmW37IItyy|RVV4499Q<	#$  N	

++bff--{{HI&w:IE5wJK I E? c%**o
B I889'(4D)EFy00r;   c                 .   [        S5      n[        X5      nU" SUSU5        [        [        U[        R
                  5      =(       a    UR                  S:H  5        UR                  n[        X5      nU" SUSU5        [        [        U[        R
                  5      =(       a    UR                  S:H  5        UR                  n[        X5      nU" SUS	U5        [        [        U[        R                  5      =(       aK    UR                  [        :H  =(       d1    UR                  [        R                  R                  R                  :H  5        [        UR                   5      n	S
UR                  R"                  /n
U	S:X  aA  XUR                  R$                  '   [        XR                   S   SS9nSUR                   S   U4$ U	S:X  aJ  XUR                  R$                  '   [        XR                   S   SS9n[        XR                   S   SS9nXU4$ [&        e)zrFind the iterator's actual range if it is either range(n), or
range(m, n), otherwise return raise GuardException.
find_iter_rangezrange_iter_var =  def = getiterzrange_var = z range_def = rV   zfunc_var = z func_def = )z"array comprehension"z
closure ofrY   r   Tlhs_onlyr   )re   r   r   r1   r   rk   rl   r2   rm   r   r  r   miscspecialprangeru   r   r,   r   r   )r5   range_iter_varrO   r   range_iter_def	range_var	range_defr   r   nargsswappingstopstarts                r9   _find_iter_ranger    s    $$56K#G<N#^YOJ~rww/ +*,$$Iw2I	?IFJy"''*Ey||v/EF~~Hg0HxBJx+ :^^u$ 9^^uzz11888; 	E79K9KLHz'/	##$g~~a'84H9>>!$h//	!'/	##$wq(9DIg~~a'84HX&&r;   c                   ^ [        U[        R                  5      (       a   UR                  mU4S jn[	        TU5      U4$ [        U[        R
                  5      (       a  S n[	        [        R                  U5      U4$ [        U[        R                  5      (       a  S n[	        [        R                  U5      U4$ [        U[        R                  5      (       a  S n[	        [        R                  U5      U4$ [        U[        R                  5      (       a  S n[	        [        R                  U5      U4$ Sn[        R                  " U5      e)zt
An implementation of len(iter) for internal use.
Primary use is for array comprehensions (see inline_closurecall).
c                    > Uu  n[         T   S   n[        R                  " U5      " XU5      nUR                  R                  n[        XUUR                  UR                  5      5      $ rX   )r&   r
   create_struct_proxycounttyper!   load)	contextbuildersigr   r2   	iter_typeiterobjint_typeval_types	           r9   codegen#length_of_iterator.<locals>.codegen  sf    HU&x03I11)<W=BDG}}))H%g&-ll7==&AC Cr;   c                     Uu  nU R                  [        R                  5      n[        XUR                  S   U5      n[        XXVR                  5      $ )Nr   )get_value_typer   intpr%   r   r!   size)r  r  r  r   r2   intp_tr  s          r9   r  r    sD    HU++EJJ7F&w!eLG%gMMr;   c                 .   UR                   u  nUu  nU R                  [        R                  5      nU R	                  XUS9nUR
                  n[        U5      " XUR                  S9n	[        R                  " XR                  5      n
[        XXjS   5      $ )N)r2   r   )r   r  r   r  make_helper
array_typer'   r  r
   unpack_tupleshaper!   )r  r  r  r   itertyr2   r  r  arraytyaryr  s              r9   r  r    s    IVHU++EJJ7F))')GG''GW%ggmmLC(())<E%gaIIr;   c                     UR                   u  nUR                  nU R                  [        R                  5      nU" UR
                  5      n[        XXg5      $ rM   )r   	containerr  r   r  r  r!   )r  r  r  r   r  tupletyr  count_consts           r9   r  r    sG    IV&&G++EJJ7F /K%gLLr;   c                     Uu  nU R                  [        R                  5      nSSKJn  U" XUR
                  S   U5      n[        XXWR                  5      $ )Nr   r$   )r  r   r  numba.typed.listobjectr%   r   r!   r  )r  r  r  r   r2   r  r%   r  s           r9   r  r    sG    HU++EJJ7F?&w!eLG%gMMr;   zdUnsupported iterator found in array comprehension, try preallocating the array and filling manually.)r1   r   RangeIteratorType
yield_typer#   ListIterr  ArrayIteratorUniTupleIterListTypeIteratorTyper   TypingError)r   valr  r8   r  s       @r9   length_of_iteratorr
    s    #u..//>>	C 3'00	C	(	(	N
 S)722	C,,	-	-		J S)722	C++	,	,	M S)722	C33	4	4	N S)722?  %%r;   c                    [        S5      n[        [        UR                  5      S:H  5        [	        [        UR                  5      5      n[        X R                  U   5      u  pnSnSnSU;   a  [        [        US   [        R                  5      5        [        XS   5      n[        [        U[        R                  5      =(       a    UR                  S:H  5        [        XR                  5      n[        X	5      nU" SU	SU5        [        U[        R                  5      (       a%  UR                  S:X  a  [        XR                  5      n[        [        U[        R                  5      =(       a    UR                  S	:H  5        [        [        UR                  5      S
:H  5        / nUR                    GHY  nUR#                  U5       Vs/ s H  nUR$                  U;   PM     nn['        U5      (       d  MD  U R                  U   nU" SU5        UR)                  [        R*                  5       H  nUR                  n[        U[        R                  5      (       d  M0  UR                  S:X  d  MB  [        U UR,                  5      n[        U[        R                  5      (       d  My  UR                  S:X  d  M  UR.                  S:X  d  M  [        U UR                  5      nU" SUUUL 5        UUL d  M  UR1                  UUU45        M     GM\     [        [        U5      S:H  5        US
   u  nnn[3        S UR5                  UR$                  5       5       5      nU" SUUR6                  [3        U/5      -  5        [        UUR6                  [3        U/5      -  :H  5        / n/ nU R                  UR$                     nUR)                  [        R*                  5       H  nUR                  n[        U[        R                  5      (       d  M0  UR                  S:X  a<  [        U UR                  5      nU" SU5        UR1                  UR                  5        M|  UR                  S:X  d  M  UR1                  UR8                  5        M     [        [        U5      S:H  =(       a    [        U5      S:H  5        US
   n US
   n![        [        UR6                  5      S:H  5        U R                  [	        [        UR6                  5      5         n"U"R:                  n#U"R<                  n$U"R>                  n%/ n&/ n'S n([A        [        U"R                   5      S-
  5       H  n)U"R                   U)   n[        U[        R*                  5      (       aD  UR                  WL d  U(" UR                  U'5      (       a  U'R1                  UR8                  5        Mu  U&R1                  U5        M     U" SU'5        [C        [D        U U U5      n*U$RG                  SU%5      n+U*(       a  U*S
   S
:X  a  U!n+O0U&R1                  [I        U U+[        RJ                  " SU%S9U%5      5        U$RG                  SU%5      n,U*(       av  U*u  n-n.n/U-S
:X  a  U.n0O.[        R                  RM                  [N        RP                  U.U-U%S9n0U(       a1  [        U/[        RR                  5      (       a  SU/l*        [V        U/l        OvU(       ai  U$RG                  SU%5      n1U&R1                  [I        U U1[        RR                  " S[X        U%S9U%5      5        [        R                  R[                  U1U 4SU%S9n0O[\        eU&R1                  [I        U U,U0U%5      5        U$RG                  S U%5      n2U&R1                  [I        U U2[        R                  R_                  U,/U%S!9U%5      5        U$RG                  S"U%5      n3U$RG                  S#U%5      n4U(       a  U(       a  U$RG                  S$U%5      n5U$RG                  SU%5      n6U&R1                  [I        U U5UU%5      5        U&R1                  [I        U U6[        R                  Ra                  U5UR.                  U%5      U%5      5        U&R1                  [I        U U4[        RR                  " S%[b        Rd                  U%S9U%5      5        SU64/nOEU(       a8  U&R1                  [I        U U4[        RR                  " S&[f        U%S9U%5      5        / nO[\        eU&R1                  [I        U U3[        R                  R[                  U4U24[i        U5      U%S9U%5      5        U' H   n7U&R1                  [I        U U7U3U%5      5        M"     U&R1                  U#5        U&U"l        U*(       a  U*S
   S
:w  a  UR:                  n#[        U#[        Rj                  5      (       d   eU#Rl                  n8U R                  U8   n9U9R>                  n%U9R                   Ro                  S
[I        U U+[        R                  RM                  [N        RP                  U!U*S
   U%S9U%5      5        OUR>                  n%UR:                  n#UR                   S
S n&U$RG                  S'U%5      n:U$RG                  S(U%5      n;U&R1                  [I        U U;[        RJ                  " SU%S9U%5      5        U&R1                  [I        U U:[        R                  RM                  [N        Rp                  U+U;U%S9U%5      5        U&R1                  [I        U U+U:U%5      5        U&R1                  U#5        U&Ul        [A        [        UR                   5      5       Hd  n)UR                   U)   UL d  M  U" S)5        [        Rr                  " U3U+UR                  Rt                  S
   UR>                  S*9UR                   U)'   Mf     U R                  U   R                   U
   n[        U[        R*                  5      (       a^  [        UR                  [        R                  5      (       a5  U3Ul        UR                  /U Rv                  UR8                  RT                  '   g+s  snf ),a  Look for array(list) call in the exit block of a given loop, and turn
list operations into array operations in the loop if the following
conditions are met:
  1. The exit block contains an array call on the list;
  2. The list variable is no longer live after array call;
  3. The list is created in the loop entry block;
  4. The loop is created from an range iterator whose length is known prior
     to the loop;
  5. There is only one list_append operation on the list variable in the
     loop body;
  6. The block that contains list_append dominates the loop head, which
     ensures list length is the same as loop length;
If any condition check fails, no modification will be made to the incoming
IR.
inline_arraycallrY   Ndtyper  zlist_var = r  r  
build_listr   zcheck loop body block rV   rw   zlist_def = c              3   *   #    U  H	  u  pUv   M     g 7frM   rZ   )rn  lbs      r9   rp  $_inline_arraycall.<locals>.<genexpr>Z  s     <;da;s   zpreds = iternextziter_def = 
pair_firstc                     [        U [        R                  5      (       a%  U H  nUR                  U R                  :X  d  M    g   g)NTF)r1   r   r  r   )r	  removedr   s      r9   
is_removed%_inline_arraycall.<locals>.is_removed~  s5    c266""66SXX%  r;   zremoved variables: r  r  r  )fnr   rhsr,   r   len_funcr
  r+   rZ   
size_tuplerd   r,   r  
empty_func	dtype_modemptyunsafe_empty_inferred
next_indexonezReplace append with SetItem)r   r  r2   r,   T)<re   r   ru   exitsr   r   r  r-   r1   r   r  r   rk   rl   r2   rd   rj   in_loopsheaderr   r/   r0   rm   attrrw   setpredecessorsentriesr   
terminatorr  r,   r  r   r  r  _new_definitionr  binopoperatorsubr   r   r   r
  rV   r   rr  r  npr!  r"  rc   Branchtruebrinsertaddr  r   r   )<r5   r   r   looprO   enable_prangerQ   r   
exit_blockr  array_call_indexr  	dtype_defdtype_mod_deflist_var_deflist_append_stmtsr1  r  in_visited_loopsr   r7   r   r   list_defappend_block_labelappend_blockappend_stmtpreds	iter_varsiter_first_varsloop_headeriter_defiter_variter_first_var
loop_entryr,  r  r,   stmtsr  r  r   r  	index_varsize_varr  r  range_func_defsize_vallen_func_varsize_tuple_varr  r  dtype_mod_var	dtype_varr+  block_idr6   next_index_varr$  s<                                                               r9   rx   rx   
  s1
   " $$67KC

Oq !d4::&'J,;
+-)H	
 IM)
9W-rvv67 #7g,>?	
9bgg.L9<<93LM&w@!'4LxLA,((\__-F%g/A/ABJ|RWW- ,OO|+- C""#q()  :=e9LM9LAAHH/9LM#$$u%,e4$$RYY/D::D$((TWW->)'499=x11hkkY6N$MMX5-gx~~FHx (L 8:</)00%1EF 0 . C!"a'(4Ea4H1k <c..t{{;<<E
EDLL38J7K3L$LNEdllS*<)=%>>?@ IO..-K&&ryy1zzdBGG$$ww*$)'4::>M84  ,L(&&t{{3 2 C	Na=C$8A$=>|H$Q'N
 C"#T$,,%7 89J&&JE
..CEG 3z'!+,q!tRYY''x':djj'+J+JNN4;;'LL - %w/ &7CIw,IYq\Q&"	 	_Wi%'XXBC%@#G 	H ~~fc*H&/#t^A:Hww}}$E), % .H Z		BB"3N#2N   >>*c:LLL,)+3G3E7:*< *-	. /
 ww||L8+rs|KH  	LL(HcBC^^L#6N	LL.$$H:3$?F G w,Ic2J]{C8NN7C0	_]M38 	9_YGGOOM9>>3?F 	G 	_Z7BHH#!FM 	Ny)*	 LL		13H!#	 
 I   
LL)j>*;T)_"%  '(+- .
 _Wc9cBC  
LLJOQ<1 %//Jz2995565!((H..*C''CHHOOYGGMMX\\~&/l " =	 oo ++
  2&c:nnUC(_WcXXA#.5 	6 	_WnWW]]hll	s'* # ,-02 	3 	_WiMNZ   3|(()*Q;.56#%:: 	!'',,Q/[__$FLa  + >>*%**+;<D$		""z$**bgg'F'F
26**T[[--.A Ns   qc                 P   [           [        [        U[        R                  5      =(       a    UR
                  S:H  5        UR                  n[        X5      n[        [        U[        R                  5      5        [        S5      " UR                  5        UR                  [         :H  $ )NrV   r  )r"  r   r1   r   rk   rl   rm   r   r   re   r2   )r5   r   r  
callee_defs       r9   r  r  "  sr    JtRWW%;$''V*;<YYF0JJz299-.34Z5E5EF444r;   c                   ^ ^^^	^
^ T R                   m[        T5      n[        T5      m[        TR	                  5        Vs/ s H  o"[        5       4PM     sn5      n[        UTTR                  U5      m
UU 4S jmUU U
U4S jm	UU	U 4S jn[        T R                   5       HR  nT R                   U   nUR                   H0  n[        XF5      (       d  M  UR                  R                  U5        M2     MT     gs  snf )zLook for assignment like: a[..] = b, where both a and b are numpy arrays,
and try to eliminate array b by expanding a with an extra dimension.
c                    > [        TU 5      n[        S5      " X5        [        U[        R                  5      (       a:  [        [        TU5      (       a  U$ UR                  S:X  a  T" UR                  5      $ [        e)zFind numpy array definition such as
    arr = numba.unsafe.ndarray.empty_inferred(...).
If it is arr = b[...], find array definition of b recursively.
find_array_defgetitem)
r   re   r1   r   rk   r   r  rl   r2   r   )arrarr_defrZ  r5   s     r9   rZ  )_fix_nested_array.<locals>.find_array_def6  sf    
 !#.*+C9grww''0'7CCy(%gmm44r;   c           	      T  > [        S5      nTR                  5        GH  u  p4UR                  nUR                  n[	        5       n[        [        U5      5       GH  nXh   n	[        U	[        R                  5      (       d  M)  UR                  U	R                  R                  5        U	R                  U L d  M_  / n
U GHV  nUR                  U;   d0  UR                  TU   ;   aC  UR                  TR                  U   ;  a&  U" UR                  S5        U
R                  U5        Mj  U" UR                  S5        [!        TUR                  5      n[        U[        R"                  5      (       a  UR$                  nUR'                  SU5      n[        R"                  " UR                  U5      n[)        TXU5      n/ nUR+                  USU 5        UR                  U5        UR+                  XhS 5        UUl        U
R                  U5        GMS  [,        e   U
s  s  $    GM     [,        e)zDouble check if all variables in varlist are defined before
expr is used. Try to move constant definition when the check fails.
Bails out by raising GuardException if it can't be moved.
fix_dependenciesz already definedz not yet definedr,  N)re   rd   r  rj   r)  r  ru   r1   r   r0   r5  r   r   r2   defmaprw   r   r  r,   r  r-  extendr   )r   varlistr   r1  r   r  rj   definedr   instnew_varlistr+  var_defr,   r,  	new_const
new_vardefnew_bodyr-   r5   livemapusedefss                     r9   r`  +_fix_nested_array.<locals>.fix_dependenciesD  s   
 ((:;"LLNLEKKE::DeG3t9%wdBII..KK 0 01zzT)&(#*C !$G 3!$WU^!;&)hh'..2G&G +CHH6H I + 2 23 7 +CHH6H I*8#((*K#-grxx#@#@*-''C.3nnY.LG020LI1@(/S2JJ/1H$,OOD!H$=$,OOJ$?$,OODH$=19EJ$/$6$6w$?*8$81 $+2  +*? &	 +J r;   c           	      6  > [        [        U [        R                  5      5        [        [        U R                  [        R
                  5      5        [        S5      nU" SU 5        U R                  nT	" U5      nU" SU5        [        TU R                  5      nU" SU5        [        [        U[        R                  5      5        UR                  S:X  a9  [        TUR                  5      n[        [        U[        R                  5      5        [        [        TU5      5        [        TUR                  S   5      n[        [        U[        R                  5      =(       a    UR                  S:H  5        U" SU5        UR                   Vs/ s H  n[        TUS	S
9PM     nnU" SU5        [        TUR                  S   5      n[        [        U[        R                  5      =(       a    UR                  S:H  5        U" SU5        T
" X5      nU=R                  U-  sl        SUl
        [        R                  Ul        [        TUS	S
9Ul        U R"                  Ul        UR$                  S	 UR$                  S	 UR$                  S	 UR$                  S	 g	s  snf )ar  For assignment like lhs[idx] = rhs, where both lhs and rhs are
arrays, do the following:
1. find the definition of rhs, which has to be a call to
   numba.unsafe.ndarray.empty_inferred
2. find the source array creation for lhs, insert an extra dimension of
   size of b.
3. replace the definition of
   rhs = numba.unsafe.ndarray.empty_inferred(...) with rhs = lhs[idx]
fix_array_assignzfound SetItem: zfound lhs_def: zfound rhs_def: r  r   rr  z
dim_def = Tr  zextra_dims = zsize_tuple_def = r[  rm   r   r  r   )r   r1   r   r  r2   r  re   r   r   rk   rl   r  r   rd   r/  r[  r  r  _kws)r7   r   r   lhs_defrhs_defdim_defr   
extra_dimssize_tuple_defrZ  r`  r5   s            r9   ro  +_fix_nested_array.<locals>.fix_array_assignq  s    	
4,-
4::rvv./'(:;%t,kk %%w/ $**5%w/
7BGG,-::$Wgmm<GJw01+GW=> ',,q/:
7BGG,L}1LML'*&}}/,! %Wa$?, 	 /OZ0'aA
>2773 3!!]2	4'8%nA

*
%%
&wdC

LL LL LL"LL)/s   JN)r-   r   r   r   rt   r)  r    ra  r   rj   r   r  )r5   r   r1  empty_deadmapro  r   r7   r-   rZ  r`  rk  rl  s   `      @@@@@r9   rz   rz   ,  s     ^^F
!&
)Cv&GfkkmDmU#%.mDEM$S&'..-PG+ +Z1f !0u%JJD%,,

!!$'  1c Es    C8c                 `    U/U R                   UR                  '   [        R                  " X!US9$ )N)r2   r   r,   )r   r   r   r0   )r5   r+  r2   r,   s       r9   r-  r-    s)    &+WG"995#66r;   zafter-inferencec                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )RewriteArrayOfConstsi  zThe RewriteArrayOfConsts class is responsible for finding
1D array creations from a constant list, and rewriting it into
direct initialization of array elements without creating the list.
c                 P   > UR                   U l         [        [        U ]  " U0 UD6  g rM   )r   superrz  rS   )rR   rR  r   r   	__class__s       r9   rS   RewriteArrayOfConsts.__init__  s#    "D2D@C@r;   c                     [        U5      S:X  a  gX l        [        [        X!U R                  X45      U l        U R
                  S L$ )Nr   F)ru   
crnt_blockr   _inline_const_arraycallr   rj  )rR   r5   r   r   r   s        r9   matchRewriteArrayOfConsts.match  s@    y>Q5u"nngB}}D((r;   c                 P    U R                   U R                  l        U R                  $ rM   )rj  r  rj   rR   s    r9   applyRewriteArrayOfConsts.apply  s    #}}r;   )r  rj  r   )
r   r   r   r   r   rS   r  r  r   __classcell__)r}  s   @r9   rz  rz    s    A) r;   rz  c           	      (  ^^^^^^ [        S5      mU R                  mUUUUUU4S jn " S S[        5      nU" 5       nU R                   GH  n[	        U[
        R                  5      (       Ga  [	        UR                  [
        R                  5      (       ar  UR                  R                  UR                  ;   aL  UR                  R                  UR                  R                  5        UR                  R                  U5        M  GO[	        UR                  [
        R                  5      (       a  UR                  n	U	R                  S:X  au  UR!                  5         U	R"                   V
s/ s H  oR                  PM     sn
Ul        UR                  R                  /Ul
        UR                  R                  U5        GMy  U	R                  S:X  aL  U	T;   aF  ['        XXR                  U	UR                  UR                  UR(                  5      (       a
  SUl        GM  GO[	        U[
        R,                  5      (       Gae  UR                  nXR$                  ;   a  UR(                  R                  U5        GM0  XR                  ;   Ga  UR.                  R                  U5        UR                  R1                  U5        UR                  R                  U5        UR                  / :X  a  / nUR                   H  n[	        U[
        R                  5      (       a$  UR                  R                  UR.                  ;   d9  [	        U[
        R,                  5      (       a  UR                  UR.                  ;   a  M  UR                  U5        M     Xl        / Ul        SUl        GM\  UR                  R                  U5        UR3                  U5      (       d  GM  UR!                  5         GM     UR*                  (       a  UR                  $ S$ s  sn
f )	ad  Look for array(list) call where list is a constant list created by
build_list, and turn them into direct array creation and initialization, if
the following conditions are met:
  1. The build_list call immediate precedes the array call;
  2. The list variable is no longer live after array call;
If any condition check fails, no modification will be made.
inline_const_arraycallc                 @	  > [        [        T"U5      n[        U=(       a    US   S:H  =(       a    US   S:H  5        [        UR                  S   R                  U;   5        TU   R
                  n[        [        U[        R                  5      =(       a    UR                  S:H  5        UR                  nUR                  S   nT$U R                     n	T!" SU SU5        U	R                  n
[        T"U5      u  p[        U5      nT#R                  SU5      nT#R                  SU5      n[        R                  n[        R                   " US5      nUT$UR                  '   UT$UR                  '   UR#                  [%        T"U[&        R(                  " XS	9U5      5        UR#                  [%        T"U[&        R*                  R-                  U/US
9U5      5        [        R.                  " U
5      nT#R                  SU5      n[1        [2        R4                  T 5      nT R7                  UU4SU05        UT$UR                  '   UR#                  [%        T"U[&        R8                  " S[2        R4                  US	9U5      5        T#R                  SU5      n[        R:                  R=                  [2        5      T$UR                  '   [&        R8                  " S[2        U5      nUR#                  [%        T"UUU5      5        T#R                  SU5      nUT$UR                  '   [?        U
5      nUS:X  a  Sn[&        R*                  RA                  UUU5      nUR#                  [%        T"UUU5      5        [&        R*                  RC                  UUU/0 US	9n[D        RF                  " U	UU5      TU'   UR#                  [%        T"U UU5      5        [I        U5       H  nT#R                  SU5      n[        R                  nUT$UR                  '   UR#                  [%        T"U[&        R(                  " UU5      U5      5        [&        RJ                  " U UUU   U5      n[D        RF                  " [        RL                  U	UU
5      TU'   UR#                  U5        M     URO                  U5        g)zCheck to see if the given "array_var" is created from a list
of constants, and try to inline the list definition as array
initialization.

Extra statements produced with be appended to "stmts".
rY   r  r   r  zinline array_var = z list_var = r  r  r+   r  r  r  r!  z	$np_g_varr1  z$np_typ_varboolbool_r  T)(r   r   r   r   r   rI  r1   r   ArrayCompatiblendimr,   r  r   ru   r  r  UniTuplerw   r-  r   r  rk   rr  DTyper   r1  r!  resolve_function_typer   r  Moduler  r  rV   r   r#   r  r  nonerb  )%r  r   rK  r  delscallnameret_typer,   r  	array_typr  seqr?   r  rM  rQ  size_typsize_tuple_typnptyper  fntyg_np_varg_nptyp_var	dtype_strnp_typ_getattr
empty_callr   rL  	index_typsetitemr   r  r   r5   r  r   s%                                  r9   inline_array-_inline_const_arraycall.<locals>.inline_array  s    6NXa[G3Nw8NO		!!!Y./T?..
8U%:%:; #"	$hh99Q<INN+	)9nhO$Wh73x>>&#.c:::!4!)'5##$GXHHT3S:	; 	G^GG//xjc/J!	" U# ^^L#6
'2%%dXK'69JK#'
 GZIIgrxxSA3H	I >>+s3!&!2!22!6yyr3'_WhcBC ..4 &J	I9cB_Wg~sKL WW\\*x.A23\O
 & 0 0Hf M	*_WiSIJ tAw4I

I&/GINN#LLBHHQ4DcJLjjIs1vsCG!'!1!1%**i2;U"DIgLL!  	Tr;   c                   *    \ rS rSrSrS rS rS rSrg)&_inline_const_arraycall.<locals>.Statei2  z
This class is used to hold the state in the following loop so as to make
it easy to reset the state of the variables tracking the various
statement kinds
c                 X    / U l         / U l        / U l        / U l        / U l        SU l        g r   )r  	dead_vars
list_itemsrK  r  r}   r  s    r9   rS   /_inline_const_arraycall.<locals>.State.__init__9  s2      DN  DN DODJ DI!DMr;   c                 <    / U l         / U l        / U l        / U l        g)z>
Resets the internal state of the variables used for tracking
N)r  r  r  r  r  s    r9   reset,_inline_const_arraycall.<locals>.State.resetI  s       DNDN DODIr;   c                     [        UR                  5        Vs/ s H  o"R                  U R                  ;   PM     sn5      $ s  snf )z\
Returns True if the list being analysed is used between the
build_list and the array call.
)r   r  r   )rR   re  r   s      r9   list_var_used4_inline_const_arraycall.<locals>.State.list_var_usedR  s5    
 $..:JK:JQ$..0:JKLLKs   "A)r  r  r  r  r}   rK  N)	r   r   r   r   r   rS   r  r  r   rZ   r;   r9   Stater  2  s    		" 		Mr;   r  r  rV   TN)re   r  objectrj   r1   r   r0   r2   r  r   r  rw   r   rK  rk   rl   r  rd   r  r   r  r}   r  r  r  r  )r   r5   r  r   r   r  r  rR  re  r   r   removed_varrj   r   r  s    ````        @@r9   r  r    s    $$<=KKKE^ ^@%M %MN GE

dBII&&$**bff--::??eoo5OO**4;;+;+;<KK&&t, 6 DJJ00zz77l*KKM8<

'C
1
'CE$'+{{'7'7&8EOKK&&t,WW&49+<\;;"[[%//5::G G)- bff%%**K...

!!$'/&&{3&&{3""4(??b( D %'bii88![[--@'bff55!ZZ5??:$D) !, #'K&(EO%)EN4 
 t$$KKMo r  ..5;;2d2[ (Ds   -P)NNNNNNNT)FF)Ur   rt  r   rw  numba.core.analysisr   rB  r   r   r   r   r   r	   r
   rC  r   numba.core.ir_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    numba.core.imputilsr!   numba.core.extendingr"   numba.core.typingr#   numba.cpython.listobjr%   numba.cpython.rangeobjr&   numba.np.arrayobjr'   r(   numba.np.unsafe.ndarrayr)   r"  r  r1  r/  numba.misc.specialrq   r:   rH   r  rJ   r   r   r   r  re   rr   r  r  r}  r  r  r  r  r
  rx   r  rz   r-  register_rewriteRewriterz  r  rZ   r;   r9   <module>r     s[      ! ! ! 0    *  3 * ' 2 1 (  K     AIF IX0Q+6 Q+h EI?CIM)-\#~BJ

'? #,9.1bD 8& 8&v KP!Up5|(~7
 
,-8++  .,M3r;   