
    uki'                     j   d dl  d dl mZ d dlmZ 	 ddl d dlmZmZ	m
ZmZ dd	lmZmZmZmZmZ  ej.                  ed
       G d de             Zdeeeeeef      deee   ee   f   fdZ ej.                  ed
       G d de             Z ej.                  ed
       G d de             Z ej.                  ed
       G d de              Z 	 	 	 dddddeee      fdZ! ej.                  ed
       G d de"             Z"	 	 	 	 ddee#e$e"f   fdZ%y# e$ rZ ed      edZ[ww xY w)   )*)_Dialect)constant   )get_op_result_or_valueget_op_results_or_valuesget_op_result_or_op_results_cextz+Error loading imports from extension moduleN    )ListOptionalSequenceTupleUnionT)replacec            	       |     e Zd ZdZ	 d	ddddeeeeee	   f      f fdZ
ed        Zed        Zed        Z xZS )
ForOpz(Specialization for the SCF for op class.Nlocip	iter_argsc          	         |g }t        |      }|D cg c]  }|j                   }}t        	|   |||||||        | j                  d   j
                  j                  | j                  d   j                  g|  yc c}w )af  Creates an SCF `for` operation.

        - `lower_bound` is the value to use as lower bound of the loop.
        - `upper_bound` is the value to use as upper bound of the loop.
        - `step` is the value to use as loop step.
        - `iter_args` is a list of additional loop-carried arguments or an operation
          producing them as results.
        Nr   r   )_get_op_results_or_valuestypesuper__init__regionsblocksappendoperands)
selflower_boundupper_boundstepr   r   r   argresults	__class__s
            S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jaxlib/mlir/dialects/scf.pyr   zForOp.__init__   s    $ I-i8	'0138811[+tYCB 	 	
 	&Q%%dmmA&6&;&;FgF	 2s   Bc                 :    | j                   d   j                  d   S z%Returns the body (block) of the loop.r   r   r   r!   s    r(   bodyz
ForOp.body8        ||A%%a((    c                 4    | j                   j                  d   S )z+Returns the induction variable of the loop.r   r-   	argumentsr,   s    r(   induction_variablezForOp.induction_variable=   s     yy""1%%r/   c                 4    | j                   j                  dd S )Returns the loop-carried arguments usable within the loop.

        To obtain the loop-carried operands, use `iter_args`.
        r   Nr1   r,   s    r(   inner_iter_argszForOp.inner_iter_argsB   s     yy""12&&r/   N)__name__
__module____qualname____doc__r   r   	OperationOpViewr   Valuer   propertyr-   r3   r6   __classcell__r'   s   @r(   r   r      s}    2 JNG G
 E)VXe_"DEFG8 ) ) & & ' 'r/   r   ofrsreturnc                     g }g }| D ]n  }t        |t        t        t        f      r@t	        |      }|j                  |       |j                  t        j                                ^|j                  |       p ||fS )z"`mlir::dispatchIndexOpFoldResults`)
isinstancer<   r=   r>   _get_op_result_or_valuer   
ShapedTypeget_dynamic_size)rB   dynamic_valsstatic_valsofrvals        r(   _dispatch_index_op_fold_resultsrM   K   sx     LK $cIvu56)#.C$z::<=s#$ $$r/   c                        e Zd ZdZ	 ddddddeeeeee	f      deeeeee	f      deeee	f      de
eeeee   f      f fdZed	efd
       Zed	e	fd       Zed	efd       Zed	efd       Zd	efdZ xZS )ForallOpz+Specialization for the SCF forall op class.Nmappingr   r   lower_boundsupper_boundsstepsshared_outsc                   t        |      t        |      cxk(  rt        |      k(  sJ d        J d       |g }t        |      }t        |      \  }}	t        |      \  }
}t        |      \  }}|D cg c]  }|j                   }}t        |   |||
||	||||||       t        |	      }t        j                         g|z  } | j                  d   j                  j                  g ||  yc c}w )aw  Creates an SCF `forall` operation.

        - `lower_bounds` are the values to use as lower bounds of the loop.
        - `upper_bounds` are the values to use as upper bounds of the loop.
        - `steps` are the values to use as loop steps.
        - `shared_outs` is a list of additional loop-carried arguments or an operation
          producing them as results.
        z;Mismatch in length of lower bounds, upper bounds, and stepsNrP   r   )lenr   rM   r   r   r   	IndexTypegetr   r   r   )r!   rR   rS   rT   rU   rQ   r   r   dynamic_lbs
