[llvm] [InstCombine] Canonicalize signed saturated additions (PR #153053)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 30 18:59:00 PST 2025


dtcxzyw wrote:

> @AZero13 @dtcxzyw we're seeing Clang assertion failures after this commit. A reduced test case (https://gcc.godbolt.org/z/Pj59avcTh):
> 
> ```
> target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
> target triple = "x86_64-unknown-linux-gnu"
> 
> define i32 @f(i32 %call77, i32 %call61) {
> entry:
>   %cmp78 = icmp slt i32 %call61, %call77
>   %add = add i32 %call61, 1
>   %cond82 = select i1 %cmp78, i32 %add, i32 2147483647
>   ret i32 %cond82
> }
> ```
> 
> ```
> clang++: /root/llvm-project/llvm/include/llvm/Support/Casting.h:572: decltype(auto) llvm::cast(From*) [with To = llvm::Constant; From = llvm::Value]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
> PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
> Stack dump:
> 0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fverbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -xir -O1 <source>
> 1.	Optimizer
> 2.	Running pass "function<eager-inv>(mem2reg,instcombine<max-iterations=1;no-verify-fixpoint>,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-arithmetic;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>)" on module "<source>"
> 3.	Running pass "instcombine<max-iterations=1;no-verify-fixpoint>" on function "f"
>  #0 0x0000000004279728 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4279728)
>  #1 0x0000000004276b54 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4276b54)
>  #2 0x00000000041ba888 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
>  #3 0x0000731569842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
>  #4 0x00007315698969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
>  #5 0x0000731569842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
>  #6 0x00007315698287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
>  #7 0x000073156982871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
>  #8 0x0000731569839e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
>  #9 0x00000000030b6ffa (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x30b6ffa)
> #10 0x0000000003e9302f canonicalizeSaturatedAddSigned(llvm::ICmpInst*, llvm::Value*, llvm::Value*, llvm::IRBuilder<llvm::TargetFolder, llvm::IRBuilderCallbackInserter>&) InstCombineSelect.cpp:0:0
> #11 0x0000000003e931d9 canonicalizeSaturatedAdd(llvm::ICmpInst*, llvm::Value*, llvm::Value*, llvm::IRBuilder<llvm::TargetFolder, llvm::IRBuilderCallbackInserter>&) InstCombineSelect.cpp:0:0
> #12 0x0000000003e9f838 llvm::InstCombinerImpl::foldSelectInstWithICmp(llvm::SelectInst&, llvm::ICmpInst*) (.part.0) InstCombineSelect.cpp:0:0
> #13 0x0000000003ea3ae9 llvm::InstCombinerImpl::visitSelectInst(llvm::SelectInst&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3ea3ae9)
> #14 0x0000000003da2f63 llvm::InstCombinerImpl::run() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3da2f63)
> #15 0x0000000003da4e56 combineInstructionsOverFunction(llvm::Function&, llvm::InstructionWorklist&, llvm::AAResults*, llvm::AssumptionCache&, llvm::TargetLibraryInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::OptimizationRemarkEmitter&, llvm::BlockFrequencyInfo*, llvm::BranchProbabilityInfo*, llvm::ProfileSummaryInfo*, llvm::InstCombineOptions const&) (.isra.0) InstructionCombining.cpp:0:0
> #16 0x0000000003da63c4 llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3da63c4)
> #17 0x000000000452605e llvm::detail::PassModel<llvm::Function, llvm::InstCombinePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452605e)
> #18 0x0000000003bcba31 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bcba31)
> #19 0x000000000126188e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x126188e)
> #20 0x0000000003bca0fa llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bca0fa)
> #21 0x0000000001261b7e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1261b7e)
> #22 0x0000000003bc9ab1 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bc9ab1)
> #23 0x0000000004534203 (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>&, std::unique_ptr<llvm::ToolOutputFile, std::default_delete<llvm::ToolOutputFile>>&, clang::BackendConsumer*) BackendUtil.cpp:0:0
> #24 0x00000000045378d9 clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45378d9)
> #25 0x0000000004bc947f clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bc947f)
> #26 0x0000000004eb4455 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4eb4455)
> #27 0x0000000004e3460e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4e3460e)
> #28 0x0000000004fad48d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4fad48d)
> #29 0x0000000000dc7780 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdc7780)
> #30 0x0000000000dbe1fa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
> #31 0x0000000000dbe37d int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)>(long, llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
> #32 0x0000000004c313d9 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
> #33 0x00000000041bad24 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x41bad24)
> #34 0x0000000004c319ef clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
> #35 0x0000000004bf21e2 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bf21e2)
> #36 0x0000000004bf318e clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bf318e)
> #37 0x0000000004bfa5c5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bfa5c5)
> #38 0x0000000000dc3ba1 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdc3ba1)
> #39 0x0000000000c72574 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc72574)
> #40 0x0000731569829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
> #41 0x0000731569829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
> #42 0x0000000000dbdc95 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdbdc95)
> clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
> ```

I will post a fix later.

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


More information about the llvm-commits mailing list