<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/116079>116079</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [NVPTX] shared memory issue between threads
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          AlexTru96
      </td>
    </tr>
</table>

<pre>
    Hello, while using nvptx64-nvidia-cuda as target and taking advantage of the recent libc for gpu feature I found the following issue

    #include <stdlib.h>
    void print_array(float *a, const int N, char *d);

    int main (int argc, char **argv, char **envp) {

    float shared[3];
    int tid=__nvvm_read_ptx_sreg_tid_x();

    if(tid == 0) {shared[0]=1.0;shared[1]=2.0;shared[2]=3.0;}
    if(tid<3){
 printf('%f check vector [%d] \n', shared[tid]);
 print_array(shared, 3, 'shared-vector');
         __syncthreads();
 return 0;
    }
    };

    void print_array(float *b, const int N, char *d) {
        int i;
        for(i=0; i<N; i++)
 printf("\n%s[%d,%d]: %f\n",d, i,__nvvm_read_ptx_sreg_tid_x(), b[i]);
    }

after compiling with 
`clang++ main.cpp --target=nvptx64-nvidia-cuda  -O3 -march=native -lc -lm /path-to/llvm-20.0_devel/lib/nvptx64-nvidia-cuda/crt1.o -gpulibc -o test.x`

when i ran it with
`nvptx-loader --threads 3 test.x`

each thread tries to access a different index of the shared vector, the thread 0 is the only one that gets the right value, the rest just 
returns 0, but if we set only fixed indexes all threads return the correct values.
Apart from this problem,  If I do a malloc each thread will allocate a private memory region, is there a way to get a dynamic shared memory allocation between threads?

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVU-Po7gT_TTOpUQEdkg6hxy64Rf95jK7h9Fqb5GxC_CMsZFtSPLtVzZk-t_sTquFoFJ-Ve_5ucy9V51BPJHyhZT1hk-ht-70rPH2zU3H_aax8n76P2ptCa3g2iuNMHllOjDzG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