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

[PPC] Compiler-rt builtins call fma in nostdlib soft floating point mode

Open vit9696 opened this issue 4 years ago • 15 comments

Starting with llvm 14.0.0 there is a regression in PowerPC backend due to compiler-rt code resulting in libc function calls regardless of the -nostdlib / -ffreestanding flags.

One such example is __fixunsdfdi (compiler-rt/lib/builtins/fixunsdfdi.c), which is implemented as follows:

COMPILER_RT_ABI du_int __fixunsdfdi(double a) {
  if (a <= 0.0)
    return 0;
  su_int high = a / 4294967296.f;               // a / 0x1p32f;
  su_int low = a - (double)high * 4294967296.f; // high * 0x1p32f;
  return ((du_int)high << 32) | low;
}

The problematic part can be shortened to the following code snippet:

unsigned func(double a, double high) {
    return a - high * 4294967296.f;
}

Upon compiling with clang -O3 -target powerpc-gnu-linux -msoft-float -mcpu=e500mc -O3 -nostdlib the following code is generated:

func(double, double):                              # @func(double, double)
        mflr 0
        stw 0, 4(1)
        stwu 1, -16(1)
        mr      7, 6
        mr      8, 5
        mr      9, 4
        mr      10, 3
        lis 5, -15888
        li 6, 0
        mr      3, 8
        mr      4, 7
        mr      7, 10
        mr      8, 9
        bl fma
        bl __fixunsdfsi
        lwz 0, 20(1)
        addi 1, 1, 16
        mtlr 0
        blr

fma is not a function provided by compiler-rt builtins and thus should not be generated here. There is no issue with 13.0.1:

func(double, double):                              # @func(double, double)
        mflr 0
        stw 0, 4(1)
        stwu 1, -32(1)
        mr      7, 6
        mr      8, 5
        stw 29, 20(1)                           # 4-byte Folded Spill
        mr      29, 3
        stw 30, 24(1)                           # 4-byte Folded Spill
        mr      30, 4
        lis 5, -15888
        li 6, 0
        mr      3, 8
        mr      4, 7
        bl __muldf3
        mr      5, 3
        mr      6, 4
        mr      3, 29
        mr      4, 30
        bl __adddf3
        bl __fixunsdfsi
        lwz 30, 24(1)                           # 4-byte Folded Reload
        lwz 29, 20(1)                           # 4-byte Folded Reload
        lwz 0, 36(1)
        addi 1, 1, 32
        mtlr 0
        blr

CC @tstellar, this may need to be fixed in some 14.0.x release.

vit9696 avatar May 02 '22 15:05 vit9696

@llvm/issue-subscribers-backend-powerpc

llvmbot avatar May 02 '22 15:05 llvmbot

The cause is f04e387055e495e3e14570087d68e93593cf2918, CC @zahiraam

vit9696 avatar May 02 '22 22:05 vit9696

Currently the workaround is to add -ffp-contract=off argument, but I believe we need to answer separately whether it is:

  • Correct to change the defaults for a target without fma?
  • Correct to generate fma() in freestanding/nostdlib mode?
  • Correct to use sequences that need stdlib support in builtins?

In the meanwhile I suggest to revert the change from 14.0.x at least for soft floating point model.

CC @andykaylor

vit9696 avatar May 02 '22 22:05 vit9696

@vit9696 yes the solution is to add the -ffp-contract=off in order to get the desired output. It is expected that the ffp-contract=on regardless if target has fma or not. I think we expect users to add the ffp-contract=off option. As for the 2 other questions I am going to let @andykaylor respond to them. I am not quite sure of the answer.

zahiraam avatar May 03 '22 12:05 zahiraam

After a short analysis I also discovered https://reviews.llvm.org/D78668, which confirms that the issue is not just with the soft floating point implementations, but also hard floating point implementations without an fma instruction. On PowerPC SPE a call to fma function is indeed generated.

