首页 > 解决方案 > gcc/clang 对局部变量和结构字段使用限制关键字

问题描述

不知道如何说服 gcc/clang 我的指针不相交;就我所见,它看起来restrict只有在函数参数中指定时才会被尊重,否则会被忽略。这是我的代码:

#if defined(_MSC_VER) || defined(__cplusplus)
#define restrict __restrict
#endif

struct s {
    int sz;
    int *a;
    int *b;
};

struct s_r {
    int sz;
    int *restrict a;
    int *restrict b;
};

void foo_dumb_struct(struct s *s, int c) {
    int sz = s->sz;
    for(int i = 0; i != sz; ++i) {
        s->a[i] = s->b[0] + c;
    }
}

void foo_restricted_arrays(int sz,
        int *restrict a, int *restrict b,
        int c) {
    for(int i = 0; i != sz; ++i) {
        a[i] = b[0] + c;
    }
}

void foo_restricted_struct(struct s_r *s, int c) {
    int sz = s->sz;
    for(int i = 0; i != sz; ++i) {
        s->a[i] = s->b[0] + c;
    }
}

void foo_restricted_subcall(struct s *s, int c) {
    foo_restricted_arrays(s->sz, s->a, s->b, c);
}

void foo_restricted_cast(struct s *s, int c) {
    int sz = s->sz;
    int *restrict a = s->a;
    int *restrict b = s->b;
    for(int i = 0; i != sz; ++i) {
        a[i] = b[0] + c;
    }
}

b[0]Icc 对这段代码很好,但 gcc/clang 会在每次迭代时重新读取foo_restricted_structand foo_restricted_cast,对于我可以用 Godbolt 测试的所有架构。任何时候在函数参数(包括嵌套函数或 C++ lambdas)中使用它都可以,并且移除了额外的负载。https://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html表明它实际上可以按我的意愿工作,但我不确定他们的 gcc 不是为单元格定制的具体来说。

我对限制的使用是错误的,还是 gcc/clang 只对函数参数实现限制而没有别的?

标签: cgccclang

解决方案


restrict不是由 C 标准为结构成员定义的。

6.7.3.1 中的正式定义restrict以“令D为普通标识符的声明……”开头。</p>

6.2.3 1 定义定义“普通标识符”以排除结构或联合的成员:

…因此,各种标识符类别都有单独的名称空间,如下所示:

标签名称(通过标签声明和使用的语法消除歧义);

—结构、联合和枚举的标签(通过遵循任何关键字structunionenum来消除歧义);

——结构或工会的成员每个结构或联合对其成员都有一个单独的名称空间(通过用于通过.->运算符访问成员的表达式的类型来消除歧义);

— 所有其他标识符,称为普通标识符(在普通声明符中声明或作为枚举常量声明)。

6.7.2.1 中的脚注 126 明确告诉我们结构成员不是普通标识符:

结构或联合不能包含具有可变修改类型的成员,因为成员名称不是 6.2.3 中定义的普通标识符。


推荐阅读