o
    ohi5                  
   @   s.  d dl Z d dlZd dlmZmZ d dlmZmZmZ G dd dZ	dee
 fddZde
fd	d
ZG dd deZG dd deZG dd dZdgZde
de
de
de
ddf
ddZedkre  Zejddddd ejdddd ejd dd!d ejd"dd#d$d e Zeejejejej dS dS )%    N)ABCabstractmethod)DictListOptionalc                   @   s   e Zd ZU eed< eed< eed< ee ed< ee ed< dededed	ee d
ee ddfddZedefddZedefddZ	edefddZ
edee fddZedee fddZdS )Symbol_name_op_name	_ret_type
_arg_names
_arg_typesnameop_nameret_type	arg_names	arg_typesreturnNc                 C   s*   || _ || _|| _t|| _t|| _dS )a#  
        A symbol is a function declaration.
        :param name: name of the symbol
        :param op_name: name of the operation
        :param ret_type: return type of the operation
        :param arg_names: names of the arguments
        :param arg_types: types of the arguments
        N)r   r	   r
   listr   r   )selfr   r   r   r   r    r   m/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/triton/tools/build_extern.py__init__   s
   
zSymbol.__init__c                 C      | j S Nr   r   r   r   r   r   $      zSymbol.namec                 C   r   r   )r	   r   r   r   r   r   (   r   zSymbol.op_namec                 C   r   r   )r
   r   r   r   r   r   ,   r   zSymbol.ret_typec                 C   r   r   )r   r   r   r   r   r   0   r   zSymbol.arg_namesc                 C   r   r   )r   r   r   r   r   r   4   r   zSymbol.arg_types)__name__
__module____qualname__str__annotations__r   r   propertyr   r   r   r   r   r   r   r   r   r      s:   
 
r   r   c                 C   sL   | dkrdS | dkrdS | dkrdS | dkrdS | d	krd
S | dkr$dS d S )Ni32int32u32uint32i64int64u64uint64floatfp32doublefp64r   type_strr   r   r   convert_type9   s   r1   c                 C   s   | dkrdS | dkrdS | S )Nr$   r&   r(   r*   r   r/   r   r   r   to_unsignedK   s
   r2   c                   @   s   e Zd ZU eed< eed< eeef ed< eed< eed< 		ddeded	ed
eddf
ddZe	defddZ
e	defddZe	deeef fddZe	defddZedddZedefddZdddZdS )ExternLibraryr   _path_symbols_format	_groupingTr   pathformatgroupingr   Nc                 C   s"   || _ || _i | _|| _|| _dS )z
        Abstract class for extern library.
        :param name: name of the library
        :param path: path of the library
        :param format: whether to format the generated stub file
        N)r   r4   r5   r6   r7   )r   r   r8   r9   r:   r   r   r   r   [   s
   
zExternLibrary.__init__c                 C   r   r   r   r   r   r   r   r   n   r   zExternLibrary.namec                 C   r   r   r4   r   r   r   r   r8   r   r   zExternLibrary.pathc                 C   r   r   )r5   r   r   r   r   symbolsv   r   zExternLibrary.symbolsc                 C   r   r   )r7   r   r   r   r   r:   z   r   zExternLibrary.groupingc                 C      d S r   r   )r   
input_filer   r   r   parse_symbols~      zExternLibrary.parse_symbolsc                 C   r=   r   r   r   r   r   r   _output_stubs   r@   zExternLibrary._output_stubsc                 C   s   |   }|d u st|dkrtd| d| j d}t|d8}|| |  | jrPtj	dddd	|gtj
d
  tj	d|gtj
d
  W d    d S W d    d S 1 s[w   Y  d S )Nr   zfile_str is empty/z.pywautopep8z-az-rz-istdoutisort)rA   len	Exceptionr   openwritecloser6   
subprocessPopenPIPEcommunicate)r   
output_dirfile_stroutput_filefr   r   r   generate_stub_file   s   
"z ExternLibrary.generate_stub_file)TTr   N)r   r   r   r    r!   r   r   boolr   r"   r   r8   r<   r:   r   r?   rA   rU   r   r   r   r   r3   T   s@   
 
