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

    <tr>
        <th>Summary</th>
        <td>
            [flang] Consistent MOD/MODULO lowering
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang:frontend
      </td>
    </tr>

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

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

<pre>
    Flang is currently lowering `MOD` to the runtime call, and `MODULO` to an inline implementation.  So for `MOD` there is a useful diagnostics about `P` being equal to 0, while for `MODULO` there is none.

I think we should lower them both to the runtime calls under `-O0`, and optimize them after lowering if optimizations are enabled (e.g. in `SimplifyIntrinsicsPass` or via LTO-like optimizations of the user code + Fortran runtime).

Reproducer:
```
program main
  real :: x, y
  x = 1.0
  y = 0.0
  call test_modulo(x, y)
  call test_mod(x, y)
contains
  subroutine test_mod(x, y)
    real :: x, y
 print *, 'MOD: ', mod(x, y)
  end subroutine test_mod
  subroutine test_modulo(x, y)
    real :: x, y
    print *, 'MODULO: ', modulo(x, y)
  end subroutine test_modulo
end program main
```

```
$ flang-new mod.f90 -flang-experimental-exec
$ ./a.out
 MODULO: NaN

fatal Fortran runtime error(/mod.f90:10): MOD with P==0
 MOD: Aborted (core dumped)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VF1v2zgQ_DX0y8ICTdqK9KAHt4aAAr0muN49HyhxJfFCkTp-xHF__YGSHaepXcDwx3J2dma8ovBe9QaxIrtPZHdYiRgG66qXH-J5EE6tGitPVa2F6UF5aKNzaII-gbZHdMr0QHL6x-OB5BSChTAguGiCGhFaoTVhn0EYeQb9_fXxjBMGlNHKIKhx0jiiCSIoazKA7xY6697TDugwDRcQPXZRg1SiN9YH1XoQjY0hoZ8StsEkCf-LQqcxNM0_DkrjO86LigutsQYzQg-E7pf3LxAGZZ7hiOAHG7VczKaOERobhltOPUQjcZ6xfqQkpxfrdgpqVD9waRddQHcNT3WX89m-B-EQ0IhGowTCCsz6DJRJrN9TUqo7fTHBKeNV65-E98mJdfCiBHz963Gt1TN-YLTdrDV6dNBaiUDYJ6itC06YiwPCyp8S-BMnZ2Vs0RF-LiVHy2v-OTnbOzHCKJRZKgAOhYbUwPfwmuyfLievQPgBNhm9FE5zgV4LKUII6MM_o5VRW8KKMwUrb2J-BbTWBKGMv8B9bJyNIS3Z_SaA3-ienDIBCNunImEPaSH5Pn1LhTt8aOTN0fdV3bH7u0Dhlra02D_Ju0N8R2FCz4h0_uv_-2EBbhfZFrp0V6wNHpOCrCsprJcSvk7o1Pyo6zW-YnvtyQirRWZjOEu8mvkmvr2f14kg9MftBXTOOsIKwurzTML3G5os85kMjioM8ET4gfADvQ5Jx_vGurA8ba11CDKOE8q3uN4crmTFZclLscJqkz_sNoxTWqyGarMp8lyUjWSc50Uhm3L7sGvoVpYbbB5yuVIVo4zTDaeM7_JtkbW7HS-6piu5yCl2nGwpjkLpTOuXMbOuXynvI1Y55WW-0qJB7ef7mbE5SsL3nbMmoJGEsXRruyq1rpvYe7KlWvngr2RBBT3f70vz7gCfrfHKBzRhToHVS-Bv99IqOl0NIUw-bR-rCat7FYbYZK0dCasT9fljPTn7L7aBsHoW7QmrZ93_BwAA___usu0V">