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

    <tr>
        <th>Summary</th>
        <td>
            clang-20.0.0 generates invalid code when uploading to gpu
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Attached are two files, main.cpp and mdspan.h. (in *.txt format. please rename main.txt to main.cpp and mdspan.txt to mdspan.h)

mdspan.h is somewhat an extension for the c++23 mdspan class, just that it works with extents on the heap as well as on the stack, and that it contains code for gpu offloading and some mathematical algorithms (strassen algorithm, lu, cholesky and qr decomposition).

It has a membervariable, called datastruct, that can be mapped to gpu.

It also contains various mathematical functions. For example, for matrix multiplication and the lu decomposition.

The matrix multiplication has flags for gpu offload. if that is set to true, mdspans datastruct object is extracted and offloaded, where the matrices are multiplied. 

In the main program in main.cpp, two matrices are multiplied on gpu, and that works if compiled with clang-20.0.0.

Now uncomment the commented out lines in main.cpp.

They create a third mdspan object, with totally different data. and then it makes an lu decomposition which I know is possible for this data. A matrix multiplication is involved there. 

But the flags of the lu decomposition are set such that it (and the multiplication) wont use the gpu.
If used alone, the lu decomposition works.

But together with the 2 calls to matrix multiply on gpu, the program will crash with the following note:

ordinary matrix multiplication, on gpu
"PluginInterface" error: Faliure to copy data from device to host. Pointers: host = 0x00007ffd7e0ec408, device = 0x00007f8647801028, size = 1: Error in cuMemcpyDtoHAsync: an illegal memory access was encountered
omptarget error: Copying data from device failed.
omptarget error: Call to targetDataEnd failed, abort target.
omptarget error: Failed to process data after launching the kernel.
omptarget error: Consult https://openmp.llvm.org/design/Runtimes.html for debugging options.

All this that does not make much sense. 

How can a program offloading a calculation on gpu fail if i add a function that works on cpu which has nothing to do with the gpu functions?

Also, when compiling, clang puts out the following warnings: (and takes a very loong time to compile that small file)

100%] Linking CXX executable arraytest
/usr/bin/cmake -E cmake_link_script CMakeFiles/arraytest.dir/link.txt --verbose=1
nvlink warning : Stack size for entry function '__omp_offloading_10306_1944f12__Z16lu_decompositionIdSt6vectorImSaImEEEvR6mdspanIT_T0_ES7_S7_32matrix_multiplication_parametersmbbm_l3121' cannot be statically determined
nvlink warning : Stack size for entry function '__omp_offloading_10306_1944f12__Z19matrix_multiply_dotIdSt6vectorImSaImEES2_S2_EbR6mdspanIT_T0_ERS3_IS4_T1_ERS3_IS4_T2_Ebmm_l3473' cannot be statically determined
nvlink warning : Stack size for entry function '__omp_offloading_10306_1944f12__Z19matrix_multiply_dotIdSt6vectorImSaImEES2_S2_EbR6mdspanIT_T0_ERS3_IS4_T1_ERS3_IS4_T2_Ebmm_l3432' cannot be statically determined
 


Such warnings are, according to a google search, printed if an algorithm contains recursion. But there is no recursion on matrix multiply or on the lu decomposition.... 

What the lu decomposition has, however, is that it allocates a matrix which is often larger than the array where it works on. But, in that case, this is done on cpu. 

The matrix multiplication function has a tile option. then it just uploads submatrices to gpu. If that flag is set on, then the matrix multiplication will fail on its own. It maybe that the device mapper insists that the entire array is copied before it can copy it back? But that does not make too much sense for very large data. 

Also, in the above example, the lu decomposition does not copy to gpu at all. 

How can a working gpu function fail if i just add code that works solely on cpu with entirely different data when this function is known to work successfully on its own?


[main.txt](https://github.com/user-attachments/files/18392692/main.txt)

[mdspan.txt](https://github.com/user-attachments/files/18392693/mdspan.txt)





</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcWE9v4zjy_TTKpRBBphw7Pvjg7sQYA7_5YdBpYAd7EUpUSeKEIrUkZcfz6RdFSbYzSRZ72L0sOmgbEln_-OrVo9F71RiibfLwLXl4usMhtNZtSy_bQf_5uLgrbXXe7kJA2VIF6AjCyUKtNPlEfIcOlUll3wOaCrrK92jSNoVEPCoDidil4S1AbV2HIYVeE3oCRwY7Grfy62A_NTO_mowmYpNkuyTbzQ9AefC2o1OLAdA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