[clang] [clang] Set correct FPOptions if attribute 'optnone' presents (PR #85605)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 25 10:11:13 PDT 2024


dyung wrote:

Hi, we have an internal test that checks that compiling with and without optnone at O0 does not change the code generated by the compiler, and one of the tests we have did change which I bisected back to your change.

The test consists of the following code:
```c++
#include <math.h>

extern int printf(const char *format, ...);

template <typename T> OPTNONE T &pi(int num_terms) {
  static T approx = 0;

  for (int i = 0; i <= num_terms; ++i) {
    approx += 4 * (pow(-1, i) / ((2 * i) + 1));
  }

  return approx;
}

OPTNONE
int main() {
  int const num_terms = 5;

  float f1000 = pi<float>(num_terms);

  printf("%f\n", f1000);

  return 0;
}
```
When compiled with `-S -O0 -mavx -DOPTNONE=` part of the function pi<float>(int) looks like this:
```assembly
        vcvtsi2sd       xmm1, xmm1, eax
        vdivsd  xmm0, xmm0, xmm1
        vmovss  xmm2, dword ptr [rip + pi<float>(int)::approx] # xmm2 = mem[0],zero,zero,zero
        vcvtss2sd       xmm1, xmm1, xmm2
        vmovsd  xmm2, qword ptr [rip + .LCPI1_0] # xmm2 = [4.0E+0,0.0E+0]
        vmulsd  xmm0, xmm0, xmm2
        vaddsd  xmm1, xmm0, xmm1
        vcvtsd2ss       xmm0, xmm0, xmm1
        vmovss  dword ptr [rip + pi<float>(int)::approx], xmm0
```
When compiled with `-S -O0 -mavx -DOPTNONE=__attribute__((optnone))`, the same code for pi<float>(int) is slightly different:
```assembly
        vcvtsi2sd       xmm1, xmm1, eax
        vdivsd  xmm1, xmm0, xmm1
        vmovsd  xmm0, qword ptr [rip + .LCPI1_0] # xmm0 = [4.0E+0,0.0E+0]
        vmulsd  xmm1, xmm0, xmm1
        vmovss  xmm2, dword ptr [rip + pi<float>(int)::approx] # xmm2 = mem[0],zero,zero,zero
        vcvtss2sd       xmm0, xmm0, xmm2
        vaddsd  xmm1, xmm0, xmm1
        vcvtsd2ss       xmm0, xmm0, xmm1
        vmovss  dword ptr [rip + pi<float>(int)::approx], xmm0
```
You can see this change on godbolt here: https://godbolt.org/z/WTWPoqY51

Is this change intended?

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


More information about the cfe-commits mailing list