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

    <tr>
        <th>Summary</th>
        <td>
            [LLVM OpenMP Offload]: Offload fails if I use a function pointer inside a for loop and try to offload the for loop.
        </td>
    </tr>

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

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

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

<pre>
    In this example code, I use a function pointer inside the for loop body and try to offload it but it fails with the following error. 

```
AMDGPU fatal error 1: Memory access fault by GPU 4 (agent 0x5555556435e0) at virtual address 0x555555555000. Reasons: Unknown (0)
Aborted (core dumped)
```

```
#include <stdio.h>
#include <omp.h>
#include <chrono>
#include <iostream>
#include <cassert>

void for_each(int *array, int size, int (*operation)(int));
int complex(int i);

int main() {

  int size = 1e8;
  int *array = (int*) malloc(sizeof(int) * size);
  int val = 100000;

  for(int i = 0; i < size; i++){
       array[i] = val;
  }

  int (*func_ptr)(int) = complex;

  auto start = std::chrono::high_resolution_clock::now();

 for_each(array, size, func_ptr);

  auto end = std::chrono::high_resolution_clock::now();

  std::chrono::duration<double> duration = end - start;
  std::cout << "Execution time: " << duration.count() << " seconds" << std::endl;

  long sum = 0;
  for (long i = 0; i < size; i++){
        assert(array[i] == complex(val) && "Element value not correct.");
        sum += array[i];
  }

 std::cout << "Sum: " << sum << std::endl;


}


int complex(int i){

 int j = 10;
 j = j * 1;
 j = j + 1;
 j = j * 10;
 j = j / 4;
 j = j + 1;

 for(int i = 0; i < 10000; i++){
   j = j * i;
 }



 return j;

}

void for_each(int *array, int size, int (*func_ptr)(int)){


        #pragma omp target teams distribute parallel for map(tofrom: array[0:size])
        for(int i = 0; i < (size/5000); i++){
           for(int j = 0; j < 5000; j++){
 array[i*5000 + j] = func_ptr(array[i*5000 + j]);
           }
 }


}
```
</pre>
<img width="1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