为什么GCC不能自动矢量化这个循环?

时间:2011-11-15 22:43:14

标签: c++ gcc vectorization

我正在尝试优化一个占用程序计算时间的循环。

但是当我使用-O3 -ffast-math -ftree-vectorizer-verbose = 6 GCC输出打开自动矢量化时,它无法对循环进行矢量化。

我正在使用GCC 4.4.5

代码:

/// Find the point in the path with the largest v parameter
void prediction::find_knife_edge(
    const float * __restrict__ const elevation_path,
    float * __restrict__ const diff_path,
    const float path_res,
    const unsigned a,
    const unsigned b,
    const float h_a,
    const float h_b,
    const float f,
    const float r_e,
) const
{
    float wavelength = (speed_of_light * 1e-6f) / f;

    float d_ab = path_res * static_cast<float>(b - a);

    for (unsigned n = a + 1; n <= b - 1; n++)
    {
        float d_an = path_res * static_cast<float>(n - a);
        float d_nb = path_res * static_cast<float>(b - n);

        float h = elevation_path[n] + (d_an * d_nb) / (2.0f * r_e) - (h_a * d_nb + h_b * d_an) / d_ab;
        float v = h * std::sqrt((2.0f * d_ab) / (wavelength * d_an * d_nb));

        diff_path[n] = v;
    }
}

来自GCC的消息:

note: not vectorized: number of iterations cannot be computed.
note: not vectorized: unhandled data-ref 

在关于自动矢量化(http://gcc.gnu.org/projects/tree-ssa/vectorization.html)的页面上,它声明它支持未知的循环边界。

如果我用

替换for
for (unsigned n = 0; n <= 100; n++)

然后它将它矢量化。

我做错了什么?

缺乏关于这些消息究竟是什么意思以及GCC自动矢量化的输入/输出的详细文档是相当烦人的。

修改

感谢David,我将循环更改为:

 for (unsigned n = a + 1; n < b; n++)

现在GCC尝试对循环进行矢量化,但抛出了这个错误:

 note: not vectorized: unhandled data-ref
 note: Alignment of access forced using peeling.
 note: Vectorizing an unaligned access.
 note: vect_model_induction_cost: inside_cost = 1, outside_cost = 2 .
 note: not vectorized: relevant stmt not supported: D.76777_65 = (float) n_34;

“D.76777_65 =(浮动)n_34;”意思?

1 个答案:

答案 0 :(得分:8)

我可能会略微破坏细节,但这是你需要重构循环以使其进行矢量化的方法。诀窍是预先计算迭代次数,并从0到1之间进行迭代。请勿更改for语句。您可能需要在它之前修复两条线,并在循环顶部修复两条线。他们大约正确。 ;)

const unsigned it=(b-a)-1;
const unsigned diff=b-a;
for (unsigned n = 0; n < it; n++)
{
    float d_an = path_res * static_cast<float>(n);
    float d_nb = path_res * static_cast<float>(diff - n);

    float h = elevation_path[n] + (d_an * d_nb) / (2.0f * r_e) - (h_a * d_nb + h_b * d_an) / d_ab;
    float v = h * sqrt((2.0f * d_ab) / (wavelength * d_an * d_nb));

    diff_path[n] = v;
}