Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
100 Views

Bug report: ICC 18.0.1 reordering write before read

See the repro below on x86-64 Linux (Ubuntu 17.10) I think the -O2 version is returning the wrong result; in the loop in memswp it appears to be writing to *q before reading from it.

$ cat main.c
#include <stdio.h>
extern void memswp(int *p, int *q, int n);
int main()
{
	__attribute__((aligned(4))) char a[] = {'a','b','c','d','e','f','g','h',0};
	memswp((int *)a, (int *)a + 1, 1);
	puts(a);
	return 0;
}
$ cat move.c
void memswp(int *p, int *q, int n)
{
	for (int i = 0; i < n; i++)
	{
		int t                           = ((struct { int x; } *)p + i)->x;
		((struct { int x; } *)p + i)->x = ((struct { int x; } *)q + i)->x;
		((struct { int x; } *)q + i)->x = t;
	}
}
$ icc -O0 main.c move.c -o swp0 && ./swp0
efghabcd
$ icc -O2 main.c move.c -o swp2 && ./swp2
efghefgh
$ icc --version
icc (ICC) 18.0.1 20171018
Copyright (C) 1985-2017 Intel Corporation.  All rights reserved.
0 Kudos
2 Replies
Highlighted
Moderator
100 Views

 

Hi Jay,

Thanks for reporting this issue. It seems to be a compiler bug. I'll report it to our developer for a fix.

Regards,

Viet

0 Kudos
Highlighted
Moderator
100 Views

I've file this bug as CMPLRS-47689 to track it internally.

Thanks,

Viet

0 Kudos