o
    8ήc4                     @   sd  d dl Z d dl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 d dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lm Z  d dl!Zd dl"m#Z#m$Z$m%Z% d dl&m'Z' G dd dej(Z)G dd dej(Z*G dd deZ+edddddddddddddd d!d"d#d$Z,G d%d& d&e,eZ-d'd( Z.d)d* Z/d+d, Z0dS )-    N)ir)_dynfunc)PyCallWrapper)BaseContextPYOBJECT)	utilstypesconfigcgutilscallconvcodegen	externalsfastmathpass
intrinsics)cached_property)TargetOptionsinclude_default_options)rtsys)global_compiler_lock)ParallelOptionsFastMathOptionsInlineOptions)ufunc_dbc                   @   s   e Zd ZdejfgZdS )ClosureBodyenvN__name__
__module____qualname__r   pyobject_fields r!   r!   5/tmp/pip-target-vg8gfxp4/lib/python/numba/core/cpu.pyr      s    r   c                   @   s    e Zd ZdejfdejfgZdS )EnvBodyglobalsconstsNr   r!   r!   r!   r"   r#      s    r#   c                       s   e Zd ZdZdZd- fdd	Zdd Zedd	 Zd
d Z	e
dd Zdd Zdd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Z	"d.d#d$Zd%d& Zd'd( Zd)d* Zd+d, Z  ZS )/
CPUContextz0
    Changes BaseContext calling convention
    Tcpuc                    s   t  || d S N)super__init__)self	typingctxtarget	__class__r!   r"   r*   '   s   zCPUContext.__init__c                 C   s   | j |S r(   )_internal_codegen_create_empty_module)r+   namer!   r!   r"   create_module+   s   zCPUContext.create_modulec                 C   sp   t jdk| _td| _t dkrt	d t
j|  t|  i | _ddlm} ddlm} || j|< d S )N    z
numba.execarmv7lzlibgcc_s.so.1r   )_lower_parfor_parallel)Parfor)r   MACHINE_BITSis32bitr   JITCPUCodegenr0   platformmachinellload_library_permanentlyr   c_math_functionsinstallr   
initializelower_extensionsnumba.parfors.parfor_loweringr6   numba.parfors.parforr7   )r+   r6   r7   r!   r!   r"   init.   s   

zCPUContext.initc           %      C   s\  ddl m}m}m}m}m}m}m}m}m	}	m
}
m}m}m} ddlm} ddlm}m} ddlm}m}m}m} ddlm}m} ddlm}m} ddlm}m} dd	l m!}m"} dd
lm#} ddlm$} ddl m%}m&} m'}!m(}" ddlm)}# ddl*m+}$ | ,|j- | ,|#j- | ,| j- | ,|j- | ,|!j- | ,|"j- | ,|$j. t/j0j12  d S )Nr   )builtinscharseqenumimplhashingheapq	iteratorslistobjnumbersrangeobjsetobjslicingtupleobjunicode)optional)gdb_hookliteral)linalg
polynomial	arraymatharrayobj)generator_coregenerator_methods)	typeddictdictimpl)	typedlist
listobject)jitclassfunction_type)
npdatetime)npyimpl)	cmathimplmathimpl	printimpl
randomimpl)cffiimpl)ClassBuilder)3numba.cpythonrF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   
numba.corerS   
numba.miscrT   rU   numba.nprV   rW   rX   rY   numba.np.randomrZ   r[   numba.typedr\   r]   r^   r_   numba.experimentalr`   ra   rb   rc   rd   re   rf   rg   rh    numba.experimental.jitclass.baseri   install_registryregistryclass_impl_registrynumbacoreentrypointsinit_all)%r+   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   jitclassimplr!   r!   r"   load_additional_registriesD   s*   <z%CPUContext.load_additional_registriesc                 C   s   | j jS r(   )r0   target_datar+   r!   r!   r"   r{   c   s   zCPUContext.target_datac                 K   s    t j|fi |}| j|ddS )NT)r0   aot_mode)r   AOTCPUCodegen	subtarget)r+   r2   aot_optionsaot_codegenr!   r!   r"   with_aot_codegeng   s   zCPUContext.with_aot_codegenc                 C   s   | j S r(   )r0   r|   r!   r!   r"   r   l   s   zCPUContext.codegenc                 C   s
   t | S r(   )r   CPUCallConvr|   r!   r!   r"   	call_convo   s   
zCPUContext.call_convc                 C   s$   t ||tjd }t| ||ddS )z
        From the given *envptr* (a pointer to a _dynfunc.Environment object),
        get a EnvBody allowing structured access to environment fields.
        offsetof_env_bodyT)refcast_ref)r
   pointer_addr   
_impl_infor#   )r+   builderenvptrbody_ptrr!   r!   r"   get_env_bodys   s   zCPUContext.get_env_bodyc                 C   sX   |  |j| | j}||}| |}|j|| jjd | ||}|	| j
||S )N)	debug_msg)declare_env_globalmoduleget_env_namefndescloadget_python_apiemit_environment_sentryenv_namer   get_env_managerenvironment)r+   r   envgvenvargpyapienv_bodyr!   r!   r"   r   |   s   


zCPUContext.get_env_managerc                 C   s   t j||tjd |dS )z~
        From the given *genptr* (a pointer to a _dynfunc.Generator object),
        get a pointer to its state area.
        offsetof_generator_state)return_type)r
   r   r   r   )r+   r   genptrr   r!   r!   r"   get_generator_state   s   zCPUContext.get_generator_statec                 C      ddl m} || |||S )zR
        Build a list from the Numba *list_type* and its initial *items*.
        r   )rL   )rj   rL   
build_list)r+   r   	list_typeitemsrL   r!   r!   r"   r         zCPUContext.build_listc                 C   r   )zP
        Build a set from the Numba *set_type* and its initial *items*.
        r   )rO   )rj   rO   	build_set)r+   r   set_typer   rO   r!   r!   r"   r      r   zCPUContext.build_setc                 C   s   ddl m} || ||||S )Nr   )
dictobject)ro   r   	build_map)r+   r   	dict_type
item_typesr   r   r!   r!   r"   r      s   zCPUContext.build_mapc                 C   s4   | j r
t|| j  | jrt| |tj d S r(   )	fastmathr   rewrite_moduler9   r   
fix_divmodadd_linking_libraryr   library)r+   modr   r!   r!   r"   post_lowering   s
   
zCPUContext.post_loweringFc           
   	   C   sX   |  d}| j|j|j}t|||j}t| ||||||d}	|		  |
| d S )Nwrapper)call_helperrelease_gil)r3   r   get_function_typerestypeargtypesr   Functionllvm_func_namer   buildadd_ir_module)
r+   r   r   r   r   r   wrapper_modulefntywrapper_calleer   r!   r!   r"   create_cpython_wrapper   s   
z!CPUContext.create_cpython_wrapperc                    s>    d} j|j|j}t|||j} fdd|jD } |j}	t	|	|}
t||
|j
}t|d} jj|||j|j|jdd\}}|j|jdd6  |}| } j|||  |jt }||}|| || || W d    n1 sw   Y  || || d S )	Ncfunc_wrapperc                    s   g | ]}  |qS r!   )get_value_type).0tyr|   r!   r"   
<listcomp>   s    z3CPUContext.create_cfunc_wrapper.<locals>.<listcomp>entry)noinline)attrsF)likely)r3   r   r   r   r   r   r   r   r   FunctionTypellvm_cfunc_wrapper_name	IRBuilderappend_basic_blockcall_functionargsif_thenis_errorr   
gil_ensureraise_errorinsert_const_stringr   reprstring_from_stringerr_write_unraisabledecrefgil_releaseretr   )r+   r   r   r   r   r   r   r   ll_argtypesll_return_typewraptywrapfnr   statusoutr   	gil_statecstrstrobjr!   r|   r"   create_cfunc_wrapper   s0   






zCPUContext.create_cfunc_wrapperc                 C   sb   | |j}| |j}d|jf }t| |jdd ||||f}|j	| 
|| |S )z
        Returns
        -------
        (cfunc, fnptr)

        - cfunc
            callable function (Can be None)
        - fnptr
            callable function address
        - env
            an execution environment (from _dynfunc)
        zcompiled wrapper for %r.)get_pointer_to_functionr   llvm_cpython_wrapper_namequalnamer   make_functionlookup_modulesplitr   set_envr   )r+   r   r   r   baseptrfnptrdoccfuncr!   r!   r"   get_executable   s   
zCPUContext.get_executablec                 C   s    t t j|d}| | |S )zI
        Calculate the size of an array struct on the CPU target
        A)r   Arrayint32get_abi_sizeofr   )r+   ndimarytyr!   r!   r"   calc_array_sizeof   s   zCPUContext.calc_array_sizeofc                 C   s
   t |S r(   )r   get_ufunc_info)r+   	ufunc_keyr!   r!   r"   r      s   
zCPUContext.get_ufunc_info)r'   )F)r   r   r   __doc__allow_dynamic_globalsr*   r3   r   rE   rz   propertyr{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r!   r!   r.   r"   r&   !   s6    


		
r&   nopythonforceobjlooplift_nrtdebugboundschecknogilno_rewritesno_cpython_wrapperno_cfunc_wrapperparallelr   error_modelinlineforceinlinetarget_backend_dbg_extend_lifetimes_dbg_optnonec                   @   s   e Zd Zdd ZdS )CPUTargetOptionsc                 C   s   | dsd|_| dsd|_|jddd | ds tj|_| ds0|jr,d|_ntj|_| ds9|j|_	d|_
|d	 |jd
dd |d |d |jrZd|_d S d S )Nenable_pyobjectTenable_loopliftnrt)default	debuginfodbg_extend_lifetimesr  r   r  pythonr  r  F)is_setr  r  inherit_if_not_setr	   DEBUGINFO_DEFAULTr  r  EXTEND_VARIABLE_LIFETIMESr  enable_pyobject_loopliftr  optnone)r+   flagsoptionsr!   r!   r"   finalize  s*   








zCPUTargetOptions.finalizeN)r   r   r   r!  r!   r!   r!   r"   r    s    r  c                 C   s    | j D ]
}t| t| qdS )zD
    Remove redundant incref/decref within on a per block basis
    N)basic_blocksremove_null_refct_callremove_refct_pairs)funcbbr!   r!   r"   remove_refct_callsE  s   

r'  c                 C   s   dS )z0
    Remove refct api calls to NULL pointer
    Nr!   )r&  r!   r!   r"   r#  N  s   r#  c                 C   s   d}|rRd}i }i }| j D ]'}t|tjr4|jj}|dkr'|jd }|||< q|dkr4|jd }|||< q| D ]}||v rM||   ||   d}q9|sdS dS )z9
    Remove incref decref pairs on the same variable
    TF	Py_IncRefr   	Py_DecRefN)	instructions
isinstancer   	CallInstrcalled_functionr2   operandskeyserase_from_parent)r&  didsomethingincrefsdecrefsinstfnameargvalr!   r!   r"   r$  ^  s,   



r$  )1sysr;   llvmlite.bindingbindingr=   llvmliter   ru   r   numba.core.callwrapperr   numba.core.baser   r   rk   r   r   r	   r
   r   r   r   r   r   numba.core.utilsr   numba.core.optionsr   r   numba.core.runtimer   numba.core.compiler_lockr   numba.core.entrypointsnumba.core.cpu_optionsr   r   r   rm   r   	Structurer   r#   r&   _options_mixinr  r'  r#  r$  r!   r!   r!   r"   <module>   sV    , d+	