llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][x86] Wrong code under -ffast-math for SSE mask/select packet code

Open Florian360 opened this issue 5 months ago • 4 comments

Summary

Clang 20+ and current Clang trunk compile the reduced SSE reproducer below to return all zeros under -O3 -ffast-math, even though all input values are finite. GCC and Clang 18 produce the expected finite values.

Godbolt reproducer:

https://godbolt.org/z/P9d9n8v91

Results

Command line:

clang++ -std=c++20 -O3 -ffast-math repro.cpp && ./a.out

Expected output:

1.40653038 -0.972404897 1.28647268 0.139403686
expected approximately: 1.40653 -0.972405 1.28647 0.139404

Observed output with Clang 20.1, Clang 21.1, Clang 22.1, and Clang trunk:

0 0 0 0
expected approximately: 1.40653 -0.972405 1.28647 0.139404

Known-good compilers on the same reproducer:

  • Clang 18.1: correct output
  • Clang 19.1: correct output for this warning-free bit-pattern reproducer
  • GCC 13.2: correct output

Known-bad compilers on the same reproducer:

  • Clang 20.1: wrong output
  • Clang 21.1: wrong output
  • Clang 22.1: wrong output
  • Clang trunk, reporting __clang_major__ == 23: wrong output

Notes

The input packet contains only finite values. The pos_inf and neg_inf constants are constructed from integer bit patterns, not from std::numeric_limits<float>::infinity(), so this version does not rely on spelling an infinity literal under -ffast-math.

This was reduced from packetized complex exponential code. Simplifying the special-case mask/select tail away makes the problem disappear, but the tail is not taken for the finite input. If those special-case comparisons are optimized away under -ffast-math, the finite-input result should still remain the same, not become all zeros.

Reproducer

#include <cmath>
#include <iomanip>
#include <iostream>
#include <xmmintrin.h>

static inline __m128 flip(__m128 x) { return _mm_shuffle_ps(x, x, _MM_SHUFFLE(2, 3, 0, 1)); }
static inline __m128 abs_ps(__m128 x) { return _mm_andnot_ps(_mm_set1_ps(-0.0f), x); }
static inline __m128 select_ps(__m128 mask, __m128 a, __m128 b) {
  return _mm_or_ps(_mm_and_ps(mask, a), _mm_andnot_ps(mask, b));
}
static inline __m128 nan_mask(__m128 x) {
  return _mm_andnot_ps(_mm_cmpeq_ps(x, x), _mm_cmpeq_ps(_mm_setzero_ps(), _mm_setzero_ps()));
}
static inline __m128 exp_ps_scalar(__m128 x) {
  alignas(16) float v[4];
  _mm_store_ps(v, x);
  return _mm_set_ps(std::exp(v[3]), std::exp(v[2]), std::exp(v[1]), std::exp(v[0]));
}
static inline __m128 cossin_selector_scalar(__m128 y) {
  alignas(16) float v[4];
  _mm_store_ps(v, y);
  // The caller flips this into [cos y0, sin y0, cos y1, sin y1].
  return _mm_set_ps(std::cos(v[3]), std::sin(v[2]), std::cos(v[1]), std::sin(v[0]));
}

__attribute__((noinline)) __m128 complex_exp_packet(__m128 a) {
  const __m128 even_mask = _mm_castsi128_ps(_mm_set_epi32(0, -1, 0, -1));
  const __m128 odd_mask = flip(even_mask);

  __m128 x = _mm_and_ps(a, even_mask);
  x = _mm_or_ps(x, flip(x));
  __m128 expx = exp_ps_scalar(x);

  __m128 y = _mm_and_ps(odd_mask, a);
  y = _mm_or_ps(y, flip(y));
  __m128 cisy = cossin_selector_scalar(y);
  cisy = flip(cisy);

  const __m128 pos_inf = _mm_castsi128_ps(_mm_set1_epi32(0x7f800000));
  const __m128 neg_inf = _mm_castsi128_ps(_mm_set1_epi32(static_cast<int>(0xff800000u)));

  __m128 cisy_sign = _mm_or_ps(_mm_andnot_ps(abs_ps(cisy), cisy), _mm_set1_ps(1.0f));
  cisy = select_ps(_mm_cmpeq_ps(x, neg_inf), cisy_sign, cisy);

  __m128 y_sign = _mm_or_ps(_mm_andnot_ps(abs_ps(y), y), _mm_set1_ps(1.0f));
  cisy = select_ps(_mm_and_ps(_mm_cmpeq_ps(x, pos_inf), nan_mask(cisy)), _mm_and_ps(y_sign, even_mask), cisy);

  __m128 cisy_sign_one = _mm_or_ps(_mm_and_ps(cisy, _mm_set1_ps(-0.0f)), _mm_set1_ps(1.0f));
  __m128 expx_inf_y_finite = _mm_and_ps(_mm_cmpeq_ps(expx, pos_inf), _mm_cmplt_ps(abs_ps(y), pos_inf));
  cisy = select_ps(expx_inf_y_finite, cisy_sign_one, cisy);

  __m128 result = _mm_mul_ps(expx, cisy);
  result = select_ps(_mm_cmpeq_ps(y, _mm_setzero_ps()),
                     _mm_or_ps(_mm_and_ps(expx, even_mask), _mm_and_ps(y, odd_mask)), result);
  return result;
}

int main() {
  __m128 z = _mm_set_ps(0.10794f, 0.257741f, -0.604897f, 0.536459f);
  alignas(16) float r[4];
  _mm_store_ps(r, complex_exp_packet(z));
  std::cout << std::setprecision(9) << r[0] << ' ' << r[1] << ' ' << r[2] << ' ' << r[3] << '\n';
  std::cout << "expected approximately: 1.40653 -0.972405 1.28647 0.139404\n";
}

Florian360 avatar Jun 07 '26 07:06 Florian360

You're still using those infinities as an argument to _mm_cmpeq_ps though; which clang lowers to fcmp nnan ninf oeq <4 x float> %0, %1 and thus LLVM gets to do anything for them when they get infinite args, which they do.

Replacing those with _mm_cmpeq_epi32 (and _mm_cmplt_ps with a swapped-args _mm_cmpgt_epi32) seems to make it return the proper values.

dzaima avatar Jun 07 '26 07:06 dzaima

Or write an alternative versions of _mm_cmpeq_ps and _mm_cmplt_ps with float_control: https://godbolt.org/z/85jKsPf7h

phoebewang avatar Jun 07 '26 09:06 phoebewang

@llvm/issue-subscribers-backend-x86

Author: Florian360

### Summary

Clang 20+ and current Clang trunk compile the reduced SSE reproducer below to return all zeros under -O3 -ffast-math, even though all input values are finite. GCC and Clang 18 produce the expected finite values.

Godbolt reproducer:

https://godbolt.org/z/P9d9n8v91

Results

Command line:

clang++ -std=c++20 -O3 -ffast-math repro.cpp &amp;&amp; ./a.out

Expected output:

1.40653038 -0.972404897 1.28647268 0.139403686
expected approximately: 1.40653 -0.972405 1.28647 0.139404

Observed output with Clang 20.1, Clang 21.1, Clang 22.1, and Clang trunk:

0 0 0 0
expected approximately: 1.40653 -0.972405 1.28647 0.139404

Known-good compilers on the same reproducer:

  • Clang 18.1: correct output
  • Clang 19.1: correct output for this warning-free bit-pattern reproducer
  • GCC 13.2: correct output

Known-bad compilers on the same reproducer:

  • Clang 20.1: wrong output
  • Clang 21.1: wrong output
  • Clang 22.1: wrong output
  • Clang trunk, reporting __clang_major__ == 23: wrong output

Notes

The input packet contains only finite values. The pos_inf and neg_inf constants are constructed from integer bit patterns, not from std::numeric_limits&lt;float&gt;::infinity(), so this version does not rely on spelling an infinity literal under -ffast-math.

This was reduced from packetized complex exponential code. Simplifying the special-case mask/select tail away makes the problem disappear, but the tail is not taken for the finite input. If those special-case comparisons are optimized away under -ffast-math, the finite-input result should still remain the same, not become all zeros.

Reproducer

#include &lt;cmath&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;xmmintrin.h&gt;

