U
    iâËd)  ã                   @   s‚   d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZmZ d	d
„ ZG dd„ deeƒZdS )z/
Tests for practical lowering specific errors.
é    N)Únjit)ÚtypesÚir)ÚCompilerBaseÚDefaultPassBuilder)ÚNopythonTypeInference)Úregister_passÚFunctionPass)ÚMemoryLeakMixinÚTestCasec                 C   s   dS )zDummy function used in testN© ©Úar   r   úc/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/tests/test_practical_lowering_issues.pyÚissue7507_lround   s    r   c                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚTestLoweringc                 C   s.   t dd„ ƒ}|dƒ}| d¡}|  ||¡ dS )a–  Test issues with zero-filling of refct'ed variables inside loops.

        Before the fix, the in-loop variables are always zero-filled at their
        definition location. As a result, their state from the previous
        iteration is erased. No decref is applied. To fix this, the
        zero-filling must only happen once after the alloca at the function
        entry block. The loop variables are technically defined once per
        function (one alloca per definition per function), but semantically
        defined once per assignment. Semantically, their lifetime stop only
        when the variable is re-assigned or when the function ends.
        c                 S   sB   t  d¡}t| ƒD ]*}|dkr(t  d¡}|dkr||d 7 }q|S ©Né   r   é   ©ÚnpÚzerosÚrangeZones)ÚNÚsum_vecÚnÚvecr   r   r   Úudt!   s    

z7TestLowering.test_issue4156_loop_vars_leak.<locals>.udté   N©r   Úpy_funcZassertPreciseEqual©Úselfr   ÚgotÚexpectr   r   r   Útest_issue4156_loop_vars_leak   s
    

z*TestLowering.test_issue4156_loop_vars_leakc                 C   s.   t dd„ ƒ}|dƒ}| d¡}|  ||¡ dS )zQVariant of test_issue4156_loop_vars_leak.

        Adding an outer loop.
        c                 S   sX   t  d¡}t| ƒD ]@}t| ƒD ]2}|| }|dkr<t  d¡}|dkr||d 7 }qq|S r   r   )r   r   ÚxÚyr   r   r   r   r   r   6   s    

z@TestLowering.test_issue4156_loop_vars_leak_variant1.<locals>.udtr   Nr   r!   r   r   r   Ú&test_issue4156_loop_vars_leak_variant11   s
    

z3TestLowering.test_issue4156_loop_vars_leak_variant1c                 C   s.   t dd„ ƒ}|dƒ}| d¡}|  ||¡ dS )zUVariant of test_issue4156_loop_vars_leak.

        Adding deeper outer loop.
        c                 S   sj   t  d¡}t| ƒD ]R}t| ƒD ]D}t| ƒD ]6}|| | }|dkrLt  d¡}|dkr*||d 7 }q*qq|S r   r   )r   r   Úzr&   r'   r   r   r   r   r   r   M   s    

z@TestLowering.test_issue4156_loop_vars_leak_variant2.<locals>.udtr   Nr   r!   r   r   r   Ú&test_issue4156_loop_vars_leak_variant2H   s
    

z3TestLowering.test_issue4156_loop_vars_leak_variant2c                 C   s.   t dd„ ƒ}|dƒ}| d¡}|  ||¡ dS )z_Variant of test_issue4156_loop_vars_leak.

        Adding inner loop around allocation
        c                 S   sf   t  d¡}t| ƒD ]N}t| ƒD ]@}|| }|dkrJt| ƒD ]}t  |¡}q:|dkr||d 7 }qq|S )Nr   r   r   )r   r   r)   r&   r   r'   r   r   r   r   r   e   s    
