[llvm] [InstCombine] Fold `sext(A < B) + zext(A > B)` into `ucmp/scmp(A, B)` (PR #103833)

Volodymyr Vasylkun via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 19 05:42:10 PDT 2024


Poseydon42 wrote:

> ```
> ; bin/opt -passes=instcombine reduced.ll -S
> define i32 @page_request_addr_cmp(ptr %ap, ptr %bp) {
> entry:
>   %cmp = icmp ugt ptr %ap, %bp
>   %conv = zext i1 %cmp to i32
>   %cmp1 = icmp ult ptr %ap, %bp
>   %conv2.neg = sext i1 %cmp1 to i32
>   %sub = add i32 %conv2.neg, %conv
>   ret i32 %sub
> }
> ```
> 
> ```
> opt: /home/dtcxzyw/WorkSpace/Projects/compilers/llvm-project/llvm/lib/IR/IRBuilder.cpp:962: llvm::CallInst* llvm::IRBuilderBase::CreateIntrinsic(llvm::Type*, llvm::Intrinsic::ID, llvm::ArrayRef<llvm::Value*>, llvm::Instruction*, const llvm::Twine&): Assertion `Res == Intrinsic::MatchIntrinsicTypes_Match && TableRef.empty() && "Wrong types for intrinsic!"' failed.
> PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
> Stack dump:
> 0.      Program arguments: bin/opt -passes=instcombine reduced.ll -S
> 1.      Running pass "function(instcombine<max-iterations=1;no-use-loop-info;verify-fixpoint>)" on module "reduced.ll"
> 2.      Running pass "instcombine<max-iterations=1;no-use-loop-info;verify-fixpoint>" on function "page_request_addr_cmp"
>  #0 0x000079f86320f312 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMSupport.so.20.0git+0x20f312)
>  #1 0x000079f86320c1df llvm::sys::RunSignalHandlers() (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMSupport.so.20.0git+0x20c1df)
>  #2 0x000079f86320c325 SignalHandler(int) Signals.cpp:0:0
>  #3 0x000079f862c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
>  #4 0x000079f862c969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
>  #5 0x000079f862c969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
>  #6 0x000079f862c969fc pthread_kill ./nptl/pthread_kill.c:89:10
>  #7 0x000079f862c42476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
>  #8 0x000079f862c287f3 abort ./stdlib/abort.c:81:7
>  #9 0x000079f862c2871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
> #10 0x000079f862c39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
> #11 0x000079f85b8989a9 llvm::IRBuilderBase::CreateIntrinsic(llvm::Type*, unsigned int, llvm::ArrayRef<llvm::Value*>, llvm::Instruction*, llvm::Twine const&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x2989a9)
> #12 0x000079f85ca961be llvm::InstCombinerImpl::visitAdd(llvm::BinaryOperator&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMInstCombine.so.20.0git+0x7e1be)
> #13 0x000079f85ca79a5d llvm::InstCombinerImpl::run() (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMInstCombine.so.20.0git+0x61a5d)
> #14 0x000079f85ca7aa7c combineInstructionsOverFunction(llvm::Function&, llvm::InstructionWorklist&, llvm::AAResults*, llvm::AssumptionCache&, llvm::TargetLibraryInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::OptimizationRemarkEmitter&, llvm::BlockFrequencyInfo*, llvm::BranchProbabilityInfo*, llvm::ProfileSummaryInfo*, llvm::LoopInfo*, llvm::InstCombineOptions const&) InstructionCombining.cpp:0:0
> #15 0x000079f85ca7bb02 llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMInstCombine.so.20.0git+0x63b02)
> #16 0x000079f85e8d34d5 llvm::detail::PassModel<llvm::Function, llvm::InstCombinePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMPasses.so.20.0git+0xd34d5)
> #17 0x000079f85b953bdd llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x353bdd)
> #18 0x000079f861ed7265 llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMX86CodeGen.so.20.0git+0xd7265)
> #19 0x000079f85b952726 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x352726)
> #20 0x000079f861ed7c25 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMX86CodeGen.so.20.0git+0xd7c25)
> #21 0x000079f85b95073a llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x35073a)
> #22 0x000079f8634c7fb7 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) (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMOptDriver.so.20.0git+0x2cfb7)
> #23 0x000079f8634d3952 optMain (/home/dtcxzyw/WorkSpace/Projects/compilers/LLVM/llvm-build/bin/../lib/libLLVMOptDriver.so.20.0git+0x38952)
> #24 0x000079f862c29d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
> #25 0x000079f862c29e40 call_init ./csu/../csu/libc-start.c:128:20
> #26 0x000079f862c29e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
> #27 0x0000652e48dc9095 _start (bin/opt+0x1095)
> Aborted (core dumped)
> ```
> 
> @nikic We should support pointer types first.

I added a check to prevent this crash. I will work on supporting pointer types in the near future, but I think for now it's worth finishing this PR and then removing the check later.

https://github.com/llvm/llvm-project/pull/103833


More information about the llvm-commits mailing list