[v5] build: reduce use of AVX compiler flags

Message ID 20250611105310.472523-1-bruce.richardson@intel.com (mailing list archive)
State Accepted
Delegated to: Thomas Monjalon
Headers
Series [v5] build: reduce use of AVX compiler flags |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation success Compilation OK
ci/github-robot: build success github build: passed
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/loongarch-compilation success Compilation OK RETEST #1
ci/loongarch-unit-testing success Unit Testing PASS RETEST #1
ci/iol-marvell-Functional success Functional Testing PASS RETEST #1
ci/iol-mellanox-Performance success Performance Testing PASS RETEST #1
ci/iol-broadcom-Performance success Performance Testing PASS RETEST #1
ci/iol-intel-Functional success Functional Testing PASS RETEST #1
ci/iol-unit-amd64-testing pending Testing pending RETEST #1
ci/iol-unit-arm64-testing success Testing PASS RETEST #1
ci/iol-abi-testing success Testing PASS RETEST #1
ci/iol-compile-amd64-testing warning Testing issues RETEST #1
ci/iol-sample-apps-testing success Testing PASS RETEST #1
ci/iol-compile-arm64-testing success Testing PASS RETEST #1
ci/aws-unit-testing success Unit Testing PASS

Commit Message

Bruce Richardson June 11, 2025, 10:53 a.m. UTC
When doing a build for a target that already has the instruction sets
for AVX2/AVX512 enabled, skip emitting the AVX compiler flags, or the
x86-64-v4 '-march' flags, as they are unnecessary. Instead, when
the default flags produce the desired output, just use them unmodified,
and don't bother adding in extra enabling flags for AVX2 or AVX-512.

Depends-on: series-35421 ("doc/linux_gsg: update recommended compiler versions")

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
v5: Use "x86-64-v4" arch, when available, in place of "skylake-avx512"

V4: Fix error flagged by CI with clang builds without AVX512 - change
    "cc_avx512_args" to correct "cc_avx512_flags"

V3: put in version check to work around an issues with some meson
    versions, (hopefully) allowing builds to pass in all CIs. The
    printout of the extra flags now only happens with meson >= 0.60.2

V2: dropped the doc update for the minimum compiler version.  Based on
    discussion, that version bump is larger than proposed in RFC and is
    now a separate patch/series [series referenced above]
---
 config/x86/meson.build | 34 +++++++++++++++++++++++-----------
 drivers/meson.build    |  9 +--------
 lib/meson.build        |  9 +--------
 3 files changed, 25 insertions(+), 27 deletions(-)
  

Comments

Varghese, Vipin June 12, 2025, 9:55 a.m. UTC | #1
[Public]

