[SelectionDAG] Remove UnsafeFPMath in visitFP_ROUND (#154768)

Remove `UnsafeFPMath` in `visitFP_ROUND` part, it blocks some bugfixes
related to clang and the ultimate goal is to remove `resetTargetOptions`
method in `TargetMachine`, see FIXME in `resetTargetOptions`.
See also
https://discourse.llvm.org/t/rfc-honor-pragmas-with-ffp-contract-fast

https://discourse.llvm.org/t/allowfpopfusion-vs-sdnodeflags-hasallowcontract

Now all UnsafeFPMath uses are eliminated in LLVMCodeGen
This commit is contained in:
paperchalice 2025-08-22 19:46:33 +08:00 committed by GitHub
parent d8769bb5b7
commit 2014890c09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 11 deletions

View File

@ -18983,7 +18983,9 @@ SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
// single-step fp_round we want to fold to.
// In other words, double rounding isn't the same as rounding.
// Also, this is a value preserving truncation iff both fp_round's are.
if (DAG.getTarget().Options.UnsafeFPMath || N0IsTrunc)
if ((N->getFlags().hasAllowContract() &&
N0->getFlags().hasAllowContract()) ||
N0IsTrunc)
return DAG.getNode(
ISD::FP_ROUND, DL, VT, N0.getOperand(0),
DAG.getIntPtrConstant(NIsTrunc && N0IsTrunc, DL, /*isTarget=*/true));

View File

@ -1,20 +1,53 @@
; RUN: llc < %s | FileCheck %s --check-prefix=CHECK --check-prefix=SAFE
; RUN: llc < %s -enable-unsafe-fp-math | FileCheck %s --check-prefix=CHECK --check-prefix=UNSAFE
; RUN: llc < %s | FileCheck %s
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64--"
; CHECK-LABEL: double_rounding_safe:
; CHECK: callq __trunctfdf2
; CHECK-NEXT: cvtsd2ss %xmm0
define void @double_rounding_safe(ptr %x, ptr %f) {
entry:
%x.fp128 = load fp128, ptr %x, align 16
%x.double = fptrunc fp128 %x.fp128 to double
%x.float = fptrunc double %x.double to float
store float %x.float, ptr %f, align 4
ret void
}
; CHECK-LABEL: double_rounding_contract_fst:
; CHECK: callq __trunctfdf2
; CHECK-NEXT: cvtsd2ss %xmm0
define void @double_rounding_contract_fst(ptr %x, ptr %f) {
entry:
%x.fp128 = load fp128, ptr %x, align 16
%x.double = fptrunc contract fp128 %x.fp128 to double
%x.float = fptrunc double %x.double to float
store float %x.float, ptr %f, align 4
ret void
}
; CHECK-LABEL: double_rounding_contract_snd:
; CHECK: callq __trunctfdf2
; CHECK-NEXT: cvtsd2ss %xmm0
define void @double_rounding_contract_snd(ptr %x, ptr %f) {
entry:
%x.fp128 = load fp128, ptr %x, align 16
%x.double = fptrunc fp128 %x.fp128 to double
%x.float = fptrunc contract double %x.double to float
store float %x.float, ptr %f, align 4
ret void
}
; CHECK-LABEL: double_rounding:
; SAFE: callq __trunctfdf2
; SAFE-NEXT: cvtsd2ss %xmm0
; UNSAFE: callq __trunctfsf2
; UNSAFE-NOT: cvt
; CHECK: callq __trunctfsf2
; CHECK-NOT: cvt
define void @double_rounding(ptr %x, ptr %f) {
entry:
%0 = load fp128, ptr %x, align 16
%1 = fptrunc fp128 %0 to double
%2 = fptrunc double %1 to float
store float %2, ptr %f, align 4
%x.fp128 = load fp128, ptr %x, align 16
%x.double = fptrunc contract fp128 %x.fp128 to double
%x.float = fptrunc contract double %x.double to float
store float %x.float, ptr %f, align 4
ret void
}