Skip to content

Conversation

@topperc
Copy link
Collaborator

@topperc topperc commented Mar 20, 2024

We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1.

We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion.

I believe this fixes at least some of the reason #79158 was reverted.

CC: @AtariDreams

@topperc topperc requested a review from nikic as a code owner March 20, 2024 23:42
@llvmbot
Copy link
Member

llvmbot commented Mar 20, 2024

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-ir

Author: Craig Topper (topperc)

Changes

We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1.

We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion.

I believe this fixes at least some of the reason #79158 was reverted.

CC: @AtariDreams


Full diff: https://github.com/llvm/llvm-project/pull/86041.diff

2 Files Affected:

  • (modified) llvm/lib/IR/ConstantRange.cpp (+2-2)
  • (modified) llvm/unittests/IR/ConstantRangeTest.cpp (+18)
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index 3394a1ec8dc476..59e7a9f5eb1114 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -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 @@ -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: diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index 34a162a5514e95..8ec120d70e99f6 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -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) { 
@AZero13
Copy link
Contributor

AZero13 commented Mar 20, 2024

@llvm/pr-subscribers-llvm-ir

Author: Craig Topper (topperc)

Changes

Could you please add the float2int tests while you are at it?

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice find!

; 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.i.0.x.i.0.x.0.x.0.x.0..i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please adjust the variable name :)

@topperc topperc force-pushed the pr/constant-range-itofp branch from e3c5053 to 7a62d5f Compare March 21, 2024 15:25
We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted.
@topperc topperc force-pushed the pr/constant-range-itofp branch from 7a62d5f to 83bb4a3 Compare March 21, 2024 15:44
@topperc topperc merged commit 1283646 into llvm:main Mar 21, 2024
@topperc topperc deleted the pr/constant-range-itofp branch March 21, 2024 16:25
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

/cherry-pick 6295e67

Error: Command failed due to missing milestone.

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

/cherry-pick AZero13@1283646

Error: Command failed due to missing milestone.

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

/cherry-pick 1283646

Error: Command failed due to missing milestone.

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

/cherry-pick 6295e67

Error: Command failed due to missing milestone.

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

/cherry-pick AZero13@6295e67

Error: Command failed due to missing milestone.

@dtcxzyw dtcxzyw added this to the LLVM 18.X Release milestone Mar 21, 2024
@AZero13
Copy link
Contributor

AZero13 commented Mar 21, 2024

/cherry-pick 6295e67 1283646

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

Failed to cherry-pick: release/18.x

https://github.com/llvm/llvm-project/actions/runs/8378684757

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

/pull-request #86153

llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Mar 21, 2024
…m#86041) We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted. (cherry picked from commit 1283646)
AZero13 pushed a commit to AZero13/llvm-project that referenced this pull request Mar 21, 2024
…m#86041) We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted.
AZero13 pushed a commit to AZero13/llvm-project that referenced this pull request Mar 21, 2024
…m#86041) We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted.
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
…m#86041) We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted.
aeubanks pushed a commit that referenced this pull request Mar 25, 2024
… fits (#86337) Originally reverted because of a bug in Range that is now fixed (#86041), we can reland this commit. Tests have been added to ensure the miscompile that caused the revert does not happen again.
tstellar pushed a commit to llvmbot/llvm-project that referenced this pull request Apr 10, 2024
…m#86041) We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted. (cherry picked from commit 1283646)
tstellar pushed a commit to llvmbot/llvm-project that referenced this pull request Apr 10, 2024
…m#86041) We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted. (cherry picked from commit 1283646)
@pointhex pointhex mentioned this pull request May 7, 2024
Tedlion pushed a commit to Tedlion/llvm-project that referenced this pull request Jun 15, 2025
…m#86041) We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted. (cherry picked from commit 1283646)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment