Skip to content

Commit

Permalink
[VPlan] Move trunc ([s|z]ext A) simplifications to simplifyRecipe.
Browse files Browse the repository at this point in the history
Split off simplification from D149903 as suggested.

This should be effectively NFC until D149903 lands.
  • Loading branch information
fhahn committed Nov 16, 2023
1 parent b2d62c9 commit 6f3b88b
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,28 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
break;
}
case Instruction::Trunc: {
VPRecipeBase *Zext = R.getOperand(0)->getDefiningRecipe();
if (!Zext || getOpcodeForRecipe(*Zext) != Instruction::ZExt)
VPRecipeBase *Ext = R.getOperand(0)->getDefiningRecipe();
if (!Ext)
break;
VPValue *A = Zext->getOperand(0);
unsigned ExtOpcode = getOpcodeForRecipe(*Ext);
if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
break;
VPValue *A = Ext->getOperand(0);
VPValue *Trunc = R.getVPSingleValue();
Type *TruncToTy = TypeInfo.inferScalarType(Trunc);
if (TruncToTy && TruncToTy == TypeInfo.inferScalarType(A))
Type *TruncTy = TypeInfo.inferScalarType(Trunc);
Type *ATy = TypeInfo.inferScalarType(A);
if (TruncTy == ATy) {
Trunc->replaceAllUsesWith(A);

} else if (ATy->getScalarSizeInBits() < TruncTy->getScalarSizeInBits()) {
auto *VPC =
new VPWidenCastRecipe(Instruction::CastOps(ExtOpcode), A, TruncTy);
VPC->insertBefore(&R);
Trunc->replaceAllUsesWith(VPC);
} else if (ATy->getScalarSizeInBits() > TruncTy->getScalarSizeInBits()) {
auto *VPC = new VPWidenCastRecipe(Instruction::Trunc, A, TruncTy);
VPC->insertBefore(&R);
Trunc->replaceAllUsesWith(VPC);
}
#ifndef NDEBUG
// Verify that the cached type info is for both A and its users is still
// accurate by comparing it to freshly computed types.
Expand Down

3 comments on commit 6f3b88b

@mikaelholmen
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fhahn

The following starts crashing with this commit:

opt -passes="loop-vectorize" bbi-91916_2.ll -o /dev/null

It fails with

Unhandled opcode!
UNREACHABLE executed at ../lib/Transforms/Vectorize/VPlanAnalysis.cpp:54!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: ../../main-github/llvm/build-all/bin/opt -passes=loop-vectorize bbi-91916_2.ll -o /dev/null
 #0 0x0000563c45042217 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (../../main-github/llvm/build-all/bin/opt+0x3d7e217)
 #1 0x0000563c4503fcee llvm::sys::RunSignalHandlers() (../../main-github/llvm/build-all/bin/opt+0x3d7bcee)
 #2 0x0000563c45042bdf SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f5975f6d630 __restore_rt sigaction.c:0:0
 #4 0x00007f59736b4387 raise (/lib64/libc.so.6+0x36387)
 #5 0x00007f59736b5a78 abort (/lib64/libc.so.6+0x37a78)
 #6 0x0000563c4502bc0f (../../main-github/llvm/build-all/bin/opt+0x3d67c0f)
 #7 0x0000563c4648318f llvm::VPTypeAnalysis::inferScalarTypeForRecipe(llvm::VPInstruction const*) (../../main-github/llvm/build-all/bin/opt+0x51bf18f)
 #8 0x0000563c46482c76 llvm::VPTypeAnalysis::inferScalarType(llvm::VPValue const*) (../../main-github/llvm/build-all/bin/opt+0x51bec76)
 #9 0x0000563c46497c74 llvm::VPlanTransforms::optimize(llvm::VPlan&, llvm::ScalarEvolution&) (../../main-github/llvm/build-all/bin/opt+0x51d3c74)
#10 0x0000563c4641bdfe llvm::LoopVectorizationPlanner::buildVPlansWithVPRecipes(llvm::ElementCount, llvm::ElementCount) (../../main-github/llvm/build-all/bin/opt+0x5157dfe)
#11 0x0000563c4641b44c llvm::LoopVectorizationPlanner::plan(llvm::ElementCount, unsigned int) (../../main-github/llvm/build-all/bin/opt+0x515744c)
#12 0x0000563c4642edc1 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (../../main-github/llvm/build-all/bin/opt+0x516adc1)
#13 0x0000563c46434e83 llvm::LoopVectorizePass::runImpl(llvm::Function&, llvm::ScalarEvolution&, llvm::LoopInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::BlockFrequencyInfo*, llvm::TargetLibraryInfo*, llvm::DemandedBits&, llvm::AssumptionCache&, llvm::LoopAccessInfoManager&, llvm::OptimizationRemarkEmitter&, llvm::ProfileSummaryInfo*) (../../main-github/llvm/build-all/bin/opt+0x5170e83)
#14 0x0000563c464357e8 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (../../main-github/llvm/build-all/bin/opt+0x51717e8)
#15 0x0000563c46313aed llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) PassBuilderPipelines.cpp:0:0
#16 0x0000563c4524c2f4 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (../../main-github/llvm/build-all/bin/opt+0x3f882f4)
#17 0x0000563c4630dfdd llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) PassBuilderPipelines.cpp:0:0
#18 0x0000563c452513be llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (../../main-github/llvm/build-all/bin/opt+0x3f8d3be)
#19 0x0000563c46307bad llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) PassBuilderPipelines.cpp:0:0
#20 0x0000563c4524ae66 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (../../main-github/llvm/build-all/bin/opt+0x3f86e66)
#21 0x0000563c462b50ab 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) (../../main-github/llvm/build-all/bin/opt+0x4ff10ab)
#22 0x0000563c4500a758 optMain (../../main-github/llvm/build-all/bin/opt+0x3d46758)
#23 0x00007f59736a0555 __libc_start_main (/lib64/libc.so.6+0x22555)
#24 0x0000563c450040e9 _start (../../main-github/llvm/build-all/bin/opt+0x3d400e9)
Abort (core dumped)

and if I add -debug we also get

LV: Found unhandled opcode for: EMIT vp<%5> = icmp ule ir<%i53.0115>, vp<%2>

bbi-91916_2.ll.gz

@fhahn
Copy link
Contributor Author

@fhahn fhahn commented on 6f3b88b Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks as always @mikaelholmen. Should be fixed in 8cb2de7

@mikaelholmen
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed in 8cb2de7

Yep, thanks!

Please sign in to comment.