{ "bug_id": "109510", "issue_url": "https://github.com/llvm/llvm-project/issues/109510", "bug_type": "crash", "base_commit": "eb6e7e8f89a3d5c1cbc9856774ca00208753fb12", "test_commit": "58e05779b483bb3d9f734bd09f5cb2241431a0b7", "knowledge_cutoff": "2024-09-21T04:01:16Z", "lit_test_dir": [ "llvm/test/Transforms/LoopVectorize" ], "hints": { "fix_commit": "bd8fe9972e3f17776e4e05e69e13ab8271d34132", "components": [ "LoopVectorize" ], "bug_location_lineno": { "llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp": [ [ 1158, 1164 ], [ 1166, 1171 ] ] }, "bug_location_funcname": { "llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp": [ "VPlanTransforms::optimize" ] } }, "patch": "commit bd8fe9972e3f17776e4e05e69e13ab8271d34132\nAuthor: Florian Hahn \nDate: Sat Sep 21 12:45:36 2024 +0100\n\n [VPlan] Mov licm to end of VPlan optimizations.\n \n This moves licm after expanding replicate regions. This fixes a crash\n when trying to hoist a predicated VPReplicateRecipes which later get\n expanded to replicate regions.\n \n Hoisting replicate regions out was not intended (see the discussion and\n at the review and comment on shallow traversal in licm()).\n \n Fixes https://github.com/llvm/llvm-project/issues/109510.\n\ndiff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\nindex 06ac15f52fe1..fd6090affbbf 100644\n--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\n+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp\n@@ -1158,7 +1158,6 @@ void VPlanTransforms::optimize(VPlan &Plan) {\n removeRedundantInductionCasts(Plan);\n \n simplifyRecipes(Plan);\n- licm(Plan);\n legalizeAndOptimizeInductions(Plan);\n removeDeadRecipes(Plan);\n \n@@ -1166,6 +1165,7 @@ void VPlanTransforms::optimize(VPlan &Plan) {\n \n removeRedundantExpandSCEVRecipes(Plan);\n mergeBlocksIntoPredecessors(Plan);\n+ licm(Plan);\n }\n \n // Add a VPActiveLaneMaskPHIRecipe and related recipes to \\p Plan and replace\n", "tests": [ { "file": "llvm/test/Transforms/LoopVectorize/invariant-replicate-region.ll", "commands": [ "opt -p loop-vectorize -force-vector-width=4 -S %s" ], "tests": [ { "test_name": "test_invariant_replicate_region", "test_body": "target datalayout = \"e-m:o-i64:64-i128:128-n32:64-S128-Fn32\"\ntarget triple = \"arm64-apple-macosx14.0.0\"\n\ndefine i32 @test_invariant_replicate_region(i32 %x, i1 %c) {\nentry:\n br label %loop.header\n\nloop.header: ; preds = %loop.latch, %entry\n %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ]\n br i1 %c, label %then, label %loop.latch\n\nthen: ; preds = %loop.header\n %rem.1 = urem i32 10, %x\n br label %loop.latch\n\nloop.latch: ; preds = %then, %loop.header\n %res = phi i32 [ 0, %loop.header ], [ %rem.1, %then ]\n %iv.next = add i32 %iv, 1\n %ec = icmp eq i32 %iv, 99\n br i1 %ec, label %exit, label %loop.header\n\nexit: ; preds = %loop.latch\n ret i32 %res\n}\n" } ] } ], "issue": { "title": "A/F: verifyVPlanIsValid(*Plan) && \"VPlan is invalid\" after a861ed411a", "body": "We have an internal test that recently started to hit an assertion failure which I bisected back to commit a861ed411a359b7cb2b58d642e4a7f2fdcf92057. \r\n\r\nConsider the following code:\r\n```c\r\nint g_78, g_659, func_32_l_1852;\r\nlong g_128;\r\nunsigned func_32_p_33;\r\nvoid func_32() {\r\n int *l_1145 = &g_659;\r\n *l_1145 = 0;\r\n short __trans_tmp_11;\r\n for (; g_78; g_78 += 1) {\r\n func_32_l_1852 = 0;\r\n for (; func_32_l_1852 <= 3; func_32_l_1852 += 1) {\r\n int *l_1863 = &g_659;\r\n __trans_tmp_11 = *l_1863;\r\n if (10 % ((__trans_tmp_11 >= 0) - func_32_p_33))\r\n break;\r\n ++g_128;\r\n *l_1863 = func_32_p_33 <= --g_128;\r\n }\r\n }\r\n}\r\n```\r\n\r\nIf compiled with optimizations and a compiler built from a861ed411a359b7cb2b58d642e4a7f2fdcf92057 you can see it hits an assertion failure:\r\n```\r\n$ ~/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang -c -O2 repro.c\r\nVPIRBasicBlock can only be used as pre-header or a successor of middle-block at the moment!\r\nclang: /home/dyung/src/upstream/llvm_clean_git/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:8594: void llvm::LoopVectorizationPlanner::buildVPlansWithVPRecipes(llvm::ElementCount, llvm::ElementCount): Assertion `verifyVPlanIsValid(*Plan) && \"VPlan is invalid\"' failed.\r\nPLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.\r\nStack dump:\r\n0. Program arguments: /home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang -c -O2 repro.c\r\n1. parser at end of file\r\n2. Optimizer\r\n3. Running pass \"function(float2int,lower-constant-intrinsics,loop(loop-rotate,loop-deletion),loop-distribute,inject-tli-mappings,loop-vectorize,infer-alignment,loop-load-elim,instcombine,simplifycfg,slp-vectorizer,vector-combine,instcombine,loop-unroll,transform-warning,sroa,infer-alignment,instcombine,loop-mssa(licm),alignment-from-assumptions,loop-sink,instsimplify,div-rem-pairs,tailcallelim,simplifycfg)\" on module \"repro.c\"\r\n4. Running pass \"loop-vectorize\" on function \"func_32\"\r\n #0 0x00005572973a049f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x233549f)\r\n #1 0x000055729739dfec llvm::sys::CleanupOnSignal(unsigned long) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2332fec)\r\n #2 0x00005572972f0418 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0\r\n #3 0x00007f5d2a347420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)\r\n #4 0x00007f5d29e1400b raise /build/glibc-LcI20x/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1\r\n #5 0x00007f5d29df3859 abort /build/glibc-LcI20x/glibc-2.31/stdlib/abort.c:81:7\r\n #6 0x00007f5d29df3729 get_sysdep_segment_value /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:509:8\r\n #7 0x00007f5d29df3729 _nl_load_domain /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:970:34\r\n #8 0x00007f5d29e04fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)\r\n #9 0x0000557298e08438 llvm::LoopVectorizationPlanner::buildVPlansWithVPRecipes(llvm::ElementCount, llvm::ElementCount) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x3d9d438)\r\n#10 0x0000557298e0878f llvm::LoopVectorizationPlanner::plan(llvm::ElementCount, unsigned int) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x3d9d78f)\r\n#11 0x0000557298e0a62e llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x3d9f62e)\r\n#12 0x0000557298e0d501 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x3da2501)\r\n#13 0x0000557298e0db73 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x3da2b73)\r\n#14 0x0000557298836146 llvm::detail::PassModel>::run(llvm::Function&, llvm::AnalysisManager&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x37cb146)\r\n#15 0x0000557296e5bd41 llvm::PassManager>::run(llvm::Function&, llvm::AnalysisManager&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x1df0d41)\r\n#16 0x0000557295f84cc6 llvm::detail::PassModel>, llvm::AnalysisManager>::run(llvm::Function&, llvm::AnalysisManager&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0xf19cc6)\r\n#17 0x0000557296e5a6ad llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x1def6ad)\r\n#18 0x0000557295f85686 llvm::detail::PassModel>::run(llvm::Module&, llvm::AnalysisManager&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0xf1a686)\r\n#19 0x0000557296e5a0e1 llvm::PassManager>::run(llvm::Module&, llvm::AnalysisManager&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x1def0e1)\r\n#20 0x0000557297636863 (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::unique_ptr>&, std::unique_ptr>&, clang::BackendConsumer*) BackendUtil.cpp:0:0\r\n#21 0x0000557297639dbd clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr, std::unique_ptr>, clang::BackendConsumer*) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x25cedbd)\r\n#22 0x0000557297d17c5c clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2cacc5c)\r\n#23 0x0000557299c7aa8c clang::ParseAST(clang::Sema&, bool, bool) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x4c0fa8c)\r\n#24 0x0000557297d18078 clang::CodeGenAction::ExecuteAction() (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2cad078)\r\n#25 0x0000557297fdf129 clang::FrontendAction::Execute() (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2f74129)\r\n#26 0x0000557297f5b6fe clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2ef06fe)\r\n#27 0x00005572980cba26 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x3060a26)\r\n#28 0x0000557295f09185 cc1_main(llvm::ArrayRef, char const*, void*) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0xe9e185)\r\n#29 0x0000557295f00f0a ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&) driver.cpp:0:0\r\n#30 0x0000557297d5e0cd void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0\r\n#31 0x00005572972f0920 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2285920)\r\n#32 0x0000557297d5e6ef clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0\r\n#33 0x0000557297d22274 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2cb7274)\r\n#34 0x0000557297d2338d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2cb838d)\r\n#35 0x0000557297d2b4a5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0x2cc04a5)\r\n#36 0x0000557295f0611b clang_main(int, char**, llvm::ToolContext const&) (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0xe9b11b)\r\n#37 0x0000557295e3609b main (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0xdcb09b)\r\n#38 0x00007f5d29df5083 __libc_start_main /build/glibc-LcI20x/glibc-2.31/csu/../csu/libc-start.c:342:3\r\n#39 0x0000557295f0099e _start (/home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin/clang+0xe9599e)\r\nclang: error: clang frontend command failed with exit code 134 (use -v to see invocation)\r\nclang version 20.0.0 (https://github.com/llvm/llvm-project.git a861ed411a359b7cb2b58d642e4a7f2fdcf92057)\r\nTarget: x86_64-unknown-linux-gnu\r\nThread model: posix\r\nInstalledDir: /home/dyung/src/upstream/a861ed411a359b7cb2b58d642e4a7f2fdcf92057-linux/bin\r\nBuild config: +assertions\r\n```\r\n\r\nBuilding with a compiler built from the commit that immediately preceded this one (37e5319a12ba47c18049728804d3d1e1b10c4eb4), you can see that the compilation succeeds.\r\n```\r\n$ ~/src/upstream/37e5319a12ba47c18049728804d3d1e1b10c4eb4-linux/bin/clang -c -O2 repro.c\r\n$\r\n```", "author": "dyung", "labels": [ "vectorizers", "crash" ], "comments": [] }, "verified": true }