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

标签 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;
    }
}

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/

相关文章:

clang-format:如果参数不合适,总是会中断?

python - 如何从 C 创建一个 numpy 记录数组

c - float arrayName[][] 和 float (* arrayNamePointer)[] 有什么区别

macos - gcc "can' t be more simple"program got Undefined symbols for architecture x86_64 编译错误

gcc 将所有警告视为错误,除了 X

excel - dylib 动态库调用 dylib : Undefined symbols for architecture i386

c++ - g++ 和 clang++ 之间的浮点运算有所不同吗?

c - 我在 matmul 函数上使用了 openMP 指令,它不起作用

c++ - 将预处理器宏定义到宏定义行

c++ - 交互设备的定义