static_lbsdynamic_ubs
static_ubsdynamic_stepsstatic_stepsr%   r&   rankiv_typesr'   s                     r(   r   zForallOp.__init___   s   ( \!2@c%j@	IH	I@	IH	I@K/<"A,"OZ"A,"OZ&Ee&L#|'2338833 	 	
 :MMO$t+%Q%%:x:':! 4s   4C2rC   c                 :    | j                   d   j                  d   S r*   r+   r,   s    r(   r-   zForallOp.body   r.   r/   c                 ,    t        | j                        S )z7Returns the number of induction variables the loop has.)rW   staticLowerBoundr,   s    r(   r`   zForallOp.rank   s     4(())r/   c                 H    | j                   j                  d| j                   S )z7Returns the induction variables usable within the loop.Nr-   r2   r`   r,   s    r(   induction_variableszForallOp.induction_variables   s     yy"";TYY//r/   c                 H    | j                   j                  | j                  d S )r5   Nrf   r,   s    r(   r6   zForallOp.inner_iter_args   s     yy""499;//r/   c                    | j                   j                  }t        | j                         5  |st               cddd       S |t	        |      dz
     }t        |t              r|n	t               cddd       S # 1 sw Y   yxY w)za
        Returns the loop terminator if it exists.
        Otherwise, creates a new one.
        Nr   )r-   
