为什么GCC -O3与std :: deque上的过滤器迭代器导致无限的std :: distance?

时间:2016-09-10 09:40:59

标签: c++ c++11 gcc deque filter-iterator

在经历了很多痛苦和痛苦之后,我已经找到了一些非常奇怪的行为,std::distanceboost::filter_iterator超过std::deque的情况下永远不会返回。看来这个问题对于GCC(6.1+)具有-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;
}

一些观察结果:

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

发生了什么?

2 个答案:

答案 0 :(得分: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'在测试中始终为true。因此看起来它的存在正在使编译器生成错误的代码。

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

#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;

}

答案 1 :(得分:4)

此行为是由于错误的矢量化优化导致的GCC bug。现已发布修正案,该修正案应出现在GCC 6.3中。

对于那些坚持使用GCC 5.4 - 6.2的人来说,编译器选项-fno-tree-slp-vectorize将会修复&#39;这个问题。

相关问题