δm d Z ddlZddlZddlZddlZddlZddlZddlZ G d d Z G d d Z i Z G d d Z G d d Z G d d Z G d d Z G d dee eee e Zd ZdS )a Provides the `CCompilerOpt` class, used for handling the CPU/hardware optimization, starting from parsing the command arguments, to managing the relation between the CPU baseline and dispatch-able features, also generating the required C headers and ending with compiling the sources with proper compiler's flags. `CCompilerOpt` doesn't provide runtime detection for the CPU features, instead only focuses on the compiler side, but it creates abstract C headers that can be used later for the final runtime dispatching process. Nc e Zd ZdZdZdZdZdZej ej ej e d Zi ZdZdZ e eddd eddd eddd ed dd eddd eddd Z eddddddd Z edzi d eddd d eddd ! d" ed#dd$! d% ed&d"d'! d( ed)d%d*! d+ ed,d(d-! d. ed/d+0 d1 ed2d.d3d4 d5 ed6d1d7! d8 ed9d1d7! d: ed;d10 d< ed=d:0 d> ed?d:0 d@ edAdBddCD dE edFd@0 dG edHdEdIdGdJ dK edLdGdMdKdJ dN edOdEdPdNddQR dS edTdNdUdSV dW edXdNdYdWdJ dZ ed[d\d]dZdJ d^ ed_dZd`d^dJ da eddbdcd de eddadf dg ed#dedf dh ed&dgddiD dj eddkl dm eddjdf dn ed#dmdf do eddpl dq eddo0 dr ed#dq0 ds ed&dtdf du ed)ds0 dv ed,ds0 dw ed/du0 Zdx Zdy ZdS ){_Configa An abstract class holds all configurable attributes of `CCompilerOpt`, these class attributes can be used to change the default behavior of `CCompilerOpt` in order to fit other requirements. Attributes ---------- conf_nocache : bool Set True to disable memory and file cache. Default is False. conf_noopt : bool Set True to forces the optimization to be disabled, in this case `CCompilerOpt` tends to generate all expected headers in order to 'not' break the build. Default is False. conf_cache_factors : list Add extra factors to the primary caching factors. The caching factors are utilized to determine if there are changes had happened that requires to discard the cache and re-updating it. The primary factors are the arguments of `CCompilerOpt` and `CCompiler`'s properties(type, flags, etc). Default is list of two items, containing the time of last modification of `ccompiler_opt` and value of attribute "conf_noopt" conf_tmp_path : str, The path of temporary directory. Default is auto-created temporary directory via ``tempfile.mkdtemp()``. conf_check_path : str The path of testing files. Each added CPU feature must have a **C** source file contains at least one intrinsic or instruction that related to this feature, so it can be tested against the compiler. Default is ``./distutils/checks``. conf_target_groups : dict Extra tokens that can be reached from dispatch-able sources through the special mark ``@targets``. Default is an empty dictionary. **Notes**: - case-insensitive for tokens and group names - sign '#' must stick in the begin of group name and only within ``@targets`` **Example**: .. code-block:: console $ "@targets #avx_group other_tokens" > group_inside.c >>> CCompilerOpt.conf_target_groups["avx_group"] = \ "$werror $maxopt avx2 avx512f avx512_skx" >>> cco = CCompilerOpt(cc_instance) >>> cco.try_dispatch(["group_inside.c"]) conf_c_prefix : str The prefix of public C definitions. Default is ``"NPY_"``. conf_c_prefix_ : str The prefix of internal C definitions. Default is ``"NPY__"``. conf_cc_flags : dict Nested dictionaries defining several compiler flags that linked to some major functions, the main key represent the compiler name and sub-keys represent flags names. Default is already covers all supported **C** compilers. Sub-keys explained as follows: "native": str or None used by argument option `native`, to detect the current machine support via the compiler. "werror": str or None utilized to treat warning as errors during testing CPU features against the compiler and also for target's policy `$werror` via dispatch-able sources. "maxopt": str or None utilized for target's policy '$maxopt' and the value should contains the maximum acceptable optimization by the compiler. e.g. in gcc `'-O3'` **Notes**: * case-sensitive for compiler names and flags * use space to separate multiple flags * any flag will tested against the compiler and it will skipped if it's not applicable. conf_min_features : dict A dictionary defines the used CPU features for argument option `'min'`, the key represent the CPU architecture name e.g. `'x86'`. Default values provide the best effort on wide range of users platforms. **Note**: case-sensitive for architecture names. conf_features : dict Nested dictionaries used for identifying the CPU features. the primary key is represented as a feature name or group name that gathers several features. Default values covers all supported features but without the major options like "flags", these undefined options handle it by method `conf_features_partial()`. Default value is covers almost all CPU features for *X86*, *IBM/Power64* and *ARM 7/8*. Sub-keys explained as follows: "implies" : str or list, optional, List of CPU feature names to be implied by it, the feature name must be defined within `conf_features`. Default is None. "flags": str or list, optional List of compiler flags. Default is None. "detect": str or list, optional List of CPU feature names that required to be detected in runtime. By default, its the feature name or features in "group" if its specified. "implies_detect": bool, optional If True, all "detect" of implied features will be combined. Default is True. see `feature_detect()`. "group": str or list, optional Same as "implies" but doesn't require the feature name to be defined within `conf_features`. "interest": int, required a key for sorting CPU features "headers": str or list, optional intrinsics C header file "disable": str, optional force disable feature, the string value should contains the reason of disabling. "autovec": bool or None, optional True or False to declare that CPU feature can be auto-vectorized by the compiler. By default(None), treated as True if the feature contains at least one applicable flag. see `feature_can_autovec()` "extra_checks": str or list, optional Extra test case names for the CPU feature that need to be tested against the compiler. Each test case must have a C file named ``extra_xxxx.c``, where ``xxxx`` is the case name in lower case, under 'conf_check_path'. It should contain at least one intrinsic or function related to the test case. If the compiler able to successfully compile the C file then `CCompilerOpt` will add a C ``#define`` for it into the main dispatch header, e.g. ``#define {conf_c_prefix}_XXXX`` where ``XXXX`` is the case name in upper case. **NOTES**: * space can be used as separator with options that supports "str or list" * case-sensitive for all values and feature name must be in upper-case. * if flags aren't applicable, its will skipped rather than disable the CPU feature * the CPU feature will disabled if the compiler fail to compile the test file FNchecksNPY_NPY__z -march=nativez-O3z-Werror)nativeoptwerrorz-Werror=switch -Werrorz-xHostz/QxHostz/O3z/Werrorz/O2z/WXz-mcpu=a64fx)gccclangicciccwmsvcfcczSSE SSE2z SSE SSE2 SSE3 zVSX VSX2zNEON NEON_FP16 NEON_VFPV4 ASIMD)x86x64ppc64ppc64les390xarmhfaarch64SSE zxmmintrin.hSSE2)interestheadersimplies zemmintrin.h)r r r SSE3 zpmmintrin.hSSSE3 ztmmintrin.hSSE41 zsmmintrin.hPOPCNT zpopcntintrin.hSSE42 )r r AVX zimmintrin.h)r r r implies_detectXOP zx86intrin.hFMA4 F16C FMA3 AVX2 AVX512F z FMA3 AVX2AVX512F_REDUCE)r r r, extra_checksAVX512CD AVX512_KNL( zAVX512ER AVX512PF)r r groupdetectr, AVX512_KNM) z)AVX5124FMAPS AVX5124VNNIW AVX512VPOPCNTDQ AVX512_SKX* zAVX512VL AVX512BW AVX512DQzAVX512BW_MASK AVX512DQ_MASK)r r r? r@ r, r: AVX512_CLX+ AVX512VNNI)r r r? r@ AVX512_CNL, zAVX512IFMA AVX512VBMI AVX512_ICL- zAVX512_CLX AVX512_CNLz(AVX512VBMI2 AVX512BITALG AVX512VPOPCNTDQ AVX512_SPR. AVX512FP16VSXz altivec.hVSX_ASM)r r r: VSX2)r r r, VSX3VSX4VSX4_MMAVXzvecintrin.h)r r VXEVXE2NEONz arm_neon.h NEON_FP16 NEON_VFPV4ASIMDzNEON_FP16 NEON_VFPV4ASIMDHPASIMDDPASIMDFHMc p | j ri S | j p| j }| j p | j p| j }|r|rt dwi dt d dt d dt d dt d d t d dt d dt d dt d dt d dt d dt d dt d dt d dt d dt d d t d! d"t d# d$t d% d&t d' d(t d) d*t d+ d,t d- S |rp| j rht dwi dt d dt d dt d dt d d t d di dt d dt d di dt d./ dt d./ dt d0d12 dt dd12 dt d3d42 dt d5d42 d t d6 d"t d7 d$t d8 d&t d9 d(t d: d*t d; d,t d S |rp| j rht dwi dt d= dt d> dt d? dt d@ d t dA di dt dB dt dC di dt d./ dt d./ dt d0dD2 dt ddD2 dt d3dE2 dt d5dE2 d t dF d"t dG d$t dH d&t dI d(t dJ d*t dK d,t d S |r.| j r&t dwi d| j rt d= ni d| j rt d> ni di di d i dt dLM di dt dC di dt dNM dt dNM dt d0dO2 dt dPdO2 dt dQdR2 dt dSdR2 d t dT/ d"t dT/ d$t dR d&i