Wrong codegen after r337498
| Bugzilla Link | 38309 |
| Resolution | FIXED |
| Resolved on | Jul 28, 2018 01:57 |
| Version | trunk |
| OS | Linux |
| CC | @aelovikov-intel,@DMG862,@efriedma-quic,@hfinkel,@zygoloid |
| Fixed by commit(s) | rL338192 |
Extended Description
This is a testcase that needs to be compiled with: clang test.c -O3 -fno-vectorize -fno-unroll-loops -o test.axf
static void test(short *Input, short *Output, short InputSize, short OutputSize) { int i; int j;
for (j = 0; j < OutputSize; j++) { int Sum = 0; for (i = 0; i < InputSize - j; i++) Sum += Input[i+j]; Output[j] = Sum; } } void test(short *Input, short *Output, short InputSize, short OutputSize);
static short g_data[] = {16, 16, 16, 16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0}; int main() { short *other = malloc(16 * sizeof(short)); test(g_data, other, 16, 8); printf("%d\n", other[0]); printf("%d\n", other[1]); printf("%d\n", other[2]); printf("%d\n", other[3]); printf("%d\n", other[4]); printf("%d\n", other[5]); printf("%d\n", other[6]); printf("%d\n", other[7]); return 0; }
test.axf should then print: 128 112 96 80 64 48 32 16
The test includes this data: static short g_data[] = {16, 16, 16, 16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0}; Which use to come out of clang as: @g_data = internal global <{ i16, i16, i16, i16, i16, i16, i16, i16, [8 x i16] }> But now after rC337498 looks like: @g_data = internal global <{ [8 x i16], [8 x i16] }> ...
The problem seems to be that after all the inlining, GlobalOpt turns that g_data into @g_data.0 = internal unnamed_addr constant [8 x i16] ... i.e it throws away the second half with the 0's. So we end up reading jibberish.
I'm not sure which part of this is wrong. My understanding is that accessing structs like that in C would be UB, but for IR I think it may be allowed. Which suggests it's GlobalOpt not doing the correct thing splitting up the struct, but that code has been there for a very long time...
Fixed in rL338192.
Thanks. I'll try and put a patch together.
GlobalUsersSafeToSRA in GlobalOpt is supposed to reject non-constant indexes, but it looks like the GEP checks aren't complete.