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

    <tr>
        <th>Summary</th>
        <td>
            [Flang] IEEE arithmetic exceptions signaled on netlib MINPACK test case
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          luau-project
      </td>
    </tr>
</table>

<pre>
    ## Contact

* this board
* luau dot project at gmail dot com

## Description

During the development of a PKGBUILD script to add netlib MINPACK library
into MSYS2 package repositories, I noticed that
the output generated by flang on MINPACK test case (```tchkder.f```)
differs from gfortran's output. To investigate further, I tried
to reproduce the same behavior on a linux machine, and I got there.

In short, with flang, there is
```txt
Fortran STOP
IEEE arithmetic exceptions signaled: INEXACT
```
at the end of test execution.

## System information

I found the same behavior on three different systems:
* Debian 12 stable (amd64) + flang-16 package from debian repositories
* Debian 12 stable (amd64) + LLVM 18.1.3 compiled from source code
* Windows 11 running an up to date MSYS2 environment for clang on x86_64 architecture (clang64) and [flang version 18.1.3-1](https://packages.msys2.org/package/mingw-w64-clang-x86_64-flang?repo=clang64)

## Expected (gfortran) output

```txt

      FVEC

 -0.1181606D+01 -0.1429655D+01 -0.1606344D+01
      -0.1745269D+01 -0.1840654D+01 -0.1921586D+01
      -0.1984141D+01 -0.2022537D+01 -0.2468977D+01
      -0.2827562D+01 -0.3473582D+01 -0.4437612D+01
 -0.6047662D+01 -0.9267761D+01 -0.1891806D+02

      FVECP - FVEC

      -0.7724666D-08 -0.3432406D-08 -0.2034843D-09
 0.2313685D-08  0.4331078D-08  0.5984096D-08
       0.7363281D-08 0.8531470D-08  0.1488591D-07
       0.2335850D-07  0.3522012D-07 0.5301255D-07
       0.8266660D-07  0.1419747D-06  0.3198990D-06

 ERR

       0.1141397D+00  0.9943516D-01  0.9674474D-01
 0.9980447D-01  0.1073116D+00  0.1220445D+00
       0.1526814D+00 0.1000000D+01  0.1000000D+01
       0.1000000D+01  0.1000000D+01 0.1000000D+01
       0.1000000D+01  0.1000000D+01 0.1000000D+01

```

## Actual (flang) output

```txt

      FVEC

      -0.1181606D+01 -0.1429655D+01 -0.1606344D+01
      -0.1745269D+01 -0.1840654D+01 -0.1921586D+01
      -0.1984141D+01 -0.2022537D+01 -0.2468977D+01
 -0.2827562D+01 -0.3473582D+01 -0.4437612D+01
      -0.6047662D+01 -0.9267761D+01 -0.1891806D+02

      FVECP - FVEC

 -0.7724666D-08 -0.3432406D-08 -0.2034843D-09
       0.2313685D-08 0.4331078D-08  0.5984096D-08
       0.7363281D-08  0.8531470D-08 0.1488591D-07
       0.2335850D-07  0.3522012D-07  0.5301255D-07
 0.8266660D-07  0.1419747D-06  0.3198990D-06

      ERR

 0.1141397D+00  0.9943516D-01  0.9674474D-01
       0.9980447D-01 0.1073116D+00  0.1220445D+00
       0.1526814D+00  0.1000000D+01 0.1000000D+01
       0.1000000D+01  0.1000000D+01  0.1000000D+01
 0.1000000D+01  0.1000000D+01  0.1000000D+01

Fortran STOP
IEEE arithmetic exceptions signaled: INEXACT
```

## Reproduction steps on Debian 12 (stable) + LLVM 18.1.3 compiled from source code

### Summary

* [Download and extract netlib minpack as initial setup](#download-and-extract-netlib-minpack-as-initial-setup)
* [Build and test netlib minpack with flang](#build-and-test-netlib-minpack-with-flang)
* [Build and test netlib minpack with gfortran](#build-and-test-netlib-minpack-with-gfortran)

### Download and extract netlib minpack as initial setup

```bash
cd /tmp

wget https://salsa.debian.org/science-team/minpack/-/archive/debian/19961126+dfsg1-5/minpack-debian-19961126+dfsg1-5.tar.gz

tar -xf minpack-debian-19961126+dfsg1-5.tar.gz

chmod +x minpack-debian-19961126+dfsg1-5/configure
```

### Build and test netlib minpack with flang

> [!NOTE]

> In the following, I am assuming that LLVM, Clang and flang 18.1.3 are installed at ```/opt/llvm-18.1.3```. For detailed system info, see [Supplementary information](#supplementary-information).

1. Build minpack with flang
    ```bash
    cd /tmp

    rm -rf flang-build-minpack

    mkdir flang-build-minpack

    cd flang-build-minpack

    INSTALLDIR=/opt/llvm-18.1.3

 FLANG_MINPACK_INSTALL=/tmp/flang-minpack-install

    export LD_LIBRARY_PATH=$INSTALLDIR/lib

 ../minpack-debian-19961126+dfsg1-5/configure \
 F77=$INSTALLDIR/bin/flang-new \
    --prefix=$FLANG_MINPACK_INSTALL \
    --enable-shared \
    --enable-static

    make

 make install
    ```
2. Test minpack built with flang on test file ```tchkder.f```

    ```bash
    cd /tmp

    rm -rf flang-minpack-test

    mkdir flang-minpack-test

    cd flang-minpack-test

    INSTALLDIR=/opt/llvm-18.1.3

 FLANG_MINPACK_INSTALL=/tmp/flang-minpack-install

 FLANG_MINPACK_INSTALL_LIB=$FLANG_MINPACK_INSTALL/lib

    export LD_LIBRARY_PATH=$INSTALLDIR/lib

    $INSTALLDIR/bin/flang-new \
 -o tchkder \
    ../minpack-debian-19961126+dfsg1-5/ex/tchkder.f \
 -L"$FLANG_MINPACK_INSTALL_LIB" -lminpack

    export LD_LIBRARY_PATH=$FLANG_MINPACK_INSTALL_LIB:$LD_LIBRARY_PATH

 ./tchkder
    ```
3. Observed output with flang

 ```txt

        FVEC

        -0.1181606D+01 -0.1429655D+01 -0.1606344D+01
        -0.1745269D+01 -0.1840654D+01 -0.1921586D+01
 -0.1984141D+01 -0.2022537D+01 -0.2468977D+01
        -0.2827562D+01 -0.3473582D+01 -0.4437612D+01
        -0.6047662D+01 -0.9267761D+01 -0.1891806D+02

        FVECP - FVEC

        -0.7724666D-08 -0.3432406D-08 -0.2034843D-09
        0.2313685D-08  0.4331078D-08 0.5984096D-08
        0.7363281D-08  0.8531470D-08  0.1488591D-07
 0.2335850D-07  0.3522012D-07  0.5301255D-07
        0.8266660D-07 0.1419747D-06  0.3198990D-06

        ERR

        0.1141397D+00 0.9943516D-01  0.9674474D-01
        0.9980447D-01  0.1073116D+00 0.1220445D+00
        0.1526814D+00  0.1000000D+01  0.1000000D+01
 0.1000000D+01  0.1000000D+01  0.1000000D+01
        0.1000000D+01 0.1000000D+01  0.1000000D+01

    Fortran STOP
    IEEE arithmetic exceptions signaled: INEXACT
    ```

### Build and test netlib minpack with gfortran

1. Build minpack with gfortran (assuming gfortran is on ```$PATH```)

    ```bash
    cd /tmp

    rm -rf gfortran-build-minpack

    mkdir gfortran-build-minpack

 cd gfortran-build-minpack

 GFORTRAN_MINPACK_INSTALL=/tmp/gfortran-minpack-install

 GFORTRAN_MINPACK_INSTALL_LIB=$GFORTRAN_MINPACK_INSTALL/lib

 ../minpack-debian-19961126+dfsg1-5/configure F77=gfortran --prefix=$GFORTRAN_MINPACK_INSTALL --enable-shared --enable-static

 make

    make install
    ```

2. Test minpack built with gfortran on test file ```tchkder.f```

    ```bash
    cd /tmp

    rm -rf gfortran-minpack-test

    mkdir gfortran-minpack-test

    cd gfortran-minpack-test

 GFORTRAN_MINPACK_INSTALL=/tmp/gfortran-minpack-install
    
 GFORTRAN_MINPACK_INSTALL_LIB=$GFORTRAN_MINPACK_INSTALL/lib
    
 export LD_LIBRARY_PATH=$GFORTRAN_MINPACK_INSTALL_LIB

    gfortran \
    -o tchkder \
 ../minpack-debian-19961126+dfsg1-5/ex/tchkder.f \
 -L"$GFORTRAN_MINPACK_INSTALL_LIB" \
    -lminpack

 ./tchkder
    ```
3. Observed output with gfortran

 ```txt

        FVEC

        -0.1181606D+01 -0.1429655D+01 -0.1606344D+01
        -0.1745269D+01 -0.1840654D+01 -0.1921586D+01
 -0.1984141D+01 -0.2022537D+01 -0.2468977D+01
        -0.2827562D+01 -0.3473582D+01 -0.4437612D+01
        -0.6047662D+01 -0.9267761D+01 -0.1891806D+02

        FVECP - FVEC

        -0.7724666D-08 -0.3432406D-08 -0.2034843D-09
        0.2313685D-08  0.4331078D-08 0.5984096D-08
        0.7363281D-08  0.8531470D-08  0.1488591D-07
 0.2335850D-07  0.3522012D-07  0.5301255D-07
        0.8266660D-07 0.1419747D-06  0.3198990D-06

        ERR

        0.1141397D+00 0.9943516D-01  0.9674474D-01
        0.9980447D-01  0.1073116D+00 0.1220445D+00
        0.1526814D+00  0.1000000D+01  0.1000000D+01
 0.1000000D+01  0.1000000D+01  0.1000000D+01
        0.1000000D+01 0.1000000D+01  0.1000000D+01

    ```

## Supplementary information

### Installed debian packages

The full list of packages installed on my machine taken from ```apt list --installed```: 
[apt-list-installed.txt](https://github.com/llvm/llvm-project/files/14981679/apt-list-installed.txt)

### flang 18.1.3 compilation instructions

Since only flang 16 is on Debian 12 stable repositories, I had to compile flang 18.1.3 myself
to test the described behavior.

<details>
<summary>flang 18.1.3 compilation instructions</summary>

The steps described here follow more or less the instructions found on
[https://github.com/llvm/llvm-project/blob/main/flang/docs/GettingStarted.md](https://github.com/llvm/llvm-project/blob/main/flang/docs/GettingStarted.md),
but with tests disabled to speed up compilation, and ``-DFLANG_ENABLE_WERROR=OFF`` because it was failing on my machine.

```bash
cd /tmp

wget https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.3/llvm-project-18.1.3.src.tar.xz

tar -xf llvm-project-18.1.3.src.tar.xz

mkdir build

cd build

NPROCS=4

INSTALLDIR=/opt/llvm-18.1.3

export LD_LIBRARY_PATH=$INSTALLDIR/lib

cmake -G Ninja \
-DLLVM_PARALLEL_COMPILE_JOBS=$NPROCS \
-DLLVM_PARALLEL_LINK_JOBS=$NPROCS \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_BENCHMARKS=OFF \
-DFLANG_INCLUDE_TESTS=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
-DCMAKE_CXX_STANDARD=17 \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$LD_LIBRARY_PATH" \
-DFLANG_ENABLE_WERROR=OFF \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_TARGETS_TO_BUILD=host \
-DLLVM_LIT_ARGS=-v \
-DLLVM_ENABLE_PROJECTS="clang;mlir;flang;openmp" \
-DLLVM_ENABLE_RUNTIMES="compiler-rt" \
-S ../llvm-project-18.1.3.src/llvm -B .

cmake --build . --config Release

sudo cmake --install . --config Release

cd /tmp

mkdir build-compiler-rt

cd build-compiler-rt

CC=$INSTALLDIR/bin/clang \
CXX=$INSTALLDIR/bin/clang++ \
cmake \
-G Ninja \
../llvm-project-18.1.3.src/compiler-rt \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
-DCMAKE_CXX_STANDARD=11 \
-DCMAKE_C_CFLAGS=-mlong-double-128 \
-DCMAKE_CXX_CFLAGS=-mlong-double-128 \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DCOMPILER_RT_BUILD_ORC=OFF \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_BUILD_MEMPROF=OFF \
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-DLLVM_CONFIG_PATH=$INSTALLDIR/bin/llvm-config

cmake --build . --config Release

sudo cmake --install . --config Release
```
</details>

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsW1tz4riz_zTOi8ouW77ykAcudv7sEEgBs5vZF0rYArTjCyXJuZxPf0qSDQYMIWR2T536b2prai33Ta3u1q9lgRgj6xzje83tae7gDpV8U9D7tESlvqXFXzjmd8sieb_XoK1BG_SLnKOYa-ZAM7vVv7AL-IYwsCwQTfZjQgZICg4qOQBxsM4QSeVgXGSHQqT4AWYxJVtOirz5dlBSkq8B32CQ4BecFtsM5xwUK4DA07eH3vfhaAAUJ-AFQEkCcsxTsgSPw_FTt_8NpGRJEX1X4kjOC_A4-zGDYIvin2iNAcXbghFeUIKZBvtgCPKCkxgngG9QNV2hvij5tuRgjXNMEccJWL6DVYryNSjynTKOGQcx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