{ "bug_id": "105785", "issue_url": "https://github.com/llvm/llvm-project/issues/105785", "bug_type": "miscompilation", "base_commit": "1241c762c165972690c4edfb82ec7421c1e64658", "knowledge_cutoff": "2024-08-23T05:45:52Z", "lit_test_dir": [ "llvm/test/Transforms/ConstraintElimination" ], "hints": { "fix_commit": "85b6aac7c25f9d2a976a76045ace1e7afebb5965", "components": [ "ConstraintElimination" ], "bug_location_lineno": { "llvm/lib/Transforms/Scalar/ConstraintElimination.cpp": [ [ 1464, 1470 ] ] }, "bug_location_funcname": { "llvm/lib/Transforms/Scalar/ConstraintElimination.cpp": [ "checkAndReplaceCmp" ] } }, "patch": "commit 3ff9d92aae0945daa85ec6f85f05a3aeaaa9f962\nAuthor: Yingwei Zheng \nDate: Fri Aug 23 16:06:00 2024 +0800\n\n [ConstraintElim] Fix miscompilation caused by PR97974 (#105790)\n \n Fixes https://github.com/llvm/llvm-project/issues/105785.\n \n (cherry picked from commit 85b6aac7c25f9d2a976a76045ace1e7afebb5965)\n\ndiff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp\nindex c31173879af1..37022104d0a9 100644\n--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp\n+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp\n@@ -1464,7 +1464,7 @@ static bool checkAndReplaceCmp(CmpIntrinsic *I, ConstraintInfo &Info,\n ToRemove.push_back(I);\n return true;\n }\n- if (checkCondition(ICmpInst::ICMP_EQ, LHS, RHS, I, Info)) {\n+ if (checkCondition(ICmpInst::ICMP_EQ, LHS, RHS, I, Info).value_or(false)) {\n I->replaceAllUsesWith(ConstantInt::get(I->getType(), 0));\n ToRemove.push_back(I);\n return true;\n", "tests": [ { "file": "llvm/test/Transforms/ConstraintElimination/pr105785.ll", "commands": [ "opt -passes=constraint-elimination -S %s" ], "tests": [ { "test_name": "pr105785", "test_body": "define void @pr105785(ptr %p) {\nentry:\n br label %for.cond\n\nfor.cond: ; preds = %for.cond1, %entry\n %for.ind = phi i32 [ 0, %entry ], [ 1, %for.cond1 ]\n %cmp = icmp eq i32 %for.ind, 0\n br i1 %cmp, label %for.cond1, label %for.end6\n\nfor.cond1: ; preds = %for.body3, %for.cond\n %for.ind2 = phi i32 [ %inc, %for.body3 ], [ 0, %for.cond ]\n %cmp2 = icmp ult i32 %for.ind2, 3\n br i1 %cmp2, label %for.body3, label %for.cond\n\nfor.body3: ; preds = %for.cond1\n %scmp = call i32 @llvm.scmp.i32.i32(i32 %for.ind, i32 1)\n store i32 %scmp, ptr %p, align 4\n %inc = add nuw nsw i32 %for.ind2, 1\n br label %for.cond1\n\nfor.end6: ; preds = %for.cond\n ret void\n}\n\n; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)\ndeclare i32 @llvm.scmp.i32.i32(i32, i32) #0\n\nattributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }\n" } ] } ], "issue": { "title": "[ConstraintElim] Miscompilation with cmp intrinsic", "body": "Reproducer: https://alive2.llvm.org/ce/z/Tvz2NA\r\n```\r\n; bin/opt -passes=constraint-elimination test.ll -S\r\ndefine void @h(ptr %p) {\r\nentry:\r\n br label %for.cond\r\n\r\nfor.cond: ; preds = %for.cond1, %entry\r\n %storemerge = phi i32 [ 0, %entry ], [ 1, %for.cond1 ]\r\n %cmp = icmp eq i32 %storemerge, 0\r\n br i1 %cmp, label %for.cond1, label %for.end6\r\n\r\nfor.cond1: ; preds = %for.cond, %for.body3\r\n %i.0 = phi i32 [ %inc, %for.body3 ], [ 0, %for.cond ]\r\n %cmp2 = icmp ult i32 %i.0, 3\r\n br i1 %cmp2, label %for.body3, label %for.cond\r\n\r\nfor.body3: ; preds = %for.cond1\r\n %sub.i = tail call range(i32 -1, 2) i32 @llvm.scmp.i32.i32(i32 1, i32 %storemerge)\r\n store i32 %sub.i, ptr %p, align 4\r\n %inc = add nuw nsw i32 %i.0, 1\r\n br label %for.cond1\r\n\r\nfor.end6:\r\n ret void\r\n}\r\n```\r\n\r\n```\r\ndefine void @h(ptr %p) {\r\nentry:\r\n br label %for.cond\r\n\r\nfor.cond: ; preds = %for.cond1, %entry\r\n %storemerge = phi i32 [ 0, %entry ], [ 1, %for.cond1 ]\r\n %cmp = icmp eq i32 %storemerge, 0\r\n br i1 %cmp, label %for.cond1, label %for.end6\r\n\r\nfor.cond1: ; preds = %for.body3, %for.cond\r\n %i.0 = phi i32 [ %inc, %for.body3 ], [ 0, %for.cond ]\r\n %cmp2 = icmp ult i32 %i.0, 3\r\n br i1 %cmp2, label %for.body3, label %for.cond\r\n\r\nfor.body3: ; preds = %for.cond1\r\n store i32 0, ptr %p, align 4\r\n %inc = add nuw nsw i32 %i.0, 1\r\n br label %for.cond1\r\n\r\nfor.end6: ; preds = %for.cond\r\n ret void\r\n}\r\n```\r\n\r\nThe result of scmp should be 1 instead of 0.\r\n\r\nReduced C reproducer:\r\n```\r\n#include \r\n#include \r\n\r\nint builtin_scmp(int d, int e) { return (d > e) - (d < e); }\r\nint32_t f = 0;\r\nint64_t g = 0;\r\nvoid h() {\r\n for (f = 0; f <= 0; f++) {\r\n int i;\r\n for (i = 0; i < 3; i++)\r\n g = builtin_scmp(1, f);\r\n }\r\n}\r\nint main() {\r\n h();\r\n printf(\"%d\\n\", (int)g);\r\n return 0;\r\n}\r\n```\r\n\r\nI will post a fix later.\r\n", "author": "dtcxzyw", "labels": [ "miscompilation", "llvm:transforms" ], "comments": [] }, "verified": true }