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

    <tr>
        <th>Summary</th>
        <td>
            Miscompilation at -O2/-O3 when Openmp optimistic attributes are enabled 
        </td>
    </tr>

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

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

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

<pre>
    I believe there is a possible miscompilation when the below C file is compiled when clang
has openmp optimistic attributes enabled at higher optimization levels. 

It looks like an issue due to the omp_get_wtime() getting CSE'd by EarlyCSE pass.


Following information is provided below:

1. Reproducer file
2. command to reproduce bug
3. current output and expected output
4. Some further information

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

void foo()
{
    const double duration_seconds = 5.0;
    const double start = omp_get_wtime();
    double x = 0.5;
    while (omp_get_wtime() - start < duration_seconds) {
        for (int i = 0; i < 100; ++i) x = x*(1.0-x);
    }

    printf("x = %lf\n", x);
}

int main() {
    foo();
    return 0;
}
```

Command to reproduce bug:
```
clang reproducer.c -mllvm -openmp-ir-builder-optimistic-attributes -fno-inline -fopenmp -O2  -o reproducer_exe
time ./reproducer_exe
```

Current output:

```
x = 0.500000

real    0m0.003s
user    0m0.003s
sys     0m0.000s
```


Expected output:
(Note: The time values need not be exact, execution should take around 5 seconds if compiled correctly):

```
x = 0.000000

real    0m5.003s
user    0m2.402s
sys     0m2.601s
```


On printing some IR + debug info, this can be seen.
```
clang reproducer.c -mllvm -openmp-ir-builder-optimistic-attributes -mllvm -debug-only=early-cse -fno-inline -fopenmp -O2  -S -emit-llvm -o -

// some other debug info
EarlyCSE CSE CALL:   %call1 = call double @omp_get_wtime()  to:   %call = call double @omp_get_wtime()
// some other debug info
```

And then a look at the IR shows that the nested loop has been optimized away leaving only the printf call.
The outer loop control condition contains a call to `omp_get_wtime()` which gets CSE'd with another call 
before it (`start = omp_get_wtime()`). This causes the IR to contain a condition like this

```
%sub = fsub double %call, %call
%cmp = fcmp uge double %sub, 5.000000e+00
```
and I suppose that makes the compiler think that the outer loop will never be executed and hence it removes it completely
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1Vt1v4jgQ_2vCyyhRMITQBx5a2kqV9q7S7r1XTmKIdx0b2Q6U--tvxg6UsGx3Xw5BcOz5-M2npzLNcfUClVBS7AX4VlgB0gGHnXFOVkpAJ11tup1U3Euj4dAKTXTEYw6who1UgSUSiSZS1IrrbZI_Jvl9yx2YndDdDv-8RHle1sC9t7LqvXAgNK-IkXto5RYhDHT_Ro0KkSmXQZQWny8elDE_HCj5QwDXCMD1Ahr8eRPgIZq3rfBvB5QkErZM2B3gu5d6C-tvTwkrG6iO8MStOuI77Lhz2aWK-Hw2Cs0kJqk3xnYREpq7s2YvG0Qd_JDMRlzTDL4KpGj6Gq0hD8V9lpGbOq4bgmlPJFD1g69mSNBbK7QH0_td74FoxftO1B51xb1IOs_gm-kEbHpLYbvEdwnlrMRm9bC_yIdvfGUzqWvVNwKS2RrdlrXJ7OnWmfONNJen4bk3soGNMdHHw0n5EBeAn9po56ExPWVT09sA8c0J3G8cyn2EIsuT2a84nOfWB7IbIR2xDQzvgTjPitHhoaU8Ra5biZGetax_QkjnI3vog64mWRLjJKM6VBaWa5jm4SVhD_iVxB4RvScMHbqcZnn6fg09KR8vfUpbO4vSNwEgiwISVqhNUqw17iRsDSMpVxIIWcelHgwcGfARrEsIVvjeashvSLxKmPBc_yqPz5Uw5goNAS6zEdJOqX0HaWwOqbRp1UvVCJt-9In0ok-kG21SqZXUAtdDS0lfGaCIC8lv4n0oN4owZAl7vnV426pR8V1V9RXHOc9y-oxrjqskv8u7PMvzmYubvRP2p013dOe93H0CLD6frhrBGR9b_m28wHf4B3tfsHvPVY9O0wLJtfHYp7CP8NpT5qAT6j50MteaXmEUObVRa3qMaAGn2pSbj65eG_RM7dUx5M2f-CX_tV-KW35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