At this step I am starting to wonder that the issue here is the wrong function name to call. If one needs an fma implementation to support -ffp-contract=on, then one of the following must hold:

  1. codegen provides fma with one instruction or many
  2. compiler-rt builtins provide fma implementation as a function and codegen invokes this function.

For case 2 we will need to avoid a conflict with the fma function from libm. Perhaps, a new compiler-rt builtin should be named __llvm_fma in this case?

vit9696 avatar May 04 '22 22:05 vit9696

Would a patch such as this solve the problem you're facing:

diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
index 47960df4cace..e0a83aea04b1 100644
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -17230,7 +17230,7 @@ bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
 
 bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(const Function &F,
                                                    Type *Ty) const {
-  if (Subtarget.hasSPE())
+  if (Subtarget.hasSPE() || Subtarget.useSoftFloat())
     return false;
   switch (Ty->getScalarType()->getTypeID()) {
   case Type::FloatTyID:

nemanjai avatar Jul 19 '23 13:07 nemanjai

I think so, but need to retest with the latest master.

vit9696 avatar Jul 19 '23 13:07 vit9696

Tested with 16.0.6 and this patch applied. Works correctly. Thank you.

vit9696 avatar Jul 24 '23 11:07 vit9696

does anyone has raised the review for this patch? so we can have this fixed in 17.x or trunk.. If not then i can help out!

Long5hot avatar Jul 25 '23 13:07 Long5hot

Nope, it would be great if you do.

vit9696 avatar Jul 26 '23 09:07 vit9696

@long5hot @vit9696 I noticed that this PR was opened to address this: https://reviews.llvm.org/D156344 However, it was never committed and I checked with the most recent Top of Trunk on main and this issue is still there. I know that the other suggestion was to use hasFMA() but that patch seems to be abandoned.

stefanp-synopsys avatar Sep 25 '23 18:09 stefanp-synopsys

this is the recent PR https://reviews.llvm.org/D158632, i raised for X86.. But it seems i failed to explain the issue properly..

Long5hot avatar Sep 25 '23 18:09 Long5hot

@long5hot I apologize for not having noticed your PR or this issue despite efforts various people have made to request my attention on them.

Regarding https://reviews.llvm.org/D158632, I have to reluctantly agree that @arsenm is right on this. The clang front end uses llvm.fmuladd to indicate that an expression was encountered that meets the requirements for contraction with fp-contract=on. There is no other way currently to distinguish between cases where operations may be fused because they were in the same expression and operations that occur in separate expressions and can only be fused with fp-contract=fast.

As Matt mentioned in the review for D158632, the backend is not required to perform a fused operation (either emulated or otherwise) when the llvm.fmuladd intrinsic is used (unlike the llvm.fma intrinsic, for which should always be evaluated in a way that is numerically equivalent to the fused operation).

If the llvm.fmuladd intrinsic reaches the backend, the backend is responsible for deciding how to represent it. If the target does not support FMA instructions, it is entirely reasonable for the backend to generate separate multiply and add operations.

andykaylor avatar Sep 26 '23 23:09 andykaylor

Looks like the fix has gone in for this issue.

commit 696ea67f197ff0818cb649db61d51df8e81eb16e
Author: Kishan Parmar <[email protected]>
Date:   Thu Sep 28 12:08:41 2023 +0530

    Disable call to fma for soft-float
    
    PowerPC backend generate calls to libc function calls
    for soft-float, regardless of the -nostdlib /-ffreestanding flag.
    fma is not a function provided by compiler-rt builtins and
    thus should not be generated here.
    PR : [[ https://github.com/llvm/llvm-project/issues/55230 | #55230 ]]
    
    Below is patch given by @nemanjai
    
    Reviewed By: jhibbits
    
    Differential Revision: https://reviews.llvm.org/D156344

Thank you for the patch!

Can we close this issue at this point?

stefanp-synopsys avatar Oct 05 '23 15:10 stefanp-synopsys

Yes, I have exactly the same patch locally and it works. Perhaps we want to backport this to LLVM 17 though? Can close when it lands to some kind of stable. CC @tstellar

vit9696 avatar Oct 05 '23 18:10 vit9696