[compiler-rt] [compiler-rt][nsan] Add support for nan detection (PR #101531)

Alexander Shaposhnikov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 12:02:52 PDT 2024


================
@@ -0,0 +1,35 @@
+// RUN: %clangxx_nsan -O0 -g -mavx %s -o %t
+// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_nsan -O3 -g -mavx %s -o %t
+// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s
+
+#include <cmath>
+#include <immintrin.h>
+#include <iostream>
+
+void simd_sqrt(const float *input, float *output, size_t size) {
+  size_t i = 0;
+  for (; i + 7 < size; i += 8) {
+    __m256 vec = _mm256_loadu_ps(&input[i]);
+    __m256 result = _mm256_sqrt_ps(vec);
+    _mm256_storeu_ps(&output[i], result);
+    // when simd is used, the warning is not triggered
----------------
alexander-shaposhnikov wrote:

I've simplified the test a bit:
```
#include <cassert>
#include <cmath>
#include <immintrin.h>
#include <iostream>

void simd_sqrt(const float *input, float *output, size_t size) {
  size_t i = 0;
  assert(size % 8 == 0);
  for (; i < size; i += 8) {
    __m256 vec = _mm256_loadu_ps(&input[i]);
    __m256 result = _mm256_sqrt_ps(vec);
    _mm256_storeu_ps(&output[i], result);
  }
}

int main() {
  float input[] = {0.0, 1.0, -2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
  float output[8];

  simd_sqrt(input, output, 8);

  for (int i = 0; i < 8; ++i) {
    std::cout << output[i] << std::endl;
  }
  return 0;
}
```

```
clang++ -fsanitize=numerical -mavx -O3 -g ~/simd2.cpp -o ~/simd2.exe
```

It also works as expected.


https://github.com/llvm/llvm-project/pull/101531


More information about the llvm-commits mailing list