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

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP][libomptarget][DeviceRTL] Removing ASSERT macro leads to test failures
        </td>
    </tr>

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

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

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

<pre>
    When replacing the ASSERT macro in Debug.h i.e.:
```
#define ASSERT(expr, msg)                                                      \
  { \
    if (config::isDebugMode(config::DebugKind::Assertion) && !(expr)) \
      __assert_fail(#expr, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__);      \
    else \
      __assert_assume(expr); \
  }
```
with an empty assert:
```
#define ASSERT(expr, msg)
```

There are 4 OpenMP tests which fail:
```
  libomptarget :: amdgcn-amd-amdhsa :: jit/empty_kernel_lvl2.c
  libomptarget :: amdgcn-amd-amdhsa :: offloading/cuda_no_devices.c
  libomptarget :: amdgcn-amd-amdhsa :: offloading/std_complex_arithmetic.cpp
  libomptarget :: amdgcn-amd-amdhsa :: offloading/test_libc.cpp
```

Further investigation has revealed that:

Removing the ASSERT in `omp_get_level()` makes the `jit/empty_kernel_lvl2.c` test fail.

Removing the ASSERT in `void *SharedMemorySmartStackTy::push(uint64_t Bytes) {` leads to 3 tests failing:
```
  libomptarget :: amdgcn-amd-amdhsa :: offloading/cuda_no_devices.c
  libomptarget :: amdgcn-amd-amdhsa :: offloading/std_complex_arithmetic.cpp
  libomptarget :: amdgcn-amd-amdhsa :: offloading/test_libc.cpp
```

In addition to the tests above the OpenMP opt pass produces different results based on whether the ASSERTs are present in the DeviceRTL or not.

```
#include <omp.h>
#include <stdio.h>

int main(void) {
  int threads = 0;

#pragma omp target map(from:threads)
  {
    threads = omp_get_num_threads();
 }

  if (threads <= 0) {
    printf("Runtime error, threads=%d\n", threads);
  }
  printf("Success!\n");

  // INFO: 1 blocks and 256 threads in Generic-SPMD mode
  return 0;
}
```

If run with `LIBOMPTARGET_INFO=16` enabled, the launch details will change as follows based on whether the ASSERTs are present in the code or not:
If the ASSERTs are present then the launch info is:
```
"PluginInterface" device 0 info: Launching kernel __omp_offloading_fd00_2448d7a_main_l6 with 1 blocks and 256 threads in Generic mode
AMDGPU device 0 info: #Args: 1 Teams x Thrds:    1x 256 (MaxFlatWorkGroupSize: 1024) LDS Usage: 0B #SGPRs/VGPRs: 4/0 #SGPR/VGPR Spills: 0/0 Tripcount: 0
```

If the ASSERTs are NOT present then the launch info is:
```
"PluginInterface" device 0 info: Launching kernel __omp_offloading_fd00_2448d7a_main_l6 with 1 blocks and 256 threads in Generic-SPMD mode
AMDGPU device 0 info: #Args: 1 Teams x Thrds:    1x 256 (MaxFlatWorkGroupSize: 1024) LDS Usage: 0B #SGPRs/VGPRs: 4/0 #SGPR/VGPR Spills: 0/0 Tripcount: 0
```

Note the Generic VS. Generic-SPMD mode.

The presence of the ASSERTs should not influence the outcome of the optimizer and the OpenMP lit tests should not fail when the ASSERTs are removed.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkV19v6joS_zTmZXRQcCDAAw9QSoW2_1Q492qfImNPEt86dmQ7tL2ffmUHKLSnq7tnz8vurUrAHs_P45mfZybMOVlqxBkZLcho2WOtr4ydCWPbQZIMezsj3ma_V6jBYqMYl7oEXyHMN5vrpy3UjFsDUsMSd23Zr0D2sU_SOUmWJJmTLDn8d0OaCiykPmoTOsHXxhJ6BbUrCZ3CT_2R0VWHD0DGi_MhgCyA0Ak3upBlMCudSxdNvTMCP0ji_D-kFt1w7hxaL40OhhGaEZoBoYOT0dM4f74ZQJ6zqJUXTCpCJ4SmFye8gjxfrW-v87z7fbu-P_1-fLrebv-Zr77fX23XD_dhekrSxaczAqBy-OXOzLm2xjMr08Wlh5Y_jM2L9BUwDVg3_g06rJ-O44-14nNboUVgFmEIDw3qu0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