[llvm] 747c6a0 - [SDAG] fix miscompile when casting int->FP->int

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon May 2 11:57:34 PDT 2022


Author: Sanjay Patel
Date: 2022-05-02T14:57:27-04:00
New Revision: 747c6a0c734e618db8132b503f432d8274cc56b5

URL: https://github.com/llvm/llvm-project/commit/747c6a0c734e618db8132b503f432d8274cc56b5
DIFF: https://github.com/llvm/llvm-project/commit/747c6a0c734e618db8132b503f432d8274cc56b5.diff

LOG: [SDAG] fix miscompile when casting int->FP->int

This is the codegen equivalent of D124692.

As shown in https://github.com/llvm/llvm-project/issues/55150 -
the existing fold may be wrong when converting to a signed value.
This is a quick fix to avoid the miscompile.
https://alive2.llvm.org/ce/z/KtaDmd

Differential Revision: https://reviews.llvm.org/D124771

Added: 
    

Modified: 
    llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/test/CodeGen/AArch64/float-conv-elim.ll
    llvm/test/CodeGen/X86/float-conv-elim.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index e139cf6268061..c4b3d373b49b8 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -15527,7 +15527,7 @@ static SDValue FoldIntToFPToInt(SDNode *N, SelectionDAG &DAG) {
   // This means this is also safe for a signed input and unsigned output, since
   // a negative input would lead to undefined behavior.
   unsigned InputSize = (int)SrcVT.getScalarSizeInBits() - IsInputSigned;
-  unsigned OutputSize = (int)VT.getScalarSizeInBits() - IsOutputSigned;
+  unsigned OutputSize = (int)VT.getScalarSizeInBits();
   unsigned ActualSize = std::min(InputSize, OutputSize);
   const fltSemantics &sem = DAG.EVTToAPFloatSemantics(N0.getValueType());
 

diff  --git a/llvm/test/CodeGen/AArch64/float-conv-elim.ll b/llvm/test/CodeGen/AArch64/float-conv-elim.ll
index dcc543fd69f54..5442125f87653 100644
--- a/llvm/test/CodeGen/AArch64/float-conv-elim.ll
+++ b/llvm/test/CodeGen/AArch64/float-conv-elim.ll
@@ -45,10 +45,13 @@ define i32 @u32_f32_u24_u32(i32 %a) {
   ret i32 %r
 }
 
+; This requires converting to FP and back.
+
 define i32 @s32_f32_s25_s32(i32 %a) {
 ; CHECK-LABEL: s32_f32_s25_s32:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    sbfx w0, w0, #0, #25
+; CHECK-NEXT:    scvtf s0, w0
+; CHECK-NEXT:    fcvtzs w0, s0
 ; CHECK-NEXT:    ret
   %f = sitofp i32 %a to float
   %i = fptosi float %f to i25
@@ -68,10 +71,13 @@ define i32 @s32_f32_u25_u32(i32 %a) {
   ret i32 %r
 }
 
+; TODO: This could avoid converting to FP.
+
 define i32 @u32_f32_s25_s32(i32 %a) {
 ; CHECK-LABEL: u32_f32_s25_s32:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    sbfx w0, w0, #0, #25
+; CHECK-NEXT:    ucvtf s0, w0
+; CHECK-NEXT:    fcvtzs w0, s0
 ; CHECK-NEXT:    ret
   %f = uitofp i32 %a to float
   %i = fptosi float %f to i25

diff  --git a/llvm/test/CodeGen/X86/float-conv-elim.ll b/llvm/test/CodeGen/X86/float-conv-elim.ll
index 09501429bf531..2a543df03eb82 100644
--- a/llvm/test/CodeGen/X86/float-conv-elim.ll
+++ b/llvm/test/CodeGen/X86/float-conv-elim.ll
@@ -82,12 +82,13 @@ define i32 @u32_f32_u24_u32(i32 %a) {
   ret i32 %r
 }
 
+; This requires converting to FP and back.
+
 define i32 @s32_f32_s25_s32(i32 %a) {
 ; CHECK-LABEL: s32_f32_s25_s32:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    movl %edi, %eax
-; CHECK-NEXT:    shll $7, %eax
-; CHECK-NEXT:    sarl $7, %eax
+; CHECK-NEXT:    cvtsi2ss %edi, %xmm0
+; CHECK-NEXT:    cvttss2si %xmm0, %eax
 ; CHECK-NEXT:    retq
   %f = sitofp i32 %a to float
   %i = fptosi float %f to i25
@@ -107,12 +108,14 @@ define i32 @s32_f32_u25_u32(i32 %a) {
   ret i32 %r
 }
 
+; TODO: This could avoid converting to FP.
+
 define i32 @u32_f32_s25_s32(i32 %a) {
 ; CHECK-LABEL: u32_f32_s25_s32:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    movl %edi, %eax
-; CHECK-NEXT:    shll $7, %eax
-; CHECK-NEXT:    sarl $7, %eax
+; CHECK-NEXT:    cvtsi2ss %rax, %xmm0
+; CHECK-NEXT:    cvttss2si %xmm0, %eax
 ; CHECK-NEXT:    retq
   %f = uitofp i32 %a to float
   %i = fptosi float %f to i25


        


More information about the llvm-commits mailing list