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

[ConstraintElim] Fix miscompilation caused by PR97974 #105790

Merged
merged 2 commits into from
Aug 23, 2024

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Aug 23, 2024

Fixes #105785.

The original version of #97974 was correct. I don't know what happened :(

@llvmbot
Copy link
Member

llvmbot commented Aug 23, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

Changes

Fixes #105785.

The original version of #97974 was correct. I don't know what happened :(


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (+1-1)
  • (added) llvm/test/Transforms/ConstraintElimination/pr105785.ll (+46)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 84ccf06d16d5e8..6565aed4bc390c 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1464,7 +1464,7 @@ static bool checkAndReplaceCmp(CmpIntrinsic *I, ConstraintInfo &Info,
     ToRemove.push_back(I);
     return true;
   }
-  if (checkCondition(ICmpInst::ICMP_EQ, LHS, RHS, I, Info)) {
+  if (checkCondition(ICmpInst::ICMP_EQ, LHS, RHS, I, Info).value_or(false)) {
     I->replaceAllUsesWith(ConstantInt::get(I->getType(), 0));
     ToRemove.push_back(I);
     return true;
diff --git a/llvm/test/Transforms/ConstraintElimination/pr105785.ll b/llvm/test/Transforms/ConstraintElimination/pr105785.ll
new file mode 100644
index 00000000000000..6c340a11dd2e2c
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/pr105785.ll
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
+
+define void @pr105785(ptr %p) {
+; CHECK-LABEL: define void @pr105785(
+; CHECK-SAME: ptr [[P:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[FOR_COND:.*]]
+; CHECK:       [[FOR_COND]]:
+; CHECK-NEXT:    [[FOR_IND:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ 1, %[[FOR_COND1:.*]] ]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[FOR_IND]], 0
+; CHECK-NEXT:    br i1 [[CMP]], label %[[FOR_COND1]], label %[[FOR_END6:.*]]
+; CHECK:       [[FOR_COND1]]:
+; CHECK-NEXT:    [[FOR_IND2:%.*]] = phi i32 [ [[INC:%.*]], %[[FOR_BODY3:.*]] ], [ 0, %[[FOR_COND]] ]
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult i32 [[FOR_IND2]], 3
+; CHECK-NEXT:    br i1 [[CMP2]], label %[[FOR_BODY3]], label %[[FOR_COND]]
+; CHECK:       [[FOR_BODY3]]:
+; CHECK-NEXT:    [[SCMP:%.*]] = call i32 @llvm.scmp.i32.i32(i32 [[FOR_IND]], i32 1)
+; CHECK-NEXT:    store i32 [[SCMP]], ptr [[P]], align 4
+; CHECK-NEXT:    [[INC]] = add nuw nsw i32 [[FOR_IND2]], 1
+; CHECK-NEXT:    br label %[[FOR_COND1]]
+; CHECK:       [[FOR_END6]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %for.cond
+
+for.cond:                                         ; preds = %for.cond1, %entry
+  %for.ind = phi i32 [ 0, %entry ], [ 1, %for.cond1 ]
+  %cmp = icmp eq i32 %for.ind, 0
+  br i1 %cmp, label %for.cond1, label %for.end6
+
+for.cond1:                                        ; preds = %for.cond, %for.body3
+  %for.ind2 = phi i32 [ %inc, %for.body3 ], [ 0, %for.cond ]
+  %cmp2 = icmp ult i32 %for.ind2, 3
+  br i1 %cmp2, label %for.body3, label %for.cond
+
+for.body3:                                        ; preds = %for.cond1
+  %scmp = call i32 @llvm.scmp.i32.i32(i32 %for.ind, i32 1)
+  store i32 %scmp, ptr %p, align 4
+  %inc = add nuw nsw i32 %for.ind2, 1
+  br label %for.cond1
+
+for.end6:
+  ret void
+}

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

@dtcxzyw dtcxzyw merged commit 85b6aac into llvm:main Aug 23, 2024
8 of 10 checks passed
@dtcxzyw dtcxzyw deleted the fix-pr105785 branch August 23, 2024 08:06
@dtcxzyw dtcxzyw added this to the LLVM 19.X Release milestone Aug 23, 2024
@dtcxzyw
Copy link
Member Author

dtcxzyw commented Aug 23, 2024

/cherry-pick 85b6aac

llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 23, 2024

/pull-request #105797

cjdb pushed a commit to cjdb/llvm-project that referenced this pull request Aug 23, 2024
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 26, 2024
dmpolukhin pushed a commit to dmpolukhin/llvm-project that referenced this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[ConstraintElim] Miscompilation with cmp intrinsic
3 participants