Incorrect code for statically initialized arrays

This issue was initially reported at test/datatype/external32 failure when building with NVHPC (+ misleading error message) · Issue #9402 · open-mpi/ompi · GitHub

Let’s consider the following data.c source file

> cat data.c 
unsigned long data[8] = {
    [4] = 1,
    [5] = 2,
};

and compile it

$ nvc -S data.c

The generated assembly for the data variable is incorrect

data:   
        .xword  0                               // 0x0
        .xword  1                               // 0x1
        .xword  2                               // 0x2
        .xword  0                               // 0x0
        .xword  0                               // 0x0
        .xword  0                               // 0x0
        .xword  0                               // 0x0
        .xword  0                               // 0x0
        .size   data, 64

the values 1 and 2 are assigned to data[1] and data[2] instead of data[4] and data[5] as requested.

I am able to reproduce this issue with the latest 21.9 compilers (I also tested 21.7 and the issue is also there).

FWIW, here is a full test:

$ nvc -c data.c
$ cat test.c
#include <stdio.h>

extern unsigned long data[8];

int main(int argc, char *argv[]) {
    if (1 == data[4]) {
        printf("OK\n");
    } else {
        printf("KO\n");
    }
    return 0;
}
$ nvc -o test test.c data.o
$ ./test
KO


The same test runs fine with GNU compilers (gcc)

Thanks Gilles. This looks like a regression first occurring in our 21.3 release. I’ve added a problem report, TPR #30644, and sent it to engineering for investigation.

-Mat