a
    gh                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlmZ e	dddZeeZe jeZe jed	ZG d
d deZdd Zdd Zdd Zdd ZdS )    N)	ASTSourcemake_backend)	GPUTarget)GluonASTSource)irZcudad       Z	FileCheckc                       s(   e Zd Z fddZ fddZ  ZS )
MatchErrorc                    s   t  | || _d S N)super__init__
module_str)selfmessager   	__class__ ?/var/www/auris/lib/python3.9/site-packages/triton/_filecheck.pyr      s    zMatchError.__init__c                    s   t    d| j S )N
)r   __str__r   )r   r   r   r   r      s    zMatchError.__str__)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r	      s   r	   c           	      C   s  t  }tj|d}t|d}|| W d    n1 sB0    Y  tj|d}t|d}|| W d    n1 s0    Y  ztjt	|d|gtj
d W n: tjy } z |jd}t|W Y d }~n
d }~0 0 W d    n1 s0    Y  d S )Nmodulewexpectedz--input-file)stderrunicode_escape)tempfileTemporaryDirectoryospathjoinopenwrite
subprocesscheck_outputfilecheck_pathSTDOUTCalledProcessErroroutputdecode
ValueError)	namer   check_templatetempdirZtemp_moduletempZtemp_expectederrordecodedr   r   r   run_filecheck"   s    
((
r4   c                 C   s   dd | j D }dd tt|D }dd t||D }|  rFtnt}|| |d}t }t	| t
	| | }t
tf i |}t
|}	t
 }
|||	|
|}| sJ |S )Nc                 S   s   g | ]
}|j qS r   )r.   ).0xr   r   r   
<listcomp>5       zrun_parser.<locals>.<listcomp>c                 S   s   g | ]}d | qS )argr   )r5   ir   r   r   r7   6   r8   c                 S   s   i | ]\}}||qS r   r   )r5   kvr   r   r   
<dictcomp>7   r8   zrun_parser.<locals>.<dictcomp>)fn	signature)paramsrangelenzipZis_gluonr   r   r   contextZload_dialectsstub_backendZparse_optionsdictZget_codegen_implementationZget_module_mapZmake_irverify)	kernel_fnZsigkeysZsigvalsr?   Z
source_clssrcrD   Zextra_optionsoptionsZcodegen_fnsZ
module_mapr   r   r   r   
run_parser4   s    


rK   c                 C   sJ   t | tjjsJ t| j}|d u r.tdt| }t	d|
 | d S )Nz=kernel function must have a docstring with FileCheck templateplaceholder)
isinstancetritonZruntimeZJITFunctioninspect	getsourcer>   r-   rK   r4   Zstr_nodebug)rH   r/   Zmlir_moduler   r   r   run_filecheck_testH   s    rQ   c                    s    fdd}|S )Nc                      s   t   d S r
   )rQ   r   r>   r   r   test_fnT   s    zfilecheck_test.<locals>.test_fnr   )r>   rS   r   rR   r   filecheck_testR   s    rT   )r!   rO   r&   r   rN   Ztriton.compilerr   r   Ztriton.backends.compilerr   Z"triton.experimental.gluon._runtimer   Ztriton._C.libtritonr   Zstub_targetrE   r"   dirname__file__Z
triton_dirr#   r(   r-   r	   r4   rK   rQ   rT   r   r   r   r   <module>   s"   

