ó
\K]c           @` sÀ  d  d l  m Z m Z m Z d  d l Z d  d l m Z d  d l m Z d  d l j	 j
 Z d  d l j 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 d
 d l m Z d
 d l m Z d
 d l m Z e ƒ  Z e j  Z  e j! j" e j j# e j j$ d ƒ ƒ ƒ Z% e d „ Z& e  e j' e j( ƒ d „  ƒ Z) e  e j* e j( ƒ d „  ƒ Z+ e  e j, e j( ƒ d „  ƒ Z- e  e j. e j( ƒ d „  ƒ Z/ e  e j0 ƒ d „  ƒ Z1 e  e j2 e j( ƒ d „  ƒ Z3 e  e j4 e j( ƒ d „  ƒ Z5 e  e j6 e j( ƒ d „  ƒ Z7 e  e j6 ƒ d „  ƒ Z8 e  e j9 e j( ƒ d „  ƒ Z: e  e j; ƒ d „  ƒ Z< e  e j= e j> e j( e j> e j? ƒ d „  ƒ Z@ d „  ZA e  e jB e j> e j> ƒ eA d ƒ ƒ e  e jC e j> e j> ƒ eA d ƒ ƒ e  e jD jE e jF e jG e j> ƒ e  e jD jE e jF e jH e j> ƒ e  e jD jE e jF e jI e j> ƒ d  „  ƒ ƒ ƒ ZJ e  d! e jH e j> ƒ d" „  ƒ ZK d# „  ZL d$ d% „ ZM d& „  ZN d S('   i    (   t   print_functiont   absolute_importt   divisionN(   t   reduce(   t   Type(   t   ir(   t   Registry(   t   cgutils(   t   types(   t   mangle_ct   manglet   mangle_typei   (   t   target(   t   stubs(   t   hlc(   t   enumsi   c         C` s§   | j  } | j t j k r- t j j ƒ  } n |  j | j ƒ } g  | j D] } |  j | ƒ ^ qI }	 t j | |	 ƒ }
 | | | ƒ } | j	 |
 | ƒ } t
 j | _ | S(   sŸ  Insert declaration for a opencl builtin function.
    Uses the Itanium mangler.

    Args
    ----
    context: target context

    builder: llvm builder

    name: str
        symbol name

    sig: signature
        function signature of the symbol being declared

    cargs: sequence of str
        C type names for the arguments

    mangler: a mangler function
        function to use to mangle the symbol

    (   t   modulet   return_typeR   t   voidt   lcR   t   get_value_typet   argst   functiont   get_or_insert_functionR   t   CC_SPIR_FUNCt   calling_convention(   t   contextt   buildert   namet   sigt   cargst   manglert   modt   llrettyt   tt   llargst   fntyt   mangledt   fn(    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   _declare_function   s    	%c         C` sU   | \ } t  |  | d | d g ƒ } | j | | g ƒ } |  j | | t j t j ƒ S(   Nt   get_global_ids   unsigned int(   R'   t   callt   castR   t   uintpt   intp(   R   R   R   R   t   dimR(   t   res(    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   get_global_id_implA   s
    	c         C` sU   | \ } t  |  | d | d g ƒ } | j | | g ƒ } |  j | | t j t j ƒ S(   Nt   get_local_ids   unsigned int(   R'   R)   R*   R   R+   R,   (   R   R   R   R   R-   R0   R.   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   get_local_id_implJ   s
    	c         C` sU   | \ } t  |  | d | d g ƒ } | j | | g ƒ } |  j | | t j t j ƒ S(   Nt   get_group_ids   unsigned int(   R'   R)   R*   R   R+   R,   (   R   R   R   R   R-   R2   R.   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   get_group_id_implS   s
    	c         C` sU   | \ } t  |  | d | d g ƒ } | j | | g ƒ } |  j | | t j t j ƒ S(   Nt   get_num_groupss   unsigned int(   R'   R)   R*   R   R+   R,   (   R   R   R   R   R-   R4   R.   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   get_num_groups_impl\   s
    	c         C` s1   t  |  | d | d g ƒ } | j | g  ƒ } | S(   Nt   get_work_dimR   (   R'   R)   (   R   R   R   R   R6   R.   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   get_work_dim_imple   s    c         C` sU   | \ } t  |  | d | d g ƒ } | j | | g ƒ } |  j | | t j t j ƒ S(   Nt   get_global_sizes   unsigned int(   R'   R)   R*   R   R+   R,   (   R   R   R   R   R-   R8   R.   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   get_global_size_implm   s
    	c         C` sU   | \ } t  |  | d | d g ƒ } | j | | g ƒ } |  j | | t j t j ƒ S(   Nt   get_local_sizes   unsigned int(   R'   R)   R*   R   R+   R,   (   R   R   R   R   R-   R:   R.   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   get_local_size_implv   s
    	c         C` s;   | \ } t  |  | d | d g ƒ } | j | | g ƒ t S(   Nt   barriers   unsigned int(   R'   R)   t   _void_value(   R   R   R   R   t   flagsR<   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   barrier_one_arg_impl   s
    	c         C` si   | s t  ‚ t j t j ƒ } t |  | d | d g ƒ } |  j t j t j ƒ } | j | | g ƒ t	 S(   NR<   s   unsigned int(
   t   AssertionErrorR   R   t   uint32R'   t   get_constantR   t   CLK_GLOBAL_MEM_FENCER)   R=   (   R   R   R   R   R<   R>   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   barrier_no_arg_impl‡   s    c         C` s;   | \ } t  |  | d | d g ƒ } | j | | g ƒ t S(   Nt	   mem_fences   unsigned int(   R'   R)   R=   (   R   R   R   R   R>   RE   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   mem_fence_impl’   s
    	c         C` sQ   | s t  ‚ t j t j ƒ  g  ƒ } | j j d d | ƒ} | j | g  ƒ t S(   Ns   llvm.amdgcn.wave.barrierR$   (   R@   R   R   R   R   t   declare_intrinsicR)   R=   (   R   R   R   R   R$   R&   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   wavebarrier_impl›   s
    c         ` s  | \ } } } } | j  d | j  d k s2 t ‚ | j  d } | j }	 t j |	 ƒ ‰ t j d ƒ }
 t j d ƒ } d j |	 ƒ } t j ˆ ˆ |
 ˆ | g ƒ } ˆ  j j | d | ƒ} t	 j
 | _ ‡  ‡ f d †  } ˆ  j | | | ƒ | | | ƒ | g ƒ } ˆ  j | |  j | ƒ ƒ S(   Ni    i   i    i   s(   __hsail_activelanepermute_wavewidth_b{0}R   c         ` s   ˆ  j  |  ˆ ƒ S(   N(   t   bitcast(   t   val(   R   t   intbitwidth(    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyR*   ³   s    (   R   R@   t   bitwidthR   t   intt   formatR   R   R   R   R   R   R)   RI   R   (   R   R   R   R   t   srct   laneidt   identityt	   use_identt	   elem_typeRL   t   i32t   i1R   R$   R&   R*   t   result(    (   R   RK   s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt    activelanepermute_wavewidth_impl£   s     	*c         ` s   ‡  f d †  } | S(   Nc         ` sà   | j  | j d k s t ‚ | \ } } t j d ƒ } t j | | | g ƒ } | j j ˆ  d | ƒ} | j | | ƒ } t	 j
 j | d ƒ }	 | j | |	 ƒ } | j | | | j | | ƒ f ƒ }
 | j |
 |  j | j  ƒ ƒ S(   s'   
        args are (index, src)
        i   i    R$   i   (   R   R   R@   R   RM   R   R   RG   t   truncR   t   Constantt   mulR)   RI   R   (   R   R   R   R   t   idxRO   RT   R$   R&   t   fourRV   (   t   intrinsic_name(    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   _implº   s    $(    (   R]   R^   (    (   R]   s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   _gen_ds_permute¹   s    s   llvm.amdgcn.ds.permutes   llvm.amdgcn.ds.bpermutec         C` sQ  | j  \ } } } | \ } } }	 | j }
 | t j k rN | g } | g } n[ t j | | d t | ƒ ƒ} g  t | | ƒ D]' \ } } |  j | | | t j ƒ ^ q| } |
 | k rÎ t	 d |
 | f ƒ ‚ n  | j
 t | ƒ k rt	 d | j
 t | ƒ f ƒ ‚ n  |  j | ƒ |  | | ƒ } t j | | | | ƒ } | j d | |	 d d ƒS(   Nt   counts   expecting %s but got %ss#   indexing %d-D array with %d-D indext   addt   orderingt	   monotonic(   R   t   dtypeR   R,   R   t   unpack_tuplet   lent   zipR*   t	   TypeErrort   ndimt
   make_arrayt   get_item_pointert
   atomic_rmw(   R   R   R   R   t   arytyt   indtyt   valtyt   aryt   indsRJ   Rd   t   indicesR"   t   it   laryt   ptr(    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   hsail_atomic_add_tupleÑ   s"    		:s   hsail.smem.allocc         C` s4   | \ } } t  |  | d | d | d d d t j ƒS(   Nt   shapeRd   t   symbol_namet   _hsapy_smemt	   addrspace(   t   _generic_arrayR   t   SPIR_LOCAL_ADDRSPACE(   R   R   R   R   Rw   Rd   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   hsail_smem_alloc_arrayð   s    c         C` sò   t  t j | ƒ } |  j | ƒ } t j | | ƒ } | t j k rÄ | j }	 |	 j	 | | | ƒ }
 | d k r{ t
 d ƒ ‚ n t j |
 _ | t j k r© t d | ƒ ‚ n  |  j | |
 t j ƒ } n t d j t ƒ    ƒ ‚ t |  | | | | ƒ S(   Ni    s   array length <= 0s   unsupported type: %ss   addrspace {addrspace}(   R   t   operatorRZ   t   get_data_typeR   t   arrayR   R|   R   t   add_global_variablet
   ValueErrorR   t   LINKAGE_INTERNALt   linkageR   t   number_domainRh   t   addrspacecastt   SPIR_GENERIC_ADDRSPACEt   NotImplementedErrorRN   t   localst   _make_array(   R   R   Rw   Rd   Rx   Rz   t	   elemcountt   lldtypet   larytyt   lmodt   gvmemt   dataptr(    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyR{   ø   s    	t   Cc         C` sŽ  t  | ƒ } t j d | d | d d ƒ } |  j | ƒ |  | ƒ } t |  ƒ }	 |  j | ƒ }
 |
 j |	 ƒ } | g } x9 t t | d ƒ ƒ D]! \ } } | j	 | | d ƒ qŒ Wg  t | ƒ D] } | ^ q¾ } g  | D] } |  j
 t j | ƒ ^ q× } g  | D] } |  j
 t j | ƒ ^ qÿ } |  j | d | j | | j j ƒ d t j | | ƒ d	 t j | | ƒ d
 |  j
 t j | ƒ d d  ƒ| j ƒ  S(   NRd   Ri   t   layoutR‘   i   iÿÿÿÿt   dataRw   t   stridest   itemsizet   meminfo(   Rf   R   t   ArrayRj   t   _get_target_dataR   t   get_abi_sizet	   enumeratet   reversedt   appendRB   R,   t   populate_arrayRI   R“   t   typeR   t
   pack_arrayt   Nonet	   _getvalue(   R   R   R   Rd   Rw   R’   Ri   Rm   Rp   t
   targetdataRŒ   R•   t   rstridesRs   t   lastsizet   sR”   t   kshapet   kstrides(    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyRŠ     s&    	#((c         C` s   t  j t j |  j ƒ S(   N(   t   llt   create_target_dataR   t
   DATALAYOUTt   address_size(   R   (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyR˜   1  s    (O   t
   __future__R    R   R   R~   t	   functoolsR   t   llvmlite.llvmpy.coreR   t   llvmpyt   coreR   t   llvmlite.bindingt   bindingR¨   t   llvmliteR   t   numba.targets.imputilsR   t   numbaR   R   t   numba.itanium_manglerR	   R
   R   t    R   R   R   R   t   registryt   lowerRY   t   nullt   pointerRM   R=   R'   R(   RA   R/   R0   R1   R2   R3   R4   R5   R6   R7   R8   R9   R:   R;   R<   R?   RD   RE   RF   t   wavebarrierRH   t   activelanepermute_wavewidtht   Anyt   bool_RW   R_   t
   ds_permutet   ds_bpermutet   atomicRa   R—   R,   t   UniTuplet   TupleRv   R}   R{   RŠ   R˜   (    (    (    s0   lib/python2.7/site-packages/numba/roc/hsaimpl.pyt   <module>   sV   		*$								'	%%!!	