[llvm] [PowerPC] Remove `UnsafeFPMath` uses (PR #154901)

Amy Kwan via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 4 22:22:33 PDT 2025


https://github.com/amy-kwan commented:

Hi! I realized I have a question regarding this PR while reviewing again, as it is possible I am misunderstanding something. I would be happy to re-approve once I am able to clarify my question.

We produce different code now for `-O3 -ffast-math` for the cases that were updated. For example, for this test:
```
$ cat near.c
#include <math.h>

double test_constrained_nearbyint(double x) {
    return nearbyint(x);
}
```
We previously generated:
```
$ clang -O3 -target powerpc64le-unknown-unknown near.c -ffast-math -S -o -
	.abiversion 2
	.file	"near.c"
	.text
	.globl	test_constrained_nearbyint      # -- Begin function test_constrained_nearbyint
	.p2align	4
	.type	test_constrained_nearbyint, at function
test_constrained_nearbyint:             # @test_constrained_nearbyint
.Lfunc_begin0:
	.cfi_startproc
# %bb.0:                                # %entry
	xsrdpic 1, 1
	blr
	.long	0
	.quad	0
.Lfunc_end0:
	.size	test_constrained_nearbyint, .Lfunc_end0-.Lfunc_begin0
	.cfi_endproc
                                        # -- End function
	.ident	"clang version 22.0.0git"
	.section	".note.GNU-stack","", at progbits
	.addrsig
```

With this change, we no longer generate this because fnearbyint is no longer made legal.

If we want to generate the same instructions with this patch, we would need `-frounding-math` to ensure we generate the correct constrained intrinsic:
```
$ clang -O3 -target powerpc64le-unknown-unknown near.c -ffast-math -frounding-math -S  -o -
        .abiversion 2
        .file   "near.c"
        .text
        .globl  test_constrained_nearbyint      # -- Begin function test_constrained_nearbyint
        .p2align        4
        .type   test_constrained_nearbyint, at function
test_constrained_nearbyint:             # @test_constrained_nearbyint
.Lfunc_begin0:
        .cfi_startproc
# %bb.0:                                # %entry
        xsrdpic 1, 1
        blr
        .long   0
        .quad   0
.Lfunc_end0:
        .size   test_constrained_nearbyint, .Lfunc_end0-.Lfunc_begin0
        .cfi_endproc
                                        # -- End function
        .ident  "clang version 22.0.0git"
        .section        ".note.GNU-stack","", at progbits
        .addrsig
```

I am aware that `-Ofast`/`-O3 -ffast-math` implies `-fno-rounding-math`, but I am wondering if the previous behaviour of generating the fast math instructions may have been a bug if they are intended for `-frounding-math`. Unless if there is another way to generate these constrained intrinsics?

@RolandF77 Do you have any opinions on this?

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


More information about the llvm-commits mailing list