Skip to content

[SCCP] Don't allow undef ranges when performing operations #93163

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 1 commit into from
May 23, 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
21 changes: 13 additions & 8 deletions llvm/lib/Transforms/Utils/SCCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static ValueLatticeElement::MergeOptions getMaxWidenStepsOpts() {
}

static ConstantRange getConstantRange(const ValueLatticeElement &LV, Type *Ty,
bool UndefAllowed = true) {
bool UndefAllowed) {
assert(Ty->isIntOrIntVectorTy() && "Should be int or int vector");
if (LV.isConstantRange(UndefAllowed))
return LV.getConstantRange();
Expand Down Expand Up @@ -1297,7 +1297,8 @@ void SCCPInstVisitor::visitCastInst(CastInst &I) {

if (I.getDestTy()->isIntegerTy() && I.getSrcTy()->isIntOrIntVectorTy()) {
auto &LV = getValueState(&I);
ConstantRange OpRange = getConstantRange(OpSt, I.getSrcTy());
ConstantRange OpRange =
getConstantRange(OpSt, I.getSrcTy(), /*UndefAllowed=*/false);

Type *DestTy = I.getDestTy();
// Vectors where all elements have the same known constant range are treated
Expand Down Expand Up @@ -1329,8 +1330,8 @@ void SCCPInstVisitor::handleExtractOfWithOverflow(ExtractValueInst &EVI,
return; // Wait to resolve.

Type *Ty = LHS->getType();
ConstantRange LR = getConstantRange(L, Ty);
ConstantRange RR = getConstantRange(R, Ty);
ConstantRange LR = getConstantRange(L, Ty, /*UndefAllowed=*/false);
ConstantRange RR = getConstantRange(R, Ty, /*UndefAllowed=*/false);
if (Idx == 0) {
ConstantRange Res = LR.binaryOp(WO->getBinaryOp(), RR);
mergeInValue(&EVI, ValueLatticeElement::getRange(Res));
Expand Down Expand Up @@ -1534,8 +1535,10 @@ void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
return markOverdefined(&I);

// Try to simplify to a constant range.
ConstantRange A = getConstantRange(V1State, I.getType());
ConstantRange B = getConstantRange(V2State, I.getType());
ConstantRange A =
getConstantRange(V1State, I.getType(), /*UndefAllowed=*/false);
ConstantRange B =
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 the code that uses the constant range information from ValueLatticeElements already should be handling the range-may-include-undef case properly (i.e. not use it when refining instructions, only when using it to replace with a constant).

But it looks like the code below (mergeInValue call) simply drops the may-include-undef bit from the operands. If we propagate the bit to the result, things should be fine?

-  mergeInValue(&I, ValueLatticeElement::getRange(R));
+  mergeInValue(&I, ValueLatticeElement::getRange(
+                       R, V1State.isConstantRangeIncludingUndef() ||
+                              V2State.isConstantRangeIncludingUndef()));

(IIRC the may-inlcude-undef bit was explicitly added to account for cases such as the AND test cases and I was pretty sure this was covered, but perhaps this was a place that got missed)

Copy link
Contributor

Choose a reason for hiding this comment

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

If the above works out, it may make sense to make the MayIncludeUndef argument for ValueLatticeElement::getRange() required or flip the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the alternative I mentioned in the patch description, and actually what I implemented first. But I think that this will usually produce a worse result than treating undef as a full range.

For example, if you have zext i8 ([a, b] | undef) to i16 we can either produce i16 [0, 255] (this patch) or i16 ([a, b] | undef), where the latter will be interprted as i16 full-range in places that don't allow undef. So we can either produce a range that is usable everywhere but is larger, or a range that is only usable in allow-undef contexts but is smaller. As a lot of folds in SCCP don't allow undef, I figured the first variant is preferable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the record, this was my initial patch: nikic@94ed59e

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Worth noting that the "treat undef as full range" approach is also what we do in LVI.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes that makes sense, thanks!

getConstantRange(V2State, I.getType(), /*UndefAllowed=*/false);

auto *BO = cast<BinaryOperator>(&I);
ConstantRange R = ConstantRange::getEmpty(I.getType()->getScalarSizeInBits());
Expand Down Expand Up @@ -1818,7 +1821,8 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {

// Combine range info for the original value with the new range from the
// condition.
auto CopyOfCR = getConstantRange(CopyOfVal, CopyOf->getType());
auto CopyOfCR = getConstantRange(CopyOfVal, CopyOf->getType(),
/*UndefAllowed=*/true);
auto NewCR = ImposedCR.intersectWith(CopyOfCR);
// If the existing information is != x, do not use the information from
// a chained predicate, as the != x information is more likely to be
Expand Down Expand Up @@ -1863,7 +1867,8 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
const ValueLatticeElement &State = getValueState(Op);
if (State.isUnknownOrUndef())
return;
OpRanges.push_back(getConstantRange(State, Op->getType()));
OpRanges.push_back(
getConstantRange(State, Op->getType(), /*UndefAllowed=*/false));
}

ConstantRange Result =
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/SCCP/ip-add-range-to-call.ll
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ exit:
}

define i32 @caller5() {
; CHECK-LABEL: define range(i32 200, 401) i32 @caller5() {
; CHECK-LABEL: define i32 @caller5() {
; CHECK-NEXT: [[C1:%.*]] = call i32 @callee5(i32 10, i32 100)
; CHECK-NEXT: [[C2:%.*]] = call i32 @callee5(i32 20, i32 200)
; CHECK-NEXT: [[A:%.*]] = add i32 [[C1]], [[C2]]
Expand Down
13 changes: 6 additions & 7 deletions llvm/test/Transforms/SCCP/range-with-undef.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
; RUN: opt -S -passes=ipsccp < %s | FileCheck %s

; Make sure that constant ranges including undef are propagated correctly.
; FIXME: All of the following are currently miscompiled.

define i8 @test_binop(i1 %cond, i8 %a) {
; CHECK-LABEL: define i8 @test_binop(
Expand All @@ -15,7 +14,7 @@ define i8 @test_binop(i1 %cond, i8 %a) {
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi i16 [ undef, %[[ENTRY]] ], [ [[A_EXT]], %[[IF]] ]
; CHECK-NEXT: [[AND:%.*]] = and i16 [[PHI]], -1
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i16 [[AND]] to i8
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i16 [[AND]] to i8
; CHECK-NEXT: ret i8 [[TRUNC]]
;
entry:
Expand Down Expand Up @@ -43,7 +42,7 @@ define i8 @test_cast(i1 %cond, i8 %a) {
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi i16 [ undef, %[[ENTRY]] ], [ [[A_EXT]], %[[IF]] ]
; CHECK-NEXT: [[ZEXT:%.*]] = zext i16 [[PHI]] to i32
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i32 [[ZEXT]] to i8
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[ZEXT]] to i8
; CHECK-NEXT: ret i8 [[TRUNC]]
;
entry:
Expand All @@ -61,7 +60,7 @@ join:
}

define i8 @test_intrin(i1 %cond, i8 %a) {
; CHECK-LABEL: define range(i8 42, 0) i8 @test_intrin(
; CHECK-LABEL: define i8 @test_intrin(
; CHECK-SAME: i1 [[COND:%.*]], i8 [[A:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*]]:
; CHECK-NEXT: br i1 [[COND]], label %[[IF:.*]], label %[[JOIN:.*]]
Expand All @@ -71,7 +70,7 @@ define i8 @test_intrin(i1 %cond, i8 %a) {
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi i16 [ undef, %[[ENTRY]] ], [ [[A_EXT]], %[[IF]] ]
; CHECK-NEXT: [[UMAX:%.*]] = call i16 @llvm.umax.i16(i16 [[PHI]], i16 42)
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i16 [[UMAX]] to i8
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i16 [[UMAX]] to i8
; CHECK-NEXT: ret i8 [[TRUNC]]
;
entry:
Expand All @@ -89,7 +88,7 @@ join:
}

define i9 @test_with_overflow(i1 %cond, i8 %a) {
; CHECK-LABEL: define range(i9 1, -255) i9 @test_with_overflow(
; CHECK-LABEL: define i9 @test_with_overflow(
; CHECK-SAME: i1 [[COND:%.*]], i8 [[A:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*]]:
; CHECK-NEXT: br i1 [[COND]], label %[[IF:.*]], label %[[JOIN:.*]]
Expand All @@ -100,7 +99,7 @@ define i9 @test_with_overflow(i1 %cond, i8 %a) {
; CHECK-NEXT: [[PHI:%.*]] = phi i16 [ undef, %[[ENTRY]] ], [ [[A_EXT]], %[[IF]] ]
; CHECK-NEXT: [[WO:%.*]] = call { i16, i1 } @llvm.uadd.with.overflow.i16(i16 [[PHI]], i16 1)
; CHECK-NEXT: [[ADD:%.*]] = extractvalue { i16, i1 } [[WO]], 0
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i16 [[ADD]] to i9
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i16 [[ADD]] to i9
; CHECK-NEXT: ret i9 [[TRUNC]]
;
entry:
Expand Down
Loading