gpt4 book ai didi

c - gcc/clang 对局部变量和结构字段使用限制关键字

转载 作者:行者123 更新时间:2023-12-03 09:31:03 24 4
gpt4 key购买 nike

不知道如何让 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;
}
}

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

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

最佳答案

restrict C 标准没有为结构成员定义。
restrict的正式定义在 6.7.3.1 中以“让 D 是一个普通标识符的声明……”

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

… Thus, there are separate name spaces for various categories of identifiers, as follows:

label names (disambiguated by the syntax of the label declaration and use);

— the tags of structures, unions, and enumerations (disambiguated by following any of the keywords struct, union, or enum);

— the members of structures or unions; each structure or union has a separate name space for its members (disambiguated by the type of the expression used to access the member via the . or -> operator);

— all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).



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

A structure or union cannot contain a member with a variably modified type because member names are not ordinary identifiers as defined in 6.2.3.

关于c - gcc/clang 对局部变量和结构字段使用限制关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60615601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com