c - 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_struct
and foo_restricted_cast
,对于我可以用 Godbolt 测试的所有架构。任何时候在函数参数(包括嵌套函数或 C++ lambdas)中使用它都可以,并且移除了额外的负载。https://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html表明它实际上可以按我的意愿工作,但我不确定他们的 gcc 不是为单元格定制的具体来说。
我对限制的使用是错误的,还是 gcc/clang 只对函数参数实现限制而没有别的?
解决方案
restrict
不是由 C 标准为结构成员定义的。
6.7.3.1 中的正式定义restrict
以“令D为普通标识符的声明……”开头。</p>
6.2.3 1 定义定义“普通标识符”以排除结构或联合的成员:
…因此,各种标识符类别都有单独的名称空间,如下所示:
—标签名称(通过标签声明和使用的语法消除歧义);
—结构、联合和枚举的标签(通过遵循任何关键字struct、union或enum来消除歧义);
——结构或工会的成员;每个结构或联合对其成员都有一个单独的名称空间(通过用于通过.或 ->运算符访问成员的表达式的类型来消除歧义);
— 所有其他标识符,称为普通标识符(在普通声明符中声明或作为枚举常量声明)。
6.7.2.1 中的脚注 126 明确告诉我们结构成员不是普通标识符:
结构或联合不能包含具有可变修改类型的成员,因为成员名称不是 6.2.3 中定义的普通标识符。
推荐阅读
- batch-file - 在批处理文件中获取原始参数或命令
- xml - xmlslurper 按子值从 xml 中删除节点
- python - 比较两个列表中的值,然后计算哪个列表更大
- python - Pandas.plotting.parallel_coordinates 中的绘图顺序
- google-cloud-platform - ./helm init --service-account=tiller 不能在 GCP 上创建一个 tiller 服务帐户
- optimization - CLP 求解器(Coin-Or)的精度是多少?
- javascript - Microsoft Edge 对 canPlayType 上的 AV1 的错误支持
- .net - Windows 10 上的 .NET 4.8 TLS 1.3 问题
- tpm - TPM 设备是否应该同时支持 TPM1.2 和 TPM2.0
- google-sheets - 根据其他列中的选择查找名称