Skip to content

[ValueTracking] Fix incorrect inferrence about the signbit of sqrt #92510

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

Merged
merged 3 commits into from
May 20, 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
5 changes: 1 addition & 4 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4940,11 +4940,8 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
// subnormal input could produce a negative zero output.
const Function *F = II->getFunction();
if (Q.IIQ.hasNoSignedZeros(II) ||
(F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))) {
(F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType())))
Known.knownNot(fcNegZero);
if (KnownSrc.isKnownNeverNaN())
Copy link
Contributor

@arsenm arsenm May 17, 2024

Choose a reason for hiding this comment

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

&& if the source is known positive (or the op has the nnan flag)

Copy link
Member Author

Choose a reason for hiding this comment

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

Isn't it covered by the above code?

// Any negative value besides -0 returns a nan.
if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
Known.knownNot(fcNan);
// The only negative value that can be returned is -0 for -0 inputs.
Known.knownNot(fcNegInf | fcNegSubnormal | fcNegNormal);

Copy link
Contributor

Choose a reason for hiding this comment

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

Semi-related, this should be using propagateNaN instead of repeating the snan logic

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we are missing handling of the nnan flag on the sqrt instruction itself

Copy link
Contributor

Choose a reason for hiding this comment

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

Or that just comes from the RAII thing

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we are missing handling of the nnan flag on the sqrt instruction itself

See

/// Wrapper to account for known fast math flags at the use instruction.
inline KnownFPClass computeKnownFPClass(const Value *V, FastMathFlags FMF,
FPClassTest InterestedClasses,
unsigned Depth,
const SimplifyQuery &SQ) {
if (FMF.noNaNs())
InterestedClasses &= ~fcNan;
if (FMF.noInfs())
InterestedClasses &= ~fcInf;
KnownFPClass Result = computeKnownFPClass(V, InterestedClasses, Depth, SQ);
if (FMF.noNaNs())
Result.KnownFPClasses &= ~fcNan;
if (FMF.noInfs())
Result.KnownFPClasses &= ~fcInf;
return Result;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

See the test sqrt_negative_input_nnan.

Known.signBitMustBeZero();
}

break;
}
Expand Down
38 changes: 38 additions & 0 deletions llvm/test/Transforms/InstCombine/known-bits.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,44 @@ define i32 @test_none(float nofpclass(all) %x) {
ret i32 %and
}

; We cannot make assumptions about the sign of result of sqrt
; when the input is a negative value (except for -0).
define i1 @pr92217() {
; CHECK-LABEL: @pr92217(
; CHECK-NEXT: [[X:%.*]] = call float @llvm.sqrt.f32(float 0xC6DEBE9E60000000)
; CHECK-NEXT: [[Y:%.*]] = bitcast float [[X]] to i32
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[Y]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%x = call float @llvm.sqrt.f32(float 0xC6DEBE9E60000000)
%y = bitcast float %x to i32
%cmp = icmp slt i32 %y, 0
ret i1 %cmp
}

define i1 @sqrt_negative_input(float nofpclass(nan zero pnorm psub pinf) %a) {
; CHECK-LABEL: @sqrt_negative_input(
; CHECK-NEXT: [[X:%.*]] = call float @llvm.sqrt.f32(float [[A:%.*]])
; CHECK-NEXT: [[Y:%.*]] = bitcast float [[X]] to i32
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[Y]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%x = call float @llvm.sqrt.f32(float %a)
%y = bitcast float %x to i32
%cmp = icmp slt i32 %y, 0
ret i1 %cmp
}

define i1 @sqrt_negative_input_nnan(float nofpclass(nan zero pnorm psub pinf) %a) {
; CHECK-LABEL: @sqrt_negative_input_nnan(
; CHECK-NEXT: ret i1 false
;
%x = call nnan float @llvm.sqrt.f32(float %a)
%y = bitcast float %x to i32
%cmp = icmp slt i32 %y, 0
ret i1 %cmp
}

define i8 @test_icmp_add(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_add(
; CHECK-NEXT: entry:
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/Analysis/ValueTrackingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2005,7 +2005,7 @@ TEST_F(ComputeKnownFPClassTest, SqrtNszSignBit) {
computeKnownFPClass(A4, M->getDataLayout(), fcAllFlags, 0, nullptr,
nullptr, nullptr, nullptr, /*UseInstrInfo=*/true);
EXPECT_EQ(fcPositive | fcQNan, UseInstrInfoNSZNoNan.KnownFPClasses);
EXPECT_EQ(false, UseInstrInfoNSZNoNan.SignBit);
EXPECT_EQ(std::nullopt, UseInstrInfoNSZNoNan.SignBit);

KnownFPClass NoUseInstrInfoNSZNoNan =
computeKnownFPClass(A4, M->getDataLayout(), fcAllFlags, 0, nullptr,
Expand Down
Loading