llvm-project/flang/lib/Testing/fp-testing.cpp
Michael Kruse 34ca553d30
[Flang/Flang-RT] Fix OldUnit tests on Windows (#150734)
Flang and Flang-RT have two flavours of unittests: 
1. GTest unittests, using lit's `lit.formats.GoogleTest` format ending
with `Tests${CMAKE_EXECUTABLE_SUFFIX}`
2. "non-GTest" or "OldUnit" unittests, a plain executable ending with
`.test${CMAKE_EXECUTABLE_SUFFIX}`

Both executables are emitted into the same unittests/ subdirectory. When
running ...
1. `tests/Unit/lit.cfg.py`, only considers executable ending with
`Tests` (or `Tests.exe` on Windows), hence skips the non-GTest tests.
2. `tests/NonGtestUnit/lit.cfg.py` considers all tests ending with
`.test` or `.exe`. On Windows, The GTest unitests also end with `.exe`.

In Flang-RT, `.exe` is considered an extension for non-GTest unitests
which causes tests such as Flang's `RuntimeTests.exe` to be executed for
both on Windows. This particular test includes a file write test, using
a hard-coded filename `ucsfile`. If the two instances are executed
concurrently, they might interfere with each other reading/writing
`ucsfile` which results in a flaky test.

This patch avoids the redundant execution by requiring the suffix
`.test.exe` on Windows. lit has to be modified because it uses
`os.path.splitext` the extract the extension, which would only recognize
the last component. It was changed from the orginal `endswith` in
c865abe747aa72192f02ebfdcabe730f2553e42f
for unknown reasons.

In Flang, `.exe` is not considered a suffix for non-GTest unittests and
hence they are not run at all. Fixing by also added `.test.exe` as valid
suffix, like with Flang-RT.

Unfortunately, the ` Evaluate/real.test.exe` test was failing on
Windows:
```
FAIL: flang-OldUnit :: Evaluate/real.test.exe (3592 of 3592)
******************** TEST 'flang-OldUnit :: Evaluate/real.test.exe' FAILED ********************
..\_src\flang\unittests\Evaluate\real.cpp:511: FAIL: FlagsToBits(prod.flags) == 0x18, not 0x10
        0 0x800001 * 0xbf7ffffe
..\_src\flang\unittests\Evaluate\real.cpp:511: FAIL: FlagsToBits(prod.flags) == 0x18, not 0x10
        0 0x800001 * 0x3f7ffffe
..\_src\flang\unittests\Evaluate\real.cpp:511: FAIL: FlagsToBits(prod.flags) == 0x18, not 0x10
        0 0x80800001 * 0xbf7ffffe
..\_src\flang\unittests\Evaluate\real.cpp:511: FAIL: FlagsToBits(prod.flags) == 0x18, not 0x10
        0 0x80800001 * 0x3f7ffffe
...
```
This is due to the `__x86_64__` macro not being set by Microsoft's
cl.exe and hence floating point status flags not being read out. The
equivalent macro for Microsofts compiler is `_M_X64` (or `_M_X64`).
2025-07-26 23:47:36 +02:00

125 lines
3.1 KiB
C++

//===-- lib/Testing/fp-testing.cpp ------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Testing/fp-testing.h"
#include "llvm/Support/Errno.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#if __x86_64__ || _M_X64
#include <xmmintrin.h>
#endif
using Fortran::common::RealFlag;
using Fortran::common::RoundingMode;
ScopedHostFloatingPointEnvironment::ScopedHostFloatingPointEnvironment(
#if __x86_64__ || _M_X64
bool treatSubnormalOperandsAsZero, bool flushSubnormalResultsToZero
#else
bool, bool
#endif
) {
errno = 0;
if (feholdexcept(&originalFenv_) != 0) {
std::fprintf(stderr, "feholdexcept() failed: %s\n",
llvm::sys::StrError(errno).c_str());
std::abort();
}
fenv_t currentFenv;
if (fegetenv(&currentFenv) != 0) {
std::fprintf(
stderr, "fegetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
#if __x86_64__ || _M_X64
originalMxcsr = _mm_getcsr();
unsigned int currentMxcsr{originalMxcsr};
if (treatSubnormalOperandsAsZero) {
currentMxcsr |= 0x0040;
} else {
currentMxcsr &= ~0x0040;
}
if (flushSubnormalResultsToZero) {
currentMxcsr |= 0x8000;
} else {
currentMxcsr &= ~0x8000;
}
#else
// TODO others
#endif
errno = 0;
if (fesetenv(&currentFenv) != 0) {
std::fprintf(
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
#if __x86_64__
_mm_setcsr(currentMxcsr);
#endif
}
ScopedHostFloatingPointEnvironment::~ScopedHostFloatingPointEnvironment() {
errno = 0;
if (fesetenv(&originalFenv_) != 0) {
std::fprintf(
stderr, "fesetenv() failed: %s\n", llvm::sys::StrError(errno).c_str());
std::abort();
}
#if __x86_64__ || _M_X64
_mm_setcsr(originalMxcsr);
#endif
}
void ScopedHostFloatingPointEnvironment::ClearFlags() const {
feclearexcept(FE_ALL_EXCEPT);
}
RealFlags ScopedHostFloatingPointEnvironment::CurrentFlags() {
int exceptions = fetestexcept(FE_ALL_EXCEPT);
RealFlags flags;
if (exceptions & FE_INVALID) {
flags.set(RealFlag::InvalidArgument);
}
if (exceptions & FE_DIVBYZERO) {
flags.set(RealFlag::DivideByZero);
}
if (exceptions & FE_OVERFLOW) {
flags.set(RealFlag::Overflow);
}
if (exceptions & FE_UNDERFLOW) {
flags.set(RealFlag::Underflow);
}
if (exceptions & FE_INEXACT) {
flags.set(RealFlag::Inexact);
}
return flags;
}
void ScopedHostFloatingPointEnvironment::SetRounding(Rounding rounding) {
switch (rounding.mode) {
case RoundingMode::TiesToEven:
fesetround(FE_TONEAREST);
break;
case RoundingMode::ToZero:
fesetround(FE_TOWARDZERO);
break;
case RoundingMode::Up:
fesetround(FE_UPWARD);
break;
case RoundingMode::Down:
fesetround(FE_DOWNWARD);
break;
case RoundingMode::TiesAwayFromZero:
std::fprintf(stderr, "SetRounding: TiesAwayFromZero not available");
std::abort();
break;
}
}