gpt4 book ai didi

c - Clang 是否误解了 'const' 指针说明符?

转载 作者:太空宇宙 更新时间:2023-11-04 00:43:06 24 4
gpt4 key购买 nike

在下面的代码中,我看到 clang 在没有隐式 restrict 的情况下无法执行更好的优化。指针说明符:

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct {
uint32_t event_type;
uintptr_t param;
} event_t;

typedef struct
{
event_t *queue;
size_t size;
uint16_t num_of_items;
uint8_t rd_idx;
uint8_t wr_idx;
} queue_t;

static bool queue_is_full(const queue_t *const queue_ptr)
{
return queue_ptr->num_of_items == queue_ptr->size;
}

static size_t queue_get_size_mask(const queue_t *const queue_ptr)
{
return queue_ptr->size - 1;
}

int queue_enqueue(queue_t *const queue_ptr, const event_t *const event_ptr)
{
if(queue_is_full(queue_ptr))
{
return 1;
}

queue_ptr->queue[queue_ptr->wr_idx++] = *event_ptr;
queue_ptr->num_of_items++;
queue_ptr->wr_idx &= queue_get_size_mask(queue_ptr);

return 0;
}

我用 clang 版本 11.0.0 (clang-1100.0.32.5) 编译了这段代码

clang -O2 -arch armv7m -S test.c -o test.s

在反汇编文件中我看到生成的代码重新读取内存:

_queue_enqueue:
.cfi_startproc
@ %bb.0:
ldrh r2, [r0, #8] ---> reads the queue_ptr->num_of_items
ldr r3, [r0, #4] ---> reads the queue_ptr->size
cmp r3, r2
itt eq
moveq r0, #1
bxeq lr
ldrb r2, [r0, #11] ---> reads the queue_ptr->wr_idx
adds r3, r2, #1
strb r3, [r0, #11] ---> stores the queue_ptr->wr_idx + 1
ldr.w r12, [r1]
ldr r3, [r0]
ldr r1, [r1, #4]
str.w r12, [r3, r2, lsl #3]
add.w r2, r3, r2, lsl #3
str r1, [r2, #4]
ldrh r1, [r0, #8] ---> !!! re-reads the queue_ptr->num_of_items
adds r1, #1
strh r1, [r0, #8]
ldrb r1, [r0, #4] ---> !!! re-reads the queue_ptr->size (only the first byte)
ldrb r2, [r0, #11] ---> !!! re-reads the queue_ptr->wr_idx
subs r1, #1
ands r1, r2
strb r1, [r0, #11] ---> !!! stores the updated queue_ptr->wr_idx once again after applying the mask
movs r0, #0
bx lr
.cfi_endproc
@ -- End function

添加 restrict 后指针的关键字,这些不需要的重读就消失了:

int queue_enqueue(queue_t * restrict const queue_ptr, const event_t * restrict const event_ptr)

我知道在 clang 中,默认情况下禁用严格别名。但在这种情况下,event_ptr指针定义为 const所以它的对象的内容不能被这个指针修改,因此它不能影响queue_ptr到的内容点(假设对象在内存中重叠的情况),对吧?

这是一个编译器优化错误,还是当 queue_ptr 指向的对象时确实存在一些奇怪的情况?会受到 event_ptr 的影响假设这个声明:

int queue_enqueue(queue_t *const queue_ptr, const event_t *const event_ptr)

顺便说一句,我尝试为 x86 目标编译相同的代码并检查了类似的优化问题。


生成的程序集带有 restrict关键字,不包含重读:

_queue_enqueue:
.cfi_startproc
@ %bb.0:
ldr r3, [r0, #4]
ldrh r2, [r0, #8]
cmp r3, r2
itt eq
moveq r0, #1
bxeq lr
push {r4, r6, r7, lr}
.cfi_def_cfa_offset 16
.cfi_offset lr, -4
.cfi_offset r7, -8
.cfi_offset r6, -12
.cfi_offset r4, -16
add r7, sp, #8
.cfi_def_cfa r7, 8
ldr.w r12, [r1]
ldr.w lr, [r1, #4]
ldrb r1, [r0, #11]
ldr r4, [r0]
subs r3, #1
str.w r12, [r4, r1, lsl #3]
add.w r4, r4, r1, lsl #3
adds r1, #1
ands r1, r3
str.w lr, [r4, #4]
strb r1, [r0, #11]
adds r1, r2, #1
strh r1, [r0, #8]
movs r0, #0
pop {r4, r6, r7, pc}
.cfi_endproc
@ -- End function

添加:

在对他的 answer 的评论中与 Lundin 进行了一些讨论之后,我的印象是可能会导致重新读取,因为编译器会假设 queue_ptr->queue可能指向 *queue_ptr本身。所以我改变了 queue_t包含数组而不是指针的结构:

typedef struct
{
event_t queue[256]; // changed from pointer to array with max size
size_t size;
uint16_t num_of_items;
uint8_t rd_idx;
uint8_t wr_idx;
} queue_t;

但是重读仍然和以前一样。我仍然不明白是什么让编译器认为 queue_t字段可能会被修改,因此需要重新读取...以下声明消除了重新读取:

int queue_enqueue(queue_t * restrict const queue_ptr, const event_t *const event_ptr)

但是为什么queue_ptr必须声明为 restrict防止我不理解的重读的指针(除非它是编译器优化“错误”)。

附言

我也无法在 clang 上找到任何不会导致编译器崩溃的文件/报告问题的链接...

最佳答案

[说说原程序]

这是由 Clang 生成的 TBAA 元数据中的缺陷引起的。

如果您使用 -S -emit-llvm 发射 LLVM IR,您将看到(为简洁起见被截断):

...

%9 = load i8, i8* %wr_idx, align 1, !tbaa !12
%10 = trunc i32 %8 to i8
%11 = add i8 %10, -1
%conv4 = and i8 %11, %9
store i8 %conv4, i8* %wr_idx, align 1, !tbaa !12
br label %return

...

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 1, !"min_enum_size", i32 4}
!2 = !{!"clang version 10.0.0 (/home/chill/src/llvm-project 07da145039e1a6a688fb2ac2035b7c062cc9d47d)"}
!3 = !{!4, !9, i64 8}
!4 = !{!"queue", !5, i64 0, !8, i64 4, !9, i64 8, !6, i64 10, !6, i64 11}
!5 = !{!"any pointer", !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!"int", !6, i64 0}
!9 = !{!"short", !6, i64 0}
!10 = !{!4, !8, i64 4}
!11 = !{!4, !5, i64 0}
!12 = !{!4, !6, i64 11}

查看 TBAA 元数据 !4:这是 queue_t 的类型描述符(顺便说一句,我为结构添加了名称,例如 typedef struct queue ... ) 你可能会在那里看到空字符串)。描述中的每个元素都对应struct字段,看!5,就是event_t *queue字段:是“任意指针”!在这一点上,我们已经丢失了有关指针实际类型的所有信息,这告诉我编译器会假定通过此指针进行的写入可以修改任何内存对象。

也就是说,TBAA 元数据有一种新形式,它更精确(仍然有不足之处,但稍后......)

-Xclang -new-struct-path-tbaa编译原程序。我的确切命令行是(自从开发构建没有 libc 以来,我已经包含了 stddef.h 而不是 stdlib.h):

./bin/clang -I lib/clang/10.0.0/include/ -target armv7m-eabi -O2 -Xclang -new-struct-path-tbaa  -S queue.c

生成的程序集是(同样,剪掉了一些绒毛):

queue_enqueue:
push {r4, r5, r6, r7, lr}
add r7, sp, #12
str r11, [sp, #-4]!
ldrh r3, [r0, #8]
ldr.w r12, [r0, #4]
cmp r12, r3
bne .LBB0_2
movs r0, #1
ldr r11, [sp], #4
pop {r4, r5, r6, r7, pc}
.LBB0_2:
ldrb r2, [r0, #11] ; load `wr_idx`
ldr.w lr, [r0] ; load `queue` member
ldrd r6, r1, [r1] ; load data pointed to by `event_ptr`
add.w r5, lr, r2, lsl #3 ; compute address to store the event
str r1, [r5, #4] ; store `param`
adds r1, r3, #1 ; increment `num_of_items`
adds r4, r2, #1 ; increment `wr_idx`
str.w r6, [lr, r2, lsl #3] ; store `event_type`
strh r1, [r0, #8] ; store new value for `num_of_items`
sub.w r1, r12, #1 ; compute size mask
ands r1, r4 ; bitwise and size mask with `wr_idx`
strb r1, [r0, #11] ; store new value for `wr_idx`
movs r0, #0
ldr r11, [sp], #4
pop {r4, r5, r6, r7, pc}

看起来不错,不是吗? :D

我之前提到“新结构路径”存在缺陷,但为此:在邮件列表中。

附言。恐怕在这种情况下没有一般的教训可以吸取。原则上,能够提供给编译器的信息越多越好:比如 restrict、强类型(不是无端强制转换、类型双关等)、相关函数和变量属性……但是不是在这种情况下,原始程序已经包含所有必要的信息。这只是一个编译器缺陷,解决这些问题的最佳方法是提高认识:在邮件列表中询问和/或提交错误报告。

关于c - Clang 是否误解了 'const' 指针说明符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58407841/

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