static inline __m128 flip(__m128 x) { return _mm_shuffle_ps(x, x, _MM_SHUFFLE(2, 3, 0, 1)); }
static inline __m128 abs_ps(__m128 x) { return _mm_andnot_ps(_mm_set1_ps(-0.0f), x); }
static inline __m128 select_ps(__m128 mask, __m128 a, __m128 b) {
  return _mm_or_ps(_mm_and_ps(mask, a), _mm_andnot_ps(mask, b));
}
static inline __m128 nan_mask(__m128 x) {
  return _mm_andnot_ps(_mm_cmpeq_ps(x, x), _mm_cmpeq_ps(_mm_setzero_ps(), _mm_setzero_ps()));
}
static inline __m128 exp_ps_scalar(__m128 x) {
  alignas(16) float v[4];
  _mm_store_ps(v, x);
  return _mm_set_ps(std::exp(v[3]), std::exp(v[2]), std::exp(v[1]), std::exp(v[0]));
}
static inline __m128 cossin_selector_scalar(__m128 y) {
  alignas(16) float v[4];
  _mm_store_ps(v, y);
  // The caller flips this into [cos y0, sin y0, cos y1, sin y1].
  return _mm_set_ps(std::cos(v[3]), std::sin(v[2]), std::cos(v[1]), std::sin(v[0]));
}

__attribute__((noinline)) __m128 complex_exp_packet(__m128 a) {
  const __m128 even_mask = _mm_castsi128_ps(_mm_set_epi32(0, -1, 0, -1));
  const __m128 odd_mask = flip(even_mask);

  __m128 x = _mm_and_ps(a, even_mask);
  x = _mm_or_ps(x, flip(x));
  __m128 expx = exp_ps_scalar(x);

  __m128 y = _mm_and_ps(odd_mask, a);
  y = _mm_or_ps(y, flip(y));
  __m128 cisy = cossin_selector_scalar(y);
  cisy = flip(cisy);

  const __m128 pos_inf = _mm_castsi128_ps(_mm_set1_epi32(0x7f800000));
  const __m128 neg_inf = _mm_castsi128_ps(_mm_set1_epi32(static_cast&lt;int&gt;(0xff800000u)));

  __m128 cisy_sign = _mm_or_ps(_mm_andnot_ps(abs_ps(cisy), cisy), _mm_set1_ps(1.0f));
  cisy = select_ps(_mm_cmpeq_ps(x, neg_inf), cisy_sign, cisy);

  __m128 y_sign = _mm_or_ps(_mm_andnot_ps(abs_ps(y), y), _mm_set1_ps(1.0f));
  cisy = select_ps(_mm_and_ps(_mm_cmpeq_ps(x, pos_inf), nan_mask(cisy)), _mm_and_ps(y_sign, even_mask), cisy);

  __m128 cisy_sign_one = _mm_or_ps(_mm_and_ps(cisy, _mm_set1_ps(-0.0f)), _mm_set1_ps(1.0f));
  __m128 expx_inf_y_finite = _mm_and_ps(_mm_cmpeq_ps(expx, pos_inf), _mm_cmplt_ps(abs_ps(y), pos_inf));
  cisy = select_ps(expx_inf_y_finite, cisy_sign_one, cisy);

  __m128 result = _mm_mul_ps(expx, cisy);
  result = select_ps(_mm_cmpeq_ps(y, _mm_setzero_ps()),
                     _mm_or_ps(_mm_and_ps(expx, even_mask), _mm_and_ps(y, odd_mask)), result);
  return result;
}

int main() {
  __m128 z = _mm_set_ps(0.10794f, 0.257741f, -0.604897f, 0.536459f);
  alignas(16) float r[4];
  _mm_store_ps(r, complex_exp_packet(z));
  std::cout &lt;&lt; std::setprecision(9) &lt;&lt; r[0] &lt;&lt; ' ' &lt;&lt; r[1] &lt;&lt; ' ' &lt;&lt; r[2] &lt;&lt; ' ' &lt;&lt; r[3] &lt;&lt; '\n';
  std::cout &lt;&lt; "expected approximately: 1.40653 -0.972405 1.28647 0.139404\n";
}

This is not really a compiler bug.

phoebewang avatar Jun 08 '26 00:06 phoebewang