当我在this question中提出时,gcc正在移除(是的,使用 -O0 )一行代码 _mm_div_ss(s1, s2); ,大概是因为结果未保存 . 但是,这应该触发浮点异常并引发SIGFPE,如果删除调用则不会发生这种情况 .
Question :是否有一个标志或多个标志传递给gcc,以便代码按原样编译?我正在考虑像 fno-remove-unused 这样的东西,但是我支持使用gcc属性/ pragma代替吗?
我尝试过的事情:
$ gcc --help=optimizers | grep -i remove
没有结果 .
$ gcc --help=optimizers | grep -i unused
没有结果 .
并明确禁用所有死代码/消除标志 - 请注意,没有关于未使用代码的警告:
$ gcc -O0 -msse2 -Wall -Wextra -pedantic -Winline \
-fno-dce -fno-dse -fno-tree-dce \
-fno-tree-dse -fno-tree-fre -fno-compare-elim -fno-gcse \
-fno-gcse-after-reload -fno-gcse-las -fno-rerun-cse-after-loop \
-fno-tree-builtin-call-dce -fno-tree-cselim a.c
a.c: In function ‘main’:
a.c:25:5: warning: ISO C90 forbids mixed declarations and code [-Wpedantic]
__m128 s1, s2;
^
$
来源计划
#include
#include
#include
#include
static void sigaction_sfpe(int signal, siginfo_t *si, void *arg)
{
printf("%d,%d,%d\n", signal, si!=NULL?1:0, arg!=NULL?1:0);
printf("inside SIGFPE handler\nexit now.\n");
exit(1);
}
int main()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = sigaction_sfpe;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGFPE, &sa, NULL);
_mm_setcsr(0x00001D80);
__m128 s1, s2;
s1 = _mm_set_ps(1.0, 1.0, 1.0, 1.0);
s2 = _mm_set_ps(0.0, 0.0, 0.0, 0.0);
_mm_div_ss(s1, s2);
printf("done (no error).\n");
return 0;
}
编译上面的程序给出了
$ ./a.out
done (no error).
换线
_mm_div_ss(s1, s2);
至
s2 = _mm_div_ss(s1, s2); // add "s2 = "
产生预期的结果:
$ ./a.out
inside SIGFPE handler
编辑更多细节 .
这似乎与 _mm_div_ss definition上的 __always_inline__ 属性有关 .
$ cat t.c
int
div(int b)
{
return 1/b;
}
int main()
{
div(0);
return 0;
}
$ gcc -O0 -Wall -Wextra -pedantic -Winline t.c -o t.out
$
(没有警告或错误)
$ ./t.out
Floating point exception
$
vs下面(功能属性除外)
$ cat t.c
__inline int __attribute__((__always_inline__))
div(int b)
{
return 1/b;
}
int main()
{
div(0);
return 0;
}
$ gcc -O0 -Wall -Wextra -pedantic -Winline t.c -o t.out
$
(没有警告或错误)
$ ./t.out
$
添加函数属性 __warn_unused_result__ 至少会给出一条有用的消息:
$ gcc -O0 -Wall -Wextra -pedantic -Winline t.c -o t.out
t.c: In function ‘main’:
t.c:9:5: warning: ignoring return value of ‘div’, declared with attribute warn_unused_result [-Wunused-result]
div(0);
^
编辑:
关于gcc mailing list的一些讨论 . 最终,我认为一切都按预期工作 .