r3   c                       sl   e Zd ZU eeee f ed< d fddZe	de
e fddZddd	Zdd
dZdefddZ  ZS )	Libdevice_symbol_groupsr   Nc                    s   t  d| i | _d| _dS )z_
        Constructor for Libdevice.
        :param path: path of the libdevice library
        	libdeviceTN)superr   rY   is_purer   r8   	__class__r   r   r      s   
zLibdevice.__init__c                 C   s<  |  d}|d }|d }|  }|d dkrd S t|d }|d u r%d S | d}|d dd}|dd}d|v r>d S |d  d	}	g }
g }t|	D ]#\}}t|  d }|d u r` d S d
t| }|
| || qM|dkr~t|
d |
d< n|drt|}t|
D ]
\}}t||
|< qt|||||
S )N@r      internal( __nv_ieee,argsadu)	splitr1   replace	enumerater    appendr2   
startswithr   )lineentriesret_strfunc_strret_strsr   	func_strs	func_namer   arg_strsr   r   iarg_strarg_typearg_namer   r   r   _extract_symbol   s>   



zLibdevice._extract_symbolc                 C   s`  i }| j  D ]	}|j}|||< qi ddddddddd	dd
dddddddddddddddddddddddi dd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d2d4d5d6d5d7d8d9d8d:d;d<d;i d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOddPddQdRdSdTdUdVdWdXdYdZd[d\i d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdldndodpdodqdrdsdtdudvdwdxdydzd{d|i d}d~ddddddddddddddddddddddddddddddddi ddddddddddddddddddddddddddddddddddi ddddddddddddddddddddddddœddœddȓddʓdd̓ddΓi ddГddғddԓdd֓ddؓddړddܓddޓddޓddddddddddddddddi ddddddddddddddddddddddddd ddddddddd	d
dd}| j  D ](}|j}||v r|| }||_|| jv r&| j| | q|g| j|< qd S (  Nllabsabsacosfacosacoshfacoshdadd_rdadd_rdfadd_rddadd_rnadd_rnfadd_rndadd_ruadd_rufadd_rudadd_rzadd_rzfadd_rzasinfasinasinhfasinhatanfatanatan2fatan2atanhfatanhbrevllbrevcbrtfcbrtceilfceilclzllclz	copysignfcopysigncosfcoscoshfcoshcospifcospicyl_bessel_i0fcyl_bessel_i0cyl_bessel_i1fcyl_bessel_i1fdiv_rddiv_rdddiv_rdfdiv_rndiv_rnddiv_rnfdiv_rudiv_ruddiv_rufdiv_rzdiv_rzddiv_rzerfferferfcferfcerfcinvferfcinverfcxferfcxerfinvferfinvexpfexpexp10fexp10exp2fexp2expm1fexpm1fabsffabsfast_fdivideffast_divideffdimffdimffsllffsfloorffloorfmaffmafmaf_rdfma_rdfmaf_rnfma_rnfmaf_rufma_rufmaf_rzfma_rzfmodffmoduhaddhaddhypotfhypotilogbfilogbisinffisinfisinfdisnanfisnanisnandj0fj0j1fj1jnfjnldexpfldexplgammaflgammallrintfllrintllroundfllroundlogfloglog10flog10log1pflog1plog2flog2logbflogbumaxmaxllmaxullmaxfmaxffmaxuminminllminullminfminffmindmul_rdmul_rdfmul_rddmul_rnmul_rnfmul_rndmul_rumul_rufmul_rudmul_rzmul_rzfmul_rzumul24mul24umulhimulhimul64hiumul64hi
nearbyintf	nearbyint
nextafterf	nextafternorm3dfnorm3dnorm4dfnorm4dnormcdffnormcdfnormcdfinvf
normcdfinvpopcllpopcpowifpowpowipowfrcbrtfrcbrtfrcp_rdrcp_rddrcp_rdfrcp_rnrcp_rndrcp_rnfrcp_rurcp_rudrcp_rufrcp_rzrcp_rzdrcp_rz
remainderf	remainderurhaddrhaddrhypotfrhypotrintfrintrnorm3dfrnorm3drnorm4dfrnorm4droundfroundrsqrtfrsqrt	frsqrt_rnrsqrt_rnusadri   scalbnfscalbnsignbitfsignbitsignbitdsinfsinsinhfsinhsinpifsinpisqrtfsqrtfsqrt_rdsqrt_rddsqrt_rdfsqrt_rnsqrt_rndsqrt_rnfsqrt_rusqrt_rudsqrt_rufsqrt_rzsqrt_rzdsqrt_rzfsub_rdsub_rddsub_rdfsub_rnsub_rndsub_rnfsub_rusub_rudsub_rufsub_rzsub_rzdsub_rztanftantanhftanhtgammaftgammatruncftruncy0fy0y1yn)y1fynf)r5   valuesr   r	   rY   ro   )r   
symbol_setsymbolr   renamingr   r   r   _group_symbols   s  
						





!
zLibdevice._group_symbolsc                 C   s`   t | jdkr	d S tdd|g  }|D ]}| |}|d u r#q|| j|j< q| 	  d S )Nr   grepdefine)
rH   r<   rM   check_outputdecode
splitlinesr}   r5   r   r  )r   r>   outputrq   r  r   r   r   r?      s   
zLibdevice.parse_symbolsc                 C   s2  d}d}d}| j  D ]}|d7 }d|d j d}|d jD ]	}|| d7 }q|d7 }d	| j d
}|d jD ]	}|| d7 }q9|d7 }d}|D ](}	|d7 }|	jD ]
}
|d|
 d7 }qTd|	j d}|d|	j d | d 7 }qK|d7 }||7 }|d| j 7 }|d7 }||| d 7 }q|| | }|S )Nzfrom . import core
