llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[VectorCombine] Assertion `is_contained(ToFreeze->users(), &UserI) && "UserI must be a user of ToFreeze"' failed.

Open XChy opened this issue 4 months ago • 1 comments

Reproducer: https://godbolt.org/z/jEca9MWvf Testcase:

#include <stdint.h>
typedef uint16_t v2u16 __attribute__((vector_size(4)));
static uint32_t g2 = 0U;
static uint32_t g9 = 3683323801U;
static v2u16 g17;
static uint16_t g20 = 48141;
static uint16_t g27 = 23163;
void f22(v2u16 a4)
{
    uint32_t bb7 = __builtin_parity(__builtin_popcount(g2));
    g20 = (g17 <= a4)[bb7];
}
void f32(uint8_t a0)
{
    do
    {
        g27 = ~g20;
        g2 = 3561532384U & g9;
        if (a0 == 82) g17 = ~g17;
        g9 = 0;
        f22(g17);
    } while (g27 == -25483LL);
}

Dump at -O1:

clang: /root/llvm-project/llvm/lib/Transforms/Vectorize/VectorCombine.cpp:1883: void {anonymous}::ScalarizationResult::freeze(llvm::IRBuilderBase&, llvm::Instruction&): Assertion `is_contained(ToFreeze->users(), &UserI) && "UserI must be a user of ToFreeze"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -O1 <source>
1.	<eof> parser at end of file
2.	Optimizer
3.	Running pass "function<eager-inv>(drop-unnecessary-assumes,float2int,lower-constant-intrinsics,loop(loop-rotate<header-duplication;no-prepare-for-lto;check-exit-count>,loop-deletion),loop-distribute,inject-tli-mappings,loop-vectorize<interleave-forced-only;vectorize-forced-only;>,drop-unnecessary-assumes<drop-deref>,infer-alignment,loop-load-elim,instcombine<max-iterations=1;no-verify-fixpoint>,simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-arithmetic;switch-to-lookup;no-keep-loops;hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>,vector-combine,instcombine<max-iterations=1;no-verify-fixpoint>,loop-unroll<O1>,transform-warning,sroa<preserve-cfg;aggregate-to-vector>,infer-alignment,instcombine<max-iterations=1;no-verify-fixpoint>,loop-mssa(licm<allowspeculation>),alignment-from-assumptions,loop-sink,instsimplify,div-rem-pairs,mergeicmps,expand-memcmp,tailcallelim,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;switch-to-arithmetic;no-switch-to-lookup;keep-loops;no-hoist-common-insts;hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;speculate-unpredictables>)" on module "<source>"
4.	Running pass "vector-combine" on function "f32"
 #0 0x000000000441d908 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x441d908)
 #1 0x000000000441a7b4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x441a7b4)
 #2 0x000000000441adec llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x441adec)
 #3 0x0000000004359dc8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #4 0x0000713523045330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #5 0x000071352309eb2c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9eb2c)
 #6 0x000071352304527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
 #7 0x00007135230288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
 #8 0x000071352302881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
 #9 0x000071352303b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x00000000062d873f (anonymous namespace)::ScalarizationResult::freeze(llvm::IRBuilderBase&, llvm::Instruction&) VectorCombine.cpp:0:0
#11 0x00000000062e6177 (anonymous namespace)::VectorCombine::scalarizeLoadExtract(llvm::LoadInst*, llvm::VectorType*, llvm::Value*) VectorCombine.cpp:0:0
#12 0x000000000630b256 (anonymous namespace)::VectorCombine::run()::'lambda'(llvm::Instruction&)::operator()(llvm::Instruction&) const (.isra.0) VectorCombine.cpp:0:0
#13 0x000000000630c146 llvm::VectorCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x630c146)
#14 0x00000000057c3ece llvm::detail::PassModel<llvm::Function, llvm::VectorCombinePass, llvm::AnalysisManager<llvm::Function>>::runImpl(llvm::detail::PassConcept<llvm::Function, llvm::AnalysisManager<llvm::Function>>&, llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x57c3ece)
#15 0x0000000003d55ad4 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d55ad4)
#16 0x000000000128c33e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::runImpl(llvm::detail::PassConcept<llvm::Function, llvm::AnalysisManager<llvm::Function>>&, llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x128c33e)
#17 0x0000000003d54533 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d54533)
#18 0x000000000128c3de llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::runImpl(llvm::detail::PassConcept<llvm::Module, llvm::AnalysisManager<llvm::Module>>&, llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x128c3de)
#19 0x0000000003d53b84 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d53b84)
#20 0x0000000004848e2c (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
#21 0x000000000484af93 (anonymous namespace)::EmitAssemblyHelper::emitAssembly(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) BackendUtil.cpp:0:0
#22 0x000000000484dc03 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+0x484dc03)
#23 0x0000000004f2008c clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4f2008c)
#24 0x0000000006d2371d clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x6d2371d)
#25 0x0000000004f204b8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4f204b8)
#26 0x000000000524e328 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x524e328)
#27 0x00000000051daeed clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x51daeed)
#28 0x0000000005322cbd clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5322cbd)
#29 0x0000000000da984c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xda984c)
#30 0x0000000000da046a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#31 0x0000000000da05ed 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 0x0000000004f97f79 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 0x000000000435a204 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x435a204)
#34 0x0000000004f98ae5 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 0x0000000004f50d92 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4f50d92)
#36 0x0000000004f555c7 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+0x4f555c7)
#37 0x0000000004f5fb25 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4f5fb25)
#38 0x0000000000da5d4a clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xda5d4a)
#39 0x0000000000cc1c2a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xcc1c2a)
#40 0x000071352302a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#41 0x000071352302a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#42 0x0000000000d9fe15 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd9fe15)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134

XChy avatar Aug 01 '26 09:08 XChy

Another, smaller IR-level reproducer for this, still failing on 9dce18f0e (2026-11-11), f2808fc79 and 5e457cda8d5a:

define i32 @f(ptr %p, i64 %i) {
  %c = and i64 %i, 15
  %lv = load <16 x i32>, ptr %p, align 64
  %e.0 = extractelement <16 x i32> %lv, i64 %c
  %e.1 = extractelement <16 x i32> %lv, i64 %c
  %res = add i32 %e.0, %e.1
  ret i32 %res
}
$ opt -mtriple=x86_64-unknown-linux-gnu -passes=vector-combine -disable-output test.ll
opt: llvm/lib/Transforms/Vectorize/VectorCombine.cpp:1802: void (anonymous namespace)::ScalarizationResult::freeze(IRBuilderBase &, Instruction &): Assertion `is_contained(ToFreeze->users(), &UserI) && "UserI must be a user of ToFreeze"' failed.
#11 (anonymous namespace)::ScalarizationResult::freeze(llvm::IRBuilderBase&, llvm::Instruction&)
#12 (anonymous namespace)::VectorCombine::run()::$_0::operator()(llvm::Instruction&) const
#13 llvm::VectorCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&)

It also crashes with -mtriple=aarch64 and with urem i64 %i, 16 instead of the and. It does not crash when the two extracts use different index instructions, or when %i is noundef. Both extracts share the index %c, so scalarizeLoadExtract schedules two freezes of %i for the same user. The second one asserts because the first already rewrote %c. As far as I can tell, #213461 (keying NeedFreeze by the index instruction) fixes exactly this; it would be great to get it merged.

Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were assisted by an LLM; verified the reproducer on 9dce18f0e.

Assisted-by: Claude (Anthropic)

arie2026 avatar Nov 11 '26 20:11 arie2026