Sync
Browse files- dataset.jsonl +1 -0
- dataset/131071.json +125 -0
- examples/baseline.py +64 -7
- scripts/llvm_helper.py +3 -2
dataset.jsonl
CHANGED
@@ -74,6 +74,7 @@
|
|
74 |
{"bug_id": "121428", "issue_url": "https://github.com/llvm/llvm-project/issues/121428", "bug_type": "miscompilation", "base_commit": "b5f21671ef04984bc00770263234dfb94833a274", "knowledge_cutoff": "2025-01-01T09:27:44Z", "lit_test_dir": ["llvm/test/Transforms/InstCombine"], "hints": {"fix_commit": "a37dbc1f51c70d92fd209c2e52a9d794eb15e4e7", "components": ["InstCombine"], "bug_location_lineno": {"llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp": [[1225, 1232]]}, "bug_location_funcname": {"llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp": ["foldSelectCttzCtlz"]}}, "patch": "commit a37dbc1f51c70d92fd209c2e52a9d794eb15e4e7\nAuthor: Yingwei Zheng <[email protected]>\nDate: Mon Jan 6 00:04:28 2025 +0800\n\n [InstCombine] Drop noundef in `foldSelectCttzCtlz` (#121692)\n \n Close https://github.com/llvm/llvm-project/issues/121428\n\ndiff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp\nindex e7a8e947705f..a18b927678ef 100644\n--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp\n+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp\n@@ -1225,8 +1225,12 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,\n // zext/trunc) have one use (ending at the select), the cttz/ctlz result will\n // not be used if the input is zero. Relax to 'zero is poison' for that case.\n if (II->hasOneUse() && SelectArg->hasOneUse() &&\n- !match(II->getArgOperand(1), m_One()))\n+ !match(II->getArgOperand(1), m_One())) {\n II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));\n+ // noundef attribute on the intrinsic may no longer be valid.\n+ II->dropUBImplyingAttrsAndMetadata();\n+ IC.addToWorklist(II);\n+ }\n \n return nullptr;\n }\n", "tests": [{"file": "llvm/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll", "commands": ["opt -passes=instcombine -S < %s"], "tests": [{"test_name": "test_cttz_not_bw_multiuse", "test_body": "define i32 @test_cttz_not_bw_multiuse(i32 %x) {\n %ct = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %sel = select i1 %cmp, i32 %ct, i32 123\n %res = or i32 %sel, %ct\n ret i32 %res\n}\n\n; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)\ndeclare i32 @llvm.cttz.i32(i32, i1 immarg) #0\n\nattributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }\n"}, {"test_name": "test_cttz_not_bw", "test_body": "define i32 @test_cttz_not_bw(i32 %x) {\n %ct = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %res = select i1 %cmp, i32 %ct, i32 123\n ret i32 %res\n}\n\n; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)\ndeclare i32 @llvm.cttz.i32(i32, i1 immarg) #0\n\nattributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }\n"}, {"test_name": "test_cttz_not_bw_noundef", "test_body": "define i32 @test_cttz_not_bw_noundef(i32 %x) {\n %ct = tail call noundef i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %res = select i1 %cmp, i32 %ct, i32 123\n ret i32 %res\n}\n\n; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)\ndeclare i32 @llvm.cttz.i32(i32, i1 immarg) #0\n\nattributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }\n"}]}], "issue": {"title": "[InstCombine] `noundef` attribute should be dropped", "body": "Alive2: https://alive2.llvm.org/ce/z/wKsUWK\n```\ndefine i32 @src(i32 noundef %x) {\n %ct = call noundef i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %res = select i1 %cmp, i32 %ct, i32 123\n ret i32 %res\n}\ndefine i32 @tgt(i32 noundef %x) {\n %ct = call noundef range(i32 0, 33) i32 @llvm.cttz.i32(i32 %x, i1 true)\n %cmp.not = icmp eq i32 %x, 0\n %res = select i1 %cmp.not, i32 123, i32 %ct\n ret i32 %res\n}\n```\n", "author": "dtcxzyw", "labels": ["miscompilation", "llvm:instcombine"], "comments": []}, "verified": true}
|
75 |
{"bug_id": "98753", "issue_url": "https://github.com/llvm/llvm-project/issues/98753", "bug_type": "miscompilation", "base_commit": "c30ce8b9d33d1050ead549705702c1472b7a7d3f", "knowledge_cutoff": "2024-07-13T17:35:41Z", "lit_test_dir": ["llvm/test/Transforms/InstSimplify"], "hints": {"fix_commit": "de29b850f03092195bf21f3a39402adb4ed3c216", "components": ["InstructionSimplify"], "bug_location_lineno": {"llvm/lib/Analysis/InstructionSimplify.cpp": [[1975, 1987], [4300, 4305], [4347, 4352], [4467, 4472], [4606, 4612]]}, "bug_location_funcname": {"llvm/lib/Analysis/InstructionSimplify.cpp": ["llvm::simplifyWithOpReplaced", "simplifyAndOrWithICmpEq", "simplifySelectWithICmpEq"]}}, "patch": "commit de29b850f03092195bf21f3a39402adb4ed3c216\nAuthor: Nikita Popov <[email protected]>\nDate: Tue Jul 16 11:40:04 2024 +0200\n\n [InstSimplify] Fix simplifyAndOrWithICmpEq with undef refinement (#98898)\n \n The final case in Simplify (where Res == Absorber and the predicate is\n inverted) is not generally safe when the simplification is a refinement.\n In particular, we may simplify assuming a specific value for undef, but\n then chose a different one later.\n \n However, it *is* safe to refine poison in this context, unlike in the\n equivalent select folds. This is the reason why this fold did not use\n AllowRefinement=false in the first place, and using that option would\n introduce a lot of test regressions.\n \n This patch takes the middle path of disabling undef refinements in\n particular using the getWithoutUndef() SimplifyQuery option. However,\n this option doesn't actually work in this case, because the problematic\n fold is inside constant folding, and we currently don't propagate this\n option all the way from InstSimplify over ConstantFolding to\n ConstantFold. Work around this by explicitly checking for undef operands\n in simplifyWithOpReplaced().\n \n Finally, make sure that places where AllowRefinement=false also use\n Q.getWithoutUndef(). I don't have a specific test case for this (the\n original one does not work because we don't simplify selects with\n constant condition in this mode in the first place) but this seems like\n the correct thing to do to be conservative.\n \n Fixes https://github.com/llvm/llvm-project/issues/98753.\n\ndiff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp\nindex 242c200f7ef1..3a7ae577bb06 100644\n--- a/llvm/lib/Analysis/InstructionSimplify.cpp\n+++ b/llvm/lib/Analysis/InstructionSimplify.cpp\n@@ -1975,13 +1975,16 @@ static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,\n return nullptr;\n };\n \n- if (Value *Res =\n- simplifyWithOpReplaced(Op1, A, B, Q, /* AllowRefinement */ true,\n- /* DropFlags */ nullptr, MaxRecurse))\n+ // In the final case (Res == Absorber with inverted predicate), it is safe to\n+ // refine poison during simplification, but not undef. For simplicity always\n+ // disable undef-based folds here.\n+ if (Value *Res = simplifyWithOpReplaced(Op1, A, B, Q.getWithoutUndef(),\n+ /* AllowRefinement */ true,\n+ /* DropFlags */ nullptr, MaxRecurse))\n return Simplify(Res);\n- if (Value *Res =\n- simplifyWithOpReplaced(Op1, B, A, Q, /* AllowRefinement */ true,\n- /* DropFlags */ nullptr, MaxRecurse))\n+ if (Value *Res = simplifyWithOpReplaced(Op1, B, A, Q.getWithoutUndef(),\n+ /* AllowRefinement */ true,\n+ /* DropFlags */ nullptr, MaxRecurse))\n return Simplify(Res);\n \n return nullptr;\n@@ -4300,6 +4303,9 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,\n bool AllowRefinement,\n SmallVectorImpl<Instruction *> *DropFlags,\n unsigned MaxRecurse) {\n+ assert((AllowRefinement || !Q.CanUseUndef) &&\n+ \"If AllowRefinement=false then CanUseUndef=false\");\n+\n // Trivial replacement.\n if (V == Op)\n return RepOp;\n@@ -4347,6 +4353,11 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,\n } else {\n NewOps.push_back(InstOp);\n }\n+\n+ // Bail out if any operand is undef and SimplifyQuery disables undef\n+ // simplification. Constant folding currently doesn't respect this option.\n+ if (isa<UndefValue>(NewOps.back()) && !Q.CanUseUndef)\n+ return nullptr;\n }\n \n if (!AnyReplaced)\n@@ -4467,6 +4478,11 @@ Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,\n const SimplifyQuery &Q,\n bool AllowRefinement,\n SmallVectorImpl<Instruction *> *DropFlags) {\n+ // If refinement is disabled, also disable undef simplifications (which are\n+ // always refinements) in SimplifyQuery.\n+ if (!AllowRefinement)\n+ return ::simplifyWithOpReplaced(V, Op, RepOp, Q.getWithoutUndef(),\n+ AllowRefinement, DropFlags, RecursionLimit);\n return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags,\n RecursionLimit);\n }\n@@ -4606,7 +4622,7 @@ static Value *simplifySelectWithICmpEq(Value *CmpLHS, Value *CmpRHS,\n Value *TrueVal, Value *FalseVal,\n const SimplifyQuery &Q,\n unsigned MaxRecurse) {\n- if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q,\n+ if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q.getWithoutUndef(),\n /* AllowRefinement */ false,\n /* DropFlags */ nullptr, MaxRecurse) == TrueVal)\n return FalseVal;\n", "tests": [{"file": "llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll", "commands": ["opt -S -passes=instsimplify < %s"], "tests": [{"test_name": "and_is_constant", "test_body": "define i1 @and_is_constant(ptr %arg, ptr %arg2) {\n %icmp = icmp eq ptr %arg, %arg2\n %call = call i1 @llvm.is.constant.i1(i1 %icmp)\n %and = and i1 %call, %icmp\n ret i1 %and\n}\n\n; Function Attrs: convergent nocallback nofree nosync nounwind willreturn memory(none)\ndeclare i1 @llvm.is.constant.i1(i1) #0\n\nattributes #0 = { convergent nocallback nofree nosync nounwind willreturn memory(none) }\n"}, {"test_name": "pr98753", "test_body": "define i1 @pr98753(i32 noundef %x, i32 %y) {\n %cmp1 = icmp ne i32 %x, 0\n %sel = select i1 %cmp1, i32 %y, i32 undef\n %cmp2 = icmp sgt i32 %sel, 0\n %and = and i1 %cmp1, %cmp2\n ret i1 %and\n}\n"}]}], "issue": {"title": "Miscompilation with libstdc++'s std::optional<int> and -O1", "body": "https://godbolt.org/z/KjEreWf57\r\n\r\nWith Clang 18.1 and libstdc++, we get different behavior when compared to Clang 17 in the code below.\r\nEDIT: requires at least -O1.\r\n\r\n```cpp\r\n#include <optional>\r\n#include <iostream>\r\n\r\n// defined in a separate compilation unit\r\nint takeIntRefAndReturn0(int&);\r\n\r\nstd::optional<int> shouldReturnEmptyOptional() {\r\n int v = 5;\r\n if (takeIntRefAndReturn0(v))\r\n return v;\r\n return std::nullopt;\r\n}\r\n\r\nint main() {\r\n auto opt = shouldReturnEmptyOptional();\r\n if (opt && *opt > 0) {\r\n std::cout << \"SHOULD NOT BE PRINTED: *opt = \" << *opt << std::endl;\r\n return 1;\r\n }\r\n std::cout << \"SHOULD BE PRINTED\" << std::endl;\r\n}\r\n```\r\n\r\nWith Clang 17, we get `SHOULD BE PRINTED`, while with with Clang 18.1 we get `SHOULD NOT BE PRINTED: *opt = 5`.\r\n\r\nWith git-bisect, I found that this is caused by https://github.com/llvm/llvm-project/commit/060de415af335fdd82910f409e2be3b8457eaa43.\r\n\r\nAn isomorphic example to reproduce this (https://godbolt.org/z/9PsjY17sT):\r\n```cpp\r\nint takeIntRefReturn0(int &);\r\nvoid assertNotReached(int);\r\n\r\nstatic bool logicalAnd(bool a, bool b) { return a && b; }\r\n\r\nint main() {\r\n int v4;\r\n bool v3;\r\n {\r\n int v1 = 5;\r\n int v2 = takeIntRefReturn0(v1);\r\n v3 = v2 != 0;\r\n if (v3)\r\n v4 = v1;\r\n }\r\n // Move to a function so that && is not short cutted.\r\n // v4 will be undefined if v2 == 0, but v3 is false, so the branch shouldn't be entered.\r\n if (logicalAnd(v3, v4 > 0))\r\n assertNotReached(v4);\r\n\r\n return 0;\r\n}\r\n```\r\nNote in the generated LLVM IR that\r\n```llvm\r\n%6 = icmp sgt i32 %5, 0\r\n%7 = and i1 %3, %6\r\nbr i1 %7, label %8, label %9\r\n``` \r\nwas reduced to only\r\n```llvm\r\n%6 = icmp sgt i32 %5, 0\r\nbr i1 %6, label %7, label %8\r\n```", "author": "cadubentzen", "labels": ["regression", "miscompilation", "llvm:analysis"], "comments": [{"author": "dtcxzyw", "body": "Reproducer: https://alive2.llvm.org/ce/z/p4ZGxe\r\n```\r\n; bin/opt -passes=instcombine test.ll -S\r\ndefine i1 @src(i32 noundef %x, i32 %y) {\r\n %3 = icmp ne i32 %x, 0\r\n %5 = select i1 %3, i32 %y, i32 undef\r\n %6 = icmp sgt i32 %5, 0\r\n %7 = and i1 %3, %6\r\n br i1 %7, label %if.then, label %if.else\r\n\r\nif.then:\r\n call void @use(i32 noundef %5)\r\n ret i1 true\r\n\r\nif.else:\r\n ret i1 false\r\n}\r\n\r\ndefine i1 @tgt(i32 noundef %x, i32 %y) {\r\n %.not = icmp eq i32 %x, 0\r\n %1 = select i1 %.not, i32 undef, i32 %y\r\n %2 = icmp sgt i32 %1, 0\r\n br i1 %2, label %if.then, label %if.else\r\n\r\nif.then: ; preds = %0\r\n call void @use(i32 noundef %1)\r\n ret i1 true\r\n\r\nif.else: ; preds = %0\r\n ret i1 false\r\n}\r\n\r\ndeclare void @use(i32 %x)\r\n```\r\ncc @nikic"}, {"author": "nikic", "body": "A bit simpler using just instsimplify: https://alive2.llvm.org/ce/z/tnkf6A"}, {"author": "nikic", "body": "I expect the fix here is to perform replacements with `Q.getWithoutUndef()`. But probably not just here, but also in other places using simplifyWithOpReplaced(). (Possibly making it always use that mode.)"}, {"author": "cadubentzen", "body": "Thanks a lot for fixing this so quickly! I wonder if this should be included in 18.1.9?"}, {"author": "nikic", "body": "The LLVM 18 branch is already closed, there will be no further releases."}, {"author": "thesamesam", "body": "I'll add the milestone anyway as it's easier to track for downstream backports then."}]}, "verified": true}
|
76 |
{"bug_id": "98435", "issue_url": "https://github.com/llvm/llvm-project/issues/98435", "bug_type": "miscompilation", "base_commit": "0e124537aabea0d18e030226a3ca4c6f494c1322", "knowledge_cutoff": "2024-07-11T06:06:08Z", "lit_test_dir": ["llvm/test/Transforms/InstCombine"], "hints": {"fix_commit": "9304af3927caecdb43d3a9b5d16c6a5b7a6b5594", "components": ["InstCombine"], "bug_location_lineno": {"llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp": [[1735, 1751]]}, "bug_location_funcname": {"llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp": ["InstCombinerImpl::SimplifyDemandedVectorElts"]}}, "patch": "commit 9304af3927caecdb43d3a9b5d16c6a5b7a6b5594\nAuthor: Jorge Botto <[email protected]>\nDate: Fri Aug 9 11:52:56 2024 +0100\n\n [InstCombine] Fixing wrong select folding in vectors with undef elements (#102244)\n \n This PR fixes https://github.com/llvm/llvm-project/issues/98435.\n `SimplifyDemandedVectorElts` mishandles the undef by assuming that\n !isNullValue() means the condition is true.\n \n By preventing any value that we're not certain equals 1 or 0, it avoids\n having to make any particular choice by not demanding bits from a\n particular branch with potentially picking a wrong value.\n \n Proof: https://alive2.llvm.org/ce/z/r8CmEu\n\ndiff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp\nindex c494fec84c1e..153d8c238ed4 100644\n--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp\n+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp\n@@ -1735,17 +1735,12 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,\n APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);\n if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {\n for (unsigned i = 0; i < VWidth; i++) {\n- // isNullValue() always returns false when called on a ConstantExpr.\n- // Skip constant expressions to avoid propagating incorrect information.\n Constant *CElt = CV->getAggregateElement(i);\n- if (isa<ConstantExpr>(CElt))\n- continue;\n- // TODO: If a select condition element is undef, we can demand from\n- // either side. If one side is known undef, choosing that side would\n- // propagate undef.\n+\n+ // isNullValue() always returns false when called on a ConstantExpr.\n if (CElt->isNullValue())\n DemandedLHS.clearBit(i);\n- else\n+ else if (CElt->isOneValue())\n DemandedRHS.clearBit(i);\n }\n }\n", "tests": [{"file": "llvm/test/Transforms/InstCombine/pr98435.ll", "commands": ["opt -S -passes=instcombine < %s 2>&1"], "tests": [{"test_name": "pr98435", "test_body": "define <2 x i1> @pr98435(<2 x i1> %val) {\n %val1 = select <2 x i1> <i1 undef, i1 true>, <2 x i1> splat (i1 true), <2 x i1> %val\n ret <2 x i1> %val1\n}\n"}]}], "issue": {"title": "Wrong select folding for undef conditions by InstCombine", "body": "https://github.com/llvm/llvm-project/blob/7eae9bb856135136cddc4208a2b1546e9db44c9c/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L3582-L3585\r\n\r\nAlive2 report: https://alive2.llvm.org/ce/z/Qtu72J\r\n\r\n\r\n```ll\r\n----------------------------------------\r\ndefine <2 x i1> @fun0(<2 x i1> %val0, <2 x i1> %val1) {\r\n#0:\r\n %val2 = select <2 x i1> %val0, <2 x i1> %val0, <2 x i1> %val0\r\n %val3 = select <2 x i1> { undef, 1 }, <2 x i1> { 1, 1 }, <2 x i1> %val2\r\n ret <2 x i1> %val3\r\n}\r\n=>\r\ndefine <2 x i1> @fun0(<2 x i1> %val0, <2 x i1> %val1) {\r\n#0:\r\n ret <2 x i1> { poison, 1 }\r\n}\r\nTransformation doesn't verify!\r\n\r\nERROR: Target is more poisonous than source\r\n\r\nExample:\r\n<2 x i1> %val0 = < #x0 (0), #x0 (0) >\r\n<2 x i1> %val1 = < #x0 (0), #x0 (0) >\r\n\r\nSource:\r\n<2 x i1> %val2 = < #x0 (0), #x0 (0) >\r\n<2 x i1> %val3 = < #x0 (0)\t[based on undef value], #x1 (1) >\r\n\r\nTarget:\r\nSource value: < #x0 (0), #x1 (1) >\r\nTarget value: < poison, #x1 (1) >\r\n\r\nSummary:\r\n 0 correct transformations\r\n 1 incorrect transformations\r\n 0 failed-to-prove transformations\r\n 0 Alive2 errors\r\n```", "author": "bongjunj", "labels": ["miscompilation", "llvm:instcombine"], "comments": [{"author": "jf-botto", "body": "Would this issue be to fix InstCombine to not try to optimise it or get InstCombine to pick `<2 x i1> { 1, 1 }` because `undef` let's it pick either values and picking a constant vector is better than picking a variable?"}, {"author": "nikic", "body": "The problematic step seems to be this one: https://alive2.llvm.org/ce/z/C2waWU\r\n\r\nI don't think the quoted code is responsible for this, as it is guarded by `!isa<Constant>(CondVal)`."}, {"author": "jf-botto", "body": "I think the problem is with how `SimplifyDemandedVectorElts` handles the undef: https://github.com/llvm/llvm-project/blob/af80d3a248101d6f5d9d5e229c7899136b8ce0b8/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp#L1721-L1760"}, {"author": "nikic", "body": "@jf-botto Agree. It assumes that !isNullValue() means \"is true\", which is not the case."}]}, "verified": true}
|
|
|
77 |
{"bug_id": "109468", "issue_url": "https://github.com/llvm/llvm-project/issues/109468", "bug_type": "crash", "base_commit": "51039101cf32591782ef564a108db71150a3b7c3", "knowledge_cutoff": "2024-09-20T20:22:11Z", "lit_test_dir": ["llvm/test/Transforms/LoopVectorize"], "hints": {"fix_commit": "a068b974b199b0e7350da2d9506adc7df3995ce3", "components": ["LoopVectorize"], "bug_location_lineno": {"llvm/lib/Transforms/Vectorize/VPlan.h": [[2709, 2714], [2787, 2792]], "llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp": [[2267, 2272], [2363, 2368]]}, "bug_location_funcname": {"llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp": ["VPWidenLoadEVLRecipe::execute", "VPWidenLoadEVLRecipe::print", "VPWidenStoreEVLRecipe::execute", "VPWidenStoreEVLRecipe::print"]}}, "patch": "commit a068b974b199b0e7350da2d9506adc7df3995ce3\nAuthor: Elvis Wang <[email protected]>\nDate: Thu Sep 26 07:10:25 2024 +0800\n\n [VPlan] Implement VPWidenLoad/StoreEVLRecipe::computeCost(). (#109644)\n \n Currently the EVL recipes transfer the tail masking to the EVL.\n But in the legacy cost model, the mask exist and will calculate the\n instruction cost of the mask.\n To fix the difference between the VPlan-based cost model and the legacy\n cost model, we always calculate the instruction cost for the mask in the\n EVL recipes.\n \n Note that we should remove the mask cost in the EVL recipes when we\n don't need to compare to the legacy cost model.\n \n This patch also fixes #109468.\n\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h\nindex bbcfaf9e19cd..23a24ce293ef 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlan.h\n+++ b/llvm/lib/Transforms/Vectorize/VPlan.h\n@@ -2709,6 +2709,10 @@ struct VPWidenLoadEVLRecipe final : public VPWidenMemoryRecipe, public VPValue {\n /// Generate the wide load or gather.\n void execute(VPTransformState &State) override;\n \n+ /// Return the cost of this VPWidenLoadEVLRecipe.\n+ InstructionCost computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const override;\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n /// Print the recipe.\n void print(raw_ostream &O, const Twine &Indent,\n@@ -2787,6 +2791,10 @@ struct VPWidenStoreEVLRecipe final : public VPWidenMemoryRecipe {\n /// Generate the wide store or scatter.\n void execute(VPTransformState &State) override;\n \n+ /// Return the cost of this VPWidenStoreEVLRecipe.\n+ InstructionCost computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const override;\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n /// Print the recipe.\n void print(raw_ostream &O, const Twine &Indent,\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp\nindex dacba152611c..9a0aefb898e5 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp\n@@ -2267,6 +2267,31 @@ void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {\n State.set(this, Res);\n }\n \n+InstructionCost VPWidenLoadEVLRecipe::computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const {\n+ if (!Consecutive || IsMasked)\n+ return VPWidenMemoryRecipe::computeCost(VF, Ctx);\n+\n+ // We need to use the getMaskedMemoryOpCost() instead of getMemoryOpCost()\n+ // here because the EVL recipes using EVL to replace the tail mask. But in the\n+ // legacy model, it will always calculate the cost of mask.\n+ // TODO: Using getMemoryOpCost() instead of getMaskedMemoryOpCost when we\n+ // don't need to compare to the legacy cost model.\n+ Type *Ty = ToVectorTy(getLoadStoreType(&Ingredient), VF);\n+ const Align Alignment =\n+ getLoadStoreAlignment(const_cast<Instruction *>(&Ingredient));\n+ unsigned AS =\n+ getLoadStoreAddressSpace(const_cast<Instruction *>(&Ingredient));\n+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;\n+ InstructionCost Cost = Ctx.TTI.getMaskedMemoryOpCost(\n+ Ingredient.getOpcode(), Ty, Alignment, AS, CostKind);\n+ if (!Reverse)\n+ return Cost;\n+\n+ return Cost + Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse,\n+ cast<VectorType>(Ty), {}, CostKind, 0);\n+}\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n void VPWidenLoadEVLRecipe::print(raw_ostream &O, const Twine &Indent,\n VPSlotTracker &SlotTracker) const {\n@@ -2363,6 +2388,31 @@ void VPWidenStoreEVLRecipe::execute(VPTransformState &State) {\n State.addMetadata(NewSI, SI);\n }\n \n+InstructionCost VPWidenStoreEVLRecipe::computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const {\n+ if (!Consecutive || IsMasked)\n+ return VPWidenMemoryRecipe::computeCost(VF, Ctx);\n+\n+ // We need to use the getMaskedMemoryOpCost() instead of getMemoryOpCost()\n+ // here because the EVL recipes using EVL to replace the tail mask. But in the\n+ // legacy model, it will always calculate the cost of mask.\n+ // TODO: Using getMemoryOpCost() instead of getMaskedMemoryOpCost when we\n+ // don't need to compare to the legacy cost model.\n+ Type *Ty = ToVectorTy(getLoadStoreType(&Ingredient), VF);\n+ const Align Alignment =\n+ getLoadStoreAlignment(const_cast<Instruction *>(&Ingredient));\n+ unsigned AS =\n+ getLoadStoreAddressSpace(const_cast<Instruction *>(&Ingredient));\n+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;\n+ InstructionCost Cost = Ctx.TTI.getMaskedMemoryOpCost(\n+ Ingredient.getOpcode(), Ty, Alignment, AS, CostKind);\n+ if (!Reverse)\n+ return Cost;\n+\n+ return Cost + Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse,\n+ cast<VectorType>(Ty), {}, CostKind, 0);\n+}\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n void VPWidenStoreEVLRecipe::print(raw_ostream &O, const Twine &Indent,\n VPSlotTracker &SlotTracker) const {\n", "tests": [{"file": "llvm/test/Transforms/LoopVectorize/RISCV/vectorize-force-tail-with-evl-uniform-store.ll", "commands": ["opt < %s --prefer-predicate-over-epilogue=predicate-dont-vectorize --passes=loop-vectorize -mcpu=sifive-p470 -mattr=+v,+f -force-tail-folding-style=data-with-evl -S"], "tests": [{"test_name": "lshift_significand", "test_body": "target datalayout = \"e-m:e-p:64:64-i64:64-i128:128-n32:64-S128\"\ntarget triple = \"riscv64-unknown-linux-gnu\"\n\ndefine void @lshift_significand(i32 %n, ptr nocapture writeonly %dst) {\nentry:\n %cmp1.peel = icmp eq i32 %n, 0\n %spec.select = select i1 %cmp1.peel, i64 2, i64 0\n br label %loop\n\nloop: ; preds = %loop, %entry\n %iv = phi i64 [ %spec.select, %entry ], [ %iv.next, %loop ]\n %0 = sub nuw nsw i64 1, %iv\n %arrayidx13 = getelementptr i64, ptr %dst, i64 %0\n store i64 0, ptr %arrayidx13, align 8\n %iv.next = add nuw nsw i64 %iv, 1\n %exitcond.not = icmp eq i64 %iv.next, 3\n br i1 %exitcond.not, label %exit, label %loop\n\nexit: ; preds = %loop\n ret void\n}\n"}]}], "issue": {"title": "[LV][VPlan] Crash due to disagreements on the VPlan cost v.s. legacy cost model", "body": "Given this input:\r\n``` llvm\r\ntarget datalayout = \"e-m:e-p:64:64-i64:64-i128:128-n32:64-S128\"\r\ntarget triple = \"riscv64-unknown-linux-gnu\"\r\n\r\ndefine void @lshift_significand(i32 %n, ptr %0) {\r\nentry:\r\n br label %for.cond\r\n\r\nfor.cond: ; preds = %for.cond, %entry\r\n %i.0 = phi i32 [ 0, %entry ], [ 2, %for.cond ]\r\n %add = or i32 %n, %i.0\r\n %cmp1 = icmp eq i32 %add, 0\r\n br i1 %cmp1, label %for.cond, label %for.cond7\r\n\r\nfor.cond7: ; preds = %for.body9, %for.cond\r\n %i.1 = phi i32 [ %i.0, %for.cond ], [ %inc15, %for.body9 ]\r\n %cmp8 = icmp ult i32 %i.1, 3\r\n br i1 %cmp8, label %for.body9, label %for.end16\r\n\r\nfor.body9: ; preds = %for.cond7\r\n %sub11 = sub nuw i32 1, %i.1\r\n %idxprom12 = zext i32 %sub11 to i64\r\n %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %idxprom12\r\n store i64 0, ptr %arrayidx13, align 8\r\n %inc15 = add i32 %i.1, 1\r\n br label %for.cond7\r\n\r\nfor.end16: ; preds = %for.cond7\r\n ret void\r\n}\r\n```\r\nAnd this command:\r\n``` bash\r\nopt -mcpu=sifive-p470 -O3 -prefer-predicate-over-epilogue=predicate-dont-vectorize -force-tail-folding-style=data-with-evl input.ll -disable-output\r\n```\r\nWe'll get the following assertion:\r\n```\r\nllvm/lib/Transforms/Vectorize/LoopVectorize.cpp:7386: VectorizationFactor llvm::LoopVectorizationPlanner::computeBestVF(): Assertion `(BestFactor.Width == LegacyVF.Width || planContainsAdditionalSimplifications(getPlanFor(BestFactor.Width), CostCtx, OrigLoop)) && \" VPlan cost model and legacy cost model disagreed\"' failed.\r\n```\r\n\r\n\r\nPreliminary investigation shows that this is caused by the disagreement on (reverse) widen store's cost. First, this is the trace for VPlan's cost model:\r\n```\r\nLV: Scalar loop costs: 4.\r\nCost of 1 for VF vscale x 1: induction instruction %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nCost of 0 for VF vscale x 1: induction instruction %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nCost of 1 for VF vscale x 1: exit condition instruction %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nCost of 0 for VF vscale x 1: EMIT vp<%4> = CANONICAL-INDUCTION ir<0>, vp<%12>\r\nCost of 0 for VF vscale x 1: EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<%5> = phi ir<0>, vp<%11>\r\nCost of 0 for VF vscale x 1: EMIT vp<%6> = EXPLICIT-VECTOR-LENGTH vp<%5>, vp<%3>\r\nCost of 0 for VF vscale x 1: vp<%7> = DERIVED-IV ir<%spec.select> + vp<%5> * ir<1>\r\nCost of 0 for VF vscale x 1: vp<%8> = SCALAR-STEPS vp<%7>, ir<1>\r\nCost of 1 for VF vscale x 1: CLONE ir<%1> = sub nuw nsw ir<1>, vp<%8>\r\nCost of 0 for VF vscale x 1: CLONE ir<%arrayidx13> = getelementptr ir<%0>, ir<0>, ir<%1>\r\nCost of 0 for VF vscale x 1: vp<%9> = vector-pointer (reverse) ir<%arrayidx13>\r\nCost of 8 for VF vscale x 1: WIDEN vp.store vp<%9>, ir<0>, vp<%6>\r\nCost of 0 for VF vscale x 1: SCALAR-CAST vp<%10> = zext vp<%6> to i64\r\nCost of 0 for VF vscale x 1: EMIT vp<%11> = add vp<%10>, vp<%5>\r\nCost of 0 for VF vscale x 1: EMIT vp<%12> = add vp<%4>, vp<%0>\r\nCost of 0 for VF vscale x 1: EMIT branch-on-count vp<%12>, vp<%1>\r\nCost of 0 for VF vscale x 1: vector loop backedge\r\nCost for VF vscale x 1: 11\r\nCost of 1 for VF vscale x 2: induction instruction %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nCost of 0 for VF vscale x 2: induction instruction %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nCost of 1 for VF vscale x 2: exit condition instruction %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nCost of 0 for VF vscale x 2: EMIT vp<%4> = CANONICAL-INDUCTION ir<0>, vp<%12>\r\nCost of 0 for VF vscale x 2: EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<%5> = phi ir<0>, vp<%11>\r\nCost of 0 for VF vscale x 2: EMIT vp<%6> = EXPLICIT-VECTOR-LENGTH vp<%5>, vp<%3>\r\nCost of 0 for VF vscale x 2: vp<%7> = DERIVED-IV ir<%spec.select> + vp<%5> * ir<1>\r\nCost of 0 for VF vscale x 2: vp<%8> = SCALAR-STEPS vp<%7>, ir<1>\r\nCost of 1 for VF vscale x 2: CLONE ir<%1> = sub nuw nsw ir<1>, vp<%8>\r\nCost of 0 for VF vscale x 2: CLONE ir<%arrayidx13> = getelementptr ir<%0>, ir<0>, ir<%1>\r\nCost of 0 for VF vscale x 2: vp<%9> = vector-pointer (reverse) ir<%arrayidx13>\r\nCost of 14 for VF vscale x 2: WIDEN vp.store vp<%9>, ir<0>, vp<%6>\r\nCost of 0 for VF vscale x 2: SCALAR-CAST vp<%10> = zext vp<%6> to i64\r\nCost of 0 for VF vscale x 2: EMIT vp<%11> = add vp<%10>, vp<%5>\r\nCost of 0 for VF vscale x 2: EMIT vp<%12> = add vp<%4>, vp<%0>\r\nCost of 0 for VF vscale x 2: EMIT branch-on-count vp<%12>, vp<%1>\r\nCost of 0 for VF vscale x 2: vector loop backedge\r\nCost for VF vscale x 2: 17\r\n```\r\nWith VPlan's cost model, we will eventually choose scalar loop, because when VF=vscale x 1, the final cost is `ceil(11 / 2) = 6`; when VF=vscale x 2, the final cost is `ceil(17 / 4) = 5`. Both of them are larger than the scalar cost, 4.\r\n\r\nWhile with the legacy cost model:\r\n```\r\nLV: Found an estimated cost of 0 for VF 1 For instruction: %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: %1 = sub nuw nsw i64 1, %indvars.iv\r\nLV: Found an estimated cost of 0 for VF 1 For instruction: %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %1\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: store i64 0, ptr %arrayidx13, align 8\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nLV: Found an estimated cost of 0 for VF 1 For instruction: br i1 %exitcond.not, label %for.end16, label %for.body9\r\nLV: Scalar loop costs: 4.\r\nLV: Found an estimated cost of 0 for VF vscale x 1 For instruction: %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nLV: Found an estimated cost of 1 for VF vscale x 1 For instruction: %1 = sub nuw nsw i64 1, %indvars.iv\r\nLV: Found an estimated cost of 0 for VF vscale x 1 For instruction: %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %1\r\nLV: Found an estimated cost of 7 for VF vscale x 1 For instruction: store i64 0, ptr %arrayidx13, align 8\r\nLV: Found an estimated cost of 1 for VF vscale x 1 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nLV: Found an estimated cost of 1 for VF vscale x 1 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nLV: Found an estimated cost of 0 for VF vscale x 1 For instruction: br i1 %exitcond.not, label %for.end16, label %for.body9\r\nLV: Vector loop of width vscale x 1 costs: 5 (assuming a minimum vscale of 2).\r\nLV: Found an estimated cost of 0 for VF vscale x 2 For instruction: %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nLV: Found an estimated cost of 1 for VF vscale x 2 For instruction: %1 = sub nuw nsw i64 1, %indvars.iv\r\nLV: Found an estimated cost of 0 for VF vscale x 2 For instruction: %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %1\r\nLV: Found an estimated cost of 13 for VF vscale x 2 For instruction: store i64 0, ptr %arrayidx13, align 8\r\nLV: Found an estimated cost of 1 for VF vscale x 2 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nLV: Found an estimated cost of 1 for VF vscale x 2 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nLV: Found an estimated cost of 0 for VF vscale x 2 For instruction: br i1 %exitcond.not, label %for.end16, label %for.body9\r\nLV: Vector loop of width vscale x 2 costs: 4 (assuming a minimum vscale of 2).\r\nLV: Selecting VF: vscale x 2.\r\n```\r\nWe will eventually choose VF=vscale x 2.\r\n\r\nThe key difference is the cost of `store i64 0, ptr %arrayidx13, align 8` v.s. cost of `WIDEN vp.store vp<%9>, ir<0>, vp<%6>`, where the latter is larger than the former by 1.\r\n\r\nIn both cases (the store instruction in VPlan cost model v.s. legacy cost model) their costs are computed by the base cost of store + the cost of shuffle due to being reverse store. I used debugger to confirm that they used the same shuffle cost, which means that for some reason, VPlan's cost model yield a slightly higher cost for store than that in the legacy cost model.\r\n", "author": "mshockwave", "labels": ["vectorizers", "crash"], "comments": [{"author": "ElvisWang123", "body": "It seems that the legacy cost model using `getMaskedMemoryOpCost` to query the instruction cost. But in the VPlan-based cost model using `getMemoryOpCost`. \r\n\r\nIn this case, the mask in the `VPWidenStoreEVLRecipe` is false since it is using the EVL recipe.\r\nThe legacy cost model will not check the tail folding type so it will using `getMaskedMemoryOpCost` instead of `getMemoryOpCost` . And the `MaskedMemoryCost` is slightly smaller than non-mask version."}, {"author": "ElvisWang123", "body": "Open a PR to fix this issue. #109644 \r\n\r\nWe remove the shuffle cost when the value stored by reversed vector is loop invariant."}, {"author": "mshockwave", "body": "CC @fhahn "}]}, "verified": true}
|
78 |
{"bug_id": "118850", "issue_url": "https://github.com/llvm/llvm-project/issues/118850", "bug_type": "crash", "base_commit": "9ad22cf0ee9eb686ea9fd76fd9cc41f4c006ccbc", "knowledge_cutoff": "2024-12-05T18:11:28Z", "lit_test_dir": ["llvm/test/Transforms/LoopVectorize"], "hints": {"fix_commit": "4f7f71b7bccdc38f37b82981e8fa9ceb536a7016", "components": ["LoopVectorize"], "bug_location_lineno": {"llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp": [[168, 174]]}, "bug_location_funcname": {"llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp": ["UnrollState::unrollWidenInductionByUF"]}}, "patch": "commit 4f7f71b7bccdc38f37b82981e8fa9ceb536a7016\nAuthor: Florian Hahn <[email protected]>\nDate: Fri Dec 6 16:28:47 2024 +0000\n\n [VPlan] Compare APInt instead of getSExtValue to fix crash in unroll.\n \n getSExtValue assumes the result fits in 64 bits, but this may not be the\n case for indcutions with wider types. Instead, directly perform the\n compare on the APInt for the ConstantInt.\n \n Fixes https://github.com/llvm/llvm-project/issues/118850.\n\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp\nindex f653269713b3..ff6c9295ee20 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp\n@@ -168,7 +168,7 @@ void UnrollState::unrollWidenInductionByUF(\n auto *ConstStep = ScalarStep->isLiveIn()\n ? dyn_cast<ConstantInt>(ScalarStep->getLiveInIRValue())\n : nullptr;\n- if (!ConstStep || ConstStep->getZExtValue() != 1) {\n+ if (!ConstStep || ConstStep->getValue() != 1) {\n if (TypeInfo.inferScalarType(ScalarStep) != IVTy) {\n ScalarStep =\n Builder.createWidenCast(Instruction::Trunc, ScalarStep, IVTy);\n", "tests": [{"file": "llvm/test/Transforms/LoopVectorize/interleave-with-i65-induction.ll", "commands": ["opt -p loop-vectorize -force-vector-width=4 -force-vector-interleave=2 -S %s"], "tests": [{"test_name": "i65_induction_with_negative_step", "test_body": "define void @i65_induction_with_negative_step(ptr %dst) {\nentry:\n br label %loop\n\nloop: ; preds = %loop, %entry\n %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]\n %iv.i65 = phi i65 [ 0, %entry ], [ %iv.i65.next, %loop ]\n %for = phi i64 [ 0, %entry ], [ %trunc, %loop ]\n %trunc = trunc i65 %iv.i65 to i64\n %gep = getelementptr inbounds i64, ptr %dst, i64 %trunc\n store i64 %for, ptr %gep, align 8\n %iv.next = add i64 %iv, 1\n %icmp = icmp eq i64 %iv.next, 1000\n %iv.i65.next = add i65 %iv.i65, -1\n br i1 %icmp, label %exit, label %loop\n\nexit: ; preds = %loop\n ret void\n}\n"}]}], "issue": {"title": "Assertion `getActiveBits() <= 64 && \"Too many bits for uint64_t\"' failed.", "body": "To reproduce run the test below with -passes=loop-vectorize\n```\n; ModuleID = './reduced.ll'\nsource_filename = \"./reduced.ll\"\ntarget datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32-ni:1-p2:32:8:8:32-ni:2\"\ntarget triple = \"aarch64-none-linux-gnu\"\n\ndefine double @wombat() gc \"statepoint-example\" {\nbb:\n br label %bb2\n\nbb1: ; preds = %bb2\n ret double 0.000000e+00\n\nbb2: ; preds = %bb2, %bb\n %phi = phi i64 [ 0, %bb ], [ %add, %bb2 ]\n %phi3 = phi i65 [ 0, %bb ], [ %add5, %bb2 ]\n %phi4 = phi i64 [ 0, %bb ], [ %trunc, %bb2 ]\n %trunc = trunc i65 %phi3 to i64\n store i64 0, ptr addrspace(1) null, align 8\n %add = add i64 %phi, 1\n %icmp = icmp ugt i64 %add, 0\n %add5 = add i65 %phi3, -1\n br i1 %icmp, label %bb2, label %bb1\n}\n```\nReproducer: https://godbolt.org/z/cjEcv8b4Y\n\nStack dump:\n```\n0.\tProgram arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=loop-vectorize <source>\n1.\tRunning pass \"function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)\" on module \"<source>\"\n2.\tRunning pass \"loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>\" on function \"wombat\"\n #0 0x00000000051b3748 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x51b3748)\n #1 0x00000000051b113c SignalHandler(int) Signals.cpp:0:0\n #2 0x000071a06ec42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)\n #3 0x000071a06ec969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)\n #4 0x000071a06ec42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)\n #5 0x000071a06ec287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)\n #6 0x000071a06ec2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)\n #7 0x000071a06ec39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)\n #8 0x0000000000b2d8a9 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xb2d8a9)\n #9 0x0000000004308ae6 (anonymous namespace)::UnrollState::unrollBlock(llvm::VPBlockBase*) VPlanUnroll.cpp:0:0\n#10 0x000000000430788c (anonymous namespace)::UnrollState::unrollBlock(llvm::VPBlockBase*) VPlanUnroll.cpp:0:0\n#11 0x0000000004309060 llvm::VPlanTransforms::unrollByUF(llvm::VPlan&, unsigned int, llvm::LLVMContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4309060)\n#12 0x0000000004176b76 llvm::LoopVectorizationPlanner::executePlan(llvm::ElementCount, unsigned int, llvm::VPlan&, llvm::InnerLoopVectorizer&, llvm::DominatorTree*, bool, llvm::DenseMap<llvm::SCEV const*, llvm::Value*, llvm::DenseMapInfo<llvm::SCEV const*, void>, llvm::detail::DenseMapPair<llvm::SCEV const*, llvm::Value*>> const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4176b76)\n#13 0x000000000418cb6f llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x418cb6f)\n#14 0x000000000418ebd9 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x418ebd9)\n#15 0x000000000418f213 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x418f213)\n#16 0x0000000002fcba8e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2fcba8e)\n#17 0x0000000004fb2700 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fb2700)\n#18 0x0000000000e33a9e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe33a9e)\n#19 0x0000000004fb101b llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fb101b)\n#20 0x0000000000e3312e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe3312e)\n#21 0x0000000004fb0a20 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fb0a20)\n#22 0x0000000000910a1a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x910a1a)\n#23 0x0000000000903099 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x903099)\n#24 0x000071a06ec29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)\n#25 0x000071a06ec29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)\n#26 0x00000000008fa97e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8fa97e)\nProgram terminated with signal: SIGSEGV\nCompiler returned: 139\n```\n", "author": "TatyanaDoubts", "labels": ["vectorizers", "crash"], "comments": [{"author": "huang-me", "body": "Hi @TatyanaDoubts, \nHow did you get the IR snippet? \nThe IR you provided is weird. I'll have to check where i65 constant 0 in `%phi3 = phi i65 [ 0, %bb ], [ %add5, %bb2 ]` come from since it seems it's the origin of the problem."}, {"author": "fhahn", "body": "I suspect this was found via a fuzzer, thanks for sharing the test case!"}, {"author": "TatyanaDoubts", "body": "Hi @huang-me, @fhahn, yes this crash was found by the fuzzer generated test. "}]}, "verified": true}
|
79 |
{"bug_id": "115149", "issue_url": "https://github.com/llvm/llvm-project/issues/115149", "bug_type": "miscompilation", "base_commit": "0f44d72e0ee74970cf696ff4c791f63e0c3fa9b4", "knowledge_cutoff": "2024-11-06T11:22:14Z", "lit_test_dir": ["llvm/test/Transforms/InstCombine"], "hints": {"fix_commit": "929cbe7f596733f85cd274485acc19442dd34a80", "components": ["InstCombine"], "bug_location_lineno": {"llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp": [[545, 551]]}, "bug_location_funcname": {"llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp": ["InstCombinerImpl::foldPHIArgGEPIntoPHI"]}}, "patch": "commit 929cbe7f596733f85cd274485acc19442dd34a80\nAuthor: Antonio Frighetto <[email protected]>\nDate: Tue Nov 12 19:02:04 2024 +0100\n\n [InstCombine] Intersect nowrap flags between geps while folding into phi\n \n A miscompilation issue has been addressed with refined checking.\n \n Fixes: https://github.com/llvm/llvm-project/issues/115149.\n\ndiff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp\nindex cb5c44730512..1fcf1c570add 100644\n--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp\n+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp\n@@ -545,7 +545,8 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {\n // especially bad when the PHIs are in the header of a loop.\n bool NeededPhi = false;\n \n- GEPNoWrapFlags NW = GEPNoWrapFlags::all();\n+ // Remember flags of the first phi-operand getelementptr.\n+ GEPNoWrapFlags NW = FirstInst->getNoWrapFlags();\n \n // Scan to see if all operands are the same opcode, and all have one user.\n for (Value *V : drop_begin(PN.incoming_values())) {\n", "tests": [{"file": "llvm/test/Transforms/InstCombine/opaque-ptr.ll", "commands": ["opt -S -passes=instcombine < %s"], "tests": [{"test_name": "phi_of_gep_flags_1", "test_body": "define ptr @phi_of_gep_flags_1(i1 %c, ptr %p) {\n br i1 %c, label %if, label %else\n\nif: ; preds = %0\n %gep1 = getelementptr inbounds i32, ptr %p, i64 1\n br label %join\n\nelse: ; preds = %0\n %gep2 = getelementptr nusw nuw i32, ptr %p, i64 1\n br label %join\n\njoin: ; preds = %else, %if\n %phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]\n ret ptr %phi\n}\n"}]}, {"file": "llvm/test/Transforms/InstCombine/phi.ll", "commands": ["opt < %s -passes=instcombine -S"], "tests": [{"test_name": "wrong_gep_arg_into_phi", "test_body": "target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64\"\n\ndefine i64 @wrong_gep_arg_into_phi(ptr noundef %ptr) {\nentry:\n %add.ptr = getelementptr i8, ptr %ptr, i64 1\n br label %for.cond\n\nfor.cond: ; preds = %for.cond, %entry\n %.pn = phi ptr [ %add.ptr, %entry ], [ %incdec.ptr, %for.cond ]\n %val = load i8, ptr %.pn, align 1\n %cond = icmp ne i8 %val, 0\n %incdec.ptr = getelementptr inbounds nuw i8, ptr %.pn, i64 1\n br i1 %cond, label %for.cond, label %exit\n\nexit: ; preds = %for.cond\n ret i64 0\n}\n"}]}], "issue": {"title": "[clang] Miscompile with -O3 and -O0/1/2 since 18.1.0", "body": "When I compiled this code with -O0/1/2, its output is 0. However, with -O3, it returned 143:\r\n\r\n```c\r\nint printf(const char *, ...);\r\nchar a, b;\r\nint c;\r\nchar *e = &b;\r\nint f(char *g, int *k) {\r\n char *d = g + *k;\r\n for (; *d && *d <= ' '; d++)\r\n ;\r\n if (*d)\r\n return 0;\r\n return 1;\r\n}\r\nint l(int g) {\r\n char h[] = {a, a, a};\r\n int i[] = {g};\r\n int j = f(h, i);\r\n return j;\r\n}\r\nlong m() {\r\n *e = 255;\r\n for (; l(b + 1);)\r\n return 0;\r\n for (;;)\r\n ;\r\n}\r\nint main() {\r\n m();\r\n printf(\"%d\\n\", c);\r\n}\r\n```\r\n\r\nDetails can be found here: https://godbolt.org/z/dnvvTPror", "author": "cardigan1008", "labels": ["miscompilation", "llvm:instcombine"], "comments": [{"author": "antoniofrighetto", "body": "This looks like a nasty one. Getting the optimized IR via -O3 and setting noinline/optnone to main, we get: https://llvm.godbolt.org/z/vaPvs6d16. \r\n\r\nThis is the initial assembly output with -O1 (which leads to the infinite loop):\r\n\r\n```asm\r\nmain: # @main\r\n push rax\r\n mov rax, qword ptr [e]\r\n mov byte ptr [rax], -1\r\n movsx rax, byte ptr [b]\r\n mov cl, byte ptr [a]\r\n mov byte ptr [rsp + 6], cl\r\n mov byte ptr [rsp + 7], cl\r\n lea rdx, [rsp + 5]\r\n add rdx, rax\r\n mov rax, rdx\r\n.LBB0_1: # %for.cond.i.i.i\r\n add rax, 1\r\n mov cl, byte ptr [rdx + 1]\r\n ...\r\n```\r\nWhereas this is with -O0 (returns 0):\r\n```asm\r\nmain: # @main\r\n sub rsp, 24\r\n mov rax, qword ptr [e]\r\n mov byte ptr [rax], -1\r\n movsx rcx, byte ptr [b]\r\n mov al, byte ptr [a]\r\n mov byte ptr [rsp + 22], al\r\n mov byte ptr [rsp + 23], al\r\n lea rax, [rsp + 21]\r\n add rax, rcx\r\n mov qword ptr [rsp + 8], rax # 8-byte Spill\r\n.LBB0_1: # %for.cond.i.i.i\r\n mov rcx, qword ptr [rsp + 8] # 8-byte Reload\r\n mov rax, rcx\r\n add rax, 1\r\n mov dl, byte ptr [rcx + 1]\r\n```\r\nIn the optimized one, $rdx is set to point to $rsp+0x5, part of the stack slot containing $rax (undef), later dereferenced to set the condition leading to the loop. Initially I thought some bug in regalloc, but to me it seems more something in prologepiloginserter. Function prologue / epilogue seems set up correctly with triple arm64-apple-macosx."}, {"author": "antoniofrighetto", "body": "Looking at the IR better, it seems like InstCombine performs a wrong optimization immediately after inlining `l` into `m`, probably related with default argument conversion done by operator + (which should include integer promotion on types smaller than integer). Alive2 seems to confirm it: https://alive2.llvm.org/ce/z/XAzrip."}, {"author": "antoniofrighetto", "body": "Reduced to:\r\n```llvm\r\n@a = global i8 0, align 1\r\n\r\ndefine i64 @src(i8 noundef %arg) {\r\nentry:\r\n %h.i = alloca [3 x i8], align 1\r\n %conv = sext i8 %arg to i32\r\n %add = add nsw i32 %conv, 1\r\n %2 = load i8, ptr @a, align 1\r\n store i8 %2, ptr %h.i, align 1\r\n %idx.ext.i.i = sext i32 %add to i64\r\n %add.ptr.i.i = getelementptr inbounds i8, ptr %h.i, i64 %idx.ext.i.i\r\n br label %for.cond.i.i\r\n\r\nfor.cond.i.i: ; preds = %for.cond.i.i, %entry\r\n %d.0.i.i = phi ptr [ %add.ptr.i.i, %entry ], [ %incdec.ptr.i.i, %for.cond.i.i ]\r\n %3 = load i8, ptr %d.0.i.i, align 1\r\n %tobool.not.i.i = icmp ne i8 %3, 0\r\n %incdec.ptr.i.i = getelementptr inbounds nuw i8, ptr %d.0.i.i, i64 1\r\n br i1 %tobool.not.i.i, label %for.cond.i.i, label %l.exit\r\n\r\nl.exit: ; preds = %for.cond.i.i\r\n ret i64 0\r\n}\r\n```\r\nWrong sext of add of sext simplified to sext leads to wrong gep offset: https://alive2.llvm.org/ce/z/JaR4j9."}, {"author": "AZero13", "body": "Can we backport this to 19.x?"}, {"author": "antoniofrighetto", "body": "Should be fixed in 929cbe7f596733f85cd274485acc19442dd34a80, thanks for reporting!"}, {"author": "AZero13", "body": "@antoniofrighetto can we please backport this "}]}, "verified": true}
|
|
|
74 |
{"bug_id": "121428", "issue_url": "https://github.com/llvm/llvm-project/issues/121428", "bug_type": "miscompilation", "base_commit": "b5f21671ef04984bc00770263234dfb94833a274", "knowledge_cutoff": "2025-01-01T09:27:44Z", "lit_test_dir": ["llvm/test/Transforms/InstCombine"], "hints": {"fix_commit": "a37dbc1f51c70d92fd209c2e52a9d794eb15e4e7", "components": ["InstCombine"], "bug_location_lineno": {"llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp": [[1225, 1232]]}, "bug_location_funcname": {"llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp": ["foldSelectCttzCtlz"]}}, "patch": "commit a37dbc1f51c70d92fd209c2e52a9d794eb15e4e7\nAuthor: Yingwei Zheng <[email protected]>\nDate: Mon Jan 6 00:04:28 2025 +0800\n\n [InstCombine] Drop noundef in `foldSelectCttzCtlz` (#121692)\n \n Close https://github.com/llvm/llvm-project/issues/121428\n\ndiff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp\nindex e7a8e947705f..a18b927678ef 100644\n--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp\n+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp\n@@ -1225,8 +1225,12 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,\n // zext/trunc) have one use (ending at the select), the cttz/ctlz result will\n // not be used if the input is zero. Relax to 'zero is poison' for that case.\n if (II->hasOneUse() && SelectArg->hasOneUse() &&\n- !match(II->getArgOperand(1), m_One()))\n+ !match(II->getArgOperand(1), m_One())) {\n II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));\n+ // noundef attribute on the intrinsic may no longer be valid.\n+ II->dropUBImplyingAttrsAndMetadata();\n+ IC.addToWorklist(II);\n+ }\n \n return nullptr;\n }\n", "tests": [{"file": "llvm/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll", "commands": ["opt -passes=instcombine -S < %s"], "tests": [{"test_name": "test_cttz_not_bw_multiuse", "test_body": "define i32 @test_cttz_not_bw_multiuse(i32 %x) {\n %ct = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %sel = select i1 %cmp, i32 %ct, i32 123\n %res = or i32 %sel, %ct\n ret i32 %res\n}\n\n; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)\ndeclare i32 @llvm.cttz.i32(i32, i1 immarg) #0\n\nattributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }\n"}, {"test_name": "test_cttz_not_bw", "test_body": "define i32 @test_cttz_not_bw(i32 %x) {\n %ct = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %res = select i1 %cmp, i32 %ct, i32 123\n ret i32 %res\n}\n\n; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)\ndeclare i32 @llvm.cttz.i32(i32, i1 immarg) #0\n\nattributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }\n"}, {"test_name": "test_cttz_not_bw_noundef", "test_body": "define i32 @test_cttz_not_bw_noundef(i32 %x) {\n %ct = tail call noundef i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %res = select i1 %cmp, i32 %ct, i32 123\n ret i32 %res\n}\n\n; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)\ndeclare i32 @llvm.cttz.i32(i32, i1 immarg) #0\n\nattributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }\n"}]}], "issue": {"title": "[InstCombine] `noundef` attribute should be dropped", "body": "Alive2: https://alive2.llvm.org/ce/z/wKsUWK\n```\ndefine i32 @src(i32 noundef %x) {\n %ct = call noundef i32 @llvm.cttz.i32(i32 %x, i1 false)\n %cmp = icmp ne i32 %x, 0\n %res = select i1 %cmp, i32 %ct, i32 123\n ret i32 %res\n}\ndefine i32 @tgt(i32 noundef %x) {\n %ct = call noundef range(i32 0, 33) i32 @llvm.cttz.i32(i32 %x, i1 true)\n %cmp.not = icmp eq i32 %x, 0\n %res = select i1 %cmp.not, i32 123, i32 %ct\n ret i32 %res\n}\n```\n", "author": "dtcxzyw", "labels": ["miscompilation", "llvm:instcombine"], "comments": []}, "verified": true}
|
75 |
{"bug_id": "98753", "issue_url": "https://github.com/llvm/llvm-project/issues/98753", "bug_type": "miscompilation", "base_commit": "c30ce8b9d33d1050ead549705702c1472b7a7d3f", "knowledge_cutoff": "2024-07-13T17:35:41Z", "lit_test_dir": ["llvm/test/Transforms/InstSimplify"], "hints": {"fix_commit": "de29b850f03092195bf21f3a39402adb4ed3c216", "components": ["InstructionSimplify"], "bug_location_lineno": {"llvm/lib/Analysis/InstructionSimplify.cpp": [[1975, 1987], [4300, 4305], [4347, 4352], [4467, 4472], [4606, 4612]]}, "bug_location_funcname": {"llvm/lib/Analysis/InstructionSimplify.cpp": ["llvm::simplifyWithOpReplaced", "simplifyAndOrWithICmpEq", "simplifySelectWithICmpEq"]}}, "patch": "commit de29b850f03092195bf21f3a39402adb4ed3c216\nAuthor: Nikita Popov <[email protected]>\nDate: Tue Jul 16 11:40:04 2024 +0200\n\n [InstSimplify] Fix simplifyAndOrWithICmpEq with undef refinement (#98898)\n \n The final case in Simplify (where Res == Absorber and the predicate is\n inverted) is not generally safe when the simplification is a refinement.\n In particular, we may simplify assuming a specific value for undef, but\n then chose a different one later.\n \n However, it *is* safe to refine poison in this context, unlike in the\n equivalent select folds. This is the reason why this fold did not use\n AllowRefinement=false in the first place, and using that option would\n introduce a lot of test regressions.\n \n This patch takes the middle path of disabling undef refinements in\n particular using the getWithoutUndef() SimplifyQuery option. However,\n this option doesn't actually work in this case, because the problematic\n fold is inside constant folding, and we currently don't propagate this\n option all the way from InstSimplify over ConstantFolding to\n ConstantFold. Work around this by explicitly checking for undef operands\n in simplifyWithOpReplaced().\n \n Finally, make sure that places where AllowRefinement=false also use\n Q.getWithoutUndef(). I don't have a specific test case for this (the\n original one does not work because we don't simplify selects with\n constant condition in this mode in the first place) but this seems like\n the correct thing to do to be conservative.\n \n Fixes https://github.com/llvm/llvm-project/issues/98753.\n\ndiff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp\nindex 242c200f7ef1..3a7ae577bb06 100644\n--- a/llvm/lib/Analysis/InstructionSimplify.cpp\n+++ b/llvm/lib/Analysis/InstructionSimplify.cpp\n@@ -1975,13 +1975,16 @@ static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,\n return nullptr;\n };\n \n- if (Value *Res =\n- simplifyWithOpReplaced(Op1, A, B, Q, /* AllowRefinement */ true,\n- /* DropFlags */ nullptr, MaxRecurse))\n+ // In the final case (Res == Absorber with inverted predicate), it is safe to\n+ // refine poison during simplification, but not undef. For simplicity always\n+ // disable undef-based folds here.\n+ if (Value *Res = simplifyWithOpReplaced(Op1, A, B, Q.getWithoutUndef(),\n+ /* AllowRefinement */ true,\n+ /* DropFlags */ nullptr, MaxRecurse))\n return Simplify(Res);\n- if (Value *Res =\n- simplifyWithOpReplaced(Op1, B, A, Q, /* AllowRefinement */ true,\n- /* DropFlags */ nullptr, MaxRecurse))\n+ if (Value *Res = simplifyWithOpReplaced(Op1, B, A, Q.getWithoutUndef(),\n+ /* AllowRefinement */ true,\n+ /* DropFlags */ nullptr, MaxRecurse))\n return Simplify(Res);\n \n return nullptr;\n@@ -4300,6 +4303,9 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,\n bool AllowRefinement,\n SmallVectorImpl<Instruction *> *DropFlags,\n unsigned MaxRecurse) {\n+ assert((AllowRefinement || !Q.CanUseUndef) &&\n+ \"If AllowRefinement=false then CanUseUndef=false\");\n+\n // Trivial replacement.\n if (V == Op)\n return RepOp;\n@@ -4347,6 +4353,11 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,\n } else {\n NewOps.push_back(InstOp);\n }\n+\n+ // Bail out if any operand is undef and SimplifyQuery disables undef\n+ // simplification. Constant folding currently doesn't respect this option.\n+ if (isa<UndefValue>(NewOps.back()) && !Q.CanUseUndef)\n+ return nullptr;\n }\n \n if (!AnyReplaced)\n@@ -4467,6 +4478,11 @@ Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,\n const SimplifyQuery &Q,\n bool AllowRefinement,\n SmallVectorImpl<Instruction *> *DropFlags) {\n+ // If refinement is disabled, also disable undef simplifications (which are\n+ // always refinements) in SimplifyQuery.\n+ if (!AllowRefinement)\n+ return ::simplifyWithOpReplaced(V, Op, RepOp, Q.getWithoutUndef(),\n+ AllowRefinement, DropFlags, RecursionLimit);\n return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags,\n RecursionLimit);\n }\n@@ -4606,7 +4622,7 @@ static Value *simplifySelectWithICmpEq(Value *CmpLHS, Value *CmpRHS,\n Value *TrueVal, Value *FalseVal,\n const SimplifyQuery &Q,\n unsigned MaxRecurse) {\n- if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q,\n+ if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q.getWithoutUndef(),\n /* AllowRefinement */ false,\n /* DropFlags */ nullptr, MaxRecurse) == TrueVal)\n return FalseVal;\n", "tests": [{"file": "llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll", "commands": ["opt -S -passes=instsimplify < %s"], "tests": [{"test_name": "and_is_constant", "test_body": "define i1 @and_is_constant(ptr %arg, ptr %arg2) {\n %icmp = icmp eq ptr %arg, %arg2\n %call = call i1 @llvm.is.constant.i1(i1 %icmp)\n %and = and i1 %call, %icmp\n ret i1 %and\n}\n\n; Function Attrs: convergent nocallback nofree nosync nounwind willreturn memory(none)\ndeclare i1 @llvm.is.constant.i1(i1) #0\n\nattributes #0 = { convergent nocallback nofree nosync nounwind willreturn memory(none) }\n"}, {"test_name": "pr98753", "test_body": "define i1 @pr98753(i32 noundef %x, i32 %y) {\n %cmp1 = icmp ne i32 %x, 0\n %sel = select i1 %cmp1, i32 %y, i32 undef\n %cmp2 = icmp sgt i32 %sel, 0\n %and = and i1 %cmp1, %cmp2\n ret i1 %and\n}\n"}]}], "issue": {"title": "Miscompilation with libstdc++'s std::optional<int> and -O1", "body": "https://godbolt.org/z/KjEreWf57\r\n\r\nWith Clang 18.1 and libstdc++, we get different behavior when compared to Clang 17 in the code below.\r\nEDIT: requires at least -O1.\r\n\r\n```cpp\r\n#include <optional>\r\n#include <iostream>\r\n\r\n// defined in a separate compilation unit\r\nint takeIntRefAndReturn0(int&);\r\n\r\nstd::optional<int> shouldReturnEmptyOptional() {\r\n int v = 5;\r\n if (takeIntRefAndReturn0(v))\r\n return v;\r\n return std::nullopt;\r\n}\r\n\r\nint main() {\r\n auto opt = shouldReturnEmptyOptional();\r\n if (opt && *opt > 0) {\r\n std::cout << \"SHOULD NOT BE PRINTED: *opt = \" << *opt << std::endl;\r\n return 1;\r\n }\r\n std::cout << \"SHOULD BE PRINTED\" << std::endl;\r\n}\r\n```\r\n\r\nWith Clang 17, we get `SHOULD BE PRINTED`, while with with Clang 18.1 we get `SHOULD NOT BE PRINTED: *opt = 5`.\r\n\r\nWith git-bisect, I found that this is caused by https://github.com/llvm/llvm-project/commit/060de415af335fdd82910f409e2be3b8457eaa43.\r\n\r\nAn isomorphic example to reproduce this (https://godbolt.org/z/9PsjY17sT):\r\n```cpp\r\nint takeIntRefReturn0(int &);\r\nvoid assertNotReached(int);\r\n\r\nstatic bool logicalAnd(bool a, bool b) { return a && b; }\r\n\r\nint main() {\r\n int v4;\r\n bool v3;\r\n {\r\n int v1 = 5;\r\n int v2 = takeIntRefReturn0(v1);\r\n v3 = v2 != 0;\r\n if (v3)\r\n v4 = v1;\r\n }\r\n // Move to a function so that && is not short cutted.\r\n // v4 will be undefined if v2 == 0, but v3 is false, so the branch shouldn't be entered.\r\n if (logicalAnd(v3, v4 > 0))\r\n assertNotReached(v4);\r\n\r\n return 0;\r\n}\r\n```\r\nNote in the generated LLVM IR that\r\n```llvm\r\n%6 = icmp sgt i32 %5, 0\r\n%7 = and i1 %3, %6\r\nbr i1 %7, label %8, label %9\r\n``` \r\nwas reduced to only\r\n```llvm\r\n%6 = icmp sgt i32 %5, 0\r\nbr i1 %6, label %7, label %8\r\n```", "author": "cadubentzen", "labels": ["regression", "miscompilation", "llvm:analysis"], "comments": [{"author": "dtcxzyw", "body": "Reproducer: https://alive2.llvm.org/ce/z/p4ZGxe\r\n```\r\n; bin/opt -passes=instcombine test.ll -S\r\ndefine i1 @src(i32 noundef %x, i32 %y) {\r\n %3 = icmp ne i32 %x, 0\r\n %5 = select i1 %3, i32 %y, i32 undef\r\n %6 = icmp sgt i32 %5, 0\r\n %7 = and i1 %3, %6\r\n br i1 %7, label %if.then, label %if.else\r\n\r\nif.then:\r\n call void @use(i32 noundef %5)\r\n ret i1 true\r\n\r\nif.else:\r\n ret i1 false\r\n}\r\n\r\ndefine i1 @tgt(i32 noundef %x, i32 %y) {\r\n %.not = icmp eq i32 %x, 0\r\n %1 = select i1 %.not, i32 undef, i32 %y\r\n %2 = icmp sgt i32 %1, 0\r\n br i1 %2, label %if.then, label %if.else\r\n\r\nif.then: ; preds = %0\r\n call void @use(i32 noundef %1)\r\n ret i1 true\r\n\r\nif.else: ; preds = %0\r\n ret i1 false\r\n}\r\n\r\ndeclare void @use(i32 %x)\r\n```\r\ncc @nikic"}, {"author": "nikic", "body": "A bit simpler using just instsimplify: https://alive2.llvm.org/ce/z/tnkf6A"}, {"author": "nikic", "body": "I expect the fix here is to perform replacements with `Q.getWithoutUndef()`. But probably not just here, but also in other places using simplifyWithOpReplaced(). (Possibly making it always use that mode.)"}, {"author": "cadubentzen", "body": "Thanks a lot for fixing this so quickly! I wonder if this should be included in 18.1.9?"}, {"author": "nikic", "body": "The LLVM 18 branch is already closed, there will be no further releases."}, {"author": "thesamesam", "body": "I'll add the milestone anyway as it's easier to track for downstream backports then."}]}, "verified": true}
|
76 |
{"bug_id": "98435", "issue_url": "https://github.com/llvm/llvm-project/issues/98435", "bug_type": "miscompilation", "base_commit": "0e124537aabea0d18e030226a3ca4c6f494c1322", "knowledge_cutoff": "2024-07-11T06:06:08Z", "lit_test_dir": ["llvm/test/Transforms/InstCombine"], "hints": {"fix_commit": "9304af3927caecdb43d3a9b5d16c6a5b7a6b5594", "components": ["InstCombine"], "bug_location_lineno": {"llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp": [[1735, 1751]]}, "bug_location_funcname": {"llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp": ["InstCombinerImpl::SimplifyDemandedVectorElts"]}}, "patch": "commit 9304af3927caecdb43d3a9b5d16c6a5b7a6b5594\nAuthor: Jorge Botto <[email protected]>\nDate: Fri Aug 9 11:52:56 2024 +0100\n\n [InstCombine] Fixing wrong select folding in vectors with undef elements (#102244)\n \n This PR fixes https://github.com/llvm/llvm-project/issues/98435.\n `SimplifyDemandedVectorElts` mishandles the undef by assuming that\n !isNullValue() means the condition is true.\n \n By preventing any value that we're not certain equals 1 or 0, it avoids\n having to make any particular choice by not demanding bits from a\n particular branch with potentially picking a wrong value.\n \n Proof: https://alive2.llvm.org/ce/z/r8CmEu\n\ndiff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp\nindex c494fec84c1e..153d8c238ed4 100644\n--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp\n+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp\n@@ -1735,17 +1735,12 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,\n APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);\n if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {\n for (unsigned i = 0; i < VWidth; i++) {\n- // isNullValue() always returns false when called on a ConstantExpr.\n- // Skip constant expressions to avoid propagating incorrect information.\n Constant *CElt = CV->getAggregateElement(i);\n- if (isa<ConstantExpr>(CElt))\n- continue;\n- // TODO: If a select condition element is undef, we can demand from\n- // either side. If one side is known undef, choosing that side would\n- // propagate undef.\n+\n+ // isNullValue() always returns false when called on a ConstantExpr.\n if (CElt->isNullValue())\n DemandedLHS.clearBit(i);\n- else\n+ else if (CElt->isOneValue())\n DemandedRHS.clearBit(i);\n }\n }\n", "tests": [{"file": "llvm/test/Transforms/InstCombine/pr98435.ll", "commands": ["opt -S -passes=instcombine < %s 2>&1"], "tests": [{"test_name": "pr98435", "test_body": "define <2 x i1> @pr98435(<2 x i1> %val) {\n %val1 = select <2 x i1> <i1 undef, i1 true>, <2 x i1> splat (i1 true), <2 x i1> %val\n ret <2 x i1> %val1\n}\n"}]}], "issue": {"title": "Wrong select folding for undef conditions by InstCombine", "body": "https://github.com/llvm/llvm-project/blob/7eae9bb856135136cddc4208a2b1546e9db44c9c/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L3582-L3585\r\n\r\nAlive2 report: https://alive2.llvm.org/ce/z/Qtu72J\r\n\r\n\r\n```ll\r\n----------------------------------------\r\ndefine <2 x i1> @fun0(<2 x i1> %val0, <2 x i1> %val1) {\r\n#0:\r\n %val2 = select <2 x i1> %val0, <2 x i1> %val0, <2 x i1> %val0\r\n %val3 = select <2 x i1> { undef, 1 }, <2 x i1> { 1, 1 }, <2 x i1> %val2\r\n ret <2 x i1> %val3\r\n}\r\n=>\r\ndefine <2 x i1> @fun0(<2 x i1> %val0, <2 x i1> %val1) {\r\n#0:\r\n ret <2 x i1> { poison, 1 }\r\n}\r\nTransformation doesn't verify!\r\n\r\nERROR: Target is more poisonous than source\r\n\r\nExample:\r\n<2 x i1> %val0 = < #x0 (0), #x0 (0) >\r\n<2 x i1> %val1 = < #x0 (0), #x0 (0) >\r\n\r\nSource:\r\n<2 x i1> %val2 = < #x0 (0), #x0 (0) >\r\n<2 x i1> %val3 = < #x0 (0)\t[based on undef value], #x1 (1) >\r\n\r\nTarget:\r\nSource value: < #x0 (0), #x1 (1) >\r\nTarget value: < poison, #x1 (1) >\r\n\r\nSummary:\r\n 0 correct transformations\r\n 1 incorrect transformations\r\n 0 failed-to-prove transformations\r\n 0 Alive2 errors\r\n```", "author": "bongjunj", "labels": ["miscompilation", "llvm:instcombine"], "comments": [{"author": "jf-botto", "body": "Would this issue be to fix InstCombine to not try to optimise it or get InstCombine to pick `<2 x i1> { 1, 1 }` because `undef` let's it pick either values and picking a constant vector is better than picking a variable?"}, {"author": "nikic", "body": "The problematic step seems to be this one: https://alive2.llvm.org/ce/z/C2waWU\r\n\r\nI don't think the quoted code is responsible for this, as it is guarded by `!isa<Constant>(CondVal)`."}, {"author": "jf-botto", "body": "I think the problem is with how `SimplifyDemandedVectorElts` handles the undef: https://github.com/llvm/llvm-project/blob/af80d3a248101d6f5d9d5e229c7899136b8ce0b8/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp#L1721-L1760"}, {"author": "nikic", "body": "@jf-botto Agree. It assumes that !isNullValue() means \"is true\", which is not the case."}]}, "verified": true}
|
77 |
+
{"bug_id": "131071", "issue_url": "https://github.com/llvm/llvm-project/issues/131071", "bug_type": "crash", "base_commit": "fecd937910f640663885e0434bfed80cf4574072", "knowledge_cutoff": "2025-03-13T04:07:59Z", "lit_test_dir": ["llvm/test/Transforms/LoopVectorize"], "hints": {"fix_commit": "2e13ec561c8139951e3f0815c35c758a7a3011db", "components": ["LoopVectorize"], "bug_location_lineno": {"llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h": [[494, 501]], "llvm/lib/Transforms/Vectorize/LoopVectorize.cpp": [[7121, 7126], [9620, 9626], [9640, 9649]], "llvm/lib/Transforms/Vectorize/VPlan.cpp": [[1532, 1543]], "llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp": [[32, 38], [83, 88], [106, 111]], "llvm/lib/Transforms/Vectorize/VPlanTransforms.h": [[68, 79]]}, "bug_location_funcname": {"llvm/lib/Transforms/Vectorize/LoopVectorize.cpp": ["LoopVectorizationPlanner::buildVPlan", "LoopVectorizationPlanner::planInVPlanNativePath", "LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes"], "llvm/lib/Transforms/Vectorize/VPlan.cpp": ["LoopVectorizationPlanner::buildVPlans"], "llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp": ["VPlanTransforms::VPInstructionsToVPRecipes", "sinkScalarOperands"]}}, "patch": "commit 2e13ec561c8139951e3f0815c35c758a7a3011db\nAuthor: Florian Hahn <[email protected]>\nDate: Wed Mar 19 21:35:15 2025 +0000\n\n [VPlan] Bail out on non-intrinsic calls in VPlanNativePath.\n \n Update initial VPlan-construction in VPlanNativePath in line with the\n inner loop path, in that it bails out when encountering constructs it\n cannot handle, like non-intrinsic calls.\n \n Fixes https://github.com/llvm/llvm-project/issues/131071.\n\ndiff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h\nindex ed3e45dd2c6c..7bbe8669cef6 100644\n--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h\n+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h\n@@ -494,8 +494,10 @@ protected:\n private:\n /// Build a VPlan according to the information gathered by Legal. \\return a\n /// VPlan for vectorization factors \\p Range.Start and up to \\p Range.End\n- /// exclusive, possibly decreasing \\p Range.End.\n- VPlanPtr buildVPlan(VFRange &Range);\n+ /// exclusive, possibly decreasing \\p Range.End. If no VPlan can be built for\n+ /// the input range, set the largest included VF to the maximum VF for which\n+ /// no plan could be built.\n+ VPlanPtr tryToBuildVPlan(VFRange &Range);\n \n /// Build a VPlan using VPRecipes according to the information gather by\n /// Legal. This method is only used for the legacy inner loop vectorizer.\ndiff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\nindex 89337dc38535..61e0d18445b6 100644\n--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\n+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\n@@ -7121,6 +7121,9 @@ LoopVectorizationPlanner::planInVPlanNativePath(ElementCount UserVF) {\n << \"VF \" << VF << \" to build VPlans.\\n\");\n buildVPlans(VF, VF);\n \n+ if (VPlans.empty())\n+ return VectorizationFactor::Disabled();\n+\n // For VPlan build stress testing, we bail out after VPlan construction.\n if (VPlanBuildStressTest)\n return VectorizationFactor::Disabled();\n@@ -9620,7 +9623,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {\n return Plan;\n }\n \n-VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {\n+VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {\n // Outer loop handling: They may require CFG and instruction level\n // transformations before even evaluating whether vectorization is profitable.\n // Since we cannot modify the incoming IR, we need to build VPlan upfront in\n@@ -9640,10 +9643,13 @@ VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {\n for (ElementCount VF : Range)\n Plan->addVF(VF);\n \n- VPlanTransforms::VPInstructionsToVPRecipes(\n- Plan,\n- [this](PHINode *P) { return Legal->getIntOrFpInductionDescriptor(P); },\n- *PSE.getSE(), *TLI);\n+ if (!VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(\n+ Plan,\n+ [this](PHINode *P) {\n+ return Legal->getIntOrFpInductionDescriptor(P);\n+ },\n+ *PSE.getSE(), *TLI))\n+ return nullptr;\n \n // Tail folding is not supported for outer loops, so the induction increment\n // is guaranteed to not wrap.\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp\nindex b63cdd04530a..ef82734840ed 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp\n@@ -1532,12 +1532,13 @@ void LoopVectorizationPlanner::buildVPlans(ElementCount MinVF,\n auto MaxVFTimes2 = MaxVF * 2;\n for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFTimes2);) {\n VFRange SubRange = {VF, MaxVFTimes2};\n- auto Plan = buildVPlan(SubRange);\n- VPlanTransforms::optimize(*Plan);\n- // Update the name of the latch of the top-level vector loop region region\n- // after optimizations which includes block folding.\n- Plan->getVectorLoopRegion()->getExiting()->setName(\"vector.latch\");\n- VPlans.push_back(std::move(Plan));\n+ if (auto Plan = tryToBuildVPlan(SubRange)) {\n+ VPlanTransforms::optimize(*Plan);\n+ // Update the name of the latch of the top-level vector loop region region\n+ // after optimizations which includes block folding.\n+ Plan->getVectorLoopRegion()->getExiting()->setName(\"vector.latch\");\n+ VPlans.push_back(std::move(Plan));\n+ }\n VF = SubRange.End;\n }\n }\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\nindex e6f788a18805..6c8680ac9661 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\n@@ -32,7 +32,7 @@\n \n using namespace llvm;\n \n-void VPlanTransforms::VPInstructionsToVPRecipes(\n+bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(\n VPlanPtr &Plan,\n function_ref<const InductionDescriptor *(PHINode *)>\n GetIntOrFpInductionDescriptor,\n@@ -83,6 +83,9 @@ void VPlanTransforms::VPInstructionsToVPRecipes(\n } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {\n NewRecipe = new VPWidenGEPRecipe(GEP, Ingredient.operands());\n } else if (CallInst *CI = dyn_cast<CallInst>(Inst)) {\n+ Intrinsic::ID VectorID = getVectorIntrinsicIDForCall(CI, &TLI);\n+ if (VectorID == Intrinsic::not_intrinsic)\n+ return false;\n NewRecipe = new VPWidenIntrinsicRecipe(\n *CI, getVectorIntrinsicIDForCall(CI, &TLI),\n {Ingredient.op_begin(), Ingredient.op_end() - 1}, CI->getType(),\n@@ -106,6 +109,7 @@ void VPlanTransforms::VPInstructionsToVPRecipes(\n Ingredient.eraseFromParent();\n }\n }\n+ return true;\n }\n \n static bool sinkScalarOperands(VPlan &Plan) {\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h\nindex 2ec07eea1ecc..fe93637ab723 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h\n+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h\n@@ -68,12 +68,13 @@ struct VPlanTransforms {\n bool RequiresScalarEpilogueCheck, bool TailFolded, Loop *TheLoop);\n \n /// Replaces the VPInstructions in \\p Plan with corresponding\n- /// widen recipes.\n- static void\n- VPInstructionsToVPRecipes(VPlanPtr &Plan,\n- function_ref<const InductionDescriptor *(PHINode *)>\n- GetIntOrFpInductionDescriptor,\n- ScalarEvolution &SE, const TargetLibraryInfo &TLI);\n+ /// widen recipes. Returns false if any VPInstructions could not be converted\n+ /// to a wide recipe if needed.\n+ static bool tryToConvertVPInstructionsToVPRecipes(\n+ VPlanPtr &Plan,\n+ function_ref<const InductionDescriptor *(PHINode *)>\n+ GetIntOrFpInductionDescriptor,\n+ ScalarEvolution &SE, const TargetLibraryInfo &TLI);\n \n /// Try to have all users of fixed-order recurrences appear after the recipe\n /// defining their previous value, by either sinking users or hoisting recipes\n", "tests": [{"file": "llvm/test/Transforms/LoopVectorize/vplan-widen-call-instruction.ll", "commands": ["opt -passes=loop-vectorize -force-vector-width=4 -enable-vplan-native-path -S %s"], "tests": [{"test_name": "call_to_non_intrinsic", "test_body": "define void @call_to_non_intrinsic() {\nentry:\n br label %outer.header\n\nouter.header: ; preds = %outer.latch, %entry\n %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]\n br label %inner.header\n\ninner.header: ; preds = %inner.header, %outer.header\n %inner.iv = phi i64 [ 0, %outer.header ], [ %inner.iv.next, %inner.header ]\n call void @use()\n %inner.iv.next = add i64 %inner.iv, 1\n %inner.ec = icmp eq i64 %inner.iv.next, 100\n br i1 %inner.ec, label %outer.latch, label %inner.header\n\nouter.latch: ; preds = %inner.header\n %outer.iv.next = add i64 %outer.iv, 1\n %outer.ec = icmp eq i64 %outer.iv.next, 100\n br i1 %outer.ec, label %exit, label %outer.header, !llvm.loop !0\n\nexit: ; preds = %outer.latch\n ret void\n}\n\ndeclare void @use()\n\n!0 = distinct !{!0, !1}\n!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"}]}], "issue": {"title": "[LoopVectorize] Assertion `ID != Intrinsic::not_intrinsic && \"Not an intrinsic!\"' failed.", "body": "Reproducer: https://godbolt.org/z/n7E1TjTv5\n```\n; bin/opt -passes=loop-vectorize -enable-vplan-native-path reduced.ll -S\ntarget datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-unknown-linux-gnu\"\n\ndefine i32 @main() {\nentry:\n br label %for.cond2.preheader.i815\n\nfor.cond2.preheader.i815: ; preds = %for.cond.cleanup8.i835.split.us, %entry\n %I.03.i816 = phi i64 [ %inc20.i839, %for.cond.cleanup8.i835.split.us ], [ 0, %entry ]\n br label %cont.i830.us\n\ncont.i830.us: ; preds = %cont.i830.us, %for.cond2.preheader.i815\n %K.01.i824.us = phi i64 [ 0, %for.cond2.preheader.i815 ], [ %inc.i833.us, %cont.i830.us ]\n call void @use(ptr null, i64 0)\n %inc.i833.us = add i64 %K.01.i824.us, 1\n %exitcond.not.i834.us = icmp eq i64 %inc.i833.us, 0\n br i1 %exitcond.not.i834.us, label %for.cond.cleanup8.i835.split.us, label %cont.i830.us\n\nfor.cond.cleanup8.i835.split.us: ; preds = %cont.i830.us\n %inc20.i839 = add i64 %I.03.i816, 1\n %exitcond5.not.i840 = icmp eq i64 %inc20.i839, 0\n br i1 %exitcond5.not.i840, label %\"_ZZ4mainENK3$_7clEmmmPi.exit.loopexit1081\", label %for.cond2.preheader.i815, !llvm.loop !0\n\n\"_ZZ4mainENK3$_7clEmmmPi.exit.loopexit1081\": ; preds = %for.cond.cleanup8.i835.split.us\n ret i32 0\n}\n\ndeclare void @use(ptr, i64)\n\n!0 = distinct !{!0, !1, !2}\n!1 = !{!\"llvm.loop.mustprogress\"}\n!2 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n```\n```\nopt: /root/llvm-project/llvm/lib/Analysis/VectorUtils.cpp:175: bool llvm::isVectorIntrinsicWithOverloadTypeAtArg(llvm::Intrinsic::ID, int, const llvm::TargetTransformInfo*): Assertion `ID != Intrinsic::not_intrinsic && \"Not an intrinsic!\"' failed.\nPLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.\nStack dump:\n0.\tProgram arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=loop-vectorize -enable-vplan-native-path <source>\n1.\tRunning pass \"function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)\" on module \"<source>\"\n2.\tRunning pass \"loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>\" on function \"main\"\n #0 0x0000000005475858 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5475858)\n #1 0x0000000005473214 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0\n #2 0x00007024fda42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)\n #3 0x00007024fda969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)\n #4 0x00007024fda42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)\n #5 0x00007024fda287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)\n #6 0x00007024fda2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)\n #7 0x00007024fda39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)\n #8 0x0000000004bea5d7 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4bea5d7)\n #9 0x00000000045582f0 llvm::VPWidenIntrinsicRecipe::execute(llvm::VPTransformState&) (.part.0) VPlanRecipes.cpp:0:0\n#10 0x0000000004513ea7 llvm::VPBasicBlock::executeRecipes(llvm::VPTransformState*, llvm::BasicBlock*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4513ea7)\n#11 0x000000000451e4cf llvm::VPBasicBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x451e4cf)\n#12 0x000000000451f5c0 llvm::VPRegionBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x451f5c0)\n#13 0x000000000451f5c0 llvm::VPRegionBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x451f5c0)\n#14 0x000000000452b451 llvm::VPlan::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x452b451)\n#15 0x00000000043ddd2f llvm::LoopVectorizationPlanner::executePlan(llvm::ElementCount, unsigned int, llvm::VPlan&, llvm::InnerLoopVectorizer&, llvm::DominatorTree*, bool, llvm::DenseMap<llvm::SCEV const*, llvm::Value*, llvm::DenseMapInfo<llvm::SCEV const*, void>, llvm::detail::DenseMapPair<llvm::SCEV const*, llvm::Value*>> const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43ddd2f)\n#16 0x00000000043f0e36 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43f0e36)\n#17 0x00000000043f45e9 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43f45e9)\n#18 0x00000000043f4c63 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43f4c63)\n#19 0x000000000319510e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x319510e)\n#20 0x0000000005270840 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5270840)\n#21 0x0000000000e79f8e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe79f8e)\n#22 0x000000000526f15b llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x526f15b)\n#23 0x0000000000e7910e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe7910e)\n#24 0x000000000526eb60 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x526eb60)\n#25 0x000000000094151a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x94151a)\n#26 0x0000000000934c6c optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x934c6c)\n#27 0x00007024fda29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)\n#28 0x00007024fda29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)\n#29 0x000000000092c755 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x92c755)\nProgram terminated with signal: SIGSEGV\nCompiler returned: 139\n```\nReduced from llvm-test-suite/SingleSource/UnitTests/Vectorizer/VPlanNativePath/outer-loop-vect.cpp with ubsan+https://github.com/llvm/llvm-project/pull/130990 enabled\n\n\n", "author": "dtcxzyw", "labels": ["vectorizers", "crash-on-valid"], "comments": [{"author": "zhangjunphy", "body": "It seems for vplan vectorization, `LoopVectorizationLegality::canVectorize` only checks the loop structure and then skips the check of individual instructions. So the pass later tries to rewirte that call to `@use` and failed."}, {"author": "zhangjunphy", "body": "From the description of `isExplicitVecOuterLoop`. This does not seem to a problem of the LoopVectorization pass.\nWhen `-enable-vplan-native-path` is provided, LLVM only attempts to vectorize outer loops marked with a pragma.\nAnd the programmer is expected to ensure vectorization must be legal for those loops.\nhttps://github.com/llvm/llvm-project/blob/125553a6779ba179a9b4b5212c531da2474258fd/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp#L2183-L2192\n\n=============\nEdited for clarity."}, {"author": "dtcxzyw", "body": "Does ubsan instrumentation break this assumption?\n"}, {"author": "zhangjunphy", "body": "Well, I'm far from familiar with the design of vectorization and this is just my conclusion from reading the code.\n\nFrom what I understand, if the flag `-enable-vplan-native-path` is specified, the IR producer is required to be certain that the loop marked with \"llvm.loop.vectorize.enable\" is actually vectorizable. Otherwise this will cause problems as that flag will cause vectorization to bypass some legality checks and to directly attempt vectorizing the loop. \n\nIn this case, the instrumentation added the \"llvm.loop.vectorize.enable\" metadata to the loop. But there is the call to `@use` which is not vectorizable. And this check is bypassed with `-enable-vplan-native-path`, thus caused the problem."}, {"author": "fhahn", "body": "It is true that the native path somewhat relies on the loops being marked safe for vectorization by the user, we still shouldn't crash. In this particular case we can easily detect that we cannot create a valid VPlan, and bail out. I will prepare a fix."}]}, "verified": true}
|
78 |
{"bug_id": "109468", "issue_url": "https://github.com/llvm/llvm-project/issues/109468", "bug_type": "crash", "base_commit": "51039101cf32591782ef564a108db71150a3b7c3", "knowledge_cutoff": "2024-09-20T20:22:11Z", "lit_test_dir": ["llvm/test/Transforms/LoopVectorize"], "hints": {"fix_commit": "a068b974b199b0e7350da2d9506adc7df3995ce3", "components": ["LoopVectorize"], "bug_location_lineno": {"llvm/lib/Transforms/Vectorize/VPlan.h": [[2709, 2714], [2787, 2792]], "llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp": [[2267, 2272], [2363, 2368]]}, "bug_location_funcname": {"llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp": ["VPWidenLoadEVLRecipe::execute", "VPWidenLoadEVLRecipe::print", "VPWidenStoreEVLRecipe::execute", "VPWidenStoreEVLRecipe::print"]}}, "patch": "commit a068b974b199b0e7350da2d9506adc7df3995ce3\nAuthor: Elvis Wang <[email protected]>\nDate: Thu Sep 26 07:10:25 2024 +0800\n\n [VPlan] Implement VPWidenLoad/StoreEVLRecipe::computeCost(). (#109644)\n \n Currently the EVL recipes transfer the tail masking to the EVL.\n But in the legacy cost model, the mask exist and will calculate the\n instruction cost of the mask.\n To fix the difference between the VPlan-based cost model and the legacy\n cost model, we always calculate the instruction cost for the mask in the\n EVL recipes.\n \n Note that we should remove the mask cost in the EVL recipes when we\n don't need to compare to the legacy cost model.\n \n This patch also fixes #109468.\n\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h\nindex bbcfaf9e19cd..23a24ce293ef 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlan.h\n+++ b/llvm/lib/Transforms/Vectorize/VPlan.h\n@@ -2709,6 +2709,10 @@ struct VPWidenLoadEVLRecipe final : public VPWidenMemoryRecipe, public VPValue {\n /// Generate the wide load or gather.\n void execute(VPTransformState &State) override;\n \n+ /// Return the cost of this VPWidenLoadEVLRecipe.\n+ InstructionCost computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const override;\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n /// Print the recipe.\n void print(raw_ostream &O, const Twine &Indent,\n@@ -2787,6 +2791,10 @@ struct VPWidenStoreEVLRecipe final : public VPWidenMemoryRecipe {\n /// Generate the wide store or scatter.\n void execute(VPTransformState &State) override;\n \n+ /// Return the cost of this VPWidenStoreEVLRecipe.\n+ InstructionCost computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const override;\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n /// Print the recipe.\n void print(raw_ostream &O, const Twine &Indent,\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp\nindex dacba152611c..9a0aefb898e5 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp\n@@ -2267,6 +2267,31 @@ void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {\n State.set(this, Res);\n }\n \n+InstructionCost VPWidenLoadEVLRecipe::computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const {\n+ if (!Consecutive || IsMasked)\n+ return VPWidenMemoryRecipe::computeCost(VF, Ctx);\n+\n+ // We need to use the getMaskedMemoryOpCost() instead of getMemoryOpCost()\n+ // here because the EVL recipes using EVL to replace the tail mask. But in the\n+ // legacy model, it will always calculate the cost of mask.\n+ // TODO: Using getMemoryOpCost() instead of getMaskedMemoryOpCost when we\n+ // don't need to compare to the legacy cost model.\n+ Type *Ty = ToVectorTy(getLoadStoreType(&Ingredient), VF);\n+ const Align Alignment =\n+ getLoadStoreAlignment(const_cast<Instruction *>(&Ingredient));\n+ unsigned AS =\n+ getLoadStoreAddressSpace(const_cast<Instruction *>(&Ingredient));\n+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;\n+ InstructionCost Cost = Ctx.TTI.getMaskedMemoryOpCost(\n+ Ingredient.getOpcode(), Ty, Alignment, AS, CostKind);\n+ if (!Reverse)\n+ return Cost;\n+\n+ return Cost + Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse,\n+ cast<VectorType>(Ty), {}, CostKind, 0);\n+}\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n void VPWidenLoadEVLRecipe::print(raw_ostream &O, const Twine &Indent,\n VPSlotTracker &SlotTracker) const {\n@@ -2363,6 +2388,31 @@ void VPWidenStoreEVLRecipe::execute(VPTransformState &State) {\n State.addMetadata(NewSI, SI);\n }\n \n+InstructionCost VPWidenStoreEVLRecipe::computeCost(ElementCount VF,\n+ VPCostContext &Ctx) const {\n+ if (!Consecutive || IsMasked)\n+ return VPWidenMemoryRecipe::computeCost(VF, Ctx);\n+\n+ // We need to use the getMaskedMemoryOpCost() instead of getMemoryOpCost()\n+ // here because the EVL recipes using EVL to replace the tail mask. But in the\n+ // legacy model, it will always calculate the cost of mask.\n+ // TODO: Using getMemoryOpCost() instead of getMaskedMemoryOpCost when we\n+ // don't need to compare to the legacy cost model.\n+ Type *Ty = ToVectorTy(getLoadStoreType(&Ingredient), VF);\n+ const Align Alignment =\n+ getLoadStoreAlignment(const_cast<Instruction *>(&Ingredient));\n+ unsigned AS =\n+ getLoadStoreAddressSpace(const_cast<Instruction *>(&Ingredient));\n+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;\n+ InstructionCost Cost = Ctx.TTI.getMaskedMemoryOpCost(\n+ Ingredient.getOpcode(), Ty, Alignment, AS, CostKind);\n+ if (!Reverse)\n+ return Cost;\n+\n+ return Cost + Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Reverse,\n+ cast<VectorType>(Ty), {}, CostKind, 0);\n+}\n+\n #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)\n void VPWidenStoreEVLRecipe::print(raw_ostream &O, const Twine &Indent,\n VPSlotTracker &SlotTracker) const {\n", "tests": [{"file": "llvm/test/Transforms/LoopVectorize/RISCV/vectorize-force-tail-with-evl-uniform-store.ll", "commands": ["opt < %s --prefer-predicate-over-epilogue=predicate-dont-vectorize --passes=loop-vectorize -mcpu=sifive-p470 -mattr=+v,+f -force-tail-folding-style=data-with-evl -S"], "tests": [{"test_name": "lshift_significand", "test_body": "target datalayout = \"e-m:e-p:64:64-i64:64-i128:128-n32:64-S128\"\ntarget triple = \"riscv64-unknown-linux-gnu\"\n\ndefine void @lshift_significand(i32 %n, ptr nocapture writeonly %dst) {\nentry:\n %cmp1.peel = icmp eq i32 %n, 0\n %spec.select = select i1 %cmp1.peel, i64 2, i64 0\n br label %loop\n\nloop: ; preds = %loop, %entry\n %iv = phi i64 [ %spec.select, %entry ], [ %iv.next, %loop ]\n %0 = sub nuw nsw i64 1, %iv\n %arrayidx13 = getelementptr i64, ptr %dst, i64 %0\n store i64 0, ptr %arrayidx13, align 8\n %iv.next = add nuw nsw i64 %iv, 1\n %exitcond.not = icmp eq i64 %iv.next, 3\n br i1 %exitcond.not, label %exit, label %loop\n\nexit: ; preds = %loop\n ret void\n}\n"}]}], "issue": {"title": "[LV][VPlan] Crash due to disagreements on the VPlan cost v.s. legacy cost model", "body": "Given this input:\r\n``` llvm\r\ntarget datalayout = \"e-m:e-p:64:64-i64:64-i128:128-n32:64-S128\"\r\ntarget triple = \"riscv64-unknown-linux-gnu\"\r\n\r\ndefine void @lshift_significand(i32 %n, ptr %0) {\r\nentry:\r\n br label %for.cond\r\n\r\nfor.cond: ; preds = %for.cond, %entry\r\n %i.0 = phi i32 [ 0, %entry ], [ 2, %for.cond ]\r\n %add = or i32 %n, %i.0\r\n %cmp1 = icmp eq i32 %add, 0\r\n br i1 %cmp1, label %for.cond, label %for.cond7\r\n\r\nfor.cond7: ; preds = %for.body9, %for.cond\r\n %i.1 = phi i32 [ %i.0, %for.cond ], [ %inc15, %for.body9 ]\r\n %cmp8 = icmp ult i32 %i.1, 3\r\n br i1 %cmp8, label %for.body9, label %for.end16\r\n\r\nfor.body9: ; preds = %for.cond7\r\n %sub11 = sub nuw i32 1, %i.1\r\n %idxprom12 = zext i32 %sub11 to i64\r\n %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %idxprom12\r\n store i64 0, ptr %arrayidx13, align 8\r\n %inc15 = add i32 %i.1, 1\r\n br label %for.cond7\r\n\r\nfor.end16: ; preds = %for.cond7\r\n ret void\r\n}\r\n```\r\nAnd this command:\r\n``` bash\r\nopt -mcpu=sifive-p470 -O3 -prefer-predicate-over-epilogue=predicate-dont-vectorize -force-tail-folding-style=data-with-evl input.ll -disable-output\r\n```\r\nWe'll get the following assertion:\r\n```\r\nllvm/lib/Transforms/Vectorize/LoopVectorize.cpp:7386: VectorizationFactor llvm::LoopVectorizationPlanner::computeBestVF(): Assertion `(BestFactor.Width == LegacyVF.Width || planContainsAdditionalSimplifications(getPlanFor(BestFactor.Width), CostCtx, OrigLoop)) && \" VPlan cost model and legacy cost model disagreed\"' failed.\r\n```\r\n\r\n\r\nPreliminary investigation shows that this is caused by the disagreement on (reverse) widen store's cost. First, this is the trace for VPlan's cost model:\r\n```\r\nLV: Scalar loop costs: 4.\r\nCost of 1 for VF vscale x 1: induction instruction %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nCost of 0 for VF vscale x 1: induction instruction %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nCost of 1 for VF vscale x 1: exit condition instruction %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nCost of 0 for VF vscale x 1: EMIT vp<%4> = CANONICAL-INDUCTION ir<0>, vp<%12>\r\nCost of 0 for VF vscale x 1: EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<%5> = phi ir<0>, vp<%11>\r\nCost of 0 for VF vscale x 1: EMIT vp<%6> = EXPLICIT-VECTOR-LENGTH vp<%5>, vp<%3>\r\nCost of 0 for VF vscale x 1: vp<%7> = DERIVED-IV ir<%spec.select> + vp<%5> * ir<1>\r\nCost of 0 for VF vscale x 1: vp<%8> = SCALAR-STEPS vp<%7>, ir<1>\r\nCost of 1 for VF vscale x 1: CLONE ir<%1> = sub nuw nsw ir<1>, vp<%8>\r\nCost of 0 for VF vscale x 1: CLONE ir<%arrayidx13> = getelementptr ir<%0>, ir<0>, ir<%1>\r\nCost of 0 for VF vscale x 1: vp<%9> = vector-pointer (reverse) ir<%arrayidx13>\r\nCost of 8 for VF vscale x 1: WIDEN vp.store vp<%9>, ir<0>, vp<%6>\r\nCost of 0 for VF vscale x 1: SCALAR-CAST vp<%10> = zext vp<%6> to i64\r\nCost of 0 for VF vscale x 1: EMIT vp<%11> = add vp<%10>, vp<%5>\r\nCost of 0 for VF vscale x 1: EMIT vp<%12> = add vp<%4>, vp<%0>\r\nCost of 0 for VF vscale x 1: EMIT branch-on-count vp<%12>, vp<%1>\r\nCost of 0 for VF vscale x 1: vector loop backedge\r\nCost for VF vscale x 1: 11\r\nCost of 1 for VF vscale x 2: induction instruction %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nCost of 0 for VF vscale x 2: induction instruction %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nCost of 1 for VF vscale x 2: exit condition instruction %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nCost of 0 for VF vscale x 2: EMIT vp<%4> = CANONICAL-INDUCTION ir<0>, vp<%12>\r\nCost of 0 for VF vscale x 2: EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<%5> = phi ir<0>, vp<%11>\r\nCost of 0 for VF vscale x 2: EMIT vp<%6> = EXPLICIT-VECTOR-LENGTH vp<%5>, vp<%3>\r\nCost of 0 for VF vscale x 2: vp<%7> = DERIVED-IV ir<%spec.select> + vp<%5> * ir<1>\r\nCost of 0 for VF vscale x 2: vp<%8> = SCALAR-STEPS vp<%7>, ir<1>\r\nCost of 1 for VF vscale x 2: CLONE ir<%1> = sub nuw nsw ir<1>, vp<%8>\r\nCost of 0 for VF vscale x 2: CLONE ir<%arrayidx13> = getelementptr ir<%0>, ir<0>, ir<%1>\r\nCost of 0 for VF vscale x 2: vp<%9> = vector-pointer (reverse) ir<%arrayidx13>\r\nCost of 14 for VF vscale x 2: WIDEN vp.store vp<%9>, ir<0>, vp<%6>\r\nCost of 0 for VF vscale x 2: SCALAR-CAST vp<%10> = zext vp<%6> to i64\r\nCost of 0 for VF vscale x 2: EMIT vp<%11> = add vp<%10>, vp<%5>\r\nCost of 0 for VF vscale x 2: EMIT vp<%12> = add vp<%4>, vp<%0>\r\nCost of 0 for VF vscale x 2: EMIT branch-on-count vp<%12>, vp<%1>\r\nCost of 0 for VF vscale x 2: vector loop backedge\r\nCost for VF vscale x 2: 17\r\n```\r\nWith VPlan's cost model, we will eventually choose scalar loop, because when VF=vscale x 1, the final cost is `ceil(11 / 2) = 6`; when VF=vscale x 2, the final cost is `ceil(17 / 4) = 5`. Both of them are larger than the scalar cost, 4.\r\n\r\nWhile with the legacy cost model:\r\n```\r\nLV: Found an estimated cost of 0 for VF 1 For instruction: %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: %1 = sub nuw nsw i64 1, %indvars.iv\r\nLV: Found an estimated cost of 0 for VF 1 For instruction: %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %1\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: store i64 0, ptr %arrayidx13, align 8\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nLV: Found an estimated cost of 1 for VF 1 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nLV: Found an estimated cost of 0 for VF 1 For instruction: br i1 %exitcond.not, label %for.end16, label %for.body9\r\nLV: Scalar loop costs: 4.\r\nLV: Found an estimated cost of 0 for VF vscale x 1 For instruction: %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nLV: Found an estimated cost of 1 for VF vscale x 1 For instruction: %1 = sub nuw nsw i64 1, %indvars.iv\r\nLV: Found an estimated cost of 0 for VF vscale x 1 For instruction: %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %1\r\nLV: Found an estimated cost of 7 for VF vscale x 1 For instruction: store i64 0, ptr %arrayidx13, align 8\r\nLV: Found an estimated cost of 1 for VF vscale x 1 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nLV: Found an estimated cost of 1 for VF vscale x 1 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nLV: Found an estimated cost of 0 for VF vscale x 1 For instruction: br i1 %exitcond.not, label %for.end16, label %for.body9\r\nLV: Vector loop of width vscale x 1 costs: 5 (assuming a minimum vscale of 2).\r\nLV: Found an estimated cost of 0 for VF vscale x 2 For instruction: %indvars.iv = phi i64 [ %spec.select, %entry ], [ %indvars.iv.next, %for.body9 ]\r\nLV: Found an estimated cost of 1 for VF vscale x 2 For instruction: %1 = sub nuw nsw i64 1, %indvars.iv\r\nLV: Found an estimated cost of 0 for VF vscale x 2 For instruction: %arrayidx13 = getelementptr [3 x i64], ptr %0, i64 0, i64 %1\r\nLV: Found an estimated cost of 13 for VF vscale x 2 For instruction: store i64 0, ptr %arrayidx13, align 8\r\nLV: Found an estimated cost of 1 for VF vscale x 2 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\r\nLV: Found an estimated cost of 1 for VF vscale x 2 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, 3\r\nLV: Found an estimated cost of 0 for VF vscale x 2 For instruction: br i1 %exitcond.not, label %for.end16, label %for.body9\r\nLV: Vector loop of width vscale x 2 costs: 4 (assuming a minimum vscale of 2).\r\nLV: Selecting VF: vscale x 2.\r\n```\r\nWe will eventually choose VF=vscale x 2.\r\n\r\nThe key difference is the cost of `store i64 0, ptr %arrayidx13, align 8` v.s. cost of `WIDEN vp.store vp<%9>, ir<0>, vp<%6>`, where the latter is larger than the former by 1.\r\n\r\nIn both cases (the store instruction in VPlan cost model v.s. legacy cost model) their costs are computed by the base cost of store + the cost of shuffle due to being reverse store. I used debugger to confirm that they used the same shuffle cost, which means that for some reason, VPlan's cost model yield a slightly higher cost for store than that in the legacy cost model.\r\n", "author": "mshockwave", "labels": ["vectorizers", "crash"], "comments": [{"author": "ElvisWang123", "body": "It seems that the legacy cost model using `getMaskedMemoryOpCost` to query the instruction cost. But in the VPlan-based cost model using `getMemoryOpCost`. \r\n\r\nIn this case, the mask in the `VPWidenStoreEVLRecipe` is false since it is using the EVL recipe.\r\nThe legacy cost model will not check the tail folding type so it will using `getMaskedMemoryOpCost` instead of `getMemoryOpCost` . And the `MaskedMemoryCost` is slightly smaller than non-mask version."}, {"author": "ElvisWang123", "body": "Open a PR to fix this issue. #109644 \r\n\r\nWe remove the shuffle cost when the value stored by reversed vector is loop invariant."}, {"author": "mshockwave", "body": "CC @fhahn "}]}, "verified": true}
|
79 |
{"bug_id": "118850", "issue_url": "https://github.com/llvm/llvm-project/issues/118850", "bug_type": "crash", "base_commit": "9ad22cf0ee9eb686ea9fd76fd9cc41f4c006ccbc", "knowledge_cutoff": "2024-12-05T18:11:28Z", "lit_test_dir": ["llvm/test/Transforms/LoopVectorize"], "hints": {"fix_commit": "4f7f71b7bccdc38f37b82981e8fa9ceb536a7016", "components": ["LoopVectorize"], "bug_location_lineno": {"llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp": [[168, 174]]}, "bug_location_funcname": {"llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp": ["UnrollState::unrollWidenInductionByUF"]}}, "patch": "commit 4f7f71b7bccdc38f37b82981e8fa9ceb536a7016\nAuthor: Florian Hahn <[email protected]>\nDate: Fri Dec 6 16:28:47 2024 +0000\n\n [VPlan] Compare APInt instead of getSExtValue to fix crash in unroll.\n \n getSExtValue assumes the result fits in 64 bits, but this may not be the\n case for indcutions with wider types. Instead, directly perform the\n compare on the APInt for the ConstantInt.\n \n Fixes https://github.com/llvm/llvm-project/issues/118850.\n\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp\nindex f653269713b3..ff6c9295ee20 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp\n@@ -168,7 +168,7 @@ void UnrollState::unrollWidenInductionByUF(\n auto *ConstStep = ScalarStep->isLiveIn()\n ? dyn_cast<ConstantInt>(ScalarStep->getLiveInIRValue())\n : nullptr;\n- if (!ConstStep || ConstStep->getZExtValue() != 1) {\n+ if (!ConstStep || ConstStep->getValue() != 1) {\n if (TypeInfo.inferScalarType(ScalarStep) != IVTy) {\n ScalarStep =\n Builder.createWidenCast(Instruction::Trunc, ScalarStep, IVTy);\n", "tests": [{"file": "llvm/test/Transforms/LoopVectorize/interleave-with-i65-induction.ll", "commands": ["opt -p loop-vectorize -force-vector-width=4 -force-vector-interleave=2 -S %s"], "tests": [{"test_name": "i65_induction_with_negative_step", "test_body": "define void @i65_induction_with_negative_step(ptr %dst) {\nentry:\n br label %loop\n\nloop: ; preds = %loop, %entry\n %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]\n %iv.i65 = phi i65 [ 0, %entry ], [ %iv.i65.next, %loop ]\n %for = phi i64 [ 0, %entry ], [ %trunc, %loop ]\n %trunc = trunc i65 %iv.i65 to i64\n %gep = getelementptr inbounds i64, ptr %dst, i64 %trunc\n store i64 %for, ptr %gep, align 8\n %iv.next = add i64 %iv, 1\n %icmp = icmp eq i64 %iv.next, 1000\n %iv.i65.next = add i65 %iv.i65, -1\n br i1 %icmp, label %exit, label %loop\n\nexit: ; preds = %loop\n ret void\n}\n"}]}], "issue": {"title": "Assertion `getActiveBits() <= 64 && \"Too many bits for uint64_t\"' failed.", "body": "To reproduce run the test below with -passes=loop-vectorize\n```\n; ModuleID = './reduced.ll'\nsource_filename = \"./reduced.ll\"\ntarget datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32-ni:1-p2:32:8:8:32-ni:2\"\ntarget triple = \"aarch64-none-linux-gnu\"\n\ndefine double @wombat() gc \"statepoint-example\" {\nbb:\n br label %bb2\n\nbb1: ; preds = %bb2\n ret double 0.000000e+00\n\nbb2: ; preds = %bb2, %bb\n %phi = phi i64 [ 0, %bb ], [ %add, %bb2 ]\n %phi3 = phi i65 [ 0, %bb ], [ %add5, %bb2 ]\n %phi4 = phi i64 [ 0, %bb ], [ %trunc, %bb2 ]\n %trunc = trunc i65 %phi3 to i64\n store i64 0, ptr addrspace(1) null, align 8\n %add = add i64 %phi, 1\n %icmp = icmp ugt i64 %add, 0\n %add5 = add i65 %phi3, -1\n br i1 %icmp, label %bb2, label %bb1\n}\n```\nReproducer: https://godbolt.org/z/cjEcv8b4Y\n\nStack dump:\n```\n0.\tProgram arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=loop-vectorize <source>\n1.\tRunning pass \"function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)\" on module \"<source>\"\n2.\tRunning pass \"loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>\" on function \"wombat\"\n #0 0x00000000051b3748 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x51b3748)\n #1 0x00000000051b113c SignalHandler(int) Signals.cpp:0:0\n #2 0x000071a06ec42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)\n #3 0x000071a06ec969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)\n #4 0x000071a06ec42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)\n #5 0x000071a06ec287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)\n #6 0x000071a06ec2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)\n #7 0x000071a06ec39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)\n #8 0x0000000000b2d8a9 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xb2d8a9)\n #9 0x0000000004308ae6 (anonymous namespace)::UnrollState::unrollBlock(llvm::VPBlockBase*) VPlanUnroll.cpp:0:0\n#10 0x000000000430788c (anonymous namespace)::UnrollState::unrollBlock(llvm::VPBlockBase*) VPlanUnroll.cpp:0:0\n#11 0x0000000004309060 llvm::VPlanTransforms::unrollByUF(llvm::VPlan&, unsigned int, llvm::LLVMContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4309060)\n#12 0x0000000004176b76 llvm::LoopVectorizationPlanner::executePlan(llvm::ElementCount, unsigned int, llvm::VPlan&, llvm::InnerLoopVectorizer&, llvm::DominatorTree*, bool, llvm::DenseMap<llvm::SCEV const*, llvm::Value*, llvm::DenseMapInfo<llvm::SCEV const*, void>, llvm::detail::DenseMapPair<llvm::SCEV const*, llvm::Value*>> const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4176b76)\n#13 0x000000000418cb6f llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x418cb6f)\n#14 0x000000000418ebd9 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x418ebd9)\n#15 0x000000000418f213 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x418f213)\n#16 0x0000000002fcba8e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2fcba8e)\n#17 0x0000000004fb2700 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fb2700)\n#18 0x0000000000e33a9e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe33a9e)\n#19 0x0000000004fb101b llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fb101b)\n#20 0x0000000000e3312e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe3312e)\n#21 0x0000000004fb0a20 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fb0a20)\n#22 0x0000000000910a1a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x910a1a)\n#23 0x0000000000903099 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x903099)\n#24 0x000071a06ec29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)\n#25 0x000071a06ec29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)\n#26 0x00000000008fa97e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8fa97e)\nProgram terminated with signal: SIGSEGV\nCompiler returned: 139\n```\n", "author": "TatyanaDoubts", "labels": ["vectorizers", "crash"], "comments": [{"author": "huang-me", "body": "Hi @TatyanaDoubts, \nHow did you get the IR snippet? \nThe IR you provided is weird. I'll have to check where i65 constant 0 in `%phi3 = phi i65 [ 0, %bb ], [ %add5, %bb2 ]` come from since it seems it's the origin of the problem."}, {"author": "fhahn", "body": "I suspect this was found via a fuzzer, thanks for sharing the test case!"}, {"author": "TatyanaDoubts", "body": "Hi @huang-me, @fhahn, yes this crash was found by the fuzzer generated test. "}]}, "verified": true}
|
80 |
{"bug_id": "115149", "issue_url": "https://github.com/llvm/llvm-project/issues/115149", "bug_type": "miscompilation", "base_commit": "0f44d72e0ee74970cf696ff4c791f63e0c3fa9b4", "knowledge_cutoff": "2024-11-06T11:22:14Z", "lit_test_dir": ["llvm/test/Transforms/InstCombine"], "hints": {"fix_commit": "929cbe7f596733f85cd274485acc19442dd34a80", "components": ["InstCombine"], "bug_location_lineno": {"llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp": [[545, 551]]}, "bug_location_funcname": {"llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp": ["InstCombinerImpl::foldPHIArgGEPIntoPHI"]}}, "patch": "commit 929cbe7f596733f85cd274485acc19442dd34a80\nAuthor: Antonio Frighetto <[email protected]>\nDate: Tue Nov 12 19:02:04 2024 +0100\n\n [InstCombine] Intersect nowrap flags between geps while folding into phi\n \n A miscompilation issue has been addressed with refined checking.\n \n Fixes: https://github.com/llvm/llvm-project/issues/115149.\n\ndiff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp\nindex cb5c44730512..1fcf1c570add 100644\n--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp\n+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp\n@@ -545,7 +545,8 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {\n // especially bad when the PHIs are in the header of a loop.\n bool NeededPhi = false;\n \n- GEPNoWrapFlags NW = GEPNoWrapFlags::all();\n+ // Remember flags of the first phi-operand getelementptr.\n+ GEPNoWrapFlags NW = FirstInst->getNoWrapFlags();\n \n // Scan to see if all operands are the same opcode, and all have one user.\n for (Value *V : drop_begin(PN.incoming_values())) {\n", "tests": [{"file": "llvm/test/Transforms/InstCombine/opaque-ptr.ll", "commands": ["opt -S -passes=instcombine < %s"], "tests": [{"test_name": "phi_of_gep_flags_1", "test_body": "define ptr @phi_of_gep_flags_1(i1 %c, ptr %p) {\n br i1 %c, label %if, label %else\n\nif: ; preds = %0\n %gep1 = getelementptr inbounds i32, ptr %p, i64 1\n br label %join\n\nelse: ; preds = %0\n %gep2 = getelementptr nusw nuw i32, ptr %p, i64 1\n br label %join\n\njoin: ; preds = %else, %if\n %phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]\n ret ptr %phi\n}\n"}]}, {"file": "llvm/test/Transforms/InstCombine/phi.ll", "commands": ["opt < %s -passes=instcombine -S"], "tests": [{"test_name": "wrong_gep_arg_into_phi", "test_body": "target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64\"\n\ndefine i64 @wrong_gep_arg_into_phi(ptr noundef %ptr) {\nentry:\n %add.ptr = getelementptr i8, ptr %ptr, i64 1\n br label %for.cond\n\nfor.cond: ; preds = %for.cond, %entry\n %.pn = phi ptr [ %add.ptr, %entry ], [ %incdec.ptr, %for.cond ]\n %val = load i8, ptr %.pn, align 1\n %cond = icmp ne i8 %val, 0\n %incdec.ptr = getelementptr inbounds nuw i8, ptr %.pn, i64 1\n br i1 %cond, label %for.cond, label %exit\n\nexit: ; preds = %for.cond\n ret i64 0\n}\n"}]}], "issue": {"title": "[clang] Miscompile with -O3 and -O0/1/2 since 18.1.0", "body": "When I compiled this code with -O0/1/2, its output is 0. However, with -O3, it returned 143:\r\n\r\n```c\r\nint printf(const char *, ...);\r\nchar a, b;\r\nint c;\r\nchar *e = &b;\r\nint f(char *g, int *k) {\r\n char *d = g + *k;\r\n for (; *d && *d <= ' '; d++)\r\n ;\r\n if (*d)\r\n return 0;\r\n return 1;\r\n}\r\nint l(int g) {\r\n char h[] = {a, a, a};\r\n int i[] = {g};\r\n int j = f(h, i);\r\n return j;\r\n}\r\nlong m() {\r\n *e = 255;\r\n for (; l(b + 1);)\r\n return 0;\r\n for (;;)\r\n ;\r\n}\r\nint main() {\r\n m();\r\n printf(\"%d\\n\", c);\r\n}\r\n```\r\n\r\nDetails can be found here: https://godbolt.org/z/dnvvTPror", "author": "cardigan1008", "labels": ["miscompilation", "llvm:instcombine"], "comments": [{"author": "antoniofrighetto", "body": "This looks like a nasty one. Getting the optimized IR via -O3 and setting noinline/optnone to main, we get: https://llvm.godbolt.org/z/vaPvs6d16. \r\n\r\nThis is the initial assembly output with -O1 (which leads to the infinite loop):\r\n\r\n```asm\r\nmain: # @main\r\n push rax\r\n mov rax, qword ptr [e]\r\n mov byte ptr [rax], -1\r\n movsx rax, byte ptr [b]\r\n mov cl, byte ptr [a]\r\n mov byte ptr [rsp + 6], cl\r\n mov byte ptr [rsp + 7], cl\r\n lea rdx, [rsp + 5]\r\n add rdx, rax\r\n mov rax, rdx\r\n.LBB0_1: # %for.cond.i.i.i\r\n add rax, 1\r\n mov cl, byte ptr [rdx + 1]\r\n ...\r\n```\r\nWhereas this is with -O0 (returns 0):\r\n```asm\r\nmain: # @main\r\n sub rsp, 24\r\n mov rax, qword ptr [e]\r\n mov byte ptr [rax], -1\r\n movsx rcx, byte ptr [b]\r\n mov al, byte ptr [a]\r\n mov byte ptr [rsp + 22], al\r\n mov byte ptr [rsp + 23], al\r\n lea rax, [rsp + 21]\r\n add rax, rcx\r\n mov qword ptr [rsp + 8], rax # 8-byte Spill\r\n.LBB0_1: # %for.cond.i.i.i\r\n mov rcx, qword ptr [rsp + 8] # 8-byte Reload\r\n mov rax, rcx\r\n add rax, 1\r\n mov dl, byte ptr [rcx + 1]\r\n```\r\nIn the optimized one, $rdx is set to point to $rsp+0x5, part of the stack slot containing $rax (undef), later dereferenced to set the condition leading to the loop. Initially I thought some bug in regalloc, but to me it seems more something in prologepiloginserter. Function prologue / epilogue seems set up correctly with triple arm64-apple-macosx."}, {"author": "antoniofrighetto", "body": "Looking at the IR better, it seems like InstCombine performs a wrong optimization immediately after inlining `l` into `m`, probably related with default argument conversion done by operator + (which should include integer promotion on types smaller than integer). Alive2 seems to confirm it: https://alive2.llvm.org/ce/z/XAzrip."}, {"author": "antoniofrighetto", "body": "Reduced to:\r\n```llvm\r\n@a = global i8 0, align 1\r\n\r\ndefine i64 @src(i8 noundef %arg) {\r\nentry:\r\n %h.i = alloca [3 x i8], align 1\r\n %conv = sext i8 %arg to i32\r\n %add = add nsw i32 %conv, 1\r\n %2 = load i8, ptr @a, align 1\r\n store i8 %2, ptr %h.i, align 1\r\n %idx.ext.i.i = sext i32 %add to i64\r\n %add.ptr.i.i = getelementptr inbounds i8, ptr %h.i, i64 %idx.ext.i.i\r\n br label %for.cond.i.i\r\n\r\nfor.cond.i.i: ; preds = %for.cond.i.i, %entry\r\n %d.0.i.i = phi ptr [ %add.ptr.i.i, %entry ], [ %incdec.ptr.i.i, %for.cond.i.i ]\r\n %3 = load i8, ptr %d.0.i.i, align 1\r\n %tobool.not.i.i = icmp ne i8 %3, 0\r\n %incdec.ptr.i.i = getelementptr inbounds nuw i8, ptr %d.0.i.i, i64 1\r\n br i1 %tobool.not.i.i, label %for.cond.i.i, label %l.exit\r\n\r\nl.exit: ; preds = %for.cond.i.i\r\n ret i64 0\r\n}\r\n```\r\nWrong sext of add of sext simplified to sext leads to wrong gep offset: https://alive2.llvm.org/ce/z/JaR4j9."}, {"author": "AZero13", "body": "Can we backport this to 19.x?"}, {"author": "antoniofrighetto", "body": "Should be fixed in 929cbe7f596733f85cd274485acc19442dd34a80, thanks for reporting!"}, {"author": "AZero13", "body": "@antoniofrighetto can we please backport this "}]}, "verified": true}
|
dataset/131071.json
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bug_id": "131071",
|
3 |
+
"issue_url": "https://github.com/llvm/llvm-project/issues/131071",
|
4 |
+
"bug_type": "crash",
|
5 |
+
"base_commit": "fecd937910f640663885e0434bfed80cf4574072",
|
6 |
+
"knowledge_cutoff": "2025-03-13T04:07:59Z",
|
7 |
+
"lit_test_dir": [
|
8 |
+
"llvm/test/Transforms/LoopVectorize"
|
9 |
+
],
|
10 |
+
"hints": {
|
11 |
+
"fix_commit": "2e13ec561c8139951e3f0815c35c758a7a3011db",
|
12 |
+
"components": [
|
13 |
+
"LoopVectorize"
|
14 |
+
],
|
15 |
+
"bug_location_lineno": {
|
16 |
+
"llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h": [
|
17 |
+
[
|
18 |
+
494,
|
19 |
+
501
|
20 |
+
]
|
21 |
+
],
|
22 |
+
"llvm/lib/Transforms/Vectorize/LoopVectorize.cpp": [
|
23 |
+
[
|
24 |
+
7121,
|
25 |
+
7126
|
26 |
+
],
|
27 |
+
[
|
28 |
+
9620,
|
29 |
+
9626
|
30 |
+
],
|
31 |
+
[
|
32 |
+
9640,
|
33 |
+
9649
|
34 |
+
]
|
35 |
+
],
|
36 |
+
"llvm/lib/Transforms/Vectorize/VPlan.cpp": [
|
37 |
+
[
|
38 |
+
1532,
|
39 |
+
1543
|
40 |
+
]
|
41 |
+
],
|
42 |
+
"llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp": [
|
43 |
+
[
|
44 |
+
32,
|
45 |
+
38
|
46 |
+
],
|
47 |
+
[
|
48 |
+
83,
|
49 |
+
88
|
50 |
+
],
|
51 |
+
[
|
52 |
+
106,
|
53 |
+
111
|
54 |
+
]
|
55 |
+
],
|
56 |
+
"llvm/lib/Transforms/Vectorize/VPlanTransforms.h": [
|
57 |
+
[
|
58 |
+
68,
|
59 |
+
79
|
60 |
+
]
|
61 |
+
]
|
62 |
+
},
|
63 |
+
"bug_location_funcname": {
|
64 |
+
"llvm/lib/Transforms/Vectorize/LoopVectorize.cpp": [
|
65 |
+
"LoopVectorizationPlanner::buildVPlan",
|
66 |
+
"LoopVectorizationPlanner::planInVPlanNativePath",
|
67 |
+
"LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes"
|
68 |
+
],
|
69 |
+
"llvm/lib/Transforms/Vectorize/VPlan.cpp": [
|
70 |
+
"LoopVectorizationPlanner::buildVPlans"
|
71 |
+
],
|
72 |
+
"llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp": [
|
73 |
+
"VPlanTransforms::VPInstructionsToVPRecipes",
|
74 |
+
"sinkScalarOperands"
|
75 |
+
]
|
76 |
+
}
|
77 |
+
},
|
78 |
+
"patch": "commit 2e13ec561c8139951e3f0815c35c758a7a3011db\nAuthor: Florian Hahn <[email protected]>\nDate: Wed Mar 19 21:35:15 2025 +0000\n\n [VPlan] Bail out on non-intrinsic calls in VPlanNativePath.\n \n Update initial VPlan-construction in VPlanNativePath in line with the\n inner loop path, in that it bails out when encountering constructs it\n cannot handle, like non-intrinsic calls.\n \n Fixes https://github.com/llvm/llvm-project/issues/131071.\n\ndiff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h\nindex ed3e45dd2c6c..7bbe8669cef6 100644\n--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h\n+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h\n@@ -494,8 +494,10 @@ protected:\n private:\n /// Build a VPlan according to the information gathered by Legal. \\return a\n /// VPlan for vectorization factors \\p Range.Start and up to \\p Range.End\n- /// exclusive, possibly decreasing \\p Range.End.\n- VPlanPtr buildVPlan(VFRange &Range);\n+ /// exclusive, possibly decreasing \\p Range.End. If no VPlan can be built for\n+ /// the input range, set the largest included VF to the maximum VF for which\n+ /// no plan could be built.\n+ VPlanPtr tryToBuildVPlan(VFRange &Range);\n \n /// Build a VPlan using VPRecipes according to the information gather by\n /// Legal. This method is only used for the legacy inner loop vectorizer.\ndiff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\nindex 89337dc38535..61e0d18445b6 100644\n--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\n+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\n@@ -7121,6 +7121,9 @@ LoopVectorizationPlanner::planInVPlanNativePath(ElementCount UserVF) {\n << \"VF \" << VF << \" to build VPlans.\\n\");\n buildVPlans(VF, VF);\n \n+ if (VPlans.empty())\n+ return VectorizationFactor::Disabled();\n+\n // For VPlan build stress testing, we bail out after VPlan construction.\n if (VPlanBuildStressTest)\n return VectorizationFactor::Disabled();\n@@ -9620,7 +9623,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {\n return Plan;\n }\n \n-VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {\n+VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {\n // Outer loop handling: They may require CFG and instruction level\n // transformations before even evaluating whether vectorization is profitable.\n // Since we cannot modify the incoming IR, we need to build VPlan upfront in\n@@ -9640,10 +9643,13 @@ VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {\n for (ElementCount VF : Range)\n Plan->addVF(VF);\n \n- VPlanTransforms::VPInstructionsToVPRecipes(\n- Plan,\n- [this](PHINode *P) { return Legal->getIntOrFpInductionDescriptor(P); },\n- *PSE.getSE(), *TLI);\n+ if (!VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(\n+ Plan,\n+ [this](PHINode *P) {\n+ return Legal->getIntOrFpInductionDescriptor(P);\n+ },\n+ *PSE.getSE(), *TLI))\n+ return nullptr;\n \n // Tail folding is not supported for outer loops, so the induction increment\n // is guaranteed to not wrap.\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp\nindex b63cdd04530a..ef82734840ed 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp\n@@ -1532,12 +1532,13 @@ void LoopVectorizationPlanner::buildVPlans(ElementCount MinVF,\n auto MaxVFTimes2 = MaxVF * 2;\n for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFTimes2);) {\n VFRange SubRange = {VF, MaxVFTimes2};\n- auto Plan = buildVPlan(SubRange);\n- VPlanTransforms::optimize(*Plan);\n- // Update the name of the latch of the top-level vector loop region region\n- // after optimizations which includes block folding.\n- Plan->getVectorLoopRegion()->getExiting()->setName(\"vector.latch\");\n- VPlans.push_back(std::move(Plan));\n+ if (auto Plan = tryToBuildVPlan(SubRange)) {\n+ VPlanTransforms::optimize(*Plan);\n+ // Update the name of the latch of the top-level vector loop region region\n+ // after optimizations which includes block folding.\n+ Plan->getVectorLoopRegion()->getExiting()->setName(\"vector.latch\");\n+ VPlans.push_back(std::move(Plan));\n+ }\n VF = SubRange.End;\n }\n }\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\nindex e6f788a18805..6c8680ac9661 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\n@@ -32,7 +32,7 @@\n \n using namespace llvm;\n \n-void VPlanTransforms::VPInstructionsToVPRecipes(\n+bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(\n VPlanPtr &Plan,\n function_ref<const InductionDescriptor *(PHINode *)>\n GetIntOrFpInductionDescriptor,\n@@ -83,6 +83,9 @@ void VPlanTransforms::VPInstructionsToVPRecipes(\n } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {\n NewRecipe = new VPWidenGEPRecipe(GEP, Ingredient.operands());\n } else if (CallInst *CI = dyn_cast<CallInst>(Inst)) {\n+ Intrinsic::ID VectorID = getVectorIntrinsicIDForCall(CI, &TLI);\n+ if (VectorID == Intrinsic::not_intrinsic)\n+ return false;\n NewRecipe = new VPWidenIntrinsicRecipe(\n *CI, getVectorIntrinsicIDForCall(CI, &TLI),\n {Ingredient.op_begin(), Ingredient.op_end() - 1}, CI->getType(),\n@@ -106,6 +109,7 @@ void VPlanTransforms::VPInstructionsToVPRecipes(\n Ingredient.eraseFromParent();\n }\n }\n+ return true;\n }\n \n static bool sinkScalarOperands(VPlan &Plan) {\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h\nindex 2ec07eea1ecc..fe93637ab723 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h\n+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h\n@@ -68,12 +68,13 @@ struct VPlanTransforms {\n bool RequiresScalarEpilogueCheck, bool TailFolded, Loop *TheLoop);\n \n /// Replaces the VPInstructions in \\p Plan with corresponding\n- /// widen recipes.\n- static void\n- VPInstructionsToVPRecipes(VPlanPtr &Plan,\n- function_ref<const InductionDescriptor *(PHINode *)>\n- GetIntOrFpInductionDescriptor,\n- ScalarEvolution &SE, const TargetLibraryInfo &TLI);\n+ /// widen recipes. Returns false if any VPInstructions could not be converted\n+ /// to a wide recipe if needed.\n+ static bool tryToConvertVPInstructionsToVPRecipes(\n+ VPlanPtr &Plan,\n+ function_ref<const InductionDescriptor *(PHINode *)>\n+ GetIntOrFpInductionDescriptor,\n+ ScalarEvolution &SE, const TargetLibraryInfo &TLI);\n \n /// Try to have all users of fixed-order recurrences appear after the recipe\n /// defining their previous value, by either sinking users or hoisting recipes\n",
|
79 |
+
"tests": [
|
80 |
+
{
|
81 |
+
"file": "llvm/test/Transforms/LoopVectorize/vplan-widen-call-instruction.ll",
|
82 |
+
"commands": [
|
83 |
+
"opt -passes=loop-vectorize -force-vector-width=4 -enable-vplan-native-path -S %s"
|
84 |
+
],
|
85 |
+
"tests": [
|
86 |
+
{
|
87 |
+
"test_name": "call_to_non_intrinsic",
|
88 |
+
"test_body": "define void @call_to_non_intrinsic() {\nentry:\n br label %outer.header\n\nouter.header: ; preds = %outer.latch, %entry\n %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]\n br label %inner.header\n\ninner.header: ; preds = %inner.header, %outer.header\n %inner.iv = phi i64 [ 0, %outer.header ], [ %inner.iv.next, %inner.header ]\n call void @use()\n %inner.iv.next = add i64 %inner.iv, 1\n %inner.ec = icmp eq i64 %inner.iv.next, 100\n br i1 %inner.ec, label %outer.latch, label %inner.header\n\nouter.latch: ; preds = %inner.header\n %outer.iv.next = add i64 %outer.iv, 1\n %outer.ec = icmp eq i64 %outer.iv.next, 100\n br i1 %outer.ec, label %exit, label %outer.header, !llvm.loop !0\n\nexit: ; preds = %outer.latch\n ret void\n}\n\ndeclare void @use()\n\n!0 = distinct !{!0, !1}\n!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
|
89 |
+
}
|
90 |
+
]
|
91 |
+
}
|
92 |
+
],
|
93 |
+
"issue": {
|
94 |
+
"title": "[LoopVectorize] Assertion `ID != Intrinsic::not_intrinsic && \"Not an intrinsic!\"' failed.",
|
95 |
+
"body": "Reproducer: https://godbolt.org/z/n7E1TjTv5\n```\n; bin/opt -passes=loop-vectorize -enable-vplan-native-path reduced.ll -S\ntarget datalayout = \"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-unknown-linux-gnu\"\n\ndefine i32 @main() {\nentry:\n br label %for.cond2.preheader.i815\n\nfor.cond2.preheader.i815: ; preds = %for.cond.cleanup8.i835.split.us, %entry\n %I.03.i816 = phi i64 [ %inc20.i839, %for.cond.cleanup8.i835.split.us ], [ 0, %entry ]\n br label %cont.i830.us\n\ncont.i830.us: ; preds = %cont.i830.us, %for.cond2.preheader.i815\n %K.01.i824.us = phi i64 [ 0, %for.cond2.preheader.i815 ], [ %inc.i833.us, %cont.i830.us ]\n call void @use(ptr null, i64 0)\n %inc.i833.us = add i64 %K.01.i824.us, 1\n %exitcond.not.i834.us = icmp eq i64 %inc.i833.us, 0\n br i1 %exitcond.not.i834.us, label %for.cond.cleanup8.i835.split.us, label %cont.i830.us\n\nfor.cond.cleanup8.i835.split.us: ; preds = %cont.i830.us\n %inc20.i839 = add i64 %I.03.i816, 1\n %exitcond5.not.i840 = icmp eq i64 %inc20.i839, 0\n br i1 %exitcond5.not.i840, label %\"_ZZ4mainENK3$_7clEmmmPi.exit.loopexit1081\", label %for.cond2.preheader.i815, !llvm.loop !0\n\n\"_ZZ4mainENK3$_7clEmmmPi.exit.loopexit1081\": ; preds = %for.cond.cleanup8.i835.split.us\n ret i32 0\n}\n\ndeclare void @use(ptr, i64)\n\n!0 = distinct !{!0, !1, !2}\n!1 = !{!\"llvm.loop.mustprogress\"}\n!2 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n```\n```\nopt: /root/llvm-project/llvm/lib/Analysis/VectorUtils.cpp:175: bool llvm::isVectorIntrinsicWithOverloadTypeAtArg(llvm::Intrinsic::ID, int, const llvm::TargetTransformInfo*): Assertion `ID != Intrinsic::not_intrinsic && \"Not an intrinsic!\"' failed.\nPLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.\nStack dump:\n0.\tProgram arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=loop-vectorize -enable-vplan-native-path <source>\n1.\tRunning pass \"function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)\" on module \"<source>\"\n2.\tRunning pass \"loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>\" on function \"main\"\n #0 0x0000000005475858 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5475858)\n #1 0x0000000005473214 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0\n #2 0x00007024fda42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)\n #3 0x00007024fda969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)\n #4 0x00007024fda42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)\n #5 0x00007024fda287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)\n #6 0x00007024fda2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)\n #7 0x00007024fda39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)\n #8 0x0000000004bea5d7 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4bea5d7)\n #9 0x00000000045582f0 llvm::VPWidenIntrinsicRecipe::execute(llvm::VPTransformState&) (.part.0) VPlanRecipes.cpp:0:0\n#10 0x0000000004513ea7 llvm::VPBasicBlock::executeRecipes(llvm::VPTransformState*, llvm::BasicBlock*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4513ea7)\n#11 0x000000000451e4cf llvm::VPBasicBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x451e4cf)\n#12 0x000000000451f5c0 llvm::VPRegionBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x451f5c0)\n#13 0x000000000451f5c0 llvm::VPRegionBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x451f5c0)\n#14 0x000000000452b451 llvm::VPlan::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x452b451)\n#15 0x00000000043ddd2f llvm::LoopVectorizationPlanner::executePlan(llvm::ElementCount, unsigned int, llvm::VPlan&, llvm::InnerLoopVectorizer&, llvm::DominatorTree*, bool, llvm::DenseMap<llvm::SCEV const*, llvm::Value*, llvm::DenseMapInfo<llvm::SCEV const*, void>, llvm::detail::DenseMapPair<llvm::SCEV const*, llvm::Value*>> const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43ddd2f)\n#16 0x00000000043f0e36 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43f0e36)\n#17 0x00000000043f45e9 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43f45e9)\n#18 0x00000000043f4c63 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x43f4c63)\n#19 0x000000000319510e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x319510e)\n#20 0x0000000005270840 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5270840)\n#21 0x0000000000e79f8e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe79f8e)\n#22 0x000000000526f15b llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x526f15b)\n#23 0x0000000000e7910e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe7910e)\n#24 0x000000000526eb60 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x526eb60)\n#25 0x000000000094151a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x94151a)\n#26 0x0000000000934c6c optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x934c6c)\n#27 0x00007024fda29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)\n#28 0x00007024fda29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)\n#29 0x000000000092c755 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x92c755)\nProgram terminated with signal: SIGSEGV\nCompiler returned: 139\n```\nReduced from llvm-test-suite/SingleSource/UnitTests/Vectorizer/VPlanNativePath/outer-loop-vect.cpp with ubsan+https://github.com/llvm/llvm-project/pull/130990 enabled\n\n\n",
|
96 |
+
"author": "dtcxzyw",
|
97 |
+
"labels": [
|
98 |
+
"vectorizers",
|
99 |
+
"crash-on-valid"
|
100 |
+
],
|
101 |
+
"comments": [
|
102 |
+
{
|
103 |
+
"author": "zhangjunphy",
|
104 |
+
"body": "It seems for vplan vectorization, `LoopVectorizationLegality::canVectorize` only checks the loop structure and then skips the check of individual instructions. So the pass later tries to rewirte that call to `@use` and failed."
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"author": "zhangjunphy",
|
108 |
+
"body": "From the description of `isExplicitVecOuterLoop`. This does not seem to a problem of the LoopVectorization pass.\nWhen `-enable-vplan-native-path` is provided, LLVM only attempts to vectorize outer loops marked with a pragma.\nAnd the programmer is expected to ensure vectorization must be legal for those loops.\nhttps://github.com/llvm/llvm-project/blob/125553a6779ba179a9b4b5212c531da2474258fd/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp#L2183-L2192\n\n=============\nEdited for clarity."
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"author": "dtcxzyw",
|
112 |
+
"body": "Does ubsan instrumentation break this assumption?\n"
|
113 |
+
},
|
114 |
+
{
|
115 |
+
"author": "zhangjunphy",
|
116 |
+
"body": "Well, I'm far from familiar with the design of vectorization and this is just my conclusion from reading the code.\n\nFrom what I understand, if the flag `-enable-vplan-native-path` is specified, the IR producer is required to be certain that the loop marked with \"llvm.loop.vectorize.enable\" is actually vectorizable. Otherwise this will cause problems as that flag will cause vectorization to bypass some legality checks and to directly attempt vectorizing the loop. \n\nIn this case, the instrumentation added the \"llvm.loop.vectorize.enable\" metadata to the loop. But there is the call to `@use` which is not vectorizable. And this check is bypassed with `-enable-vplan-native-path`, thus caused the problem."
|
117 |
+
},
|
118 |
+
{
|
119 |
+
"author": "fhahn",
|
120 |
+
"body": "It is true that the native path somewhat relies on the loops being marked safe for vectorization by the user, we still shouldn't crash. In this particular case we can easily detect that we cannot create a valid VPlan, and bail out. I will prepare a fix."
|
121 |
+
}
|
122 |
+
]
|
123 |
+
},
|
124 |
+
"verified": true
|
125 |
+
}
|
examples/baseline.py
CHANGED
@@ -33,6 +33,7 @@ temperature = 0.0
|
|
33 |
max_input_tokens = int(os.environ.get("LAB_LLM_CONTEXT_WINDOW_SIZE", 65536))
|
34 |
# Seems not working, sad :(
|
35 |
enable_tooling = os.environ.get("LAB_LLM_ENABLE_TOOLING", "OFF") == "ON"
|
|
|
36 |
max_log_size = int(os.environ.get("LAB_LLM_MAX_LOG_SIZE", 1000000000))
|
37 |
fix_dir = os.environ["LAB_FIX_DIR"]
|
38 |
os.makedirs(fix_dir, exist_ok=True)
|
@@ -195,7 +196,7 @@ def append_message(messages, full_messages, message, dump=True):
|
|
195 |
full_messages.append(message)
|
196 |
|
197 |
|
198 |
-
def
|
199 |
reasoning_content = ""
|
200 |
content = ""
|
201 |
try:
|
@@ -252,10 +253,15 @@ def chat(env, messages, full_messages):
|
|
252 |
content = response.content
|
253 |
print("Answer:")
|
254 |
print(content)
|
255 |
-
except
|
256 |
print(e)
|
257 |
-
|
258 |
-
|
|
|
|
|
|
|
|
|
|
|
259 |
answer = {"role": "assistant", "content": content}
|
260 |
if len(reasoning_content) > 0:
|
261 |
answer["reasoning_content"] = reasoning_content
|
@@ -263,6 +269,57 @@ def chat(env, messages, full_messages):
|
|
263 |
return content
|
264 |
|
265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
format_requirement = """
|
267 |
Please answer with the code directly. Do not include any additional information in the output.
|
268 |
Please answer with the complete code snippet (including the unmodified part) that replaces the original code. Do not answer with a diff.
|
@@ -381,7 +438,8 @@ def fix_issue(issue_id):
|
|
381 |
env = Env(issue_id, basemodel_cutoff)
|
382 |
bug_funcs = env.get_hint_bug_functions()
|
383 |
if len(bug_funcs) != 1 or len(next(iter(bug_funcs.values()))) != 1:
|
384 |
-
|
|
|
385 |
messages = []
|
386 |
full_messages = [] # Log with COT tokens
|
387 |
append_message(
|
@@ -431,9 +489,8 @@ else:
|
|
431 |
override = True
|
432 |
|
433 |
for task in task_list:
|
434 |
-
# fix_issue(task)
|
435 |
try:
|
436 |
fix_issue(task)
|
437 |
except Exception as e:
|
438 |
print(e)
|
439 |
-
|
|
|
33 |
max_input_tokens = int(os.environ.get("LAB_LLM_CONTEXT_WINDOW_SIZE", 65536))
|
34 |
# Seems not working, sad :(
|
35 |
enable_tooling = os.environ.get("LAB_LLM_ENABLE_TOOLING", "OFF") == "ON"
|
36 |
+
enable_streaming = os.environ.get("LAB_LLM_ENABLE_STREAMING", "OFF") == "ON"
|
37 |
max_log_size = int(os.environ.get("LAB_LLM_MAX_LOG_SIZE", 1000000000))
|
38 |
fix_dir = os.environ["LAB_FIX_DIR"]
|
39 |
os.makedirs(fix_dir, exist_ok=True)
|
|
|
196 |
full_messages.append(message)
|
197 |
|
198 |
|
199 |
+
def chat_with_tooling(env, messages, full_messages):
|
200 |
reasoning_content = ""
|
201 |
content = ""
|
202 |
try:
|
|
|
253 |
content = response.content
|
254 |
print("Answer:")
|
255 |
print(content)
|
256 |
+
except Exception as e:
|
257 |
print(e)
|
258 |
+
append_message(
|
259 |
+
messages,
|
260 |
+
full_messages,
|
261 |
+
{"role": "assistant", "content": f"Exception: {e}"},
|
262 |
+
dump=False,
|
263 |
+
)
|
264 |
+
return ""
|
265 |
answer = {"role": "assistant", "content": content}
|
266 |
if len(reasoning_content) > 0:
|
267 |
answer["reasoning_content"] = reasoning_content
|
|
|
269 |
return content
|
270 |
|
271 |
|
272 |
+
def chat_with_streaming(env, messages, full_messages):
|
273 |
+
reasoning_content = ""
|
274 |
+
content = ""
|
275 |
+
try:
|
276 |
+
completion = client.chat.completions.create(
|
277 |
+
model=model,
|
278 |
+
messages=messages,
|
279 |
+
timeout=300,
|
280 |
+
temperature=temperature,
|
281 |
+
stream=True,
|
282 |
+
)
|
283 |
+
is_thinking = False
|
284 |
+
is_answering = False
|
285 |
+
for chunk in completion:
|
286 |
+
delta = chunk.choices[0].delta
|
287 |
+
if hasattr(delta, "reasoning_content") and delta.reasoning_content != None:
|
288 |
+
if not is_thinking:
|
289 |
+
print("Thinking:")
|
290 |
+
is_thinking = True
|
291 |
+
print(delta.reasoning_content, end="", flush=True)
|
292 |
+
reasoning_content += delta.reasoning_content
|
293 |
+
else:
|
294 |
+
if delta.content != "" and is_answering is False:
|
295 |
+
print("\nAnswer:")
|
296 |
+
is_answering = True
|
297 |
+
print(delta.content, end="", flush=True)
|
298 |
+
content += delta.content
|
299 |
+
|
300 |
+
except Exception as e:
|
301 |
+
print(e)
|
302 |
+
append_message(
|
303 |
+
messages,
|
304 |
+
full_messages,
|
305 |
+
{"role": "assistant", "content": f"Exception: {e}"},
|
306 |
+
dump=False,
|
307 |
+
)
|
308 |
+
return ""
|
309 |
+
answer = {"role": "assistant", "content": content}
|
310 |
+
if len(reasoning_content) > 0:
|
311 |
+
answer["reasoning_content"] = reasoning_content
|
312 |
+
append_message(messages, full_messages, answer, dump=False)
|
313 |
+
return content
|
314 |
+
|
315 |
+
|
316 |
+
def chat(env, messages, full_messages):
|
317 |
+
if enable_streaming:
|
318 |
+
assert not enable_tooling
|
319 |
+
return chat_with_streaming(env, messages, full_messages)
|
320 |
+
return chat_with_tooling(env, messages, full_messages)
|
321 |
+
|
322 |
+
|
323 |
format_requirement = """
|
324 |
Please answer with the code directly. Do not include any additional information in the output.
|
325 |
Please answer with the complete code snippet (including the unmodified part) that replaces the original code. Do not answer with a diff.
|
|
|
438 |
env = Env(issue_id, basemodel_cutoff)
|
439 |
bug_funcs = env.get_hint_bug_functions()
|
440 |
if len(bug_funcs) != 1 or len(next(iter(bug_funcs.values()))) != 1:
|
441 |
+
print("Multi-func bug is not supported")
|
442 |
+
return
|
443 |
messages = []
|
444 |
full_messages = [] # Log with COT tokens
|
445 |
append_message(
|
|
|
489 |
override = True
|
490 |
|
491 |
for task in task_list:
|
|
|
492 |
try:
|
493 |
fix_issue(task)
|
494 |
except Exception as e:
|
495 |
print(e)
|
496 |
+
exit(-1)
|
scripts/llvm_helper.py
CHANGED
@@ -25,7 +25,8 @@ llvm_dir = os.environ["LAB_LLVM_DIR"]
|
|
25 |
llvm_build_dir = os.environ["LAB_LLVM_BUILD_DIR"]
|
26 |
llvm_alive_tv = os.environ["LAB_LLVM_ALIVE_TV"]
|
27 |
dataset_dir = os.environ["LAB_DATASET_DIR"]
|
28 |
-
|
|
|
29 |
|
30 |
def git_execute(args):
|
31 |
return subprocess.check_output(
|
@@ -130,7 +131,7 @@ def build(max_build_jobs: int):
|
|
130 |
cwd=llvm_build_dir,
|
131 |
).decode()
|
132 |
log += subprocess.check_output(
|
133 |
-
["cmake", "--build", ".", "-j", str(max_build_jobs)],
|
134 |
stderr=subprocess.STDOUT,
|
135 |
cwd=llvm_build_dir,
|
136 |
).decode()
|
|
|
25 |
llvm_build_dir = os.environ["LAB_LLVM_BUILD_DIR"]
|
26 |
llvm_alive_tv = os.environ["LAB_LLVM_ALIVE_TV"]
|
27 |
dataset_dir = os.environ["LAB_DATASET_DIR"]
|
28 |
+
if '--quiet' not in subprocess.run(["ninja", "--help"], capture_output=True).stderr.decode("utf-8"):
|
29 |
+
raise RuntimeError("Please update ninja to version 1.11.0 or later")
|
30 |
|
31 |
def git_execute(args):
|
32 |
return subprocess.check_output(
|
|
|
131 |
cwd=llvm_build_dir,
|
132 |
).decode()
|
133 |
log += subprocess.check_output(
|
134 |
+
["cmake", "--build", ".", "-j", str(max_build_jobs), "--", "--quiet"],
|
135 |
stderr=subprocess.STDOUT,
|
136 |
cwd=llvm_build_dir,
|
137 |
).decode()
|