Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release/18.x: [ConstantRange] Fix off by 1 bugs in UIToFP and SIToFP handling. (#86041) #86153

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/lib/IR/ConstantRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
Min = Min.zext(ResultBitWidth);
Max = Max.zext(ResultBitWidth);
}
return ConstantRange(std::move(Min), std::move(Max));
return getNonEmpty(std::move(Min), std::move(Max) + 1);
}
case Instruction::SIToFP: {
// TODO: use input range if available
Expand All @@ -757,7 +757,7 @@ ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
SMin = SMin.sext(ResultBitWidth);
SMax = SMax.sext(ResultBitWidth);
}
return ConstantRange(std::move(SMin), std::move(SMax));
return getNonEmpty(std::move(SMin), std::move(SMax) + 1);
}
case Instruction::FPTrunc:
case Instruction::FPExt:
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/Transforms/Float2Int/pr79158.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt < %s -passes=float2int -S | FileCheck %s

define i32 @pr79158(i32 %x) {
; CHECK-LABEL: define i32 @pr79158(
; CHECK-SAME: i32 [[X:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X]], 0
; CHECK-NEXT: [[TMP0:%.*]] = zext i1 [[CMP]] to i64
; CHECK-NEXT: [[MUL1:%.*]] = mul i64 [[TMP0]], 4294967295
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[MUL1]] to i32
; CHECK-NEXT: ret i32 [[TMP1]]
;
entry:
%cmp = icmp sgt i32 %x, 0
%conv = uitofp i1 %cmp to double
%mul = fmul double %conv, 0x41EFFFFFFFE00000
%conv1 = fptoui double %mul to i32
ret i32 %conv1
}
18 changes: 18 additions & 0 deletions llvm/unittests/IR/ConstantRangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,24 @@ TEST_F(ConstantRangeTest, castOps) {
ConstantRange IntToPtr = A.castOp(Instruction::IntToPtr, 64);
EXPECT_EQ(64u, IntToPtr.getBitWidth());
EXPECT_TRUE(IntToPtr.isFullSet());

ConstantRange UIToFP = A.castOp(Instruction::UIToFP, 16);
EXPECT_EQ(16u, UIToFP.getBitWidth());
EXPECT_TRUE(UIToFP.isFullSet());

ConstantRange UIToFP2 = A.castOp(Instruction::UIToFP, 64);
ConstantRange B(APInt(64, 0), APInt(64, 65536));
EXPECT_EQ(64u, UIToFP2.getBitWidth());
EXPECT_EQ(B, UIToFP2);

ConstantRange SIToFP = A.castOp(Instruction::SIToFP, 16);
EXPECT_EQ(16u, SIToFP.getBitWidth());
EXPECT_TRUE(SIToFP.isFullSet());

ConstantRange SIToFP2 = A.castOp(Instruction::SIToFP, 64);
ConstantRange C(APInt(64, -32768), APInt(64, 32768));
EXPECT_EQ(64u, SIToFP2.getBitWidth());
EXPECT_EQ(C, SIToFP2);
}

TEST_F(ConstantRangeTest, binaryAnd) {
Expand Down
Loading