o
    n0jA                     @   s   d dl Z 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 d dlmZ e
ddd	ZejeZejed
ZG dd deZdd Zdi efddZdd Zdd ZdS )    N)	ASTSourcemake_backend)	GPUTarget)GluonASTSource)create_function_from_signature)ir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__ X/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/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  r}tj|d}t|d}|| W d    n1 s!w   Y  tj|d}t|d}|| W d    n1 sBw   Y  ztjt	|d|dgtj
d W n tjym } z
|jd}t|d }~ww W d    d S 1 syw   Y  d S )Nmodulewexpectedz--input-filez--dump-input-context=50)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*   

"r6   r   c                 C   s   d|vrt |}d|d< t|}t| j| j|}||i |\}}}| |||||\}}	}
}|  r5tnt}|| |	|
|}t	
 }t	| || ||}| }||||||}|S )NZsanitize_overflowF)dictr   r   	signatureparamsZ
_pack_argsZis_gluonr   r   r   contextZload_dialectsZget_codegen_implementationZget_module_mapZmake_ir)	kernel_fnargskwargstargetbackendZbinderZ
bound_argsZspecializationoptionsr8   Z
constexprsattrsZ
source_clssrcr:   Zcodegen_fnsZ
module_mapr   r   r   r   
run_parser6   s(   


rC   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	getsourcefnr/   rC   r6   Zstr_nodebug)r;   r1   Zmlir_moduler   r   r   run_filecheck_testP   s   rJ   c                    s   t   fdd}|S )Nc                      s   t   d S r   )rJ   r   rI   r   r   test_fn\   s   zfilecheck_test.<locals>.test_fn)	functoolswraps)rI   rL   r   rK   r   filecheck_testZ   s   rO   )rM   r#   rG   r(   r!   rF   Ztriton.compilerr   r   Ztriton.backends.compilerr   Z"triton.experimental.gluon._runtimer   Ztriton.runtime.jitr   Ztriton._C.libtritonr   Zstub_targetr$   dirname__file__Z
triton_dirr%   r*   r/   r   r6   rC   rJ   rO   r   r   r   r   <module>   s&    