operationsInsertionPointInParallelOprW   rE   )r!   opslasts      r(   
terminatorzForallOp.terminator   sp    
 ii""DII& 	N#~	N 	N s3x!|$D%dL94|~		N 	N 	Ns   A8,A88Br7   )r8   r9   r:   r;   r   r   r<   r=   r>   intr   r   r?   Blockr-   r`   BlockArgumentListrg   r6   rl   ro   r@   rA   s   @r(   rO   rO   [   s   5 LP.; .;uYs%BCD.; uYs%BCD.; eSj)*	.;
 eIvx$FGH.;` )e ) ) *c * * 0%6 0 0 0!2 0 0
NL 
Nr/   rO   c                   :     e Zd ZdZd fd	Zedefd       Z xZS )rl   z6Specialization of the SCF forall.in_parallel op class.c                 p    t         |   ||       | j                  j                  j	                          y )Nr   )r   r   regionr   r   )r!   r   r   r'   s      r(   r   zInParallelOp.__init__   s+    SR(!!#r/   rC   c                 4    | j                   j                  d   S Nr   )ru   r   r,   s    r(   blockzInParallelOp.block   s    {{!!!$$r/   )NN)	r8   r9   r:   r;   r   r?   rq   rx   r@   rA   s   @r(   rl   rl      s&    @$ %u % %r/   rl   c                   N     e Zd ZdZddddd fdZed        Zed        Z xZS )	IfOpz'Specialization for the SCF if op class.NF)hasElser   r   c                   |g }g }|j                  |       g }|j                  |       t        |   ||||        | j                  d   j
                  j                   g   |r' | j                  d   j
                  j                   g   yy)zCreates an SCF `if` operation.

        - `cond` is a MLIR value of 'i1' type to determine which regions of code will be executed.
        - `hasElse` determines whether the if operation has the else branch.
        Nr   r   r   )r   extendr   r   r   r   )	r!   condresults_r{   r   r   r    r&   r'   s	           r(   r   zIfOp.__init__   s     Hx $CB7%Q%%r*)DLLO""))2. r/   c                 :    | j                   d   j                  d   S )z+Returns the then block of the if operation.r   r+   r,   s    r(   
then_blockzIfOp.then_block   r.   r/   c                 :    | j                   d   j                  d   S )z+Returns the else block of the if operation.r   r   r+   r,   s    r(   
else_blockzIfOp.else_block   r.   r/   r7   )	r8   r9   r:   r;   r   r?   r   r   r@   rA   s   @r(   rz   rz      s>    1/u$4 /" ) ) ) )r/   rz   r   r   c             #   N  K   |d}|| }d} | ||g}t        |      D ]X  \  }}t        |t              rt        t        j                         |      }nt        |t              rt        d|d      |||<   Z |\  } }}t        | |||||      }	|	j                  }
t        |	j                        }t        |	j                        5  t        |      dkD  r|
||	j                  f n*t        |      dk(  r|
|d   |	j                  d   f n|
 d d d        y # 1 sw Y   y xY ww)Nr   r   zp=z must be int.r   )	enumeraterE   rp   r   rX   rY   float
ValueErrorr   r3   tupler6   rk   r-   rW   r&   )startstopr$   r   r   r   paramsipfor_opivs              r(   for_r      s-     ||T4 F&! 1a!,A5!t=122q	 E45$iSR@F		"	"Bf,,-I		$ y>Ai//^q ilFNN1$555H  s   CD%AD	D%D"D%c                        e Zd Zej                  Z	 	 	 	 d
 fd	Zedefd       Zede	fd       Z
edee   fd       ZdedefdZedee	   fd       Zdede	fd	Z xZS )IndexSwitchOpc                    t         j                  |      }t        |   |||t	        |      ||       | j
                  D ]  }|j                  j                           |&t        | j                        5   ||        d d d        |Nt        |      D ]?  \  }	}
t        | j                  |	            5   || |	| j                  |	          d d d        A y y # 1 sw Y   ZxY w# 1 sw Y   YxY w)N)num_caseRegionsr   r   )DenseI64ArrayAttrrY   r   r   rW   r   r   r   rk   default_blockr   
case_blockcases)r!   r&   r%   r   case_body_builderdefault_body_builderr   r   ru   r   caser'   s              r(   r   zIndexSwitchOp.__init__  s     "%%e,S%U 	 	
 ll 	#FMM  "	#  + 2 23 +$T*+ ($U+ >4#DOOA$67 >%dAtzz!}=> >> )+ +
> >s   7	C5C%C"%C.	rC   c                      | j                   d   S rw   r   r,   s    r(   default_regionzIndexSwitchOp.default_region"  s    ||Ar/   c                 4    | j                   j                  d   S rw   )r   r   r,   s    r(   r   zIndexSwitchOp.default_block&  s    ""))!,,r/   c                      | j                   dd  S )Nr   r   r,   s    r(   case_regionszIndexSwitchOp.case_regions*  s    ||ABr/   r   c                      | j                   |   S r7   )r   r!   r   s     r(   case_regionzIndexSwitchOp.case_region.  s      ##r/   c                 Z    | j                   D cg c]  }|j                  d    c}S c c}w rw   r   r   )r!   ru   s     r(   case_blockszIndexSwitchOp.case_blocks1  s%    /3/@/@AVa AAAs   (c                 :    | j                   |   j                  d   S rw   r   r   s     r(   r   zIndexSwitchOp.case_block5  s      #**1--r/   NNNN)r8   r9   r:   r   r;   r   r?   Regionr   rq   r   r   r   rp   r   r   r   r@   rA   s   @r(   r   r     s    ##G !>4    -u - -  hv.    $S $V $ BXe_ B B.C .E .r/   r   c           	      <    t        | ||||||      }t        |      S )N)r&   r%   r   r   r   r   r   )r   _get_op_result_or_op_results)r&   r%   r   r   r   r   r   ops           r(   index_switchr   9  s0     
+1
B (++r/   )NNNr   )&_scf_ops_genr   arithr   ir_ods_commonr   rF   r   r   r	   r   r
   	_ods_cextImportErroreRuntimeErrortypingr   r   r   r   r   register_operationr   r<   r=   r>   rp   rM   rO   rl   rz   r   r   OpResultOpResultListr    r/   r(   <module>r      s    " 	M  : 9 h5/'E /' 6/'d%
5FE367
8%
4;S	!"%  h5TNx TN 6TNn h5	%< 	% 6	% h5)4 ) 6)B 
	+/	! 	! (	!H h51.M 1. 61.p , 8\=01,O	  M
D
E1LMs   D D2$	D--D2