c++ - 为什么 GCC -O3 在 std::deque 上使用过滤器迭代器导致无限的 std::distance?

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

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

#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 问题就会消失。

发生了什么事?

最佳答案

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

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

选项集是:

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

}

关于c++ - 为什么 GCC -O3 在 std::deque 上使用过滤器迭代器导致无限的 std::distance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39424753/

相关文章:

c - GCC错误结构灵活数组成员没有命名成员

c++ - 使用 g++ 进行奇怪的零初始化

c++ - 使用 STL 解析整数

c++ - 移动 basic_iostream 时 basic_ostream 基会发生什么变化?

c++ - 不同编译器中的 std::filesystem 和 std::experimental::filesystem 问题

c++ - 从模板函数返回的 const 相关名称,const 去哪里了?

c - 指向字符串的指针数组,发生段错误

python - 有人可以解释 pybind11 安装吗?

c++ - 从 python 脚本调用非返回 python 函数

c++ - 为什么动态分配的指针数组不需要解引用来获取它们的实际成员