z@TestLowering.test_issue4156_loop_vars_leak_variant3.<locals>.udtr   Nr   r!   r   r   r   Ú&test_issue4156_loop_vars_leak_variant3`   s
    

z3TestLowering.test_issue4156_loop_vars_leak_variant3c                 C   s.   t dd„ ƒ}|dƒ}| d¡}|  ||¡ dS )z]Variant of test_issue4156_loop_vars_leak.

        Interleaves loops and allocations
        c                 S   sL   d}t | ƒD ]:}t d¡}t | ƒD ]}t d¡}q"||d |d  7 }q|S )Nr   é   )r   r   r   )r   r   r   r   r)   r   r   r   r   }   s    
z@TestLowering.test_issue4156_loop_vars_leak_variant4.<locals>.udtr   Nr   r!   r   r   r   Ú&test_issue4156_loop_vars_leak_variant4x   s
    

z3TestLowering.test_issue4156_loop_vars_leak_variant4c                    sn   t ddd�G dd„ dtƒƒ‰ G ‡ fdd„dtƒ}t|d�dd	„ ƒ}t d
¡}||ƒ}| |¡}|  ||¡ dS )zHTest an issue with literal type used as index of static_getitem
        F)Zmutates_CFGZanalysis_onlyc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )zYTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteralZforce_static_getitem_literalc                 S   s   t  | ¡ d S ©N)r	   Ú__init__)r"   r   r   r   r/   –   s    zbTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteral.__init__c                 S   sh   i }|j  ¡ D ]H\}}t|tjƒr|jdkr|j\}}|j|t 	|j
¡fd�}|||< q|j  |¡ dS )NZstatic_getitem)ÚargsT)Z	calltypesÚitemsÚ
isinstancer   ÚExprÚopr0   Úreplacer   ÚliteralÚindexÚupdate)r"   ÚstateÚreplÚinstÚsigÚobjÚidxZnew_sigr   r   r   Úrun_pass™   s    ÿ

ÿ
zbTestLowering.test_issue_with_literal_in_static_getitem.<locals>.ForceStaticGetitemLiteral.run_passN)Ú__name__Ú
__module__Ú__qualname__Ú_namer/   r?   r   r   r   r   ÚForceStaticGetitemLiteral‘   s   rD   c                       s   e Zd Z‡ fdd„ZdS )zNTestLowering.test_issue_with_literal_in_static_getitem.<locals>.CustomPipelinec                    s&   t  | j¡}| ˆ t¡ | ¡  |gS r.   )r   Zdefine_nopython_pipeliner9   Zadd_pass_afterr   Úfinalize)r"   Úpm©rD   r   r   Údefine_pipelines¨   s    ÿz_TestLowering.test_issue_with_literal_in_static_getitem.<locals>.CustomPipeline.define_pipelinesN)r@   rA   rB   rH   r   rG   r   r   ÚCustomPipeline§   s   rI   )Zpipeline_classc                 S   s   | d S )Nr   r   )Úarrr   r   r   Úfoo¯   s    zCTestLowering.test_issue_with_literal_in_static_getitem.<locals>.fooé
   N)r   r	   r   r   r   Zaranger    ÚassertEqual)r"   rI   rK   rJ   r#   r$   r   rG   r   Ú)test_issue_with_literal_in_static_getitem�   s    



z6TestLowering.test_issue_with_literal_in_static_getitemc                    s\   ddl m}m} ddlm‰  |tƒG ‡ fdd„d|ƒƒ}tdƒdd„ ƒ}|  |d	ƒd
¡ dS )z„
        Test a problem with BaseContext.get_function() because of changes
        related to the new style error handling.
        r   )ÚAbstractTemplateÚinfer_global©Úlower_builtinc                       s   e Zd ZeZ‡ fdd„ZdS )z3TestLowering.test_issue7507.<locals>.lroundTemplatec                    s$   t  t j¡}ˆ tt jƒdd„ ƒ}|S )Nc                 S   s   |   ||d |jd |j¡S )Nr   )Úcastr0   Úreturn_type)ÚcontextZbuilderr<   r0   r   r   r   ÚcodegenÈ   s    ÿzLTestLowering.test_issue7507.<locals>.lroundTemplate.generic.<locals>.codegen)r   Zint64Zfloat64r   )r"   r0   ÚkwsÚ	signaturerV   rQ   r   r   ÚgenericÄ   s    

z;TestLowering.test_issue7507.<locals>.lroundTemplate.genericN)r@   rA   rB   r   ÚkeyrY   r   rQ   r   r   ÚlroundTemplateÀ   s   r[   zint64(float64)c                 S   s   t | ƒS r.   )r   r   r   r   r   rK   Ð   s    z(TestLowering.test_issue7507.<locals>.foog333333@r   N)Znumba.core.typing.templatesrO   rP   Znumba.core.imputilsrR   r   r   rM   )r"   rO   rP   r[   rK   r   rQ   r   Útest_issue7507¸   s    
zTestLowering.test_issue7507N)
r@   rA   rB   r%   r(   r*   r+   r-   rN   r\   r   r   r   r   r      s   +r   )Ú__doc__Únumpyr   Znumbar   Z
numba.corer   r   Znumba.core.compilerr   r   Znumba.core.typed_passesr   Znumba.core.compiler_machineryr   r	   Znumba.tests.supportr
   r   r   r   r   r   r   r   Ú<module>   s   