调用 always_inline ‘_mm_mullo_epi32’ 时内联失败:目标特定选项不匹配

inlining failed in call to always_inline ‘_mm_mullo_epi32’: target specific option mismatch

我正在尝试使用使用 SIMD 内在函数的 cmake 编译 C 程序。当我尝试编译它时,出现两个错误

/usr/lib/gcc/x86_64-linux-gnu/5/include/smmintrin.h:326:1:错误:调用 always_inline '_mm_mullo_epi32' 时内联失败: 目标特定选项不匹配 _mm_mullo_epi32 (__m128i __X, __m128i __Y)

/usr/lib/gcc/x86_64-linux-gnu/5/include/tmmintrin.h:136:1: 错误:调用 always_inline '_mm_shuffle_epi8' 时内联失败: 目标特定选项不匹配 _mm_shuffle_epi8 (__m128i __X, __m128i __Y)

此问题已在此处 Whosebug 通过设置

解决
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1")

我尝试了相同的选项和许多其他选项。但是我的项目仍然无法编译。

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1")  
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -sse4_1")  
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=nehalem")  
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1 -msse4.2")  
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")  
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ssse3")  

由于您正在编译 C 代码,而不是 C++,因此您需要:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.1")

您可以删除所有其他 -march XXX-msseXXX 设置。

如果您混合使用 C 和 C++,那么您还可以添加:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1")

A general method to find the instruction switch for gcc

文件intrin.sh:

#!/bin/bash

get_instruction ()
{
    [ -z "" ] && exit
    func_name=" "

    header_file=`grep --include=\*intrin.h -Rl "$func_name" /usr/lib/gcc | head -n1`
    [ -z "$header_file" ] && exit
    >&2 echo "find in: $header_file"

    target_directive=`grep "#pragma GCC target(\|$func_name" $header_file | grep -B 1 "$func_name" | head -n1`
    echo $target_directive | grep -o '"[^,]*[,"]' | sed 's/"//g' | sed 's/,//g'
}

instruction=`get_instruction `
if [ -z "$instruction" ]; then
    echo "Error: function not found: "
else
    echo "add this option to gcc: -m$instruction"
fi

用法:

./intrin.sh _mm_shuffle_epi8      # output: -mssse3
./intrin.sh _mm_cvtepu8_epi32     # output: -msse4.1
./intrin.sh _mm_loadu_ps          # output: -msse
./intrin.sh _mm_clmulepi64_si128  # output: -mpclmul
./intrin.sh _mm256_loadu_si256    # output: -mavx
./intrin.sh _mm512_and_ps         # output: -mavx512dq