xmake icon indicating copy to clipboard operation
xmake copied to clipboard

avx512 for add_vectorexts

Open xq114 opened this issue 3 years ago • 5 comments

你在什么场景下需要该功能?

当前的add_vectorexts("avx512")无法添加avx512的flag,只能手动加/arch:AVX512

描述可能的解决方案

add_vectorexts支持avx512

xq114 avatar Aug 29 '21 10:08 xq114

自己改下 https://github.com/xmake-io/xmake/blob/4497dd33fb475a388d5d51192d432b0156be78a3/xmake/modules/core/tools/cl.lua#L178

waruqi avatar Aug 29 '21 10:08 waruqi

看了下 https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html

MSVC的avx512就一个flag /arch:AVX512,但gcc的avx512有十几个flag;而且gcc推荐的方式是用-march=native开启所有可用的矢量扩展,不建议使用-mavx512f这样的flag,而不是msvc推荐的一个个设置扩展的flag、测试是否可用再加上。感觉这个差异太大,用add_vectorexts()来抽象avx512有点不太合适,应该有更好的设计。现在暂时用的判断

if is_plat("windows") then
	add_cxxflags("/arch:AVX512") -- leave detection to xmake
else
	add_cxxflags("-march=native")
end

xq114 avatar Aug 29 '21 12:08 xq114

不好抽象的话,就先外面设置下好了

waruqi avatar Aug 29 '21 12:08 waruqi

似乎有些cmake项目用-mavx512f -mavx512dq -mavx512bw -mavx512vl这几个flag同时存在来判断avx512,是否可以借鉴一下?

https://www.zhihu.com/question/433345697/answer/1611130480

根据这篇文章,这几个flag也是普通cpu主要支持的几个,其他很多是专业cpu才有

xq114 avatar Apr 11 '22 16:04 xq114

最近没啥空,如果好搞,就来个 pr 过来好了

waruqi avatar Apr 11 '22 16:04 waruqi

gcc 用这个呢?-march=skylake-avx512

https://www.gnu.org/software/gcc/gcc-6/changes.html https://stackoverflow.com/questions/57043592/compilation-error-for-avx512-is-it-a-gcc-issue

GCC now supports the Intel CPU named Skylake with AVX-512 extensions through -march=skylake-avx512. The switch enables the following ISA extensions: AVX-512F, AVX512VL, AVX-512CD, AVX-512BW, AVX-512DQ.

chatgpt 也推荐这个

waruqi avatar Jul 26 '23 08:07 waruqi

我加上了,可以试试,sse4.2 顺带一起加了 https://github.com/xmake-io/xmake/pull/4012

xmake update -s github:xmake-io/xmake#avx512

waruqi avatar Jul 26 '23 09:07 waruqi

gcc 用这个呢?-march=skylake-avx512

这个我觉得不太合适吧,arch指定为skylake-avx512意味着skylake-avx512之后的intrinsic都被禁用了,而且没法通过加flag开启,会有副作用。我觉得不如反向考虑,给windows加一个add_vectorexts("native")的选项,在非windows平台为-march=native,在windows上由xmake探测并加上支持到最高的simd扩展,类似set_languages("c++latest")。通用情况要avx512直接开native,有特殊需求手动加flag

xq114 avatar Jul 26 '23 10:07 xq114

Bot detected the issue body's language is not English, translate it automatically.


What about gcc using this? -march=skylake-avx512

I don't think this is appropriate. The arch designation as skylake-avx512 means that the intrinsics after skylake-avx512 are disabled, and it cannot be enabled by adding a flag, which will have side effects. I think it is better to think in reverse, add an option of add_vectorexts("native") to windows, -march=native on non-windows platforms, and detect and add support to the highest simd extension on windows, similar to set_languages("c++latest"). In general, avx512 should be directly enabled native, and if there are special needs, manually add flag

Issues-translate-bot avatar Jul 26 '23 10:07 Issues-translate-bot

gcc 用这个呢?-march=skylake-avx512

这个我觉得不太合适吧,arch指定为skylake-avx512意味着skylake-avx512之后的intrinsic都被禁用了,而且没法通过加flag开启,会有副作用。我觉得不如反向考虑,给windows加一个add_vectorexts("native")的选项,在非windows平台为-march=native,在windows上由xmake探测并加上支持到最高的simd扩展,类似set_languages("c++latest")。通用情况要avx512直接开native,有特殊需求手动加flag

那这个 native 的语义不是就是 all 么。。那直接用 add_vectorexts("all") 应该会更好些,也更直观。设置 vectorexts 本身就是 native 了,用 native 有点歧义。

waruqi avatar Jul 26 '23 13:07 waruqi

我改了下,再看看呢。。

add_vectorexts("avx512")

默认 gcc 下,我暂时加了 "-mavx512f", "-mavx512dq", "-mavx512bw", "-mavx512vl" 这四个,实际会自动检测支持力度来过滤掉当前 gcc 不支持的。

然后额外加了个 all,gcc 下对标 -march=native,msvc 下,加上了全部

add_vectorexts("all")

waruqi avatar Jul 26 '23 14:07 waruqi

那这个 native 的语义不是就是 all 么。。

主要是有的avx指令当前平台不支持,-march=native只在支持avx512的平台开启avx512,就像c++latest也不一定是c++23

xq114 avatar Jul 26 '23 14:07 xq114

那这个 native 的语义不是就是 all 么。。

主要是有的avx指令当前平台不支持,-march=native只在支持avx512的平台开启avx512,就像c++latest也不一定是c++23

所以它这个 native 的语义很晦涩,xmake 里面还是用 all 更贴切直观。。就是尽可能开启全部能支持的 simd 指令。。内部到底用 -march:native ,或者改成其他的,或者加其他组合,都可以随时变,并不是跟 -march=native 强绑定的

waruqi avatar Jul 26 '23 14:07 waruqi

如果现在这个 patch 没啥问题,我就先 merge 了

waruqi avatar Jul 26 '23 14:07 waruqi

Bot detected the issue body's language is not English, translate it automatically.


Isn't the semantics of this native just all? .

The main reason is that some avx instructions are not supported by the current platform. -march=native only enables avx512 on platforms that support avx512, just like c++latest is not necessarily c++23

Issues-translate-bot avatar Jul 26 '23 14:07 Issues-translate-bot

Bot detected the issue body's language is not English, translate it automatically.


Then the semantics of this native is not all. .

The main reason is that some avx instructions are not supported by the current platform, -march=native only enables avx512 on platforms that support avx512, just like c++latest is not necessarily c++23

So the semantics of its native is very obscure, and it is more appropriate and intuitive to use all in xmake. . It is to enable all supported simd instructions as much as possible. . Use -march:native internally, or change it to other, or add other combinations, it can be changed at any time, and it is not strongly bound to -march=native

Issues-translate-bot avatar Jul 26 '23 14:07 Issues-translate-bot

Bot detected the issue body's language is not English, translate it automatically.


If there is nothing wrong with this patch now, I will merge it first

Issues-translate-bot avatar Jul 26 '23 14:07 Issues-translate-bot