37

在经历了很多痛苦和痛苦之后,我发现了一些非常奇怪的行为,当给定s 超过 astd::distance的范围时,这些行为永远不会返回。看来这个问题是 GCC (6.1+) 所独有的,并进行了优化。这是一个演示违规行为的示例:boost::filter_iteratorstd::deque-O3

#include <string>
#include <deque>
#include <iterator>
#include <iostream>

#include <boost/iterator/filter_iterator.hpp>

struct Foo
{
    std::string bar, s = "";
    char a = '\0';
};

int main()
{
    const std::deque<Foo> foos(14, {""});
    const std::string test {};
    const auto p = [test] (const auto& foo) { return foo.bar == test; };
    using boost::make_filter_iterator;
    const auto begin = make_filter_iterator(p, std::cbegin(foos), std::cend(foos));
    const auto end   = make_filter_iterator(p, std::cend(foos), std::cend(foos));
    std::cout << std::distance(begin, end) << std::endl;
}

一些观察:

  • GCC 具有优化-O2或预期更少的回报。
  • Clang (3.8) 以任何优化级别返回正确答案。
  • 更改std::dequestd::vectorstd::list导致预期的行为。
  • 14很关键;少一点,问题就消失了。
  • sizeof(Foo)很重要;消除sa使问题消失。
  • 通过引用捕获test或仅与常量表达式(例如foo.bar == " ")进行比较会导致正常行为。
  • 没有编译器警告(带有-Wall -Wextra -pedantic)。
  • Valgrind 没有报告错误。
  • 使用fsanitize=undefined,问题就消失了。

这是怎么回事?

4

2 回答 2

4

我认为下面的这些发现可能对改进错误报告以及在您的代码中使用来解决问题很有用。

通过调试优化的输出并使用优化标志和微小的代码更改,我已经得出关于导致错误的特定优化标志的结论。

选项集是:

-O -fno-auto-inc-dec -fno-branch-count-reg -fno-combine-stack-adjustments -fno-compare-elim -fno-cprop-registers -fno-dce -fno-defer-pop -fno-delayed-branch -fno-dse -fno-forward-propagate -fno-guess-branch-probability -fno-if-conversion2 -fno-if-conversion -fno-inline-functions-called-once -fno-ipa-pure-const -fno-ipa-profile -fno-ipa-reference -fno-merge-constants -fno-move-loop-invariants -fno-reorder-blocks -fno-shrink-wrap -fno-split-wide-types -fno-ssa-backprop -fno-ssa-phiopt -fno-tree-bit-ccp -fno-tree-ccp -fno-tree-ch -fno-tree-coalesce-vars -fno-tree-phiprop -fno-tree-sink -fno-tree-slsr -fno-tree-dse -fno-tree-forwprop -fno-tree-fre -fno-unit-at-a-time -fno-tree-ter -fno-tree-sra -fno-tree-copy-prop -fstrict-aliasing -ftree-slp-vectorize -std=c++14

很抱歉这么长的设置,但我真正想要的是:(-O0 -ftree-copy-prop -ftree-pta -ftree-dce -fstrict-aliasing -ftree-slp-vectorize我也尝试过 -Og )加上 O1 的神奇步骤......

请注意,这-O3 -f-no-tree-slp-vectorize已经修复了该行为,但是通过使用我发送的完整选项,调试几乎很容易......

此外,看起来运算符的存在==(string, string)正在编译器中产生混乱。

如果您检查粘贴在下面所有由#if 0 代码注释的代码,激活后在原始代码的位置工作,您可能会发现我没有发现的问题。

请注意,==()甚至没有调用运算符,因为foo.a != '\0'在测试中始终为真。因此,看起来它的存在正在使编译器生成错误的代码。

另请注意,循环内的任何注释代码也会将行为更改为预期的行为,这就是我怀疑初学者的矢量化标志的原因。

#include <string>
#include <deque>
#include <iterator>
#include <iostream>

#include <boost/iterator/filter_iterator.hpp>
#include <string.h>

struct Foo
{
    std::string bar, s = "";
    char a = 'n';
};

std::ostream& operator<<(std::ostream& os, const Foo& f)
{
    os << f.bar << '/' << f.a;
    return os;
}

int main()
{
    std::deque<Foo> foos(14, {"abc"});
    const std::string test {"abc"};
    Foo other;
    other.bar = "last"; other.a = 'l';
    foos.push_back(other);
    other.bar = "first";
    other.a = 'f';
    foos.push_front(other);
    //
#if 0
    const auto p = [test] (const auto& foo) { return foo.a != '\0'; };
#elif 0
    const auto p = [test] (const auto& foo) {
        bool  rc =  (foo.a != '\0');
        if (!rc)
            rc = (foo.bar == std::string(test));
        return rc;
    };
#elif 1
    const auto p = [test] (const auto& foo) {
        bool  rc =  (foo.a != '\0');
        if (!rc)
            rc = (foo.bar == test);
        return rc;
    };
#endif
    using boost::make_filter_iterator;
    const auto begin = make_filter_iterator(p, std::cbegin(foos), std::cend(foos));
    const auto end   = make_filter_iterator(p, std::cend(foos), std::cend(foos));
    std::cout << std::distance(end, end) << std::endl;
    std::cout << std::distance(begin, begin) << std::endl;
    std::cout << std::distance(std::cbegin(foos), std::cend(foos)) << std::endl;

    auto __first = begin;
    auto __last = end;

    int __n = 0;
    //std::cout << __last << std::endl;
    //std::deque<char> trace;
    //Foo trace[21];
    const int max = foos.size();
    char trace[max+5]; memset(trace, 'c', sizeof(trace));

    std::cout << max << std::endl;
    std::cout << *__last << std::endl;

    while (__first != __last)
    {
        trace[__n] = (*__first).a;
        //trace[__n] = (*__first);
        //trace.push_back((*__first).a);
        //std::cout << *__first << std::endl;
        ++__n;
        ++__first;
        if (__n > max + 5)
            break;
        //std::cout << __n << std::endl;
        //std::cout << (__first != __last) << std::endl;
    }

    for (auto f: trace)
        std::cout << f  << std::endl;
    std::cout << "Tadaaaaa: " <<  __n << std::endl;

    //std::cout << std::distance(begin, end) << std::endl;

}
于 2016-10-12T15:45:15.717 回答
4

此行为是由于矢量化优化错误导致的GCC 错误。现在已经发布了一个修复程序,它应该出现在 GCC 6.3 中。

对于那些坚持使用 GCC 5.4 - 6.2 的人,编译器选项-fno-tree-slp-vectorize将“修复”问题。

于 2016-11-03T11:41:07.770 回答