o
    8ήc8                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZ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 d dlmZ d	d
lmZ d dlmZmZ G dd dejZe de jZG dd deZG dd deZ dS )    N)ir)typingtypes	debuginfoitanium_manglercgutils)
Dispatcher)cached_property)BaseContext)MinimalCallConv)	cmathdecl   )nvvm)codegen	nvvmutilsc                       s$   e Zd Zdd Z fddZ  ZS )CUDATypingContextc                 C   sp   ddl m}m}m}m} ddlm} | |j | |j | t	j | |j | |j | |j
 d S )Nr   )cudadeclcudamathlibdevicedeclvector_typesr   )enumdecl) r   r   r   r   numba.core.typingr   install_registryregistryr   typing_registry)selfr   r   r   r   r    r   8/tmp/pip-target-vg8gfxp4/lib/python/numba/cuda/target.pyload_additional_registries   s   z,CUDATypingContext.load_additional_registriesc                    s   ddl m} t|trJt||sJz|j}W n4 tyI   |js#td|j	 }d|d< |
dd|d< |
dd|d< ||j|}||_|}Y nw tt| |S )	Nr   )CUDADispatcherz<using cpu function on device but its compilation is disabledTdevicedebugFopt)numba.cuda.dispatcherr    
isinstancer   _CUDATypingContext__dispatcherAttributeError_can_compile
ValueErrortargetoptionscopygetpy_funcsuperr   resolve_value_type)r   valr    r*   disp	__class__r   r   r/       s$   


z$CUDATypingContext.resolve_value_type)__name__
__module____qualname__r   r/   __classcell__r   r   r2   r   r      s    r   z	[^a-z0-9]c                       s   e Zd ZdZdZd* fdd	Zedd Zedd Zd	d
 Z	dd Z
dd Zdd Zedd Zedd Zedd ZdddddZ	d+ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Z  ZS ),CUDATargetContextTcudac                    s   t  || d S N)r.   __init__)r   	typingctxtargetr2   r   r   r;   D   s   zCUDATargetContext.__init__c                 C   s   t  jrtjS tjS r:   )r   NVVM	is_nvvm70r   	DIBuilderNvvmDIBuilderr   r   r   r   r@   G   s   
zCUDATargetContext.DIBuilderc                 C      dS )NFr   rB   r   r   r   enable_boundscheckN      z$CUDATargetContext.enable_boundscheckc                 C   s   | j |S r:   )_internal_codegen_create_empty_module)r   namer   r   r   create_moduleT   s   zCUDATargetContext.create_modulec                 C   s   t d| _ttj| _d S )Nznumba.cuda.jit)r   JITCUDACodegenrF   llcreate_target_datar   data_layout_target_datarB   r   r   r   initW   s   zCUDATargetContext.initc                 C   s   ddl m}m}m} ddl m}m}m} ddl m}m} ddl m	}	 ddl
m}
 ddl
m} dd	lm}m}m}m}m} | |j | |j | |j | |	j | |j | |j d S )
Nr   )numberstupleobjslicing)rangeobj	iteratorsenumimpl)unicodecharseq)	cmathimpl)arrayobj)
npdatetimer   )cudaimpl	printimpllibdeviceimplmathimplr   )numba.cpythonrP   rQ   rR   rS   rT   rU   rV   rW   rX   numba.nprY   rZ   r   r[   r\   r]   r^   r   r   r   impl_registry)r   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r   r   r   r   r   [   s   z,CUDATargetContext.load_additional_registriesc                 C      | j S r:   )rF   rB   r   r   r   r   o   s   zCUDATargetContext.codegenc                 C   rb   r:   )rN   rB   r   r   r   target_datar   s   zCUDATargetContext.target_datac                    s*   ddl m  d}t fdd|D }|S )z
        Some CUDA intrinsics are at the module level, but cannot be treated as
        constants, because they are loaded from a special register in the PTX.
        These include threadIdx, blockDim, etc.
        r   r9   )	threadIdxblockDimblockIdxgridDimlaneidwarpsizec                    s   g | ]	}t  |fqS r   )r   Module).0ncrd   r   r   
<listcomp>   s    z;CUDATargetContext.nonconst_module_attrs.<locals>.<listcomp>)numbar9   tuple)r   	nonconstsnonconsts_with_modr   rd   r   nonconst_module_attrsv   s   z'CUDATargetContext.nonconst_module_attrsc                 C   s   t | S r:   )CUDACallConvrB   r   r   r   	call_conv   s   zCUDATargetContext.call_convr   Nabi_tagsuidc                C   s   t j||||dS )Nrv   )r   mangle)r   rH   argtypesrw   rx   r   r   r   mangler   s   
zCUDATargetContext.manglerc                 C   sT   t j|jdd}|  j|j d|||d}	|	| | |	|||||}
|	|
fS )a  
        Adapt a code library ``codelib`` with the numba compiled CUDA kernel
        with name ``fname`` and arguments ``argtypes`` for NVVM.
        A new library is created with a wrapper function that can be used as
        the kernel entry point for the given kernel.

        Returns the new code library and the wrapper function.

        Parameters:

        codelib:       The CodeLibrary containing the device function to wrap
                       in a kernel call.
        fndesc:        The FunctionDescriptor of the source function.
        debug:         Whether to compile with debug.
        nvvm_options:  Dict of NVVM options used when compiling the new library.
        filename:      The source filename that the function is contained in.
        linenum:       The source line that the function is on.
        max_registers: The max_registers argument for the code library.
        cudapyns_kernel_)
entry_namenvvm_optionsmax_registers)r   prepend_namespacellvm_func_namer   create_libraryrH   add_linking_librarygenerate_kernel_wrapper)r   codelibfndescr"   r   filenamelinenumr   kernel_namelibrarywrapperr   r   r   prepare_cuda_kernel   s   

z%CUDATargetContext.prepare_cuda_kernelc           "   	      s  |j }| |}t|j}	tt |	}
| dttd| j	
tjg|	 }t||j}tj|jdd}t|
| t d}|rd| j|| d}| ||j|| |||  fdd}|d	}g }g }d
D ]}||d|  ||d|  qu|| j}| j	||tj||\}}|rct||j |  W d   n1 sw   Y  | |!|j" t#|j$j%d}t&' j(r|)|||j*dd}|+|d}n(t|j$|j$|j$|j$g}d}tj||d}|,||||j*g}|-d||}t./|}| |4 t0d
|D ]\}} |1|}!|2|!|  qt0d
|D ]\}} |3|}!|2|!|  q2W d   n	1 sNw   Y  W d   n	1 s^w   Y  |  t&4  |5 |rx|6  |6  |7 j  S )z
        Generate the kernel wrapper in the given ``library``.
        The function being wrapped is described by ``fndesc``.
        The wrapper function is returned.
        zcuda.kernel.wrapper    r|   r}   r   )modulefilepathcgctxc                    s4    j |  }ttd|}t|jjd |_|S )Nr   )	rH   r   add_global_variabler   IntTypeConstanttypepointeeinitializer)postfixrH   gvwrapfnwrapper_moduler   r   define_error_gv   s   
zBCUDATargetContext.generate_kernel_wrapper.<locals>.define_error_gv__errcode__xyzz	__tid%s__z__ctaid%s__N	monotonicr   ___numba_atomic_i32_cas_hack)rH   z==)8rz   get_arg_packerlistargument_typesr   FunctionTypeVoidTyperI   r   ru   get_return_typer   pyobjectFunctionr   r   r   rH   	IRBuilderappend_basic_blockr@   mark_subprogramargsmark_locationappendfrom_argumentscall_functionvoidr   	if_likelyis_okret_voidif_thennot_is_python_excr   r   r   r   r>   r?   cmpxchgcodeextract_valuecallicmp_unsignedr   SRegBuilderziptidstorectaidset_cuda_kerneladd_ir_modulefinalizeget_function)"r   r   r   r   r"   r   r   rz   arginfoargtyswrapfntyfntyfuncprefixedbuilderr   r   gv_excgv_tidgv_ctaidicallargsstatus_oldxchgchangedcasfntycas_hackcasfnsregdimptrr0   r   r   r   r      s   









