This is an archive of the discontinued LLVM Phabricator instance.

[SLP]Fix vectorization of the alternate cmp instruction with swapped predicates.
ClosedPublic

Authored by ABataev on Feb 15 2022, 9:07 AM.

Details

Summary

If the alternate cmp instruction is a swapped predicate of the main cmp
instruction, need to generate alternate instruction, not the one with
the swapped predicate. Also, the lane with the alternate opcode should
be selected only, if the corresponding operands are not compatible.

Correctness confirmed:
https://alive2.llvm.org/ce/z/94BG66

Diff Detail

Event Timeline

ABataev created this revision.Feb 15 2022, 9:07 AM
ABataev requested review of this revision.Feb 15 2022, 9:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2022, 9:07 AM

I've lost track - did any of the cmp slp work end up in 14.x?

I've lost track - did any of the cmp slp work end up in 14.x?

No, all reverted.

bjope added a subscriber: bjope.Feb 16 2022, 3:21 AM
anton-afanasyev accepted this revision.Feb 16 2022, 10:57 AM

LGTM

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
5585

May be not for this quick fix, but I would make a static bool IsAltOp() to prevent copy/paste from getEntryCost() to vectorize(), or even inlined it to buildShuffleEntryMask() itself, since it isn't used anywhere.
Also, typo: buildSuffleEntryMask() -> buildShuffleEntryMask().

This revision is now accepted and ready to land.Feb 16 2022, 10:57 AM
ABataev added inline comments.Feb 16 2022, 10:59 AM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
5585

Yes, was going to do it.

I can confirm that this patch fixes the miscompile I saw in
https://reviews.llvm.org/D115955#3322857

bgraur added a subscriber: bgraur.Feb 17 2022, 1:55 AM

I was trying to confirm that this patch fixes a mis-compile and it seems clang crashes with this patched in (does not crash without the patch).

I'm working on a reproducer, but I'd advise not to submit this yet.

bgraur added a comment.EditedFeb 17 2022, 5:14 AM

Here's a stack of the crash:

 #0 0x00005602c046a36a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (work/bin/clang+0x706a36a)
 #1 0x00005602c04683f8 llvm::sys::RunSignalHandlers() (work/bin/clang+0x70683f8)
 #2 0x00005602c046aa0c SignalHandler(int) (work/bin/clang+0x706aa0c)
 #3 0x00007f0f0a086750 __restore_rt (/usr/grte/v5/lib64/libpthread.so.0+0x15750)
 #4 0x00005602bfd6ad0c llvm::propagateIRFlags(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Value*) (work/bin/clang+0x696ad0c)
 #5 0x00005602bfc79609 llvm::slpvectorizer::BoUpSLP::vectorizeTree(llvm::slpvectorizer::BoUpSLP::TreeEntry*) (work/bin/clang+0x6879609)
 #6 0x00005602bfc766c1 llvm::slpvectorizer::BoUpSLP::vectorizeTree(llvm::ArrayRef<llvm::Value*>) (work/bin/clang+0x68766c1)
 #7 0x00005602bfc77f3e llvm::slpvectorizer::BoUpSLP::vectorizeTree(llvm::slpvectorizer::BoUpSLP::TreeEntry*) (work/bin/clang+0x6877f3e)
 #8 0x00005602bfc766c1 llvm::slpvectorizer::BoUpSLP::vectorizeTree(llvm::ArrayRef<llvm::Value*>) (work/bin/clang+0x68766c1)
 #9 0x00005602bfc77663 llvm::slpvectorizer::BoUpSLP::vectorizeTree(llvm::slpvectorizer::BoUpSLP::TreeEntry*) (work/bin/clang+0x6877663)