> -----Original Message-----
> From: Bruce Richardson <bruce.richardson@intel.com>
> Sent: Wednesday, June 11, 2025 4:23 PM
> To: dev@dpdk.org
> Cc: Varghese, Vipin <Vipin.Varghese@amd.com>; Bruce Richardson
> <bruce.richardson@intel.com>
> Subject: [PATCH v5] build: reduce use of AVX compiler flags
>
> Caution: This message originated from an External Source. Use proper caution
> when opening attachments, clicking links, or responding.
>
>
> When doing a build for a target that already has the instruction sets for
> AVX2/AVX512 enabled, skip emitting the AVX compiler flags, or the
> x86-64-v4 '-march' flags, as they are unnecessary. Instead, when the default flags
> produce the desired output, just use them unmodified, and don't bother adding in
> extra enabling flags for AVX2 or AVX-512.
>
> Depends-on: series-35421 ("doc/linux_gsg: update recommended compiler
> versions")
>
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> ---
> v5: Use "x86-64-v4" arch, when available, in place of "skylake-avx512"
>
> V4: Fix error flagged by CI with clang builds without AVX512 - change
>     "cc_avx512_args" to correct "cc_avx512_flags"
>
> V3: put in version check to work around an issues with some meson
>     versions, (hopefully) allowing builds to pass in all CIs. The
>     printout of the extra flags now only happens with meson >= 0.60.2
>
> V2: dropped the doc update for the minimum compiler version.  Based on
>     discussion, that version bump is larger than proposed in RFC and is
>     now a separate patch/series [series referenced above]
> ---
>  config/x86/meson.build | 34 +++++++++++++++++++++++-----------
>  drivers/meson.build    |  9 +--------
>  lib/meson.build        |  9 +--------
>  3 files changed, 25 insertions(+), 27 deletions(-)
>
> diff --git a/config/x86/meson.build b/config/x86/meson.build index
> c3564b0011..e2ccfb6d12 100644
> --- a/config/x86/meson.build
> +++ b/config/x86/meson.build
> @@ -4,11 +4,13 @@
>  if is_ms_compiler
>      cc_avx2_flags = ['/arch:AVX2']
>  else
> -    cc_avx2_flags = ['-mavx2']
> +    cc_avx2_flags = []
> +    if cc.get_define('__AVX2__', args: machine_args) == ''
> +        cc_avx2_flags = ['-mavx2']
> +    endif
>  endif
>
>  cc_has_avx512 = false
> -target_has_avx512 = false
>
>  dpdk_conf.set('RTE_ARCH_X86', 1)
>  if dpdk_conf.get('RTE_ARCH_64')
> @@ -65,26 +67,36 @@ if is_linux or cc.get_id() == 'gcc'
>      endif
>  endif
>
> -cc_avx512_flags = ['-mavx512f', '-mavx512vl', '-mavx512dq', '-mavx512bw', '-
> mavx512cd'] -if (binutils_ok and cc.has_multi_arguments(cc_avx512_flags)
> +avx512_march_flag = '-march=x86-64-v4'
> +if not cc.has_argument(avx512_march_flag)
> +    avx512_march_flag = '-march=skylake-avx512'
> +endif
> +cc_avx512_flags = []
> +if (binutils_ok and cc.has_argument(avx512_march_flag)
>          and '-mno-avx512f' not in get_option('c_args'))
>      # check if compiler is working with _mm512_extracti64x4_epi64
>      # Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82887
>      code = '''#include <immintrin.h>
>      void test(__m512i zmm){
>          __m256i ymm = _mm512_extracti64x4_epi64(zmm, 0);}'''
> -    result = cc.compiles(code, args : cc_avx512_flags, name : 'AVX512 checking')
> +    result = cc.compiles(code, args : [avx512_march_flag], name :
> + 'AVX512 checking')
>      if result == false
>          machine_args += '-mno-avx512f'
>          warning('Broken _mm512_extracti64x4_epi64, disabling AVX512 support')
>      else
>          cc_has_avx512 = true
> -        target_has_avx512 = (
> -                cc.get_define('__AVX512F__', args: machine_args) != '' and
> -                cc.get_define('__AVX512BW__', args: machine_args) != '' and
> -                cc.get_define('__AVX512DQ__', args: machine_args) != '' and
> -                cc.get_define('__AVX512VL__', args: machine_args) != ''
> -            )
> +        if cc.get_define('__AVX512F__', args: machine_args) == ''
> +            cc_avx512_flags = [avx512_march_flag]
> +            if cc.has_argument('-Wno-overriding-option')
> +                cc_avx512_flags += '-Wno-overriding-option'
> +            endif
> +        endif
> +    endif
> +endif
> +if developer_mode and meson.version().version_compare('>=0.60.2')
> +    message('Extra C flags needed for AVX2 output: @0@'.format(cc_avx2_flags))
> +    if cc_has_avx512
> +        message('Extra C flags needed for AVX512 output:
> +@0@'.format(cc_avx512_flags))
>      endif
>  endif
>
> diff --git a/drivers/meson.build b/drivers/meson.build index 7b7205dfac..b62880db02
> 100644
> --- a/drivers/meson.build
> +++ b/drivers/meson.build
> @@ -267,18 +267,11 @@ foreach subpath:subdirs
>              endif
>              if sources_avx512.length() > 0 and cc_has_avx512
>                  cflags += '-DCC_AVX512_SUPPORT'
> -                avx512_args = [cflags, cc_avx512_flags]
> -                if not target_has_avx512 and cc.has_argument('-march=skylake-
> avx512')
> -                    avx512_args += '-march=skylake-avx512'
> -                    if cc.has_argument('-Wno-overriding-option')
> -                        avx512_args += '-Wno-overriding-option'
> -                    endif
> -                endif
>                  avx512_lib = static_library(lib_name + '_avx512_lib',
>                          sources_avx512,
>                          dependencies: static_deps,
>                          include_directories: includes,
> -                        c_args: avx512_args)
> +                        c_args: [cflags, cc_avx512_flags])
>                  objs += avx512_lib.extract_objects(sources_avx512)
>              endif
>          endif
> diff --git a/lib/meson.build b/lib/meson.build index 1934cb4a29..0d56b2083b 100644
> --- a/lib/meson.build
> +++ b/lib/meson.build
> @@ -261,18 +261,11 @@ foreach l:libraries
>          endif
>          if sources_avx512.length() > 0 and cc_has_avx512
>              cflags += '-DCC_AVX512_SUPPORT'
> -            avx512_args = [cflags, cflags_avx512, cc_avx512_flags]
> -            if not target_has_avx512 and cc.has_argument('-march=skylake-avx512')
> -                avx512_args += '-march=skylake-avx512'
> -                if cc.has_argument('-Wno-overriding-option')
> -                    avx512_args += '-Wno-overriding-option'
> -                endif
> -            endif
>              avx512_lib = static_library(libname + '_avx512_lib',
>                      sources_avx512,
>                      dependencies: static_deps,
>                      include_directories: includes,
> -                    c_args: avx512_args)
> +                    c_args: [cflags, cflags_avx512, cc_avx512_flags])
>              objs += avx512_lib.extract_objects(sources_avx512)
>          endif
>      endif
> --
> 2.48.1


Acked-by: Vipin Varghese <vipin.varghese@amd.com>
  
Bruce Richardson June 24, 2025, 2:13 p.m. UTC | #2
On Wed, Jun 11, 2025 at 11:53:10AM +0100, Bruce Richardson wrote:
> When doing a build for a target that already has the instruction sets
> for AVX2/AVX512 enabled, skip emitting the AVX compiler flags, or the
> x86-64-v4 '-march' flags, as they are unnecessary. Instead, when
> the default flags produce the desired output, just use them unmodified,
> and don't bother adding in extra enabling flags for AVX2 or AVX-512.
> 
> Depends-on: series-35421 ("doc/linux_gsg: update recommended compiler versions")
> 
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> ---
> v5: Use "x86-64-v4" arch, when available, in place of "skylake-avx512"
> 
> V4: Fix error flagged by CI with clang builds without AVX512 - change
>     "cc_avx512_args" to correct "cc_avx512_flags"
> 
> V3: put in version check to work around an issues with some meson
>     versions, (hopefully) allowing builds to pass in all CIs. The
>     printout of the extra flags now only happens with meson >= 0.60.2
> 
> V2: dropped the doc update for the minimum compiler version.  Based on
>     discussion, that version bump is larger than proposed in RFC and is
>     now a separate patch/series [series referenced above]
> ---
>  config/x86/meson.build | 34 +++++++++++++++++++++++-----------
>  drivers/meson.build    |  9 +--------
>  lib/meson.build        |  9 +--------
>  3 files changed, 25 insertions(+), 27 deletions(-)
>
Recheck-request: rebase=main, iol-unit-amd64-testing
  
Thomas Monjalon June 27, 2025, 10:11 a.m. UTC | #3
> > When doing a build for a target that already has the instruction sets for
> > AVX2/AVX512 enabled, skip emitting the AVX compiler flags, or the
> > x86-64-v4 '-march' flags, as they are unnecessary. Instead, when the default flags
> > produce the desired output, just use them unmodified, and don't bother adding in
> > extra enabling flags for AVX2 or AVX-512.
> >
> > Depends-on: series-35421 ("doc/linux_gsg: update recommended compiler
> > versions")
> >
> > Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> 
> Acked-by: Vipin Varghese <vipin.varghese@amd.com>

Applied, thanks.
  

Patch

diff --git a/config/x86/meson.build b/config/x86/meson.build
index c3564b0011..e2ccfb6d12 100644
--- a/config/x86/meson.build
+++ b/config/x86/meson.build
@@ -4,11 +4,13 @@ 
 if is_ms_compiler
     cc_avx2_flags = ['/arch:AVX2']
 else
-    cc_avx2_flags = ['-mavx2']
+    cc_avx2_flags = []
+    if cc.get_define('__AVX2__', args: machine_args) == ''
+        cc_avx2_flags = ['-mavx2']
+    endif
 endif
 
 cc_has_avx512 = false
-target_has_avx512 = false
 
 dpdk_conf.set('RTE_ARCH_X86', 1)
 if dpdk_conf.get('RTE_ARCH_64')
@@ -65,26 +67,36 @@  if is_linux or cc.get_id() == 'gcc'
     endif
 endif
 
-cc_avx512_flags = ['-mavx512f', '-mavx512vl', '-mavx512dq', '-mavx512bw', '-mavx512cd']
-if (binutils_ok and cc.has_multi_arguments(cc_avx512_flags)
+avx512_march_flag = '-march=x86-64-v4'
+if not cc.has_argument(avx512_march_flag)
+    avx512_march_flag = '-march=skylake-avx512'
+endif
+cc_avx512_flags = []
+if (binutils_ok and cc.has_argument(avx512_march_flag)
         and '-mno-avx512f' not in get_option('c_args'))
     # check if compiler is working with _mm512_extracti64x4_epi64
     # Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82887
     code = '''#include <immintrin.h>
     void test(__m512i zmm){
         __m256i ymm = _mm512_extracti64x4_epi64(zmm, 0);}'''
-    result = cc.compiles(code, args : cc_avx512_flags, name : 'AVX512 checking')
+    result = cc.compiles(code, args : [avx512_march_flag], name : 'AVX512 checking')
     if result == false
         machine_args += '-mno-avx512f'
         warning('Broken _mm512_extracti64x4_epi64, disabling AVX512 support')
     else
         cc_has_avx512 = true
-        target_has_avx512 = (
-                cc.get_define('__AVX512F__', args: machine_args) != '' and
-                cc.get_define('__AVX512BW__', args: machine_args) != '' and
-                cc.get_define('__AVX512DQ__', args: machine_args) != '' and
-                cc.get_define('__AVX512VL__', args: machine_args) != ''
-            )
+        if cc.get_define('__AVX512F__', args: machine_args) == ''
+            cc_avx512_flags = [avx512_march_flag]
+            if cc.has_argument('-Wno-overriding-option')
+                cc_avx512_flags += '-Wno-overriding-option'
+            endif
+        endif
+    endif
+endif
+if developer_mode and meson.version().version_compare('>=0.60.2')
+    message('Extra C flags needed for AVX2 output: @0@'.format(cc_avx2_flags))
+    if cc_has_avx512
+        message('Extra C flags needed for AVX512 output: @0@'.format(cc_avx512_flags))
     endif
 endif
 
diff --git a/drivers/meson.build b/drivers/meson.build
index 7b7205dfac..b62880db02 100644
--- a/drivers/meson.build
+++ b/drivers/meson.build
@@ -267,18 +267,11 @@  foreach subpath:subdirs
             endif
             if sources_avx512.length() > 0 and cc_has_avx512
                 cflags += '-DCC_AVX512_SUPPORT'
-                avx512_args = [cflags, cc_avx512_flags]
-                if not target_has_avx512 and cc.has_argument('-march=skylake-avx512')
-                    avx512_args += '-march=skylake-avx512'
-                    if cc.has_argument('-Wno-overriding-option')
-                        avx512_args += '-Wno-overriding-option'
-                    endif
-                endif
                 avx512_lib = static_library(lib_name + '_avx512_lib',
                         sources_avx512,
                         dependencies: static_deps,
                         include_directories: includes,
-                        c_args: avx512_args)
+                        c_args: [cflags, cc_avx512_flags])
                 objs += avx512_lib.extract_objects(sources_avx512)
             endif
         endif
diff --git a/lib/meson.build b/lib/meson.build
index 1934cb4a29..0d56b2083b 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -261,18 +261,11 @@  foreach l:libraries
         endif
         if sources_avx512.length() > 0 and cc_has_avx512
             cflags += '-DCC_AVX512_SUPPORT'
-            avx512_args = [cflags, cflags_avx512, cc_avx512_flags]
-            if not target_has_avx512 and cc.has_argument('-march=skylake-avx512')
-                avx512_args += '-march=skylake-avx512'
-                if cc.has_argument('-Wno-overriding-option')
-                    avx512_args += '-Wno-overriding-option'
-                endif
-            endif
             avx512_lib = static_library(libname + '_avx512_lib',
                     sources_avx512,
                     dependencies: static_deps,
                     include_directories: includes,
-                    c_args: avx512_args)
+                    c_args: [cflags, cflags_avx512, cc_avx512_flags])
             objs += avx512_lib.extract_objects(sources_avx512)
         endif
     endif