z)CUDATargetContext.generate_kernel_wrapperc              	      s<  |j } fddt|jddD }ttdt|}t||}tj	}t
j||jd|d}	d|	_d	|	_||	_ |j}
 |
}d
|d   |	_t|td|}|	ttd|}|||g} | |} fdd|jD } fdd|jD } j||||jj|||j|j dd |! S )i
        Unlike the parent version.  This returns a a pointer in the constant
        addrspace.
        c                       g | ]	}  tj|qS r   )get_constantr   byte)rl   r   rB   r   r   rn     s    z9CUDATargetContext.make_constant_array.<locals>.<listcomp>A)order   _cudapy_cmem	addrspaceinternalT   r   c                    r   r   r   r   intprl   srB   r   r   rn   0      c                    r   r   r   r   rB   r   r   rn   1  r   N)datashapestridesitemsizeparentmeminfo)"r   itertobytesr   	ArrayTyper   lenr   r   ADDRSPACE_CONSTANTr   r   r   linkageglobal_constantr   get_data_typedtypeget_abi_sizeof
bit_lengthalignr   insert_addrspace_convbitcastPointerTyper   
make_arrayr   r   populate_arrayr   r   r   	_getvalue)r   r   arytyarrlmod	constvals
constarytyconstaryr   r   lldtyper  convaddrspaceptrgenptrarykshapekstridesr   rB   r   make_constant_array  s:   

z%CUDATargetContext.make_constant_arrayc                 C   s   t |dd }ddt|g}|j|}|du r2t j||j	|t
jd}d|_d|_||_|j	jj}||t
jS )	r   zutf-8    $__conststring__Nr   r   T)r   make_bytearrayencodejoinr   mangle_identifierglobalsr,   r   r   r   r   r   r   r   r   elementr  
as_pointer)r   modstringtextrH   r   chartyr   r   r   insert_const_string:  s   
z%CUDATargetContext.insert_const_stringc                 C   s"   |j }| ||}| ||tjS )z
        Insert a constant string in the constant addresspace and return a
        generic i8 pointer to the data.

        This function attempts to deduplicate.
        )r   r%  r  r   r   )r   r   r"  r  r   r   r   r   insert_string_const_addrspaceP  s
   z/CUDATargetContext.insert_string_const_addrspacec                 C   s*   |j }|jj}t|||}|||gS )zI
        Perform addrspace conversion according to the NVVM spec
        )r   r   r   r   r  r   )r   r   r   r   r  	base_typer  r   r   r   r  \  s   z'CUDATargetContext.insert_addrspace_convc                 C   rC   )zRun O1 function passes
        Nr   )r   r   r   r   r   optimize_functione  rE   z#CUDATargetContext.optimize_functionrd   r:   )r4   r5   r6   implement_powi_as_math_callstrict_alignmentr;   propertyr@   rD   rI   rO   r   r   rc   r	   rs   ru   r{   r   r   r  r%  r&  r  r(  r7   r   r   r2   r   r8   @   s6    





"`,	r8   c                   @   s   e Zd ZdS )rt   N)r4   r5   r6   r   r   r   r   rt   s  s    rt   )!rellvmlite.bindingbindingrK   llvmliter   
numba.corer   r   r   r   r   numba.core.dispatcherr   numba.core.utilsr	   numba.core.baser
   numba.core.callconvr   r   r   cudadrvr   
numba.cudar   r   r   compileIVALID_CHARSr8   rt   r   r   r   r   <module>   s"    )  5