
    k=1e3                         d Z 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Zddlm	Z	 ddl
mZ d Z G d d          Zd	 Zd
 Zd Z ee          Z G d de          ZdS )zETypes and functions to manage information
on CPU microarchitectures.
    N   )FEATURE_ALIASES)LazyDictionaryc                 F     t          j                    fd            }|S )zhDecorator that automatically converts a known target name to a proper
    Microarchitecture object.
    c                     t          |t                    r:|t          vr$d}t          |                    |                    t          |         } | |          S )Nz "{0}" is not a valid target name)
isinstancestrTARGETS
ValueErrorformat)selfothermsgfuncs      >lib/python3.11/site-packages/archspec/cpu/microarchitecture.py_implz"coerce_target_names.<locals>._impl   sZ    eS!! 	#G##8 E!2!2333ENEtD%       )	functoolswraps)r   r   s   ` r   coerce_target_namesr      s:    
 _T! ! ! ! ! Lr   c                   
   e Zd ZdZeZddZed             Zd Z	e
d             Ze
d             Ze
d             Ze
d	             Ze
d
             Ze
d             Zd Zd Zd Zed             Zed             ZddZd ZdS )Microarchitecturean  Represents a specific CPU micro-architecture.

    Args:
        name (str): name of the micro-architecture (e.g. skylake).
        parents (list): list of parents micro-architectures, if any.
            Parenthood is considered by cpu features and not
            chronologically. As such each micro-architecture is
            compatible with its ancestors. For example "skylake",
            which has "broadwell" as a parent, supports running binaries
            optimized for "broadwell".
        vendor (str): vendor of the micro-architecture
        features (list of str): supported CPU flags. Note that the semantic
            of the flags in this field might vary among architectures, if
            at all present. For instance x86_64 processors will list all
            the flags supported by a given CPU while Arm processors will
            list instead only the flags that have been added on top of the
            base model for the current micro-architecture.
        compilers (dict): compiler support to generate tuned code for this
            micro-architecture. This dictionary has as keys names of
            supported compilers, while values are list of dictionaries
            with fields:

            * name: name of the micro-architecture according to the
                compiler. This is the name passed to the ``-march`` option
                or similar. Not needed if the name is the same as that
                passed in as argument above.
            * versions: versions that support this micro-architecture.

        generation (int): generation of the micro-architecture, if
            relevant.
    r   c                 Z    || _         || _        || _        || _        || _        || _        d S N)nameparentsvendorfeatures	compilers
generation)r   r   r   r   r   r   r    s          r   __init__zMicroarchitecture.__init__K   s0    	 "$r   c                     | j         dd         | j         D ](}                    fd|j        D                        )S )z,All the ancestors of this microarchitecture.Nc              3   $   K   | ]
}|v|V  d S r    ).0avalues     r   	<genexpr>z.Microarchitecture.ancestors.<locals>.<genexpr>X   s'      GGqGGr   )r   extend	ancestors)r   parentr'   s     @r   r*   zMicroarchitecture.ancestorsS   sY     QQQl 	H 	HFLLGGGGF$4GGGGGGGr   c                 `    t          t          |           gd | j        D             z             S )z9Returns a set of the nodes in this microarchitecture DAG.c                 ,    g | ]}t          |          S r$   r	   r%   xs     r   
<listcomp>z-Microarchitecture._to_set.<locals>.<listcomp>_   s    !A!A!AQ#a&&!A!A!Ar   )setr	   r*   r   s    r   _to_setzMicroarchitecture._to_set[   s0     CII;!A!A$.!A!A!AABBBr   c                     t          |t                    st          S | j        |j        k    oO| j        |j        k    o?| j        |j        k    o/| j        |j        k    o| j        |j        k    o| j        |j        k    S r   )	r   r   NotImplementedr   r   r   r   r   r    r   r   s     r   __eq__zMicroarchitecture.__eq__a   s    %!233 	"!! I# 4u|+4/4 -4 %/1	4
 5#33	
r   c                     | |k     S r   r$   r7   s     r   __ne__zMicroarchitecture.__ne__o   s    5=  r   c                     t          |t                    st          S |                                 |                                k     S r   r   r   r6   r4   r7   s     r   __lt__zMicroarchitecture.__lt__s   6    %!233 	"!!||~~//r   c                     | |k    p| |k     S r   r$   r7   s     r   __le__zMicroarchitecture.__le__z       04%<0r   c                     t          |t                    st          S |                                 |                                k    S r   r<   r7   s     r   __gt__zMicroarchitecture.__gt__~   r>   r   c                     | |k    p| |k    S r   r$   r7   s     r   __ge__zMicroarchitecture.__ge__   rA   r   c                 N    | j         j        }|dz   }|                    |           S )Nz\({0.name!r}, {0.parents!r}, {0.vendor!r}, {0.features!r}, {0.compilers!r}, {0.generation!r}))	__class____name__r   )r   cls_namefmts      r   __repr__zMicroarchitecture.__repr__   s4    >* A A 	 zz$r   c                     | j         S r   r   r3   s    r   __str__zMicroarchitecture.__str__   s
    yr   c           	         t          |t                    s>d}t          |                    t          t	          |                                        || j        v rdS t          j                            |d           } ||           S )Nz3only objects of string types are accepted [got {0}]Tc                     dS )NFr$   r0   s    r   <lambda>z0Microarchitecture.__contains__.<locals>.<lambda>   s    u r   )	r   r	   	TypeErrorr   typer   r   feature_aliasesget)r   featurer   match_aliass       r   __contains__zMicroarchitecture.__contains__   s    '3'' 	<GCCJJs4=='9'9::;;; dm##4 (7;;G__UU{4   r   c                     d | g| j         z   D             }d}|dd                    d |D                        dz  }t          |          dk    s
J |            |                                S )z9Returns the architecture family a given target belongs toc                      g | ]}|j         	|S r$   )r*   r/   s     r   r1   z,Microarchitecture.family.<locals>.<listcomp>   s    GGGq1;GGGGr   z>a target is expected to belong to just one architecture familyz[found , c              3   4   K   | ]}t          |          V  d S r   r.   r/   s     r   r(   z+Microarchitecture.family.<locals>.<genexpr>   s(      "9"9a3q66"9"9"9"9"9"9r   ]r   )r*   joinlenpop)r   rootsr   s      r   familyzMicroarchitecture.family   s~     HGTFT^3GGGN<"9"95"9"9"999<<<<5zzQyy{{r   c                 P    d | g| j         z   D             }t          |d           S )zBReturns the best generic architecture that is compatible with selfc                 (    g | ]}|j         d k    |S )generic)r   r/   s     r   r1   z-Microarchitecture.generic.<locals>.<listcomp>   s$    PPP!!(i:O:OA:O:O:Or   c                 *    t          | j                  S r   )r`   r*   rQ   s    r   rR   z+Microarchitecture.generic.<locals>.<lambda>   s    3q{+;+; r   )key)r*   max)r   genericss     r   rf   zMicroarchitecture.generic   s8     QPv6PPP8!;!;<<<<r   Fc                     dt          | j                  fdt          | j                  fdt          d | j        D                       fd| j        fdd | j        D             fg}|r|S t          |          S )zReturns a dictionary representation of this object.

        Args:
            return_list_of_items (bool): if True returns an ordered list of
                items instead of the dictionary
        r   r   r   c              3   4   K   | ]}t          |          V  d S r   r.   r/   s     r   r(   z,Microarchitecture.to_dict.<locals>.<genexpr>   s(      >>1A>>>>>>r   r    r   c                 ,    g | ]}t          |          S r$   r.   r/   s     r   r1   z-Microarchitecture.to_dict.<locals>.<listcomp>   s    666AQ666r   )r	   r   r   sortedr   r    r   dict)r   return_list_of_itemslist_of_itemss      r   to_dictzMicroarchitecture.to_dict   s     S^^$s4;''(>>>>>>>?4?+666667
   	!  M"""r   c                 ~   | j         j        vrdS | j        vrFfd| j        D             d         }d}|                    ||j                   }t	          |          | j                 }d }|D ]n} |||          r`|d         }|                    d| j                   |                    dd	          }	|	rt          j	        |	            |j        di |}
|
c S od
}|r)d |D             }|dd
                    |           dz  }n|dz  }|                    | j        |          }t	          |          )a  Returns a string containing the optimization flags that needs
        to be used to produce code optimized for this micro-architecture.

        If there is no information on the compiler passed as argument the
        function returns an empty string. If it is known that the compiler
        version we want to use does not support this architecture the function
        raises an exception.

        Args:
            compiler (str): name of the compiler to be used
            version (str): version of the compiler to be used
         c                 &    g | ]}|j         v |S r$   )r   )r%   r0   compilers     r   r1   z8Microarchitecture.optimization_flags.<locals>.<listcomp>   s%    PPPAK8O8O18O8O8Or   r   zf'{0}' compiler is known to optimize up to the '{1}' microarchitecture in the '{2}' architecture familyc                    | d                              d          \  }}t          |          \  }}t          |          \  }}t          |          \  }}d } ||          }|r ||          }||k    rdS |r ||          }||k     rdS dS )Nversions:c                 Z    t          d |                     d          D                       S )Nc              3   4   K   | ]}t          |          V  d S r   )int)r%   ys     r   r(   zfMicroarchitecture.optimization_flags.<locals>.satisfies_constraint.<locals>.tuplify.<locals>.<genexpr>   s(      <<SVV<<<<<<r   .)tuplesplit)vers    r   tuplifyzSMicroarchitecture.optimization_flags.<locals>.satisfies_constraint.<locals>.tuplify   s)    <<SYYs^^<<<<<<r   FT)r   version_components)entryversionmin_versionmax_version_r   s         r   satisfies_constraintzBMicroarchitecture.optimization_flags.<locals>.satisfies_constraint   s    ',Z'8'>'>s'C'C$K 0<<NK/<<NK+G44JGQ= = = gg&&G !%gk22(( 5 !%gk22(( 54r   flagsr   warningsNzIcannot produce optimized binary for micro-architecture '{0}' with {1}@{2}c                     g | ]
}|d          S )rx   r$   r/   s     r   r1   z8Microarchitecture.optimization_flags.<locals>.<listcomp>  s    ===!*===r   z" [supported compiler versions are r\   r^   z! [no supported compiler versions]r$   )rc   r   r*   r   UnsupportedMicroarchitecture
setdefaultr   rV   r   warnr_   )r   rv   r   best_targetr   compiler_infor   compiler_entry	flags_fmtwarning_messager   rx   s    `          r   optimization_flagsz$Microarchitecture.optimization_flags   s    4;0002 4>))PPPPdnPPPQRSKF  **X{K4FGGC.s333 x0	 	 	2 , 	 	N##NG<< *73	 ))&$)<<< #1"4"4Z"F"F" 3M/222(	(::>:: X 	  	7==}===HN		(8K8KNNNNCC66CjjHg66*3///r   N)r   )F)rH   
__module____qualname____doc__r   rU   r!   propertyr*   r4   r   r8   r:   r=   r@   rC   rE   rK   rN   rY   rc   rf   rr   r   r$   r   r   r   r   &   s        D &O% % % %   XC C C 
 
 
 ! ! ! 0 0 0 1 1 1 0 0 0 1 1 1       ! ! !   X = = X=
# # # #&Q0 Q0 Q0 Q0 Q0r   r   c                 *    t          | g dg i           S )zReturns a generic micro-architecture with no vendor and no features.

    Args:
        name (str): name of the micro-architecture
    rf   )r   r   r   r   )r   rM   s    r   generic_microarchitecturer     s&     bR2   r   c                     t          j        dt          |                     }|sdS |                    d          }|                    d          }||fS )a  Decomposes the version passed as input in version number and
    suffix and returns them.

    If the version number or the suffix are not present, an empty
    string is returned.

    Args:
        version (str): version to be decomposed into its components
    z([\d.]*)(-?)(.*))rt   rt   r      )rematchr	   group)r   r   version_numbersuffixs       r   r   r   %  sR     H(#g,,77E v[[^^N[[^^F6!!r   c                      fdi } t           j        j        j        d         }|D ]}|| v r |||            t	          j                    }|                     |t          |                     | S )zReturns a dictionary of the known micro-architectures. If the
    current host platform is unknown adds it too as a generic target.
    c                 4   ||          }|d         }|D ]}|v r ||           fd|D             }|d         }t          |d                   }|                    di           }	|                    dd          }
t          | ||||	|
          | <   dS )	av  Recursively fills targets by adding the micro-architecture
        passed as argument and all its ancestors.

        Args:
            name (str): micro-architecture to be added to targets.
            data (dict): raw data loaded from JSON.
            targets (dict): dictionary that maps micro-architecture names
                to ``Microarchitecture`` objects
        fromc                 :    g | ]}                     |          S r$   )rV   )r%   r+   targetss     r   r1   zL_known_microarchitectures.<locals>.fill_target_from_dict.<locals>.<listcomp>Q  s%    BBB67;;v&&BBBr   r   r   r   r    r   N)r2   rV   r   )r   datar   valuesparent_namesr+   r   r   r   r   r    fill_target_from_dicts     `        r   r   z8_known_microarchitectures.<locals>.fill_target_from_dict>  s     d f~" 	9 	9F  !!&$8888BBBB\BBB!vj)**JJ{B//	ZZa00
)'68Y

 
r   microarchitectures)archspeccpuschemaTARGETS_JSONplatformmachiner   r   )known_targetsr   r   host_platformr   s       @r   _known_microarchitecturesr   9  s    

 
 
 
 
< M<+,@AD 9 9=  dD-8888 $&&M],Em,T,TUUUr   c                       e Zd ZdZdS )r   zcRaised if a compiler version does not support optimization for a given
    micro-architecture.
    N)rH   r   r   r   r$   r   r   r   r   o  s           r   r   )r   r   r   r   r   r   archspec.cpu.aliasarchspec.cpu.schemaaliasr   r   r   r   r   r   r   r   r
   r   r   r$   r   r   <module>r      s1  
       				           " " " " " " " " " " " "  $q0 q0 q0 q0 q0 q0 q0 q0h  " " "(/ / /f .2
3
3    :     r   