rd   z@core.extern
zdef r   rc   z, z_builder=None):
z!	return core.extern_elementwise("z", libdevice_path(), [z], 
{zcore.dtype("z"),z")z): ("z", z),
}z
, is_pure=z, _builder=_builder)

)	rY   r  r   r   r   r   r   r   r\   )r   
import_str
header_strrt   r<   func_name_strr|   
return_strarg_type_symbol_dict_strr  r{   r   rR   r   r   r   rA     s6   
zLibdevice._output_stubsrV   )r   r   r   r   r    r   r   r!   r   staticmethodr   r}   r  r?   rA   __classcell__r   r   r^   r   rX      s   
 	
)
2rX   c                   @   s^   e Zd ZU eed< eed< dddZdeddfdd	Zedefd
dZedefddZ	dS )LLVMDisassemblerr4   _ll_filer   Nc                 C   s   || _ d| _dS )zf
        Invoke llvm-dis to disassemble the given file.
        :param path: path to llvm-dis
        z/tmp/extern_lib.llN)r4   r  r]   r   r   r   r   5  s   
zLLVMDisassembler.__init__lib_pathc                 C   s$   t j| j|d| jgt jd  d S )Nz-orE   )rM   rN   r4   ll_filerO   rP   )r   r  r   r   r   disasm=  s   $zLLVMDisassembler.disasmc                 C   r   r   )r  r   r   r   r   r  @  r   zLLVMDisassembler.ll_filec                 C   r   r   r;   r   r   r   r   r8   D  r   zLLVMDisassembler.pathrV   )
r   r   r   r    r!   r   r  r"   r  r8   r   r   r   r   r  1  s   
 
r  rZ   llvm_dis_pathr  lib_namerQ   c                 C   sL   |dkr	t |}ntd| t| }|| ||j || dS )a  
      Interface function to build the library file.
      :param llvm_dis_path: path to the llvm-dis binary
      :param lib_path: path to the external library file
      :param lib_name: name of the library
      :param output_dir: path to the output directory
    rZ   zUnknown extern library: N)rX   rI   r  r  r?   r  rU   )r  r  r  rQ   
extern_libllvm_disassemblerr   r   r   buildL  s   

r  __main__z
--llvm-diszPath to llvm-diszllvm-dis)desthelpdefaultz
--lib-pathzPath to the extern library)r  r  z
--lib-namezName of the extern libraryz--outputzOutput file pathz/tmp/)argparserM   abcr   r   typingr   r   r   r   r    r1   r2   r3   rX   r  extern_libsr  r   ArgumentParserparseradd_argument
parse_argsargsr  r  r  rQ   r   r   r   r   <module>   s@    2	@ 
