简体   繁体   English

将 avx 变量传递给 std::function 时引发 bad_function_call 和分段错误

[英]bad_function_call thrown and segmentation fault caused when passing avx variables to std::function

This problem is found when writing some code related to computer graphics, a simplified version of the code is shown below:这个问题是在写一些计算机图形相关的代码时发现的,简化版的代码如下图所示:

#include <bits/stdc++.h>

#define __AVX__ 1
#define __AVX2__ 1
#pragma GCC target("avx,avx2,popcnt,tune=native")
#include <immintrin.h>

namespace with_avx {
class vec {
   public:
    vec(double x = 0, double y = 0, double z = 0, double t = 0) {
        vec_data = _mm256_set_pd(t, z, y, x);
    }
    __m256d vec_data;
};
}  // namespace with_avx

namespace without_avx {
class vec {
   public:
    vec(double x = 0, double y = 0, double z = 0, double t = 0) {
        vec_data[0] = x, vec_data[1] = y, vec_data[2] = z, vec_data[3] = t;
    }
    double vec_data[4];
};
}  // namespace without_avx

#ifdef USE_AVX
using namespace with_avx;
#else
using namespace without_avx;
#endif

vec same(vec x) { return x; }
std::function<vec(vec)> stdfunc = same;

int main() { 
    vec rand_vec(rand(), rand(), rand());
    vec ret = stdfunc(rand_vec);
    std::cout<<(double)ret.vec_data[0];
}

If I compile the code with the flag USE_AVX like the following:如果我使用标志USE_AVX编译代码,如下所示:

 g++-12 stdfunction_test.cpp -o ../build/unit_test -D USE_AVX -g

g++ will output some warnings: g++ 将 output 一些警告:

In file included from /usr/include/c++/12/functional:59,
                 from /usr/include/x86_64-linux-gnu/c++/12/bits/stdc++.h:71,
                 from stdfunction_test.cpp:2:
/usr/include/c++/12/bits/std_function.h: In member function ‘_Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = with_avx::vec; _ArgTypes = {with_avx::vec}]’:
/usr/include/c++/12/bits/std_function.h:587:7: note: the ABI for passing parameters with 32-byte alignment has changed in GCC 4.6
  587 |       operator()(_ArgTypes... __args) const
      |       ^~~~~~~~

Then if I run the code, sometimes segmentation fault is caused with the following output:然后,如果我运行代码,有时会导致以下 output 导致分段错误:

[1]    12710 segmentation fault  ../build/unit_test

Sometimes, bad_function_call is thrown with the following output:有时,bad_function_call 与以下 output 一起抛出:

terminate called after throwing an instance of 'std::bad_function_call'
  what():  bad_function_call
[1]    12678 IOT instruction  ../build/unit_test

Both of these two errors are made when this line is executed:执行此行时会出现这两个错误:

vec ret = stdfunc(rand_vec);

I then used gdb for backtrace:然后我使用 gdb 进行回溯:

(gdb) bt
#0  0x00007ffff7e35521 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007ffff7e2c6f4 in std::__throw_bad_function_call() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x000055555555558b in std::function<with_avx::vec (with_avx::vec)>::operator()(with_avx::vec) const (this=0x7fffffffda74,
    __args#0=...) at /usr/include/c++/12/bits/std_function.h:590
#3  0x000055555555528d in main () at stdfunction_test.cpp:39

However if I don't add the flag, the code would run normally.但是,如果我不添加标志,代码将正常运行。

I think this is possibly caused by some kind of alignment problems like the warning sait I just don't know how to solve this.我认为这可能是由某种 alignment 问题引起的,比如警告说我只是不知道如何解决这个问题。

My environment is listed on the following, hope they will be useful:我的环境列在下面,希望对你有用:

  • g++ version: g++-12 (Ubuntu 12-20220319-1ubuntu1) 12.0.1 20220319 (experimental) [master r12-7719-g8ca61ad148f] g++ 版本: g++-12 (Ubuntu 12-20220319-1ubuntu1) 12.0.1 20220319 (experimental) [master r12-7719-g8ca61ad148f]
  • OS: Ubuntu-22.04 running on WSL2操作系统:在 WSL2 上运行的 Ubuntu-22.04

Changing the target architecture half way through the file is causing your issue.在文件中途更改目标体系结构会导致您的问题。 Presumably parts of std::function 's implementation changes with the target architecture.大概std::function的部分实现随目标架构而变化。 Moving your pragma to the start of the file fixes the problem: https://godbolt.org/z/WP5ah38WP将您的编译指示移到文件的开头可以解决问题: https://godbolt.org/z/WP5ah38WP

It'll be safer in general if you set your architecture target via the compiler command line (eg -mavx2 ) which will ensure all your code is compiled with the same architectures: https://godbolt.org/z/z5j79c5eh如果您通过编译器命令行(例如-mavx2 )设置架构目标,通常会更安全,这将确保您的所有代码都使用相同的架构编译: https://godbolt.org/z/z5j79c5eh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM