{ "bug_id": "108004", "issue_url": "https://github.com/llvm/llvm-project/issues/108004", "bug_type": "crash", "base_commit": "512cecad4c384c84b79fea050a755cb7e46c6ac5", "knowledge_cutoff": "2024-09-10T11:32:26Z", "lit_test_dir": [ "llvm/test/Transforms/LoopVectorize" ], "hints": { "fix_commit": "7858e14547c509c95503b74ff8ffc7bf2fc5b110", "components": [ "LoopVectorize" ], "bug_location_lineno": { "llvm/lib/Transforms/Vectorize/LoopVectorize.cpp": [ [ 8663, 8668 ] ] }, "bug_location_funcname": { "llvm/lib/Transforms/Vectorize/LoopVectorize.cpp": [ "collectUsersInExitBlock" ] } }, "patch": "commit 7858e14547c509c95503b74ff8ffc7bf2fc5b110\nAuthor: Hari Limaye \nDate: Wed Sep 11 16:43:34 2024 +0100\n\n [LV] Amend check for IV increments in collectUsersInEntryBlock (#108020)\n \n The check for IV increments in collectUsersInEntryBlock currently\n triggers for exit-block PHIs which use the IV start value, resulting in\n us failing to add the input value for the middle block to these PHIs.\n \n Fix this by amending the check for IV increments to only include\n incoming values that are instructions inside the loop.\n \n Fixes #108004\n\ndiff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\nindex b821da03c16e..640a7bf3d672 100644\n--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\n+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp\n@@ -8663,6 +8663,7 @@ static MapVector collectUsersInExitBlock(\n !cast(V)->getTruncInst()) ||\n isa(V) ||\n (isa(IncomingValue) &&\n+ OrigLoop->contains(cast(IncomingValue)) &&\n any_of(IncomingValue->users(), [&Inductions](User *U) {\n auto *P = dyn_cast(U);\n return P && Inductions.contains(P);\n", "tests": [ { "file": "llvm/test/Transforms/LoopVectorize/use-iv-start-value.ll", "commands": [ "opt < %s -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S" ], "tests": [ { "test_name": "foo", "test_body": "define i64 @foo(ptr %p1, ptr %p2, i64 %start, i64 %end) {\nentry:\n %start2 = and i64 %start, 12345\n br label %for.body\n\nfor.body: ; preds = %for.body, %entry\n %ind = phi i64 [ %start2, %entry ], [ %ind.next, %for.body ]\n %arrayidx1 = getelementptr inbounds i32, ptr %p1, i64 %ind\n %0 = load i32, ptr %arrayidx1, align 4\n %arrayidx2 = getelementptr inbounds i32, ptr %p2, i64 %ind\n %1 = load i32, ptr %arrayidx2, align 4\n %ind.next = add i64 %ind, 1\n %cmp = icmp ne i64 %ind.next, %end\n br i1 %cmp, label %for.body, label %exit\n\nexit: ; preds = %for.body\n %use = phi i64 [ %start2, %for.body ]\n ret i64 %use\n}\n" } ] } ], "issue": { "title": "[LoopVectorize] Assertion fails when IV start value is used by an exit block PHI", "body": "With the following IR:\r\n```\r\ndefine i64 @foo(i64 %start, i64 %end) {\r\nentry:\r\n %p1 = alloca [1024 x i32]\r\n %p2 = alloca [1024 x i32]\r\n call void @init_mem(ptr %p1, i64 1024)\r\n call void @init_mem(ptr %p2, i64 1024)\r\n %start2 = and i64 %start, 12345\r\n br label %for.body\r\n\r\nfor.body:\r\n %ind = phi i64 [ %ind.next, %for.body ], [ %start2, %entry ]\r\n %arrayidx1 = getelementptr inbounds i32, ptr %p1, i64 %ind\r\n %0 = load i32, ptr %arrayidx1, align 4\r\n %arrayidx2 = getelementptr inbounds i32, ptr %p2, i64 %ind\r\n %1 = load i32, ptr %arrayidx2, align 4\r\n %ind.next = add i64 %ind, 1\r\n %cmp = icmp ne i64 %ind.next, %end\r\n br i1 %cmp, label %for.body, label %exit\r\n\r\nexit:\r\n %use = phi i64 [ %start2, %for.body ]\r\n ret i64 %use\r\n}\r\n\r\ndeclare void @init_mem(ptr, i64)\r\n```\r\n\r\nLV crashes due to hitting the following assertion:\r\n```\r\nLV: Vectorizing: innermost loop.\r\nPHINode should have one entry for each predecessor of its parent basic block!\r\n %bob = phi i64 [ %start2, %for.body ]\r\nopt: /work/llvm-project-wt/nuw-clang-arr/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:10244: bool llvm::LoopVectorizePass::processLoop(Loop *): Assertion `!verif\r\nyFunction(*L->getHeader()->getParent(), &dbgs())' failed.\r\nPLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.\r\nStack dump:\r\n0. Program arguments: ./build/bin/opt -passes=loop-vectorize -force-vector-interleave=2 -force-vector-width=4 -debug test.ll -o - -S\r\n1. Running pass \"function(loop-vectorize)\" on module \"test.ll\"\r\n2. Running pass \"loop-vectorize\" on function \"foo\"\r\n #0 0x0000aaaaaec431c0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (./build/bin/opt+0x41a31c0)\r\n #1 0x0000aaaaaec4102c llvm::sys::RunSignalHandlers() (./build/bin/opt+0x41a102c)\r\n #2 0x0000aaaaaec438dc SignalHandler(int) Signals.cpp:0:0\r\n #3 0x0000fffff7ffb9d0 (linux-vdso.so.1+0x9d0)\r\n #4 0x0000fffff7b5f200 __pthread_kill_implementation ./nptl/pthread_kill.c:44:76\r\n #5 0x0000fffff7b1a67c gsignal ./signal/../sysdeps/posix/raise.c:27:6\r\n #6 0x0000fffff7b07130 abort ./stdlib/abort.c:81:7\r\n #7 0x0000fffff7b13fd0 __assert_fail_base ./assert/assert.c:89:7\r\n #8 0x0000fffff7b14040 __assert_perror_fail ./assert/assert-perr.c:31:1\r\n #9 0x0000aaaaadee375c llvm::LoopVectorizePass::processLoop(llvm::Loop*) (./build/bin/opt+0x344375c)\r\n#10 0x0000aaaaadee72d0 llvm::LoopVectorizePass::runImpl(llvm::Function&) (./build/bin/opt+0x34472d0)\r\n#11 0x0000aaaaadee7ac0 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager&) (./build/bin/opt+0x3447ac0)\r\n#12 0x0000aaaaaea9676c llvm::PassManager>::run(llvm::Function&, llvm::AnalysisManager&) (./bui\r\nld/bin/opt+0x3ff676c)\r\n#13 0x0000aaaaaea9a1fc llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager&) (./build/bin/opt+0x3ffa1fc)\r\n#14 0x0000aaaaaea958cc llvm::PassManager>::run(llvm::Module&, llvm::AnalysisManager&) (./build/bin/o\r\npt+0x3ff58cc)\r\n#15 0x0000aaaaab5b3d88 llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOu\r\ntputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef, llvm::ArrayRef>, llvm::opt_tool::Outpu\r\ntKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (./build/bin/opt+0xb13d88)\r\n#16 0x0000aaaaab5a7fc0 optMain (./build/bin/opt+0xb07fc0)\r\n#17 0x0000fffff7b073fc __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3\r\n#18 0x0000fffff7b074cc call_init ./csu/../csu/libc-start.c:128:20\r\n#19 0x0000fffff7b074cc __libc_start_main ./csu/../csu/libc-start.c:379:5\r\n#20 0x0000aaaaab5a1ef0 _start (./build/bin/opt+0xb01ef0)\r\n```\r\n\r\nSeems like we fail to properly handle fixing up the incoming value for this PHI from the middle block. ", "author": "hazzlim", "labels": [ "vectorizers", "crash" ], "comments": [ { "author": "hazzlim", "body": "Note: I am actively looking into this and plan to put up a fix " } ] }, "verified": true }