#10 0x00005602bfc7a09e llvm::slpvectorizer::BoUpSLP::vectorizeTree(llvm::MapVector<llvm::Value*, llvm::SmallVector<llvm::Instruction*, 2u>, llvm::DenseMap<llvm::Value*, unsigned int, llvm::DenseMapInfo<llvm::Value*, void>, llvm
::detail::DenseMapPair<llvm::Value*, unsigned int> >, std::__u::vector<std::__u::pair<llvm::Value*, llvm::SmallVector<llvm::Instruction*, 2u> >, std::__u::allocator<std::__u::pair<llvm::Value*, llvm::SmallVector<llvm::Instructi
on*, 2u> > > > >&) (work/bin/clang+0x687a09e)
#11 0x00005602bfc848df llvm::SLPVectorizerPass::tryToVectorizeList(llvm::ArrayRef<llvm::Value*>, llvm::slpvectorizer::BoUpSLP&, bool) (work/bin/clang+0x68848df)
#12 0x00005602bfc85d88 llvm::SLPVectorizerPass::vectorizeInsertValueInst(llvm::InsertValueInst*, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (work/bin/clang+0x6885d88)
#13 0x00005602bfc862e9 llvm::SLPVectorizerPass::vectorizeSimpleInstructions(llvm::SmallVectorImpl<llvm::Instruction*>&, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&, bool) (work/bin/clang+0x68862e9)
#14 0x00005602bfc81495 llvm::SLPVectorizerPass::vectorizeChainsInBlock(llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (work/bin/clang+0x6881495)
#15 0x00005602bfc7fae1 llvm::SLPVectorizerPass::runImpl(llvm::Function&, llvm::ScalarEvolution*, llvm::TargetTransformInfo*, llvm::TargetLibraryInfo*, llvm::AAResults*, llvm::LoopInfo*, llvm::DominatorTree*, llvm::AssumptionCache*, llvm::DemandedBits*, llvm::OptimizationRemarkEmitter*) (work/bin/clang+0x687fae1)
#16 0x00005602bfc7f55e llvm::SLPVectorizerPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (work/bin/clang+0x687f55e)
#17 0x00005602beeb3b32 llvm::detail::PassModel<llvm::Function, llvm::SLPVectorizerPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (work/bin/clang+0x5ab3b32)
#18 0x00005602c02c8b35 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (work/bin/clang+0x6ec8b35)
#19 0x00005602bc5a8d32 llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function> >, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function> >::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (work/bin/clang+0x31a8d32)
#20 0x00005602c02caf11 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (work/bin/clang+0x6ecaf11)
#21 0x00005602bc5aaa32 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (work/bin/clang+0x31aaa32)
#22 0x00005602c02c7f3f llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module> >::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (work/bin/clang+0x6ec7f3f)
#23 0x00005602bc5a3b3c (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::__u::unique_ptr<llvm::raw_pwrite_stream, std::__u::default_delete<llvm::raw_pwrite_stream> >&, std::__u::unique_ptr<llvm::ToolOutputFile, std::__u::default_delete<llvm::ToolOutputFile> >&) (work/bin/clang+0x31a3b3c)
#24 0x00005602bc5996f6 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::__u::unique_ptr<llvm::raw_pwrite_stream, std::__u::default_delete<llvm::raw_pwrite_stream> >) (work/bin/clang+0x31996f6)
#25 0x00005602bc596aa5 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (work/bin/clang+0x3196aa5)
#26 0x00005602bd1bb794 clang::ParseAST(clang::Sema&, bool, bool) (work/bin/clang+0x3dbb794)
#27 0x00005602bcf640fa clang::FrontendAction::Execute() (work/bin/clang+0x3b640fa)
#28 0x00005602bcebb516 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (work/bin/clang+0x3abb516)
#29 0x00005602bc262f46 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (work/bin/clang+0x2e62f46)
#30 0x00005602bc257a69 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (work/bin/clang+0x2e57a69)
#31 0x00005602bc255e04 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) (work/bin/clang+0x2e55e04)
#32 0x00005602bc255beb main (work/bin/clang+0x2e55beb)
#33 0x00007f0f09f148d3 __libc_start_main (work/lib64/libc.so.6+0x628d3)
#34 0x00005602bc2527ea _start (work/bin/clang+0x2e527ea)

I was trying to confirm that this patch fixes a mis-compile and it seems clang crashes with this patched in (does not crash without the patch).

I'm working on a reproducer, but I'd advise not to submit this yet.

I'll wait for the reproducer.

Please find attached the reproducer for the crash.

The compile command line is:

clang -cc1 \
  -emit-obj \
  -O3 \
  -std=gnu++17 \
  -fsized-deallocation \
  -vectorize-slp \
  -o /tmp/rect.o \
  -x c++ rect.ii

rect.ii file:

Since we had so many issues related to the initial patch (https://reviews.llvm.org/D115955), would you please consider reverting that and work on the fix without the stress of adding new issues?

Since we had so many issues related to the initial patch (https://reviews.llvm.org/D115955), would you please consider reverting that and work on the fix without the stress of adding new issues?

The problems appear in a single corner case - when the main and alternate opcode has swapped predicates. I'll try to fix it ASAP (this case is pretty rare and tricky, did not see any problems with SPEC 2006/2017), if won't be able to fix it soon, will revert the part for swapped alternate vectorization.

ABataev updated this revision to Diff 409714.Feb 17 2022, 10:32 AM

Added explicit check for main/alternate instructions in alternate op selection function + outlined common functionality.

I can confirm the clang crash is fixed with the updated patch.
Thanks for fixing Alexey!

This revision was landed with ongoing or failed builds.Feb 18 2022, 4:28 AM
This revision was automatically updated to reflect the committed changes.