Index: lib/Target/NVPTX/NVPTXISelDAGToDAG.h =================================================================== --- lib/Target/NVPTX/NVPTXISelDAGToDAG.h +++ lib/Target/NVPTX/NVPTXISelDAGToDAG.h @@ -34,6 +34,7 @@ bool usePrecSqrtF32() const; bool useF32FTZ() const; bool allowFMA() const; + bool allowUnsafeFPMath() const; public: explicit NVPTXDAGToDAGISel(NVPTXTargetMachine &tm, Index: lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp =================================================================== --- lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp +++ lib/Target/NVPTX/NVPTXISelDAGToDAG.cpp @@ -103,6 +103,11 @@ return TL->allowFMA(*MF, OptLevel); } +bool NVPTXDAGToDAGISel::allowUnsafeFPMath() const { + const NVPTXTargetLowering *TL = Subtarget->getTargetLowering(); + return TL->allowUnsafeFPMath(*MF); +} + /// Select - Select instructions not customized! Used for /// expanded, promoted and normal instructions. void NVPTXDAGToDAGISel::Select(SDNode *N) { Index: lib/Target/NVPTX/NVPTXISelLowering.h =================================================================== --- lib/Target/NVPTX/NVPTXISelLowering.h +++ lib/Target/NVPTX/NVPTXISelLowering.h @@ -511,6 +511,7 @@ getPreferredVectorAction(EVT VT) const override; bool allowFMA(MachineFunction &MF, CodeGenOpt::Level OptLevel) const; + bool allowUnsafeFPMath(MachineFunction &MF) const; bool isFMAFasterThanFMulAndFAdd(EVT) const override { return true; } Index: lib/Target/NVPTX/NVPTXISelLowering.cpp =================================================================== --- lib/Target/NVPTX/NVPTXISelLowering.cpp +++ lib/Target/NVPTX/NVPTXISelLowering.cpp @@ -3848,27 +3848,35 @@ bool NVPTXTargetLowering::allowFMA(MachineFunction &MF, CodeGenOpt::Level OptLevel) const { - const Function *F = MF.getFunction(); - const TargetOptions &TO = MF.getTarget().Options; - // Always honor command-line argument - if (FMAContractLevelOpt.getNumOccurrences() > 0) { + if (FMAContractLevelOpt.getNumOccurrences() > 0) return FMAContractLevelOpt > 0; - } else if (OptLevel == 0) { - // Do not contract if we're not optimizing the code + + // Do not contract if we're not optimizing the code. + if (OptLevel == 0) return false; - } else if (TO.AllowFPOpFusion == FPOpFusion::Fast || TO.UnsafeFPMath) { - // Honor TargetOptions flags that explicitly say fusion is okay + + // Honor TargetOptions flags that explicitly say fusion is okay. + if (MF.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast) return true; - } else if (F->hasFnAttribute("unsafe-fp-math")) { - // Check for unsafe-fp-math=true coming from Clang + + return allowUnsafeFPMath(MF); +} + +bool NVPTXTargetLowering::allowUnsafeFPMath(MachineFunction &MF) const { + // Honor TargetOptions flags that explicitly say unsafe math is okay. + if (MF.getTarget().Options.UnsafeFPMath) + return true; + + // Allow unsafe math if unsafe-fp-math attribute explicitly says so. + const Function *F = MF.getFunction(); + if (F->hasFnAttribute("unsafe-fp-math")) { Attribute Attr = F->getFnAttribute("unsafe-fp-math"); StringRef Val = Attr.getValueAsString(); if (Val == "true") return true; } - // We did not have a clear indication that fusion is allowed, so assume not return false; } Index: lib/Target/NVPTX/NVPTXInstrInfo.td =================================================================== --- lib/Target/NVPTX/NVPTXInstrInfo.td +++ lib/Target/NVPTX/NVPTXInstrInfo.td @@ -134,6 +134,7 @@ def allowFMA : Predicate<"allowFMA()">; def noFMA : Predicate<"!allowFMA()">; +def allowUnsafeFPMath : Predicate<"allowUnsafeFPMath()">; def do_DIVF32_APPROX : Predicate<"getDivF32Level()==0">; def do_DIVF32_FULL : Predicate<"getDivF32Level()==1">; @@ -949,10 +950,12 @@ // sin/cos def SINF: NVPTXInst<(outs Float32Regs:$dst), (ins Float32Regs:$src), "sin.approx.f32 \t$dst, $src;", - [(set Float32Regs:$dst, (fsin Float32Regs:$src))]>; + [(set Float32Regs:$dst, (fsin Float32Regs:$src))]>, + Requires<[allowUnsafeFPMath]>; def COSF: NVPTXInst<(outs Float32Regs:$dst), (ins Float32Regs:$src), "cos.approx.f32 \t$dst, $src;", - [(set Float32Regs:$dst, (fcos Float32Regs:$src))]>; + [(set Float32Regs:$dst, (fcos Float32Regs:$src))]>, + Requires<[allowUnsafeFPMath]>; // Lower (frem x, y) into (sub x, (mul (floor (div x, y)) y)), // i.e. "poor man's fmod()" Index: test/CodeGen/NVPTX/fast-math.ll =================================================================== --- test/CodeGen/NVPTX/fast-math.ll +++ test/CodeGen/NVPTX/fast-math.ll @@ -37,7 +37,22 @@ ret float %t1 } +declare float @llvm.sin.f32(float) +declare float @llvm.cos.f32(float) +; CHECK-LABEL: fsin_approx +; CHECK: sin.approx.f32 +define float @fsin_approx(float %a) #0 { + %r = tail call float @llvm.sin.f32(float %a) + ret float %r +} + +; CHECK-LABEL: fcos_approx +; CHECK: cos.approx.f32 +define float @fcos_approx(float %a) #0 { + %r = tail call float @llvm.cos.f32(float %a) + ret float %r +} attributes #0 = { "unsafe-fp-math" = "true" } attributes #1 = { "nvptx-f32ftz" = "true" } Index: test/CodeGen/NVPTX/fcos-no-fast-math.ll =================================================================== --- /dev/null +++ test/CodeGen/NVPTX/fcos-no-fast-math.ll @@ -0,0 +1,14 @@ +; RUN: not llc < %s -march=nvptx -mcpu=sm_20 2>&1 | FileCheck %s + +; Check that we fail to select fcos without fast-math enabled + +declare float @llvm.cos.f32(float) + +; CHECK: LLVM ERROR: Cannot select: {{.*}}: f32 = fcos +; CHECK: In function: test_fcos_safe +define float @test_fcos_safe(float %a) #0 { + %r = tail call float @llvm.cos.f32(float %a) + ret float %r +} + +attributes #0 = { "unsafe-fp-math" = "false" } Index: test/CodeGen/NVPTX/fsin-no-fast-math.ll =================================================================== --- /dev/null +++ test/CodeGen/NVPTX/fsin-no-fast-math.ll @@ -0,0 +1,14 @@ +; RUN: not llc < %s -march=nvptx -mcpu=sm_20 2>&1 | FileCheck %s + +; Check that we fail to select fsin without fast-math enabled + +declare float @llvm.sin.f32(float) + +; CHECK: LLVM ERROR: Cannot select: {{.*}}: f32 = fsin +; CHECK: In function: test_fsin_safe +define float @test_fsin_safe(float %a) #0 { + %r = tail call float @llvm.sin.f32(float %a) + ret float %r +} + +attributes #0 = { "unsafe-fp-math" = "false" }